chore: rewrite the internal mutual exclusion logic of the outbound

This commit is contained in:
wwqgtxx committed 2026-07-15 17:55:48 +08:00
1 parent b6ed8e2643
commit da5a3e52ae
4 files changed
+88 -135

No files matched your search

+11 -8
View File
@@ -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,
+12 -17
View File
@@ -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 {
+40 -66
View File
@@ -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 {
+25 -44
View File
@@ -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)
}
}