diff --git a/app/dns/nameserver.go b/app/dns/nameserver.go index 277963d03..abbfb4085 100644 --- a/app/dns/nameserver.go +++ b/app/dns/nameserver.go @@ -7,12 +7,13 @@ import ( "v2ray.com/core/features/dns/localdns" ) +// IPOption is an object for IP query options. type IPOption struct { IPv4Enable bool IPv6Enable bool } -type NameServerInterface interface { +type Client interface { Name() string QueryIP(ctx context.Context, domain string, option IPOption) ([]net.IP, error) } diff --git a/app/dns/server.go b/app/dns/server.go index d7b9acd88..e25315ed0 100644 --- a/app/dns/server.go +++ b/app/dns/server.go @@ -20,7 +20,7 @@ import ( type Server struct { sync.Mutex hosts *StaticHosts - servers []NameServerInterface + clients []Client clientIP net.IP domainMatcher strmatcher.IndexMatcher domainIndexMap map[uint32]uint32 @@ -29,7 +29,7 @@ type Server struct { // New creates a new DNS server with given configuration. func New(ctx context.Context, config *Config) (*Server, error) { server := &Server{ - servers: make([]NameServerInterface, 0, len(config.NameServers)+len(config.NameServer)), + clients: make([]Client, 0, len(config.NameServers)+len(config.NameServer)), } if len(config.ClientIp) > 0 { if len(config.ClientIp) != 4 && len(config.ClientIp) != 16 { @@ -47,22 +47,22 @@ func New(ctx context.Context, config *Config) (*Server, error) { addNameServer := func(endpoint *net.Endpoint) int { address := endpoint.Address.AsAddress() if address.Family().IsDomain() && address.Domain() == "localhost" { - server.servers = append(server.servers, NewLocalNameServer()) + server.clients = append(server.clients, NewLocalNameServer()) } else { dest := endpoint.AsDestination() if dest.Network == net.Network_Unknown { dest.Network = net.Network_UDP } if dest.Network == net.Network_UDP { - idx := len(server.servers) - server.servers = append(server.servers, nil) + idx := len(server.clients) + server.clients = append(server.clients, nil) common.Must(core.RequireFeatures(ctx, func(d routing.Dispatcher) { - server.servers[idx] = NewClassicNameServer(dest, d, server.clientIP) + server.clients[idx] = NewClassicNameServer(dest, d, server.clientIP) })) } } - return len(server.servers) - 1 + return len(server.clients) - 1 } if len(config.NameServers) > 0 { @@ -94,8 +94,8 @@ func New(ctx context.Context, config *Config) (*Server, error) { server.domainIndexMap = domainIndexMap } - if len(server.servers) == 0 { - server.servers = append(server.servers, NewLocalNameServer()) + if len(server.clients) == 0 { + server.clients = append(server.clients, NewLocalNameServer()) } return server, nil @@ -116,9 +116,9 @@ func (s *Server) Close() error { return nil } -func (s *Server) queryIPTimeout(server NameServerInterface, domain string, option IPOption) ([]net.IP, error) { +func (s *Server) queryIPTimeout(client Client, domain string, option IPOption) ([]net.IP, error) { ctx, cancel := context.WithTimeout(context.Background(), time.Second*4) - ips, err := server.QueryIP(ctx, domain, option) + ips, err := client.QueryIP(ctx, domain, option) cancel() return ips, err } @@ -156,7 +156,7 @@ func (s *Server) lookupIPInternal(domain string, option IPOption) ([]net.IP, err if s.domainMatcher != nil { idx := s.domainMatcher.Match(domain) if idx > 0 { - ns := s.servers[s.domainIndexMap[idx]] + ns := s.clients[s.domainIndexMap[idx]] ips, err := s.queryIPTimeout(ns, domain, option) if len(ips) > 0 { return ips, nil @@ -168,13 +168,13 @@ func (s *Server) lookupIPInternal(domain string, option IPOption) ([]net.IP, err } } - for _, server := range s.servers { - ips, err := s.queryIPTimeout(server, domain, option) + for _, client := range s.clients { + ips, err := s.queryIPTimeout(client, domain, option) if len(ips) > 0 { return ips, nil } if err != nil { - newError("failed to lookup ip for domain ", domain, " at server ", server.Name()).Base(err).WriteToLog() + newError("failed to lookup ip for domain ", domain, " at server ", client.Name()).Base(err).WriteToLog() lastErr = err } }