| 1 | // +build !confonly |
| 2 | |
| 3 | package dnsscip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/ |
| 4 | |
| 5 | import ( |
| 6 | "contextscip-go gomod github.com/golang/go/src go1.15 context/" |
| 7 | "net/urlscip-go gomod github.com/golang/go/src go1.15 `net/url`/" |
| 8 | "timescip-go gomod github.com/golang/go/src go1.15 time/" |
| 9 | |
| 10 | "v2ray.com/corescip-go gomod v2ray.com/core . `v2ray.com/core`/" |
| 11 | "v2ray.com/core/app/routerscip-go gomod v2ray.com/core . `v2ray.com/core/app/router`/" |
| 12 | "v2ray.com/core/common/errorsscip-go gomod v2ray.com/core . `v2ray.com/core/common/errors`/" |
| 13 | "v2ray.com/core/common/netscip-go gomod v2ray.com/core . `v2ray.com/core/common/net`/" |
| 14 | "v2ray.com/core/common/strmatcherscip-go gomod v2ray.com/core . `v2ray.com/core/common/strmatcher`/" |
| 15 | "v2ray.com/core/features/routingscip-go gomod v2ray.com/core . `v2ray.com/core/features/routing`/" |
| 16 | ) |
| 17 | |
| 18 | // IPOption is an object for IP query options. |
| 19 | type IPOptionscip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/IPOption# struct { |
| 20 | IPv4Enablescip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/IPOption#IPv4Enable. bool |
| 21 | IPv6Enablescip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/IPOption#IPv6Enable. bool |
| 22 | } |
| 23 | |
| 24 | // Server is the interface for Name Server. |
| 25 | type Serverscip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/Server# interface { |
| 26 | // Name of the Client. |
| 27 | Namescip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/Server#Name.() string |
| 28 | // QueryIP sends IP queries to its configured server. |
| 29 | QueryIPscip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/Server#QueryIP.(ctx contextscip-go gomod github.com/golang/go/src go1.15 context/.Contextscip-go gomod github.com/golang/go/src go1.15 context/Context#, domain string, clientIP netscip-go gomod v2ray.com/core . `v2ray.com/core/common/net`/.IPscip-go gomod v2ray.com/core . `v2ray.com/core/common/net`/IP#, option IPOptionscip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/IPOption#) ([]netscip-go gomod v2ray.com/core . `v2ray.com/core/common/net`/.IPscip-go gomod v2ray.com/core . `v2ray.com/core/common/net`/IP#, error) |
| 30 | } |
| 31 | |
| 32 | // Client is the interface for DNS client. |
| 33 | type Clientscip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/Client# struct { |
| 34 | serverscip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/Client#server. Serverscip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/Server# |
| 35 | clientIPscip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/Client#clientIP. netscip-go gomod v2ray.com/core . `v2ray.com/core/common/net`/.IPscip-go gomod v2ray.com/core . `v2ray.com/core/common/net`/IP# |
| 36 | domainsscip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/Client#domains. []string |
| 37 | expectIPsscip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/Client#expectIPs. []*routerscip-go gomod v2ray.com/core . `v2ray.com/core/app/router`/.GeoIPMatcherscip-go gomod v2ray.com/core . `v2ray.com/core/app/router`/GeoIPMatcher# |
| 38 | } |
| 39 | |
| 40 | var errExpectedIPNonMatchscip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/errExpectedIPNonMatch. = errorsscip-go gomod v2ray.com/core . `v2ray.com/core/common/errors`/.Newscip-go gomod v2ray.com/core . `v2ray.com/core/common/errors`/New().("expectIPs not match") |
| 41 | |
| 42 | // NewServer creates a name server object according to the network destination url. |
| 43 | func NewServerscip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/NewServer().(dest netscip-go gomod v2ray.com/core . `v2ray.com/core/common/net`/.Destinationscip-go gomod v2ray.com/core . `v2ray.com/core/common/net`/Destination#, dispatcher routingscip-go gomod v2ray.com/core . `v2ray.com/core/features/routing`/.Dispatcherscip-go gomod v2ray.com/core . `v2ray.com/core/features/routing`/Dispatcher#) (Serverscip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/Server#, error) { |
| 44 | if address := dest.Addressscip-go gomod v2ray.com/core . `v2ray.com/core/common/net`/Destination#Address.; address.Familyscip-go gomod v2ray.com/core . `v2ray.com/core/common/net`/Address#Family.().IsDomainscip-go gomod v2ray.com/core . `v2ray.com/core/common/net`/AddressFamily#IsDomain().() { |
| 45 | u, err := urlscip-go gomod github.com/golang/go/src go1.15 `net/url`/.Parsescip-go gomod github.com/golang/go/src go1.15 `net/url`/Parse().(address.Domainscip-go gomod v2ray.com/core . `v2ray.com/core/common/net`/Address#Domain.()) |
| 46 | if err != nil { |
| 47 | return nil, err |
| 48 | } |
| 49 | switch { |
| 50 | case u.Stringscip-go gomod github.com/golang/go/src go1.15 `net/url`/URL#String().() == "localhost": |
| 51 | return NewLocalNameServerscip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/NewLocalNameServer().(), nil |
| 52 | case u.Schemescip-go gomod github.com/golang/go/src go1.15 `net/url`/URL#Scheme. == "https": // DOH Remote mode |
| 53 | return NewDoHNameServerscip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/NewDoHNameServer().(u, dispatcher) |
| 54 | case u.Schemescip-go gomod github.com/golang/go/src go1.15 `net/url`/URL#Scheme. == "https+local": // DOH Local mode |
| 55 | return NewDoHLocalNameServerscip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/NewDoHLocalNameServer().(u), nil |
| 56 | case u.Schemescip-go gomod github.com/golang/go/src go1.15 `net/url`/URL#Scheme. == "quic+local": // DNS-over-QUIC Local mode |
| 57 | return NewQUICNameServerscip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/NewQUICNameServer().(u) |
| 58 | } |
| 59 | } |
| 60 | if dest.Networkscip-go gomod v2ray.com/core . `v2ray.com/core/common/net`/Destination#Network. == netscip-go gomod v2ray.com/core . `v2ray.com/core/common/net`/.Network_Unknownscip-go gomod v2ray.com/core . `v2ray.com/core/common/net`/Network_Unknown. { |
| 61 | dest.Networkscip-go gomod v2ray.com/core . `v2ray.com/core/common/net`/Destination#Network. = netscip-go gomod v2ray.com/core . `v2ray.com/core/common/net`/.Network_UDPscip-go gomod v2ray.com/core . `v2ray.com/core/common/net`/Network_UDP. |
| 62 | } |
| 63 | if dest.Networkscip-go gomod v2ray.com/core . `v2ray.com/core/common/net`/Destination#Network. == netscip-go gomod v2ray.com/core . `v2ray.com/core/common/net`/.Network_UDPscip-go gomod v2ray.com/core . `v2ray.com/core/common/net`/Network_UDP. { // UDP classic DNS mode |
| 64 | return NewClassicNameServerscip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/NewClassicNameServer().(dest, dispatcher), nil |
| 65 | } |
| 66 | return nil, newErrorscip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/newError().("No available name server could be created from ", dest).AtWarningscip-go gomod v2ray.com/core . `v2ray.com/core/common/errors`/Error#AtWarning().() |
| 67 | } |
| 68 | |
| 69 | // NewClient creates a DNS client managing a name server with client IP, domain rules and expected IPs. |
| 70 | func NewClientscip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/NewClient().(ctx contextscip-go gomod github.com/golang/go/src go1.15 context/.Contextscip-go gomod github.com/golang/go/src go1.15 context/Context#, ns *NameServerscip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/NameServer#, clientIP netscip-go gomod v2ray.com/core . `v2ray.com/core/common/net`/.IPscip-go gomod v2ray.com/core . `v2ray.com/core/common/net`/IP#, container routerscip-go gomod v2ray.com/core . `v2ray.com/core/app/router`/.GeoIPMatcherContainerscip-go gomod v2ray.com/core . `v2ray.com/core/app/router`/GeoIPMatcherContainer#, updateDomainRule func(strmatcherscip-go gomod v2ray.com/core . `v2ray.com/core/common/strmatcher`/.Matcherscip-go gomod v2ray.com/core . `v2ray.com/core/common/strmatcher`/Matcher#, int) error) (*Clientscip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/Client#, error) { |
| 71 | client := &Clientscip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/Client#{} |
| 72 | err := corescip-go gomod v2ray.com/core . `v2ray.com/core`/.RequireFeaturesscip-go gomod v2ray.com/core . `v2ray.com/core`/RequireFeatures().(ctx, func(dispatcher routingscip-go gomod v2ray.com/core . `v2ray.com/core/features/routing`/.Dispatcherscip-go gomod v2ray.com/core . `v2ray.com/core/features/routing`/Dispatcher#) error { |
| 73 | // Create a new server for each client for now |
| 74 | server, err := NewServerscip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/NewServer().(ns.Addressscip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/NameServer#Address..AsDestinationscip-go gomod v2ray.com/core . `v2ray.com/core/common/net`/Endpoint#AsDestination().(), dispatcher) |
| 75 | if err != nil { |
| 76 | return newErrorscip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/newError().("failed to create nameserver").Basescip-go gomod v2ray.com/core . `v2ray.com/core/common/errors`/Error#Base().(err).AtWarningscip-go gomod v2ray.com/core . `v2ray.com/core/common/errors`/Error#AtWarning().() |
| 77 | } |
| 78 | |
| 79 | // Priotize local domains with specific TLDs or without any dot to local DNS |
| 80 | if _, isLocalDNS := server.(*LocalNameServerscip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/LocalNameServer#); isLocalDNS { |
| 81 | ns.PrioritizedDomainscip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/NameServer#PrioritizedDomain. = append(ns.PrioritizedDomainscip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/NameServer#PrioritizedDomain., localTLDsAndDotlessDomainsscip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/localTLDsAndDotlessDomains....) |
| 82 | ns.OriginalRulesscip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/NameServer#OriginalRules. = append(ns.OriginalRulesscip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/NameServer#OriginalRules., localTLDsAndDotlessDomainsRulescip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/localTLDsAndDotlessDomainsRule.) |
| 83 | } |
| 84 | |
| 85 | // Establish domain rules |
| 86 | var rules []string |
| 87 | ruleCurr := 0 |
| 88 | ruleIter := 0 |
| 89 | for _, domain := range ns.PrioritizedDomainscip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/NameServer#PrioritizedDomain. { |
| 90 | domainRule, err := toStrMatcherscip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/toStrMatcher().(domain.Typescip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/NameServer_PriorityDomain#Type., domain.Domainscip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/NameServer_PriorityDomain#Domain.) |
| 91 | if err != nil { |
| 92 | return newErrorscip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/newError().("failed to create prioritized domain").Basescip-go gomod v2ray.com/core . `v2ray.com/core/common/errors`/Error#Base().(err).AtWarningscip-go gomod v2ray.com/core . `v2ray.com/core/common/errors`/Error#AtWarning().() |
| 93 | } |
| 94 | originalRuleIdx := ruleCurr |
| 95 | if ruleCurr < len(ns.OriginalRulesscip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/NameServer#OriginalRules.) { |
| 96 | rule := ns.OriginalRulesscip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/NameServer#OriginalRules.[ruleCurr] |
| 97 | if ruleCurr >= len(rules) { |
| 98 | rules = append(rules, rule.Rulescip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/NameServer_OriginalRule#Rule.) |
| 99 | } |
| 100 | ruleIter++ |
| 101 | if ruleIter >= int(rule.Sizescip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/NameServer_OriginalRule#Size.) { |
| 102 | ruleIter = 0 |
| 103 | ruleCurr++ |
| 104 | } |
| 105 | } else { // No original rule, generate one according to current domain matcher (majorly for compatibility with tests) |
| 106 | rules = append(rules, domainRule.Stringscip-go gomod v2ray.com/core . `v2ray.com/core/common/strmatcher`/Matcher#String.()) |
| 107 | ruleCurr++ |
| 108 | } |
| 109 | err = updateDomainRule(domainRule, originalRuleIdx) |
| 110 | if err != nil { |
| 111 | return newErrorscip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/newError().("failed to create prioritized domain").Basescip-go gomod v2ray.com/core . `v2ray.com/core/common/errors`/Error#Base().(err).AtWarningscip-go gomod v2ray.com/core . `v2ray.com/core/common/errors`/Error#AtWarning().() |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | // Establish expected IPs |
| 116 | var matchers []*routerscip-go gomod v2ray.com/core . `v2ray.com/core/app/router`/.GeoIPMatcherscip-go gomod v2ray.com/core . `v2ray.com/core/app/router`/GeoIPMatcher# |
| 117 | for _, geoip := range ns.Geoipscip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/NameServer#Geoip. { |
| 118 | matcher, err := container.Addscip-go gomod v2ray.com/core . `v2ray.com/core/app/router`/GeoIPMatcherContainer#Add().(geoip) |
| 119 | if err != nil { |
| 120 | return newErrorscip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/newError().("failed to create ip matcher").Basescip-go gomod v2ray.com/core . `v2ray.com/core/common/errors`/Error#Base().(err).AtWarningscip-go gomod v2ray.com/core . `v2ray.com/core/common/errors`/Error#AtWarning().() |
| 121 | } |
| 122 | matchers = append(matchers, matcher) |
| 123 | } |
| 124 | |
| 125 | if len(clientIP) > 0 { |
| 126 | switch ns.Addressscip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/NameServer#Address..Addressscip-go gomod v2ray.com/core . `v2ray.com/core/common/net`/Endpoint#Address..GetAddressscip-go gomod v2ray.com/core . `v2ray.com/core/common/net`/IPOrDomain#GetAddress().().(type) { |
| 127 | case *netscip-go gomod v2ray.com/core . `v2ray.com/core/common/net`/.IPOrDomain_Domainscip-go gomod v2ray.com/core . `v2ray.com/core/common/net`/IPOrDomain_Domain#: |
| 128 | newErrorscip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/newError().("DNS: client ", ns.Addressscip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/NameServer#Address..Addressscip-go gomod v2ray.com/core . `v2ray.com/core/common/net`/Endpoint#Address..GetDomainscip-go gomod v2ray.com/core . `v2ray.com/core/common/net`/IPOrDomain#GetDomain().(), " uses clientIP ", clientIP.Stringscip-go gomod github.com/golang/go/src go1.15 net/IP#String().()).AtInfoscip-go gomod v2ray.com/core . `v2ray.com/core/common/errors`/Error#AtInfo().().WriteToLogscip-go gomod v2ray.com/core . `v2ray.com/core/common/errors`/Error#WriteToLog().() |
| 129 | case *netscip-go gomod v2ray.com/core . `v2ray.com/core/common/net`/.IPOrDomain_Ipscip-go gomod v2ray.com/core . `v2ray.com/core/common/net`/IPOrDomain_Ip#: |
| 130 | newErrorscip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/newError().("DNS: client ", ns.Addressscip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/NameServer#Address..Addressscip-go gomod v2ray.com/core . `v2ray.com/core/common/net`/Endpoint#Address..GetIpscip-go gomod v2ray.com/core . `v2ray.com/core/common/net`/IPOrDomain#GetIp().(), " uses clientIP ", clientIP.Stringscip-go gomod github.com/golang/go/src go1.15 net/IP#String().()).AtInfoscip-go gomod v2ray.com/core . `v2ray.com/core/common/errors`/Error#AtInfo().().WriteToLogscip-go gomod v2ray.com/core . `v2ray.com/core/common/errors`/Error#WriteToLog().() |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | client.serverscip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/Client#server. = server |
| 135 | client.clientIPscip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/Client#clientIP. = clientIP |
| 136 | client.domainsscip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/Client#domains. = rules |
| 137 | client.expectIPsscip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/Client#expectIPs. = matchers |
| 138 | return nil |
| 139 | }) |
| 140 | return client, err |
| 141 | } |
| 142 | |
| 143 | // NewSimpleClient creates a DNS client with a simple destination. |
| 144 | func NewSimpleClientscip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/NewSimpleClient().(ctx contextscip-go gomod github.com/golang/go/src go1.15 context/.Contextscip-go gomod github.com/golang/go/src go1.15 context/Context#, endpoint *netscip-go gomod v2ray.com/core . `v2ray.com/core/common/net`/.Endpointscip-go gomod v2ray.com/core . `v2ray.com/core/common/net`/Endpoint#, clientIP netscip-go gomod v2ray.com/core . `v2ray.com/core/common/net`/.IPscip-go gomod v2ray.com/core . `v2ray.com/core/common/net`/IP#) (*Clientscip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/Client#, error) { |
| 145 | client := &Clientscip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/Client#{} |
| 146 | err := corescip-go gomod v2ray.com/core . `v2ray.com/core`/.RequireFeaturesscip-go gomod v2ray.com/core . `v2ray.com/core`/RequireFeatures().(ctx, func(dispatcher routingscip-go gomod v2ray.com/core . `v2ray.com/core/features/routing`/.Dispatcherscip-go gomod v2ray.com/core . `v2ray.com/core/features/routing`/Dispatcher#) error { |
| 147 | server, err := NewServerscip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/NewServer().(endpoint.AsDestinationscip-go gomod v2ray.com/core . `v2ray.com/core/common/net`/Endpoint#AsDestination().(), dispatcher) |
| 148 | if err != nil { |
| 149 | return newErrorscip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/newError().("failed to create nameserver").Basescip-go gomod v2ray.com/core . `v2ray.com/core/common/errors`/Error#Base().(err).AtWarningscip-go gomod v2ray.com/core . `v2ray.com/core/common/errors`/Error#AtWarning().() |
| 150 | } |
| 151 | client.serverscip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/Client#server. = server |
| 152 | client.clientIPscip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/Client#clientIP. = clientIP |
| 153 | return nil |
| 154 | }) |
| 155 | |
| 156 | if len(clientIP) > 0 { |
| 157 | switch endpoint.Addressscip-go gomod v2ray.com/core . `v2ray.com/core/common/net`/Endpoint#Address..GetAddressscip-go gomod v2ray.com/core . `v2ray.com/core/common/net`/IPOrDomain#GetAddress().().(type) { |
| 158 | case *netscip-go gomod v2ray.com/core . `v2ray.com/core/common/net`/.IPOrDomain_Domainscip-go gomod v2ray.com/core . `v2ray.com/core/common/net`/IPOrDomain_Domain#: |
| 159 | newErrorscip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/newError().("DNS: client ", endpoint.Addressscip-go gomod v2ray.com/core . `v2ray.com/core/common/net`/Endpoint#Address..GetDomainscip-go gomod v2ray.com/core . `v2ray.com/core/common/net`/IPOrDomain#GetDomain().(), " uses clientIP ", clientIP.Stringscip-go gomod github.com/golang/go/src go1.15 net/IP#String().()).AtInfoscip-go gomod v2ray.com/core . `v2ray.com/core/common/errors`/Error#AtInfo().().WriteToLogscip-go gomod v2ray.com/core . `v2ray.com/core/common/errors`/Error#WriteToLog().() |
| 160 | case *netscip-go gomod v2ray.com/core . `v2ray.com/core/common/net`/.IPOrDomain_Ipscip-go gomod v2ray.com/core . `v2ray.com/core/common/net`/IPOrDomain_Ip#: |
| 161 | newErrorscip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/newError().("DNS: client ", endpoint.Addressscip-go gomod v2ray.com/core . `v2ray.com/core/common/net`/Endpoint#Address..GetIpscip-go gomod v2ray.com/core . `v2ray.com/core/common/net`/IPOrDomain#GetIp().(), " uses clientIP ", clientIP.Stringscip-go gomod github.com/golang/go/src go1.15 net/IP#String().()).AtInfoscip-go gomod v2ray.com/core . `v2ray.com/core/common/errors`/Error#AtInfo().().WriteToLogscip-go gomod v2ray.com/core . `v2ray.com/core/common/errors`/Error#WriteToLog().() |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | return client, err |
| 166 | } |
| 167 | |
| 168 | // Name returns the server name the client manages. |
| 169 | func (c *Clientscip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/Client#) Namescip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/Client#Name().() string { |
| 170 | return c.serverscip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/Client#server..Namescip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/Server#Name.() |
| 171 | } |
| 172 | |
| 173 | // QueryIP send DNS query to the name server with the client's IP. |
| 174 | func (c *Clientscip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/Client#) QueryIPscip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/Client#QueryIP().(ctx contextscip-go gomod github.com/golang/go/src go1.15 context/.Contextscip-go gomod github.com/golang/go/src go1.15 context/Context#, domain string, option IPOptionscip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/IPOption#) ([]netscip-go gomod v2ray.com/core . `v2ray.com/core/common/net`/.IPscip-go gomod v2ray.com/core . `v2ray.com/core/common/net`/IP#, error) { |
| 175 | ctx, cancel := contextscip-go gomod github.com/golang/go/src go1.15 context/.WithTimeoutscip-go gomod github.com/golang/go/src go1.15 context/WithTimeout().(ctx, 4*timescip-go gomod github.com/golang/go/src go1.15 time/.Secondscip-go gomod github.com/golang/go/src go1.15 time/Second.) |
| 176 | ips, err := c.serverscip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/Client#server..QueryIPscip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/Server#QueryIP.(ctx, domain, c.clientIPscip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/Client#clientIP., option) |
| 177 | cancel() |
| 178 | |
| 179 | if err != nil { |
| 180 | return ips, err |
| 181 | } |
| 182 | return c.MatchExpectedIPsscip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/Client#MatchExpectedIPs().(domain, ips) |
| 183 | } |
| 184 | |
| 185 | // MatchExpectedIPs matches queried domain IPs with expected IPs and returns matched ones. |
| 186 | func (c *Clientscip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/Client#) MatchExpectedIPsscip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/Client#MatchExpectedIPs().(domain string, ips []netscip-go gomod v2ray.com/core . `v2ray.com/core/common/net`/.IPscip-go gomod v2ray.com/core . `v2ray.com/core/common/net`/IP#) ([]netscip-go gomod v2ray.com/core . `v2ray.com/core/common/net`/.IPscip-go gomod v2ray.com/core . `v2ray.com/core/common/net`/IP#, error) { |
| 187 | if len(c.expectIPsscip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/Client#expectIPs.) == 0 { |
| 188 | return ips, nil |
| 189 | } |
| 190 | newIps := []netscip-go gomod v2ray.com/core . `v2ray.com/core/common/net`/.IPscip-go gomod v2ray.com/core . `v2ray.com/core/common/net`/IP#{} |
| 191 | for _, ip := range ips { |
| 192 | for _, matcher := range c.expectIPsscip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/Client#expectIPs. { |
| 193 | if matcher.Matchscip-go gomod v2ray.com/core . `v2ray.com/core/app/router`/GeoIPMatcher#Match().(ip) { |
| 194 | newIps = append(newIps, ip) |
| 195 | break |
| 196 | } |
| 197 | } |
| 198 | } |
| 199 | if len(newIps) == 0 { |
| 200 | return nil, errExpectedIPNonMatchscip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/errExpectedIPNonMatch. |
| 201 | } |
| 202 | newErrorscip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/newError().("domain ", domain, " expectIPs ", newIps, " matched at server ", c.Namescip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/Client#Name().()).AtDebugscip-go gomod v2ray.com/core . `v2ray.com/core/common/errors`/Error#AtDebug().().WriteToLogscip-go gomod v2ray.com/core . `v2ray.com/core/common/errors`/Error#WriteToLog().() |
| 203 | return newIps, nil |
| 204 | } |