2019-06-28 04:29:08 +00:00
|
|
|
package dns
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
2020-04-16 10:31:40 +00:00
|
|
|
"fmt"
|
2022-04-28 14:40:06 +00:00
|
|
|
"go.uber.org/atomic"
|
2020-02-14 08:36:20 +00:00
|
|
|
"math/rand"
|
2022-04-05 20:25:53 +00:00
|
|
|
"net/netip"
|
2019-06-28 04:29:08 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/Dreamacro/clash/common/cache"
|
|
|
|
"github.com/Dreamacro/clash/common/picker"
|
|
|
|
"github.com/Dreamacro/clash/component/fakeip"
|
2021-11-17 08:03:47 +00:00
|
|
|
"github.com/Dreamacro/clash/component/geodata/router"
|
2020-02-15 13:42:46 +00:00
|
|
|
"github.com/Dreamacro/clash/component/resolver"
|
2020-08-11 02:28:17 +00:00
|
|
|
"github.com/Dreamacro/clash/component/trie"
|
2021-10-18 13:08:27 +00:00
|
|
|
C "github.com/Dreamacro/clash/constant"
|
2019-06-28 04:29:08 +00:00
|
|
|
|
|
|
|
D "github.com/miekg/dns"
|
2019-07-14 11:29:58 +00:00
|
|
|
"golang.org/x/sync/singleflight"
|
2019-06-28 04:29:08 +00:00
|
|
|
)
|
|
|
|
|
2020-02-15 13:42:46 +00:00
|
|
|
type dnsClient interface {
|
2019-06-28 04:29:08 +00:00
|
|
|
Exchange(m *D.Msg) (msg *D.Msg, err error)
|
|
|
|
ExchangeContext(ctx context.Context, m *D.Msg) (msg *D.Msg, err error)
|
|
|
|
}
|
|
|
|
|
|
|
|
type result struct {
|
|
|
|
Msg *D.Msg
|
|
|
|
Error error
|
|
|
|
}
|
|
|
|
|
|
|
|
type Resolver struct {
|
2020-09-28 14:17:10 +00:00
|
|
|
ipv6 bool
|
2022-04-05 20:25:53 +00:00
|
|
|
hosts *trie.DomainTrie[netip.Addr]
|
2020-09-28 14:17:10 +00:00
|
|
|
main []dnsClient
|
|
|
|
fallback []dnsClient
|
|
|
|
fallbackDomainFilters []fallbackDomainFilter
|
|
|
|
fallbackIPFilters []fallbackIPFilter
|
|
|
|
group singleflight.Group
|
2022-04-05 12:23:16 +00:00
|
|
|
lruCache *cache.LruCache[string, *D.Msg]
|
2022-04-05 20:25:53 +00:00
|
|
|
policy *trie.DomainTrie[*Policy]
|
2022-03-27 16:44:13 +00:00
|
|
|
proxyServer []dnsClient
|
2019-06-28 04:29:08 +00:00
|
|
|
}
|
|
|
|
|
2022-04-22 16:27:22 +00:00
|
|
|
func (r *Resolver) ResolveAllIPPrimaryIPv4(host string) (ips []netip.Addr, err error) {
|
|
|
|
ch := make(chan []netip.Addr, 1)
|
2019-06-28 04:29:08 +00:00
|
|
|
go func() {
|
2019-06-28 16:58:59 +00:00
|
|
|
defer close(ch)
|
2020-02-17 12:11:46 +00:00
|
|
|
ip, err := r.resolveIP(host, D.TypeAAAA)
|
2019-06-28 04:29:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ch <- ip
|
|
|
|
}()
|
|
|
|
|
2022-04-22 16:27:22 +00:00
|
|
|
ips, err = r.resolveIP(host, D.TypeA)
|
2019-06-28 04:29:08 +00:00
|
|
|
if err == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-06-28 16:58:59 +00:00
|
|
|
ip, open := <-ch
|
|
|
|
if !open {
|
2022-04-22 16:27:22 +00:00
|
|
|
return nil, resolver.ErrIPNotFound
|
2019-06-28 04:29:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ip, nil
|
|
|
|
}
|
|
|
|
|
2022-04-22 16:27:22 +00:00
|
|
|
func (r *Resolver) ResolveAllIP(host string) (ips []netip.Addr, err error) {
|
|
|
|
ch := make(chan []netip.Addr, 1)
|
|
|
|
go func() {
|
|
|
|
defer close(ch)
|
|
|
|
ip, err := r.resolveIP(host, D.TypeAAAA)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ch <- ip
|
|
|
|
}()
|
|
|
|
|
|
|
|
ips, err = r.resolveIP(host, D.TypeA)
|
|
|
|
|
2022-05-20 16:34:00 +00:00
|
|
|
select {
|
|
|
|
case ipv6s, open := <-ch:
|
|
|
|
if !open && err != nil {
|
|
|
|
return nil, resolver.ErrIPNotFound
|
|
|
|
}
|
|
|
|
ips = append(ips, ipv6s...)
|
2022-05-28 01:58:45 +00:00
|
|
|
case <-time.After(1 * time.Millisecond):
|
|
|
|
// wait ipv6 result
|
2022-04-22 16:27:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ips, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Resolver) ResolveAllIPv4(host string) (ips []netip.Addr, err error) {
|
|
|
|
return r.resolveIP(host, D.TypeA)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Resolver) ResolveAllIPv6(host string) (ips []netip.Addr, err error) {
|
|
|
|
return r.resolveIP(host, D.TypeAAAA)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ResolveIP request with TypeA and TypeAAAA, priority return TypeA
|
|
|
|
func (r *Resolver) ResolveIP(host string) (ip netip.Addr, err error) {
|
|
|
|
if ips, err := r.ResolveAllIPPrimaryIPv4(host); err == nil {
|
|
|
|
return ips[rand.Intn(len(ips))], nil
|
|
|
|
} else {
|
|
|
|
return netip.Addr{}, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-28 04:29:08 +00:00
|
|
|
// ResolveIPv4 request with TypeA
|
2022-04-19 17:52:51 +00:00
|
|
|
func (r *Resolver) ResolveIPv4(host string) (ip netip.Addr, err error) {
|
2022-04-22 16:27:22 +00:00
|
|
|
if ips, err := r.ResolveAllIPv4(host); err == nil {
|
|
|
|
return ips[rand.Intn(len(ips))], nil
|
|
|
|
} else {
|
|
|
|
return netip.Addr{}, err
|
|
|
|
}
|
2019-09-11 09:00:55 +00:00
|
|
|
}
|
2019-06-28 04:29:08 +00:00
|
|
|
|
2019-09-11 09:00:55 +00:00
|
|
|
// ResolveIPv6 request with TypeAAAA
|
2022-04-19 17:52:51 +00:00
|
|
|
func (r *Resolver) ResolveIPv6(host string) (ip netip.Addr, err error) {
|
2022-04-22 16:27:22 +00:00
|
|
|
if ips, err := r.ResolveAllIPv6(host); err == nil {
|
|
|
|
return ips[rand.Intn(len(ips))], nil
|
|
|
|
} else {
|
|
|
|
return netip.Addr{}, err
|
|
|
|
}
|
2019-06-28 04:29:08 +00:00
|
|
|
}
|
|
|
|
|
2022-04-19 17:52:51 +00:00
|
|
|
func (r *Resolver) shouldIPFallback(ip netip.Addr) bool {
|
2020-09-28 14:17:10 +00:00
|
|
|
for _, filter := range r.fallbackIPFilters {
|
2019-09-15 05:36:45 +00:00
|
|
|
if filter.Match(ip) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-06-28 04:29:08 +00:00
|
|
|
// Exchange a batch of dns request, and it use cache
|
|
|
|
func (r *Resolver) Exchange(m *D.Msg) (msg *D.Msg, err error) {
|
2021-09-06 15:07:34 +00:00
|
|
|
return r.ExchangeContext(context.Background(), m)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ExchangeContext a batch of dns request with context.Context, and it use cache
|
|
|
|
func (r *Resolver) ExchangeContext(ctx context.Context, m *D.Msg) (msg *D.Msg, err error) {
|
2019-06-28 04:29:08 +00:00
|
|
|
if len(m.Question) == 0 {
|
|
|
|
return nil, errors.New("should have one question at least")
|
|
|
|
}
|
|
|
|
|
|
|
|
q := m.Question[0]
|
2022-04-19 17:52:51 +00:00
|
|
|
cacheM, expireTime, hit := r.lruCache.GetWithExpire(q.String())
|
2020-05-07 07:10:14 +00:00
|
|
|
if hit {
|
|
|
|
now := time.Now()
|
2022-04-19 17:52:51 +00:00
|
|
|
msg = cacheM.Copy()
|
2020-05-07 07:10:14 +00:00
|
|
|
if expireTime.Before(now) {
|
|
|
|
setMsgTTL(msg, uint32(1)) // Continue fetch
|
2021-09-06 15:07:34 +00:00
|
|
|
go r.exchangeWithoutCache(ctx, m)
|
2020-05-07 07:10:14 +00:00
|
|
|
} else {
|
2020-08-25 14:19:59 +00:00
|
|
|
setMsgTTL(msg, uint32(time.Until(expireTime).Seconds()))
|
2020-05-07 07:10:14 +00:00
|
|
|
}
|
2019-06-28 04:29:08 +00:00
|
|
|
return
|
|
|
|
}
|
2021-09-06 15:07:34 +00:00
|
|
|
return r.exchangeWithoutCache(ctx, m)
|
2020-05-07 07:10:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ExchangeWithoutCache a batch of dns request, and it do NOT GET from cache
|
2021-09-06 15:07:34 +00:00
|
|
|
func (r *Resolver) exchangeWithoutCache(ctx context.Context, m *D.Msg) (msg *D.Msg, err error) {
|
2020-05-07 07:10:14 +00:00
|
|
|
q := m.Question[0]
|
|
|
|
|
2022-03-16 04:10:13 +00:00
|
|
|
ret, err, shared := r.group.Do(q.String(), func() (result any, err error) {
|
2020-07-20 13:16:36 +00:00
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
msg := result.(*D.Msg)
|
|
|
|
|
|
|
|
putMsgToCache(r.lruCache, q.String(), msg)
|
|
|
|
}()
|
|
|
|
|
2020-07-17 09:34:40 +00:00
|
|
|
isIPReq := isIPRequest(q)
|
|
|
|
if isIPReq {
|
2021-09-06 15:07:34 +00:00
|
|
|
return r.ipExchange(ctx, m)
|
2019-06-28 04:29:08 +00:00
|
|
|
}
|
|
|
|
|
2021-05-19 03:17:35 +00:00
|
|
|
if matched := r.matchPolicy(m); len(matched) != 0 {
|
2021-09-06 15:07:34 +00:00
|
|
|
return r.batchExchange(ctx, matched, m)
|
2021-05-19 03:17:35 +00:00
|
|
|
}
|
2021-09-06 15:07:34 +00:00
|
|
|
return r.batchExchange(ctx, r.main, m)
|
2019-07-14 11:29:58 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
msg = ret.(*D.Msg)
|
2020-03-24 02:13:53 +00:00
|
|
|
if shared {
|
|
|
|
msg = msg.Copy()
|
|
|
|
}
|
2019-06-28 04:29:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-09-06 15:07:34 +00:00
|
|
|
func (r *Resolver) batchExchange(ctx context.Context, clients []dnsClient, m *D.Msg) (msg *D.Msg, err error) {
|
2022-04-23 18:07:57 +00:00
|
|
|
fast, ctx := picker.WithTimeout[*D.Msg](ctx, resolver.DefaultDNSTimeout)
|
2019-08-02 01:22:09 +00:00
|
|
|
for _, client := range clients {
|
|
|
|
r := client
|
2022-04-23 18:07:57 +00:00
|
|
|
fast.Go(func() (*D.Msg, error) {
|
2020-02-25 13:51:48 +00:00
|
|
|
m, err := r.ExchangeContext(ctx, m)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-03-08 05:00:42 +00:00
|
|
|
} else if m.Rcode == D.RcodeServerFailure || m.Rcode == D.RcodeRefused {
|
2020-02-25 13:51:48 +00:00
|
|
|
return nil, errors.New("server failure")
|
|
|
|
}
|
|
|
|
return m, nil
|
2019-07-02 11:18:03 +00:00
|
|
|
})
|
2019-06-28 04:29:08 +00:00
|
|
|
}
|
|
|
|
|
2020-02-14 08:36:20 +00:00
|
|
|
elm := fast.Wait()
|
2019-07-02 11:18:03 +00:00
|
|
|
if elm == nil {
|
2020-08-25 14:19:59 +00:00
|
|
|
err := errors.New("all DNS requests failed")
|
2020-04-16 10:31:40 +00:00
|
|
|
if fErr := fast.Error(); fErr != nil {
|
|
|
|
err = fmt.Errorf("%w, first error: %s", err, fErr.Error())
|
|
|
|
}
|
|
|
|
return nil, err
|
2019-06-28 04:29:08 +00:00
|
|
|
}
|
|
|
|
|
2022-04-23 18:07:57 +00:00
|
|
|
msg = elm
|
2019-06-28 04:29:08 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-05-19 03:17:35 +00:00
|
|
|
func (r *Resolver) matchPolicy(m *D.Msg) []dnsClient {
|
|
|
|
if r.policy == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-05-15 05:16:45 +00:00
|
|
|
domain := msgToDomain(m)
|
2021-05-19 03:17:35 +00:00
|
|
|
if domain == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
record := r.policy.Search(domain)
|
|
|
|
if record == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-04-05 20:25:53 +00:00
|
|
|
p := record.Data
|
|
|
|
return p.GetData()
|
2021-05-19 03:17:35 +00:00
|
|
|
}
|
|
|
|
|
2020-09-28 14:17:10 +00:00
|
|
|
func (r *Resolver) shouldOnlyQueryFallback(m *D.Msg) bool {
|
|
|
|
if r.fallback == nil || len(r.fallbackDomainFilters) == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-05-15 05:16:45 +00:00
|
|
|
domain := msgToDomain(m)
|
2020-09-28 14:17:10 +00:00
|
|
|
|
|
|
|
if domain == "" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, df := range r.fallbackDomainFilters {
|
|
|
|
if df.Match(domain) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-09-06 15:07:34 +00:00
|
|
|
func (r *Resolver) ipExchange(ctx context.Context, m *D.Msg) (msg *D.Msg, err error) {
|
2021-05-19 03:17:35 +00:00
|
|
|
if matched := r.matchPolicy(m); len(matched) != 0 {
|
2021-09-06 15:07:34 +00:00
|
|
|
res := <-r.asyncExchange(ctx, matched, m)
|
2021-05-19 03:17:35 +00:00
|
|
|
return res.Msg, res.Error
|
|
|
|
}
|
|
|
|
|
2020-09-28 14:17:10 +00:00
|
|
|
onlyFallback := r.shouldOnlyQueryFallback(m)
|
|
|
|
|
|
|
|
if onlyFallback {
|
2021-09-06 15:07:34 +00:00
|
|
|
res := <-r.asyncExchange(ctx, r.fallback, m)
|
2020-09-28 14:17:10 +00:00
|
|
|
return res.Msg, res.Error
|
|
|
|
}
|
|
|
|
|
2021-09-06 15:07:34 +00:00
|
|
|
msgCh := r.asyncExchange(ctx, r.main, m)
|
2020-09-28 14:17:10 +00:00
|
|
|
|
2021-11-17 08:03:47 +00:00
|
|
|
if r.fallback == nil || len(r.fallback) == 0 { // directly return if no fallback servers are available
|
2019-06-28 04:29:08 +00:00
|
|
|
res := <-msgCh
|
|
|
|
msg, err = res.Msg, res.Error
|
|
|
|
return
|
|
|
|
}
|
2020-09-28 14:17:10 +00:00
|
|
|
|
2019-06-28 04:29:08 +00:00
|
|
|
res := <-msgCh
|
|
|
|
if res.Error == nil {
|
2021-01-23 06:49:46 +00:00
|
|
|
if ips := msgToIP(res.Msg); len(ips) != 0 {
|
2020-09-28 14:17:10 +00:00
|
|
|
if !r.shouldIPFallback(ips[0]) {
|
2022-03-17 09:41:02 +00:00
|
|
|
msg, err = res.Msg, res.Error // no need to wait for fallback result
|
|
|
|
return
|
2019-06-28 04:29:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-17 08:03:47 +00:00
|
|
|
res = <-r.asyncExchange(ctx, r.fallback, m)
|
2019-06-28 04:29:08 +00:00
|
|
|
msg, err = res.Msg, res.Error
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-04-22 16:27:22 +00:00
|
|
|
func (r *Resolver) resolveIP(host string, dnsType uint16) (ips []netip.Addr, err error) {
|
|
|
|
ip, err := netip.ParseAddr(host)
|
2022-04-19 17:52:51 +00:00
|
|
|
if err == nil {
|
|
|
|
isIPv4 := ip.Is4()
|
2019-09-27 07:26:07 +00:00
|
|
|
if dnsType == D.TypeAAAA && !isIPv4 {
|
2022-04-22 16:27:22 +00:00
|
|
|
return []netip.Addr{ip}, nil
|
2019-09-27 07:26:07 +00:00
|
|
|
} else if dnsType == D.TypeA && isIPv4 {
|
2022-04-22 16:27:22 +00:00
|
|
|
return []netip.Addr{ip}, nil
|
2020-02-17 12:11:46 +00:00
|
|
|
} else {
|
2022-04-22 16:27:22 +00:00
|
|
|
return []netip.Addr{}, resolver.ErrIPVersion
|
2019-09-11 09:00:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-28 04:29:08 +00:00
|
|
|
query := &D.Msg{}
|
|
|
|
query.SetQuestion(D.Fqdn(host), dnsType)
|
|
|
|
|
|
|
|
msg, err := r.Exchange(query)
|
|
|
|
if err != nil {
|
2022-04-22 16:27:22 +00:00
|
|
|
return []netip.Addr{}, err
|
2019-06-28 04:29:08 +00:00
|
|
|
}
|
|
|
|
|
2022-04-22 16:27:22 +00:00
|
|
|
ips = msgToIP(msg)
|
2020-02-14 08:36:20 +00:00
|
|
|
ipLength := len(ips)
|
|
|
|
if ipLength == 0 {
|
2022-04-22 16:27:22 +00:00
|
|
|
return []netip.Addr{}, resolver.ErrIPNotFound
|
2019-06-28 04:29:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-09-06 15:07:34 +00:00
|
|
|
func (r *Resolver) asyncExchange(ctx context.Context, client []dnsClient, msg *D.Msg) <-chan *result {
|
2020-03-12 16:11:54 +00:00
|
|
|
ch := make(chan *result, 1)
|
2019-06-28 04:29:08 +00:00
|
|
|
go func() {
|
2021-09-06 15:07:34 +00:00
|
|
|
res, err := r.batchExchange(ctx, client, msg)
|
2019-06-28 04:29:08 +00:00
|
|
|
ch <- &result{Msg: res, Error: err}
|
|
|
|
}()
|
|
|
|
return ch
|
|
|
|
}
|
|
|
|
|
2022-03-27 16:44:13 +00:00
|
|
|
// HasProxyServer has proxy server dns client
|
|
|
|
func (r *Resolver) HasProxyServer() bool {
|
|
|
|
return len(r.main) > 0
|
|
|
|
}
|
|
|
|
|
2019-06-28 04:29:08 +00:00
|
|
|
type NameServer struct {
|
2021-11-17 08:03:47 +00:00
|
|
|
Net string
|
|
|
|
Addr string
|
2022-04-28 14:40:06 +00:00
|
|
|
Interface *atomic.String
|
2021-11-17 08:03:47 +00:00
|
|
|
ProxyAdapter string
|
2019-06-28 04:29:08 +00:00
|
|
|
}
|
|
|
|
|
2019-09-15 05:36:45 +00:00
|
|
|
type FallbackFilter struct {
|
2021-08-25 07:15:13 +00:00
|
|
|
GeoIP bool
|
|
|
|
GeoIPCode string
|
2022-04-19 17:52:51 +00:00
|
|
|
IPCIDR []*netip.Prefix
|
2021-08-25 07:15:13 +00:00
|
|
|
Domain []string
|
2021-11-17 08:03:47 +00:00
|
|
|
GeoSite []*router.DomainMatcher
|
2019-09-15 05:36:45 +00:00
|
|
|
}
|
|
|
|
|
2019-06-28 04:29:08 +00:00
|
|
|
type Config struct {
|
|
|
|
Main, Fallback []NameServer
|
2020-02-15 13:42:46 +00:00
|
|
|
Default []NameServer
|
2022-03-27 16:44:13 +00:00
|
|
|
ProxyServer []NameServer
|
2019-06-28 04:29:08 +00:00
|
|
|
IPv6 bool
|
2021-10-18 13:08:27 +00:00
|
|
|
EnhancedMode C.DNSMode
|
2019-09-15 05:36:45 +00:00
|
|
|
FallbackFilter FallbackFilter
|
2019-06-28 04:29:08 +00:00
|
|
|
Pool *fakeip.Pool
|
2022-04-05 20:25:53 +00:00
|
|
|
Hosts *trie.DomainTrie[netip.Addr]
|
2021-05-19 03:17:35 +00:00
|
|
|
Policy map[string]NameServer
|
2019-06-28 04:29:08 +00:00
|
|
|
}
|
|
|
|
|
2020-09-17 02:48:42 +00:00
|
|
|
func NewResolver(config Config) *Resolver {
|
2020-02-15 13:42:46 +00:00
|
|
|
defaultResolver := &Resolver{
|
2020-05-07 07:10:14 +00:00
|
|
|
main: transform(config.Default, nil),
|
2022-04-05 12:23:16 +00:00
|
|
|
lruCache: cache.NewLRUCache[string, *D.Msg](cache.WithSize[string, *D.Msg](4096), cache.WithStale[string, *D.Msg](true)),
|
2020-02-15 13:42:46 +00:00
|
|
|
}
|
|
|
|
|
2019-06-28 04:29:08 +00:00
|
|
|
r := &Resolver{
|
2020-05-07 07:10:14 +00:00
|
|
|
ipv6: config.IPv6,
|
|
|
|
main: transform(config.Main, defaultResolver),
|
2022-04-05 12:23:16 +00:00
|
|
|
lruCache: cache.NewLRUCache[string, *D.Msg](cache.WithSize[string, *D.Msg](4096), cache.WithStale[string, *D.Msg](true)),
|
2020-08-11 02:28:17 +00:00
|
|
|
hosts: config.Hosts,
|
2019-06-28 04:29:08 +00:00
|
|
|
}
|
2019-09-15 05:36:45 +00:00
|
|
|
|
2019-06-28 04:29:08 +00:00
|
|
|
if len(config.Fallback) != 0 {
|
2020-02-15 13:42:46 +00:00
|
|
|
r.fallback = transform(config.Fallback, defaultResolver)
|
2019-06-28 04:29:08 +00:00
|
|
|
}
|
2019-09-15 05:36:45 +00:00
|
|
|
|
2022-03-27 16:44:13 +00:00
|
|
|
if len(config.ProxyServer) != 0 {
|
|
|
|
r.proxyServer = transform(config.ProxyServer, defaultResolver)
|
|
|
|
}
|
|
|
|
|
2021-05-19 03:17:35 +00:00
|
|
|
if len(config.Policy) != 0 {
|
2022-04-05 20:25:53 +00:00
|
|
|
r.policy = trie.New[*Policy]()
|
2021-05-19 03:17:35 +00:00
|
|
|
for domain, nameserver := range config.Policy {
|
2022-04-19 17:52:51 +00:00
|
|
|
_ = r.policy.Insert(domain, NewPolicy(transform([]NameServer{nameserver}, defaultResolver)))
|
2021-05-19 03:17:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-28 14:17:10 +00:00
|
|
|
fallbackIPFilters := []fallbackIPFilter{}
|
2019-09-15 05:36:45 +00:00
|
|
|
if config.FallbackFilter.GeoIP {
|
2021-08-25 07:15:13 +00:00
|
|
|
fallbackIPFilters = append(fallbackIPFilters, &geoipFilter{
|
|
|
|
code: config.FallbackFilter.GeoIPCode,
|
|
|
|
})
|
2019-09-15 05:36:45 +00:00
|
|
|
}
|
|
|
|
for _, ipnet := range config.FallbackFilter.IPCIDR {
|
2020-09-28 14:17:10 +00:00
|
|
|
fallbackIPFilters = append(fallbackIPFilters, &ipnetFilter{ipnet: ipnet})
|
|
|
|
}
|
|
|
|
r.fallbackIPFilters = fallbackIPFilters
|
|
|
|
|
2021-11-17 08:03:47 +00:00
|
|
|
fallbackDomainFilters := []fallbackDomainFilter{}
|
2020-09-28 14:17:10 +00:00
|
|
|
if len(config.FallbackFilter.Domain) != 0 {
|
2021-11-17 08:03:47 +00:00
|
|
|
fallbackDomainFilters = append(fallbackDomainFilters, NewDomainFilter(config.FallbackFilter.Domain))
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(config.FallbackFilter.GeoSite) != 0 {
|
|
|
|
fallbackDomainFilters = append(fallbackDomainFilters, &geoSiteFilter{
|
|
|
|
matchers: config.FallbackFilter.GeoSite,
|
|
|
|
})
|
2019-09-15 05:36:45 +00:00
|
|
|
}
|
2021-11-17 08:03:47 +00:00
|
|
|
r.fallbackDomainFilters = fallbackDomainFilters
|
|
|
|
|
|
|
|
return r
|
|
|
|
}
|
2019-09-15 05:36:45 +00:00
|
|
|
|
2022-03-27 16:44:13 +00:00
|
|
|
func NewProxyServerHostResolver(old *Resolver) *Resolver {
|
2021-11-17 08:03:47 +00:00
|
|
|
r := &Resolver{
|
|
|
|
ipv6: old.ipv6,
|
2022-03-27 16:44:13 +00:00
|
|
|
main: old.proxyServer,
|
2021-11-17 08:03:47 +00:00
|
|
|
lruCache: old.lruCache,
|
|
|
|
hosts: old.hosts,
|
|
|
|
policy: old.policy,
|
|
|
|
}
|
2019-06-28 04:29:08 +00:00
|
|
|
return r
|
|
|
|
}
|