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
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
}
}
}()