nameserver.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. package dns
  2. import (
  3. "context"
  4. "net/url"
  5. "strings"
  6. "time"
  7. core "github.com/v2fly/v2ray-core/v5"
  8. "github.com/v2fly/v2ray-core/v5/app/router"
  9. "github.com/v2fly/v2ray-core/v5/common/errors"
  10. "github.com/v2fly/v2ray-core/v5/common/net"
  11. "github.com/v2fly/v2ray-core/v5/common/session"
  12. "github.com/v2fly/v2ray-core/v5/features"
  13. "github.com/v2fly/v2ray-core/v5/features/dns"
  14. "github.com/v2fly/v2ray-core/v5/features/routing"
  15. )
  16. // Server is the interface for Name Server.
  17. type Server interface {
  18. // Name of the Client.
  19. Name() string
  20. // QueryIP sends IP queries to its configured server.
  21. QueryIP(ctx context.Context, domain string, clientIP net.IP, option dns.IPOption, disableCache bool) ([]net.IP, error)
  22. }
  23. // Client is the interface for DNS client.
  24. type Client struct {
  25. server Server
  26. clientIP net.IP
  27. tag string
  28. queryStrategy dns.IPOption
  29. cacheStrategy CacheStrategy
  30. fallbackStrategy FallbackStrategy
  31. domains []string
  32. expectIPs []*router.GeoIPMatcher
  33. }
  34. var errExpectedIPNonMatch = errors.New("expectIPs not match")
  35. // NewServer creates a name server object according to the network destination url.
  36. func NewServer(dest net.Destination, dispatcher routing.Dispatcher) (Server, error) {
  37. if address := dest.Address; address.Family().IsDomain() {
  38. u, err := url.Parse(address.Domain())
  39. if err != nil {
  40. return nil, err
  41. }
  42. switch {
  43. case strings.EqualFold(u.String(), "localhost"):
  44. return NewLocalNameServer(), nil
  45. case strings.EqualFold(u.Scheme, "https"): // DOH Remote mode
  46. return NewDoHNameServer(u, dispatcher)
  47. case strings.EqualFold(u.Scheme, "https+local"): // DOH Local mode
  48. return NewDoHLocalNameServer(u), nil
  49. case strings.EqualFold(u.Scheme, "quic+local"): // DNS-over-QUIC Local mode
  50. return NewQUICNameServer(u)
  51. case strings.EqualFold(u.Scheme, "tcp"): // DNS-over-TCP Remote mode
  52. return NewTCPNameServer(u, dispatcher)
  53. case strings.EqualFold(u.Scheme, "tcp+local"): // DNS-over-TCP Local mode
  54. return NewTCPLocalNameServer(u)
  55. case strings.EqualFold(u.String(), "fakedns"):
  56. return NewFakeDNSServer(), nil
  57. }
  58. }
  59. if dest.Network == net.Network_Unknown {
  60. dest.Network = net.Network_UDP
  61. }
  62. if dest.Network == net.Network_UDP { // UDP classic DNS mode
  63. return NewClassicNameServer(dest, dispatcher), nil
  64. }
  65. return nil, newError("No available name server could be created from ", dest).AtWarning()
  66. }
  67. // NewClient creates a DNS client managing a name server with client IP, domain rules and expected IPs.
  68. func NewClient(ctx context.Context, ns *NameServer, dns *Config) (*Client, error) {
  69. client := &Client{}
  70. // Create DNS server instance
  71. err := core.RequireFeatures(ctx, func(dispatcher routing.Dispatcher) error {
  72. // Create a new server for each client for now
  73. server, err := NewServer(ns.Address.AsDestination(), dispatcher)
  74. if err != nil {
  75. return newError("failed to create nameserver").Base(err).AtWarning()
  76. }
  77. client.server = server
  78. return nil
  79. })
  80. if err != nil {
  81. return nil, err
  82. }
  83. // Initialize fields with default values
  84. if len(ns.Tag) == 0 {
  85. ns.Tag = dns.Tag
  86. if len(ns.Tag) == 0 {
  87. ns.Tag = generateRandomTag()
  88. }
  89. }
  90. if len(ns.ClientIp) == 0 {
  91. ns.ClientIp = dns.ClientIp
  92. }
  93. if ns.QueryStrategy == nil {
  94. ns.QueryStrategy = &dns.QueryStrategy
  95. }
  96. if ns.CacheStrategy == nil {
  97. ns.CacheStrategy = new(CacheStrategy)
  98. switch {
  99. case dns.CacheStrategy != CacheStrategy_CacheEnabled:
  100. *ns.CacheStrategy = dns.CacheStrategy
  101. case dns.DisableCache:
  102. features.PrintDeprecatedFeatureWarning("DNS disableCache settings")
  103. *ns.CacheStrategy = CacheStrategy_CacheDisabled
  104. }
  105. }
  106. if ns.FallbackStrategy == nil {
  107. ns.FallbackStrategy = new(FallbackStrategy)
  108. switch {
  109. case ns.SkipFallback:
  110. features.PrintDeprecatedFeatureWarning("DNS server skipFallback settings")
  111. *ns.FallbackStrategy = FallbackStrategy_Disabled
  112. case dns.FallbackStrategy != FallbackStrategy_Enabled:
  113. *ns.FallbackStrategy = dns.FallbackStrategy
  114. case dns.DisableFallback:
  115. features.PrintDeprecatedFeatureWarning("DNS disableFallback settings")
  116. *ns.FallbackStrategy = FallbackStrategy_Disabled
  117. case dns.DisableFallbackIfMatch:
  118. features.PrintDeprecatedFeatureWarning("DNS disableFallbackIfMatch settings")
  119. *ns.FallbackStrategy = FallbackStrategy_DisabledIfAnyMatch
  120. }
  121. }
  122. // Priotize local domains with specific TLDs or without any dot to local DNS
  123. if strings.EqualFold(ns.Address.Address.GetDomain(), "localhost") {
  124. ns.PrioritizedDomain = append(ns.PrioritizedDomain, localTLDsAndDotlessDomains...)
  125. ns.OriginalRules = append(ns.OriginalRules, localTLDsAndDotlessDomainsRule)
  126. }
  127. if len(ns.ClientIp) > 0 {
  128. newError("DNS: client ", ns.Address.Address.AsAddress(), " uses clientIP ", net.IP(ns.ClientIp).String()).AtInfo().WriteToLog()
  129. }
  130. client.clientIP = ns.ClientIp
  131. client.tag = ns.Tag
  132. client.queryStrategy = toIPOption(*ns.QueryStrategy)
  133. client.cacheStrategy = *ns.CacheStrategy
  134. client.fallbackStrategy = *ns.FallbackStrategy
  135. return client, nil
  136. }
  137. // Name returns the server name the client manages.
  138. func (c *Client) Name() string {
  139. return c.server.Name()
  140. }
  141. // QueryIP send DNS query to the name server with the client's IP.
  142. func (c *Client) QueryIP(ctx context.Context, domain string, option dns.IPOption) ([]net.IP, error) {
  143. queryOption := option.With(c.queryStrategy)
  144. if !queryOption.IsValid() {
  145. newError(c.server.Name(), " returns empty answer: ", domain, ". ", toReqTypes(option)).AtInfo().WriteToLog()
  146. return nil, dns.ErrEmptyResponse
  147. }
  148. disableCache := c.cacheStrategy == CacheStrategy_CacheDisabled
  149. ctx = session.ContextWithInbound(ctx, &session.Inbound{Tag: c.tag})
  150. ctx, cancel := context.WithTimeout(ctx, 4*time.Second)
  151. ips, err := c.server.QueryIP(ctx, domain, c.clientIP, queryOption, disableCache)
  152. cancel()
  153. if err != nil {
  154. return ips, err
  155. }
  156. return c.MatchExpectedIPs(domain, ips)
  157. }
  158. // MatchExpectedIPs matches queried domain IPs with expected IPs and returns matched ones.
  159. func (c *Client) MatchExpectedIPs(domain string, ips []net.IP) ([]net.IP, error) {
  160. if len(c.expectIPs) == 0 {
  161. return ips, nil
  162. }
  163. newIps := []net.IP{}
  164. for _, ip := range ips {
  165. for _, matcher := range c.expectIPs {
  166. if matcher.Match(ip) {
  167. newIps = append(newIps, ip)
  168. break
  169. }
  170. }
  171. }
  172. if len(newIps) == 0 {
  173. return nil, errExpectedIPNonMatch
  174. }
  175. newError("domain ", domain, " expectIPs ", newIps, " matched at server ", c.Name()).AtDebug().WriteToLog()
  176. return newIps, nil
  177. }