|
@@ -20,7 +20,7 @@ import (
|
|
|
type Server struct {
|
|
type Server struct {
|
|
|
sync.Mutex
|
|
sync.Mutex
|
|
|
hosts *StaticHosts
|
|
hosts *StaticHosts
|
|
|
- servers []NameServerInterface
|
|
|
|
|
|
|
+ clients []Client
|
|
|
clientIP net.IP
|
|
clientIP net.IP
|
|
|
domainMatcher strmatcher.IndexMatcher
|
|
domainMatcher strmatcher.IndexMatcher
|
|
|
domainIndexMap map[uint32]uint32
|
|
domainIndexMap map[uint32]uint32
|
|
@@ -29,7 +29,7 @@ type Server struct {
|
|
|
// New creates a new DNS server with given configuration.
|
|
// New creates a new DNS server with given configuration.
|
|
|
func New(ctx context.Context, config *Config) (*Server, error) {
|
|
func New(ctx context.Context, config *Config) (*Server, error) {
|
|
|
server := &Server{
|
|
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) > 0 {
|
|
|
if len(config.ClientIp) != 4 && len(config.ClientIp) != 16 {
|
|
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 {
|
|
addNameServer := func(endpoint *net.Endpoint) int {
|
|
|
address := endpoint.Address.AsAddress()
|
|
address := endpoint.Address.AsAddress()
|
|
|
if address.Family().IsDomain() && address.Domain() == "localhost" {
|
|
if address.Family().IsDomain() && address.Domain() == "localhost" {
|
|
|
- server.servers = append(server.servers, NewLocalNameServer())
|
|
|
|
|
|
|
+ server.clients = append(server.clients, NewLocalNameServer())
|
|
|
} else {
|
|
} else {
|
|
|
dest := endpoint.AsDestination()
|
|
dest := endpoint.AsDestination()
|
|
|
if dest.Network == net.Network_Unknown {
|
|
if dest.Network == net.Network_Unknown {
|
|
|
dest.Network = net.Network_UDP
|
|
dest.Network = net.Network_UDP
|
|
|
}
|
|
}
|
|
|
if 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) {
|
|
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 {
|
|
if len(config.NameServers) > 0 {
|
|
@@ -94,8 +94,8 @@ func New(ctx context.Context, config *Config) (*Server, error) {
|
|
|
server.domainIndexMap = domainIndexMap
|
|
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
|
|
return server, nil
|
|
@@ -116,9 +116,9 @@ func (s *Server) Close() error {
|
|
|
return nil
|
|
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)
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second*4)
|
|
|
- ips, err := server.QueryIP(ctx, domain, option)
|
|
|
|
|
|
|
+ ips, err := client.QueryIP(ctx, domain, option)
|
|
|
cancel()
|
|
cancel()
|
|
|
return ips, err
|
|
return ips, err
|
|
|
}
|
|
}
|
|
@@ -156,7 +156,7 @@ func (s *Server) lookupIPInternal(domain string, option IPOption) ([]net.IP, err
|
|
|
if s.domainMatcher != nil {
|
|
if s.domainMatcher != nil {
|
|
|
idx := s.domainMatcher.Match(domain)
|
|
idx := s.domainMatcher.Match(domain)
|
|
|
if idx > 0 {
|
|
if idx > 0 {
|
|
|
- ns := s.servers[s.domainIndexMap[idx]]
|
|
|
|
|
|
|
+ ns := s.clients[s.domainIndexMap[idx]]
|
|
|
ips, err := s.queryIPTimeout(ns, domain, option)
|
|
ips, err := s.queryIPTimeout(ns, domain, option)
|
|
|
if len(ips) > 0 {
|
|
if len(ips) > 0 {
|
|
|
return ips, nil
|
|
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 {
|
|
if len(ips) > 0 {
|
|
|
return ips, nil
|
|
return ips, nil
|
|
|
}
|
|
}
|
|
|
if err != 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
|
|
lastErr = err
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|