Add WebSocket upgrade handling and tests in Mihomo
Publish telemt-api gateway Docker image / test (push) Successful in 26s
Publish telemt-api gateway Docker image / build-and-push (push) Successful in 2m0s

- Implemented enhanced logic in `isWebSocketUpgrade` to accurately determine WebSocket upgrade requests by checking both "Connection" and "Upgrade" headers.
- Added a new test function `TestIsWebSocketUpgrade` to validate the WebSocket upgrade detection logic, ensuring correct behavior for various header configurations.
This commit is contained in:
Denozordec
2026-03-31 10:22:37 +07:00
parent 6c1f8a8a37
commit 1686840b0e
2 changed files with 30 additions and 1 deletions
+15 -1
View File
@@ -36,7 +36,21 @@ func isWebSocketUpgrade(r *http.Request) bool {
if r == nil {
return false
}
return strings.EqualFold(r.Header.Get("Upgrade"), "websocket")
// Some clients/proxies pass comma-separated tokens or extra spaces.
// Treat request as WS only when both headers contain required upgrade tokens.
return headerHasToken(r.Header, "Connection", "upgrade") &&
headerHasToken(r.Header, "Upgrade", "websocket")
}
func headerHasToken(h http.Header, key, token string) bool {
for _, v := range h.Values(key) {
for _, part := range strings.Split(v, ",") {
if strings.EqualFold(strings.TrimSpace(part), token) {
return true
}
}
}
return false
}
func newMihomoWebSocketReverseProxy(