feat: support restls for shadowsocks listener

This commit is contained in:
wwqgtxx committed 2026-07-05 18:14:26 +08:00
1 parent 6414c563dd
commit a1c09adb91
12 files changed
+167 -8

No files matched your search

+15 -5
View File
@@ -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{}
+7
View File
@@ -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
+1 -1
View File
@@ -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
+2 -2
View File
@@ -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=
+17
View File
@@ -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)
}
+1
View File
@@ -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"`
}
+25
View File
@@ -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,
}
}
+2
View File
@@ -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(),
},
+31
View File
@@ -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{
+23
View File
@@ -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)
}
+23
View File
@@ -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)
}
+20
View File
@@ -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),