chore: exposure ipv6 wait time

This commit is contained in:
Skyxim 2023-03-10 23:38:16 +08:00
parent 035d878a9f
commit 07f3cd2ae5
4 changed files with 30 additions and 14 deletions

View file

@ -92,6 +92,7 @@ type DNS struct {
Enable bool `yaml:"enable"` Enable bool `yaml:"enable"`
PreferH3 bool `yaml:"prefer-h3"` PreferH3 bool `yaml:"prefer-h3"`
IPv6 bool `yaml:"ipv6"` IPv6 bool `yaml:"ipv6"`
IPv6Timeout uint `yaml:"ipv6-timeout"`
NameServer []dns.NameServer `yaml:"nameserver"` NameServer []dns.NameServer `yaml:"nameserver"`
Fallback []dns.NameServer `yaml:"fallback"` Fallback []dns.NameServer `yaml:"fallback"`
FallbackFilter FallbackFilter `yaml:"fallback-filter"` FallbackFilter FallbackFilter `yaml:"fallback-filter"`
@ -171,6 +172,7 @@ type RawDNS struct {
Enable bool `yaml:"enable"` Enable bool `yaml:"enable"`
PreferH3 bool `yaml:"prefer-h3"` PreferH3 bool `yaml:"prefer-h3"`
IPv6 bool `yaml:"ipv6"` IPv6 bool `yaml:"ipv6"`
IPv6Timeout uint `yaml:"ipv6-timeout"`
UseHosts bool `yaml:"use-hosts"` UseHosts bool `yaml:"use-hosts"`
NameServer []string `yaml:"nameserver"` NameServer []string `yaml:"nameserver"`
Fallback []string `yaml:"fallback"` Fallback []string `yaml:"fallback"`
@ -377,6 +379,7 @@ func UnmarshalRawConfig(buf []byte) (*RawConfig, error) {
Enable: false, Enable: false,
IPv6: false, IPv6: false,
UseHosts: true, UseHosts: true,
IPv6Timeout: 100,
EnhancedMode: C.DNSMapping, EnhancedMode: C.DNSMapping,
FakeIPRange: "198.18.0.1/16", FakeIPRange: "198.18.0.1/16",
FallbackFilter: RawFallbackFilter{ FallbackFilter: RawFallbackFilter{
@ -1048,6 +1051,7 @@ func parseDNS(rawCfg *RawConfig, hosts *trie.DomainTrie[netip.Addr], rules []C.R
Enable: cfg.Enable, Enable: cfg.Enable,
Listen: cfg.Listen, Listen: cfg.Listen,
PreferH3: cfg.PreferH3, PreferH3: cfg.PreferH3,
IPv6Timeout: cfg.IPv6Timeout,
IPv6: cfg.IPv6, IPv6: cfg.IPv6,
EnhancedMode: cfg.EnhancedMode, EnhancedMode: cfg.EnhancedMode,
FallbackFilter: FallbackFilter{ FallbackFilter: FallbackFilter{

View file

@ -42,6 +42,7 @@ type geositePolicyRecord struct {
type Resolver struct { type Resolver struct {
ipv6 bool ipv6 bool
ipv6Timeout time.Duration
hosts *trie.DomainTrie[netip.Addr] hosts *trie.DomainTrie[netip.Addr]
main []dnsClient main []dnsClient
fallback []dnsClient fallback []dnsClient
@ -91,14 +92,20 @@ func (r *Resolver) LookupIP(ctx context.Context, host string) (ips []netip.Addr,
}() }()
ips, err = r.lookupIP(ctx, host, D.TypeA) ips, err = r.lookupIP(ctx, host, D.TypeA)
var waitIPv6 *time.Timer
if r != nil {
waitIPv6 = time.NewTimer(r.ipv6Timeout)
} else {
waitIPv6 = time.NewTimer(100 * time.Millisecond)
}
defer waitIPv6.Stop()
select { select {
case ipv6s, open := <-ch: case ipv6s, open := <-ch:
if !open && err != nil { if !open && err != nil {
return nil, resolver.ErrIPNotFound return nil, resolver.ErrIPNotFound
} }
ips = append(ips, ipv6s...) ips = append(ips, ipv6s...)
case <-time.After(30 * time.Millisecond): case <-waitIPv6.C:
// wait ipv6 result // wait ipv6 result
} }
@ -419,6 +426,7 @@ type Config struct {
Default []NameServer Default []NameServer
ProxyServer []NameServer ProxyServer []NameServer
IPv6 bool IPv6 bool
IPv6Timeout uint
EnhancedMode C.DNSMode EnhancedMode C.DNSMode
FallbackFilter FallbackFilter FallbackFilter FallbackFilter
Pool *fakeip.Pool Pool *fakeip.Pool
@ -428,15 +436,17 @@ type Config struct {
func NewResolver(config Config) *Resolver { func NewResolver(config Config) *Resolver {
defaultResolver := &Resolver{ defaultResolver := &Resolver{
main: transform(config.Default, nil), main: transform(config.Default, nil),
lruCache: cache.New(cache.WithSize[string, *D.Msg](4096), cache.WithStale[string, *D.Msg](true)), lruCache: cache.New(cache.WithSize[string, *D.Msg](4096), cache.WithStale[string, *D.Msg](true)),
ipv6Timeout: time.Duration(config.IPv6Timeout) * time.Millisecond,
} }
r := &Resolver{ r := &Resolver{
ipv6: config.IPv6, ipv6: config.IPv6,
main: transform(config.Main, defaultResolver), main: transform(config.Main, defaultResolver),
lruCache: cache.New(cache.WithSize[string, *D.Msg](4096), cache.WithStale[string, *D.Msg](true)), lruCache: cache.New(cache.WithSize[string, *D.Msg](4096), cache.WithStale[string, *D.Msg](true)),
hosts: config.Hosts, hosts: config.Hosts,
ipv6Timeout: time.Duration(config.IPv6Timeout) * time.Millisecond,
} }
if len(config.Fallback) != 0 { if len(config.Fallback) != 0 {
@ -502,11 +512,12 @@ func NewResolver(config Config) *Resolver {
func NewProxyServerHostResolver(old *Resolver) *Resolver { func NewProxyServerHostResolver(old *Resolver) *Resolver {
r := &Resolver{ r := &Resolver{
ipv6: old.ipv6, ipv6: old.ipv6,
main: old.proxyServer, main: old.proxyServer,
lruCache: old.lruCache, lruCache: old.lruCache,
hosts: old.hosts, hosts: old.hosts,
policy: old.policy, policy: old.policy,
ipv6Timeout: old.ipv6Timeout,
} }
return r return r
} }

View file

@ -166,7 +166,7 @@ dns:
prefer-h3: true # 开启 DoH 支持 HTTP/3将并发尝试 prefer-h3: true # 开启 DoH 支持 HTTP/3将并发尝试
listen: 0.0.0.0:53 # 开启 DNS 服务器监听 listen: 0.0.0.0:53 # 开启 DNS 服务器监听
# ipv6: false # false 将返回 AAAA 的空结果 # ipv6: false # false 将返回 AAAA 的空结果
# ipv6-timeout: 300 # 单位ms内部双栈并发时向上游查询 AAAA 时,等待 AAAA 的时间,默认 100ms
# 用于解析 nameserverfallback 以及其他DNS服务器配置的DNS 服务域名 # 用于解析 nameserverfallback 以及其他DNS服务器配置的DNS 服务域名
# 只能使用纯 IP 地址,可使用加密 DNS # 只能使用纯 IP 地址,可使用加密 DNS
default-nameserver: default-nameserver:

View file

@ -190,6 +190,7 @@ func updateDNS(c *config.DNS, generalIPv6 bool) {
Main: c.NameServer, Main: c.NameServer,
Fallback: c.Fallback, Fallback: c.Fallback,
IPv6: c.IPv6 && generalIPv6, IPv6: c.IPv6 && generalIPv6,
IPv6Timeout: c.IPv6Timeout,
EnhancedMode: c.EnhancedMode, EnhancedMode: c.EnhancedMode,
Pool: c.FakeIPRange, Pool: c.FakeIPRange,
Hosts: c.Hosts, Hosts: c.Hosts,