-
Notifications
You must be signed in to change notification settings - Fork 2
/
dns.go
196 lines (154 loc) · 3.89 KB
/
dns.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
package main
import (
"fmt"
"log"
"net"
"github.com/miekg/dns"
)
func setupSOA() *dns.SOA {
s := *flagdomain + ". 3600 IN SOA " +
primaryNsList[0] + " hostmaster " +
"1 5400 5400 2419200 300"
rr, err := dns.NewRR(s)
if err != nil {
log.Println("SOA Error", err)
panic("Could not setup SOA")
}
return rr.(*dns.SOA)
}
func setupNS() []dns.RR {
var nsList []dns.RR
for _, ns := range primaryNsList {
s := *flagdomain + ". 20800 IN NS " + ns + "."
rr, err := dns.NewRR(s)
if err != nil {
log.Println("NS Error", err)
panic("Could not setup NS")
}
nsList = append(nsList, rr)
}
return nsList
}
func getEdnsSubNet(req *dns.Msg) (enum string, rr *dns.OPT, edns *dns.EDNS0_SUBNET) {
for _, extra := range req.Extra {
// log.Println("Extra:", extra)
for _, o := range extra.(*dns.OPT).Option {
// opt_rr = extra.(*dns.OPT)
switch e := o.(type) {
case *dns.EDNS0_NSID:
// do stuff with e.Nsid
case *dns.EDNS0_SUBNET:
// log.Println("Got edns", e.Address, e.Family, e.SourceNetmask, e.SourceScope)
if e.Address != nil {
edns = e
rr = extra.(*dns.OPT)
enum = fmt.Sprintf("%s/%d", e.Address.String(), e.SourceNetmask)
}
}
}
}
return
}
func setupServerFunc() func(dns.ResponseWriter, *dns.Msg) {
soa := setupSOA()
ns := setupNS()
h := &dns.RR_Header{Ttl: 5, Class: dns.ClassINET, Rrtype: dns.TypeA}
a := &dns.A{Hdr: *h, A: net.ParseIP(*flagip)}
hasACME := false
if len(*flagacmedomain) > 0 {
hasACME = true
if !dns.IsFqdn(*flagacmedomain) {
*flagacmedomain = *flagacmedomain + "."
}
}
return func(w dns.ResponseWriter, req *dns.Msg) {
m := new(dns.Msg)
m.SetReply(req)
if e := m.IsEdns0(); e != nil {
m.SetEdns0(4096, e.Do())
}
m.Authoritative = true
uuid := getUUIDFromDomain(req.Question[0].Name)
qtype := req.Question[0].Qtype
ednsIP, extraRR, edns := getEdnsSubNet(req)
ip, _, _ := net.SplitHostPort(w.RemoteAddr().String())
if edns != nil {
// log.Println("family", edns.Family)
if edns.Family != 0 {
edns.SourceScope = 0
m.Extra = append(m.Extra, extraRR)
}
}
if qtype == dns.TypeNS && len(uuid) == 0 {
m.Answer = ns
w.WriteMsg(m)
return
}
log.Printf("DNS request from %s for %s", ip, uuid)
if hasACME && uuid == "_acme-challenge" {
acmeCNAME := &dns.CNAME{
Hdr: dns.RR_Header{
Name: req.Question[0].Name,
Ttl: 600,
Rrtype: dns.TypeCNAME, Class: dns.ClassINET,
},
Target: *flagacmedomain,
}
m.Answer = []dns.RR{acmeCNAME}
w.WriteMsg(m)
return
}
// we only know how to do A records
if qtype != dns.TypeA {
m.Ns = []dns.RR{soa}
w.WriteMsg(m)
return
}
if len(uuid) == 0 {
// NOERROR
m.Ns = []dns.RR{soa}
w.WriteMsg(m)
return
}
if len(m.Answer) == 0 {
a.Header().Name = req.Question[0].Name
m.Answer = []dns.RR{a}
if uuid == "www" {
// we always redirect on 'www' so tell DNS caches
// it is good for a little longer and don't store
// the session
a.Header().Ttl = 120
} else {
// We expire the session data after 10 seconds, so
// encourage DNS caches to come back after 5.
a.Header().Ttl = 5
setCache(uuid, ip, ednsIP)
if edns != nil {
edns.SourceScope = edns.SourceNetmask
}
}
}
if len(m.Answer) == 0 {
// return NXDOMAIN
m.SetRcode(req, dns.RcodeNameError)
m.Authoritative = true
m.Ns = []dns.RR{soa}
}
// log.Println("Returning", m)
w.WriteMsg(m)
}
}
func listenAndServeDNS(ip string, port int) {
listen := fmt.Sprintf("%s:%d", ip, port)
prots := []string{"udp", "tcp"}
for _, prot := range prots {
go func(p string) {
server := &dns.Server{Addr: listen, Net: p}
log.Printf("DNS listen on %s %s", ip, p)
if err := server.ListenAndServe(); err != nil {
log.Fatalf("geodns: failed to setup dns %s %s: %s", ip, p, err)
}
log.Fatalf("geodns: ListenAndServe unexpectedly returned")
}(prot)
}
}