diff --git a/adapter/outbound/gost_relay.go b/adapter/outbound/gost_relay.go index b6b6674d..2eb92c8b 100644 --- a/adapter/outbound/gost_relay.go +++ b/adapter/outbound/gost_relay.go @@ -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, diff --git a/adapter/outbound/shadowsocks.go b/adapter/outbound/shadowsocks.go index 8c562a8d..9ca0bdc1 100644 --- a/adapter/outbound/shadowsocks.go +++ b/adapter/outbound/shadowsocks.go @@ -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 { diff --git a/docs/config.yaml b/docs/config.yaml index 8f3bf323..befac07e 100644 --- a/docs/config.yaml +++ b/docs/config.yaml @@ -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 diff --git a/transport/gost/relay.go b/transport/gost/relay.go index 7064ef0f..93d0c74e 100644 --- a/transport/gost/relay.go +++ b/transport/gost/relay.go @@ -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 diff --git a/transport/gost/websocket.go b/transport/gost/websocket.go index d4e03a34..93f97e5b 100644 --- a/transport/gost/websocket.go +++ b/transport/gost/websocket.go @@ -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 {