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

261 lines
6.4 KiB
Go
Raw Normal View History

2019-02-01 19:08:21 +00:00
// +build !confonly
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"
2019-01-16 19:32:41 +00:00
"v2ray.com/core/common/session"
"v2ray.com/core/common/strmatcher"
2019-02-06 09:21:04 +00:00
"v2ray.com/core/common/uuid"
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
)
2018-11-13 22:19:58 +00:00
// Server is a DNS rely server.
2017-12-28 22:19:41 +00:00
type Server struct {
2017-11-14 23:36:14 +00:00
sync.Mutex
hosts *StaticHosts
2018-12-28 19:28:31 +00:00
clients []Client
clientIP net.IP
domainMatcher strmatcher.IndexMatcher
domainIndexMap map[uint32]uint32
2019-01-16 19:32:41 +00:00
tag string
2016-01-31 16:01:28 +00:00
}
2019-02-06 09:21:04 +00:00
func generateRandomTag() string {
id := uuid.New()
return "v2ray.system." + id.String()
}
2018-11-13 22:19:58 +00:00
// New creates a new DNS server with given configuration.
2017-12-28 22:19:41 +00:00
func New(ctx context.Context, config *Config) (*Server, error) {
server := &Server{
2018-12-28 19:28:31 +00:00
clients: make([]Client, 0, len(config.NameServers)+len(config.NameServer)),
2019-01-16 19:32:41 +00:00
tag: config.Tag,
2016-05-16 06:09:28 +00:00
}
if server.tag == "" {
2019-02-06 09:21:04 +00:00
server.tag = generateRandomTag()
}
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" {
2018-12-28 19:28:31 +00:00
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 {
2018-12-28 19:28:31 +00:00
idx := len(server.clients)
server.clients = append(server.clients, nil)
2018-10-22 09:26:22 +00:00
2018-11-13 22:19:58 +00:00
common.Must(core.RequireFeatures(ctx, func(d routing.Dispatcher) {
2018-12-28 19:28:31 +00:00
server.clients[idx] = NewClassicNameServer(dest, d, server.clientIP)
2018-11-13 22:19:58 +00:00
}))
2016-05-18 15:12:04 +00:00
}
}
2018-12-28 19:28:31 +00:00
return len(server.clients) - 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 {
2018-10-29 11:24:17 +00:00
return nil, newError("failed to create prioritized domain").Base(err).AtWarning()
}
midx := domainMatcher.Add(matcher)
domainIndexMap[midx] = uint32(idx)
}
}
server.domainMatcher = domainMatcher
server.domainIndexMap = domainIndexMap
}
2018-12-28 19:28:31 +00:00
if len(server.clients) == 0 {
server.clients = append(server.clients, 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
2019-02-06 09:21:04 +00:00
func (s *Server) IsOwnLink(ctx context.Context) bool {
inbound := session.InboundFromContext(ctx)
return inbound != nil && inbound.Tag == s.tag
}
2018-12-28 19:28:31 +00:00
func (s *Server) queryIPTimeout(client Client, domain string, option IPOption) ([]net.IP, error) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*4)
2019-01-16 19:32:41 +00:00
if len(s.tag) > 0 {
ctx = session.ContextWithInbound(ctx, &session.Inbound{
Tag: s.tag,
})
}
2018-12-28 19:28:31 +00:00
ips, err := client.QueryIP(ctx, domain, option)
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) {
return s.lookupIPInternal(domain, IPOption{
IPv4Enable: true,
IPv6Enable: true,
})
}
// LookupIPv4 implements dns.IPv4Lookup.
func (s *Server) LookupIPv4(domain string) ([]net.IP, error) {
return s.lookupIPInternal(domain, IPOption{
IPv4Enable: true,
IPv6Enable: false,
})
}
// LookupIPv6 implements dns.IPv6Lookup.
func (s *Server) LookupIPv6(domain string) ([]net.IP, error) {
return s.lookupIPInternal(domain, IPOption{
IPv4Enable: false,
IPv6Enable: true,
})
}
func (s *Server) lookupStatic(domain string, option IPOption, depth int32) []net.Address {
ips := s.hosts.LookupIP(domain, option)
if ips == nil {
return nil
}
if ips[0].Family().IsDomain() && depth < 5 {
if newIPs := s.lookupStatic(ips[0].Domain(), option, depth+1); newIPs != nil {
return newIPs
}
}
return ips
}
func toNetIP(ips []net.Address) []net.IP {
if len(ips) == 0 {
return nil
}
netips := make([]net.IP, 0, len(ips))
for _, ip := range ips {
netips = append(netips, ip.IP())
}
return netips
}
func (s *Server) lookupIPInternal(domain string, option IPOption) ([]net.IP, error) {
if domain == "" {
2019-02-06 20:02:03 +00:00
return nil, newError("empty domain name")
}
if domain[len(domain)-1] == '.' {
domain = domain[:len(domain)-1]
}
ips := s.lookupStatic(domain, option, 0)
if ips != nil && ips[0].Family().IsIP() {
2019-02-12 20:04:28 +00:00
newError("returning ", len(ips), " IPs for domain ", domain).WriteToLog()
return toNetIP(ips), nil
}
if ips != nil && ips[0].Family().IsDomain() {
newdomain := ips[0].Domain()
newError("domain replaced: ", domain, " -> ", newdomain).WriteToLog()
domain = newdomain
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-12-28 19:28:31 +00:00
ns := s.clients[s.domainIndexMap[idx]]
2019-02-12 20:04:28 +00:00
newError("querying domain ", domain, " at ", ns.Name()).WriteToLog()
ips, err := s.queryIPTimeout(ns, domain, option)
if len(ips) > 0 {
return ips, nil
}
if err == dns.ErrEmptyResponse {
return nil, err
}
if err != nil {
2018-11-22 15:29:09 +00:00
newError("failed to lookup ip for domain ", domain, " at server ", ns.Name()).Base(err).WriteToLog()
lastErr = err
}
2018-06-26 13:04:47 +00:00
}
}
2018-12-28 19:28:31 +00:00
for _, client := range s.clients {
ips, err := s.queryIPTimeout(client, domain, option)
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 {
2018-12-28 19:28:31 +00:00
newError("failed to lookup ip for domain ", domain, " at server ", client.Name()).Base(err).WriteToLog()
lastErr = err
}
2019-02-21 14:17:04 +00:00
if err != context.Canceled && err != context.DeadlineExceeded {
return nil, 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
}
func init() {
common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
return New(ctx, config.(*Config))
}))
}