-
Notifications
You must be signed in to change notification settings - Fork 4
/
server.go
214 lines (198 loc) · 4.52 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
package zdns
import (
"fmt"
"io"
"log"
"net"
"net/http"
"net/url"
"os"
"sync"
"time"
"github.com/cenkalti/backoff/v4"
"github.com/mpolden/zdns/dns"
"github.com/mpolden/zdns/hosts"
)
const (
// HijackZero returns the zero IP address to matching requests.
HijackZero = iota
// HijackEmpty returns an empty answer to matching requests.
HijackEmpty
// HijackHosts returns the value of the hoss entry to matching request.
HijackHosts
)
// A Server defines parameters for running a DNS server.
type Server struct {
Config Config
hosts hosts.Hosts
proxy *dns.Proxy
done chan bool
mu sync.RWMutex
httpClient *http.Client
}
// NewServer returns a new server configured according to config.
func NewServer(proxy *dns.Proxy, config Config) (*Server, error) {
server := &Server{
Config: config,
done: make(chan bool, 1),
proxy: proxy,
httpClient: &http.Client{Timeout: 10 * time.Second},
}
proxy.Handler = server.hijack
// Periodically refresh hosts
if interval := config.DNS.refreshInterval; interval > 0 {
go server.reloadHosts(interval)
}
// Load initial hosts
go server.loadHosts()
return server, nil
}
func (s *Server) httpGet(url string) (io.ReadCloser, error) {
var body io.ReadCloser
policy := backoff.NewExponentialBackOff()
policy.MaxInterval = 2 * time.Second
policy.MaxElapsedTime = 30 * time.Second
err := backoff.Retry(func() error {
res, err := s.httpClient.Get(url)
if err == nil {
body = res.Body
}
return err
}, policy)
if err != nil {
return nil, err
}
return body, nil
}
func (s *Server) readHosts(name string) (hosts.Hosts, error) {
url, err := url.Parse(name)
if err != nil {
return nil, err
}
var rc io.ReadCloser
switch url.Scheme {
case "file":
f, err := os.Open(url.Path)
if err != nil {
return nil, err
}
rc = f
case "http", "https":
rc, err = s.httpGet(url.String())
if err != nil {
return nil, err
}
default:
return nil, fmt.Errorf("%s: invalid scheme: %s", url, url.Scheme)
}
hosts, err := hosts.Parse(rc)
if err1 := rc.Close(); err == nil {
err = err1
}
return hosts, err
}
func nonFqdn(s string) string {
sz := len(s)
if sz > 0 && s[sz-1:] == "." {
return s[:sz-1]
}
return s
}
func (s *Server) reloadHosts(interval time.Duration) {
for {
select {
case <-s.done:
return
case <-time.After(interval):
s.loadHosts()
}
}
}
func (s *Server) loadHosts() {
hs := make(hosts.Hosts)
for _, h := range s.Config.Hosts {
src := "inline hosts"
hs1 := h.hosts
if h.URL != "" {
src = h.URL
var err error
hs1, err = s.readHosts(h.URL)
if err != nil {
log.Printf("failed to read hosts from %s: %s", h.URL, err)
continue
}
}
if h.Hijack {
for name, ipAddrs := range hs1 {
hs[name] = ipAddrs
}
log.Printf("loaded %d hosts from %s", len(hs1), src)
} else {
removed := 0
for hostToRemove := range hs1 {
if _, ok := hs.Get(hostToRemove); ok {
removed++
hs.Del(hostToRemove)
}
}
if removed > 0 {
log.Printf("removed %d hosts from %s", removed, src)
}
}
}
s.mu.Lock()
s.hosts = hs
s.mu.Unlock()
log.Printf("loaded %d hosts in total", len(hs))
}
// Reload updates hosts entries of Server s.
func (s *Server) Reload() { s.loadHosts() }
// Close terminates all active operations and shuts down the DNS server.
func (s *Server) Close() error {
s.done <- true
return nil
}
func (s *Server) hijack(r *dns.Request) *dns.Reply {
if r.Type != dns.TypeA && r.Type != dns.TypeAAAA {
return nil // Type not applicable
}
s.mu.RLock()
ipAddrs, ok := s.hosts.Get(nonFqdn(r.Name))
s.mu.RUnlock()
if !ok {
return nil // No match
}
switch s.Config.DNS.hijackMode {
case HijackZero:
switch r.Type {
case dns.TypeA:
return dns.ReplyA(r.Name, net.IPv4zero)
case dns.TypeAAAA:
return dns.ReplyAAAA(r.Name, net.IPv6zero)
}
case HijackEmpty:
return &dns.Reply{}
case HijackHosts:
var ipv4Addr []net.IP
var ipv6Addr []net.IP
for _, ipAddr := range ipAddrs {
if ipAddr.IP.To4() == nil {
ipv6Addr = append(ipv6Addr, ipAddr.IP)
} else {
ipv4Addr = append(ipv4Addr, ipAddr.IP)
}
}
switch r.Type {
case dns.TypeA:
return dns.ReplyA(r.Name, ipv4Addr...)
case dns.TypeAAAA:
return dns.ReplyAAAA(r.Name, ipv6Addr...)
}
}
return nil
}
// ListenAndServe starts a server on configured address and protocol.
func (s *Server) ListenAndServe() error {
log.Printf("dns server listening on %s [%s]", s.Config.DNS.Listen, s.Config.DNS.Protocol)
return s.proxy.ListenAndServe(s.Config.DNS.Listen, s.Config.DNS.Protocol)
}