From 62fa48293af3f9d07c1e7bb0b4153a352258e9d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Sat, 3 Sep 2022 12:55:10 +0800 Subject: [PATCH] Merge dialer options --- common/dialer/dialer.go | 9 +++------ option/direct.go | 2 +- option/hysteria.go | 2 +- option/outbound.go | 4 ---- option/shadowsocks.go | 2 +- option/shadowtls.go | 2 +- option/simple.go | 4 ++-- option/ssh.go | 2 +- option/tor.go | 2 +- option/trojan.go | 2 +- option/vmess.go | 2 +- option/wireguard.go | 2 +- outbound/direct.go | 2 +- outbound/http.go | 2 +- outbound/hysteria.go | 2 +- outbound/shadowsocks.go | 2 +- outbound/shadowtls.go | 2 +- outbound/socks.go | 2 +- outbound/ssh.go | 2 +- outbound/tor.go | 2 +- outbound/trojan.go | 2 +- outbound/vmess.go | 2 +- outbound/wireguard.go | 2 +- test/inbound_detour_test.go | 6 ++---- test/shadowtls_test.go | 12 ++++-------- 25 files changed, 31 insertions(+), 44 deletions(-) diff --git a/common/dialer/dialer.go b/common/dialer/dialer.go index 9fb9391a..5b1750cc 100644 --- a/common/dialer/dialer.go +++ b/common/dialer/dialer.go @@ -10,15 +10,12 @@ import ( ) func New(router adapter.Router, options option.DialerOptions) N.Dialer { + var dialer N.Dialer if options.Detour == "" { - return NewDefault(router, options) + dialer = NewDefault(router, options) } else { - return NewDetour(router, options.Detour) + dialer = NewDetour(router, options.Detour) } -} - -func NewOutbound(router adapter.Router, options option.OutboundDialerOptions) N.Dialer { - dialer := New(router, options.DialerOptions) domainStrategy := dns.DomainStrategy(options.DomainStrategy) if domainStrategy != dns.DomainStrategyAsIS || options.Detour == "" { dialer = NewResolveDialer(router, dialer, domainStrategy, time.Duration(options.FallbackDelay)) diff --git a/option/direct.go b/option/direct.go index 77cd0a06..8624846d 100644 --- a/option/direct.go +++ b/option/direct.go @@ -8,7 +8,7 @@ type DirectInboundOptions struct { } type DirectOutboundOptions struct { - OutboundDialerOptions + DialerOptions OverrideAddress string `json:"override_address,omitempty"` OverridePort uint16 `json:"override_port,omitempty"` ProxyProtocol uint8 `json:"proxy_protocol,omitempty"` diff --git a/option/hysteria.go b/option/hysteria.go index 9ab9a41e..f9266924 100644 --- a/option/hysteria.go +++ b/option/hysteria.go @@ -17,7 +17,7 @@ type HysteriaInboundOptions struct { } type HysteriaOutboundOptions struct { - OutboundDialerOptions + DialerOptions ServerOptions Up string `json:"up,omitempty"` UpMbps int `json:"up_mbps,omitempty"` diff --git a/option/outbound.go b/option/outbound.go index b4ca57aa..f5664971 100644 --- a/option/outbound.go +++ b/option/outbound.go @@ -113,10 +113,6 @@ type DialerOptions struct { ReuseAddr bool `json:"reuse_addr,omitempty"` ConnectTimeout Duration `json:"connect_timeout,omitempty"` TCPFastOpen bool `json:"tcp_fast_open,omitempty"` -} - -type OutboundDialerOptions struct { - DialerOptions DomainStrategy DomainStrategy `json:"domain_strategy,omitempty"` FallbackDelay Duration `json:"fallback_delay,omitempty"` } diff --git a/option/shadowsocks.go b/option/shadowsocks.go index 2d4b6422..fae2a98a 100644 --- a/option/shadowsocks.go +++ b/option/shadowsocks.go @@ -22,7 +22,7 @@ type ShadowsocksDestination struct { } type ShadowsocksOutboundOptions struct { - OutboundDialerOptions + DialerOptions ServerOptions Method string `json:"method"` Password string `json:"password"` diff --git a/option/shadowtls.go b/option/shadowtls.go index 7db12556..4440aa69 100644 --- a/option/shadowtls.go +++ b/option/shadowtls.go @@ -11,7 +11,7 @@ type ShadowTLSHandshakeOptions struct { } type ShadowTLSOutboundOptions struct { - OutboundDialerOptions + DialerOptions ServerOptions TLS *OutboundTLSOptions `json:"tls,omitempty"` } diff --git a/option/simple.go b/option/simple.go index fc2c0bb4..32645fbe 100644 --- a/option/simple.go +++ b/option/simple.go @@ -15,7 +15,7 @@ type HTTPMixedInboundOptions struct { } type SocksOutboundOptions struct { - OutboundDialerOptions + DialerOptions ServerOptions Version string `json:"version,omitempty"` Username string `json:"username,omitempty"` @@ -25,7 +25,7 @@ type SocksOutboundOptions struct { } type HTTPOutboundOptions struct { - OutboundDialerOptions + DialerOptions ServerOptions Username string `json:"username,omitempty"` Password string `json:"password,omitempty"` diff --git a/option/ssh.go b/option/ssh.go index af3f9588..30321705 100644 --- a/option/ssh.go +++ b/option/ssh.go @@ -1,7 +1,7 @@ package option type SSHOutboundOptions struct { - OutboundDialerOptions + DialerOptions ServerOptions User string `json:"user,omitempty"` Password string `json:"password,omitempty"` diff --git a/option/tor.go b/option/tor.go index a4842fd4..a56f70ae 100644 --- a/option/tor.go +++ b/option/tor.go @@ -1,7 +1,7 @@ package option type TorOutboundOptions struct { - OutboundDialerOptions + DialerOptions ExecutablePath string `json:"executable_path,omitempty"` ExtraArgs []string `json:"extra_args,omitempty"` DataDirectory string `json:"data_directory,omitempty"` diff --git a/option/trojan.go b/option/trojan.go index ad81e3b2..11392cd5 100644 --- a/option/trojan.go +++ b/option/trojan.go @@ -15,7 +15,7 @@ type TrojanUser struct { } type TrojanOutboundOptions struct { - OutboundDialerOptions + DialerOptions ServerOptions Password string `json:"password"` Network NetworkList `json:"network,omitempty"` diff --git a/option/vmess.go b/option/vmess.go index 2f9cceb0..c1e25db2 100644 --- a/option/vmess.go +++ b/option/vmess.go @@ -14,7 +14,7 @@ type VMessUser struct { } type VMessOutboundOptions struct { - OutboundDialerOptions + DialerOptions ServerOptions UUID string `json:"uuid"` Security string `json:"security"` diff --git a/option/wireguard.go b/option/wireguard.go index f9846968..8fbe6de2 100644 --- a/option/wireguard.go +++ b/option/wireguard.go @@ -1,7 +1,7 @@ package option type WireGuardOutboundOptions struct { - OutboundDialerOptions + DialerOptions ServerOptions LocalAddress Listable[string] `json:"local_address"` PrivateKey string `json:"private_key"` diff --git a/outbound/direct.go b/outbound/direct.go index e37a009f..567c963a 100644 --- a/outbound/direct.go +++ b/outbound/direct.go @@ -45,7 +45,7 @@ func NewDirect(router adapter.Router, logger log.ContextLogger, tag string, opti }, domainStrategy: dns.DomainStrategy(options.DomainStrategy), fallbackDelay: time.Duration(options.FallbackDelay), - dialer: dialer.NewOutbound(router, options.OutboundDialerOptions), + dialer: dialer.New(router, options.DialerOptions), proxyProto: options.ProxyProtocol, } if options.ProxyProtocol > 2 { diff --git a/outbound/http.go b/outbound/http.go index 95e19430..9d7bc094 100644 --- a/outbound/http.go +++ b/outbound/http.go @@ -24,7 +24,7 @@ type HTTP struct { } func NewHTTP(router adapter.Router, logger log.ContextLogger, tag string, options option.HTTPOutboundOptions) (*HTTP, error) { - detour, err := dialer.NewTLS(dialer.NewOutbound(router, options.OutboundDialerOptions), options.Server, common.PtrValueOrDefault(options.TLS)) + detour, err := dialer.NewTLS(dialer.New(router, options.DialerOptions), options.Server, common.PtrValueOrDefault(options.TLS)) if err != nil { return nil, err } diff --git a/outbound/hysteria.go b/outbound/hysteria.go index 27b2d872..43e342de 100644 --- a/outbound/hysteria.go +++ b/outbound/hysteria.go @@ -117,7 +117,7 @@ func NewHysteria(ctx context.Context, router adapter.Router, logger log.ContextL tag: tag, }, ctx: ctx, - dialer: dialer.NewOutbound(router, options.OutboundDialerOptions), + dialer: dialer.New(router, options.DialerOptions), serverAddr: options.ServerOptions.Build(), tlsConfig: tlsConfig, quicConfig: quicConfig, diff --git a/outbound/shadowsocks.go b/outbound/shadowsocks.go index cba3ef3a..23cf343a 100644 --- a/outbound/shadowsocks.go +++ b/outbound/shadowsocks.go @@ -44,7 +44,7 @@ func NewShadowsocks(ctx context.Context, router adapter.Router, logger log.Conte logger: logger, tag: tag, }, - dialer: dialer.NewOutbound(router, options.OutboundDialerOptions), + dialer: dialer.New(router, options.DialerOptions), method: method, serverAddr: options.ServerOptions.Build(), uot: options.UoT, diff --git a/outbound/shadowtls.go b/outbound/shadowtls.go index 71586bc9..fa255d57 100644 --- a/outbound/shadowtls.go +++ b/outbound/shadowtls.go @@ -34,7 +34,7 @@ func NewShadowTLS(ctx context.Context, router adapter.Router, logger log.Context logger: logger, tag: tag, }, - dialer: dialer.NewOutbound(router, options.OutboundDialerOptions), + dialer: dialer.New(router, options.DialerOptions), serverAddr: options.ServerOptions.Build(), } if options.TLS == nil || !options.TLS.Enabled { diff --git a/outbound/socks.go b/outbound/socks.go index d51bc9e5..2400e427 100644 --- a/outbound/socks.go +++ b/outbound/socks.go @@ -25,7 +25,7 @@ type Socks struct { } func NewSocks(router adapter.Router, logger log.ContextLogger, tag string, options option.SocksOutboundOptions) (*Socks, error) { - detour := dialer.NewOutbound(router, options.OutboundDialerOptions) + detour := dialer.New(router, options.DialerOptions) var version socks.Version var err error if options.Version != "" { diff --git a/outbound/ssh.go b/outbound/ssh.go index 2319c16e..23187270 100644 --- a/outbound/ssh.go +++ b/outbound/ssh.go @@ -47,7 +47,7 @@ func NewSSH(ctx context.Context, router adapter.Router, logger log.ContextLogger tag: tag, }, ctx: ctx, - dialer: dialer.NewOutbound(router, options.OutboundDialerOptions), + dialer: dialer.New(router, options.DialerOptions), serverAddr: options.ServerOptions.Build(), user: options.User, hostKeyAlgorithms: options.HostKeyAlgorithms, diff --git a/outbound/tor.go b/outbound/tor.go index 105bfd83..b1818e3b 100644 --- a/outbound/tor.go +++ b/outbound/tor.go @@ -66,7 +66,7 @@ func NewTor(ctx context.Context, router adapter.Router, logger log.ContextLogger tag: tag, }, ctx: ctx, - proxy: NewProxyListener(ctx, logger, dialer.NewOutbound(router, options.OutboundDialerOptions)), + proxy: NewProxyListener(ctx, logger, dialer.New(router, options.DialerOptions)), startConf: &startConf, options: options.Options, }, nil diff --git a/outbound/trojan.go b/outbound/trojan.go index 54f7e784..faebd61d 100644 --- a/outbound/trojan.go +++ b/outbound/trojan.go @@ -40,7 +40,7 @@ func NewTrojan(ctx context.Context, router adapter.Router, logger log.ContextLog logger: logger, tag: tag, }, - dialer: dialer.NewOutbound(router, options.OutboundDialerOptions), + dialer: dialer.New(router, options.DialerOptions), serverAddr: options.ServerOptions.Build(), key: trojan.Key(options.Password), } diff --git a/outbound/vmess.go b/outbound/vmess.go index 585a866b..b4abc906 100644 --- a/outbound/vmess.go +++ b/outbound/vmess.go @@ -42,7 +42,7 @@ func NewVMess(ctx context.Context, router adapter.Router, logger log.ContextLogg logger: logger, tag: tag, }, - dialer: dialer.NewOutbound(router, options.OutboundDialerOptions), + dialer: dialer.New(router, options.DialerOptions), serverAddr: options.ServerOptions.Build(), } var err error diff --git a/outbound/wireguard.go b/outbound/wireguard.go index b8fca843..a3c5b374 100644 --- a/outbound/wireguard.go +++ b/outbound/wireguard.go @@ -64,7 +64,7 @@ func NewWireGuard(ctx context.Context, router adapter.Router, logger log.Context }, ctx: ctx, serverAddr: options.ServerOptions.Build(), - dialer: dialer.NewOutbound(router, options.OutboundDialerOptions), + dialer: dialer.New(router, options.DialerOptions), } var endpointIp netip.Addr if !outbound.serverAddr.IsFqdn() { diff --git a/test/inbound_detour_test.go b/test/inbound_detour_test.go index d21514b1..62d32072 100644 --- a/test/inbound_detour_test.go +++ b/test/inbound_detour_test.go @@ -59,10 +59,8 @@ func TestChainedInbound(t *testing.T) { ShadowsocksOptions: option.ShadowsocksOutboundOptions{ Method: method, Password: password, - OutboundDialerOptions: option.OutboundDialerOptions{ - DialerOptions: option.DialerOptions{ - Detour: "detour-out", - }, + DialerOptions: option.DialerOptions{ + Detour: "detour-out", }, }, }, diff --git a/test/shadowtls_test.go b/test/shadowtls_test.go index b4e6067b..4c6b2156 100644 --- a/test/shadowtls_test.go +++ b/test/shadowtls_test.go @@ -60,10 +60,8 @@ func TestShadowTLS(t *testing.T) { ShadowsocksOptions: option.ShadowsocksOutboundOptions{ Method: method, Password: password, - OutboundDialerOptions: option.OutboundDialerOptions{ - DialerOptions: option.DialerOptions{ - Detour: "detour", - }, + DialerOptions: option.DialerOptions{ + Detour: "detour", }, }, }, @@ -131,10 +129,8 @@ func TestShadowTLSOutbound(t *testing.T) { { Type: C.TypeSocks, SocksOptions: option.SocksOutboundOptions{ - OutboundDialerOptions: option.OutboundDialerOptions{ - DialerOptions: option.DialerOptions{ - Detour: "detour", - }, + DialerOptions: option.DialerOptions{ + Detour: "detour", }, }, },