server.go 3.7 KB

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