-
Notifications
You must be signed in to change notification settings - Fork 17
/
server.go
141 lines (118 loc) · 3.81 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
package main
import (
"strconv"
"time"
"github.com/coreos/go-etcd/etcd"
"github.com/miekg/dns"
"github.com/rcrowley/go-metrics"
)
type Server struct {
addr string
port int
etcd *etcd.Client
rTimeout time.Duration
wTimeout time.Duration
defaultTtl uint32
queryFilterer *QueryFilterer
}
type Handler struct {
resolver *Resolver
queryFilterer *QueryFilterer
// Metrics
requestCounter metrics.Counter
acceptCounter metrics.Counter
rejectCounter metrics.Counter
responseTimer metrics.Timer
}
func (h *Handler) Handle(response dns.ResponseWriter, req *dns.Msg) {
h.requestCounter.Inc(1)
h.responseTimer.Time(func() {
debugMsg("Handling incoming query for domain " + req.Question[0].Name)
// Lookup the dns record for the request
// This method will add any answers to the message
var msg *dns.Msg
if h.queryFilterer.ShouldAcceptQuery(req) != true {
debugMsg("Query not accepted")
h.rejectCounter.Inc(1)
msg = new(dns.Msg)
msg.SetReply(req)
msg.SetRcode(req, dns.RcodeNameError)
msg.Authoritative = true
msg.RecursionAvailable = false
// Add a useful TXT record
header := dns.RR_Header{Name: req.Question[0].Name,
Class: dns.ClassINET,
Rrtype: dns.TypeTXT}
msg.Ns = []dns.RR{&dns.TXT{header, []string{"Rejected query based on matched filters"}}}
} else {
h.acceptCounter.Inc(1)
msg = h.resolver.Lookup(req)
}
if msg != nil {
err := response.WriteMsg(msg)
if err != nil {
debugMsg("Error writing message: ", err)
}
}
debugMsg("Sent response to ", response.RemoteAddr())
})
}
func (s *Server) Addr() string {
return s.addr + ":" + strconv.Itoa(s.port)
}
func (s *Server) Run() {
tcpResponseTimer := metrics.NewTimer()
metrics.Register("request.handler.tcp.response_time", tcpResponseTimer)
tcpRequestCounter := metrics.NewCounter()
metrics.Register("request.handler.tcp.requests", tcpRequestCounter)
tcpAcceptCounter := metrics.NewCounter()
metrics.Register("request.handler.tcp.filter_accepts", tcpAcceptCounter)
tcpRejectCounter := metrics.NewCounter()
metrics.Register("request.handler.tcp.filter_rejects", tcpRejectCounter)
udpResponseTimer := metrics.NewTimer()
metrics.Register("request.handler.udp.response_time", udpResponseTimer)
udpRequestCounter := metrics.NewCounter()
metrics.Register("request.handler.udp.requests", udpRequestCounter)
udpAcceptCounter := metrics.NewCounter()
metrics.Register("request.handler.udp.filter_accepts", udpAcceptCounter)
udpRejectCounter := metrics.NewCounter()
metrics.Register("request.handler.udp.filter_rejects", udpRejectCounter)
resolver := Resolver{etcd: s.etcd, defaultTtl: s.defaultTtl}
tcpDNShandler := &Handler{
resolver: &resolver,
requestCounter: tcpRequestCounter,
acceptCounter: tcpAcceptCounter,
rejectCounter: tcpRejectCounter,
responseTimer: tcpResponseTimer,
queryFilterer: s.queryFilterer}
udpDNShandler := &Handler{
resolver: &resolver,
requestCounter: udpRequestCounter,
acceptCounter: udpAcceptCounter,
rejectCounter: udpRejectCounter,
responseTimer: udpResponseTimer,
queryFilterer: s.queryFilterer}
udpHandler := dns.NewServeMux()
tcpHandler := dns.NewServeMux()
tcpHandler.HandleFunc(".", tcpDNShandler.Handle)
udpHandler.HandleFunc(".", udpDNShandler.Handle)
tcpServer := &dns.Server{Addr: s.Addr(),
Net: "tcp",
Handler: tcpHandler,
ReadTimeout: s.rTimeout,
WriteTimeout: s.wTimeout}
udpServer := &dns.Server{Addr: s.Addr(),
Net: "udp",
Handler: udpHandler,
UDPSize: 65535,
ReadTimeout: s.rTimeout,
WriteTimeout: s.wTimeout}
go s.start(udpServer)
go s.start(tcpServer)
}
func (s *Server) start(ds *dns.Server) {
err := ds.ListenAndServe()
if err != nil {
logger.Fatalf("Start %s listener on %s failed:%s", ds.Net, s.Addr(), err.Error())
}
}