Make websockets stop panicking about buggy clients

This commit is contained in:
Tracker-Friendly 2025-03-24 19:03:47 +00:00
parent 7f40ec7052
commit e92504b4ca

21
main.go
View file

@ -1,7 +1,6 @@
package main package main
import ( import (
"errors"
"log" "log"
"os" "os"
"strconv" "strconv"
@ -358,14 +357,14 @@ func main() {
// Kick over to a WebSocket // Kick over to a WebSocket
conn, err := upgrade.Upgrade(c.Writer, c.Request, nil) conn, err := upgrade.Upgrade(c.Writer, c.Request, nil)
if err != nil { if err != nil {
panic("Error upgrading connection: " + err.Error()) println("Error upgrading connection: " + err.Error())
} }
// Create a new nonce // Create a new nonce
nonce := make([]byte, 16) nonce := make([]byte, 16)
_, err = rand.Read(nonce) _, err = rand.Read(nonce)
if err != nil { if err != nil {
panic("Error generating nonce: " + err.Error()) println("Error generating nonce: " + err.Error())
} }
// Base64 encode the nonce // Base64 encode the nonce
@ -381,7 +380,7 @@ func main() {
"error": "Did not respond to nonce", "error": "Did not respond to nonce",
}) })
if err != nil { if err != nil {
panic("Error writing back to WebSocket: " + err.Error()) return
} }
} }
}() }()
@ -397,11 +396,7 @@ func main() {
_, nonceResponse, err := conn.ReadMessage() _, nonceResponse, err := conn.ReadMessage()
if err != nil { if err != nil {
if !errors.Is(err, websocket.ErrCloseSent) { return
panic("Error reading nonce response: " + err.Error())
} else {
return
}
} }
read = true read = true
@ -409,7 +404,7 @@ func main() {
jsonResponse := make(map[string]interface{}) jsonResponse := make(map[string]interface{})
err = json.Unmarshal(nonceResponse, &jsonResponse) err = json.Unmarshal(nonceResponse, &jsonResponse)
if err != nil { if err != nil {
panic("Error unmarshalling nonce response: " + err.Error()) println("Error unmarshalling nonce response: " + err.Error())
} }
heartbeatStop := make(chan struct{}) heartbeatStop := make(chan struct{})
@ -422,12 +417,12 @@ func main() {
// Write back to the WebSocket // Write back to the WebSocket
err = conn.WriteJSON(gin.H{"type": "success"}) err = conn.WriteJSON(gin.H{"type": "success"})
if err != nil { if err != nil {
panic("Error writing back to WebSocket: " + err.Error()) return
} }
err = conn.Close() err = conn.Close()
if err != nil { if err != nil {
panic("Error closing WebSocket: " + err.Error()) return
} }
// Tell the heartbeat loop to stop // Tell the heartbeat loop to stop
@ -458,7 +453,7 @@ func main() {
"error": "Did not respond to ping", "error": "Did not respond to ping",
}) })
if err != nil { if err != nil {
panic("Error writing back to WebSocket: " + err.Error()) return
} }
} }
}() }()