From 961b56b176ae8b75fb4eca34d0edf3d863e12738 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Mon, 16 Nov 2026 06:50:44 +0000 Subject: [PATCH] Use domain resolver for ECH config queries When ECH is enabled without a static config, the HTTPS record query for the ECH config list went through the DNS rules instead of the domain resolver, and the migration of outbound DNS rule items to the domain resolver left no way to select a DNS server for it. A rule rejecting HTTPS records refused the query, and a default DNS server detoured through the same outbound waited for its own connection until the query timed out. Send the query to the domain resolver of the dial fields, then the default domain resolver, then the only DNS server, and fail it when none of them is available. DNS servers must set their own domain resolver for it, as they do for a domain server address. --- common/httpclient/client.go | 2 ++ common/tls/client.go | 17 +++++++---------- common/tls/ech.go | 18 ++++++++++++++++-- common/tls/reality_client.go | 2 +- common/tls/std_client.go | 6 +++--- common/tls/utls_client.go | 6 +++--- common/tls/utls_stub.go | 4 ++-- dns/transport/https.go | 9 ++++++++- dns/transport/tls.go | 9 ++++++++- docs/configuration/shared/dial.md | 2 +- docs/configuration/shared/dial.zh.md | 2 +- protocol/anytls/outbound.go | 8 +++++++- protocol/http/outbound.go | 8 +++++++- protocol/shadowtls/outbound.go | 8 +++++++- protocol/trojan/outbound.go | 9 +++++---- protocol/vless/outbound.go | 9 +++++---- protocol/vmess/outbound.go | 8 +++++++- 17 files changed, 90 insertions(+), 37 deletions(-) diff --git a/common/httpclient/client.go b/common/httpclient/client.go index f5a56162..c6f1f578 100644 --- a/common/httpclient/client.go +++ b/common/httpclient/client.go @@ -62,6 +62,8 @@ func NewTransport(ctx context.Context, logger logger.ContextLogger, tag string, Logger: logger, Options: tlsOptions, AllowEmptyServerName: true, + DomainResolver: options.DomainResolver, + DirectResolver: options.DirectResolver, }) if err != nil { return nil, err diff --git a/common/tls/client.go b/common/tls/client.go index 7b303687..0977b843 100644 --- a/common/tls/client.go +++ b/common/tls/client.go @@ -45,16 +45,11 @@ func applyTLSSpoof(conn net.Conn, spoof string, method tlsspoof.Method) (net.Con return tlsspoof.NewConn(conn, method, spoof) } -func NewDialerFromOptions(ctx context.Context, logger logger.ContextLogger, dialer N.Dialer, serverAddress string, options option.OutboundTLSOptions) (N.Dialer, error) { - if !options.Enabled { +func NewDialerFromOptions(dialer N.Dialer, options ClientOptions) (N.Dialer, error) { + if !options.Options.Enabled { return dialer, nil } - config, err := NewClientWithOptions(ClientOptions{ - Context: ctx, - Logger: logger, - ServerAddress: serverAddress, - Options: options, - }) + config, err := NewClientWithOptions(options) if err != nil { return nil, err } @@ -77,6 +72,8 @@ type ClientOptions struct { Options option.OutboundTLSOptions AllowEmptyServerName bool KTLSCompatible bool + DomainResolver *option.DomainResolveOptions + DirectResolver bool } func NewClientWithOptions(options ClientOptions) (Config, error) { @@ -103,9 +100,9 @@ func NewClientWithOptions(options ClientOptions) (Config, error) { if options.Options.Reality != nil && options.Options.Reality.Enabled { return newRealityClient(options.Context, options.Logger, options.ServerAddress, options.Options, options.AllowEmptyServerName) } else if options.Options.UTLS != nil && options.Options.UTLS.Enabled { - return newUTLSClient(options.Context, options.Logger, options.ServerAddress, options.Options, options.AllowEmptyServerName) + return newUTLSClient(options.Context, options.Logger, options.ServerAddress, options.Options, options.AllowEmptyServerName, options.DomainResolver, options.DirectResolver) } - return newSTDClient(options.Context, options.Logger, options.ServerAddress, options.Options, options.AllowEmptyServerName) + return newSTDClient(options.Context, options.Logger, options.ServerAddress, options.Options, options.AllowEmptyServerName, options.DomainResolver, options.DirectResolver) } func ClientHandshake(ctx context.Context, conn net.Conn, config Config) (Conn, error) { diff --git a/common/tls/ech.go b/common/tls/ech.go index 46ab50a2..474e1b85 100644 --- a/common/tls/ech.go +++ b/common/tls/ech.go @@ -13,6 +13,7 @@ import ( "time" "github.com/sagernet/sing-box/adapter" + "github.com/sagernet/sing-box/common/dialer" "github.com/sagernet/sing-box/dns" "github.com/sagernet/sing-box/option" E "github.com/sagernet/sing/common/exceptions" @@ -24,7 +25,7 @@ import ( "golang.org/x/crypto/cryptobyte" ) -func parseECHClientConfig(ctx context.Context, clientConfig ECHCapableConfig, options option.OutboundTLSOptions) (Config, error) { +func parseECHClientConfig(ctx context.Context, clientConfig ECHCapableConfig, options option.OutboundTLSOptions, domainResolver *option.DomainResolveOptions, directResolver bool) (Config, error) { var echConfig []byte if len(options.ECH.Config) > 0 { echConfig = []byte(strings.Join(options.ECH.Config, "\n")) @@ -47,9 +48,14 @@ func parseECHClientConfig(ctx context.Context, clientConfig ECHCapableConfig, op clientConfig.SetECHConfigList(block.Bytes) return clientConfig, nil } else { + if directResolver && (domainResolver == nil || domainResolver.Server == "") { + return nil, E.New("missing domain resolver for ECH config query") + } return &ECHClientConfig{ ECHCapableConfig: clientConfig, + ctx: ctx, dnsRouter: service.FromContext[adapter.DNSRouter](ctx), + domainResolver: domainResolver, queryServerName: options.ECH.QueryServerName, }, nil } @@ -109,7 +115,9 @@ func parseECHKeys(echKey []byte) ([]tls.EncryptedClientHelloKey, error) { type ECHClientConfig struct { ECHCapableConfig access sync.Mutex + ctx context.Context dnsRouter adapter.DNSRouter + domainResolver *option.DomainResolveOptions queryServerName string lastTTL time.Duration lastUpdate time.Time @@ -147,7 +155,11 @@ func (s *ECHClientConfig) fetchAndHandshake(ctx context.Context, conn net.Conn) }, }, } - response, err := s.dnsRouter.Exchange(ctx, message, adapter.DNSQueryOptions{}) + queryOptions, err := dialer.NewDNSQueryOptions(s.ctx, s.domainResolver, true) + if err != nil { + return nil, E.Cause(err, "fetch ECH config list") + } + response, err := s.dnsRouter.Exchange(ctx, message, queryOptions) if err != nil { return nil, E.Cause(err, "fetch ECH config list") } @@ -182,7 +194,9 @@ func (s *ECHClientConfig) fetchAndHandshake(ctx context.Context, conn net.Conn) func (s *ECHClientConfig) Clone() Config { return &ECHClientConfig{ ECHCapableConfig: s.ECHCapableConfig.Clone().(ECHCapableConfig), + ctx: s.ctx, dnsRouter: s.dnsRouter, + domainResolver: s.domainResolver, queryServerName: s.queryServerName, lastUpdate: s.lastUpdate, } diff --git a/common/tls/reality_client.go b/common/tls/reality_client.go index 0f8e1dc4..ccb69aa1 100644 --- a/common/tls/reality_client.go +++ b/common/tls/reality_client.go @@ -60,7 +60,7 @@ func newRealityClient(ctx context.Context, logger logger.ContextLogger, serverAd return nil, E.New("spoof is unsupported in reality") } - uClient, err := newUTLSClient(ctx, logger, serverAddress, options, allowEmptyServerName) + uClient, err := newUTLSClient(ctx, logger, serverAddress, options, allowEmptyServerName, nil, false) if err != nil { return nil, err } diff --git a/common/tls/std_client.go b/common/tls/std_client.go index 80fa6cb2..dd26105a 100644 --- a/common/tls/std_client.go +++ b/common/tls/std_client.go @@ -112,10 +112,10 @@ func (c *STDClientConfig) SetECHConfigList(EncryptedClientHelloConfigList []byte } func NewSTDClient(ctx context.Context, logger logger.ContextLogger, serverAddress string, options option.OutboundTLSOptions) (Config, error) { - return newSTDClient(ctx, logger, serverAddress, options, false) + return newSTDClient(ctx, logger, serverAddress, options, false, nil, false) } -func newSTDClient(ctx context.Context, logger logger.ContextLogger, serverAddress string, options option.OutboundTLSOptions, allowEmptyServerName bool) (Config, error) { +func newSTDClient(ctx context.Context, logger logger.ContextLogger, serverAddress string, options option.OutboundTLSOptions, allowEmptyServerName bool, domainResolver *option.DomainResolveOptions, directResolver bool) (Config, error) { var serverName string if options.ServerName != "" { serverName = options.ServerName @@ -247,7 +247,7 @@ func newSTDClient(ctx context.Context, logger logger.ContextLogger, serverAddres config.SetServerName(serverName) if options.ECH != nil && options.ECH.Enabled { var err error - config, err = parseECHClientConfig(ctx, config.(ECHCapableConfig), options) + config, err = parseECHClientConfig(ctx, config.(ECHCapableConfig), options, domainResolver, directResolver) if err != nil { return nil, err } diff --git a/common/tls/utls_client.go b/common/tls/utls_client.go index b28d3e08..e2261eee 100644 --- a/common/tls/utls_client.go +++ b/common/tls/utls_client.go @@ -185,10 +185,10 @@ func (c *utlsALPNWrapper) HandshakeContext(ctx context.Context) error { } func NewUTLSClient(ctx context.Context, logger logger.ContextLogger, serverAddress string, options option.OutboundTLSOptions) (Config, error) { - return newUTLSClient(ctx, logger, serverAddress, options, false) + return newUTLSClient(ctx, logger, serverAddress, options, false, nil, false) } -func newUTLSClient(ctx context.Context, logger logger.ContextLogger, serverAddress string, options option.OutboundTLSOptions, allowEmptyServerName bool) (Config, error) { +func newUTLSClient(ctx context.Context, logger logger.ContextLogger, serverAddress string, options option.OutboundTLSOptions, allowEmptyServerName bool, domainResolver *option.DomainResolveOptions, directResolver bool) (Config, error) { var serverName string if options.ServerName != "" { serverName = options.ServerName @@ -326,7 +326,7 @@ func newUTLSClient(ctx context.Context, logger logger.ContextLogger, serverAddre if options.Reality != nil && options.Reality.Enabled { return nil, E.New("Reality is conflict with ECH") } - config, err = parseECHClientConfig(ctx, config.(ECHCapableConfig), options) + config, err = parseECHClientConfig(ctx, config.(ECHCapableConfig), options, domainResolver, directResolver) if err != nil { return nil, err } diff --git a/common/tls/utls_stub.go b/common/tls/utls_stub.go index 67fac209..3c6fb69a 100644 --- a/common/tls/utls_stub.go +++ b/common/tls/utls_stub.go @@ -12,10 +12,10 @@ import ( ) func NewUTLSClient(ctx context.Context, logger logger.ContextLogger, serverAddress string, options option.OutboundTLSOptions) (Config, error) { - return newUTLSClient(ctx, logger, serverAddress, options, false) + return newUTLSClient(ctx, logger, serverAddress, options, false, nil, false) } -func newUTLSClient(ctx context.Context, logger logger.ContextLogger, serverAddress string, options option.OutboundTLSOptions, allowEmptyServerName bool) (Config, error) { +func newUTLSClient(ctx context.Context, logger logger.ContextLogger, serverAddress string, options option.OutboundTLSOptions, allowEmptyServerName bool, domainResolver *option.DomainResolveOptions, directResolver bool) (Config, error) { return nil, E.New(`uTLS is not included in this build, rebuild with -tags with_utls`) } diff --git a/dns/transport/https.go b/dns/transport/https.go index 07ee9d4d..b50cfc9c 100644 --- a/dns/transport/https.go +++ b/dns/transport/https.go @@ -62,7 +62,14 @@ func NewHTTPS(ctx context.Context, logger log.ContextLogger, tag string, options } tlsOptions := common.PtrValueOrDefault(options.TLS) tlsOptions.Enabled = true - tlsConfig, err := tls.NewClient(ctx, logger, options.Server, tlsOptions) + tlsConfig, err := tls.NewClientWithOptions(tls.ClientOptions{ + Context: ctx, + Logger: logger, + ServerAddress: options.Server, + Options: tlsOptions, + DomainResolver: options.DomainResolver, + DirectResolver: true, + }) if err != nil { return nil, err } diff --git a/dns/transport/tls.go b/dns/transport/tls.go index ed70b7ef..76d749da 100644 --- a/dns/transport/tls.go +++ b/dns/transport/tls.go @@ -44,7 +44,14 @@ func NewTLS(ctx context.Context, logger log.ContextLogger, tag string, options o } tlsOptions := common.PtrValueOrDefault(options.TLS) tlsOptions.Enabled = true - tlsConfig, err := tls.NewClient(ctx, logger, options.Server, tlsOptions) + tlsConfig, err := tls.NewClientWithOptions(tls.ClientOptions{ + Context: ctx, + Logger: logger, + ServerAddress: options.Server, + Options: tlsOptions, + DomainResolver: options.DomainResolver, + DirectResolver: true, + }) if err != nil { return nil, err } diff --git a/docs/configuration/shared/dial.md b/docs/configuration/shared/dial.md index f292f548..c0192c19 100644 --- a/docs/configuration/shared/dial.md +++ b/docs/configuration/shared/dial.md @@ -190,7 +190,7 @@ Setting this option directly to a string is equivalent to setting `server` of th | Outbound/Endpoints | Effected domains | |--------------------|--------------------------| | `direct` | Domain in request | -| others | Domain in server address | +| others | Domain in server address, domain of the ECH config query | #### network_strategy diff --git a/docs/configuration/shared/dial.zh.md b/docs/configuration/shared/dial.zh.md index d43851fd..788886c1 100644 --- a/docs/configuration/shared/dial.zh.md +++ b/docs/configuration/shared/dial.zh.md @@ -188,7 +188,7 @@ TCP keep alive 间隔。 | 出站/端点 | 受影响的域名 | |----------------|---------------------------| | `direct` | 请求中的域名 | -| 其他类型 | 服务器地址中的域名 | +| 其他类型 | 服务器地址中的域名、ECH 配置查询的域名 | #### network_strategy diff --git a/protocol/anytls/outbound.go b/protocol/anytls/outbound.go index 30d262ca..7d005489 100644 --- a/protocol/anytls/outbound.go +++ b/protocol/anytls/outbound.go @@ -59,7 +59,13 @@ func NewOutbound(ctx context.Context, router adapter.Router, logger log.ContextL return nil, E.New("tcp_fast_open is not supported with anytls outbound") } - tlsConfig, err := tls.NewClient(ctx, logger, options.Server, common.PtrValueOrDefault(options.TLS)) + tlsConfig, err := tls.NewClientWithOptions(tls.ClientOptions{ + Context: ctx, + Logger: logger, + ServerAddress: options.Server, + Options: common.PtrValueOrDefault(options.TLS), + DomainResolver: options.DomainResolver, + }) if err != nil { return nil, err } diff --git a/protocol/http/outbound.go b/protocol/http/outbound.go index 48c4be6b..c25b3d83 100644 --- a/protocol/http/outbound.go +++ b/protocol/http/outbound.go @@ -34,7 +34,13 @@ func NewOutbound(ctx context.Context, router adapter.Router, logger log.ContextL if err != nil { return nil, err } - detour, err := tls.NewDialerFromOptions(ctx, logger, outboundDialer, options.Server, common.PtrValueOrDefault(options.TLS)) + detour, err := tls.NewDialerFromOptions(outboundDialer, tls.ClientOptions{ + Context: ctx, + Logger: logger, + ServerAddress: options.Server, + Options: common.PtrValueOrDefault(options.TLS), + DomainResolver: options.DomainResolver, + }) if err != nil { return nil, err } diff --git a/protocol/shadowtls/outbound.go b/protocol/shadowtls/outbound.go index 81ce0b38..6f16b637 100644 --- a/protocol/shadowtls/outbound.go +++ b/protocol/shadowtls/outbound.go @@ -43,7 +43,13 @@ func NewOutbound(ctx context.Context, router adapter.Router, logger log.ContextL options.TLS.MinVersion = "1.2" options.TLS.MaxVersion = "1.2" } - tlsConfig, err := tls.NewClient(ctx, logger, options.Server, common.PtrValueOrDefault(options.TLS)) + tlsConfig, err := tls.NewClientWithOptions(tls.ClientOptions{ + Context: ctx, + Logger: logger, + ServerAddress: options.Server, + Options: common.PtrValueOrDefault(options.TLS), + DomainResolver: options.DomainResolver, + }) if err != nil { return nil, err } diff --git a/protocol/trojan/outbound.go b/protocol/trojan/outbound.go index d1cd853a..52dfb832 100644 --- a/protocol/trojan/outbound.go +++ b/protocol/trojan/outbound.go @@ -58,10 +58,11 @@ func NewOutbound(ctx context.Context, router adapter.Router, logger log.ContextL } if options.TLS != nil { outbound.tlsConfig, err = tls.NewClientWithOptions(tls.ClientOptions{ - Context: ctx, - Logger: logger, - ServerAddress: options.Server, - Options: common.PtrValueOrDefault(options.TLS), + Context: ctx, + Logger: logger, + ServerAddress: options.Server, + Options: common.PtrValueOrDefault(options.TLS), + DomainResolver: options.DomainResolver, KTLSCompatible: common.PtrValueOrDefault(options.Transport).Type == "" && !common.PtrValueOrDefault(options.Multiplex).Enabled, }) diff --git a/protocol/vless/outbound.go b/protocol/vless/outbound.go index c0ece0a2..eb6569b3 100644 --- a/protocol/vless/outbound.go +++ b/protocol/vless/outbound.go @@ -60,10 +60,11 @@ func NewOutbound(ctx context.Context, router adapter.Router, logger log.ContextL } if options.TLS != nil { outbound.tlsConfig, err = tls.NewClientWithOptions(tls.ClientOptions{ - Context: ctx, - Logger: logger, - ServerAddress: options.Server, - Options: common.PtrValueOrDefault(options.TLS), + Context: ctx, + Logger: logger, + ServerAddress: options.Server, + Options: common.PtrValueOrDefault(options.TLS), + DomainResolver: options.DomainResolver, KTLSCompatible: common.PtrValueOrDefault(options.Transport).Type == "" && !common.PtrValueOrDefault(options.Multiplex).Enabled && options.Flow == "", diff --git a/protocol/vmess/outbound.go b/protocol/vmess/outbound.go index 2d867676..f1048676 100644 --- a/protocol/vmess/outbound.go +++ b/protocol/vmess/outbound.go @@ -59,7 +59,13 @@ func NewOutbound(ctx context.Context, router adapter.Router, logger log.ContextL serverAddr: options.ServerOptions.Build(), } if options.TLS != nil { - outbound.tlsConfig, err = tls.NewClient(ctx, logger, options.Server, common.PtrValueOrDefault(options.TLS)) + outbound.tlsConfig, err = tls.NewClientWithOptions(tls.ClientOptions{ + Context: ctx, + Logger: logger, + ServerAddress: options.Server, + Options: common.PtrValueOrDefault(options.TLS), + DomainResolver: options.DomainResolver, + }) if err != nil { return nil, err }