feat: support shadowtls for vmess/vless/trojan outbound and listener

This commit is contained in:
wwqgtxx committed 2026-07-15 15:51:35 +08:00
1 parent 8413f50407
commit c015694bf3
20 files changed
+396 -40

No files matched your search

+15
View File
@@ -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)
}
+47 -24
View File
@@ -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,
}
+37 -4
View File
@@ -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")
}
+36 -4
View File
@@ -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(),
+41 -2
View File
@@ -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
+1
View File
@@ -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
+1
View File
@@ -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"`
+1
View File
@@ -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"`
+2
View File
@@ -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(),
+31
View File
@@ -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) {
+2
View File
@@ -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(),
+32
View File
@@ -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) {
+2
View File
@@ -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(),
+32
View File
@@ -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) {
+24 -2
View File
@@ -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)
+29 -1
View File
@@ -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)
+24 -2
View File
@@ -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)
+1 -1
View File
@@ -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()
+18
View File
@@ -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
+20
View File
@@ -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,