omg
This commit is contained in:
@@ -7,32 +7,27 @@ import (
|
|||||||
|
|
||||||
var (
|
var (
|
||||||
mu sync.RWMutex
|
mu sync.RWMutex
|
||||||
clients = make(map[uint32]Client)
|
clients = make(map[uint32]*Client)
|
||||||
ChatGroups = make(map[uint32]ChatGroup)
|
chatGroups = make(map[uint32]ChatGroup)
|
||||||
ClientsMap = make(map[uint32]map[uint32]*Client)
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func AddGroupToCache(chatGroup *ChatGroup) {
|
func AddGroupToCache(chatGroup *ChatGroup) {
|
||||||
mu.Lock()
|
mu.Lock()
|
||||||
defer mu.Unlock()
|
defer mu.Unlock()
|
||||||
ChatGroups[chatGroup.Id] = *chatGroup
|
chatGroups[chatGroup.Id] = *chatGroup
|
||||||
ClientsMap[chatGroup.Id] = make(map[uint32]*Client)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func RemoveGroupFromCache(chatGroup *ChatGroup) {
|
func RemoveGroupFromCache(chatGroup *ChatGroup) {
|
||||||
mu.Lock()
|
mu.Lock()
|
||||||
defer mu.Unlock()
|
defer mu.Unlock()
|
||||||
delete(ChatGroups, chatGroup.Id)
|
delete(chatGroups, chatGroup.Id)
|
||||||
delete(ClientsMap, chatGroup.Id)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func AddClientConnectionsToCache(client *Client) {
|
func AddClientConnectionsToCache(client *Client) {
|
||||||
mu.Lock()
|
mu.Lock()
|
||||||
defer mu.Unlock()
|
defer mu.Unlock()
|
||||||
for _, groupIn := range client.Groups {
|
for _, groupIn := range client.Groups {
|
||||||
if clients, ok := ClientsMap[groupIn.Id]; ok {
|
chatGroups[groupIn.Id].Members[client.Id] = client
|
||||||
clients[client.Id] = client
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -40,16 +35,14 @@ func RemoveClientConnectionsToCache(client *Client) {
|
|||||||
mu.Lock()
|
mu.Lock()
|
||||||
defer mu.Unlock()
|
defer mu.Unlock()
|
||||||
for _, groupIn := range client.Groups {
|
for _, groupIn := range client.Groups {
|
||||||
if clients, ok := ClientsMap[groupIn.Id]; ok {
|
delete(chatGroups[groupIn.Id].Members, client.Id)
|
||||||
delete(clients, client.Id)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetClientFromId(id uint32) (*Client, error) {
|
func GetClientFromId(id uint32) (*Client, error) {
|
||||||
client, ok := &clients[id]
|
client, ok := clients[id]
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, errors.New("No such user")
|
return nil, errors.New("no such user")
|
||||||
}
|
}
|
||||||
return client, nil
|
return client, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,19 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "net/http"
|
||||||
|
|
||||||
|
func isMethodAllowed(response *http.ResponseWriter, request *http.Request) bool {
|
||||||
|
if request.Method != http.MethodPost {
|
||||||
|
http.Error(*response, "POST only", http.StatusMethodNotAllowed)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func RegisterHandler(response http.ResponseWriter, request *http.Request) {
|
||||||
|
if !isMethodAllowed(&response, request) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx := request.Context()
|
||||||
|
}
|
||||||
@@ -15,7 +15,7 @@ type Client struct {
|
|||||||
|
|
||||||
type ChatGroup struct {
|
type ChatGroup struct {
|
||||||
Name string
|
Name string
|
||||||
Members [32]*Client
|
|
||||||
Id uint32
|
Id uint32
|
||||||
|
Members map[uint32]*Client
|
||||||
Color [3]byte
|
Color [3]byte
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,8 +20,8 @@ func GetToken(client *Client) (string, error) {
|
|||||||
return token.SignedString(secretKey)
|
return token.SignedString(secretKey)
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetClientIdFromToken(token *string) (uint32, error) {
|
func GetClientIdFromToken(token string) (uint32, error) {
|
||||||
parsed, err := jwt.ParseWithClaims(*token, &jwt.RegisteredClaims{}, func(t *jwt.Token) (any, error) {
|
parsed, err := jwt.ParseWithClaims(token, &jwt.RegisteredClaims{}, func(t *jwt.Token) (any, error) {
|
||||||
if _, ok := t.Method.(*jwt.SigningMethodHMAC); !ok {
|
if _, ok := t.Method.(*jwt.SigningMethodHMAC); !ok {
|
||||||
return nil, jwt.ErrSignatureInvalid
|
return nil, jwt.ErrSignatureInvalid
|
||||||
}
|
}
|
||||||
|
|||||||
+12
-1
@@ -68,7 +68,7 @@ func handleUnauthenticatedMessage(conn *websocket.Conn, client *Client, message
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
err := SetClientFromToken(client, token)
|
clientId, err := GetClientIdFromToken(token)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
var errmsg = map[string]any{
|
var errmsg = map[string]any{
|
||||||
"type": WSServerResponse(InvalidCredentials),
|
"type": WSServerResponse(InvalidCredentials),
|
||||||
@@ -77,6 +77,17 @@ func handleUnauthenticatedMessage(conn *websocket.Conn, client *Client, message
|
|||||||
sendMessageCloseIfTimeout(conn, &errmsg)
|
sendMessageCloseIfTimeout(conn, &errmsg)
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
client, err = GetClientFromId(clientId)
|
||||||
|
if err != nil {
|
||||||
|
var errmsg = map[string]any{
|
||||||
|
"type": WSServerResponse(InvalidCredentials),
|
||||||
|
"message": "bad token",
|
||||||
|
}
|
||||||
|
sendMessageCloseIfTimeout(conn, &errmsg)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
client.IsAuthenticated = true
|
client.IsAuthenticated = true
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user