forked from nadoo/glider
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdns.go
244 lines (189 loc) · 5.31 KB
/
dns.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
// https://tools.ietf.org/html/rfc1035
package main
import (
"encoding/binary"
"io/ioutil"
"net"
"strings"
)
// DNSUDPHeaderLen is the length of UDP dns msg header
const DNSUDPHeaderLen = 12
// DNSTCPHeaderLen is the length of TCP dns msg header
const DNSTCPHeaderLen = 2 + DNSUDPHeaderLen
// DNSUDPMaxLen is the max size of udp dns request.
// https://tools.ietf.org/html/rfc1035#section-4.2.1
// Messages carried by UDP are restricted to 512 bytes (not counting the IP
// or UDP headers). Longer messages are truncated and the TC bit is set in
// the header.
// TODO: If the request length > 512 then the client will send TCP packets instead,
// so we should also serve tcp requests.
const DNSUDPMaxLen = 512
// DNSQueryTypeA ipv4
const DNSQueryTypeA = 1
// DNSQueryTypeAAAA ipv6
const DNSQueryTypeAAAA = 28
type dnsQuery struct {
DomainName string
QueryType uint16
QueryClass uint16
Offset int
}
type dnsAnswer struct {
// DomainName string
QueryType uint16
QueryClass uint16
TTL uint32
DataLength uint16
Data []byte
IP string
}
// DNSAnswerHandler .
type DNSAnswerHandler func(domain, ip string) error
// DNS .
type DNS struct {
*Forwarder // as proxy client
sDialer Dialer // dialer for server
dnsServer string
dnsServerMap map[string]string
answerHandlers []DNSAnswerHandler
}
// NewDNS returns a dns forwarder. client[dns.udp] -> glider[tcp] -> forwarder[dns.tcp] -> remote dns addr
func NewDNS(addr, raddr string, sDialer Dialer) (*DNS, error) {
s := &DNS{
Forwarder: NewForwarder(addr, nil),
sDialer: sDialer,
dnsServer: raddr,
dnsServerMap: make(map[string]string),
}
return s, nil
}
// ListenAndServe .
func (s *DNS) ListenAndServe() {
c, err := net.ListenPacket("udp", s.addr)
if err != nil {
logf("failed to listen on %s: %v", s.addr, err)
return
}
defer c.Close()
logf("listening UDP on %s", s.addr)
for {
data := make([]byte, DNSUDPMaxLen)
n, clientAddr, err := c.ReadFrom(data)
if err != nil {
logf("DNS local read error: %v", err)
continue
}
data = data[:n]
go func() {
// TODO: check domain rules and get a proper upstream name server.
query := parseQuery(data)
domain := query.DomainName
dnsServer := s.GetServer(domain)
// TODO: check here; ADD dnsServer to rule ip lists
rc, err := s.sDialer.NextDialer(domain+":53").Dial("tcp", dnsServer)
if err != nil {
logf("failed to connect to server %v: %v", dnsServer, err)
return
}
defer rc.Close()
// 2 bytes length after tcp header, before dns message
length := make([]byte, 2)
binary.BigEndian.PutUint16(length, uint16(len(data)))
rc.Write(length)
rc.Write(data)
resp, err := ioutil.ReadAll(rc)
if err != nil {
logf("error in ioutil.ReadAll: %s\n", err)
return
}
var ip string
// length is not needed in udp dns response. (2 bytes)
// SEE RFC1035, section 4.2.2 TCP: The message is prefixed with a two byte length field which gives the message length, excluding the two byte length field.
if len(resp) > 2 {
msg := resp[2:]
// TODO: Get IP from response, check and add to ipset
query := parseQuery(msg)
if len(msg) > query.Offset {
answers := parseAnswers(msg[query.Offset:])
for _, answer := range answers {
if answer.IP != "" {
ip += answer.IP + ","
}
for _, h := range s.answerHandlers {
h(query.DomainName, answer.IP)
}
}
}
_, err = c.WriteTo(msg, clientAddr)
if err != nil {
logf("error in local write: %s\n", err)
}
}
logf("proxy-dns %s <-> %s, %s: %s", clientAddr.String(), dnsServer, domain, ip)
}()
}
}
// SetServer .
func (s *DNS) SetServer(domain, server string) {
s.dnsServerMap[domain] = server
}
// GetServer .
func (s *DNS) GetServer(domain string) string {
domainParts := strings.Split(domain, ".")
length := len(domainParts)
for i := length - 2; i >= 0; i-- {
domain := strings.Join(domainParts[i:length], ".")
if server, ok := s.dnsServerMap[domain]; ok {
return server
}
}
return s.dnsServer
}
// AddAnswerHandler .
func (s *DNS) AddAnswerHandler(h DNSAnswerHandler) {
s.answerHandlers = append(s.answerHandlers, h)
}
func parseQuery(p []byte) *dnsQuery {
q := &dnsQuery{}
var i int
var domain []byte
for i = DNSUDPHeaderLen; i < len(p); {
l := int(p[i])
if l == 0 {
i++
break
}
domain = append(domain, p[i+1:i+l+1]...)
domain = append(domain, '.')
i = i + l + 1
}
q.DomainName = string(domain[:len(domain)-1])
q.QueryType = binary.BigEndian.Uint16(p[i:])
q.QueryClass = binary.BigEndian.Uint16(p[i+2:])
q.Offset = i + 4
return q
}
func parseAnswers(p []byte) []*dnsAnswer {
var answers []*dnsAnswer
for i := 0; i < len(p); {
l := int(p[i])
if l == 0 {
i++
break
}
answer := &dnsAnswer{}
answer.QueryType = binary.BigEndian.Uint16(p[i+2:])
answer.QueryClass = binary.BigEndian.Uint16(p[i+4:])
answer.TTL = binary.BigEndian.Uint32(p[i+6:])
answer.DataLength = binary.BigEndian.Uint16(p[i+10:])
answer.Data = p[i+12 : i+12+int(answer.DataLength)]
if answer.QueryType == DNSQueryTypeA {
answer.IP = net.IP(answer.Data[:net.IPv4len]).String()
} else if answer.QueryType == DNSQueryTypeAAAA {
answer.IP = net.IP(answer.Data[:net.IPv6len]).String()
}
answers = append(answers, answer)
i = i + 12 + int(answer.DataLength)
}
return answers
}