Enhance WebSocket upgrade detection in Mihomo
- Updated the `isWebSocketUpgrade` function to improve handling of WebSocket upgrade requests by considering the presence of the "Sec-WebSocket-Key" header as a strong indicator. - Added a new test case in `TestIsWebSocketUpgrade` to validate the detection logic for requests with the "Sec-WebSocket-Key" header, ensuring comprehensive test coverage.
This commit is contained in:
@@ -36,10 +36,14 @@ func isWebSocketUpgrade(r *http.Request) bool {
|
||||
if r == nil {
|
||||
return false
|
||||
}
|
||||
// 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")
|
||||
// Be tolerant to proxy/header quirks:
|
||||
// - RFC path: Connection: upgrade + Upgrade: websocket
|
||||
// - Fallback: Sec-WebSocket-Key presence strongly indicates WS handshake.
|
||||
if headerHasToken(r.Header, "Upgrade", "websocket") &&
|
||||
headerHasToken(r.Header, "Connection", "upgrade") {
|
||||
return true
|
||||
}
|
||||
return strings.TrimSpace(r.Header.Get("Sec-WebSocket-Key")) != ""
|
||||
}
|
||||
|
||||
func headerHasToken(h http.Header, key, token string) bool {
|
||||
|
||||
@@ -53,4 +53,10 @@ func TestIsWebSocketUpgrade(t *testing.T) {
|
||||
if isWebSocketUpgrade(r2) {
|
||||
t.Fatal("expected non-websocket when Connection lacks upgrade")
|
||||
}
|
||||
|
||||
r3 := httptest.NewRequest(http.MethodGet, "http://gw/api/mtg/mihomo/traffic", nil)
|
||||
r3.Header.Set("Sec-WebSocket-Key", "dGhlIHNhbXBsZSBub25jZQ==")
|
||||
if !isWebSocketUpgrade(r3) {
|
||||
t.Fatal("expected websocket upgrade when Sec-WebSocket-Key is present")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user