diff --git a/docs/config.yaml b/docs/config.yaml index c2bdc263..030328b6 100644 --- a/docs/config.yaml +++ b/docs/config.yaml @@ -936,6 +936,7 @@ listeners: - username: 1 uuid: 9d0cb9d0-964f-4ef6-897d-6c6b3ccf9e68 alterId: 1 + # ws-path: "/" # 如果不为空则开启websocket传输层 - name: tuic-in-1 type: tuic diff --git a/listener/config/vmess.go b/listener/config/vmess.go index cc49433e..88bde9a4 100644 --- a/listener/config/vmess.go +++ b/listener/config/vmess.go @@ -14,6 +14,7 @@ type VmessServer struct { Enable bool Listen string Users []VmessUser + WsPath string } func (t VmessServer) String() string { diff --git a/listener/inbound/vmess.go b/listener/inbound/vmess.go index fa2c30f1..36bb208b 100644 --- a/listener/inbound/vmess.go +++ b/listener/inbound/vmess.go @@ -9,7 +9,8 @@ import ( type VmessOption struct { BaseOption - Users []VmessUser `inbound:"users"` + Users []VmessUser `inbound:"users"` + WsPath string `inbound:"ws-path,omitempty"` } type VmessUser struct { @@ -49,6 +50,7 @@ func NewVmess(options *VmessOption) (*Vmess, error) { Enable: true, Listen: base.RawAddress(), Users: users, + WsPath: options.WsPath, }, }, nil } diff --git a/listener/sing_vmess/server.go b/listener/sing_vmess/server.go index d28213c8..96d713c9 100644 --- a/listener/sing_vmess/server.go +++ b/listener/sing_vmess/server.go @@ -3,6 +3,7 @@ package sing_vmess import ( "context" "net" + "net/http" "net/url" "strings" @@ -12,6 +13,7 @@ import ( LC "github.com/Dreamacro/clash/listener/config" "github.com/Dreamacro/clash/listener/sing" "github.com/Dreamacro/clash/ntp" + clashVMess "github.com/Dreamacro/clash/transport/vmess" vmess "github.com/metacubex/sing-vmess" "github.com/sagernet/sing/common" @@ -65,6 +67,20 @@ func New(config LC.VmessServer, tunnel C.Tunnel, additions ...inbound.Addition) sl = &Listener{false, config, nil, service} + var httpMux *http.ServeMux + + if config.WsPath != "" { + httpMux = http.NewServeMux() + httpMux.HandleFunc(config.WsPath, func(w http.ResponseWriter, r *http.Request) { + conn, err := clashVMess.StreamUpgradedWebsocketConn(w, r) + if err != nil { + http.Error(w, err.Error(), 500) + return + } + sl.HandleConn(conn, tunnel) + }) + } + for _, addr := range strings.Split(config.Listen, ",") { addr := addr @@ -76,6 +92,10 @@ func New(config LC.VmessServer, tunnel C.Tunnel, additions ...inbound.Addition) sl.listeners = append(sl.listeners, l) go func() { + if httpMux != nil { + _ = http.Serve(l, httpMux) + return + } for { c, err := l.Accept() if err != nil { diff --git a/transport/vmess/websocket.go b/transport/vmess/websocket.go index 2b26eb1a..6d679e29 100644 --- a/transport/vmess/websocket.go +++ b/transport/vmess/websocket.go @@ -118,12 +118,17 @@ func (wsc *websocketConn) WriteBuffer(buffer *buf.Buffer) error { var headerLen int headerLen += 1 // FIN / RSV / OPCODE headerLen += payloadBitLength - headerLen += 4 // MASK KEY + if wsc.state.ClientSide() { + headerLen += 4 // MASK KEY + } header := buffer.ExtendHeader(headerLen) - _ = header[2] // bounds check hint to compiler header[0] = byte(ws.OpBinary) | 0x80 - header[1] = 1 << 7 + if wsc.state.ClientSide() { + header[1] = 1 << 7 + } else { + header[1] = 0 + } if dataLen < 126 { header[1] |= byte(dataLen) @@ -456,17 +461,15 @@ func decodeXray0rtt(requestHeader http.Header) ([]byte, http.Header) { func StreamUpgradedWebsocketConn(w http.ResponseWriter, r *http.Request) (net.Conn, error) { edBuf, responseHeader := decodeXray0rtt(r.Header) - wsConn, rw, _, err := ws.HTTPUpgrader{ - Header: responseHeader, - }.Upgrade(r, w) + wsConn, rw, _, err := ws.HTTPUpgrader{Header: responseHeader}.Upgrade(r, w) if err != nil { return nil, err } conn := newWebsocketConn(wsConn, rw.Reader, ws.StateServerSide) if len(edBuf) > 0 { - return &websocketWithReaderConn{conn, io.MultiReader(bytes.NewReader(edBuf), conn)}, nil + return N.NewDeadlineConn(&websocketWithReaderConn{conn, io.MultiReader(bytes.NewReader(edBuf), conn)}), nil } - return conn, nil + return N.NewDeadlineConn(conn), nil } type websocketWithReaderConn struct {