mirror of
https://github.com/MetaCubeX/mihomo.git
synced 2026-12-06 20:18:33 +00:00
feat: support shadowtls for anytls outbound and listener
This commit is contained in:
1 parent
c015694bf3
commit
bc0e5aa835
6 files changed
+98
-22
No files matched your search
+28
-18
@@ -2,6 +2,7 @@ package outbound
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net"
|
||||
"strconv"
|
||||
"time"
|
||||
@@ -24,24 +25,25 @@ type AnyTLS struct {
|
||||
|
||||
type AnyTLSOption 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"`
|
||||
ECHOpts ECHOptions `proxy:"ech-opts,omitempty"`
|
||||
JLSOpts JLSOptions `proxy:"jls-opts,omitempty"`
|
||||
ClientFingerprint string `proxy:"client-fingerprint,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"`
|
||||
IdleSessionCheckInterval int `proxy:"idle-session-check-interval,omitempty"`
|
||||
IdleSessionTimeout int `proxy:"idle-session-timeout,omitempty"`
|
||||
MinIdleSession int `proxy:"min-idle-session,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"`
|
||||
ECHOpts ECHOptions `proxy:"ech-opts,omitempty"`
|
||||
ShadowTLSOpts ShadowTLSOptions `proxy:"shadow-tls-opts,omitempty"`
|
||||
JLSOpts JLSOptions `proxy:"jls-opts,omitempty"`
|
||||
ClientFingerprint string `proxy:"client-fingerprint,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"`
|
||||
IdleSessionCheckInterval int `proxy:"idle-session-check-interval,omitempty"`
|
||||
IdleSessionTimeout int `proxy:"idle-session-timeout,omitempty"`
|
||||
MinIdleSession int `proxy:"min-idle-session,omitempty"`
|
||||
}
|
||||
|
||||
func (t *AnyTLS) DialContext(ctx context.Context, metadata *C.Metadata) (_ C.Conn, err error) {
|
||||
@@ -117,10 +119,17 @@ func NewAnyTLS(option AnyTLSOption) (*AnyTLS, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
shadowTLSConfig, err := option.ShadowTLSOpts.Parse()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
jlsConfig, err := option.JLSOpts.Parse()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if shadowTLSConfig != nil && jlsConfig != nil {
|
||||
return nil, errors.New("ShadowTLS is incompatible with JLS")
|
||||
}
|
||||
tlsConfig := &vmess.TLSConfig{
|
||||
Host: option.SNI,
|
||||
SkipCertVerify: option.SkipCertVerify,
|
||||
@@ -131,6 +140,7 @@ func NewAnyTLS(option AnyTLSOption) (*AnyTLS, error) {
|
||||
PrivateKey: option.PrivateKey,
|
||||
ClientFingerprint: option.ClientFingerprint,
|
||||
ECH: echConfig,
|
||||
ShadowTLS: shadowTLSConfig,
|
||||
JLS: jlsConfig,
|
||||
}
|
||||
if tlsConfig.Host == "" {
|
||||
|
||||
+15
-2
@@ -1564,6 +1564,9 @@ proxies: # socks5
|
||||
# - http/1.1
|
||||
# skip-cert-verify: true
|
||||
# name-cert-verify: example.com # 仅修改证书 DNSName 校验目标,不修改 SNI
|
||||
# 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
|
||||
@@ -2339,7 +2342,7 @@ listeners:
|
||||
users:
|
||||
username1: password1
|
||||
username2: password2
|
||||
# 未启用 "jls-config" 且 "allow-insecure" 不为 true 时,必须填写 "certificate" 和 "private-key";启用 JLS 时不要填写
|
||||
# "shadow-tls" 和 "jls-config" 均未启用且 "allow-insecure" 不为 true 时,必须填写 "certificate" 和 "private-key";启用 ShadowTLS 或 JLS 时不要填写
|
||||
certificate: ./server.crt # 证书 PEM 格式,或者 证书的路径
|
||||
private-key: ./server.key
|
||||
# 下面两项为mTLS配置项,如果client-auth-type设置为 "verify-if-given" 或 "require-and-verify" 则client-auth-cert必须不为空
|
||||
@@ -2352,6 +2355,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:
|
||||
@@ -2362,7 +2375,7 @@ listeners:
|
||||
# # alpn: [h2, http/1.1]
|
||||
# # proxy: ""
|
||||
# # rate-limit: 0 # fallback 转发限速,单位 bit/s;0 表示不限速
|
||||
### 注意,anytls listener, 如果 "allow-insecure" 不为 true, 至少需要填写 “certificate和private-key” 或 “jls-config” 的其中一项 ###
|
||||
### 注意,anytls listener, 如果 "allow-insecure" 不为 true, 至少需要填写 “certificate和private-key” 或 “shadow-tls” 或 “jls-config” 的其中一项 ###
|
||||
# allow-insecure: false # 是否允许不开启tls加密(注意:仅用于有 nginx, caddy 前置的情况)
|
||||
# padding-scheme: "" # https://github.com/anytls/anytls-go/blob/main/docs/protocol.md#cmdupdatepaddingscheme
|
||||
|
||||
|
||||
@@ -16,6 +16,7 @@ import (
|
||||
C "github.com/metacubex/mihomo/constant"
|
||||
LC "github.com/metacubex/mihomo/listener/config"
|
||||
"github.com/metacubex/mihomo/listener/jls"
|
||||
"github.com/metacubex/mihomo/listener/shadowtls"
|
||||
"github.com/metacubex/mihomo/listener/sing"
|
||||
"github.com/metacubex/mihomo/ntp"
|
||||
"github.com/metacubex/mihomo/transport/anytls/padding"
|
||||
@@ -44,6 +45,7 @@ func New(config LC.AnyTLSServer, lc C.InboundListenConfig, tunnel C.Tunnel, addi
|
||||
}
|
||||
}
|
||||
|
||||
var shadowTLSBuilder *shadowtls.Builder
|
||||
var jlsBuilder *jls.Builder
|
||||
tlsConfig := &tls.Config{Time: ntp.Now}
|
||||
if config.Certificate != "" && config.PrivateKey != "" {
|
||||
@@ -75,6 +77,18 @@ func New(config LC.AnyTLSServer, lc C.InboundListenConfig, tunnel C.Tunnel, addi
|
||||
}
|
||||
tlsConfig.ClientCAs = pool
|
||||
}
|
||||
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")
|
||||
}
|
||||
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")
|
||||
@@ -82,6 +96,9 @@ func New(config LC.AnyTLSServer, lc C.InboundListenConfig, tunnel C.Tunnel, addi
|
||||
if tlsConfig.ClientAuth != tls.NoClientCert {
|
||||
return nil, errors.New("client-auth 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
|
||||
@@ -124,12 +141,14 @@ func New(config LC.AnyTLSServer, 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 tlsConfig.GetCertificate != nil {
|
||||
l = tls.NewListener(l, tlsConfig)
|
||||
} else if !config.AllowInsecure {
|
||||
return nil, errors.New("disallow using AnyTLS without certificates/jls/allow-insecure config")
|
||||
return nil, errors.New("disallow using AnyTLS without certificates/shadow-tls/jls/allow-insecure config")
|
||||
}
|
||||
sl.listeners = append(sl.listeners, l)
|
||||
|
||||
|
||||
@@ -13,6 +13,7 @@ type AnyTLSServer struct {
|
||||
ClientAuthType string `yaml:"client-auth-type" json:"client-auth-type,omitempty"`
|
||||
ClientAuthCert string `yaml:"client-auth-cert" json:"client-auth-cert,omitempty"`
|
||||
EchKey string `yaml:"ech-key" json:"ech-key,omitempty"`
|
||||
ShadowTLS ShadowTLS `yaml:"shadow-tls" json:"shadow-tls,omitempty"`
|
||||
JLSConfig JLSConfig `yaml:"jls-config" json:"jls-config,omitempty"`
|
||||
AllowInsecure bool `yaml:"allow-insecure" json:"allow-insecure,omitempty"`
|
||||
PaddingScheme string `yaml:"padding-scheme" json:"padding-scheme,omitempty"`
|
||||
|
||||
@@ -17,6 +17,7 @@ type AnyTLSOption 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"`
|
||||
AllowInsecure bool `inbound:"allow-insecure,omitempty"`
|
||||
PaddingScheme string `inbound:"padding-scheme,omitempty"`
|
||||
@@ -50,6 +51,7 @@ func NewAnyTLS(options *AnyTLSOption) (*AnyTLS, error) {
|
||||
ClientAuthType: options.ClientAuthType,
|
||||
ClientAuthCert: options.ClientAuthCert,
|
||||
EchKey: options.EchKey,
|
||||
ShadowTLS: options.ShadowTLS.Build(),
|
||||
JLSConfig: options.JLSConfig.Build(),
|
||||
AllowInsecure: options.AllowInsecure,
|
||||
PaddingScheme: options.PaddingScheme,
|
||||
|
||||
@@ -96,6 +96,37 @@ func TestInboundAnyTLS_TLS(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func testInboundAnyTLSShadowTLS(t *testing.T, inboundOptions inbound.AnyTLSOption, outboundOptions outbound.AnyTLSOption) {
|
||||
t.Parallel()
|
||||
t.Run("Conn", func(t *testing.T) {
|
||||
inboundOptions, outboundOptions := inboundOptions, outboundOptions // don't modify outside options value
|
||||
testInboundAnyTLS(t, inboundOptions, outboundOptions)
|
||||
})
|
||||
t.Run("UConn", func(t *testing.T) {
|
||||
inboundOptions, outboundOptions := inboundOptions, outboundOptions // don't modify outside options value
|
||||
outboundOptions.ClientFingerprint = "chrome"
|
||||
testInboundAnyTLS(t, inboundOptions, outboundOptions)
|
||||
})
|
||||
}
|
||||
|
||||
func TestInboundAnyTLS_ShadowTLS(t *testing.T) {
|
||||
const password = "shadow-tls-password"
|
||||
inboundOptions := inbound.AnyTLSOption{
|
||||
ShadowTLS: inbound.ShadowTLS{
|
||||
Enable: true,
|
||||
Version: 3,
|
||||
Users: []inbound.ShadowTLSUser{{Name: "test", Password: password}},
|
||||
Handshake: inbound.ShadowTLSHandshakeOptions{Dest: net.JoinHostPort(realityDest, "443")},
|
||||
},
|
||||
}
|
||||
outboundOptions := outbound.AnyTLSOption{
|
||||
SNI: realityDest,
|
||||
Fingerprint: tlsFingerprint,
|
||||
ShadowTLSOpts: outbound.ShadowTLSOptions{Password: password, Version: 3},
|
||||
}
|
||||
testInboundAnyTLSShadowTLS(t, inboundOptions, outboundOptions)
|
||||
}
|
||||
|
||||
func testInboundAnyTLSJLS(t *testing.T, inboundOptions inbound.AnyTLSOption, outboundOptions outbound.AnyTLSOption) {
|
||||
t.Parallel()
|
||||
t.Run("Conn", func(t *testing.T) {
|
||||
|
||||
Reference in new issue
Block a user