-
Notifications
You must be signed in to change notification settings - Fork 193
/
serve.go
147 lines (115 loc) · 3.28 KB
/
serve.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
package main
import (
"encoding/json"
"github.com/miekg/dns"
"log"
"os"
"strconv"
"strings"
"time"
)
func getQuestionName(z *Zone, req *dns.Msg) string {
lx := dns.SplitLabels(req.Question[0].Name)
ql := lx[0 : len(lx)-z.LenLabels]
return strings.ToLower(strings.Join(ql, "."))
}
var geoIP = setupGeoIP()
func serve(w dns.ResponseWriter, req *dns.Msg, z *Zone) {
qtype := req.Question[0].Qtype
logPrintf("[zone %s] incoming %s %s %d from %s\n", z.Origin, req.Question[0].Name,
dns.Rr_str[qtype], req.MsgHdr.Id, w.RemoteAddr())
// is this safe/atomic or does it need to go through a channel?
qCounter++
logPrintln("Got request", req)
label := getQuestionName(z, req)
var country string
if geoIP != nil {
country = geoIP.GetCountry(w.RemoteAddr().String())
logPrintln("Country:", country)
}
m := new(dns.Msg)
m.SetReply(req)
if e := m.IsEdns0(); e != nil {
m.SetEdns0(4096, e.Do())
}
m.Authoritative = true
// TODO(ask) Fix the findLabels API to make this work better
if alias := z.findLabels(label, "", dns.TypeMF); alias != nil &&
alias.Records[dns.TypeMF] != nil {
// We found an alias record, so pretend the question was for that name instead
label = alias.firstRR(dns.TypeMF).(*dns.RR_MF).Mf
}
labels := z.findLabels(label, country, qtype)
if labels == nil {
if label == "_status" && (qtype == dns.TypeANY || qtype == dns.TypeTXT) {
m.Answer = statusRR(z)
m.Authoritative = true
w.Write(m)
return
}
// return NXDOMAIN
m.SetRcode(req, dns.RcodeNameError)
m.Authoritative = true
m.Ns = []dns.RR{z.SoaRR()}
w.Write(m)
return
}
if servers := labels.Picker(qtype, 4); servers != nil {
var rrs []dns.RR
for _, record := range servers {
rr := record.RR
rr.Header().Name = req.Question[0].Name
rrs = append(rrs, rr)
}
m.Answer = rrs
}
if len(m.Answer) == 0 {
if labels := z.Labels[label]; labels != nil {
if _, ok := labels.Records[dns.TypeCNAME]; ok {
cname := labels.firstRR(dns.TypeCNAME)
m.Answer = append(m.Answer, cname)
}
} else {
m.Ns = append(m.Ns, z.SoaRR())
}
}
logPrintln(m)
err := w.Write(m)
if err != nil {
// if Pack'ing fails the Write fails. Return SERVFAIL.
log.Println("Error writing packet", m)
dns.HandleFailed(w, req)
}
return
}
func statusRR(z *Zone) []dns.RR {
h := dns.RR_Header{Ttl: 1, Class: dns.ClassINET, Rrtype: dns.TypeTXT}
h.Name = "_status." + z.Origin + "."
status := map[string]string{"v": VERSION, "id": serverId}
var hostname, err = os.Hostname()
if err == nil {
status["h"] = hostname
}
status["up"] = strconv.Itoa(int(time.Since(timeStarted).Seconds()))
status["qs"] = strconv.FormatUint(qCounter, 10)
js, err := json.Marshal(status)
return []dns.RR{&dns.RR_TXT{Hdr: h, Txt: []string{string(js)}}}
}
func setupServerFunc(Zone *Zone) func(dns.ResponseWriter, *dns.Msg) {
return func(w dns.ResponseWriter, r *dns.Msg) {
serve(w, r, Zone)
}
}
func listenAndServe(ip string, Zones *Zones) {
prots := []string{"udp", "tcp"}
for _, prot := range prots {
go func(p string) {
server := &dns.Server{Addr: ip, Net: p}
log.Printf("Opening on %s %s", ip, p)
if err := server.ListenAndServe(); err != nil {
log.Fatalf("geodns: failed to setup %s %s: %s", ip, p, err)
}
log.Fatalf("geodns: ListenAndServe unexpectedly returned")
}(prot)
}
}