forked from partkyle/docker-dns
/
main.go
164 lines (139 loc) · 3.59 KB
/
main.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
package main
import (
"flag"
"fmt"
"net"
"os"
"path"
"strings"
docker "github.com/fsouza/go-dockerclient"
log "github.com/Sirupsen/logrus"
"github.com/miekg/dns"
)
var (
network = flag.String("net", "udp", "network type (tcp/udp)")
addr = flag.String("addr", ":53", "addr to bind to")
domain = flag.String("domain", ".docker", "domain to listen for")
)
type DockerClient interface {
InspectContainer(string) (*docker.Container, error)
ListContainers(docker.ListContainersOptions) ([]docker.APIContainers, error)
}
type Handler struct {
docker DockerClient
domain string
}
func (h *Handler) ServeDNS(w dns.ResponseWriter, r *dns.Msg) {
reply := &dns.Msg{}
reply.SetReply(r)
answer := make([]dns.RR, 0)
lookup:
for _, question := range r.Question {
log.WithField("type", question.Qtype).WithField("name", question.Name).Info("resolving dns")
switch question.Qtype {
case dns.TypeA:
container, err := h.docker.InspectContainer(strings.TrimSuffix(question.Name, h.domain))
if err != nil {
log.Error(err)
continue
}
a := &dns.A{
Hdr: dns.RR_Header{
Name: question.Name,
Rrtype: dns.TypeA,
Class: dns.ClassINET,
Ttl: 0,
},
A: net.ParseIP(container.NetworkSettings.IPAddress),
}
answer = append(answer, a)
case dns.TypeMX:
// if there is no container running, we shoudn't return a record
_, err := h.docker.InspectContainer(strings.TrimSuffix(question.Name, h.domain))
if err != nil {
log.Error(err)
continue
}
mx := &dns.MX{
Hdr: dns.RR_Header{
Name: question.Name,
Rrtype: dns.TypeMX,
Class: dns.ClassINET,
Ttl: 0,
},
Preference: 10,
Mx: question.Name,
}
answer = append(answer, mx)
case dns.TypePTR:
containers, err := h.docker.ListContainers(docker.ListContainersOptions{})
if err != nil {
log.Error(err)
continue
}
// these look like: 73.6.17.172.in-addr.arpa.
reverseIP := strings.TrimSuffix(question.Name, ".in-addr.arpa.")
reverseParts := strings.Split(reverseIP, ".")
parts := make([]string, len(reverseParts))
for i := range parts {
parts[i] = reverseParts[len(reverseParts)-i-1]
}
ip := strings.Join(parts, ".")
log.WithField("ip", ip).Info("rDNS lookup")
for _, c := range containers {
container, err := h.docker.InspectContainer(c.ID)
if err != nil {
log.Error(err)
continue
}
if container.NetworkSettings.IPAddress == ip {
ptr := &dns.PTR{
Hdr: dns.RR_Header{
Name: question.Name,
Rrtype: dns.TypePTR,
Class: dns.ClassINET,
Ttl: 0,
},
Ptr: strings.TrimPrefix(container.Name, "/") + h.domain,
}
answer = append(answer, ptr)
continue lookup
}
}
}
}
reply.Answer = answer
err := w.WriteMsg(reply)
if err != nil {
log.Error(err)
}
}
func main() {
flag.Parse()
log.SetOutput(os.Stdout)
log.SetLevel(log.DebugLevel)
var client *docker.Client
if os.Getenv("DOCKER_HOST") != "" {
var err error
certPath := os.Getenv("DOCKER_CERT_PATH")
client, err = docker.NewTLSClient(os.Getenv("DOCKER_HOST"), path.Join(certPath, "cert.pem"), path.Join(certPath, "key.pem"), path.Join(certPath, "ca.pem"))
if err != nil {
log.Fatal(err)
}
} else {
var err error
client, err = docker.NewClient("unix:///var/run/docker.sock")
if err != nil {
log.Fatal(err)
}
}
handler := Handler{docker: client, domain: fmt.Sprintf("%s.", *domain)}
server := dns.Server{}
server.Handler = &handler
server.Net = *network
server.Addr = *addr
err := server.ListenAndServe()
if err != nil {
log.Fatal(err)
}
}