-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocker-devdns.go
321 lines (244 loc) · 8.69 KB
/
docker-devdns.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
package main
import (
"flag"
"github.com/rrva/dockerclient"
"log"
"os"
"strings"
"github.com/miekg/dns"
"net"
"os/signal"
"syscall"
)
type myDockerClient interface {
StartMonitorEvents(cb dockerclient.Callback, ec chan error, args ...interface{})
ListContainers(all bool, size bool, filters string) ([]dockerclient.Container, error)
InspectContainer(id string) (*dockerclient.ContainerInfo, error)
Version() (*dockerclient.Version, error)
}
func registerShutdownSignalHandlers() {
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM, syscall.SIGQUIT)
for s := range sigChan {
log.Fatalf("Signal (%s) received, shutting down", s)
}
}
func cb(event *dockerclient.Event, errors chan error, args ...interface{}) {
if (event.Status == "start") {
info, err := docker.InspectContainer(event.Id)
if err != nil {
log.Fatalf("Err: %s", err)
} else {
name := strings.TrimPrefix(info.Name, "/")
log.Printf("%s=%s", name, info.NetworkSettings.IpAddress)
nameCache[name] = info.NetworkSettings.IpAddress
nameCache[info.NetworkSettings.IpAddress] = name
}
}
if (event.Status == "die") {
info, err := docker.InspectContainer(event.Id)
if err != nil {
log.Fatalf("Err: %s", err)
} else {
name := strings.TrimPrefix(info.Name, "/")
delete(nameCache, name)
delete(nameCache, info.NetworkSettings.IpAddress)
}
}
}
var docker myDockerClient
func refreshEntireCache() {
log.Printf("Listing all running containers...")
containers, err := docker.ListContainers(false, false, "")
if err != nil {
log.Printf("error listing containers %s", err)
}
for _, container := range containers {
info, _ := docker.InspectContainer(container.Id)
for _, name := range container.Names {
name = strings.TrimPrefix(name, "/")
log.Printf("%s=%s", name, info.NetworkSettings.IpAddress)
nameCache[name] = info.NetworkSettings.IpAddress
nameCache[info.NetworkSettings.IpAddress] = name
}
}
}
func writeNameResponse(m *dns.Msg, ip string, name *string) {
rr := new(dns.PTR)
str := *name + "." + domain + "."
rr.Hdr = dns.RR_Header{Name: ip, Rrtype: dns.TypePTR, Class: dns.ClassINET, Ttl: 5}
rr.Ptr = str
m.Answer = append(m.Answer, rr)
}
func writeIPAddressResponse(IPAddress *string, m *dns.Msg, name string, qtype uint16) {
ip := net.ParseIP(*IPAddress)
ip4 := ip.To4()
if ip4 != nil && qtype == dns.TypeA {
rr := new(dns.A)
rr.Hdr = dns.RR_Header{Name: name, Rrtype: dns.TypeA, Class: dns.ClassINET, Ttl: 5}
rr.A = ip4
m.Answer = append(m.Answer, rr)
} else if ip4 == nil && qtype == dns.TypeAAAA {
rr := new(dns.AAAA)
rr.Hdr = dns.RR_Header{Name: name, Rrtype: dns.TypeAAAA, Class: dns.ClassINET, Ttl: 5}
rr.AAAA = ip.To16()
m.Answer = append(m.Answer, rr)
}
}
func inAddrArpaToIPAddress(n string) string {
ips := strings.Split(strings.Replace(n, ".in-addr.arpa.", "", 1), ".")
s := ""
last := len(ips)
for i := range ips {
s = s + ips[last-1-i]
if i < last-1 {
s = s + "."
}
}
return s
}
func writeDNSResponse(w dns.ResponseWriter, m *dns.Msg) {
err := w.WriteMsg(m)
if err != nil {
log.Printf("Failed writing DNS response: %s", err)
}
}
func dockerNameLookupServer(w dns.ResponseWriter, req *dns.Msg) {
m := new(dns.Msg)
m.SetReply(req)
for _, question := range req.Question {
rr := new(dns.A)
rr.Hdr = dns.RR_Header{Name: question.Name, Rrtype: dns.TypeA, Class: dns.ClassINET, Ttl: 5}
name := strings.TrimSuffix(question.Name, "."+domain+".")
IPAddress, exists := nameCache[name]
if !exists {
log.Printf("Name not found: %s %s", name, IPAddress)
writeDNSResponse(w, m)
return
}
writeIPAddressResponse(&IPAddress, m, question.Name, question.Qtype)
}
writeDNSResponse(w, m)
}
func localLookupServer(w dns.ResponseWriter, req *dns.Msg) {
m := new(dns.Msg)
m.SetReply(req)
for _, question := range req.Question {
if question.Qtype == dns.TypeA || question.Qtype == dns.TypeAAAA {
IPAddresses, err := net.LookupHost(strings.TrimSuffix(question.Name, "."))
if err != nil {
log.Printf("Local lookup error: %s", err)
m.SetRcode(req, dns.RcodeServerFailure)
writeDNSResponse(w, m)
return
}
for _, IPAddress := range IPAddresses {
writeIPAddressResponse(&IPAddress, m, question.Name, question.Qtype)
}
}
}
writeDNSResponse(w, m)
}
func reverseLookupServer(w dns.ResponseWriter, req *dns.Msg) {
m := new(dns.Msg)
m.SetReply(req)
for _, question := range req.Question {
if question.Qtype != dns.TypePTR {
continue
}
ip := inAddrArpaToIPAddress(question.Name)
name, exists := nameCache[ip]
if exists {
writeNameResponse(m, question.Name, &name)
} else {
m.SetRcode(req, dns.RcodeRefused)
log.Printf("No match found for %s sending empty reply", question.Name)
}
}
writeDNSResponse(w, m)
}
func dnsStarted() {
log.Printf("DNS server started")
if startedChan != nil {
startedChan <- true
}
}
func startDNS(addr string, errors chan error) {
log.Printf("Trying to listen on %s", addr)
server := &dns.Server{Addr: addr, Net: "udp", NotifyStartedFunc: dnsStarted}
err := server.ListenAndServe()
if err != nil {
errors <- err
}
}
func myselfAsResolverErrorResponse(w dns.ResponseWriter, m *dns.Msg, name string) {
log.Printf("Oh my! I'm pointing to myself!")
rr := new(dns.A)
rr.Hdr = dns.RR_Header{Name: name, Rrtype: dns.TypeA, Class: dns.ClassINET, Ttl: 5}
rr.A = net.ParseIP("0.0.0.0").To4()
m.Answer = append(m.Answer, rr)
writeDNSResponse(w, m)
}
func loopbackSanityCheckResponse(w dns.ResponseWriter, req *dns.Msg) {
m := new(dns.Msg)
m.SetReply(req)
for _, question := range req.Question {
if question.Name == "dockerdns.loopbacktest." {
myselfAsResolverErrorResponse(w, m, question.Name)
return
}
}
writeDNSResponse(w, m)
}
var domain string
var nameCache map[string]string
var startedChan chan bool
func shutdownOnMonitorErrors(monitorErrors chan error) {
err := <-monitorErrors
log.Fatalf("Failed listening to docker events: %s", err)
}
func main() {
domain := flag.String("domain", "dev", "domain")
defaultDockerHost := os.Getenv("DOCKER_HOST")
if defaultDockerHost == "" {
defaultDockerHost = "tcp://docker:2375"
}
var dockerHost = flag.String("docker-host", defaultDockerHost, "docker host url, or set DOCKER_HOST")
var listenAddr = flag.String("listen-addr", ":5355", "Listen address for DNS")
var otherLookupsToLocalResolver = *flag.Bool("local-resolver", true, "Perform local gethostbyname queries for other domains")
flag.Parse()
log.Printf("Listen addr is %s", *listenAddr)
log.Printf("Resolving %s domain against docker container names at %s", *domain, *dockerHost)
var err error
docker, err = dockerclient.NewDockerClient(*dockerHost, nil)
if err != nil {
log.Fatalf("Failed creating docker client: %s", err)
}
_, err = docker.Version()
if err != nil {
log.Fatalf("Failed reading docker version: %s specify host with -docker-host", err)
}
mymain(*listenAddr, otherLookupsToLocalResolver, docker, *domain, nil)
}
func mymain(listenAddr string, otherLookupsToLocalResolver bool, mydocker myDockerClient, mydomain string, started chan bool) {
docker = mydocker
domain = mydomain
startedChan = started
nameCache = make(map[string]string)
dns.HandleFunc("in-addr.arpa", reverseLookupServer)
if otherLookupsToLocalResolver {
log.Println("Resolving other domains through system resolver")
dns.HandleFunc(".", localLookupServer)
dns.HandleFunc("loopbacktest", loopbackSanityCheckResponse)
}
dns.HandleFunc(domain, dockerNameLookupServer)
monitorErrors := make(chan error)
docker.StartMonitorEvents(cb, monitorErrors)
dnsListenErrors := make(chan error)
go startDNS(listenAddr, dnsListenErrors)
go registerShutdownSignalHandlers()
go refreshEntireCache()
go shutdownOnMonitorErrors(monitorErrors)
err := <-dnsListenErrors
log.Print(err)
}