forked from v2fly/v2ray-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nameserver.go
217 lines (185 loc) · 4.39 KB
/
nameserver.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
package server
import (
"net"
"sync"
"time"
"v2ray.com/core/app/dispatcher"
"v2ray.com/core/common/buf"
"v2ray.com/core/common/dice"
"v2ray.com/core/common/log"
v2net "v2ray.com/core/common/net"
"v2ray.com/core/proxy"
"v2ray.com/core/transport/internet/udp"
"github.com/miekg/dns"
)
const (
DefaultTTL = uint32(3600)
CleanupInterval = time.Second * 120
CleanupThreshold = 512
)
var (
pseudoDestination = v2net.UDPDestination(v2net.LocalHostIP, v2net.Port(53))
)
type ARecord struct {
IPs []net.IP
Expire time.Time
}
type NameServer interface {
QueryA(domain string) <-chan *ARecord
}
type PendingRequest struct {
expire time.Time
response chan<- *ARecord
}
type UDPNameServer struct {
sync.Mutex
address v2net.Destination
requests map[uint16]*PendingRequest
udpServer *udp.Server
nextCleanup time.Time
}
func NewUDPNameServer(address v2net.Destination, dispatcher dispatcher.PacketDispatcher) *UDPNameServer {
s := &UDPNameServer{
address: address,
requests: make(map[uint16]*PendingRequest),
udpServer: udp.NewServer(dispatcher),
}
return s
}
// Private: Visible for testing.
func (v *UDPNameServer) Cleanup() {
expiredRequests := make([]uint16, 0, 16)
now := time.Now()
v.Lock()
for id, r := range v.requests {
if r.expire.Before(now) {
expiredRequests = append(expiredRequests, id)
close(r.response)
}
}
for _, id := range expiredRequests {
delete(v.requests, id)
}
v.Unlock()
expiredRequests = nil
}
// Private: Visible for testing.
func (v *UDPNameServer) AssignUnusedID(response chan<- *ARecord) uint16 {
var id uint16
v.Lock()
if len(v.requests) > CleanupThreshold && v.nextCleanup.Before(time.Now()) {
v.nextCleanup = time.Now().Add(CleanupInterval)
go v.Cleanup()
}
for {
id = uint16(dice.Roll(65536))
if _, found := v.requests[id]; found {
continue
}
log.Debug("DNS: Add pending request id ", id)
v.requests[id] = &PendingRequest{
expire: time.Now().Add(time.Second * 8),
response: response,
}
break
}
v.Unlock()
return id
}
// Private: Visible for testing.
func (v *UDPNameServer) HandleResponse(dest v2net.Destination, payload *buf.Buffer) {
msg := new(dns.Msg)
err := msg.Unpack(payload.Bytes())
if err != nil {
log.Warning("DNS: Failed to parse DNS response: ", err)
return
}
record := &ARecord{
IPs: make([]net.IP, 0, 16),
}
id := msg.Id
ttl := DefaultTTL
log.Debug("DNS: Handling response for id ", id, " content: ", msg.String())
v.Lock()
request, found := v.requests[id]
if !found {
v.Unlock()
return
}
delete(v.requests, id)
v.Unlock()
for _, rr := range msg.Answer {
switch rr := rr.(type) {
case *dns.A:
record.IPs = append(record.IPs, rr.A)
if rr.Hdr.Ttl < ttl {
ttl = rr.Hdr.Ttl
}
case *dns.AAAA:
record.IPs = append(record.IPs, rr.AAAA)
if rr.Hdr.Ttl < ttl {
ttl = rr.Hdr.Ttl
}
}
}
record.Expire = time.Now().Add(time.Second * time.Duration(ttl))
request.response <- record
close(request.response)
}
func (v *UDPNameServer) BuildQueryA(domain string, id uint16) *buf.Buffer {
msg := new(dns.Msg)
msg.Id = id
msg.RecursionDesired = true
msg.Question = []dns.Question{
{
Name: dns.Fqdn(domain),
Qtype: dns.TypeA,
Qclass: dns.ClassINET,
}}
buffer := buf.New()
buffer.AppendSupplier(func(b []byte) (int, error) {
writtenBuffer, err := msg.PackBuffer(b)
return len(writtenBuffer), err
})
return buffer
}
func (v *UDPNameServer) DispatchQuery(payload *buf.Buffer) {
v.udpServer.Dispatch(&proxy.SessionInfo{Source: pseudoDestination, Destination: v.address}, payload, v.HandleResponse)
}
func (v *UDPNameServer) QueryA(domain string) <-chan *ARecord {
response := make(chan *ARecord, 1)
id := v.AssignUnusedID(response)
v.DispatchQuery(v.BuildQueryA(domain, id))
go func() {
for i := 0; i < 2; i++ {
time.Sleep(time.Second)
v.Lock()
_, found := v.requests[id]
v.Unlock()
if found {
v.DispatchQuery(v.BuildQueryA(domain, id))
} else {
break
}
}
}()
return response
}
type LocalNameServer struct {
}
func (v *LocalNameServer) QueryA(domain string) <-chan *ARecord {
response := make(chan *ARecord, 1)
go func() {
defer close(response)
ips, err := net.LookupIP(domain)
if err != nil {
log.Info("DNS: Failed to lookup IPs for domain ", domain)
return
}
response <- &ARecord{
IPs: ips,
Expire: time.Now().Add(time.Second * time.Duration(DefaultTTL)),
}
}()
return response
}