mirror of
https://github.com/shtorm-7/sing-box-extended.git
synced 2026-12-01 14:18:36 +00:00
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.
223 lines
6.4 KiB
Go
223 lines
6.4 KiB
Go
//go:build go1.24
|
|
|
|
package tls
|
|
|
|
import (
|
|
"context"
|
|
"crypto/tls"
|
|
"encoding/base64"
|
|
"encoding/pem"
|
|
"net"
|
|
"strings"
|
|
"sync"
|
|
"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"
|
|
aTLS "github.com/sagernet/sing/common/tls"
|
|
"github.com/sagernet/sing/service"
|
|
"github.com/sagernet/sing/service/filemanager"
|
|
|
|
mDNS "github.com/miekg/dns"
|
|
"golang.org/x/crypto/cryptobyte"
|
|
)
|
|
|
|
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"))
|
|
} else if options.ECH.ConfigPath != "" {
|
|
content, err := filemanager.ReadFile(ctx, options.ECH.ConfigPath)
|
|
if err != nil {
|
|
return nil, E.Cause(err, "read ECH config")
|
|
}
|
|
echConfig = content
|
|
}
|
|
//nolint:staticcheck
|
|
if options.ECH.PQSignatureSchemesEnabled || options.ECH.DynamicRecordSizingDisabled {
|
|
return nil, E.New("legacy ECH options are deprecated in sing-box 1.12.0 and removed in sing-box 1.13.0")
|
|
}
|
|
if len(echConfig) > 0 {
|
|
block, rest := pem.Decode(echConfig)
|
|
if block == nil || block.Type != "ECH CONFIGS" || len(rest) > 0 {
|
|
return nil, E.New("invalid ECH configs pem")
|
|
}
|
|
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
|
|
}
|
|
}
|
|
|
|
func parseECHServerConfig(ctx context.Context, options option.InboundTLSOptions, tlsConfig *tls.Config, echKeyPath *string) error {
|
|
var echKey []byte
|
|
if len(options.ECH.Key) > 0 {
|
|
echKey = []byte(strings.Join(options.ECH.Key, "\n"))
|
|
} else if options.ECH.KeyPath != "" {
|
|
content, err := filemanager.ReadFile(ctx, options.ECH.KeyPath)
|
|
if err != nil {
|
|
return E.Cause(err, "read ECH keys")
|
|
}
|
|
echKey = content
|
|
*echKeyPath = options.ECH.KeyPath
|
|
} else {
|
|
return E.New("missing ECH keys")
|
|
}
|
|
echKeys, err := parseECHKeys(echKey)
|
|
if err != nil {
|
|
return E.Cause(err, "parse ECH keys")
|
|
}
|
|
tlsConfig.EncryptedClientHelloKeys = echKeys
|
|
//nolint:staticcheck
|
|
if options.ECH.PQSignatureSchemesEnabled || options.ECH.DynamicRecordSizingDisabled {
|
|
return E.New("legacy ECH options are deprecated in sing-box 1.12.0 and removed in sing-box 1.13.0")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (c *STDServerConfig) setECHServerConfig(echKey []byte) error {
|
|
echKeys, err := parseECHKeys(echKey)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
c.access.Lock()
|
|
config := c.config.Clone()
|
|
config.EncryptedClientHelloKeys = echKeys
|
|
c.config = config
|
|
c.access.Unlock()
|
|
return nil
|
|
}
|
|
|
|
func parseECHKeys(echKey []byte) ([]tls.EncryptedClientHelloKey, error) {
|
|
block, _ := pem.Decode(echKey)
|
|
if block == nil || block.Type != "ECH KEYS" {
|
|
return nil, E.New("invalid ECH keys pem")
|
|
}
|
|
echKeys, err := UnmarshalECHKeys(block.Bytes)
|
|
if err != nil {
|
|
return nil, E.Cause(err, "parse ECH keys")
|
|
}
|
|
return echKeys, nil
|
|
}
|
|
|
|
type ECHClientConfig struct {
|
|
ECHCapableConfig
|
|
access sync.Mutex
|
|
ctx context.Context
|
|
dnsRouter adapter.DNSRouter
|
|
domainResolver *option.DomainResolveOptions
|
|
queryServerName string
|
|
lastTTL time.Duration
|
|
lastUpdate time.Time
|
|
}
|
|
|
|
func (s *ECHClientConfig) ClientHandshake(ctx context.Context, conn net.Conn) (aTLS.Conn, error) {
|
|
tlsConn, err := s.fetchAndHandshake(ctx, conn)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
err = tlsConn.HandshakeContext(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return tlsConn, nil
|
|
}
|
|
|
|
func (s *ECHClientConfig) fetchAndHandshake(ctx context.Context, conn net.Conn) (aTLS.Conn, error) {
|
|
s.access.Lock()
|
|
defer s.access.Unlock()
|
|
if len(s.ECHConfigList()) == 0 || s.lastTTL == 0 || time.Since(s.lastUpdate) > s.lastTTL {
|
|
queryServerName := s.queryServerName
|
|
if queryServerName == "" {
|
|
queryServerName = s.ServerName()
|
|
}
|
|
message := &mDNS.Msg{
|
|
MsgHdr: mDNS.MsgHdr{
|
|
RecursionDesired: true,
|
|
},
|
|
Question: []mDNS.Question{
|
|
{
|
|
Name: mDNS.Fqdn(queryServerName),
|
|
Qtype: mDNS.TypeHTTPS,
|
|
Qclass: mDNS.ClassINET,
|
|
},
|
|
},
|
|
}
|
|
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")
|
|
}
|
|
if response.Rcode != mDNS.RcodeSuccess {
|
|
return nil, E.Cause(dns.RcodeError(response.Rcode), "fetch ECH config list")
|
|
}
|
|
match:
|
|
for _, rr := range response.Answer {
|
|
switch resource := rr.(type) {
|
|
case *mDNS.HTTPS:
|
|
for _, value := range resource.Value {
|
|
if value.Key().String() == "ech" {
|
|
echConfigList, err := base64.StdEncoding.DecodeString(value.String())
|
|
if err != nil {
|
|
return nil, E.Cause(err, "decode ECH config")
|
|
}
|
|
s.lastTTL = time.Duration(rr.Header().Ttl) * time.Second
|
|
s.lastUpdate = time.Now()
|
|
s.SetECHConfigList(echConfigList)
|
|
break match
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if len(s.ECHConfigList()) == 0 {
|
|
return nil, E.New("no ECH config found in DNS records")
|
|
}
|
|
}
|
|
return s.Client(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,
|
|
}
|
|
}
|
|
|
|
func UnmarshalECHKeys(raw []byte) ([]tls.EncryptedClientHelloKey, error) {
|
|
var keys []tls.EncryptedClientHelloKey
|
|
rawString := cryptobyte.String(raw)
|
|
for !rawString.Empty() {
|
|
var key tls.EncryptedClientHelloKey
|
|
if !rawString.ReadUint16LengthPrefixed((*cryptobyte.String)(&key.PrivateKey)) {
|
|
return nil, E.New("error parsing private key")
|
|
}
|
|
if !rawString.ReadUint16LengthPrefixed((*cryptobyte.String)(&key.Config)) {
|
|
return nil, E.New("error parsing config")
|
|
}
|
|
keys = append(keys, key)
|
|
}
|
|
if len(keys) == 0 {
|
|
return nil, E.New("empty ECH keys")
|
|
}
|
|
return keys, nil
|
|
}
|