diff --git a/adapter/outbound/shadowtls.go b/adapter/outbound/shadowtls.go new file mode 100644 index 00000000..07f98e91 --- /dev/null +++ b/adapter/outbound/shadowtls.go @@ -0,0 +1,15 @@ +package outbound + +import "github.com/metacubex/mihomo/transport/shadowtls" + +type ShadowTLSOptions struct { + Password string `proxy:"password,omitempty"` + Version int `proxy:"version,omitempty"` +} + +func (o ShadowTLSOptions) Parse() (*shadowtls.Config, error) { + if o.Password == "" && o.Version == 0 { + return nil, nil + } + return shadowtls.NewConfig(o.Password, o.Version) +} diff --git a/adapter/outbound/trojan.go b/adapter/outbound/trojan.go index 2ed7cf6f..f16df913 100644 --- a/adapter/outbound/trojan.go +++ b/adapter/outbound/trojan.go @@ -15,6 +15,7 @@ import ( "github.com/metacubex/mihomo/transport/gun" "github.com/metacubex/mihomo/transport/jls" "github.com/metacubex/mihomo/transport/shadowsocks/core" + "github.com/metacubex/mihomo/transport/shadowtls" "github.com/metacubex/mihomo/transport/trojan" "github.com/metacubex/mihomo/transport/vmess" @@ -30,35 +31,37 @@ type Trojan struct { // for gun mux gunClient *gun.Client - echConfig *ech.Config - jlsConfig *jls.Config - realityConfig *tlsC.RealityConfig + echConfig *ech.Config + shadowTLSConfig *shadowtls.Config + jlsConfig *jls.Config + realityConfig *tlsC.RealityConfig ssCipher core.Cipher } type TrojanOption struct { BasicOption - Name string `proxy:"name"` - Server string `proxy:"server"` - Port int `proxy:"port"` - Password string `proxy:"password"` - ALPN []string `proxy:"alpn,omitempty"` - SNI string `proxy:"sni,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"` - UDP bool `proxy:"udp,omitempty"` - Network string `proxy:"network,omitempty"` - ECHOpts ECHOptions `proxy:"ech-opts,omitempty"` - JLSOpts JLSOptions `proxy:"jls-opts,omitempty"` - RealityOpts RealityOptions `proxy:"reality-opts,omitempty"` - GrpcOpts GrpcOptions `proxy:"grpc-opts,omitempty"` - WSOpts WSOptions `proxy:"ws-opts,omitempty"` - SSOpts TrojanSSOption `proxy:"ss-opts,omitempty"` - ClientFingerprint string `proxy:"client-fingerprint,omitempty"` + Name string `proxy:"name"` + Server string `proxy:"server"` + Port int `proxy:"port"` + Password string `proxy:"password"` + ALPN []string `proxy:"alpn,omitempty"` + SNI string `proxy:"sni,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"` + UDP bool `proxy:"udp,omitempty"` + Network string `proxy:"network,omitempty"` + ECHOpts ECHOptions `proxy:"ech-opts,omitempty"` + ShadowTLSOpts ShadowTLSOptions `proxy:"shadow-tls-opts,omitempty"` + JLSOpts JLSOptions `proxy:"jls-opts,omitempty"` + RealityOpts RealityOptions `proxy:"reality-opts,omitempty"` + GrpcOpts GrpcOptions `proxy:"grpc-opts,omitempty"` + WSOpts WSOptions `proxy:"ws-opts,omitempty"` + SSOpts TrojanSSOption `proxy:"ss-opts,omitempty"` + ClientFingerprint string `proxy:"client-fingerprint,omitempty"` } // TrojanSSOption from https://github.com/p4gefau1t/trojan-go/blob/v0.10.6/tunnel/shadowsocks/config.go#L5 @@ -101,11 +104,17 @@ func (t *Trojan) StreamConnContext(ctx context.Context, c net.Conn, metadata *C. alpn = t.option.ALPN } - if t.jlsConfig != nil { + if t.shadowTLSConfig != nil || t.jlsConfig != nil { c, err = vmess.StreamTLSConn(ctx, c, &vmess.TLSConfig{ Host: t.option.SNI, + SkipCertVerify: t.option.SkipCertVerify, + NameCertVerify: t.option.NameCertVerify, + FingerPrint: t.option.Fingerprint, + Certificate: t.option.Certificate, + PrivateKey: t.option.PrivateKey, ClientFingerprint: t.option.ClientFingerprint, NextProtos: []string{"http/1.1"}, + ShadowTLS: t.shadowTLSConfig, JLS: t.jlsConfig, }) if err != nil { @@ -150,6 +159,7 @@ func (t *Trojan) StreamConnContext(ctx context.Context, c net.Conn, metadata *C. ClientFingerprint: t.option.ClientFingerprint, NextProtos: alpn, ECH: t.echConfig, + ShadowTLS: t.shadowTLSConfig, JLS: t.jlsConfig, Reality: t.realityConfig, }) @@ -294,6 +304,10 @@ func NewTrojan(option TrojanOption) (*Trojan, error) { if err != nil { return nil, err } + t.shadowTLSConfig, err = option.ShadowTLSOpts.Parse() + if err != nil { + return nil, err + } t.jlsConfig, err = option.JLSOpts.Parse() if err != nil { return nil, err @@ -302,6 +316,14 @@ func NewTrojan(option TrojanOption) (*Trojan, error) { if err != nil { return nil, err } + if t.shadowTLSConfig != nil { + if t.jlsConfig != nil { + return nil, errors.New("ShadowTLS is incompatible with JLS") + } + if t.realityConfig != nil { + return nil, errors.New("ShadowTLS is incompatible with REALITY") + } + } if t.jlsConfig != nil && t.realityConfig != nil { return nil, errors.New("JLS is incompatible with REALITY") } @@ -339,6 +361,7 @@ func NewTrojan(option TrojanOption) (*Trojan, error) { ClientFingerprint: option.ClientFingerprint, NextProtos: []string{"h2"}, ECH: t.echConfig, + ShadowTLS: t.shadowTLSConfig, JLS: t.jlsConfig, Reality: t.realityConfig, } diff --git a/adapter/outbound/vless.go b/adapter/outbound/vless.go index 17725840..198e4e28 100644 --- a/adapter/outbound/vless.go +++ b/adapter/outbound/vless.go @@ -17,6 +17,7 @@ import ( C "github.com/metacubex/mihomo/constant" "github.com/metacubex/mihomo/transport/gun" "github.com/metacubex/mihomo/transport/jls" + "github.com/metacubex/mihomo/transport/shadowtls" "github.com/metacubex/mihomo/transport/tuic/common" "github.com/metacubex/mihomo/transport/vless" "github.com/metacubex/mihomo/transport/vless/encryption" @@ -44,9 +45,10 @@ type Vless struct { // for xhttp xhttpClient *xhttp.Client - echConfig *ech.Config - jlsConfig *jls.Config - realityConfig *tlsC.RealityConfig + echConfig *ech.Config + shadowTLSConfig *shadowtls.Config + jlsConfig *jls.Config + realityConfig *tlsC.RealityConfig } type VlessOption struct { @@ -65,6 +67,7 @@ type VlessOption struct { Encryption string `proxy:"encryption,omitempty"` Network string `proxy:"network,omitempty"` ECHOpts ECHOptions `proxy:"ech-opts,omitempty"` + ShadowTLSOpts ShadowTLSOptions `proxy:"shadow-tls-opts,omitempty"` JLSOpts JLSOptions `proxy:"jls-opts,omitempty"` RealityOpts RealityOptions `proxy:"reality-opts,omitempty"` HTTPOpts HTTPOptions `proxy:"http-opts,omitempty"` @@ -172,11 +175,17 @@ func (v *Vless) StreamConnContext(ctx context.Context, c net.Conn, metadata *C.M serverName = host } - if v.jlsConfig != nil { + if v.shadowTLSConfig != nil || v.jlsConfig != nil { c, err = vmess.StreamTLSConn(ctx, c, &vmess.TLSConfig{ Host: serverName, + SkipCertVerify: v.option.SkipCertVerify, + NameCertVerify: v.option.NameCertVerify, + FingerPrint: v.option.Fingerprint, + Certificate: v.option.Certificate, + PrivateKey: v.option.PrivateKey, ClientFingerprint: v.option.ClientFingerprint, NextProtos: []string{"http/1.1"}, + ShadowTLS: v.shadowTLSConfig, JLS: v.jlsConfig, }) if err != nil { @@ -299,6 +308,7 @@ func (v *Vless) streamTLSConn(ctx context.Context, conn net.Conn, isH2 bool) (ne PrivateKey: v.option.PrivateKey, ClientFingerprint: v.option.ClientFingerprint, ECH: v.echConfig, + ShadowTLS: v.shadowTLSConfig, JLS: v.jlsConfig, Reality: v.realityConfig, NextProtos: v.option.ALPN, @@ -498,6 +508,10 @@ func NewVless(option VlessOption) (*Vless, error) { if err != nil { return nil, err } + v.shadowTLSConfig, err = option.ShadowTLSOpts.Parse() + if err != nil { + return nil, err + } v.jlsConfig, err = option.JLSOpts.Parse() if err != nil { return nil, err @@ -506,6 +520,17 @@ func NewVless(option VlessOption) (*Vless, error) { if err != nil { return nil, err } + if v.shadowTLSConfig != nil { + if !option.TLS { + return nil, errors.New("ShadowTLS requires TLS") + } + if v.jlsConfig != nil { + return nil, errors.New("ShadowTLS is incompatible with JLS") + } + if v.realityConfig != nil { + return nil, errors.New("ShadowTLS is incompatible with REALITY") + } + } if v.jlsConfig != nil { if !option.TLS { return nil, errors.New("JLS requires TLS") @@ -550,6 +575,7 @@ func NewVless(option VlessOption) (*Vless, error) { ClientFingerprint: option.ClientFingerprint, NextProtos: []string{"h2"}, ECH: v.echConfig, + ShadowTLS: v.shadowTLSConfig, JLS: v.jlsConfig, Reality: v.realityConfig, } @@ -646,6 +672,9 @@ func NewVless(option VlessOption) (*Vless, error) { if !v.option.TLS { return nil, errors.New("xhttp HTTP/3 requires TLS") } + if v.shadowTLSConfig != nil { + return nil, errors.New("xhttp HTTP/3 does not support ShadowTLS") + } if v.jlsConfig != nil { return nil, errors.New("xhttp HTTP/3 does not support JLS") } @@ -760,6 +789,7 @@ func NewVless(option VlessOption) (*Vless, error) { PrivateKey: downloadPrivateKey, ClientFingerprint: downloadClientFingerprint, ECH: downloadEchConfig, + ShadowTLS: v.shadowTLSConfig, JLS: downloadJLSConfig, Reality: downloadRealityCfg, NextProtos: downloadALPN, @@ -798,6 +828,9 @@ func NewVless(option VlessOption) (*Vless, error) { if !downloadTLS { return nil, errors.New("xhttp HTTP/3 requires TLS") } + if v.shadowTLSConfig != nil { + return nil, errors.New("xhttp HTTP/3 does not support ShadowTLS") + } if downloadJLSConfig != nil { return nil, errors.New("xhttp HTTP/3 does not support JLS") } diff --git a/adapter/outbound/vmess.go b/adapter/outbound/vmess.go index 008e2fad..dbd23186 100644 --- a/adapter/outbound/vmess.go +++ b/adapter/outbound/vmess.go @@ -21,6 +21,7 @@ import ( "github.com/metacubex/mihomo/transport/jls" "github.com/metacubex/mihomo/transport/mekya" "github.com/metacubex/mihomo/transport/mkcp" + "github.com/metacubex/mihomo/transport/shadowtls" mihomoVMess "github.com/metacubex/mihomo/transport/vmess" "github.com/metacubex/http" @@ -41,9 +42,10 @@ type Vmess struct { gunClient *gun.Client mekyaClient *mekya.Client - echConfig *ech.Config - jlsConfig *jls.Config - realityConfig *tlsC.RealityConfig + echConfig *ech.Config + shadowTLSConfig *shadowtls.Config + jlsConfig *jls.Config + realityConfig *tlsC.RealityConfig } type VmessOption struct { @@ -65,6 +67,7 @@ type VmessOption struct { PrivateKey string `proxy:"private-key,omitempty"` ServerName string `proxy:"servername,omitempty"` ECHOpts ECHOptions `proxy:"ech-opts,omitempty"` + ShadowTLSOpts ShadowTLSOptions `proxy:"shadow-tls-opts,omitempty"` JLSOpts JLSOptions `proxy:"jls-opts,omitempty"` RealityOpts RealityOptions `proxy:"reality-opts,omitempty"` TLSMirrorOpts TLSMirrorOptions `proxy:"tlsmirror-opts,omitempty"` @@ -196,11 +199,17 @@ func (v *Vmess) StreamConnContext(ctx context.Context, c net.Conn, metadata *C.M serverName = host } - if v.jlsConfig != nil { + if v.shadowTLSConfig != nil || v.jlsConfig != nil { c, err = mihomoVMess.StreamTLSConn(ctx, c, &mihomoVMess.TLSConfig{ Host: serverName, + SkipCertVerify: v.option.SkipCertVerify, + NameCertVerify: v.option.NameCertVerify, + FingerPrint: v.option.Fingerprint, + Certificate: v.option.Certificate, + PrivateKey: v.option.PrivateKey, ClientFingerprint: v.option.ClientFingerprint, NextProtos: []string{"http/1.1"}, + ShadowTLS: v.shadowTLSConfig, JLS: v.jlsConfig, }) if err != nil { @@ -343,6 +352,7 @@ func (v *Vmess) streamTLSConn(ctx context.Context, conn net.Conn, isH2 bool) (ne PrivateKey: v.option.PrivateKey, ClientFingerprint: v.option.ClientFingerprint, ECH: v.echConfig, + ShadowTLS: v.shadowTLSConfig, JLS: v.jlsConfig, Reality: v.realityConfig, NextProtos: v.option.ALPN, @@ -499,6 +509,10 @@ func NewVmess(option VmessOption) (*Vmess, error) { if err != nil { return nil, err } + v.shadowTLSConfig, err = option.ShadowTLSOpts.Parse() + if err != nil { + return nil, err + } v.jlsConfig, err = option.JLSOpts.Parse() if err != nil { return nil, err @@ -507,6 +521,23 @@ func NewVmess(option VmessOption) (*Vmess, error) { if err != nil { return nil, err } + if v.shadowTLSConfig != nil { + if !option.TLS { + return nil, errors.New("ShadowTLS requires TLS") + } + if v.jlsConfig != nil { + return nil, errors.New("ShadowTLS is incompatible with JLS") + } + if v.realityConfig != nil { + return nil, errors.New("ShadowTLS is incompatible with REALITY") + } + if option.TLSMirrorOpts.PrimaryKey != "" { + return nil, errors.New("ShadowTLS is incompatible with TLSMirror") + } + if option.Network == "mkcp" || option.Network == "kcp" { + return nil, errors.New("ShadowTLS only supports TCP transports") + } + } if v.jlsConfig != nil { if !option.TLS { return nil, errors.New("JLS requires TLS") @@ -580,6 +611,7 @@ func NewVmess(option VmessOption) (*Vmess, error) { ClientFingerprint: option.ClientFingerprint, NextProtos: []string{"h2"}, ECH: v.echConfig, + ShadowTLS: v.shadowTLSConfig, JLS: v.jlsConfig, Reality: v.realityConfig, TLSMirror: option.TLSMirrorOpts.Build(), diff --git a/docs/config.yaml b/docs/config.yaml index 93fe261b..71eb66b2 100644 --- a/docs/config.yaml +++ b/docs/config.yaml @@ -729,6 +729,9 @@ proxies: # socks5 # # 如果config为空则通过dns解析,不为空则通过该值指定,格式为经过base64编码的ech参数(dig +short TYPE65 tls-ech.dev) # config: AEn+DQBFKwAgACABWIHUGj4u+PIggYXcR5JF0gYk3dCRioBW8uJq9H4mKAAIAAEAAQABAANAEnB1YmxpYy50bHMtZWNoLmRldgAA # # query-server-name: xxx.com # 可选项,不为空时用于指定通过dns解析时的域名 + # shadow-tls-opts: # 需要 tls: true;使用 servername 作为 ShadowTLS SNI + # version: 3 # 支持 v1/v2/v3;留空时默认为 v2 + # password: shadow-tls-password # jls-opts: # 需要 tls: true;使用 servername 作为 JLS SNI # username: jls-user # password: jls-password @@ -870,6 +873,9 @@ proxies: # socks5 # # 如果config为空则通过dns解析,不为空则通过该值指定,格式为经过base64编码的ech参数(dig +short TYPE65 tls-ech.dev) # config: AEn+DQBFKwAgACABWIHUGj4u+PIggYXcR5JF0gYk3dCRioBW8uJq9H4mKAAIAAEAAQABAANAEnB1YmxpYy50bHMtZWNoLmRldgAA # # query-server-name: xxx.com # 可选项,不为空时用于指定通过dns解析时的域名 + # shadow-tls-opts: # 需要 tls: true;使用 servername 作为 ShadowTLS SNI + # version: 3 # 支持 v1/v2/v3;留空时默认为 v2 + # password: shadow-tls-password # jls-opts: # 需要 tls: true;使用 servername 作为 JLS SNI # username: jls-user # password: jls-password @@ -1084,6 +1090,9 @@ proxies: # socks5 # # 如果config为空则通过dns解析,不为空则通过该值指定,格式为经过base64编码的ech参数(dig +short TYPE65 tls-ech.dev) # config: AEn+DQBFKwAgACABWIHUGj4u+PIggYXcR5JF0gYk3dCRioBW8uJq9H4mKAAIAAEAAQABAANAEnB1YmxpYy50bHMtZWNoLmRldgAA # # query-server-name: xxx.com # 可选项,不为空时用于指定通过dns解析时的域名 + # shadow-tls-opts: # 使用 sni 作为 ShadowTLS SNI + # version: 3 # 支持 v1/v2/v3;留空时默认为 v2 + # password: shadow-tls-password # jls-opts: # 使用 sni 作为 JLS SNI # username: jls-user # password: jls-password @@ -2101,6 +2110,16 @@ listeners: # # alpn: [h2, http/1.1] # # proxy: "" # # rate-limit: 0 # fallback 转发限速,单位 bit/s;0 表示不限速 + # shadow-tls: + # enable: true + # version: 3 # 支持 v1/v2/v3 + # # password: shadow-tls-password # v2 配置项 + # users: # v3 配置项 + # - name: shadow-tls-user + # password: shadow-tls-password + # handshake: + # dest: www.example.com:443 + # # proxy: "" # 如果填写reality-config则开启reality(注意不可与certificate和private-key同时填写) # reality-config: # dest: test.com:443 @@ -2299,7 +2318,17 @@ listeners: after-bytes: 0 # 传输指定字节后开始限速 bytes-per-sec: 0 # 基准速率(字节/秒) burst-bytes-per-sec: 0 # 突发速率(字节/秒),大于 bytesPerSec 时生效 - ### 注意,对于vless listener, 如果 "allow-insecure" 不为 true, 至少需要填写 “certificate和private-key” 或 “jls-config” 或 “reality-config” 或 “decryption” 的其中一项 ### + # shadow-tls: + # enable: true + # version: 3 # 支持 v1/v2/v3 + # # password: shadow-tls-password # v2 配置项 + # users: # v3 配置项 + # - name: shadow-tls-user + # password: shadow-tls-password + # handshake: + # dest: www.example.com:443 + # # proxy: "" + ### 注意,对于vless listener, 如果 "allow-insecure" 不为 true, 至少需要填写 “certificate和private-key” 或 “shadow-tls” 或 “jls-config” 或 “reality-config” 或 “decryption” 的其中一项 ### # allow-insecure: false # 是否允许不开启tls加密(注意:仅用于有 nginx, caddy 前置的情况) - name: anytls-in-1 @@ -2400,6 +2429,16 @@ listeners: # madSJjYQIf9o1N5GXjkW4DEEeb17qMxHdwMdNnwADAABAAEAAQACAAEAAwAIdGVz # dC5jb20AAA== # -----END ECH KEYS----- + # shadow-tls: + # enable: true + # version: 3 # 支持 v1/v2/v3 + # # password: shadow-tls-password # v2 配置项 + # users: # v3 配置项 + # - name: shadow-tls-user + # password: shadow-tls-password + # handshake: + # dest: www.example.com:443 + # # proxy: "" # jls-config: # JLS 替代普通 TLS;未认证连接回落到 dest # enable: true # users: @@ -2432,7 +2471,7 @@ listeners: # enabled: false # method: aes-128-gcm # aes-128-gcm/aes-256-gcm/chacha20-ietf-poly1305 # password: "example" - ### 注意,对于trojan listener, 如果 "allow-insecure" 不为 true, 至少需要填写 “certificate和private-key” 或 “jls-config” 或 “reality-config” 或 “ss-option” 的其中一项 ### + ### 注意,对于trojan listener, 如果 "allow-insecure" 不为 true, 至少需要填写 “certificate和private-key” 或 “shadow-tls” 或 “jls-config” 或 “reality-config” 或 “ss-option” 的其中一项 ### # allow-insecure: false # 是否允许不开启tls加密(注意:仅用于有 nginx, caddy 前置的情况) - name: hysteria2-in-1 diff --git a/listener/config/trojan.go b/listener/config/trojan.go index f6b3b03c..819e7567 100644 --- a/listener/config/trojan.go +++ b/listener/config/trojan.go @@ -24,6 +24,7 @@ type TrojanServer struct { ClientAuthCert string EchKey string AllowInsecure bool + ShadowTLS ShadowTLS `yaml:"shadow-tls" json:"shadow-tls,omitempty"` JLSConfig JLSConfig `yaml:"jls-config" json:"jls-config,omitempty"` RealityConfig reality.Config `yaml:"reality-config" json:"reality-config,omitempty"` MuxOption sing.MuxOption diff --git a/listener/config/vless.go b/listener/config/vless.go index 74fbe400..0a811a80 100644 --- a/listener/config/vless.go +++ b/listener/config/vless.go @@ -27,6 +27,7 @@ type VlessServer struct { ClientAuthCert string EchKey string AllowInsecure bool + ShadowTLS ShadowTLS `yaml:"shadow-tls" json:"shadow-tls,omitempty"` JLSConfig JLSConfig `yaml:"jls-config" json:"jls-config,omitempty"` RealityConfig reality.Config `yaml:"reality-config" json:"reality-config,omitempty"` MuxOption sing.MuxOption `yaml:"mux-option" json:"mux-option,omitempty"` diff --git a/listener/config/vmess.go b/listener/config/vmess.go index 7e51ab7f..3c90e459 100644 --- a/listener/config/vmess.go +++ b/listener/config/vmess.go @@ -25,6 +25,7 @@ type VmessServer struct { ClientAuthType string ClientAuthCert string EchKey string + ShadowTLS ShadowTLS `yaml:"shadow-tls" json:"shadow-tls,omitempty"` JLSConfig JLSConfig `yaml:"jls-config" json:"jls-config,omitempty"` RealityConfig reality.Config `yaml:"reality-config" json:"reality-config,omitempty"` TLSMirrorConfig TLSMirrorConfig `yaml:"tlsmirror-config" json:"tlsmirror-config,omitempty"` diff --git a/listener/inbound/trojan.go b/listener/inbound/trojan.go index 693cdd48..c8409a57 100644 --- a/listener/inbound/trojan.go +++ b/listener/inbound/trojan.go @@ -20,6 +20,7 @@ type TrojanOption struct { ClientAuthCert string `inbound:"client-auth-cert,omitempty"` EchKey string `inbound:"ech-key,omitempty"` AllowInsecure bool `inbound:"allow-insecure,omitempty"` + ShadowTLS ShadowTLS `inbound:"shadow-tls,omitempty"` JLSConfig JLSConfig `inbound:"jls-config,omitempty"` RealityConfig RealityConfig `inbound:"reality-config,omitempty"` MuxOption MuxOption `inbound:"mux-option,omitempty"` @@ -76,6 +77,7 @@ func NewTrojan(options *TrojanOption) (*Trojan, error) { ClientAuthCert: options.ClientAuthCert, EchKey: options.EchKey, AllowInsecure: options.AllowInsecure, + ShadowTLS: options.ShadowTLS.Build(), JLSConfig: options.JLSConfig.Build(), RealityConfig: options.RealityConfig.Build(), MuxOption: options.MuxOption.Build(), diff --git a/listener/inbound/trojan_test.go b/listener/inbound/trojan_test.go index a2bf4b98..e482a8a0 100644 --- a/listener/inbound/trojan_test.go +++ b/listener/inbound/trojan_test.go @@ -168,6 +168,37 @@ func TestInboundTrojan_Grpc2(t *testing.T) { testInboundTrojanTLS(t, inboundOptions, outboundOptions) } +func testInboundTrojanShadowTLS(t *testing.T, inboundOptions inbound.TrojanOption, outboundOptions outbound.TrojanOption) { + t.Parallel() + t.Run("Conn", func(t *testing.T) { + inboundOptions, outboundOptions := inboundOptions, outboundOptions // don't modify outside options value + testInboundTrojan(t, inboundOptions, outboundOptions) + }) + t.Run("UConn", func(t *testing.T) { + inboundOptions, outboundOptions := inboundOptions, outboundOptions // don't modify outside options value + outboundOptions.ClientFingerprint = "chrome" + testInboundTrojan(t, inboundOptions, outboundOptions) + }) +} + +func TestInboundTrojan_ShadowTLS(t *testing.T) { + const password = "shadow-tls-password" + inboundOptions := inbound.TrojanOption{ + ShadowTLS: inbound.ShadowTLS{ + Enable: true, + Version: 3, + Users: []inbound.ShadowTLSUser{{Name: "test", Password: password}}, + Handshake: inbound.ShadowTLSHandshakeOptions{Dest: net.JoinHostPort(realityDest, "443")}, + }, + } + outboundOptions := outbound.TrojanOption{ + SNI: realityDest, + Fingerprint: tlsFingerprint, + ShadowTLSOpts: outbound.ShadowTLSOptions{Password: password, Version: 3}, + } + testInboundTrojanShadowTLS(t, inboundOptions, outboundOptions) +} + func testInboundTrojanJLS(t *testing.T, inboundOptions inbound.TrojanOption, outboundOptions outbound.TrojanOption) { t.Parallel() t.Run("Conn", func(t *testing.T) { diff --git a/listener/inbound/vless.go b/listener/inbound/vless.go index ff71439d..8c03b778 100644 --- a/listener/inbound/vless.go +++ b/listener/inbound/vless.go @@ -22,6 +22,7 @@ type VlessOption struct { ClientAuthCert string `inbound:"client-auth-cert,omitempty"` EchKey string `inbound:"ech-key,omitempty"` AllowInsecure bool `inbound:"allow-insecure,omitempty"` + ShadowTLS ShadowTLS `inbound:"shadow-tls,omitempty"` JLSConfig JLSConfig `inbound:"jls-config,omitempty"` RealityConfig RealityConfig `inbound:"reality-config,omitempty"` MuxOption MuxOption `inbound:"mux-option,omitempty"` @@ -124,6 +125,7 @@ func NewVless(options *VlessOption) (*Vless, error) { ClientAuthCert: options.ClientAuthCert, EchKey: options.EchKey, AllowInsecure: options.AllowInsecure, + ShadowTLS: options.ShadowTLS.Build(), JLSConfig: options.JLSConfig.Build(), RealityConfig: options.RealityConfig.Build(), MuxOption: options.MuxOption.Build(), diff --git a/listener/inbound/vless_test.go b/listener/inbound/vless_test.go index bbffa5d2..c8647d9e 100644 --- a/listener/inbound/vless_test.go +++ b/listener/inbound/vless_test.go @@ -278,6 +278,38 @@ func TestInboundVless_Grpc2(t *testing.T) { testInboundVlessTLS(t, inboundOptions, outboundOptions, false) } +func testInboundVlessShadowTLS(t *testing.T, inboundOptions inbound.VlessOption, outboundOptions outbound.VlessOption) { + t.Parallel() + t.Run("Conn", func(t *testing.T) { + inboundOptions, outboundOptions := inboundOptions, outboundOptions // don't modify outside options value + testInboundVless(t, inboundOptions, outboundOptions) + }) + t.Run("UConn", func(t *testing.T) { + inboundOptions, outboundOptions := inboundOptions, outboundOptions // don't modify outside options value + outboundOptions.ClientFingerprint = "chrome" + testInboundVless(t, inboundOptions, outboundOptions) + }) +} + +func TestInboundVless_ShadowTLS(t *testing.T) { + const password = "shadow-tls-password" + inboundOptions := inbound.VlessOption{ + ShadowTLS: inbound.ShadowTLS{ + Enable: true, + Version: 3, + Users: []inbound.ShadowTLSUser{{Name: "test", Password: password}}, + Handshake: inbound.ShadowTLSHandshakeOptions{Dest: net.JoinHostPort(realityDest, "443")}, + }, + } + outboundOptions := outbound.VlessOption{ + TLS: true, + ServerName: realityDest, + Fingerprint: tlsFingerprint, + ShadowTLSOpts: outbound.ShadowTLSOptions{Password: password, Version: 3}, + } + testInboundVlessShadowTLS(t, inboundOptions, outboundOptions) +} + func testInboundVlessJLS(t *testing.T, inboundOptions inbound.VlessOption, outboundOptions outbound.VlessOption) { t.Parallel() t.Run("Conn", func(t *testing.T) { diff --git a/listener/inbound/vmess.go b/listener/inbound/vmess.go index ec2aee6f..2fe30b04 100644 --- a/listener/inbound/vmess.go +++ b/listener/inbound/vmess.go @@ -19,6 +19,7 @@ type VmessOption struct { ClientAuthType string `inbound:"client-auth-type,omitempty"` ClientAuthCert string `inbound:"client-auth-cert,omitempty"` EchKey string `inbound:"ech-key,omitempty"` + ShadowTLS ShadowTLS `inbound:"shadow-tls,omitempty"` JLSConfig JLSConfig `inbound:"jls-config,omitempty"` RealityConfig RealityConfig `inbound:"reality-config,omitempty"` TLSMirrorConfig TLSMirrorConfig `inbound:"tlsmirror-config,omitempty"` @@ -71,6 +72,7 @@ func NewVmess(options *VmessOption) (*Vmess, error) { ClientAuthType: options.ClientAuthType, ClientAuthCert: options.ClientAuthCert, EchKey: options.EchKey, + ShadowTLS: options.ShadowTLS.Build(), JLSConfig: options.JLSConfig.Build(), RealityConfig: options.RealityConfig.Build(), TLSMirrorConfig: options.TLSMirrorConfig.Build(), diff --git a/listener/inbound/vmess_test.go b/listener/inbound/vmess_test.go index fa66be10..9401f2f4 100644 --- a/listener/inbound/vmess_test.go +++ b/listener/inbound/vmess_test.go @@ -502,6 +502,38 @@ func TestInboundVMess_Grpc2(t *testing.T) { testInboundVMessTLS(t, inboundOptions, outboundOptions) } +func testInboundVMessShadowTLS(t *testing.T, inboundOptions inbound.VmessOption, outboundOptions outbound.VmessOption) { + t.Parallel() + t.Run("Conn", func(t *testing.T) { + inboundOptions, outboundOptions := inboundOptions, outboundOptions // don't modify outside options value + testInboundVMess(t, inboundOptions, outboundOptions) + }) + t.Run("UConn", func(t *testing.T) { + inboundOptions, outboundOptions := inboundOptions, outboundOptions // don't modify outside options value + outboundOptions.ClientFingerprint = "chrome" + testInboundVMess(t, inboundOptions, outboundOptions) + }) +} + +func TestInboundVMess_ShadowTLS(t *testing.T) { + const password = "shadow-tls-password" + inboundOptions := inbound.VmessOption{ + ShadowTLS: inbound.ShadowTLS{ + Enable: true, + Version: 3, + Users: []inbound.ShadowTLSUser{{Name: "test", Password: password}}, + Handshake: inbound.ShadowTLSHandshakeOptions{Dest: net.JoinHostPort(realityDest, "443")}, + }, + } + outboundOptions := outbound.VmessOption{ + TLS: true, + ServerName: realityDest, + Fingerprint: tlsFingerprint, + ShadowTLSOpts: outbound.ShadowTLSOptions{Password: password, Version: 3}, + } + testInboundVMessShadowTLS(t, inboundOptions, outboundOptions) +} + func testInboundVMessJLS(t *testing.T, inboundOptions inbound.VmessOption, outboundOptions outbound.VmessOption) { t.Parallel() t.Run("Conn", func(t *testing.T) { diff --git a/listener/sing_vless/server.go b/listener/sing_vless/server.go index a55ce65c..cba040d8 100644 --- a/listener/sing_vless/server.go +++ b/listener/sing_vless/server.go @@ -14,6 +14,7 @@ import ( LC "github.com/metacubex/mihomo/listener/config" "github.com/metacubex/mihomo/listener/jls" "github.com/metacubex/mihomo/listener/reality" + "github.com/metacubex/mihomo/listener/shadowtls" "github.com/metacubex/mihomo/listener/sing" "github.com/metacubex/mihomo/ntp" "github.com/metacubex/mihomo/transport/gun" @@ -85,6 +86,7 @@ func New(config LC.VlessServer, lc C.InboundListenConfig, tunnel C.Tunnel, addit Protocols: new(http.Protocols), } tlsConfig := &tls.Config{Time: ntp.Now} + var shadowTLSBuilder *shadowtls.Builder var jlsBuilder *jls.Builder var realityBuilder *reality.Builder @@ -129,6 +131,21 @@ func New(config LC.VlessServer, lc C.InboundListenConfig, tunnel C.Tunnel, addit return nil, err } } + if config.ShadowTLS.Enable { + if tlsConfig.GetCertificate != nil { + return nil, errors.New("certificate is unavailable in ShadowTLS") + } + if tlsConfig.ClientAuth != tls.NoClientCert { + return nil, errors.New("client-auth is unavailable in ShadowTLS") + } + if realityBuilder != nil { + return nil, errors.New("REALITY is unavailable in ShadowTLS") + } + shadowTLSBuilder, err = shadowtls.New(config.ShadowTLS, tunnel) + if err != nil { + return nil, err + } + } if config.JLSConfig.Enable { if tlsConfig.GetCertificate != nil { return nil, errors.New("certificate is unavailable in JLS") @@ -139,6 +156,9 @@ func New(config LC.VlessServer, lc C.InboundListenConfig, tunnel C.Tunnel, addit if realityBuilder != nil { return nil, errors.New("REALITY is unavailable in JLS") } + if shadowTLSBuilder != nil { + return nil, errors.New("ShadowTLS is unavailable in JLS") + } jlsBuilder, err = jls.New(config.JLSConfig, tunnel) if err != nil { return nil, err @@ -242,14 +262,16 @@ func New(config LC.VlessServer, lc C.InboundListenConfig, tunnel C.Tunnel, addit if err != nil { return nil, err } - if jlsBuilder != nil { + if shadowTLSBuilder != nil { + l = shadowTLSBuilder.NewListener(l) + } else if jlsBuilder != nil { l = jlsBuilder.NewListener(l) } else if realityBuilder != nil { l = realityBuilder.NewListener(l) } else if tlsConfig.GetCertificate != nil { l = tls.NewListener(l, tlsConfig) } else if sl.decryption == nil && !config.AllowInsecure { - return nil, errors.New("disallow using Vless without any certificates/jls/reality/decryption/allow-insecure config") + return nil, errors.New("disallow using Vless without any certificates/shadow-tls/jls/reality/decryption/allow-insecure config") } sl.listeners = append(sl.listeners, l) diff --git a/listener/sing_vmess/server.go b/listener/sing_vmess/server.go index 48a7408b..281d725e 100644 --- a/listener/sing_vmess/server.go +++ b/listener/sing_vmess/server.go @@ -14,6 +14,7 @@ import ( LC "github.com/metacubex/mihomo/listener/config" "github.com/metacubex/mihomo/listener/jls" "github.com/metacubex/mihomo/listener/reality" + "github.com/metacubex/mihomo/listener/shadowtls" "github.com/metacubex/mihomo/listener/sing" "github.com/metacubex/mihomo/listener/tlsmirror" "github.com/metacubex/mihomo/ntp" @@ -95,6 +96,7 @@ func New(config LC.VmessServer, lc C.InboundListenConfig, tunnel C.Tunnel, addit Protocols: new(http.Protocols), } tlsConfig := &tls.Config{Time: ntp.Now} + var shadowTLSBuilder *shadowtls.Builder var jlsBuilder *jls.Builder var realityBuilder *reality.Builder var tlsMirrorBuilder *tlsmirror.Builder @@ -153,6 +155,27 @@ func New(config LC.VmessServer, lc C.InboundListenConfig, tunnel C.Tunnel, addit }.Build(tunnel) h.Tunnel = tlsMirrorBuilder.WrapTunnel(tunnel) } + if config.ShadowTLS.Enable { + if tlsConfig.GetCertificate != nil { + return nil, errors.New("certificate is unavailable in ShadowTLS") + } + if tlsConfig.ClientAuth != tls.NoClientCert { + return nil, errors.New("client-auth is unavailable in ShadowTLS") + } + if realityBuilder != nil { + return nil, errors.New("REALITY is unavailable in ShadowTLS") + } + if tlsMirrorBuilder != nil { + return nil, errors.New("TLSMirror is unavailable in ShadowTLS") + } + if config.MKCPConfig.Enable { + return nil, errors.New("ShadowTLS only supports TCP transports") + } + shadowTLSBuilder, err = shadowtls.New(config.ShadowTLS, tunnel) + if err != nil { + return nil, err + } + } if config.JLSConfig.Enable { if tlsConfig.GetCertificate != nil { return nil, errors.New("certificate is unavailable in JLS") @@ -166,6 +189,9 @@ func New(config LC.VmessServer, lc C.InboundListenConfig, tunnel C.Tunnel, addit if tlsMirrorBuilder != nil { return nil, errors.New("TLSMirror is unavailable in JLS") } + if shadowTLSBuilder != nil { + return nil, errors.New("ShadowTLS is unavailable in JLS") + } if config.MKCPConfig.Enable { return nil, errors.New("JLS only supports TCP transports") } @@ -237,7 +263,9 @@ func New(config LC.VmessServer, lc C.InboundListenConfig, tunnel C.Tunnel, addit return nil, err } } - if jlsBuilder != nil { + if shadowTLSBuilder != nil { + l = shadowTLSBuilder.NewListener(l) + } else if jlsBuilder != nil { l = jlsBuilder.NewListener(l) } else if tlsMirrorBuilder != nil { l = tlsMirrorBuilder.NewListener(l) diff --git a/listener/trojan/server.go b/listener/trojan/server.go index b440e0e9..5aee1e87 100644 --- a/listener/trojan/server.go +++ b/listener/trojan/server.go @@ -17,6 +17,7 @@ import ( LC "github.com/metacubex/mihomo/listener/config" "github.com/metacubex/mihomo/listener/jls" "github.com/metacubex/mihomo/listener/reality" + "github.com/metacubex/mihomo/listener/shadowtls" "github.com/metacubex/mihomo/listener/sing" "github.com/metacubex/mihomo/ntp" "github.com/metacubex/mihomo/transport/gun" @@ -81,6 +82,7 @@ func New(config LC.TrojanServer, lc C.InboundListenConfig, tunnel C.Tunnel, addi Protocols: new(http.Protocols), } tlsConfig := &tls.Config{Time: ntp.Now} + var shadowTLSBuilder *shadowtls.Builder var jlsBuilder *jls.Builder var realityBuilder *reality.Builder @@ -125,6 +127,21 @@ func New(config LC.TrojanServer, lc C.InboundListenConfig, tunnel C.Tunnel, addi return nil, err } } + if config.ShadowTLS.Enable { + if tlsConfig.GetCertificate != nil { + return nil, errors.New("certificate is unavailable in ShadowTLS") + } + if tlsConfig.ClientAuth != tls.NoClientCert { + return nil, errors.New("client-auth is unavailable in ShadowTLS") + } + if realityBuilder != nil { + return nil, errors.New("REALITY is unavailable in ShadowTLS") + } + shadowTLSBuilder, err = shadowtls.New(config.ShadowTLS, tunnel) + if err != nil { + return nil, err + } + } if config.JLSConfig.Enable { if tlsConfig.GetCertificate != nil { return nil, errors.New("certificate is unavailable in JLS") @@ -135,6 +152,9 @@ func New(config LC.TrojanServer, lc C.InboundListenConfig, tunnel C.Tunnel, addi if realityBuilder != nil { return nil, errors.New("REALITY is unavailable in JLS") } + if shadowTLSBuilder != nil { + return nil, errors.New("ShadowTLS is unavailable in JLS") + } jlsBuilder, err = jls.New(config.JLSConfig, tunnel) if err != nil { return nil, err @@ -182,14 +202,16 @@ func New(config LC.TrojanServer, lc C.InboundListenConfig, tunnel C.Tunnel, addi if err != nil { return nil, err } - if jlsBuilder != nil { + if shadowTLSBuilder != nil { + l = shadowTLSBuilder.NewListener(l) + } else if jlsBuilder != nil { l = jlsBuilder.NewListener(l) } else if realityBuilder != nil { l = realityBuilder.NewListener(l) } else if tlsConfig.GetCertificate != nil { l = tls.NewListener(l, tlsConfig) } else if !config.TrojanSSOption.Enabled && !config.AllowInsecure { - return nil, errors.New("disallow using Trojan without any certificates/jls/reality/ss/allow-insecure config") + return nil, errors.New("disallow using Trojan without any certificates/shadow-tls/jls/reality/ss/allow-insecure config") } sl.listeners = append(sl.listeners, l) diff --git a/transport/gun/gun.go b/transport/gun/gun.go index 09881f22..ebbfbe6d 100644 --- a/transport/gun/gun.go +++ b/transport/gun/gun.go @@ -279,7 +279,7 @@ func NewTransport(dialFn DialFn, tlsConfig *vmess.TLSConfig, gunCfg *Config) *Tr return nil, err } - if tlsConfig.Reality == nil && tlsConfig.TLSMirror == nil { // reality and tlsmirror don't return the negotiated ALPN + if tlsConfig.ShadowTLS == nil && tlsConfig.Reality == nil && tlsConfig.TLSMirror == nil { // shadowtls, reality and tlsmirror don't return the negotiated ALPN state := tlsC.GetTLSConnectionState(conn) if p := state.NegotiatedProtocol; p != Http2NextProtoTLS { _ = conn.Close() diff --git a/transport/shadowtls/client.go b/transport/shadowtls/client.go index c70bffb3..43d8b116 100644 --- a/transport/shadowtls/client.go +++ b/transport/shadowtls/client.go @@ -45,6 +45,24 @@ type ShadowTLSOption struct { ALPN []string } +type Config struct { + Password string + Version int +} + +func NewConfig(password string, version int) (*Config, error) { + if version == 0 { + version = 2 + } + if err := checkVersion(version); err != nil { + return nil, err + } + return &Config{ + Password: password, + Version: version, + }, nil +} + type ClientConfig struct { Version int Password string diff --git a/transport/vmess/tls.go b/transport/vmess/tls.go index 2df06688..666c3b15 100644 --- a/transport/vmess/tls.go +++ b/transport/vmess/tls.go @@ -9,6 +9,7 @@ import ( "github.com/metacubex/mihomo/component/ech" tlsC "github.com/metacubex/mihomo/component/tls" "github.com/metacubex/mihomo/transport/jls" + "github.com/metacubex/mihomo/transport/shadowtls" "github.com/metacubex/mihomo/transport/tlsmirror" "github.com/metacubex/tls" @@ -24,6 +25,7 @@ type TLSConfig struct { ClientFingerprint string NextProtos []string ECH *ech.Config + ShadowTLS *shadowtls.Config JLS *jls.Config Reality *tlsC.RealityConfig TLSMirror *tlsmirror.Config @@ -45,6 +47,24 @@ func (cfg *TLSConfig) ToStdConfig() (*tls.Config, error) { } func StreamTLSConn(ctx context.Context, conn net.Conn, cfg *TLSConfig) (net.Conn, error) { + if cfg.ShadowTLS != nil { + alpn := cfg.NextProtos + if alpn == nil { + alpn = shadowtls.DefaultALPN + } + return shadowtls.NewShadowTLS(ctx, conn, &shadowtls.ShadowTLSOption{ + Password: cfg.ShadowTLS.Password, + Host: cfg.Host, + Fingerprint: cfg.FingerPrint, + Certificate: cfg.Certificate, + PrivateKey: cfg.PrivateKey, + ClientFingerprint: cfg.ClientFingerprint, + SkipCertVerify: cfg.SkipCertVerify, + NameCertVerify: cfg.NameCertVerify, + Version: cfg.ShadowTLS.Version, + ALPN: alpn, + }) + } if cfg.JLS != nil { return jls.NewClient(ctx, conn, &jls.ClientConfig{ Config: *cfg.JLS,