From a1c09adb91d623d06bb04f9cc9d14b0e1b828b4d Mon Sep 17 00:00:00 2001 From: wwqgtxx Date: Sun, 5 Jul 2026 18:12:57 +0800 Subject: [PATCH] feat: support restls for shadowsocks listener --- adapter/outbound/shadowsocks.go | 20 +++++++++++++----- docs/config.yaml | 7 +++++++ go.mod | 2 +- go.sum | 4 ++-- listener/config/restls.go | 17 +++++++++++++++ listener/config/shadowsocks.go | 1 + listener/inbound/restls.go | 25 ++++++++++++++++++++++ listener/inbound/shadowsocks.go | 2 ++ listener/inbound/shadowsocks_test.go | 31 ++++++++++++++++++++++++++++ listener/shadowsocks/tcp.go | 23 +++++++++++++++++++++ listener/sing_shadowsocks/server.go | 23 +++++++++++++++++++++ transport/restls/restls.go | 20 ++++++++++++++++++ 12 files changed, 167 insertions(+), 8 deletions(-) create mode 100644 listener/config/restls.go create mode 100644 listener/inbound/restls.go diff --git a/adapter/outbound/shadowsocks.go b/adapter/outbound/shadowsocks.go index 343c3a191..651d5da32 100644 --- a/adapter/outbound/shadowsocks.go +++ b/adapter/outbound/shadowsocks.go @@ -100,10 +100,13 @@ type shadowTLSOption struct { } type restlsOption struct { - Password string `obfs:"password"` - Host string `obfs:"host"` - VersionHint string `obfs:"version-hint"` - RestlsScript string `obfs:"restls-script,omitempty"` + Password string `obfs:"password"` + Host string `obfs:"host"` + VersionHint string `obfs:"version-hint"` + RestlsScript string `obfs:"restls-script,omitempty"` + Fingerprint string `obfs:"fingerprint,omitempty"` + SkipCertVerify bool `obfs:"skip-cert-verify,omitempty"` + ForceTLS12 bool `obfs:"force-tls12,omitempty"` // for test } type kcpTunOption struct { @@ -404,7 +407,14 @@ func NewShadowSocks(option ShadowSocksOption) (*ShadowSocks, error) { if err != nil { return nil, fmt.Errorf("ss %s initialize restls-plugin error: %w", addr, err) } - + restlsConfig.InsecureSkipVerify = restlsOpt.SkipCertVerify + if restlsOpt.Fingerprint != "" { + err = restls.SetFingerprint(restlsConfig, restlsOpt.Fingerprint) + if err != nil { + return nil, fmt.Errorf("ss %s initialize restls-plugin error: %w", addr, err) + } + } + restlsConfig.ForceTLS12 = restlsOpt.ForceTLS12 } else if option.Plugin == kcptun.Mode { obfsMode = kcptun.Mode kcptunOpt := &kcpTunOption{} diff --git a/docs/config.yaml b/docs/config.yaml index 0f87bfc4d..a68b6840c 100644 --- a/docs/config.yaml +++ b/docs/config.yaml @@ -1765,6 +1765,13 @@ listeners: # password: password # handshake: # dest: test.com:443 + # res-tls: + # enable: false # 设置为true时开启 + # dest: test.com:443 + # password: password + # restls-script: "" + # min-record-len: 0 + # proxy: "" # kcp-tun: # enable: false # key: it's a secrect # pre-shared secret between client and server diff --git a/go.mod b/go.mod index ae2f3da5e..02c307a97 100644 --- a/go.mod +++ b/go.mod @@ -28,7 +28,7 @@ require ( github.com/metacubex/mlkem v0.1.0 github.com/metacubex/quic-go v0.59.1-0.20260606115121-0662b57ad5bf github.com/metacubex/randv2 v0.2.0 - github.com/metacubex/restls-client-go v0.1.7 + github.com/metacubex/restls-client-go v0.1.8 github.com/metacubex/sevenzip v1.6.4 github.com/metacubex/sing v0.5.7 github.com/metacubex/sing-mux v0.3.10 diff --git a/go.sum b/go.sum index a811a823e..80a5c6b87 100644 --- a/go.sum +++ b/go.sum @@ -149,8 +149,8 @@ github.com/metacubex/quic-go v0.59.1-0.20260606115121-0662b57ad5bf h1:WvIp5pF+LL github.com/metacubex/quic-go v0.59.1-0.20260606115121-0662b57ad5bf/go.mod h1:2YEQEvFrZ5V76oynMBDTlN+4fdnSHCa2uNJxv3cm1HU= github.com/metacubex/randv2 v0.2.0 h1:uP38uBvV2SxYfLj53kuvAjbND4RUDfFJjwr4UigMiLs= github.com/metacubex/randv2 v0.2.0/go.mod h1:kFi2SzrQ5WuneuoLLCMkABtiBu6VRrMrWFqSPyj2cxY= -github.com/metacubex/restls-client-go v0.1.7 h1:eCwiXCTQb5WJu9IlgYvDBA1OgrINv58dEe7hcN5H15k= -github.com/metacubex/restls-client-go v0.1.7/go.mod h1:BN/U52vPw7j8VTSh2vleD/MnmVKCov84mS5VcjVHH4g= +github.com/metacubex/restls-client-go v0.1.8 h1:0kQ699TWnbK3bWLhCPE0oIiBJLN+errOLQ9Z3/P1lbA= +github.com/metacubex/restls-client-go v0.1.8/go.mod h1:BN/U52vPw7j8VTSh2vleD/MnmVKCov84mS5VcjVHH4g= github.com/metacubex/sevenzip v1.6.4 h1:OIL+DeOeSAbKNsjqxcYUMiarRmX6Kaxakb0GT7E9Oik= github.com/metacubex/sevenzip v1.6.4/go.mod h1:FP3X9bzFKj9wPxifGN9B3w2fIEicMjzKYIGIhnu+1pw= github.com/metacubex/sing v0.5.7 h1:8OC+fhKFSv/l9ehEhJRaZZAOuthfZo68SteBVLe8QqM= diff --git a/listener/config/restls.go b/listener/config/restls.go new file mode 100644 index 000000000..bfb559fbc --- /dev/null +++ b/listener/config/restls.go @@ -0,0 +1,17 @@ +package config + +import "encoding/json" + +type ResTLS struct { + Enable bool + Dest string + Password string + RestlsScript string + MinRecordLen int + Proxy string +} + +func (r ResTLS) String() string { + b, _ := json.Marshal(r) + return string(b) +} diff --git a/listener/config/shadowsocks.go b/listener/config/shadowsocks.go index 2e3a8d7ef..e10cf14eb 100644 --- a/listener/config/shadowsocks.go +++ b/listener/config/shadowsocks.go @@ -14,6 +14,7 @@ type ShadowsocksServer struct { Udp bool MuxOption sing.MuxOption `yaml:"mux-option" json:"mux-option,omitempty"` ShadowTLS ShadowTLS `yaml:"shadow-tls" json:"shadow-tls,omitempty"` + ResTLS ResTLS `yaml:"res-tls" json:"res-tls,omitempty"` KcpTun KcpTun `yaml:"kcp-tun" json:"kcp-tun,omitempty"` SimpleObfs SimpleObfs `yaml:"simple-obfs" json:"simple-obfs,omitempty"` } diff --git a/listener/inbound/restls.go b/listener/inbound/restls.go new file mode 100644 index 000000000..01e33e9fc --- /dev/null +++ b/listener/inbound/restls.go @@ -0,0 +1,25 @@ +package inbound + +import ( + LC "github.com/metacubex/mihomo/listener/config" +) + +type ResTLS struct { + Enable bool `inbound:"enable"` + Dest string `inbound:"dest"` + Password string `inbound:"password"` + RestlsScript string `inbound:"restls-script,omitempty"` + MinRecordLen int `inbound:"min-record-len,omitempty"` + Proxy string `inbound:"proxy,omitempty"` +} + +func (r ResTLS) Build() LC.ResTLS { + return LC.ResTLS{ + Enable: r.Enable, + Dest: r.Dest, + Password: r.Password, + RestlsScript: r.RestlsScript, + MinRecordLen: r.MinRecordLen, + Proxy: r.Proxy, + } +} diff --git a/listener/inbound/shadowsocks.go b/listener/inbound/shadowsocks.go index 14b7f4a2a..e45896a9f 100644 --- a/listener/inbound/shadowsocks.go +++ b/listener/inbound/shadowsocks.go @@ -16,6 +16,7 @@ type ShadowSocksOption struct { UDP bool `inbound:"udp,omitempty"` MuxOption MuxOption `inbound:"mux-option,omitempty"` ShadowTLS ShadowTLS `inbound:"shadow-tls,omitempty"` + ResTLS ResTLS `inbound:"res-tls,omitempty"` KcpTun KcpTun `inbound:"kcp-tun,omitempty"` SimpleObfs SimpleObfs `inbound:"simple-obfs,omitempty"` } @@ -59,6 +60,7 @@ func NewShadowSocks(options *ShadowSocksOption) (*ShadowSocks, error) { Udp: options.UDP, MuxOption: options.MuxOption.Build(), ShadowTLS: options.ShadowTLS.Build(), + ResTLS: options.ResTLS.Build(), KcpTun: options.KcpTun.Build(), SimpleObfs: options.SimpleObfs.Build(), }, diff --git a/listener/inbound/shadowsocks_test.go b/listener/inbound/shadowsocks_test.go index e6276aab3..692ee9e77 100644 --- a/listener/inbound/shadowsocks_test.go +++ b/listener/inbound/shadowsocks_test.go @@ -12,6 +12,7 @@ import ( "github.com/metacubex/mihomo/adapter/outbound" "github.com/metacubex/mihomo/listener/inbound" "github.com/metacubex/mihomo/transport/kcptun" + "github.com/metacubex/mihomo/transport/restls" shadowtls "github.com/metacubex/mihomo/transport/sing-shadowtls" shadowsocks "github.com/metacubex/sing-shadowsocks" @@ -168,6 +169,36 @@ func TestInboundShadowSocks_ShadowTlsv3(t *testing.T) { testInboundShadowSocksShadowTls(t, inboundOptions, outboundOptions) } +func TestInboundShadowSocks_Restls_tls12(t *testing.T) { + inboundOptions := inbound.ShadowSocksOption{ + ResTLS: inbound.ResTLS{ + Enable: true, + Dest: net.JoinHostPort(realityDest, "443"), + Password: shadowsocksPassword16, + }, + } + outboundOptions := outbound.ShadowSocksOption{ + Plugin: restls.Mode, + PluginOpts: map[string]any{"host": realityDest, "password": shadowsocksPassword16, "fingerprint": tlsFingerprint, "version-hint": "tls12", "force-tls12": true}, + } + testInboundShadowSocks(t, inboundOptions, outboundOptions, shadowsocksCipherShortLists, false) +} + +func TestInboundShadowSocks_Restls_tls13(t *testing.T) { + inboundOptions := inbound.ShadowSocksOption{ + ResTLS: inbound.ResTLS{ + Enable: true, + Dest: net.JoinHostPort(realityDest, "443"), + Password: shadowsocksPassword16, + }, + } + outboundOptions := outbound.ShadowSocksOption{ + Plugin: restls.Mode, + PluginOpts: map[string]any{"host": realityDest, "password": shadowsocksPassword16, "fingerprint": tlsFingerprint, "version-hint": "tls13"}, + } + testInboundShadowSocks(t, inboundOptions, outboundOptions, shadowsocksCipherShortLists, false) +} + func TestInboundShadowSocks_SimpleObfs_Http(t *testing.T) { inboundOptions := inbound.ShadowSocksOption{ SimpleObfs: inbound.SimpleObfs{ diff --git a/listener/shadowsocks/tcp.go b/listener/shadowsocks/tcp.go index 10e3f2afc..694964052 100644 --- a/listener/shadowsocks/tcp.go +++ b/listener/shadowsocks/tcp.go @@ -10,7 +10,9 @@ import ( N "github.com/metacubex/mihomo/common/net" C "github.com/metacubex/mihomo/constant" LC "github.com/metacubex/mihomo/listener/config" + "github.com/metacubex/mihomo/listener/inner" "github.com/metacubex/mihomo/listener/sing" + "github.com/metacubex/mihomo/transport/restls" "github.com/metacubex/mihomo/transport/shadowsocks/core" obfs "github.com/metacubex/mihomo/transport/simple-obfs" "github.com/metacubex/mihomo/transport/socks5" @@ -23,6 +25,7 @@ type Listener struct { udpListeners []*UDPListener pickCipher core.Cipher handler *sing.ListenerHandler + resTLS *restls.ServerConfig simpleObfs func(net.Conn) net.Conn } @@ -47,6 +50,18 @@ func New(config LC.ShadowsocksServer, lc C.InboundListenConfig, tunnel C.Tunnel, sl := &Listener{config: config, pickCipher: pickCipher, handler: h} _listener = sl + if config.ResTLS.Enable { + sl.resTLS = &restls.ServerConfig{ + ServerHostname: config.ResTLS.Dest, + Password: config.ResTLS.Password, + RestlsScript: config.ResTLS.RestlsScript, + MinRecordLen: config.ResTLS.MinRecordLen, + DialContext: func(ctx context.Context, network, address string) (net.Conn, error) { + return inner.HandleTcp(tunnel, address, config.ResTLS.Proxy) + }, + } + } + if config.SimpleObfs.Enable { switch config.SimpleObfs.Mode { case "http": @@ -126,6 +141,14 @@ func (l *Listener) AddrList() (addrList []net.Addr) { } func (l *Listener) HandleConn(conn net.Conn, tunnel C.Tunnel, additions ...inbound.Addition) { + if l.resTLS != nil { + c, err := restls.Server(context.TODO(), conn, l.resTLS) + if err != nil { + _ = conn.Close() + return + } + conn = c + } if l.simpleObfs != nil { conn = l.simpleObfs(conn) } diff --git a/listener/sing_shadowsocks/server.go b/listener/sing_shadowsocks/server.go index d680da44b..7cdad86ef 100644 --- a/listener/sing_shadowsocks/server.go +++ b/listener/sing_shadowsocks/server.go @@ -10,11 +10,13 @@ import ( "github.com/metacubex/mihomo/common/sockopt" C "github.com/metacubex/mihomo/constant" LC "github.com/metacubex/mihomo/listener/config" + "github.com/metacubex/mihomo/listener/inner" embedSS "github.com/metacubex/mihomo/listener/shadowsocks" "github.com/metacubex/mihomo/listener/sing" "github.com/metacubex/mihomo/log" "github.com/metacubex/mihomo/ntp" "github.com/metacubex/mihomo/transport/kcptun" + "github.com/metacubex/mihomo/transport/restls" obfs "github.com/metacubex/mihomo/transport/simple-obfs" shadowsocks "github.com/metacubex/sing-shadowsocks" @@ -35,6 +37,7 @@ type Listener struct { udpListeners []net.PacketConn service shadowsocks.Service shadowTLS *shadowtls.Service + resTLS *restls.ServerConfig simpleObfs func(net.Conn) net.Conn } @@ -141,6 +144,18 @@ func New(config LC.ShadowsocksServer, lc C.InboundListenConfig, tunnel C.Tunnel, } } + if config.ResTLS.Enable { + sl.resTLS = &restls.ServerConfig{ + ServerHostname: config.ResTLS.Dest, + Password: config.ResTLS.Password, + RestlsScript: config.ResTLS.RestlsScript, + MinRecordLen: config.ResTLS.MinRecordLen, + DialContext: func(ctx context.Context, network, address string) (net.Conn, error) { + return inner.HandleTcp(tunnel, address, config.ResTLS.Proxy) + }, + } + } + if config.SimpleObfs.Enable { switch config.SimpleObfs.Mode { case "http": @@ -281,6 +296,14 @@ func (l *Listener) AddrList() (addrList []net.Addr) { func (l *Listener) HandleConn(conn net.Conn, tunnel C.Tunnel, additions ...inbound.Addition) { ctx := sing.WithAdditions(context.TODO(), additions...) + if l.resTLS != nil { + c, err := restls.Server(context.TODO(), conn, l.resTLS) + if err != nil { + _ = conn.Close() + return + } + conn = c + } if l.simpleObfs != nil { conn = l.simpleObfs(conn) } diff --git a/transport/restls/restls.go b/transport/restls/restls.go index 200c9dad8..be832d966 100644 --- a/transport/restls/restls.go +++ b/transport/restls/restls.go @@ -4,6 +4,9 @@ import ( "context" "net" + "github.com/metacubex/mihomo/component/ca" + "github.com/metacubex/mihomo/ntp" + tls "github.com/metacubex/restls-client-go" ) @@ -23,6 +26,22 @@ type Config = tls.Config var NewRestlsConfig = tls.NewRestlsConfig +type ServerConfig = tls.RestlsServerConfig + +var Server = tls.RestlsServer + +func SetFingerprint(config *Config, fingerprint string) (err error) { + verifier, err := ca.NewFingerprintVerifier(fingerprint, ntp.Now) + if err != nil { + return err + } + config.InsecureSkipVerify = true + config.VerifyConnection = func(state tls.ConnectionState) error { + return verifier(state.PeerCertificates, state.ServerName) + } + return nil +} + // NewRestls return a Restls Connection func NewRestls(ctx context.Context, conn net.Conn, config *Config) (net.Conn, error) { clientHellowID := tls.HelloChrome_Auto @@ -31,6 +50,7 @@ func NewRestls(ctx context.Context, conn net.Conn, config *Config) (net.Conn, er if clientIDPtr != nil { clientHellowID = *clientIDPtr } + config = config.Clone() // avoid race condition in HandshakeContext } restls := &Restls{ UConn: tls.UClient(conn, config, clientHellowID),