| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398 |
- //go:build !confonly
- // +build !confonly
- // Package dns is an implementation of core.DNS feature.
- package dns
- //go:generate go run github.com/v2fly/v2ray-core/v5/common/errors/errorgen
- import (
- "context"
- "fmt"
- "strings"
- "sync"
- "github.com/v2fly/v2ray-core/v5/app/router"
- "github.com/v2fly/v2ray-core/v5/common"
- "github.com/v2fly/v2ray-core/v5/common/errors"
- "github.com/v2fly/v2ray-core/v5/common/net"
- "github.com/v2fly/v2ray-core/v5/common/platform"
- "github.com/v2fly/v2ray-core/v5/common/session"
- "github.com/v2fly/v2ray-core/v5/common/strmatcher"
- "github.com/v2fly/v2ray-core/v5/features"
- "github.com/v2fly/v2ray-core/v5/features/dns"
- "github.com/v2fly/v2ray-core/v5/infra/conf/cfgcommon"
- "github.com/v2fly/v2ray-core/v5/infra/conf/geodata"
- )
- // DNS is a DNS rely server.
- type DNS struct {
- sync.Mutex
- ipOption dns.IPOption
- hosts *StaticHosts
- clients []*Client
- ctx context.Context
- clientTags map[string]bool
- domainMatcher strmatcher.IndexMatcher
- matcherInfos []DomainMatcherInfo
- }
- // 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) {
- // Create static hosts
- hosts, err := NewStaticHosts(config.StaticHosts, config.Hosts)
- if err != nil {
- return nil, newError("failed to create hosts").Base(err)
- }
- // Create name servers from legacy configs
- clients := []*Client{}
- for _, endpoint := range config.NameServers {
- features.PrintDeprecatedFeatureWarning("simple DNS server")
- client, err := NewClient(ctx, &NameServer{Address: endpoint}, config)
- if err != nil {
- return nil, newError("failed to create client").Base(err)
- }
- clients = append(clients, client)
- }
- // Create name servers
- nsClientMap := map[int]int{}
- for nsIdx, ns := range config.NameServer {
- client, err := NewClient(ctx, ns, config)
- if err != nil {
- return nil, newError("failed to create client").Base(err)
- }
- nsClientMap[nsIdx] = len(clients)
- clients = append(clients, client)
- }
- // If there is no DNS client in config, add a `localhost` DNS client
- if len(clients) == 0 {
- clients = append(clients, NewLocalDNSClient())
- }
- // Establish members related to global DNS state
- domainMatcher, matcherInfos, err := establishDomainRules(config, clients, nsClientMap)
- if err != nil {
- return nil, err
- }
- if err := establishExpectedIPs(config, clients, nsClientMap); err != nil {
- return nil, err
- }
- clientTags := make(map[string]bool)
- for _, client := range clients {
- clientTags[client.tag] = true
- }
- return &DNS{
- ipOption: toIPOption(config.QueryStrategy),
- hosts: hosts,
- clients: clients,
- ctx: ctx,
- clientTags: clientTags,
- domainMatcher: domainMatcher,
- matcherInfos: matcherInfos,
- }, nil
- }
- func establishDomainRules(config *Config, clients []*Client, nsClientMap map[int]int) (strmatcher.IndexMatcher, []DomainMatcherInfo, error) {
- domainRuleCount := 0
- for _, ns := range config.NameServer {
- domainRuleCount += len(ns.PrioritizedDomain)
- }
- // MatcherInfos is ensured to cover the maximum index domainMatcher could return, where matcher's index starts from 1
- matcherInfos := make([]DomainMatcherInfo, domainRuleCount+1)
- domainMatcher := &strmatcher.LinearIndexMatcher{}
- for nsIdx, ns := range config.NameServer {
- clientIdx := nsClientMap[nsIdx]
- var rules []string
- ruleCurr := 0
- ruleIter := 0
- for _, domain := range ns.PrioritizedDomain {
- domainRule, err := toStrMatcher(domain.Type, domain.Domain)
- if err != nil {
- return nil, nil, newError("failed to create prioritized domain").Base(err).AtWarning()
- }
- originalRuleIdx := ruleCurr
- if ruleCurr < len(ns.OriginalRules) {
- rule := ns.OriginalRules[ruleCurr]
- if ruleCurr >= len(rules) {
- rules = append(rules, rule.Rule)
- }
- ruleIter++
- if ruleIter >= int(rule.Size) {
- ruleIter = 0
- ruleCurr++
- }
- } else { // No original rule, generate one according to current domain matcher (majorly for compatibility with tests)
- rules = append(rules, domainRule.String())
- ruleCurr++
- }
- midx := domainMatcher.Add(domainRule)
- matcherInfos[midx] = DomainMatcherInfo{
- clientIdx: uint16(clientIdx),
- domainRuleIdx: uint16(originalRuleIdx),
- }
- if err != nil {
- return nil, nil, newError("failed to create prioritized domain").Base(err).AtWarning()
- }
- }
- clients[clientIdx].domains = rules
- }
- return domainMatcher, matcherInfos, nil
- }
- func establishExpectedIPs(config *Config, clients []*Client, nsClientMap map[int]int) error {
- geoipContainer := router.GeoIPMatcherContainer{}
- for nsIdx, ns := range config.NameServer {
- clientIdx := nsClientMap[nsIdx]
- var matchers []*router.GeoIPMatcher
- for _, geoip := range ns.Geoip {
- matcher, err := geoipContainer.Add(geoip)
- if err != nil {
- return newError("failed to create ip matcher").Base(err).AtWarning()
- }
- matchers = append(matchers, matcher)
- }
- clients[clientIdx].expectIPs = matchers
- }
- return 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 && s.clientTags[inbound.Tag]
- }
- // LookupIP implements dns.Client.
- func (s *DNS) LookupIP(domain string) ([]net.IP, error) {
- return s.lookupIPInternal(domain, s.ipOption)
- }
- // LookupIPv4 implements dns.IPv4Lookup.
- func (s *DNS) LookupIPv4(domain string) ([]net.IP, error) {
- if option := s.ipOption.With(dns.IPOption{IPv4Enable: true}); option.IsValid() {
- return s.lookupIPInternal(domain, option)
- }
- return nil, dns.ErrEmptyResponse
- }
- // LookupIPv6 implements dns.IPv6Lookup.
- func (s *DNS) LookupIPv6(domain string) ([]net.IP, error) {
- if option := s.ipOption.With(dns.IPOption{IPv6Enable: true}); option.IsValid() {
- return s.lookupIPInternal(domain, option)
- }
- return nil, dns.ErrEmptyResponse
- }
- func (s *DNS) lookupIPInternal(domain string, option dns.IPOption) ([]net.IP, error) {
- if domain == "" {
- return nil, newError("empty domain name")
- }
- // Normalize the FQDN form query
- domain = strings.TrimSuffix(domain, ".")
- // 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
- newError("returning ", len(addrs), " IP(s) for domain ", domain, " -> ", addrs).WriteToLog()
- return toNetIP(addrs)
- }
- // Name servers lookup
- errs := []error{}
- for _, client := range s.sortClients(domain, option) {
- ips, err := client.QueryIP(s.ctx, domain, option)
- if len(ips) > 0 {
- return ips, nil
- }
- if err != nil {
- errs = append(errs, err)
- }
- if err != dns.ErrEmptyResponse { // ErrEmptyResponse is not seen as failure, so no failed log
- newError("failed to lookup ip for domain ", domain, " at server ", client.Name()).Base(err).WriteToLog()
- }
- if err != context.Canceled && err != context.DeadlineExceeded && err != errExpectedIPNonMatch {
- return nil, err // Continues lookup for certain errors
- }
- }
- return nil, newError("returning nil for domain ", domain).Base(errors.Combine(errs...))
- }
- // 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
- }
- func (s *DNS) sortClients(domain string, option dns.IPOption) []*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
- 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))
- switch {
- case clientUsed[info.clientIdx]:
- continue
- case !option.FakeEnable && strings.EqualFold(client.Name(), "FakeDNS"):
- continue
- }
- clientUsed[info.clientIdx] = true
- clients = append(clients, client)
- clientNames = append(clientNames, client.Name())
- }
- // Default round-robin query
- hasDomainMatch := len(clients) > 0
- for idx, client := range s.clients {
- switch {
- case clientUsed[idx]:
- continue
- case !option.FakeEnable && strings.EqualFold(client.Name(), "FakeDNS"):
- continue
- case client.fallbackStrategy == FallbackStrategy_Disabled:
- continue
- case client.fallbackStrategy == FallbackStrategy_DisabledIfAnyMatch && hasDomainMatch:
- continue
- }
- clientUsed[idx] = true
- clients = append(clients, client)
- clientNames = append(clientNames, client.Name())
- }
- 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, " ", toReqTypes(option)).AtDebug().WriteToLog()
- }
- return clients
- }
- func init() {
- common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
- return New(ctx, config.(*Config))
- }))
- common.Must(common.RegisterConfig((*SimplifiedConfig)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
- ctx = cfgcommon.NewConfigureLoadingContext(ctx)
- 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
- } else {
- geo.CountryCode = geo.Code
- }
- var err error
- geo.Cidr, err = geoLoader.LoadIP(filepath, geo.Code)
- if err != nil {
- return nil, newError("unable to load geoip").Base(err)
- }
- }
- }
- }
- var nameservers []*NameServer
- for _, v := range simplifiedConfig.NameServer {
- nameserver := &NameServer{
- Address: v.Address,
- ClientIp: net.ParseIP(v.ClientIp),
- Tag: v.Tag,
- QueryStrategy: v.QueryStrategy,
- CacheStrategy: v.CacheStrategy,
- FallbackStrategy: v.FallbackStrategy,
- 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)
- }
- fullConfig := &Config{
- StaticHosts: simplifiedConfig.StaticHosts,
- NameServer: nameservers,
- ClientIp: net.ParseIP(simplifiedConfig.ClientIp),
- Tag: simplifiedConfig.Tag,
- QueryStrategy: simplifiedConfig.QueryStrategy,
- CacheStrategy: simplifiedConfig.CacheStrategy,
- FallbackStrategy: simplifiedConfig.FallbackStrategy,
- // Deprecated flags
- DisableCache: simplifiedConfig.DisableCache,
- DisableFallback: simplifiedConfig.DisableFallback,
- DisableFallbackIfMatch: simplifiedConfig.DisableFallbackIfMatch,
- }
- return common.CreateObject(ctx, fullConfig)
- }))
- }
|