/
udpns.go
executable file
·229 lines (201 loc) · 4.6 KB
/
udpns.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package dns
import (
"context"
"sync"
"sync/atomic"
"time"
"github.com/miekg/dns"
"v2ray.com/core"
"v2ray.com/core/common"
"v2ray.com/core/common/buf"
"v2ray.com/core/common/net"
"v2ray.com/core/common/signal"
"v2ray.com/core/common/task"
"v2ray.com/core/transport/internet/udp"
)
type IPRecord struct {
IP net.IP
Expire time.Time
}
type ClassicNameServer struct {
sync.RWMutex
address net.Destination
ips map[string][]IPRecord
updated signal.Notifier
udpServer *udp.Dispatcher
cleanup *task.Periodic
reqID uint32
}
func NewClassicNameServer(address net.Destination, dispatcher core.Dispatcher) *ClassicNameServer {
s := &ClassicNameServer{
address: address,
ips: make(map[string][]IPRecord),
udpServer: udp.NewDispatcher(dispatcher),
}
s.cleanup = &task.Periodic{
Interval: time.Minute,
Execute: s.Cleanup,
}
common.Must(s.cleanup.Start())
return s
}
func (s *ClassicNameServer) Cleanup() error {
now := time.Now()
s.Lock()
for domain, ips := range s.ips {
newIPs := make([]IPRecord, 0, len(ips))
for _, ip := range ips {
if ip.Expire.After(now) {
newIPs = append(newIPs, ip)
}
}
if len(newIPs) == 0 {
delete(s.ips, domain)
} else if len(newIPs) < len(ips) {
s.ips[domain] = newIPs
}
}
if len(s.ips) == 0 {
s.ips = make(map[string][]IPRecord)
}
s.Unlock()
return nil
}
func (s *ClassicNameServer) HandleResponse(payload *buf.Buffer) {
msg := new(dns.Msg)
err := msg.Unpack(payload.Bytes())
if err == dns.ErrTruncated {
newError("truncated message received. DNS server should still work. If you see anything abnormal, please submit an issue to v2ray-core.").AtWarning().WriteToLog()
} else if err != nil {
newError("failed to parse DNS response").Base(err).AtWarning().WriteToLog()
return
}
var domain string
ips := make([]IPRecord, 0, 16)
now := time.Now()
for _, rr := range msg.Answer {
var ip net.IP
domain = rr.Header().Name
ttl := rr.Header().Ttl
switch rr := rr.(type) {
case *dns.A:
ip = rr.A
case *dns.AAAA:
ip = rr.AAAA
}
if ttl == 0 {
ttl = 300
}
if len(ip) > 0 {
ips = append(ips, IPRecord{
IP: ip,
Expire: now.Add(time.Second * time.Duration(ttl)),
})
}
}
if len(domain) > 0 && len(ips) > 0 {
s.updateIP(domain, ips)
}
}
func (s *ClassicNameServer) updateIP(domain string, ips []IPRecord) {
s.Lock()
defer s.Unlock()
newError("updating IP records for domain:", domain).AtDebug().WriteToLog()
now := time.Now()
eips := s.ips[domain]
for _, ip := range eips {
if ip.Expire.After(now) {
ips = append(ips, ip)
}
}
s.ips[domain] = ips
s.updated.Signal()
}
func (s *ClassicNameServer) buildMsgs(domain string) []*dns.Msg {
allowMulti := multiQuestionDNS[s.address.Address]
var msgs []*dns.Msg
{
msg := new(dns.Msg)
msg.Id = uint16(atomic.AddUint32(&s.reqID, 1))
msg.RecursionDesired = true
msg.Question = []dns.Question{
{
Name: domain,
Qtype: dns.TypeA,
Qclass: dns.ClassINET,
}}
if allowMulti {
msg.Question = append(msg.Question, dns.Question{
Name: domain,
Qtype: dns.TypeAAAA,
Qclass: dns.ClassINET,
})
}
msgs = append(msgs, msg)
}
if !allowMulti {
msg := new(dns.Msg)
msg.Id = uint16(atomic.AddUint32(&s.reqID, 1))
msg.RecursionDesired = true
msg.Question = []dns.Question{
{
Name: domain,
Qtype: dns.TypeAAAA,
Qclass: dns.ClassINET,
},
}
msgs = append(msgs, msg)
}
return msgs
}
func msgToBuffer(msg *dns.Msg) (*buf.Buffer, error) {
buffer := buf.New()
if err := buffer.Reset(func(b []byte) (int, error) {
writtenBuffer, err := msg.PackBuffer(b)
return len(writtenBuffer), err
}); err != nil {
return nil, err
}
return buffer, nil
}
func (s *ClassicNameServer) sendQuery(ctx context.Context, domain string) {
msgs := s.buildMsgs(domain)
for _, msg := range msgs {
b, err := msgToBuffer(msg)
common.Must(err)
s.udpServer.Dispatch(ctx, s.address, b, s.HandleResponse)
}
}
func (s *ClassicNameServer) findIPsForDomain(domain string) []net.IP {
records, found := s.ips[domain]
if found && len(records) > 0 {
var ips []net.IP
now := time.Now()
for _, rec := range records {
if rec.Expire.After(now) {
ips = append(ips, rec.IP)
}
}
return ips
}
return nil
}
func (s *ClassicNameServer) QueryIP(ctx context.Context, domain string) ([]net.IP, error) {
fqdn := dns.Fqdn(domain)
ips := s.findIPsForDomain(fqdn)
if len(ips) > 0 {
return ips, nil
}
s.sendQuery(ctx, fqdn)
for {
ips := s.findIPsForDomain(fqdn)
if len(ips) > 0 {
return ips, nil
}
select {
case <-ctx.Done():
return nil, ctx.Err()
case <-s.updated.Wait():
}
}
}