From da5a3e52aec3adbc821ccae8ad9e47e602b5a76b Mon Sep 17 00:00:00 2001 From: wwqgtxx Date: Wed, 15 Jul 2026 17:55:48 +0800 Subject: [PATCH] chore: rewrite the internal mutual exclusion logic of the outbound --- adapter/outbound/anytls.go | 19 ++++--- adapter/outbound/trojan.go | 29 +++++----- adapter/outbound/vless.go | 106 ++++++++++++++----------------------- adapter/outbound/vmess.go | 69 +++++++++--------------- 4 files changed, 88 insertions(+), 135 deletions(-) diff --git a/adapter/outbound/anytls.go b/adapter/outbound/anytls.go index ce8f4f8dc..aad043705 100644 --- a/adapter/outbound/anytls.go +++ b/adapter/outbound/anytls.go @@ -5,6 +5,7 @@ import ( "errors" "net" "strconv" + "strings" "time" N "github.com/metacubex/mihomo/common/net" @@ -132,16 +133,18 @@ func NewAnyTLS(option AnyTLSOption) (*AnyTLS, error) { if err != nil { return nil, err } + securityModes := make([]string, 0, 3) if shadowTLSConfig != nil { - if restlsConfig != nil { - return nil, errors.New("ShadowTLS is incompatible with Restls") - } - if jlsConfig != nil { - return nil, errors.New("ShadowTLS is incompatible with JLS") - } + securityModes = append(securityModes, "ShadowTLS") } - if restlsConfig != nil && jlsConfig != nil { - return nil, errors.New("Restls is incompatible with JLS") + if restlsConfig != nil { + securityModes = append(securityModes, "Restls") + } + if jlsConfig != nil { + securityModes = append(securityModes, "JLS") + } + if len(securityModes) > 1 { + return nil, errors.New("security modes are mutually exclusive: " + strings.Join(securityModes, ", ")) } tlsConfig := &vmess.TLSConfig{ Host: option.SNI, diff --git a/adapter/outbound/trojan.go b/adapter/outbound/trojan.go index 5b2a6846a..d581e80a6 100644 --- a/adapter/outbound/trojan.go +++ b/adapter/outbound/trojan.go @@ -6,6 +6,7 @@ import ( "fmt" "net" "strconv" + "strings" N "github.com/metacubex/mihomo/common/net" "github.com/metacubex/mihomo/component/ca" @@ -325,27 +326,21 @@ func NewTrojan(option TrojanOption) (*Trojan, error) { if err != nil { return nil, err } + securityModes := make([]string, 0, 4) if t.shadowTLSConfig != nil { - if t.restlsConfig != nil { - return nil, errors.New("ShadowTLS is incompatible with Restls") - } - 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") - } + securityModes = append(securityModes, "ShadowTLS") } if t.restlsConfig != nil { - if t.jlsConfig != nil { - return nil, errors.New("Restls is incompatible with JLS") - } - if t.realityConfig != nil { - return nil, errors.New("Restls is incompatible with REALITY") - } + securityModes = append(securityModes, "Restls") } - if t.jlsConfig != nil && t.realityConfig != nil { - return nil, errors.New("JLS is incompatible with REALITY") + if t.jlsConfig != nil { + securityModes = append(securityModes, "JLS") + } + if t.realityConfig != nil { + securityModes = append(securityModes, "REALITY") + } + if len(securityModes) > 1 { + return nil, errors.New("security modes are mutually exclusive: " + strings.Join(securityModes, ", ")) } if option.SSOpts.Enabled { diff --git a/adapter/outbound/vless.go b/adapter/outbound/vless.go index 3fd4d7ae1..4d076b592 100644 --- a/adapter/outbound/vless.go +++ b/adapter/outbound/vless.go @@ -6,6 +6,7 @@ import ( "fmt" "net" "strconv" + "strings" "time" "github.com/metacubex/mihomo/common/convert" @@ -531,38 +532,28 @@ func NewVless(option VlessOption) (*Vless, error) { if err != nil { return nil, err } + securityModes := make([]string, 0, 4) if v.shadowTLSConfig != nil { - if !option.TLS { - return nil, errors.New("ShadowTLS requires TLS") - } - if v.restlsConfig != nil { - return nil, errors.New("ShadowTLS is incompatible with Restls") - } - 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") - } + securityModes = append(securityModes, "ShadowTLS") } if v.restlsConfig != nil { - if !option.TLS { - return nil, errors.New("Restls requires TLS") - } - if v.jlsConfig != nil { - return nil, errors.New("Restls is incompatible with JLS") - } - if v.realityConfig != nil { - return nil, errors.New("Restls is incompatible with REALITY") - } + securityModes = append(securityModes, "Restls") } if v.jlsConfig != nil { - if !option.TLS { - return nil, errors.New("JLS requires TLS") - } - if v.realityConfig != nil { - return nil, errors.New("JLS is incompatible with REALITY") - } + securityModes = append(securityModes, "JLS") + } + if v.realityConfig != nil { + securityModes = append(securityModes, "REALITY") + } + if len(securityModes) > 1 { + return nil, errors.New("security modes are mutually exclusive: " + strings.Join(securityModes, ", ")) + } + securityMode := "" + if len(securityModes) == 1 { + securityMode = securityModes[0] + } + if securityMode != "" && !option.TLS { + return nil, fmt.Errorf("%s requires TLS", securityMode) } switch option.Network { @@ -698,17 +689,8 @@ 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.restlsConfig != nil { - return nil, errors.New("xhttp HTTP/3 does not support Restls") - } - if v.jlsConfig != nil { - return nil, errors.New("xhttp HTTP/3 does not support JLS") - } - if v.realityConfig != nil { - return nil, errors.New("xhttp HTTP/3 does not support reality") + if securityMode != "" { + return nil, fmt.Errorf("xhttp HTTP/3 does not support %s", securityMode) } tlsConfig, err := tlsOpts.ToStdConfig() if err != nil { @@ -782,27 +764,28 @@ func NewVless(option VlessOption) (*Vless, error) { return nil, err } } + downloadSecurityModes := make([]string, 0, 4) if downloadShadowTLSConfig != nil { - if downloadRestlsConfig != nil { - return nil, errors.New("xhttp download-settings: ShadowTLS is incompatible with Restls") - } - if downloadJLSConfig != nil { - return nil, errors.New("xhttp download-settings: ShadowTLS is incompatible with JLS") - } - if downloadRealityCfg != nil { - return nil, errors.New("xhttp download-settings: ShadowTLS is incompatible with REALITY") - } + downloadSecurityModes = append(downloadSecurityModes, "ShadowTLS") } if downloadRestlsConfig != nil { - if downloadJLSConfig != nil { - return nil, errors.New("xhttp download-settings: Restls is incompatible with JLS") - } - if downloadRealityCfg != nil { - return nil, errors.New("xhttp download-settings: Restls is incompatible with REALITY") - } + downloadSecurityModes = append(downloadSecurityModes, "Restls") } - if downloadJLSConfig != nil && downloadRealityCfg != nil { - return nil, errors.New("xhttp download-settings: JLS is incompatible with REALITY") + if downloadJLSConfig != nil { + downloadSecurityModes = append(downloadSecurityModes, "JLS") + } + if downloadRealityCfg != nil { + downloadSecurityModes = append(downloadSecurityModes, "REALITY") + } + if len(downloadSecurityModes) > 1 { + return nil, errors.New("xhttp download-settings security modes are mutually exclusive: " + strings.Join(downloadSecurityModes, ", ")) + } + downloadSecurityMode := "" + if len(downloadSecurityModes) == 1 { + downloadSecurityMode = downloadSecurityModes[0] + } + if downloadSecurityMode != "" && !downloadTLS { + return nil, fmt.Errorf("xhttp download-settings: %s requires TLS", downloadSecurityMode) } downloadAddr := net.JoinHostPort(downloadServer, strconv.Itoa(downloadPort)) @@ -894,17 +877,8 @@ func NewVless(option VlessOption) (*Vless, error) { if !downloadTLS { return nil, errors.New("xhttp HTTP/3 requires TLS") } - if downloadShadowTLSConfig != nil { - return nil, errors.New("xhttp HTTP/3 does not support ShadowTLS") - } - if downloadRestlsConfig != nil { - return nil, errors.New("xhttp HTTP/3 does not support Restls") - } - if downloadJLSConfig != nil { - return nil, errors.New("xhttp HTTP/3 does not support JLS") - } - if downloadRealityCfg != nil { - return nil, errors.New("xhttp HTTP/3 does not support reality") + if downloadSecurityMode != "" { + return nil, fmt.Errorf("xhttp HTTP/3 does not support %s", downloadSecurityMode) } tlsConfig, err := tlsOpts.ToStdConfig() if err != nil { diff --git a/adapter/outbound/vmess.go b/adapter/outbound/vmess.go index 4e8b8672a..aef7cce88 100644 --- a/adapter/outbound/vmess.go +++ b/adapter/outbound/vmess.go @@ -530,55 +530,36 @@ func NewVmess(option VmessOption) (*Vmess, error) { if err != nil { return nil, err } + securityModes := make([]string, 0, 5) if v.shadowTLSConfig != nil { - if !option.TLS { - return nil, errors.New("ShadowTLS requires TLS") - } - if v.restlsConfig != nil { - return nil, errors.New("ShadowTLS is incompatible with Restls") - } - 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") - } + securityModes = append(securityModes, "ShadowTLS") } if v.restlsConfig != nil { - if !option.TLS { - return nil, errors.New("Restls requires TLS") - } - if v.jlsConfig != nil { - return nil, errors.New("Restls is incompatible with JLS") - } - if v.realityConfig != nil { - return nil, errors.New("Restls is incompatible with REALITY") - } - if option.TLSMirrorOpts.PrimaryKey != "" { - return nil, errors.New("Restls is incompatible with TLSMirror") - } - if option.Network == "mkcp" || option.Network == "kcp" { - return nil, errors.New("Restls only supports TCP transports") - } + securityModes = append(securityModes, "Restls") } if v.jlsConfig != nil { - if !option.TLS { - return nil, errors.New("JLS requires TLS") - } - if v.realityConfig != nil { - return nil, errors.New("JLS is incompatible with REALITY") - } - if option.TLSMirrorOpts.PrimaryKey != "" { - return nil, errors.New("JLS is incompatible with TLSMirror") - } - if option.Network == "mkcp" || option.Network == "kcp" { - return nil, errors.New("JLS only supports TCP transports") + securityModes = append(securityModes, "JLS") + } + if v.realityConfig != nil { + securityModes = append(securityModes, "REALITY") + } + if option.TLSMirrorOpts.PrimaryKey != "" { + securityModes = append(securityModes, "TLSMirror") + } + if len(securityModes) > 1 { + return nil, errors.New("security modes are mutually exclusive: " + strings.Join(securityModes, ", ")) + } + securityMode := "" + if len(securityModes) == 1 { + securityMode = securityModes[0] + } + if securityMode != "" && !option.TLS { + return nil, fmt.Errorf("%s requires TLS", securityMode) + } + if option.Network == "mkcp" || option.Network == "kcp" { + switch securityMode { + case "ShadowTLS", "Restls", "JLS": + return nil, fmt.Errorf("%s only supports TCP transports", securityMode) } }