server.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. type Server struct {
  16. sync.Mutex
  17. hosts *StaticHosts
  18. servers []NameServerInterface
  19. clientIP net.IP
  20. domainMatcher strmatcher.IndexMatcher
  21. domainIndexMap map[uint32]uint32
  22. }
  23. func New(ctx context.Context, config *Config) (*Server, error) {
  24. server := &Server{
  25. servers: make([]NameServerInterface, 0, len(config.NameServers)+len(config.NameServer)),
  26. }
  27. if len(config.ClientIp) > 0 {
  28. if len(config.ClientIp) != 4 && len(config.ClientIp) != 16 {
  29. return nil, newError("unexpected IP length", len(config.ClientIp))
  30. }
  31. server.clientIP = net.IP(config.ClientIp)
  32. }
  33. hosts, err := NewStaticHosts(config.StaticHosts, config.Hosts)
  34. if err != nil {
  35. return nil, newError("failed to create hosts").Base(err)
  36. }
  37. server.hosts = hosts
  38. addNameServer := func(endpoint *net.Endpoint) int {
  39. address := endpoint.Address.AsAddress()
  40. if address.Family().IsDomain() && address.Domain() == "localhost" {
  41. server.servers = append(server.servers, NewLocalNameServer())
  42. } else {
  43. dest := endpoint.AsDestination()
  44. if dest.Network == net.Network_Unknown {
  45. dest.Network = net.Network_UDP
  46. }
  47. if dest.Network == net.Network_UDP {
  48. idx := len(server.servers)
  49. server.servers = append(server.servers, nil)
  50. core.RequireFeatures(ctx, func(d routing.Dispatcher) {
  51. server.servers[idx] = NewClassicNameServer(dest, d, server.clientIP)
  52. })
  53. }
  54. }
  55. return len(server.servers) - 1
  56. }
  57. if len(config.NameServers) > 0 {
  58. features.PrintDeprecatedFeatureWarning("simple DNS server")
  59. }
  60. for _, destPB := range config.NameServers {
  61. addNameServer(destPB)
  62. }
  63. if len(config.NameServer) > 0 {
  64. domainMatcher := &strmatcher.MatcherGroup{}
  65. domainIndexMap := make(map[uint32]uint32)
  66. for _, ns := range config.NameServer {
  67. idx := addNameServer(ns.Address)
  68. for _, domain := range ns.PrioritizedDomain {
  69. matcher, err := toStrMatcher(domain.Type, domain.Domain)
  70. if err != nil {
  71. return nil, newError("failed to create proritized domain").Base(err).AtWarning()
  72. }
  73. midx := domainMatcher.Add(matcher)
  74. domainIndexMap[midx] = uint32(idx)
  75. }
  76. }
  77. server.domainMatcher = domainMatcher
  78. server.domainIndexMap = domainIndexMap
  79. }
  80. if len(config.NameServers) == 0 {
  81. server.servers = append(server.servers, NewLocalNameServer())
  82. }
  83. return server, nil
  84. }
  85. // Type implements common.HasType.
  86. func (*Server) Type() interface{} {
  87. return dns.ClientType()
  88. }
  89. // Start implements common.Runnable.
  90. func (s *Server) Start() error {
  91. return nil
  92. }
  93. // Close implements common.Closable.
  94. func (s *Server) Close() error {
  95. return nil
  96. }
  97. func (s *Server) queryIPTimeout(server NameServerInterface, domain string) ([]net.IP, error) {
  98. ctx, cancel := context.WithTimeout(context.Background(), time.Second*4)
  99. ips, err := server.QueryIP(ctx, domain)
  100. cancel()
  101. return ips, err
  102. }
  103. // LookupIP implements dns.Client.
  104. func (s *Server) LookupIP(domain string) ([]net.IP, error) {
  105. if ip := s.hosts.LookupIP(domain); len(ip) > 0 {
  106. return ip, nil
  107. }
  108. var lastErr error
  109. if s.domainMatcher != nil {
  110. idx := s.domainMatcher.Match(domain)
  111. if idx > 0 {
  112. ns := s.servers[s.domainIndexMap[idx]]
  113. ips, err := s.queryIPTimeout(ns, domain)
  114. if len(ips) > 0 {
  115. return ips, nil
  116. }
  117. if err != nil {
  118. lastErr = err
  119. }
  120. }
  121. }
  122. for _, server := range s.servers {
  123. ips, err := s.queryIPTimeout(server, domain)
  124. if len(ips) > 0 {
  125. return ips, nil
  126. }
  127. if err != nil {
  128. lastErr = err
  129. }
  130. }
  131. return nil, newError("returning nil for domain ", domain).Base(lastErr)
  132. }
  133. func init() {
  134. common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
  135. return New(ctx, config.(*Config))
  136. }))
  137. }