From e92504b4ca31e04d2453f8e7d019b6e91ebea509 Mon Sep 17 00:00:00 2001 From: arzumify Date: Mon, 24 Mar 2025 19:03:47 +0000 Subject: [PATCH] Make websockets stop panicking about buggy clients --- main.go | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/main.go b/main.go index 42164fd..1fc1dcd 100644 --- a/main.go +++ b/main.go @@ -1,7 +1,6 @@ package main import ( - "errors" "log" "os" "strconv" @@ -358,14 +357,14 @@ func main() { // Kick over to a WebSocket conn, err := upgrade.Upgrade(c.Writer, c.Request, nil) if err != nil { - panic("Error upgrading connection: " + err.Error()) + println("Error upgrading connection: " + err.Error()) } // Create a new nonce nonce := make([]byte, 16) _, err = rand.Read(nonce) if err != nil { - panic("Error generating nonce: " + err.Error()) + println("Error generating nonce: " + err.Error()) } // Base64 encode the nonce @@ -381,7 +380,7 @@ func main() { "error": "Did not respond to nonce", }) if err != nil { - panic("Error writing back to WebSocket: " + err.Error()) + return } } }() @@ -397,11 +396,7 @@ func main() { _, nonceResponse, err := conn.ReadMessage() if err != nil { - if !errors.Is(err, websocket.ErrCloseSent) { - panic("Error reading nonce response: " + err.Error()) - } else { - return - } + return } read = true @@ -409,7 +404,7 @@ func main() { jsonResponse := make(map[string]interface{}) err = json.Unmarshal(nonceResponse, &jsonResponse) if err != nil { - panic("Error unmarshalling nonce response: " + err.Error()) + println("Error unmarshalling nonce response: " + err.Error()) } heartbeatStop := make(chan struct{}) @@ -422,12 +417,12 @@ func main() { // Write back to the WebSocket err = conn.WriteJSON(gin.H{"type": "success"}) if err != nil { - panic("Error writing back to WebSocket: " + err.Error()) + return } err = conn.Close() if err != nil { - panic("Error closing WebSocket: " + err.Error()) + return } // Tell the heartbeat loop to stop @@ -458,7 +453,7 @@ func main() { "error": "Did not respond to ping", }) if err != nil { - panic("Error writing back to WebSocket: " + err.Error()) + return } } }()