93 lines
2.1 KiB
Go
93 lines
2.1 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"log"
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/coder/websocket"
|
|
"github.com/coder/websocket/wsjson"
|
|
)
|
|
|
|
func ServeConnection(responseWriter http.ResponseWriter, request *http.Request) {
|
|
connection, err := websocket.Accept(responseWriter, request, nil)
|
|
if err != nil {
|
|
log.Printf("websocket accept error: %v", err)
|
|
return
|
|
}
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
|
|
defer cancel()
|
|
|
|
var (
|
|
isAuthenticated = false
|
|
continueConnection = true
|
|
client = Client{}
|
|
)
|
|
|
|
for continueConnection {
|
|
var clientMessage map[string]any
|
|
err := wsjson.Read(ctx, connection, &clientMessage)
|
|
if err != nil {
|
|
log.Printf("read error: %clientMessage", err)
|
|
return
|
|
}
|
|
|
|
if len(clientMessage) > 0 {
|
|
if isAuthenticated {
|
|
handleAuthenticatedMessage()
|
|
} else {
|
|
handleUnauthenticatedMessage(connection, &client, &clientMessage, &isAuthenticated, &continueConnection)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func sendMessageCloseIfTimeout(conn *websocket.Conn, message *map[string]any) {
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
|
|
defer cancel()
|
|
|
|
err := wsjson.Write(ctx, conn, message)
|
|
if err != nil {
|
|
if errors.Is(err, context.DeadlineExceeded) {
|
|
closeConnection(conn)
|
|
}
|
|
log.Printf("write error: %v", err)
|
|
}
|
|
}
|
|
|
|
func handleUnauthenticatedMessage(conn *websocket.Conn, client *Client, message *map[string]any, isAuthenticated *bool, continueConnection *bool) {
|
|
token, ok := (*message)["token"].(string)
|
|
if !ok {
|
|
var errmsg = map[string]any{
|
|
"type": WSServerResponse(BadMessage),
|
|
"message": "token required",
|
|
}
|
|
sendMessageCloseIfTimeout(conn, &errmsg)
|
|
return
|
|
}
|
|
|
|
err := SetClientFromToken(client, token)
|
|
if err != nil {
|
|
var errmsg = map[string]any{
|
|
"type": WSServerResponse(InvalidCredentials),
|
|
"message": "bad token",
|
|
}
|
|
sendMessageCloseIfTimeout(conn, &errmsg)
|
|
continueConnection = false
|
|
return
|
|
}
|
|
isAuthenticated = true
|
|
clientInCache, ok := ClientsMap[]
|
|
}
|
|
|
|
func handleAuthenticatedMessage() {
|
|
|
|
}
|
|
|
|
func closeConnection(conn *websocket.Conn) {
|
|
conn.Close(websocket.StatusNormalClosure, "closing connection")
|
|
}
|