mirror of
https://github.com/MetaCubeX/mihomo.git
synced 2026-12-06 20:18:33 +00:00
feat: add WebSocket support to GOST relay (#3507)
Co-authored-by: Zou <zou@outlook.com>
This commit is contained in:
1 parent
fc3bd70369
commit
c7af25cee0
5 files changed
+200
-87
No files matched your search
@@ -18,22 +18,29 @@ type GostRelay struct {
|
||||
|
||||
type GostRelayOption struct {
|
||||
BasicOption
|
||||
Name string `proxy:"name"`
|
||||
Server string `proxy:"server"`
|
||||
Port int `proxy:"port"`
|
||||
Forward bool `proxy:"forward,omitempty"`
|
||||
UDP bool `proxy:"udp,omitempty"`
|
||||
TLS bool `proxy:"tls,omitempty"`
|
||||
Mux bool `proxy:"mux,omitempty"`
|
||||
SNI string `proxy:"sni,omitempty"`
|
||||
Username string `proxy:"username,omitempty"`
|
||||
Password string `proxy:"password,omitempty"`
|
||||
SkipCertVerify bool `proxy:"skip-cert-verify,omitempty"`
|
||||
NameCertVerify string `proxy:"name-cert-verify,omitempty"`
|
||||
Fingerprint string `proxy:"fingerprint,omitempty"`
|
||||
Certificate string `proxy:"certificate,omitempty"`
|
||||
PrivateKey string `proxy:"private-key,omitempty"`
|
||||
ClientFingerprint string `proxy:"client-fingerprint,omitempty"`
|
||||
Name string `proxy:"name"`
|
||||
Server string `proxy:"server"`
|
||||
Port int `proxy:"port"`
|
||||
Forward bool `proxy:"forward,omitempty"`
|
||||
UDP bool `proxy:"udp,omitempty"`
|
||||
WS bool `proxy:"ws,omitempty"`
|
||||
WSOpts GostRelayWSOptions `proxy:"ws-opts,omitempty"`
|
||||
TLS bool `proxy:"tls,omitempty"`
|
||||
Mux bool `proxy:"mux,omitempty"`
|
||||
SNI string `proxy:"sni,omitempty"`
|
||||
Username string `proxy:"username,omitempty"`
|
||||
Password string `proxy:"password,omitempty"`
|
||||
SkipCertVerify bool `proxy:"skip-cert-verify,omitempty"`
|
||||
NameCertVerify string `proxy:"name-cert-verify,omitempty"`
|
||||
Fingerprint string `proxy:"fingerprint,omitempty"`
|
||||
Certificate string `proxy:"certificate,omitempty"`
|
||||
PrivateKey string `proxy:"private-key,omitempty"`
|
||||
ClientFingerprint string `proxy:"client-fingerprint,omitempty"`
|
||||
}
|
||||
|
||||
type GostRelayWSOptions struct {
|
||||
Path string `proxy:"path,omitempty"`
|
||||
Headers map[string]string `proxy:"headers,omitempty"`
|
||||
}
|
||||
|
||||
func (g *GostRelay) DialContext(ctx context.Context, metadata *C.Metadata) (_ C.Conn, err error) {
|
||||
@@ -95,6 +102,9 @@ func NewGostRelay(option GostRelayOption) (*GostRelay, error) {
|
||||
Server: option.Server,
|
||||
Port: option.Port,
|
||||
Forward: option.Forward,
|
||||
WS: option.WS,
|
||||
WSPath: option.WSOpts.Path,
|
||||
WSHeaders: option.WSOpts.Headers,
|
||||
TLS: option.TLS,
|
||||
Mux: option.Mux,
|
||||
SNI: option.SNI,
|
||||
|
||||
@@ -373,10 +373,11 @@ func NewShadowSocks(option ShadowSocksOption) (*ShadowSocks, error) {
|
||||
}
|
||||
obfsMode = opts.Mode
|
||||
gostOption = &gost.Option{
|
||||
Host: opts.Host,
|
||||
Path: opts.Path,
|
||||
Headers: opts.Headers,
|
||||
Mux: opts.Mux,
|
||||
Host: opts.Host,
|
||||
Path: opts.Path,
|
||||
Headers: opts.Headers,
|
||||
Mux: opts.Mux,
|
||||
MuxKeepAliveDisabled: true,
|
||||
}
|
||||
|
||||
if opts.TLS {
|
||||
|
||||
+7
-2
@@ -594,8 +594,13 @@ proxies: # socks5
|
||||
port: 443
|
||||
udp: true
|
||||
tls: true
|
||||
# mux: true # relay+mtls when tls is also true
|
||||
# sni: relay.example.com
|
||||
# mux: true # with tls enabled and ws disabled, this selects relay+mtls
|
||||
# ws: true # enables WebSocket; combine with tls/mux for relay+wss/mws/mwss
|
||||
# ws-opts:
|
||||
# path: /ws # GOST default; set to match the listener's ws.path
|
||||
# headers:
|
||||
# Host: relay.example.com
|
||||
# sni: relay.example.com # optional; defaults to ws-opts.headers.Host, or server if absent
|
||||
# username: user
|
||||
# password: pass
|
||||
# client-fingerprint: chrome
|
||||
|
||||
+136
-47
@@ -49,20 +49,23 @@ const (
|
||||
|
||||
// RelayOption is the common gost relay transport configuration that can wrap any TCP outbound dial.
|
||||
type RelayOption struct {
|
||||
Server string `proxy:"server,omitempty"`
|
||||
Port int `proxy:"port,omitempty"`
|
||||
Forward bool `proxy:"forward,omitempty"`
|
||||
TLS bool `proxy:"tls,omitempty"`
|
||||
Mux bool `proxy:"mux,omitempty"`
|
||||
SNI string `proxy:"sni,omitempty"`
|
||||
Username string `proxy:"username,omitempty"`
|
||||
Password string `proxy:"password,omitempty"`
|
||||
SkipCertVerify bool `proxy:"skip-cert-verify,omitempty"`
|
||||
NameCertVerify string `proxy:"name-cert-verify,omitempty"`
|
||||
Fingerprint string `proxy:"fingerprint,omitempty"`
|
||||
Certificate string `proxy:"certificate,omitempty"`
|
||||
PrivateKey string `proxy:"private-key,omitempty"`
|
||||
ClientFingerprint string `proxy:"client-fingerprint,omitempty"`
|
||||
Server string `proxy:"server,omitempty"`
|
||||
Port int `proxy:"port,omitempty"`
|
||||
Forward bool `proxy:"forward,omitempty"`
|
||||
WS bool `proxy:"ws,omitempty"`
|
||||
WSPath string `proxy:"ws-path,omitempty"`
|
||||
WSHeaders map[string]string `proxy:"ws-headers,omitempty"`
|
||||
TLS bool `proxy:"tls,omitempty"`
|
||||
Mux bool `proxy:"mux,omitempty"`
|
||||
SNI string `proxy:"sni,omitempty"`
|
||||
Username string `proxy:"username,omitempty"`
|
||||
Password string `proxy:"password,omitempty"`
|
||||
SkipCertVerify bool `proxy:"skip-cert-verify,omitempty"`
|
||||
NameCertVerify string `proxy:"name-cert-verify,omitempty"`
|
||||
Fingerprint string `proxy:"fingerprint,omitempty"`
|
||||
Certificate string `proxy:"certificate,omitempty"`
|
||||
PrivateKey string `proxy:"private-key,omitempty"`
|
||||
ClientFingerprint string `proxy:"client-fingerprint,omitempty"`
|
||||
}
|
||||
|
||||
type relayDialer struct {
|
||||
@@ -106,12 +109,9 @@ func (d *relayDialer) DialContext(ctx context.Context, network, address string)
|
||||
if err := writeRelayRequest(conn, relayCmdConnect, targetAddress, relayNetworkTCP, d.option.Username, d.option.Password); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := readRelayConnectResponse(conn); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
success = true
|
||||
return conn, nil
|
||||
return newRelayConn(conn), nil
|
||||
}
|
||||
|
||||
func (d *relayDialer) ListenPacket(ctx context.Context, network, _ string, rAddrPort netip.AddrPort) (net.PacketConn, error) {
|
||||
@@ -142,9 +142,6 @@ func (d *relayDialer) ListenPacket(ctx context.Context, network, _ string, rAddr
|
||||
if err := writeRelayRequest(conn, relayCmdConnect|relayFlagUDP, targetAddress, relayNetworkUDP, d.option.Username, d.option.Password); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := readRelayConnectResponse(conn); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
success = true
|
||||
return &relayPacketConn{
|
||||
@@ -172,42 +169,76 @@ func (d *relayDialer) dialRelayServer(ctx context.Context, fallbackAddress strin
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if d.option.TLS {
|
||||
tlsConn, err := mihomoVMess.StreamTLSConn(ctx, conn, &mihomoVMess.TLSConfig{
|
||||
Host: d.serverName(relayAddress),
|
||||
if d.option.WS {
|
||||
host, port, err := net.SplitHostPort(relayAddress)
|
||||
if err != nil {
|
||||
_ = conn.Close()
|
||||
return nil, fmt.Errorf("invalid gost relay websocket address %q: %w", relayAddress, err)
|
||||
}
|
||||
wsPath := d.option.WSPath
|
||||
if wsPath == "" {
|
||||
// Match the default path of GOST's ws/wss/mws/mwss listeners.
|
||||
wsPath = "/ws"
|
||||
}
|
||||
|
||||
wsConn, err := NewGostWebsocket(ctx, conn, &Option{
|
||||
Host: host,
|
||||
Port: port,
|
||||
Path: wsPath,
|
||||
Headers: d.option.WSHeaders,
|
||||
TLS: d.option.TLS,
|
||||
SNI: d.option.SNI,
|
||||
SkipCertVerify: d.option.SkipCertVerify,
|
||||
NameCertVerify: d.option.NameCertVerify,
|
||||
FingerPrint: d.option.Fingerprint,
|
||||
Fingerprint: d.option.Fingerprint,
|
||||
Certificate: d.option.Certificate,
|
||||
PrivateKey: d.option.PrivateKey,
|
||||
ClientFingerprint: d.option.ClientFingerprint,
|
||||
Mux: d.option.Mux,
|
||||
})
|
||||
if err != nil {
|
||||
_ = conn.Close()
|
||||
return nil, err
|
||||
}
|
||||
conn = tlsConn
|
||||
}
|
||||
|
||||
if d.option.Mux {
|
||||
config := smux.DefaultConfig()
|
||||
config.KeepAliveDisabled = true
|
||||
|
||||
session, err := smux.Client(conn, config)
|
||||
if err != nil {
|
||||
_ = conn.Close()
|
||||
return nil, err
|
||||
conn = wsConn
|
||||
} else {
|
||||
if d.option.TLS {
|
||||
tlsConn, err := mihomoVMess.StreamTLSConn(ctx, conn, &mihomoVMess.TLSConfig{
|
||||
Host: d.serverName(relayAddress),
|
||||
SkipCertVerify: d.option.SkipCertVerify,
|
||||
NameCertVerify: d.option.NameCertVerify,
|
||||
FingerPrint: d.option.Fingerprint,
|
||||
Certificate: d.option.Certificate,
|
||||
PrivateKey: d.option.PrivateKey,
|
||||
ClientFingerprint: d.option.ClientFingerprint,
|
||||
})
|
||||
if err != nil {
|
||||
_ = conn.Close()
|
||||
return nil, err
|
||||
}
|
||||
conn = tlsConn
|
||||
}
|
||||
|
||||
stream, err := session.OpenStream()
|
||||
if err != nil {
|
||||
_ = session.Close()
|
||||
return nil, err
|
||||
}
|
||||
if d.option.Mux {
|
||||
// Keep the default smux v1 and keepalive settings aligned with GOST.
|
||||
config := smux.DefaultConfig()
|
||||
|
||||
conn = &muxConn{
|
||||
Conn: stream,
|
||||
session: session,
|
||||
session, err := smux.Client(conn, config)
|
||||
if err != nil {
|
||||
_ = conn.Close()
|
||||
return nil, err
|
||||
}
|
||||
|
||||
stream, err := session.OpenStream()
|
||||
if err != nil {
|
||||
_ = session.Close()
|
||||
return nil, err
|
||||
}
|
||||
|
||||
conn = &muxConn{
|
||||
Conn: stream,
|
||||
session: session,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -284,13 +315,71 @@ func writeRelayRequest(w io.Writer, command byte, address string, network uint16
|
||||
return nil
|
||||
}
|
||||
|
||||
// relayConn strips GOST's CONNECT response on the first read. With the
|
||||
// default nodelay=false, GOST sends that response together with the first
|
||||
// bytes received from the target, so waiting inside DialContext would stall.
|
||||
type relayConn struct {
|
||||
net.Conn
|
||||
responseOnce sync.Once
|
||||
responseErr error
|
||||
}
|
||||
|
||||
func newRelayConn(conn net.Conn) net.Conn {
|
||||
wrapped := &relayConn{Conn: conn}
|
||||
if closeWriter, ok := conn.(interface{ CloseWrite() error }); ok {
|
||||
return &relayConnWithCloseWrite{
|
||||
relayConn: wrapped,
|
||||
closeWriter: closeWriter,
|
||||
}
|
||||
}
|
||||
return wrapped
|
||||
}
|
||||
|
||||
func (c *relayConn) Read(p []byte) (int, error) {
|
||||
if len(p) == 0 {
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
c.responseOnce.Do(func() {
|
||||
c.responseErr = readRelayConnectResponse(c.Conn)
|
||||
})
|
||||
if c.responseErr != nil {
|
||||
return 0, c.responseErr
|
||||
}
|
||||
|
||||
return c.Conn.Read(p)
|
||||
}
|
||||
|
||||
// Keep TCP half-close available where the wrapped transport supports it.
|
||||
type relayConnWithCloseWrite struct {
|
||||
*relayConn
|
||||
closeWriter interface{ CloseWrite() error }
|
||||
}
|
||||
|
||||
func (c *relayConnWithCloseWrite) CloseWrite() error {
|
||||
return c.closeWriter.CloseWrite()
|
||||
}
|
||||
|
||||
type relayPacketConn struct {
|
||||
conn net.Conn
|
||||
raddr net.Addr
|
||||
wmu sync.Mutex
|
||||
conn net.Conn
|
||||
raddr net.Addr
|
||||
rmu sync.Mutex
|
||||
wmu sync.Mutex
|
||||
responseOnce sync.Once
|
||||
responseErr error
|
||||
}
|
||||
|
||||
func (c *relayPacketConn) ReadFrom(p []byte) (int, net.Addr, error) {
|
||||
c.rmu.Lock()
|
||||
defer c.rmu.Unlock()
|
||||
|
||||
c.responseOnce.Do(func() {
|
||||
c.responseErr = readRelayConnectResponse(c.conn)
|
||||
})
|
||||
if c.responseErr != nil {
|
||||
return 0, nil, c.responseErr
|
||||
}
|
||||
|
||||
var header [2]byte
|
||||
if _, err := io.ReadFull(c.conn, header[:]); err != nil {
|
||||
return 0, nil, err
|
||||
|
||||
+26
-18
@@ -15,18 +15,21 @@ import (
|
||||
|
||||
// Option is options of gost websocket
|
||||
type Option struct {
|
||||
Host string
|
||||
Port string
|
||||
Path string
|
||||
Headers map[string]string
|
||||
TLS bool
|
||||
ECHConfig *ech.Config
|
||||
SkipCertVerify bool
|
||||
NameCertVerify string
|
||||
Fingerprint string
|
||||
Certificate string
|
||||
PrivateKey string
|
||||
Mux bool
|
||||
Host string
|
||||
Port string
|
||||
Path string
|
||||
Headers map[string]string
|
||||
TLS bool
|
||||
ECHConfig *ech.Config
|
||||
SkipCertVerify bool
|
||||
NameCertVerify string
|
||||
Fingerprint string
|
||||
Certificate string
|
||||
PrivateKey string
|
||||
SNI string
|
||||
ClientFingerprint string
|
||||
Mux bool
|
||||
MuxKeepAliveDisabled bool
|
||||
}
|
||||
|
||||
// muxConn is a wrapper around smux.Stream that also closes the session when closed
|
||||
@@ -54,11 +57,12 @@ func NewGostWebsocket(ctx context.Context, conn net.Conn, option *Option) (net.C
|
||||
}
|
||||
|
||||
config := &vmess.WebsocketConfig{
|
||||
Host: option.Host,
|
||||
Port: option.Port,
|
||||
Path: option.Path,
|
||||
ECHConfig: option.ECHConfig,
|
||||
Headers: header,
|
||||
Host: option.Host,
|
||||
Port: option.Port,
|
||||
Path: option.Path,
|
||||
ECHConfig: option.ECHConfig,
|
||||
Headers: header,
|
||||
ClientFingerprint: option.ClientFingerprint,
|
||||
}
|
||||
|
||||
var err error
|
||||
@@ -82,6 +86,9 @@ func NewGostWebsocket(ctx context.Context, conn net.Conn, option *Option) (net.C
|
||||
if host := config.Headers.Get("Host"); host != "" {
|
||||
config.TLSConfig.ServerName = host
|
||||
}
|
||||
if option.SNI != "" {
|
||||
config.TLSConfig.ServerName = option.SNI
|
||||
}
|
||||
}
|
||||
|
||||
conn, err = vmess.StreamWebsocketConn(ctx, conn, config)
|
||||
@@ -90,8 +97,9 @@ func NewGostWebsocket(ctx context.Context, conn net.Conn, option *Option) (net.C
|
||||
}
|
||||
|
||||
if option.Mux {
|
||||
// Keep the default smux v1 and keepalive settings aligned with GOST MWS.
|
||||
config := smux.DefaultConfig()
|
||||
config.KeepAliveDisabled = true
|
||||
config.KeepAliveDisabled = option.MuxKeepAliveDisabled
|
||||
|
||||
session, err := smux.Client(conn, config)
|
||||
if err != nil {
|
||||
|
||||
Reference in new issue
Block a user