mirror of
https://github.com/MetaCubeX/mihomo.git
synced 2024-11-16 11:42:43 +08:00
feat: add ws-path
to vmess listener
This commit is contained in:
parent
5ff4473083
commit
791ecfbb32
|
@ -936,6 +936,7 @@ listeners:
|
||||||
- username: 1
|
- username: 1
|
||||||
uuid: 9d0cb9d0-964f-4ef6-897d-6c6b3ccf9e68
|
uuid: 9d0cb9d0-964f-4ef6-897d-6c6b3ccf9e68
|
||||||
alterId: 1
|
alterId: 1
|
||||||
|
# ws-path: "/" # 如果不为空则开启websocket传输层
|
||||||
|
|
||||||
- name: tuic-in-1
|
- name: tuic-in-1
|
||||||
type: tuic
|
type: tuic
|
||||||
|
|
|
@ -14,6 +14,7 @@ type VmessServer struct {
|
||||||
Enable bool
|
Enable bool
|
||||||
Listen string
|
Listen string
|
||||||
Users []VmessUser
|
Users []VmessUser
|
||||||
|
WsPath string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t VmessServer) String() string {
|
func (t VmessServer) String() string {
|
||||||
|
|
|
@ -10,6 +10,7 @@ import (
|
||||||
type VmessOption struct {
|
type VmessOption struct {
|
||||||
BaseOption
|
BaseOption
|
||||||
Users []VmessUser `inbound:"users"`
|
Users []VmessUser `inbound:"users"`
|
||||||
|
WsPath string `inbound:"ws-path,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type VmessUser struct {
|
type VmessUser struct {
|
||||||
|
@ -49,6 +50,7 @@ func NewVmess(options *VmessOption) (*Vmess, error) {
|
||||||
Enable: true,
|
Enable: true,
|
||||||
Listen: base.RawAddress(),
|
Listen: base.RawAddress(),
|
||||||
Users: users,
|
Users: users,
|
||||||
|
WsPath: options.WsPath,
|
||||||
},
|
},
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,7 @@ package sing_vmess
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"net"
|
"net"
|
||||||
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
@ -12,6 +13,7 @@ import (
|
||||||
LC "github.com/Dreamacro/clash/listener/config"
|
LC "github.com/Dreamacro/clash/listener/config"
|
||||||
"github.com/Dreamacro/clash/listener/sing"
|
"github.com/Dreamacro/clash/listener/sing"
|
||||||
"github.com/Dreamacro/clash/ntp"
|
"github.com/Dreamacro/clash/ntp"
|
||||||
|
clashVMess "github.com/Dreamacro/clash/transport/vmess"
|
||||||
|
|
||||||
vmess "github.com/metacubex/sing-vmess"
|
vmess "github.com/metacubex/sing-vmess"
|
||||||
"github.com/sagernet/sing/common"
|
"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}
|
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, ",") {
|
for _, addr := range strings.Split(config.Listen, ",") {
|
||||||
addr := addr
|
addr := addr
|
||||||
|
|
||||||
|
@ -76,6 +92,10 @@ func New(config LC.VmessServer, tunnel C.Tunnel, additions ...inbound.Addition)
|
||||||
sl.listeners = append(sl.listeners, l)
|
sl.listeners = append(sl.listeners, l)
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
|
if httpMux != nil {
|
||||||
|
_ = http.Serve(l, httpMux)
|
||||||
|
return
|
||||||
|
}
|
||||||
for {
|
for {
|
||||||
c, err := l.Accept()
|
c, err := l.Accept()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -118,12 +118,17 @@ func (wsc *websocketConn) WriteBuffer(buffer *buf.Buffer) error {
|
||||||
var headerLen int
|
var headerLen int
|
||||||
headerLen += 1 // FIN / RSV / OPCODE
|
headerLen += 1 // FIN / RSV / OPCODE
|
||||||
headerLen += payloadBitLength
|
headerLen += payloadBitLength
|
||||||
|
if wsc.state.ClientSide() {
|
||||||
headerLen += 4 // MASK KEY
|
headerLen += 4 // MASK KEY
|
||||||
|
}
|
||||||
|
|
||||||
header := buffer.ExtendHeader(headerLen)
|
header := buffer.ExtendHeader(headerLen)
|
||||||
_ = header[2] // bounds check hint to compiler
|
|
||||||
header[0] = byte(ws.OpBinary) | 0x80
|
header[0] = byte(ws.OpBinary) | 0x80
|
||||||
|
if wsc.state.ClientSide() {
|
||||||
header[1] = 1 << 7
|
header[1] = 1 << 7
|
||||||
|
} else {
|
||||||
|
header[1] = 0
|
||||||
|
}
|
||||||
|
|
||||||
if dataLen < 126 {
|
if dataLen < 126 {
|
||||||
header[1] |= byte(dataLen)
|
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) {
|
func StreamUpgradedWebsocketConn(w http.ResponseWriter, r *http.Request) (net.Conn, error) {
|
||||||
edBuf, responseHeader := decodeXray0rtt(r.Header)
|
edBuf, responseHeader := decodeXray0rtt(r.Header)
|
||||||
wsConn, rw, _, err := ws.HTTPUpgrader{
|
wsConn, rw, _, err := ws.HTTPUpgrader{Header: responseHeader}.Upgrade(r, w)
|
||||||
Header: responseHeader,
|
|
||||||
}.Upgrade(r, w)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
conn := newWebsocketConn(wsConn, rw.Reader, ws.StateServerSide)
|
conn := newWebsocketConn(wsConn, rw.Reader, ws.StateServerSide)
|
||||||
if len(edBuf) > 0 {
|
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 {
|
type websocketWithReaderConn struct {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user