server.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. package dns
  2. //go:generate errorgen
  3. import (
  4. "context"
  5. "sync"
  6. "time"
  7. "v2ray.com/core"
  8. "v2ray.com/core/common"
  9. "v2ray.com/core/common/net"
  10. "v2ray.com/core/common/strmatcher"
  11. "v2ray.com/core/features"
  12. "v2ray.com/core/features/dns"
  13. "v2ray.com/core/features/routing"
  14. )
  15. // Server is a DNS rely server.
  16. type Server struct {
  17. sync.Mutex
  18. hosts *StaticHosts
  19. clients []Client
  20. clientIP net.IP
  21. domainMatcher strmatcher.IndexMatcher
  22. domainIndexMap map[uint32]uint32
  23. }
  24. // New creates a new DNS server with given configuration.
  25. func New(ctx context.Context, config *Config) (*Server, error) {
  26. server := &Server{
  27. clients: make([]Client, 0, len(config.NameServers)+len(config.NameServer)),
  28. }
  29. if len(config.ClientIp) > 0 {
  30. if len(config.ClientIp) != 4 && len(config.ClientIp) != 16 {
  31. return nil, newError("unexpected IP length", len(config.ClientIp))
  32. }
  33. server.clientIP = net.IP(config.ClientIp)
  34. }
  35. hosts, err := NewStaticHosts(config.StaticHosts, config.Hosts)
  36. if err != nil {
  37. return nil, newError("failed to create hosts").Base(err)
  38. }
  39. server.hosts = hosts
  40. addNameServer := func(endpoint *net.Endpoint) int {
  41. address := endpoint.Address.AsAddress()
  42. if address.Family().IsDomain() && address.Domain() == "localhost" {
  43. server.clients = append(server.clients, NewLocalNameServer())
  44. } else {
  45. dest := endpoint.AsDestination()
  46. if dest.Network == net.Network_Unknown {
  47. dest.Network = net.Network_UDP
  48. }
  49. if dest.Network == net.Network_UDP {
  50. idx := len(server.clients)
  51. server.clients = append(server.clients, nil)
  52. common.Must(core.RequireFeatures(ctx, func(d routing.Dispatcher) {
  53. server.clients[idx] = NewClassicNameServer(dest, d, server.clientIP)
  54. }))
  55. }
  56. }
  57. return len(server.clients) - 1
  58. }
  59. if len(config.NameServers) > 0 {
  60. features.PrintDeprecatedFeatureWarning("simple DNS server")
  61. }
  62. for _, destPB := range config.NameServers {
  63. addNameServer(destPB)
  64. }
  65. if len(config.NameServer) > 0 {
  66. domainMatcher := &strmatcher.MatcherGroup{}
  67. domainIndexMap := make(map[uint32]uint32)
  68. for _, ns := range config.NameServer {
  69. idx := addNameServer(ns.Address)
  70. for _, domain := range ns.PrioritizedDomain {
  71. matcher, err := toStrMatcher(domain.Type, domain.Domain)
  72. if err != nil {
  73. return nil, newError("failed to create prioritized domain").Base(err).AtWarning()
  74. }
  75. midx := domainMatcher.Add(matcher)
  76. domainIndexMap[midx] = uint32(idx)
  77. }
  78. }
  79. server.domainMatcher = domainMatcher
  80. server.domainIndexMap = domainIndexMap
  81. }
  82. if len(server.clients) == 0 {
  83. server.clients = append(server.clients, NewLocalNameServer())
  84. }
  85. return server, nil
  86. }
  87. // Type implements common.HasType.
  88. func (*Server) Type() interface{} {
  89. return dns.ClientType()
  90. }
  91. // Start implements common.Runnable.
  92. func (s *Server) Start() error {
  93. return nil
  94. }
  95. // Close implements common.Closable.
  96. func (s *Server) Close() error {
  97. return nil
  98. }
  99. func (s *Server) queryIPTimeout(client Client, domain string, option IPOption) ([]net.IP, error) {
  100. ctx, cancel := context.WithTimeout(context.Background(), time.Second*4)
  101. ips, err := client.QueryIP(ctx, domain, option)
  102. cancel()
  103. return ips, err
  104. }
  105. // LookupIP implements dns.Client.
  106. func (s *Server) LookupIP(domain string) ([]net.IP, error) {
  107. return s.lookupIPInternal(domain, IPOption{
  108. IPv4Enable: true,
  109. IPv6Enable: true,
  110. })
  111. }
  112. // LookupIPv4 implements dns.IPv4Lookup.
  113. func (s *Server) LookupIPv4(domain string) ([]net.IP, error) {
  114. return s.lookupIPInternal(domain, IPOption{
  115. IPv4Enable: true,
  116. IPv6Enable: false,
  117. })
  118. }
  119. // LookupIPv6 implements dns.IPv6Lookup.
  120. func (s *Server) LookupIPv6(domain string) ([]net.IP, error) {
  121. return s.lookupIPInternal(domain, IPOption{
  122. IPv4Enable: false,
  123. IPv6Enable: true,
  124. })
  125. }
  126. func (s *Server) lookupIPInternal(domain string, option IPOption) ([]net.IP, error) {
  127. if ip := s.hosts.LookupIP(domain, option); len(ip) > 0 {
  128. return ip, nil
  129. }
  130. var lastErr error
  131. if s.domainMatcher != nil {
  132. idx := s.domainMatcher.Match(domain)
  133. if idx > 0 {
  134. ns := s.clients[s.domainIndexMap[idx]]
  135. ips, err := s.queryIPTimeout(ns, domain, option)
  136. if len(ips) > 0 {
  137. return ips, nil
  138. }
  139. if err != nil {
  140. newError("failed to lookup ip for domain ", domain, " at server ", ns.Name()).Base(err).WriteToLog()
  141. lastErr = err
  142. }
  143. }
  144. }
  145. for _, client := range s.clients {
  146. ips, err := s.queryIPTimeout(client, domain, option)
  147. if len(ips) > 0 {
  148. return ips, nil
  149. }
  150. if err != nil {
  151. newError("failed to lookup ip for domain ", domain, " at server ", client.Name()).Base(err).WriteToLog()
  152. lastErr = err
  153. }
  154. }
  155. return nil, newError("returning nil for domain ", domain).Base(lastErr)
  156. }
  157. func init() {
  158. common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
  159. return New(ctx, config.(*Config))
  160. }))
  161. }