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 {
|
||||
|
||||
Reference in New Issue
Block a user