forked from AdguardTeam/dnsproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
216 lines (175 loc) · 4.66 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
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
package proxy
import (
"fmt"
"net"
"time"
"github.com/AdguardTeam/golibs/log"
"github.com/miekg/dns"
)
// startListeners configures and starts listener loops
func (p *Proxy) startListeners() error {
err := p.createUDPListeners()
if err != nil {
return err
}
err = p.createTCPListeners()
if err != nil {
return err
}
err = p.createTLSListeners()
if err != nil {
return err
}
err = p.createHTTPSListeners()
if err != nil {
return err
}
err = p.createQUICListeners()
if err != nil {
return err
}
err = p.createDNSCryptListeners()
if err != nil {
return err
}
for _, l := range p.udpListen {
go p.udpPacketLoop(l, p.requestGoroutinesSema)
}
for _, l := range p.tcpListen {
go p.tcpPacketLoop(l, ProtoTCP, p.requestGoroutinesSema)
}
for _, l := range p.tlsListen {
go p.tcpPacketLoop(l, ProtoTLS, p.requestGoroutinesSema)
}
for i := range p.httpsServer {
go p.listenHTTPS(p.httpsServer[i], p.httpsListen[i])
}
for _, l := range p.quicListen {
go p.quicPacketLoop(l, p.requestGoroutinesSema)
}
for _, l := range p.dnsCryptUDPListen {
go func(l *net.UDPConn) { _ = p.dnsCryptServer.ServeUDP(l) }(l)
}
for _, l := range p.dnsCryptTCPListen {
go func(l net.Listener) { _ = p.dnsCryptServer.ServeTCP(l) }(l)
}
return nil
}
// handleDNSRequest processes the incoming packet bytes and returns with an optional response packet.
func (p *Proxy) handleDNSRequest(d *DNSContext) error {
d.StartTime = time.Now()
p.logDNSMessage(d.Req)
if d.Req.Response {
log.Debug("Dropping incoming Reply packet from %s", d.Addr.String())
return nil
}
if p.BeforeRequestHandler != nil {
ok, err := p.BeforeRequestHandler(p, d)
if err != nil {
log.Error("Error in the BeforeRequestHandler: %s", err)
d.Res = p.genServerFailure(d.Req)
p.respond(d)
return nil
}
if !ok {
return nil // do nothing, don't reply
}
}
// ratelimit based on IP only, protects CPU cycles and outbound connections
if d.Proto == ProtoUDP && p.isRatelimited(d.Addr) {
log.Tracef("Ratelimiting %v based on IP only", d.Addr)
return nil // do nothing, don't reply, we got ratelimited
}
if len(d.Req.Question) != 1 {
log.Debug("got invalid number of questions: %v", len(d.Req.Question))
d.Res = p.genServerFailure(d.Req)
}
// refuse ANY requests (anti-DDOS measure)
if p.RefuseAny && len(d.Req.Question) > 0 && d.Req.Question[0].Qtype == dns.TypeANY {
log.Tracef("Refusing type=ANY request")
d.Res = p.genNotImpl(d.Req)
}
var err error
if d.Res == nil {
if len(p.UpstreamConfig.Upstreams) == 0 {
panic("SHOULD NOT HAPPEN: no default upstreams specified")
}
// execute the DNS request
// if there is a custom middleware configured, use it
if p.RequestHandler != nil {
err = p.RequestHandler(p, d)
} else {
err = p.Resolve(d)
}
if err != nil {
err = fmt.Errorf("talking to dns upstream: %w", err)
}
}
p.logDNSMessage(d.Res)
p.respond(d)
return err
}
// respond writes the specified response to the client (or does nothing if d.Res is empty)
func (p *Proxy) respond(d *DNSContext) {
// d.Conn can be nil in the case of a DOH request
if d.Conn != nil {
d.Conn.SetWriteDeadline(time.Now().Add(defaultTimeout)) //nolint
}
var err error
switch d.Proto {
case ProtoUDP:
err = p.respondUDP(d)
case ProtoTCP:
err = p.respondTCP(d)
case ProtoTLS:
err = p.respondTCP(d)
case ProtoHTTPS:
err = p.respondHTTPS(d)
case ProtoQUIC:
err = p.respondQUIC(d)
case ProtoDNSCrypt:
err = p.respondDNSCrypt(d)
default:
err = fmt.Errorf("SHOULD NOT HAPPEN - unknown protocol: %s", d.Proto)
}
if err != nil {
logWithNonCrit(err, fmt.Sprintf("responding %s request", d.Proto))
}
}
// Set TTL value of all records according to our settings
func (p *Proxy) setMinMaxTTL(r *dns.Msg) {
for _, rr := range r.Answer {
originalTTL := rr.Header().Ttl
newTTL := respectTTLOverrides(originalTTL, p.CacheMinTTL, p.CacheMaxTTL)
if originalTTL != newTTL {
log.Debug("Override TTL from %d to %d", originalTTL, newTTL)
rr.Header().Ttl = newTTL
}
}
}
func (p *Proxy) genServerFailure(request *dns.Msg) *dns.Msg {
return p.genWithRCode(request, dns.RcodeServerFailure)
}
func (p *Proxy) genNotImpl(request *dns.Msg) (resp *dns.Msg) {
resp = p.genWithRCode(request, dns.RcodeNotImplemented)
// NOTIMPL without EDNS is treated as 'we don't support EDNS', so
// explicitly set it.
resp.SetEdns0(1452, false)
return resp
}
func (p *Proxy) genWithRCode(r *dns.Msg, code int) (resp *dns.Msg) {
resp = &dns.Msg{}
resp.SetRcode(r, code)
resp.RecursionAvailable = true
return resp
}
func (p *Proxy) logDNSMessage(m *dns.Msg) {
if m == nil {
return
}
if m.Response {
log.Tracef("OUT: %s", m)
} else {
log.Tracef("IN: %s", m)
}
}