2017-12-19 17:55:09 -05:00
|
|
|
package dns
|
2016-01-31 11:01:28 -05:00
|
|
|
|
2018-01-10 06:22:37 -05:00
|
|
|
//go:generate go run $GOPATH/src/v2ray.com/core/common/errors/errorgen/main.go -pkg dns -path App,DNS
|
2017-04-08 19:43:25 -04:00
|
|
|
|
2016-01-31 11:01:28 -05:00
|
|
|
import (
|
2017-01-13 07:41:40 -05:00
|
|
|
"context"
|
2016-05-16 02:09:28 -04:00
|
|
|
"sync"
|
2016-01-31 11:01:28 -05:00
|
|
|
"time"
|
|
|
|
|
2018-01-10 06:22:37 -05:00
|
|
|
"v2ray.com/core"
|
2017-01-06 09:32:36 -05:00
|
|
|
"v2ray.com/core/common"
|
2017-08-29 06:56:57 -04:00
|
|
|
"v2ray.com/core/common/net"
|
2018-08-24 15:51:03 -04:00
|
|
|
"v2ray.com/core/common/strmatcher"
|
2016-01-31 11:01:28 -05:00
|
|
|
)
|
|
|
|
|
2017-12-28 17:19:41 -05:00
|
|
|
type Server struct {
|
2017-11-14 18:36:14 -05:00
|
|
|
sync.Mutex
|
2018-08-24 15:51:03 -04:00
|
|
|
hosts *StaticHosts
|
|
|
|
servers []NameServerInterface
|
|
|
|
clientIP net.IP
|
|
|
|
domainMatcher strmatcher.IndexMatcher
|
|
|
|
domainIndexMap map[uint32]uint32
|
2016-01-31 11:01:28 -05:00
|
|
|
}
|
|
|
|
|
2017-12-28 17:19:41 -05:00
|
|
|
func New(ctx context.Context, config *Config) (*Server, error) {
|
|
|
|
server := &Server{
|
2018-08-24 15:51:03 -04:00
|
|
|
servers: make([]NameServerInterface, 0, len(config.NameServers)+len(config.NameServer)),
|
2016-05-16 02:09:28 -04:00
|
|
|
}
|
2018-06-26 17:23:59 -04:00
|
|
|
if len(config.ClientIp) > 0 {
|
|
|
|
if len(config.ClientIp) != 4 && len(config.ClientIp) != 16 {
|
|
|
|
return nil, newError("unexpected IP length", len(config.ClientIp))
|
|
|
|
}
|
|
|
|
server.clientIP = net.IP(config.ClientIp)
|
2018-06-26 11:14:51 -04:00
|
|
|
}
|
|
|
|
|
2018-06-26 16:34:05 -04:00
|
|
|
hosts, err := NewStaticHosts(config.StaticHosts, config.Hosts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, newError("failed to create hosts").Base(err)
|
|
|
|
}
|
|
|
|
server.hosts = hosts
|
|
|
|
|
2018-02-21 11:05:29 -05:00
|
|
|
v := core.MustFromContext(ctx)
|
2018-01-10 06:22:37 -05:00
|
|
|
if err := v.RegisterFeature((*core.DNSClient)(nil), server); err != nil {
|
|
|
|
return nil, newError("unable to register DNSClient.").Base(err)
|
|
|
|
}
|
|
|
|
|
2018-08-24 15:51:03 -04:00
|
|
|
addNameServer := func(endpoint *net.Endpoint) int {
|
|
|
|
address := endpoint.Address.AsAddress()
|
2018-01-10 06:22:37 -05:00
|
|
|
if address.Family().IsDomain() && address.Domain() == "localhost" {
|
2018-08-24 15:51:03 -04:00
|
|
|
server.servers = append(server.servers, NewLocalNameServer())
|
2018-01-10 06:22:37 -05:00
|
|
|
} else {
|
2018-08-24 15:51:03 -04:00
|
|
|
dest := endpoint.AsDestination()
|
2018-01-10 06:22:37 -05:00
|
|
|
if dest.Network == net.Network_Unknown {
|
|
|
|
dest.Network = net.Network_UDP
|
|
|
|
}
|
|
|
|
if dest.Network == net.Network_UDP {
|
2018-08-24 15:51:03 -04:00
|
|
|
server.servers = append(server.servers, NewClassicNameServer(dest, v.Dispatcher(), server.clientIP))
|
2016-05-18 11:12:04 -04:00
|
|
|
}
|
|
|
|
}
|
2018-08-24 15:51:03 -04:00
|
|
|
return len(server.servers) - 1
|
2018-01-10 06:22:37 -05:00
|
|
|
}
|
2018-08-24 15:51:03 -04:00
|
|
|
|
|
|
|
for _, destPB := range config.NameServers {
|
|
|
|
addNameServer(destPB)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(config.NameServer) > 0 {
|
|
|
|
domainMatcher := &strmatcher.MatcherGroup{}
|
|
|
|
domainIndexMap := make(map[uint32]uint32)
|
|
|
|
|
|
|
|
for _, ns := range config.NameServer {
|
|
|
|
idx := addNameServer(ns.Address)
|
|
|
|
|
|
|
|
for _, domain := range ns.PrioritizedDomain {
|
|
|
|
matcher, err := toStrMatcher(domain.Type, domain.Domain)
|
|
|
|
if err != nil {
|
|
|
|
return nil, newError("failed to create proritized domain").Base(err).AtWarning()
|
|
|
|
}
|
|
|
|
midx := domainMatcher.Add(matcher)
|
|
|
|
domainIndexMap[midx] = uint32(idx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-29 17:00:01 -04:00
|
|
|
server.domainMatcher = domainMatcher
|
2018-08-24 15:51:03 -04:00
|
|
|
server.domainIndexMap = domainIndexMap
|
|
|
|
}
|
|
|
|
|
2018-01-10 06:22:37 -05:00
|
|
|
if len(config.NameServers) == 0 {
|
2018-06-27 05:23:39 -04:00
|
|
|
server.servers = append(server.servers, NewLocalNameServer())
|
2018-01-10 06:22:37 -05:00
|
|
|
}
|
|
|
|
|
2017-01-13 07:41:40 -05:00
|
|
|
return server, nil
|
|
|
|
}
|
|
|
|
|
2018-02-08 11:00:22 -05:00
|
|
|
// Start implements common.Runnable.
|
2017-12-28 17:19:41 -05:00
|
|
|
func (s *Server) Start() error {
|
2018-06-26 09:35:22 -04:00
|
|
|
return nil
|
2017-02-01 15:35:40 -05:00
|
|
|
}
|
|
|
|
|
2018-04-03 05:11:54 -04:00
|
|
|
// Close implements common.Closable.
|
2018-02-08 11:00:22 -05:00
|
|
|
func (s *Server) Close() error {
|
2018-06-26 09:35:22 -04:00
|
|
|
return nil
|
2017-12-28 17:19:41 -05:00
|
|
|
}
|
2017-02-01 15:35:40 -05:00
|
|
|
|
2018-08-24 15:51:03 -04:00
|
|
|
func (s *Server) queryIPTimeout(server NameServerInterface, domain string) ([]net.IP, error) {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second*4)
|
|
|
|
ips, err := server.QueryIP(ctx, domain)
|
|
|
|
cancel()
|
|
|
|
return ips, err
|
|
|
|
}
|
|
|
|
|
2017-12-28 17:19:41 -05:00
|
|
|
func (s *Server) LookupIP(domain string) ([]net.IP, error) {
|
2018-06-26 16:34:05 -04:00
|
|
|
if ip := s.hosts.LookupIP(domain); len(ip) > 0 {
|
|
|
|
return ip, nil
|
2016-05-22 16:30:08 -04:00
|
|
|
}
|
|
|
|
|
2018-06-26 09:04:47 -04:00
|
|
|
var lastErr error
|
2018-08-24 15:51:03 -04:00
|
|
|
if s.domainMatcher != nil {
|
|
|
|
idx := s.domainMatcher.Match(domain)
|
|
|
|
if idx > 0 {
|
|
|
|
ns := s.servers[idx]
|
|
|
|
ips, err := s.queryIPTimeout(ns, domain)
|
|
|
|
if len(ips) > 0 {
|
|
|
|
return ips, nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
lastErr = err
|
|
|
|
}
|
2018-06-26 09:04:47 -04:00
|
|
|
}
|
2018-08-24 15:51:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, server := range s.servers {
|
|
|
|
ips, err := s.queryIPTimeout(server, domain)
|
2018-06-26 09:04:47 -04:00
|
|
|
if len(ips) > 0 {
|
|
|
|
return ips, nil
|
2016-05-16 02:09:28 -04:00
|
|
|
}
|
2018-08-24 15:51:03 -04:00
|
|
|
if err != nil {
|
|
|
|
lastErr = err
|
|
|
|
}
|
2016-01-31 11:01:28 -05:00
|
|
|
}
|
2016-05-16 02:09:28 -04:00
|
|
|
|
2018-06-26 09:04:47 -04:00
|
|
|
return nil, newError("returning nil for domain ", domain).Base(lastErr)
|
2016-01-31 11:01:28 -05:00
|
|
|
}
|
2016-10-16 10:04:30 -04:00
|
|
|
|
|
|
|
func init() {
|
2017-12-19 17:55:09 -05:00
|
|
|
common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
|
2017-12-28 17:19:41 -05:00
|
|
|
return New(ctx, config.(*Config))
|
2017-01-13 07:41:40 -05:00
|
|
|
}))
|
2016-10-16 10:04:30 -04:00
|
|
|
}
|