mirror of
https://github.com/MetaCubeX/mihomo.git
synced 2026-12-04 02:38:31 +00:00
fix: validate websocket upgrade and Hijacker before writing 101 (#3538)
This commit is contained in:
1 parent
72214858cf
commit
4ffa6eea4b
4 files changed
+25
-8
No files matched your search
@@ -169,7 +169,11 @@ func New(config LC.VlessServer, lc C.InboundListenConfig, tunnel C.Tunnel, addit
|
||||
httpMux.HandleFunc(config.WsPath, func(w http.ResponseWriter, r *http.Request) {
|
||||
conn, err := mihomoVMess.StreamUpgradedWebsocketConn(w, r)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), 500)
|
||||
if errors.Is(err, mihomoVMess.ErrNotWebSocketUpgrade) {
|
||||
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
sl.HandleConn(conn, tunnel, additions...)
|
||||
|
||||
@@ -203,7 +203,11 @@ func New(config LC.VmessServer, lc C.InboundListenConfig, tunnel C.Tunnel, addit
|
||||
httpMux.HandleFunc(config.WsPath, func(w http.ResponseWriter, r *http.Request) {
|
||||
conn, err := mihomoVMess.StreamUpgradedWebsocketConn(w, r)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), 500)
|
||||
if errors.Is(err, mihomoVMess.ErrNotWebSocketUpgrade) {
|
||||
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
sl.HandleConn(conn, tunnel, additions...)
|
||||
|
||||
@@ -165,7 +165,11 @@ func New(config LC.TrojanServer, lc C.InboundListenConfig, tunnel C.Tunnel, addi
|
||||
httpMux.HandleFunc(config.WsPath, func(w http.ResponseWriter, r *http.Request) {
|
||||
conn, err := mihomoVMess.StreamUpgradedWebsocketConn(w, r)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), 500)
|
||||
if errors.Is(err, mihomoVMess.ErrNotWebSocketUpgrade) {
|
||||
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
sl.HandleConn(conn, tunnel, additions...)
|
||||
|
||||
@@ -542,8 +542,10 @@ func decodeXray0rtt(requestHeader http.Header) []byte {
|
||||
return nil
|
||||
}
|
||||
|
||||
var ErrNotWebSocketUpgrade = errors.New("not a websocket upgrade request")
|
||||
|
||||
func IsWebSocketUpgrade(r *http.Request) bool {
|
||||
return r.Header.Get("Upgrade") == "websocket"
|
||||
return strings.EqualFold(r.Header.Get("Upgrade"), "websocket")
|
||||
}
|
||||
|
||||
func IsV2rayHttpUpdate(r *http.Request) bool {
|
||||
@@ -554,6 +556,13 @@ func StreamUpgradedWebsocketConn(w http.ResponseWriter, r *http.Request) (net.Co
|
||||
var conn net.Conn
|
||||
var rw *bufio.ReadWriter
|
||||
var err error
|
||||
if !IsWebSocketUpgrade(r) {
|
||||
return nil, ErrNotWebSocketUpgrade
|
||||
}
|
||||
hijacker, canHijack := w.(http.Hijacker)
|
||||
if !canHijack {
|
||||
return nil, errors.New("invalid connection, maybe HTTP/2")
|
||||
}
|
||||
isRaw := IsV2rayHttpUpdate(r)
|
||||
w.Header().Set("Connection", "upgrade")
|
||||
w.Header().Set("Upgrade", "websocket")
|
||||
@@ -567,10 +576,6 @@ func StreamUpgradedWebsocketConn(w http.ResponseWriter, r *http.Request) (net.Co
|
||||
return nil, fmt.Errorf("flush response: %w", err)
|
||||
}
|
||||
}
|
||||
hijacker, canHijack := w.(http.Hijacker)
|
||||
if !canHijack {
|
||||
return nil, errors.New("invalid connection, maybe HTTP/2")
|
||||
}
|
||||
conn, rw, err = hijacker.Hijack()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("hijack failed: %w", err)
|
||||
|
||||
Reference in new issue
Block a user