This commit is contained in:
GitProtogen
2026-03-17 18:54:04 +01:00
parent bbd3d390c2
commit 912136e2bc
6 changed files with 104 additions and 52 deletions
+12 -14
View File
@@ -21,13 +21,9 @@ func ServeConnection(responseWriter http.ResponseWriter, request *http.Request)
ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
defer cancel()
var (
isAuthenticated = false
continueConnection = true
client = Client{}
)
client := Client{}
for continueConnection {
for {
var clientMessage map[string]any
err := wsjson.Read(ctx, connection, &clientMessage)
if err != nil {
@@ -36,10 +32,13 @@ func ServeConnection(responseWriter http.ResponseWriter, request *http.Request)
}
if len(clientMessage) > 0 {
if isAuthenticated {
if client.IsAuthenticated {
handleAuthenticatedMessage()
} else {
handleUnauthenticatedMessage(connection, &client, &clientMessage, &isAuthenticated, &continueConnection)
if !handleUnauthenticatedMessage(connection, &client, &clientMessage) {
closeConnection(connection)
return
}
}
}
}
@@ -58,7 +57,7 @@ func sendMessageCloseIfTimeout(conn *websocket.Conn, message *map[string]any) {
}
}
func handleUnauthenticatedMessage(conn *websocket.Conn, client *Client, message *map[string]any, isAuthenticated *bool, continueConnection *bool) {
func handleUnauthenticatedMessage(conn *websocket.Conn, client *Client, message *map[string]any) bool {
token, ok := (*message)["token"].(string)
if !ok {
var errmsg = map[string]any{
@@ -66,7 +65,7 @@ func handleUnauthenticatedMessage(conn *websocket.Conn, client *Client, message
"message": "token required",
}
sendMessageCloseIfTimeout(conn, &errmsg)
return
return false
}
err := SetClientFromToken(client, token)
@@ -76,11 +75,10 @@ func handleUnauthenticatedMessage(conn *websocket.Conn, client *Client, message
"message": "bad token",
}
sendMessageCloseIfTimeout(conn, &errmsg)
continueConnection = false
return
return false
}
isAuthenticated = true
clientInCache, ok := ClientsMap[]
client.IsAuthenticated = true
return true
}
func handleAuthenticatedMessage() {