system ready for adding logging and registery
This commit is contained in:
@@ -12,21 +12,29 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type wsServer struct {
|
type wsServer struct {
|
||||||
OnOpen func(conn *websocket.Conn)
|
OnOpen func(ctx context.Context, conn *websocket.Conn)
|
||||||
OnClose func(conn *websocket.Conn, err error)
|
OnClose func(ctx context.Context, conn *websocket.Conn, err error)
|
||||||
OnMessage func(conn *websocket.Conn, msg map[string]any)
|
OnMessage func(ctx context.Context, conn *websocket.Conn, msg map[string]any)
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
unauthenticatedConnections []*websocket.Conn
|
unauthenticatedConnections []*websocket.Conn
|
||||||
authenticatedConnections []*websocket.Conn
|
authenticatedConnections []AuthConnection
|
||||||
mu sync.Mutex
|
mu sync.Mutex
|
||||||
)
|
)
|
||||||
|
|
||||||
func removeConnection(conn *websocket.Conn) {
|
func removeConnectionCache(conn *websocket.Conn) {
|
||||||
mu.Lock()
|
mu.Lock()
|
||||||
defer mu.Unlock()
|
defer mu.Unlock()
|
||||||
if isConnectionAuthenticated(conn) {
|
if getConnectionDataIfAuth(conn) != nil {
|
||||||
|
for i, c := range authenticatedConnections {
|
||||||
|
if c.connection == conn {
|
||||||
|
authenticatedConnections[i] = authenticatedConnections[len(authenticatedConnections)-1]
|
||||||
|
authenticatedConnections = authenticatedConnections[:len(authenticatedConnections)-1]
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
for i, c := range unauthenticatedConnections {
|
for i, c := range unauthenticatedConnections {
|
||||||
if c == conn {
|
if c == conn {
|
||||||
unauthenticatedConnections[i] = unauthenticatedConnections[len(unauthenticatedConnections)-1]
|
unauthenticatedConnections[i] = unauthenticatedConnections[len(unauthenticatedConnections)-1]
|
||||||
@@ -37,15 +45,24 @@ func removeConnection(conn *websocket.Conn) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func isConnectionAuthenticated(conn *websocket.Conn) bool {
|
func getConnectionDataIfAuth(conn *websocket.Conn) *AuthConnection {
|
||||||
mu.Lock()
|
mu.Lock()
|
||||||
defer mu.Unlock()
|
defer mu.Unlock()
|
||||||
for _, c := range unauthenticatedConnections {
|
for _, c := range authenticatedConnections {
|
||||||
if c == conn {
|
if c.connection == conn {
|
||||||
return true
|
return &c
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false
|
return nil
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func sendAndCloseIfFails(conn *websocket.Conn, message map[string]any) {
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
if err := wsjson.Write(ctx, conn, message); err != nil {
|
||||||
|
conn.Close(websocket.StatusGoingAway, "Write error")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *wsServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
func (s *wsServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
@@ -58,11 +75,12 @@ func (s *wsServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
defer conn.CloseNow()
|
defer conn.CloseNow()
|
||||||
|
|
||||||
if s.OnOpen != nil {
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
s.OnOpen(conn)
|
defer cancel()
|
||||||
}
|
|
||||||
|
|
||||||
ctx := r.Context()
|
if s.OnOpen != nil {
|
||||||
|
s.OnOpen(ctx, conn)
|
||||||
|
}
|
||||||
|
|
||||||
var readErr error
|
var readErr error
|
||||||
for {
|
for {
|
||||||
@@ -71,35 +89,72 @@ func (s *wsServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
if s.OnMessage != nil {
|
if s.OnMessage != nil {
|
||||||
s.OnMessage(conn, msg)
|
s.OnMessage(ctx, conn, msg)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
cancel() // cancel before OnClose so any in-flight queries are canceled first
|
||||||
|
|
||||||
if s.OnClose != nil {
|
if s.OnClose != nil {
|
||||||
s.OnClose(conn, readErr)
|
s.OnClose(ctx, conn, readErr)
|
||||||
}
|
}
|
||||||
|
|
||||||
conn.Close(websocket.StatusNormalClosure, "done")
|
conn.Close(websocket.StatusNormalClosure, "done")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func handleUnauthenticatedMessage(ctx context.Context, conn *websocket.Conn, msg map[string]any) {
|
||||||
|
token := msg["token"].(string)
|
||||||
|
subject, err := GetSubject(token)
|
||||||
|
if err != nil {
|
||||||
|
log.Println("invalid or expired token:", err)
|
||||||
|
conn.Close(websocket.StatusPolicyViolation, "invalid token")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
user, err := GetUserData(ctx, subject)
|
||||||
|
if err != nil {
|
||||||
|
conn.Close(websocket.StatusPolicyViolation, "invalid token")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
mu.Lock()
|
||||||
|
authenticatedConnections = append(authenticatedConnections, AuthConnection{connection: conn, user: user})
|
||||||
|
mu.Unlock()
|
||||||
|
sendAndCloseIfFails(conn, map[string]any{
|
||||||
|
"authAs": user.Name,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleAuthenticatedMessage(conn *websocket.Conn, msg map[string]any) {
|
||||||
|
message := msg["message"].(string)
|
||||||
|
if message == "" {
|
||||||
|
sendAndCloseIfFails(conn, map[string]any{
|
||||||
|
"error": "no message",
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
InitDatabase(context.Background())
|
InitDatabase(context.Background())
|
||||||
srv := &wsServer{
|
srv := &wsServer{
|
||||||
OnOpen: func(conn *websocket.Conn) {
|
OnOpen: func(ctx context.Context, conn *websocket.Conn) {
|
||||||
log.Println("client connected")
|
log.Println("client connected")
|
||||||
mu.Lock()
|
if getConnectionDataIfAuth(conn) != nil {
|
||||||
unauthenticatedConnections = append(unauthenticatedConnections, conn)
|
mu.Lock()
|
||||||
mu.Unlock()
|
unauthenticatedConnections = append(unauthenticatedConnections, conn)
|
||||||
|
mu.Unlock()
|
||||||
|
}
|
||||||
},
|
},
|
||||||
OnClose: func(conn *websocket.Conn, err error) {
|
OnClose: func(ctx context.Context, conn *websocket.Conn, err error) {
|
||||||
log.Println("client disconnected:", err)
|
log.Println("client disconnected:", err)
|
||||||
|
removeConnectionCache(conn)
|
||||||
},
|
},
|
||||||
OnMessage: func(conn *websocket.Conn, msg map[string]any) {
|
OnMessage: func(ctx context.Context, conn *websocket.Conn, msg map[string]any) {
|
||||||
log.Printf("received: %v\n", msg)
|
log.Printf("received: %v\n", msg)
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
authConnOrNil := getConnectionDataIfAuth(conn)
|
||||||
defer cancel()
|
if authConnOrNil == nil {
|
||||||
if err := wsjson.Write(ctx, conn, msg); err != nil {
|
handleUnauthenticatedMessage(ctx, conn, msg)
|
||||||
removeConnection(conn)
|
} else {
|
||||||
|
handleAuthenticatedMessage(conn, msg)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -10,7 +10,7 @@ type User struct {
|
|||||||
IsPasswordHashed bool
|
IsPasswordHashed bool
|
||||||
}
|
}
|
||||||
|
|
||||||
type authConnection struct {
|
type AuthConnection struct {
|
||||||
connection *websocket.Conn
|
connection *websocket.Conn
|
||||||
user User
|
user User
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user