server.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. package dns
  2. //go:generate errorgen
  3. import (
  4. "context"
  5. "sync"
  6. "time"
  7. "v2ray.com/core/features/routing"
  8. "v2ray.com/core"
  9. "v2ray.com/core/common"
  10. "v2ray.com/core/common/net"
  11. "v2ray.com/core/common/strmatcher"
  12. "v2ray.com/core/features"
  13. "v2ray.com/core/features/dns"
  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. v := core.MustFromContext(ctx)
  39. addNameServer := func(endpoint *net.Endpoint) int {
  40. address := endpoint.Address.AsAddress()
  41. if address.Family().IsDomain() && address.Domain() == "localhost" {
  42. server.servers = append(server.servers, NewLocalNameServer())
  43. } else {
  44. dest := endpoint.AsDestination()
  45. if dest.Network == net.Network_Unknown {
  46. dest.Network = net.Network_UDP
  47. }
  48. if dest.Network == net.Network_UDP {
  49. idx := len(server.servers)
  50. server.servers = append(server.servers, nil)
  51. v.RequireFeatures([]interface{}{routing.DispatcherType()}, func(fs []features.Feature) {
  52. dispatcher := fs[0].(routing.Dispatcher)
  53. server.servers[idx] = NewClassicNameServer(dest, dispatcher, server.clientIP)
  54. })
  55. }
  56. }
  57. return len(server.servers) - 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 proritized 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(config.NameServers) == 0 {
  83. server.servers = append(server.servers, NewLocalNameServer())
  84. }
  85. return server, nil
  86. }
  87. func (*Server) Type() interface{} {
  88. return dns.ClientType()
  89. }
  90. // Start implements common.Runnable.
  91. func (s *Server) Start() error {
  92. return nil
  93. }
  94. // Close implements common.Closable.
  95. func (s *Server) Close() error {
  96. return nil
  97. }
  98. func (s *Server) queryIPTimeout(server NameServerInterface, domain string) ([]net.IP, error) {
  99. ctx, cancel := context.WithTimeout(context.Background(), time.Second*4)
  100. ips, err := server.QueryIP(ctx, domain)
  101. cancel()
  102. return ips, err
  103. }
  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. }