2021-08-21 01:20:40 -04:00
|
|
|
//go:build !confonly
|
2020-12-18 04:24:33 -05:00
|
|
|
// +build !confonly
|
|
|
|
|
2018-04-19 15:33:18 -04:00
|
|
|
// Package dns is an implementation of core.DNS feature.
|
2015-12-06 05:00:10 -05:00
|
|
|
package dns
|
|
|
|
|
2021-02-16 15:31:50 -05:00
|
|
|
//go:generate go run github.com/v2fly/v2ray-core/v4/common/errors/errorgen
|
2020-12-18 04:24:33 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2021-09-07 03:00:17 -04:00
|
|
|
"github.com/v2fly/v2ray-core/v4/common/platform"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/infra/conf/cfgcommon"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/infra/conf/geodata"
|
2021-02-22 21:17:20 -05:00
|
|
|
"strings"
|
2020-12-18 04:24:33 -05:00
|
|
|
"sync"
|
|
|
|
|
2021-02-16 15:31:50 -05:00
|
|
|
"github.com/v2fly/v2ray-core/v4/app/router"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/common"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/common/errors"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/common/net"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/common/session"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/common/strmatcher"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/features"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/features/dns"
|
2020-12-18 04:24:33 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// DNS is a DNS rely server.
|
|
|
|
type DNS struct {
|
|
|
|
sync.Mutex
|
2021-09-12 22:35:34 -04:00
|
|
|
tag string
|
|
|
|
disableCache bool
|
|
|
|
disableFallback bool
|
|
|
|
disableFallbackIfMatch bool
|
|
|
|
ipOption *dns.IPOption
|
|
|
|
hosts *StaticHosts
|
|
|
|
clients []*Client
|
|
|
|
ctx context.Context
|
|
|
|
domainMatcher strmatcher.IndexMatcher
|
|
|
|
matcherInfos []DomainMatcherInfo
|
2020-12-18 04:24:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// DomainMatcherInfo contains information attached to index returned by Server.domainMatcher
|
|
|
|
type DomainMatcherInfo struct {
|
|
|
|
clientIdx uint16
|
|
|
|
domainRuleIdx uint16
|
|
|
|
}
|
|
|
|
|
|
|
|
// New creates a new DNS server with given configuration.
|
|
|
|
func New(ctx context.Context, config *Config) (*DNS, error) {
|
|
|
|
var tag string
|
|
|
|
if len(config.Tag) > 0 {
|
|
|
|
tag = config.Tag
|
|
|
|
} else {
|
|
|
|
tag = generateRandomTag()
|
|
|
|
}
|
|
|
|
|
|
|
|
var clientIP net.IP
|
|
|
|
switch len(config.ClientIp) {
|
|
|
|
case 0, net.IPv4len, net.IPv6len:
|
|
|
|
clientIP = net.IP(config.ClientIp)
|
|
|
|
default:
|
|
|
|
return nil, newError("unexpected client IP length ", len(config.ClientIp))
|
|
|
|
}
|
|
|
|
|
2021-03-19 03:55:18 -04:00
|
|
|
var ipOption *dns.IPOption
|
|
|
|
switch config.QueryStrategy {
|
|
|
|
case QueryStrategy_USE_IP:
|
|
|
|
ipOption = &dns.IPOption{
|
|
|
|
IPv4Enable: true,
|
|
|
|
IPv6Enable: true,
|
|
|
|
FakeEnable: false,
|
|
|
|
}
|
|
|
|
case QueryStrategy_USE_IP4:
|
|
|
|
ipOption = &dns.IPOption{
|
|
|
|
IPv4Enable: true,
|
|
|
|
IPv6Enable: false,
|
|
|
|
FakeEnable: false,
|
|
|
|
}
|
|
|
|
case QueryStrategy_USE_IP6:
|
|
|
|
ipOption = &dns.IPOption{
|
|
|
|
IPv4Enable: false,
|
|
|
|
IPv6Enable: true,
|
|
|
|
FakeEnable: false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-18 04:24:33 -05:00
|
|
|
hosts, err := NewStaticHosts(config.StaticHosts, config.Hosts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, newError("failed to create hosts").Base(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
clients := []*Client{}
|
|
|
|
domainRuleCount := 0
|
|
|
|
for _, ns := range config.NameServer {
|
|
|
|
domainRuleCount += len(ns.PrioritizedDomain)
|
|
|
|
}
|
2020-12-18 23:17:55 -05:00
|
|
|
|
2020-12-18 04:24:33 -05:00
|
|
|
// MatcherInfos is ensured to cover the maximum index domainMatcher could return, where matcher's index starts from 1
|
2021-09-04 06:53:05 -04:00
|
|
|
matcherInfos := make([]DomainMatcherInfo, domainRuleCount+1)
|
2020-12-18 04:24:33 -05:00
|
|
|
domainMatcher := &strmatcher.MatcherGroup{}
|
|
|
|
geoipContainer := router.GeoIPMatcherContainer{}
|
|
|
|
|
|
|
|
for _, endpoint := range config.NameServers {
|
|
|
|
features.PrintDeprecatedFeatureWarning("simple DNS server")
|
|
|
|
client, err := NewSimpleClient(ctx, endpoint, clientIP)
|
|
|
|
if err != nil {
|
|
|
|
return nil, newError("failed to create client").Base(err)
|
|
|
|
}
|
|
|
|
clients = append(clients, client)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, ns := range config.NameServer {
|
|
|
|
clientIdx := len(clients)
|
2021-09-04 06:53:05 -04:00
|
|
|
updateDomain := func(domainRule strmatcher.Matcher, originalRuleIdx int, matcherInfos []DomainMatcherInfo) error {
|
2020-12-18 04:24:33 -05:00
|
|
|
midx := domainMatcher.Add(domainRule)
|
2021-09-04 06:53:05 -04:00
|
|
|
matcherInfos[midx] = DomainMatcherInfo{
|
2020-12-18 04:24:33 -05:00
|
|
|
clientIdx: uint16(clientIdx),
|
|
|
|
domainRuleIdx: uint16(originalRuleIdx),
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
myClientIP := clientIP
|
|
|
|
switch len(ns.ClientIp) {
|
|
|
|
case net.IPv4len, net.IPv6len:
|
|
|
|
myClientIP = net.IP(ns.ClientIp)
|
|
|
|
}
|
2021-03-03 03:53:42 -05:00
|
|
|
client, err := NewClient(ctx, ns, myClientIP, geoipContainer, &matcherInfos, updateDomain)
|
2020-12-18 04:24:33 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, newError("failed to create client").Base(err)
|
|
|
|
}
|
|
|
|
clients = append(clients, client)
|
|
|
|
}
|
|
|
|
|
2021-02-22 21:17:20 -05:00
|
|
|
// If there is no DNS client in config, add a `localhost` DNS client
|
2020-12-18 04:24:33 -05:00
|
|
|
if len(clients) == 0 {
|
|
|
|
clients = append(clients, NewLocalDNSClient())
|
|
|
|
}
|
|
|
|
|
|
|
|
return &DNS{
|
2021-09-12 22:35:34 -04:00
|
|
|
tag: tag,
|
|
|
|
hosts: hosts,
|
|
|
|
ipOption: ipOption,
|
|
|
|
clients: clients,
|
|
|
|
ctx: ctx,
|
|
|
|
domainMatcher: domainMatcher,
|
|
|
|
matcherInfos: matcherInfos,
|
|
|
|
disableCache: config.DisableCache,
|
|
|
|
disableFallback: config.DisableFallback,
|
|
|
|
disableFallbackIfMatch: config.DisableFallbackIfMatch,
|
2020-12-18 04:24:33 -05:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Type implements common.HasType.
|
|
|
|
func (*DNS) Type() interface{} {
|
|
|
|
return dns.ClientType()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start implements common.Runnable.
|
|
|
|
func (s *DNS) Start() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close implements common.Closable.
|
|
|
|
func (s *DNS) Close() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsOwnLink implements proxy.dns.ownLinkVerifier
|
|
|
|
func (s *DNS) IsOwnLink(ctx context.Context) bool {
|
|
|
|
inbound := session.InboundFromContext(ctx)
|
|
|
|
return inbound != nil && inbound.Tag == s.tag
|
|
|
|
}
|
|
|
|
|
|
|
|
// LookupIP implements dns.Client.
|
2021-03-19 03:55:18 -04:00
|
|
|
func (s *DNS) LookupIP(domain string) ([]net.IP, error) {
|
2021-09-24 01:46:34 -04:00
|
|
|
return s.lookupIPInternal(domain, *s.ipOption)
|
2021-03-19 03:55:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// LookupIPv4 implements dns.IPv4Lookup.
|
|
|
|
func (s *DNS) LookupIPv4(domain string) ([]net.IP, error) {
|
2021-09-24 01:46:34 -04:00
|
|
|
if !s.ipOption.IPv4Enable {
|
|
|
|
return nil, dns.ErrEmptyResponse
|
|
|
|
}
|
|
|
|
o := *s.ipOption
|
|
|
|
o.IPv6Enable = false
|
|
|
|
return s.lookupIPInternal(domain, o)
|
2021-03-19 03:55:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// LookupIPv6 implements dns.IPv6Lookup.
|
|
|
|
func (s *DNS) LookupIPv6(domain string) ([]net.IP, error) {
|
2021-09-24 01:46:34 -04:00
|
|
|
if !s.ipOption.IPv6Enable {
|
|
|
|
return nil, dns.ErrEmptyResponse
|
|
|
|
}
|
|
|
|
o := *s.ipOption
|
|
|
|
o.IPv4Enable = false
|
|
|
|
return s.lookupIPInternal(domain, o)
|
2021-03-19 03:55:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DNS) lookupIPInternal(domain string, option dns.IPOption) ([]net.IP, error) {
|
2020-12-18 04:24:33 -05:00
|
|
|
if domain == "" {
|
|
|
|
return nil, newError("empty domain name")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Normalize the FQDN form query
|
2021-06-22 07:56:35 -04:00
|
|
|
domain = strings.TrimSuffix(domain, ".")
|
2020-12-18 04:24:33 -05:00
|
|
|
|
|
|
|
// Static host lookup
|
|
|
|
switch addrs := s.hosts.Lookup(domain, option); {
|
|
|
|
case addrs == nil: // Domain not recorded in static host
|
|
|
|
break
|
|
|
|
case len(addrs) == 0: // Domain recorded, but no valid IP returned (e.g. IPv4 address with only IPv6 enabled)
|
|
|
|
return nil, dns.ErrEmptyResponse
|
|
|
|
case len(addrs) == 1 && addrs[0].Family().IsDomain(): // Domain replacement
|
|
|
|
newError("domain replaced: ", domain, " -> ", addrs[0].Domain()).WriteToLog()
|
|
|
|
domain = addrs[0].Domain()
|
|
|
|
default: // Successfully found ip records in static host
|
2021-04-10 23:52:12 -04:00
|
|
|
newError("returning ", len(addrs), " IP(s) for domain ", domain, " -> ", addrs).WriteToLog()
|
2020-12-18 04:24:33 -05:00
|
|
|
return toNetIP(addrs)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Name servers lookup
|
|
|
|
errs := []error{}
|
2021-02-08 05:18:52 -05:00
|
|
|
ctx := session.ContextWithInbound(s.ctx, &session.Inbound{Tag: s.tag})
|
2020-12-18 04:24:33 -05:00
|
|
|
for _, client := range s.sortClients(domain) {
|
2021-02-22 21:17:20 -05:00
|
|
|
if !option.FakeEnable && strings.EqualFold(client.Name(), "FakeDNS") {
|
|
|
|
newError("skip DNS resolution for domain ", domain, " at server ", client.Name()).AtDebug().WriteToLog()
|
|
|
|
continue
|
|
|
|
}
|
2021-02-24 03:03:50 -05:00
|
|
|
ips, err := client.QueryIP(ctx, domain, option, s.disableCache)
|
2020-12-18 04:24:33 -05:00
|
|
|
if len(ips) > 0 {
|
|
|
|
return ips, nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
newError("failed to lookup ip for domain ", domain, " at server ", client.Name()).Base(err).WriteToLog()
|
|
|
|
errs = append(errs, err)
|
|
|
|
}
|
|
|
|
if err != context.Canceled && err != context.DeadlineExceeded && err != errExpectedIPNonMatch {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, newError("returning nil for domain ", domain).Base(errors.Combine(errs...))
|
|
|
|
}
|
|
|
|
|
2021-03-19 03:55:18 -04:00
|
|
|
// GetIPOption implements ClientWithIPOption.
|
|
|
|
func (s *DNS) GetIPOption() *dns.IPOption {
|
|
|
|
return s.ipOption
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetQueryOption implements ClientWithIPOption.
|
|
|
|
func (s *DNS) SetQueryOption(isIPv4Enable, isIPv6Enable bool) {
|
|
|
|
s.ipOption.IPv4Enable = isIPv4Enable
|
|
|
|
s.ipOption.IPv6Enable = isIPv6Enable
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetFakeDNSOption implements ClientWithIPOption.
|
|
|
|
func (s *DNS) SetFakeDNSOption(isFakeEnable bool) {
|
|
|
|
s.ipOption.FakeEnable = isFakeEnable
|
|
|
|
}
|
|
|
|
|
2020-12-18 04:24:33 -05:00
|
|
|
func (s *DNS) sortClients(domain string) []*Client {
|
|
|
|
clients := make([]*Client, 0, len(s.clients))
|
|
|
|
clientUsed := make([]bool, len(s.clients))
|
|
|
|
clientNames := make([]string, 0, len(s.clients))
|
|
|
|
domainRules := []string{}
|
|
|
|
|
|
|
|
// Priority domain matching
|
2021-09-12 22:35:34 -04:00
|
|
|
hasMatch := false
|
2020-12-18 04:24:33 -05:00
|
|
|
for _, match := range s.domainMatcher.Match(domain) {
|
|
|
|
info := s.matcherInfos[match]
|
|
|
|
client := s.clients[info.clientIdx]
|
|
|
|
domainRule := client.domains[info.domainRuleIdx]
|
|
|
|
domainRules = append(domainRules, fmt.Sprintf("%s(DNS idx:%d)", domainRule, info.clientIdx))
|
|
|
|
if clientUsed[info.clientIdx] {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
clientUsed[info.clientIdx] = true
|
|
|
|
clients = append(clients, client)
|
|
|
|
clientNames = append(clientNames, client.Name())
|
2021-09-12 22:35:34 -04:00
|
|
|
hasMatch = true
|
2020-12-18 04:24:33 -05:00
|
|
|
}
|
|
|
|
|
2021-09-12 22:35:34 -04:00
|
|
|
if !(s.disableFallback || s.disableFallbackIfMatch && hasMatch) {
|
2021-04-08 22:35:26 -04:00
|
|
|
// Default round-robin query
|
|
|
|
for idx, client := range s.clients {
|
|
|
|
if clientUsed[idx] || client.skipFallback {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
clientUsed[idx] = true
|
|
|
|
clients = append(clients, client)
|
|
|
|
clientNames = append(clientNames, client.Name())
|
2020-12-18 04:24:33 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(domainRules) > 0 {
|
|
|
|
newError("domain ", domain, " matches following rules: ", domainRules).AtDebug().WriteToLog()
|
|
|
|
}
|
|
|
|
if len(clientNames) > 0 {
|
|
|
|
newError("domain ", domain, " will use DNS in order: ", clientNames).AtDebug().WriteToLog()
|
|
|
|
}
|
2021-04-08 22:35:26 -04:00
|
|
|
|
|
|
|
if len(clients) == 0 {
|
|
|
|
clients = append(clients, s.clients[0])
|
|
|
|
clientNames = append(clientNames, s.clients[0].Name())
|
|
|
|
newError("domain ", domain, " will use the first DNS: ", clientNames).AtDebug().WriteToLog()
|
|
|
|
}
|
|
|
|
|
2020-12-18 04:24:33 -05:00
|
|
|
return clients
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
|
|
|
|
return New(ctx, config.(*Config))
|
|
|
|
}))
|
2021-09-07 03:00:17 -04:00
|
|
|
|
|
|
|
common.Must(common.RegisterConfig((*SimplifiedConfig)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
|
|
|
|
|
|
|
|
ctx = cfgcommon.NewConfigureLoadingContext(context.Background())
|
|
|
|
|
|
|
|
geoloadername := platform.NewEnvFlag("v2ray.conf.geoloader").GetValue(func() string {
|
|
|
|
return "standard"
|
|
|
|
})
|
|
|
|
|
|
|
|
if loader, err := geodata.GetGeoDataLoader(geoloadername); err == nil {
|
|
|
|
cfgcommon.SetGeoDataLoader(ctx, loader)
|
|
|
|
} else {
|
|
|
|
return nil, newError("unable to create geo data loader ").Base(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cfgEnv := cfgcommon.GetConfigureLoadingEnvironment(ctx)
|
|
|
|
geoLoader := cfgEnv.GetGeoLoader()
|
|
|
|
|
|
|
|
simplifiedConfig := config.(*SimplifiedConfig)
|
|
|
|
for _, v := range simplifiedConfig.NameServer {
|
|
|
|
for _, geo := range v.Geoip {
|
|
|
|
if geo.Code != "" {
|
|
|
|
filepath := "geoip.dat"
|
|
|
|
if geo.FilePath != "" {
|
|
|
|
filepath = geo.FilePath
|
2021-09-07 06:09:34 -04:00
|
|
|
} else {
|
|
|
|
geo.CountryCode = geo.Code
|
2021-09-07 03:00:17 -04:00
|
|
|
}
|
|
|
|
var err error
|
|
|
|
geo.Cidr, err = geoLoader.LoadIP(filepath, geo.Code)
|
|
|
|
if err != nil {
|
|
|
|
return nil, newError("unable to load geoip").Base(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-10 15:35:02 -04:00
|
|
|
var nameservers []*NameServer
|
|
|
|
|
|
|
|
for _, v := range simplifiedConfig.NameServer {
|
|
|
|
nameserver := &NameServer{
|
|
|
|
Address: v.Address,
|
|
|
|
ClientIp: net.ParseIP(v.ClientIp),
|
|
|
|
SkipFallback: v.SkipFallback,
|
|
|
|
Geoip: v.Geoip,
|
|
|
|
}
|
|
|
|
for _, prioritizedDomain := range v.PrioritizedDomain {
|
|
|
|
nameserver.PrioritizedDomain = append(nameserver.PrioritizedDomain, &NameServer_PriorityDomain{
|
|
|
|
Type: prioritizedDomain.Type,
|
|
|
|
Domain: prioritizedDomain.Domain,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
nameservers = append(nameservers, nameserver)
|
|
|
|
}
|
|
|
|
|
2021-09-07 03:00:17 -04:00
|
|
|
fullConfig := &Config{
|
2021-09-10 15:35:02 -04:00
|
|
|
NameServer: nameservers,
|
|
|
|
ClientIp: net.ParseIP(simplifiedConfig.ClientIp),
|
2021-09-07 03:00:17 -04:00
|
|
|
StaticHosts: simplifiedConfig.StaticHosts,
|
|
|
|
Tag: simplifiedConfig.Tag,
|
|
|
|
DisableCache: simplifiedConfig.DisableCache,
|
|
|
|
QueryStrategy: simplifiedConfig.QueryStrategy,
|
|
|
|
DisableFallback: simplifiedConfig.DisableFallback,
|
|
|
|
}
|
|
|
|
return common.CreateObject(ctx, fullConfig)
|
|
|
|
}))
|
2020-12-18 04:24:33 -05:00
|
|
|
}
|