Files
sing-box-extended-mirror/adapter/dns.go
T

117 lines
4.2 KiB
Go
Raw Normal View History

2025-03-16 14:50:44 +08:00
package adapter
import (
"context"
"net/netip"
2026-04-11 12:10:52 +08:00
"time"
2025-03-16 14:50:44 +08:00
C "github.com/sagernet/sing-box/constant"
"github.com/sagernet/sing-box/log"
"github.com/sagernet/sing-box/option"
2025-03-29 17:24:34 +08:00
E "github.com/sagernet/sing/common/exceptions"
2025-03-16 14:50:44 +08:00
"github.com/sagernet/sing/common/logger"
2025-03-29 17:24:34 +08:00
"github.com/sagernet/sing/service"
2025-03-16 14:50:44 +08:00
"github.com/miekg/dns"
)
type DNSRouter interface {
Lifecycle
Exchange(ctx context.Context, message *dns.Msg, options DNSQueryOptions) (*dns.Msg, error)
2026-07-19 16:37:16 +08:00
ExchangeAsync(ctx context.Context, message *dns.Msg, options DNSQueryOptions, callback func(response *dns.Msg, err error))
2025-03-16 14:50:44 +08:00
Lookup(ctx context.Context, domain string, options DNSQueryOptions) ([]netip.Addr, error)
ClearCache()
LookupReverseMapping(ip netip.Addr) (string, bool)
ResetNetwork()
}
type DNSClient interface {
Start()
Exchange(ctx context.Context, transport DNSTransport, message *dns.Msg, options DNSQueryOptions, responseChecker func(response *dns.Msg) bool) (*dns.Msg, error)
2026-07-19 16:37:16 +08:00
ExchangeAsync(ctx context.Context, transport DNSTransport, message *dns.Msg, options DNSQueryOptions, responseChecker func(response *dns.Msg) bool, callback func(response *dns.Msg, err error))
Lookup(ctx context.Context, transport DNSTransport, domain string, options DNSQueryOptions, responseChecker func(response *dns.Msg) bool) ([]netip.Addr, error)
2025-03-16 14:50:44 +08:00
ClearCache()
}
type DNSQueryOptions struct {
2026-04-11 12:10:52 +08:00
Transport DNSTransport
Strategy C.DomainStrategy
LookupStrategy C.DomainStrategy
DisableCache bool
DisableOptimisticCache bool
RewriteTTL *uint32
2026-04-28 14:29:52 +08:00
Timeout time.Duration
2026-04-11 12:10:52 +08:00
ClientSubnet netip.Prefix
RemoveClientSubnet bool
2025-03-16 14:50:44 +08:00
}
2026-05-11 16:24:34 +08:00
func DNSQueryOptionsFrom(ctx context.Context, options *option.DomainResolveOptions) (DNSQueryOptions, error) {
if options == nil || options.Server == "" {
return DNSQueryOptions{}, nil
2025-03-29 17:24:34 +08:00
}
transportManager := service.FromContext[DNSTransportManager](ctx)
transport, loaded := transportManager.Transport(options.Server)
if !loaded {
2026-05-11 16:24:34 +08:00
return DNSQueryOptions{}, E.New("domain resolver not found: " + options.Server)
2025-03-29 17:24:34 +08:00
}
2026-05-11 16:24:34 +08:00
return DNSQueryOptions{
2026-04-11 12:10:52 +08:00
Transport: transport,
Strategy: C.DomainStrategy(options.Strategy),
DisableCache: options.DisableCache,
DisableOptimisticCache: options.DisableOptimisticCache,
RewriteTTL: options.RewriteTTL,
2026-04-28 14:29:52 +08:00
Timeout: time.Duration(options.Timeout),
2026-04-11 12:10:52 +08:00
ClientSubnet: options.ClientSubnet.Build(netip.Prefix{}),
2025-03-29 17:24:34 +08:00
}, nil
}
2025-03-16 14:50:44 +08:00
type RDRCStore interface {
LoadRDRC(transportName string, qName string, qType uint16) (rejected bool)
SaveRDRC(transportName string, qName string, qType uint16) error
SaveRDRCAsync(transportName string, qName string, qType uint16, logger logger.Logger)
}
2026-04-11 12:10:52 +08:00
type DNSCacheStore interface {
LoadDNSCache(transportName string, qName string, qType uint16) (rawMessage []byte, expireAt time.Time, loaded bool)
SaveDNSCache(transportName string, qName string, qType uint16, rawMessage []byte, expireAt time.Time) error
SaveDNSCacheAsync(transportName string, qName string, qType uint16, rawMessage []byte, expireAt time.Time, logger logger.Logger)
ClearDNSCache() error
}
2025-03-16 14:50:44 +08:00
type DNSTransport interface {
Lifecycle
2025-03-16 14:50:44 +08:00
Type() string
Tag() string
Dependencies() []string
2026-04-23 01:33:38 +08:00
// Reset closes the transport's existing connections so later requests use fresh connections.
// Exchanges that are currently using those connections may fail.
2025-12-29 20:44:30 +08:00
Reset()
2025-03-16 14:50:44 +08:00
Exchange(ctx context.Context, message *dns.Msg) (*dns.Msg, error)
2026-07-19 16:37:16 +08:00
ExchangeAsync(ctx context.Context, message *dns.Msg, callback func(response *dns.Msg, err error))
2025-03-16 14:50:44 +08:00
}
2026-04-29 22:48:02 +08:00
type DNSTransportWithPreferredDomain interface {
DNSTransport
PreferredDomain(domain string) bool
}
type DNSTransportWithEnvironment interface {
DNSTransport
Environment() []string
}
2025-03-16 14:50:44 +08:00
type DNSTransportRegistry interface {
option.DNSTransportOptionsRegistry
CreateDNSTransport(ctx context.Context, logger log.ContextLogger, tag string, transportType string, options any) (DNSTransport, error)
}
type DNSTransportManager interface {
Lifecycle
Transports() []DNSTransport
Transport(tag string) (DNSTransport, bool)
Default() DNSTransport
FakeIP() FakeIPTransport
Remove(tag string) error
Create(ctx context.Context, logger log.ContextLogger, tag string, outboundType string, options any) error
}