2019-06-28 04:29:08 +00:00
|
|
|
package dns
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
2022-11-12 13:31:07 +00:00
|
|
|
"fmt"
|
2022-04-05 20:25:53 +00:00
|
|
|
"net/netip"
|
2023-01-21 06:40:36 +00:00
|
|
|
"strings"
|
2019-06-28 04:29:08 +00:00
|
|
|
"time"
|
|
|
|
|
2023-01-21 06:40:36 +00:00
|
|
|
"go.uber.org/atomic"
|
|
|
|
|
2019-06-28 04:29:08 +00:00
|
|
|
"github.com/Dreamacro/clash/common/cache"
|
|
|
|
"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"
|
2023-04-01 03:53:39 +00:00
|
|
|
"github.com/Dreamacro/clash/constant/provider"
|
2023-01-21 06:40:36 +00:00
|
|
|
"github.com/Dreamacro/clash/log"
|
2019-06-28 04:29:08 +00:00
|
|
|
|
|
|
|
D "github.com/miekg/dns"
|
2023-03-06 10:10:14 +00:00
|
|
|
"github.com/zhangyunhao116/fastrand"
|
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)
|
2023-01-28 14:33:03 +00:00
|
|
|
Address() string
|
2019-06-28 04:29:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type result struct {
|
|
|
|
Msg *D.Msg
|
|
|
|
Error error
|
|
|
|
}
|
|
|
|
|
2023-01-21 06:40:36 +00:00
|
|
|
type geositePolicyRecord struct {
|
|
|
|
matcher fallbackDomainFilter
|
|
|
|
policy *Policy
|
|
|
|
inversedMatching bool
|
|
|
|
}
|
|
|
|
|
2023-04-01 03:53:39 +00:00
|
|
|
type domainSetPolicyRecord struct {
|
|
|
|
domainSetProvider provider.RuleProvider
|
|
|
|
policy *Policy
|
|
|
|
}
|
|
|
|
|
2019-06-28 04:29:08 +00:00
|
|
|
type Resolver struct {
|
2020-09-28 14:17:10 +00:00
|
|
|
ipv6 bool
|
2023-03-10 15:38:16 +00:00
|
|
|
ipv6Timeout time.Duration
|
2023-03-12 07:00:59 +00:00
|
|
|
hosts *trie.DomainTrie[resolver.HostValue]
|
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]
|
2023-04-01 03:53:39 +00:00
|
|
|
domainSetPolicy []domainSetPolicyRecord
|
2023-01-21 06:40:36 +00:00
|
|
|
geositePolicy []geositePolicyRecord
|
2022-03-27 16:44:13 +00:00
|
|
|
proxyServer []dnsClient
|
2019-06-28 04:29:08 +00:00
|
|
|
}
|
|
|
|
|
2022-11-12 05:18:36 +00:00
|
|
|
func (r *Resolver) LookupIPPrimaryIPv4(ctx context.Context, host string) (ips []netip.Addr, err error) {
|
2022-04-22 16:27:22 +00:00
|
|
|
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)
|
2022-08-13 05:07:35 +00:00
|
|
|
ip, err := r.lookupIP(ctx, host, D.TypeAAAA)
|
2019-06-28 04:29:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ch <- ip
|
|
|
|
}()
|
|
|
|
|
2022-11-12 12:43:48 +00:00
|
|
|
ips, err = r.lookupIP(ctx, 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-11-12 05:18:36 +00:00
|
|
|
func (r *Resolver) LookupIP(ctx context.Context, host string) (ips []netip.Addr, err error) {
|
2022-04-22 16:27:22 +00:00
|
|
|
ch := make(chan []netip.Addr, 1)
|
|
|
|
go func() {
|
|
|
|
defer close(ch)
|
2022-11-12 12:43:48 +00:00
|
|
|
ip, err := r.lookupIP(ctx, host, D.TypeAAAA)
|
2022-04-22 16:27:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ch <- ip
|
|
|
|
}()
|
|
|
|
|
2022-11-12 12:43:48 +00:00
|
|
|
ips, err = r.lookupIP(ctx, host, D.TypeA)
|
2023-03-10 15:38:16 +00:00
|
|
|
var waitIPv6 *time.Timer
|
|
|
|
if r != nil {
|
|
|
|
waitIPv6 = time.NewTimer(r.ipv6Timeout)
|
|
|
|
} else {
|
|
|
|
waitIPv6 = time.NewTimer(100 * time.Millisecond)
|
|
|
|
}
|
|
|
|
defer waitIPv6.Stop()
|
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...)
|
2023-03-10 15:38:16 +00:00
|
|
|
case <-waitIPv6.C:
|
2022-05-28 01:58:45 +00:00
|
|
|
// wait ipv6 result
|
2022-04-22 16:27:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ips, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ResolveIP request with TypeA and TypeAAAA, priority return TypeA
|
2022-11-12 05:18:36 +00:00
|
|
|
func (r *Resolver) ResolveIP(ctx context.Context, host string) (ip netip.Addr, err error) {
|
2022-11-12 13:31:07 +00:00
|
|
|
ips, err := r.LookupIPPrimaryIPv4(ctx, host)
|
|
|
|
if err != nil {
|
2022-04-22 16:27:22 +00:00
|
|
|
return netip.Addr{}, err
|
2022-11-12 13:31:07 +00:00
|
|
|
} else if len(ips) == 0 {
|
|
|
|
return netip.Addr{}, fmt.Errorf("%w: %s", resolver.ErrIPNotFound, host)
|
2022-04-22 16:27:22 +00:00
|
|
|
}
|
2023-03-06 10:10:14 +00:00
|
|
|
return ips[fastrand.Intn(len(ips))], nil
|
2022-11-12 13:31:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// LookupIPv4 request with TypeA
|
|
|
|
func (r *Resolver) LookupIPv4(ctx context.Context, host string) ([]netip.Addr, error) {
|
|
|
|
return r.lookupIP(ctx, host, D.TypeA)
|
2022-04-22 16:27:22 +00:00
|
|
|
}
|
|
|
|
|
2019-06-28 04:29:08 +00:00
|
|
|
// ResolveIPv4 request with TypeA
|
2022-11-12 05:18:36 +00:00
|
|
|
func (r *Resolver) ResolveIPv4(ctx context.Context, host string) (ip netip.Addr, err error) {
|
2022-11-12 13:31:07 +00:00
|
|
|
ips, err := r.lookupIP(ctx, host, D.TypeA)
|
|
|
|
if err != nil {
|
2022-04-22 16:27:22 +00:00
|
|
|
return netip.Addr{}, err
|
2022-11-12 13:31:07 +00:00
|
|
|
} else if len(ips) == 0 {
|
|
|
|
return netip.Addr{}, fmt.Errorf("%w: %s", resolver.ErrIPNotFound, host)
|
2022-04-22 16:27:22 +00:00
|
|
|
}
|
2023-03-06 10:10:14 +00:00
|
|
|
return ips[fastrand.Intn(len(ips))], nil
|
2022-11-12 13:31:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// LookupIPv6 request with TypeAAAA
|
|
|
|
func (r *Resolver) LookupIPv6(ctx context.Context, host string) ([]netip.Addr, error) {
|
|
|
|
return r.lookupIP(ctx, host, D.TypeAAAA)
|
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-11-12 05:18:36 +00:00
|
|
|
func (r *Resolver) ResolveIPv6(ctx context.Context, host string) (ip netip.Addr, err error) {
|
2022-11-12 13:31:07 +00:00
|
|
|
ips, err := r.lookupIP(ctx, host, D.TypeAAAA)
|
|
|
|
if err != nil {
|
2022-04-22 16:27:22 +00:00
|
|
|
return netip.Addr{}, err
|
2022-11-12 13:31:07 +00:00
|
|
|
} else if len(ips) == 0 {
|
|
|
|
return netip.Addr{}, fmt.Errorf("%w: %s", resolver.ErrIPNotFound, host)
|
2022-04-22 16:27:22 +00:00
|
|
|
}
|
2023-03-06 10:10:14 +00:00
|
|
|
return ips[fastrand.Intn(len(ips))], nil
|
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")
|
|
|
|
}
|
2022-11-12 10:29:19 +00:00
|
|
|
continueFetch := false
|
|
|
|
defer func() {
|
|
|
|
if continueFetch || errors.Is(err, context.DeadlineExceeded) || errors.Is(err, context.Canceled) {
|
|
|
|
go func() {
|
2022-11-16 09:53:52 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), resolver.DefaultDNSTimeout)
|
|
|
|
defer cancel()
|
2022-11-12 10:29:19 +00:00
|
|
|
_, _ = r.exchangeWithoutCache(ctx, m) // ignore result, just for putMsgToCache
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
}()
|
2019-06-28 04:29:08 +00:00
|
|
|
|
|
|
|
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
|
2022-11-12 10:29:19 +00:00
|
|
|
continueFetch = true
|
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-11-19 00:51:00 +00:00
|
|
|
retryNum := 0
|
|
|
|
retryMax := 3
|
|
|
|
fn := func() (result any, err error) {
|
2022-11-18 10:02:46 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), resolver.DefaultDNSTimeout) // reset timeout in singleflight
|
|
|
|
defer cancel()
|
|
|
|
|
2020-07-20 13:16:36 +00:00
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
2022-11-19 00:51:00 +00:00
|
|
|
result = retryNum
|
|
|
|
retryNum++
|
2020-07-20 13:16:36 +00:00
|
|
|
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)
|
2022-11-19 00:51:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ch := r.group.DoChan(q.String(), fn)
|
2019-07-14 11:29:58 +00:00
|
|
|
|
2022-11-18 10:02:46 +00:00
|
|
|
var result singleflight.Result
|
|
|
|
|
|
|
|
select {
|
|
|
|
case result = <-ch:
|
|
|
|
break
|
|
|
|
case <-ctx.Done():
|
|
|
|
select {
|
2022-11-19 00:51:00 +00:00
|
|
|
case result = <-ch: // maybe ctxDone and chFinish in same time, get DoChan's result as much as possible
|
2022-11-18 10:02:46 +00:00
|
|
|
break
|
2022-11-19 00:51:00 +00:00
|
|
|
default:
|
|
|
|
go func() { // start a retrying monitor in background
|
|
|
|
result := <-ch
|
|
|
|
ret, err, shared := result.Val, result.Err, result.Shared
|
|
|
|
if err != nil && !shared && ret.(int) < retryMax { // retry
|
|
|
|
r.group.DoChan(q.String(), fn)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return nil, ctx.Err()
|
2022-11-18 10:02:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ret, err, shared := result.Val, result.Err, result.Shared
|
2022-11-19 00:51:00 +00:00
|
|
|
if err != nil && !shared && ret.(int) < retryMax { // retry
|
|
|
|
r.group.DoChan(q.String(), fn)
|
|
|
|
}
|
2022-11-18 10:02:46 +00:00
|
|
|
|
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-08-24 13:36:19 +00:00
|
|
|
ctx, cancel := context.WithTimeout(ctx, resolver.DefaultDNSTimeout)
|
|
|
|
defer cancel()
|
2019-06-28 04:29:08 +00:00
|
|
|
|
2022-08-24 13:36:19 +00:00
|
|
|
return batchExchange(ctx, clients, m)
|
2019-06-28 04:29:08 +00:00
|
|
|
}
|
|
|
|
|
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)
|
2023-01-21 06:40:36 +00:00
|
|
|
if record != nil {
|
|
|
|
p := record.Data()
|
|
|
|
return p.GetData()
|
2021-05-19 03:17:35 +00:00
|
|
|
}
|
|
|
|
|
2023-01-21 06:40:36 +00:00
|
|
|
for _, geositeRecord := range r.geositePolicy {
|
|
|
|
matched := geositeRecord.matcher.Match(domain)
|
|
|
|
if matched != geositeRecord.inversedMatching {
|
|
|
|
return geositeRecord.policy.GetData()
|
|
|
|
}
|
|
|
|
}
|
2023-04-01 03:53:39 +00:00
|
|
|
metadata := &C.Metadata{Host: domain}
|
|
|
|
for _, domainSetRecord := range r.domainSetPolicy {
|
|
|
|
if ok := domainSetRecord.domainSetProvider.Match(metadata); ok {
|
|
|
|
return domainSetRecord.policy.GetData()
|
|
|
|
}
|
|
|
|
}
|
2023-01-21 06:40:36 +00:00
|
|
|
return nil
|
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-11-12 12:43:48 +00:00
|
|
|
func (r *Resolver) lookupIP(ctx context.Context, host string, dnsType uint16) (ips []netip.Addr, err error) {
|
2022-04-22 16:27:22 +00:00
|
|
|
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)
|
|
|
|
|
2022-11-12 05:18:36 +00:00
|
|
|
msg, err := r.ExchangeContext(ctx, query)
|
2019-06-28 04:29:08 +00:00
|
|
|
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
|
2022-07-21 06:03:49 +00:00
|
|
|
Params map[string]string
|
2022-11-12 03:14:51 +00:00
|
|
|
PreferH3 bool
|
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 {
|
2023-04-01 03:53:39 +00:00
|
|
|
Main, Fallback []NameServer
|
|
|
|
Default []NameServer
|
|
|
|
ProxyServer []NameServer
|
|
|
|
IPv6 bool
|
|
|
|
IPv6Timeout uint
|
|
|
|
EnhancedMode C.DNSMode
|
|
|
|
FallbackFilter FallbackFilter
|
|
|
|
Pool *fakeip.Pool
|
|
|
|
Hosts *trie.DomainTrie[resolver.HostValue]
|
|
|
|
Policy map[string][]NameServer
|
|
|
|
DomainSetPolicy map[provider.RuleProvider][]NameServer
|
|
|
|
GeositePolicy map[router.DomainMatcher][]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{
|
2023-03-10 15:38:16 +00:00
|
|
|
main: transform(config.Default, nil),
|
|
|
|
lruCache: cache.New(cache.WithSize[string, *D.Msg](4096), cache.WithStale[string, *D.Msg](true)),
|
|
|
|
ipv6Timeout: time.Duration(config.IPv6Timeout) * time.Millisecond,
|
2020-02-15 13:42:46 +00:00
|
|
|
}
|
|
|
|
|
2019-06-28 04:29:08 +00:00
|
|
|
r := &Resolver{
|
2023-03-10 15:38:16 +00:00
|
|
|
ipv6: config.IPv6,
|
|
|
|
main: transform(config.Main, defaultResolver),
|
|
|
|
lruCache: cache.New(cache.WithSize[string, *D.Msg](4096), cache.WithStale[string, *D.Msg](true)),
|
|
|
|
hosts: config.Hosts,
|
|
|
|
ipv6Timeout: time.Duration(config.IPv6Timeout) * time.Millisecond,
|
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 {
|
2022-07-21 06:03:49 +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 {
|
2022-07-21 06:03:49 +00:00
|
|
|
r.proxyServer = transform(config.ProxyServer, defaultResolver)
|
2022-03-27 16:44:13 +00:00
|
|
|
}
|
|
|
|
|
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 {
|
2023-01-27 16:19:58 +00:00
|
|
|
if strings.HasPrefix(strings.ToLower(domain), "geosite:") {
|
|
|
|
groupname := domain[8:]
|
2023-01-21 06:40:36 +00:00
|
|
|
inverse := false
|
|
|
|
if strings.HasPrefix(groupname, "!") {
|
|
|
|
inverse = true
|
|
|
|
groupname = groupname[1:]
|
|
|
|
}
|
2023-02-18 17:23:06 +00:00
|
|
|
log.Debugln("adding geosite policy: %s inversed %t", groupname, inverse)
|
2023-01-21 06:40:36 +00:00
|
|
|
matcher, err := NewGeoSite(groupname)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
r.geositePolicy = append(r.geositePolicy, geositePolicyRecord{
|
|
|
|
matcher: matcher,
|
2023-02-03 13:40:05 +00:00
|
|
|
policy: NewPolicy(transform(nameserver, defaultResolver)),
|
2023-01-21 06:40:36 +00:00
|
|
|
inversedMatching: inverse,
|
|
|
|
})
|
|
|
|
} else {
|
2023-02-03 13:40:05 +00:00
|
|
|
_ = r.policy.Insert(domain, NewPolicy(transform(nameserver, defaultResolver)))
|
2023-01-21 06:40:36 +00:00
|
|
|
}
|
2021-05-19 03:17:35 +00:00
|
|
|
}
|
2022-11-30 11:42:05 +00:00
|
|
|
r.policy.Optimize()
|
2021-05-19 03:17:35 +00:00
|
|
|
}
|
2023-04-01 03:53:39 +00:00
|
|
|
if len(config.DomainSetPolicy) > 0 {
|
|
|
|
for p, n := range config.DomainSetPolicy {
|
|
|
|
r.domainSetPolicy = append(r.domainSetPolicy, domainSetPolicyRecord{
|
|
|
|
domainSetProvider: p,
|
|
|
|
policy: NewPolicy(transform(n, 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{
|
2023-03-10 15:38:16 +00:00
|
|
|
ipv6: old.ipv6,
|
|
|
|
main: old.proxyServer,
|
|
|
|
lruCache: old.lruCache,
|
|
|
|
hosts: old.hosts,
|
2023-03-23 04:58:59 +00:00
|
|
|
policy: trie.New[*Policy](),
|
2023-03-10 15:38:16 +00:00
|
|
|
ipv6Timeout: old.ipv6Timeout,
|
2021-11-17 08:03:47 +00:00
|
|
|
}
|
2019-06-28 04:29:08 +00:00
|
|
|
return r
|
|
|
|
}
|