mirror of
https://github.com/v2fly/v2ray-core.git
synced 2025-01-17 23:06:30 -05:00
rename NameServerInterface to Client
This commit is contained in:
parent
fc1e660c27
commit
daa8c9c5da
@ -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)
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user