mirror of
https://github.com/shtorm-7/sing-box-extended.git
synced 2026-12-01 14:18:36 +00:00
Fetch ECH config from DNS for QUIC
QUIC clients took the standard TLS config directly, so with ECH enabled and no static config, hysteria, hysteria2, tuic and DNS over QUIC never queried the ECH config list and sent the real server name in the clear without reporting an error. Query it before the QUIC handshake as the TCP path does.
This commit is contained in:
+14
-6
@@ -138,6 +138,14 @@ func (s *ECHClientConfig) ClientHandshake(ctx context.Context, conn net.Conn) (a
|
||||
func (s *ECHClientConfig) fetchAndHandshake(ctx context.Context, conn net.Conn) (aTLS.Conn, error) {
|
||||
s.access.Lock()
|
||||
defer s.access.Unlock()
|
||||
err := s.fetchLocked(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return s.Client(conn)
|
||||
}
|
||||
|
||||
func (s *ECHClientConfig) fetchLocked(ctx context.Context) error {
|
||||
if len(s.ECHConfigList()) == 0 || s.lastTTL == 0 || time.Since(s.lastUpdate) > s.lastTTL {
|
||||
queryServerName := s.queryServerName
|
||||
if queryServerName == "" {
|
||||
@@ -157,14 +165,14 @@ func (s *ECHClientConfig) fetchAndHandshake(ctx context.Context, conn net.Conn)
|
||||
}
|
||||
queryOptions, err := dialer.NewDNSQueryOptions(s.ctx, s.domainResolver, true)
|
||||
if err != nil {
|
||||
return nil, E.Cause(err, "fetch ECH config list")
|
||||
return 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")
|
||||
return E.Cause(err, "fetch ECH config list")
|
||||
}
|
||||
if response.Rcode != mDNS.RcodeSuccess {
|
||||
return nil, E.Cause(dns.RcodeError(response.Rcode), "fetch ECH config list")
|
||||
return E.Cause(dns.RcodeError(response.Rcode), "fetch ECH config list")
|
||||
}
|
||||
match:
|
||||
for _, rr := range response.Answer {
|
||||
@@ -174,7 +182,7 @@ func (s *ECHClientConfig) fetchAndHandshake(ctx context.Context, conn net.Conn)
|
||||
if value.Key().String() == "ech" {
|
||||
echConfigList, err := base64.StdEncoding.DecodeString(value.String())
|
||||
if err != nil {
|
||||
return nil, E.Cause(err, "decode ECH config")
|
||||
return E.Cause(err, "decode ECH config")
|
||||
}
|
||||
s.lastTTL = time.Duration(rr.Header().Ttl) * time.Second
|
||||
s.lastUpdate = time.Now()
|
||||
@@ -185,10 +193,10 @@ func (s *ECHClientConfig) fetchAndHandshake(ctx context.Context, conn net.Conn)
|
||||
}
|
||||
}
|
||||
if len(s.ECHConfigList()) == 0 {
|
||||
return nil, E.New("no ECH config found in DNS records")
|
||||
return E.New("no ECH config found in DNS records")
|
||||
}
|
||||
}
|
||||
return s.Client(conn)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *ECHClientConfig) Clone() Config {
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
//go:build go1.24 && with_quic
|
||||
|
||||
package tls
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"net"
|
||||
"net/http"
|
||||
|
||||
"github.com/sagernet/quic-go"
|
||||
"github.com/sagernet/quic-go/http3"
|
||||
qtls "github.com/sagernet/sing-quic"
|
||||
)
|
||||
|
||||
func (s *ECHClientConfig) fetchSTDConfig(ctx context.Context) (*STDConfig, error) {
|
||||
s.access.Lock()
|
||||
defer s.access.Unlock()
|
||||
err := s.fetchLocked(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return s.STDConfig()
|
||||
}
|
||||
|
||||
func (s *ECHClientConfig) Dial(ctx context.Context, conn net.Conn, config *quic.Config) (*quic.Conn, error) {
|
||||
tlsConfig, err := s.fetchSTDConfig(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return quic.DialConn(ctx, conn, tlsConfig, config)
|
||||
}
|
||||
|
||||
func (s *ECHClientConfig) DialEarly(ctx context.Context, conn net.Conn, config *quic.Config) (*quic.Conn, error) {
|
||||
tlsConfig, err := s.fetchSTDConfig(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return quic.DialEarlyConn(ctx, conn, tlsConfig, config)
|
||||
}
|
||||
|
||||
func (s *ECHClientConfig) CreateTransport(conn net.Conn, quicConnPtr **quic.Conn, quicConfig *quic.Config) http.RoundTripper {
|
||||
return s.createHTTP3Transport(quicConnPtr, quicConfig, func(ctx context.Context, tlsConfig *tls.Config, config *quic.Config) (*quic.Conn, error) {
|
||||
return quic.DialEarlyConn(ctx, conn, tlsConfig, config)
|
||||
})
|
||||
}
|
||||
|
||||
func (s *ECHClientConfig) CreatePacketTransport(conn net.PacketConn, remoteAddr net.Addr, quicConnPtr **quic.Conn, quicConfig *quic.Config) http.RoundTripper {
|
||||
return s.createHTTP3Transport(quicConnPtr, quicConfig, func(ctx context.Context, tlsConfig *tls.Config, config *quic.Config) (*quic.Conn, error) {
|
||||
return quic.DialEarly(ctx, conn, remoteAddr, tlsConfig, config)
|
||||
})
|
||||
}
|
||||
|
||||
func (s *ECHClientConfig) createHTTP3Transport(quicConnPtr **quic.Conn, quicConfig *quic.Config, dial func(ctx context.Context, tlsConfig *tls.Config, config *quic.Config) (*quic.Conn, error)) http.RoundTripper {
|
||||
return &http3.Transport{
|
||||
QUICConfig: quicConfig,
|
||||
Dial: func(ctx context.Context, _ string, _ *tls.Config, config *quic.Config) (*quic.Conn, error) {
|
||||
tlsConfig, err := s.fetchSTDConfig(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
tlsConfig = tlsConfig.Clone()
|
||||
tlsConfig.NextProtos = []string{http3.NextProtoH3}
|
||||
quicConn, err := dial(ctx, tlsConfig, config)
|
||||
if err != nil {
|
||||
return nil, qtls.WrapError(err)
|
||||
}
|
||||
*quicConnPtr = quicConn
|
||||
return quicConn, nil
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
var _ qtls.Config = (*ECHClientConfig)(nil)
|
||||
@@ -47,7 +47,14 @@ func NewQUIC(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
|
||||
}
|
||||
|
||||
@@ -43,7 +43,13 @@ func NewOutbound(ctx context.Context, router adapter.Router, logger log.ContextL
|
||||
if options.TLS == nil || !options.TLS.Enabled {
|
||||
return nil, C.ErrTLSRequired
|
||||
}
|
||||
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
|
||||
}
|
||||
|
||||
@@ -54,7 +54,13 @@ func NewOutbound(ctx context.Context, router adapter.Router, logger log.ContextL
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
tlsConfig, err := tls.NewClient(ctx, logger, tlsServerAddress, tlsOptions)
|
||||
tlsConfig, err := tls.NewClientWithOptions(tls.ClientOptions{
|
||||
Context: ctx,
|
||||
Logger: logger,
|
||||
ServerAddress: tlsServerAddress,
|
||||
Options: tlsOptions,
|
||||
DomainResolver: options.DomainResolver,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -47,7 +47,13 @@ func NewOutbound(ctx context.Context, router adapter.Router, logger log.ContextL
|
||||
if options.TLS == nil || !options.TLS.Enabled {
|
||||
return nil, C.ErrTLSRequired
|
||||
}
|
||||
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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user