-
Notifications
You must be signed in to change notification settings - Fork 11
/
server.go
370 lines (299 loc) · 7.63 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
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
package dns
import (
"bufio"
"context"
"crypto/tls"
"io"
"log"
"net"
"sync"
)
// A Server defines parameters for running a DNS server. The zero value for
// Server is a valid configuration.
type Server struct {
Addr string // TCP and UDP address to listen on, ":domain" if empty
Handler Handler // handler to invoke
TLSConfig *tls.Config // optional TLS config, used by ListenAndServeTLS
// Forwarder relays a recursive query. If nil, recursive queries are
// answered with a "Query Refused" message.
Forwarder RoundTripper
// ErrorLog specifies an optional logger for errors accepting connections,
// reading data, and unpacking messages.
// If nil, logging is done via the log package's standard logger.
ErrorLog *log.Logger
}
// ListenAndServe listens on both the TCP and UDP network address s.Addr and
// then calls Serve or ServePacket to handle queries on incoming connections.
// If srv.Addr is blank, ":domain" is used. ListenAndServe always returns a
// non-nil error.
func (s *Server) ListenAndServe(ctx context.Context) error {
addr := s.Addr
if addr == "" {
addr = ":domain"
}
ln, err := net.Listen("tcp", addr)
if err != nil {
return err
}
conn, err := net.ListenPacket("udp", addr)
if err != nil {
return err
}
errc := make(chan error, 1)
go func() { errc <- s.Serve(ctx, ln) }()
go func() { errc <- s.ServePacket(ctx, conn) }()
return <-errc
}
// ListenAndServeTLS listens on the TCP network address s.Addr and then calls
// Serve to handle requests on incoming TLS connections.
//
// If s.Addr is blank, ":853" is used.
//
// ListenAndServeTLS always returns a non-nil error.
func (s *Server) ListenAndServeTLS(ctx context.Context) error {
addr := s.Addr
if addr == "" {
addr = ":domain"
}
ln, err := net.Listen("tcp", addr)
if err != nil {
return err
}
return s.ServeTLS(ctx, ln)
}
// Serve accepts incoming connections on the Listener ln, creating a new
// service goroutine for each. The service goroutines read TCP encoded queries
// and then call s.Handler to reply to them.
//
// See RFC 1035, section 4.2.2 "TCP usage" for transport encoding of messages.
//
// Serve always returns a non-nil error.
func (s *Server) Serve(ctx context.Context, ln net.Listener) error {
defer ln.Close()
for {
conn, err := ln.Accept()
if err != nil {
return err
}
go s.serveStream(ctx, conn)
}
}
// ServePacket reads UDP encoded queries from the PacketConn conn, creating a
// new service goroutine for each. The service goroutines call s.Handler to
// reply.
//
// See RFC 1035, section 4.2.1 "UDP usage" for transport encoding of messages.
//
// ServePacket always returns a non-nil error.
func (s *Server) ServePacket(ctx context.Context, conn net.PacketConn) error {
defer conn.Close()
for {
buf := make([]byte, maxPacketLen)
n, addr, err := conn.ReadFrom(buf)
if err != nil {
return err
}
req := &Query{
Message: new(Message),
RemoteAddr: addr,
}
if buf, err = req.Message.Unpack(buf[:n]); err != nil {
s.logf("dns unpack: %s", err.Error())
continue
}
if len(buf) != 0 {
s.logf("dns unpack: malformed packet, extra message bytes")
continue
}
pw := &packetWriter{
messageWriter: &messageWriter{
msg: response(req.Message),
},
addr: addr,
conn: conn,
}
go s.handle(ctx, pw, req)
}
}
// ServeTLS accepts incoming connections on the Listener ln, creating a new
// service goroutine for each. The service goroutines read TCP encoded queries
// over a TLS channel and then call s.Handler to reply to them, in another
// service goroutine.
//
// See RFC 7858, section 3.3 for transport encoding of messages.
//
// ServeTLS always returns a non-nil error.
func (s *Server) ServeTLS(ctx context.Context, ln net.Listener) error {
ln = tls.NewListener(ln, s.TLSConfig.Clone())
defer ln.Close()
for {
conn, err := ln.Accept()
if err != nil {
return err
}
go func(conn net.Conn) {
if err := conn.(*tls.Conn).Handshake(); err != nil {
s.logf("dns handshake: %s", err.Error())
return
}
s.serveStream(ctx, conn)
}(conn)
}
}
func (s *Server) serveStream(ctx context.Context, conn net.Conn) {
var (
rbuf = bufio.NewReader(conn)
lbuf [2]byte
mu sync.Mutex
)
for {
if _, err := rbuf.Read(lbuf[:]); err != nil {
if err != io.EOF {
s.logf("dns read: %s", err.Error())
}
return
}
buf := make([]byte, int(nbo.Uint16(lbuf[:])))
if _, err := io.ReadFull(rbuf, buf); err != nil {
s.logf("dns read: %s", err.Error())
return
}
req := &Query{
Message: new(Message),
RemoteAddr: conn.RemoteAddr(),
}
var err error
if buf, err = req.Message.Unpack(buf); err != nil {
s.logf("dns unpack: %s", err.Error())
continue
}
if len(buf) != 0 {
s.logf("dns unpack: malformed packet, extra message bytes")
continue
}
sw := streamWriter{
messageWriter: &messageWriter{
msg: response(req.Message),
},
mu: &mu,
conn: conn,
}
go s.handle(ctx, sw, req)
}
}
func (s *Server) handle(ctx context.Context, w MessageWriter, r *Query) {
sw := &serverWriter{
MessageWriter: w,
forwarder: s.Forwarder,
query: r,
}
s.Handler.ServeDNS(ctx, sw, r)
if !sw.replied {
if err := sw.Reply(ctx); err != nil {
s.logf("dns: %s", err.Error())
}
}
}
func (s *Server) logf(format string, args ...interface{}) {
printf := log.Printf
if s.ErrorLog != nil {
printf = s.ErrorLog.Printf
}
printf(format, args...)
}
type packetWriter struct {
*messageWriter
addr net.Addr
conn net.PacketConn
}
func (w packetWriter) Recur(ctx context.Context) (*Message, error) {
return nil, ErrUnsupportedOp
}
func (w packetWriter) Reply(ctx context.Context) error {
buf, err := w.msg.Pack(nil, true)
if err != nil {
return err
}
if len(buf) > maxPacketLen {
return w.truncate(buf)
}
_, err = w.conn.WriteTo(buf, w.addr)
return err
}
func (w packetWriter) truncate(buf []byte) error {
var err error
if buf, err = truncate(buf, maxPacketLen); err != nil {
return err
}
if _, err := w.conn.WriteTo(buf, w.addr); err != nil {
return err
}
return ErrTruncatedMessage
}
type streamWriter struct {
*messageWriter
mu *sync.Mutex
conn net.Conn
}
func (w streamWriter) Recur(ctx context.Context) (*Message, error) {
return nil, ErrUnsupportedOp
}
func (w streamWriter) Reply(ctx context.Context) error {
buf, err := w.msg.Pack(make([]byte, 2), true)
if err != nil {
return err
}
blen := uint16(len(buf) - 2)
if int(blen) != len(buf)-2 {
return ErrOversizedMessage
}
nbo.PutUint16(buf[:2], blen)
w.mu.Lock()
defer w.mu.Unlock()
_, err = w.conn.Write(buf)
return err
}
type serverWriter struct {
MessageWriter
forwarder RoundTripper
query *Query
replied bool
}
func (w serverWriter) Recur(ctx context.Context) (*Message, error) {
query := &Query{
Message: request(w.query.Message),
RemoteAddr: w.query.RemoteAddr,
}
qs := make([]Question, 0, len(w.query.Questions))
for _, q := range w.query.Questions {
if !questionMatched(q, query.Message) {
qs = append(qs, q)
}
}
query.Questions = qs
return w.forward(ctx, query)
}
func (w serverWriter) Reply(ctx context.Context) error {
w.replied = true
return w.MessageWriter.Reply(ctx)
}
func response(msg *Message) *Message {
res := new(Message)
*res = *msg // shallow copy
res.Response = true
return res
}
var refuser = &Client{
Transport: nopDialer{},
Resolver: HandlerFunc(Refuse),
}
func (w serverWriter) forward(ctx context.Context, query *Query) (*Message, error) {
if w.forwarder != nil {
return w.forwarder.Do(ctx, query)
}
return refuser.Do(ctx, query)
}
type nopDialer struct{}
func (nopDialer) DialAddr(ctx context.Context, addr net.Addr) (Conn, error) {
return nil, nil
}