1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-06-11 18:30:43 +00:00
v2fly/app/dns/server.go

163 lines
3.9 KiB
Go
Raw Normal View History

2017-12-19 22:55:09 +00:00
package dns
2016-01-31 16:01:28 +00:00
2018-09-30 21:08:41 +00:00
//go:generate errorgen
2017-04-08 23:43:25 +00:00
2016-01-31 16:01:28 +00:00
import (
2017-01-13 12:41:40 +00:00
"context"
2016-05-16 06:09:28 +00:00
"sync"
2016-01-31 16:01:28 +00:00
"time"
"v2ray.com/core"
2017-01-06 14:32:36 +00:00
"v2ray.com/core/common"
"v2ray.com/core/common/net"
"v2ray.com/core/common/strmatcher"
2018-10-13 13:15:49 +00:00
"v2ray.com/core/features"
2018-10-11 21:09:15 +00:00
"v2ray.com/core/features/dns"
2018-10-22 09:26:22 +00:00
"v2ray.com/core/features/routing"
2016-01-31 16:01:28 +00:00
)
2017-12-28 22:19:41 +00:00
type Server struct {
2017-11-14 23:36:14 +00:00
sync.Mutex
hosts *StaticHosts
servers []NameServerInterface
clientIP net.IP
domainMatcher strmatcher.IndexMatcher
domainIndexMap map[uint32]uint32
2016-01-31 16:01:28 +00:00
}
2017-12-28 22:19:41 +00:00
func New(ctx context.Context, config *Config) (*Server, error) {
server := &Server{
servers: make([]NameServerInterface, 0, len(config.NameServers)+len(config.NameServer)),
2016-05-16 06:09:28 +00:00
}
2018-06-26 21:23:59 +00: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 15:14:51 +00:00
}
hosts, err := NewStaticHosts(config.StaticHosts, config.Hosts)
if err != nil {
return nil, newError("failed to create hosts").Base(err)
}
server.hosts = hosts
addNameServer := func(endpoint *net.Endpoint) int {
address := endpoint.Address.AsAddress()
if address.Family().IsDomain() && address.Domain() == "localhost" {
server.servers = append(server.servers, NewLocalNameServer())
} else {
dest := endpoint.AsDestination()
if dest.Network == net.Network_Unknown {
dest.Network = net.Network_UDP
}
if dest.Network == net.Network_UDP {
2018-10-21 08:27:13 +00:00
idx := len(server.servers)
server.servers = append(server.servers, nil)
2018-10-22 09:26:22 +00:00
core.RequireFeatures(ctx, func(d routing.Dispatcher) {
server.servers[idx] = NewClassicNameServer(dest, d, server.clientIP)
2018-10-21 08:27:13 +00:00
})
2016-05-18 15:12:04 +00:00
}
}
return len(server.servers) - 1
}
2018-09-21 14:54:06 +00:00
if len(config.NameServers) > 0 {
2018-10-13 13:15:49 +00:00
features.PrintDeprecatedFeatureWarning("simple DNS server")
2018-09-21 14:54:06 +00: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)
}
}
server.domainMatcher = domainMatcher
server.domainIndexMap = domainIndexMap
}
if len(config.NameServers) == 0 {
2018-06-27 09:23:39 +00:00
server.servers = append(server.servers, NewLocalNameServer())
}
2017-01-13 12:41:40 +00:00
return server, nil
}
2018-10-22 09:26:22 +00:00
// Type implements common.HasType.
2018-10-12 21:57:56 +00:00
func (*Server) Type() interface{} {
return dns.ClientType()
}
2018-02-08 16:00:22 +00:00
// Start implements common.Runnable.
2017-12-28 22:19:41 +00:00
func (s *Server) Start() error {
2018-06-26 13:35:22 +00:00
return nil
2017-02-01 20:35:40 +00:00
}
2018-04-03 09:11:54 +00:00
// Close implements common.Closable.
2018-02-08 16:00:22 +00:00
func (s *Server) Close() error {
2018-06-26 13:35:22 +00:00
return nil
2017-12-28 22:19:41 +00:00
}
2017-02-01 20:35:40 +00: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
}
2018-10-22 09:26:22 +00:00
// LookupIP implements dns.Client.
2017-12-28 22:19:41 +00:00
func (s *Server) LookupIP(domain string) ([]net.IP, error) {
if ip := s.hosts.LookupIP(domain); len(ip) > 0 {
return ip, nil
2016-05-22 20:30:08 +00:00
}
2018-06-26 13:04:47 +00:00
var lastErr error
if s.domainMatcher != nil {
idx := s.domainMatcher.Match(domain)
if idx > 0 {
2018-10-19 04:45:10 +00:00
ns := s.servers[s.domainIndexMap[idx]]
ips, err := s.queryIPTimeout(ns, domain)
if len(ips) > 0 {
return ips, nil
}
if err != nil {
lastErr = err
}
2018-06-26 13:04:47 +00:00
}
}
for _, server := range s.servers {
ips, err := s.queryIPTimeout(server, domain)
2018-06-26 13:04:47 +00:00
if len(ips) > 0 {
return ips, nil
2016-05-16 06:09:28 +00:00
}
if err != nil {
lastErr = err
}
2016-01-31 16:01:28 +00:00
}
2016-05-16 06:09:28 +00:00
2018-06-26 13:04:47 +00:00
return nil, newError("returning nil for domain ", domain).Base(lastErr)
2016-01-31 16:01:28 +00:00
}
2016-10-16 14:04:30 +00:00
func init() {
2017-12-19 22:55:09 +00:00
common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
2017-12-28 22:19:41 +00:00
return New(ctx, config.(*Config))
2017-01-13 12:41:40 +00:00
}))
2016-10-16 14:04:30 +00:00
}