server.go 3.8 KB

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