forked from v2fly/v2ray-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
153 lines (129 loc) · 3.71 KB
/
server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package dns
//go:generate errorgen
import (
"context"
"sync"
"time"
"v2ray.com/core"
"v2ray.com/core/common"
"v2ray.com/core/common/net"
"v2ray.com/core/common/strmatcher"
)
type Server struct {
sync.Mutex
hosts *StaticHosts
servers []NameServerInterface
clientIP net.IP
domainMatcher strmatcher.IndexMatcher
domainIndexMap map[uint32]uint32
}
func New(ctx context.Context, config *Config) (*Server, error) {
server := &Server{
servers: make([]NameServerInterface, 0, len(config.NameServers)+len(config.NameServer)),
}
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)
}
hosts, err := NewStaticHosts(config.StaticHosts, config.Hosts)
if err != nil {
return nil, newError("failed to create hosts").Base(err)
}
server.hosts = hosts
v := core.MustFromContext(ctx)
if err := v.RegisterFeature((*core.DNSClient)(nil), server); err != nil {
return nil, newError("unable to register DNSClient.").Base(err)
}
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 {
server.servers = append(server.servers, NewClassicNameServer(dest, v.Dispatcher(), server.clientIP))
}
}
return len(server.servers) - 1
}
if len(config.NameServers) > 0 {
core.PrintDeprecatedFeatureWarning("simple DNS server")
}
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 {
server.servers = append(server.servers, NewLocalNameServer())
}
return server, nil
}
// Start implements common.Runnable.
func (s *Server) Start() error {
return nil
}
// Close implements common.Closable.
func (s *Server) Close() error {
return nil
}
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
}
func (s *Server) LookupIP(domain string) ([]net.IP, error) {
if ip := s.hosts.LookupIP(domain); len(ip) > 0 {
return ip, nil
}
var lastErr error
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
}
}
}
for _, server := range s.servers {
ips, err := s.queryIPTimeout(server, domain)
if len(ips) > 0 {
return ips, nil
}
if err != nil {
lastErr = err
}
}
return nil, newError("returning nil for domain ", domain).Base(lastErr)
}
func init() {
common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
return New(ctx, config.(*Config))
}))
}