-
Notifications
You must be signed in to change notification settings - Fork 193
/
config.go
317 lines (255 loc) · 6.82 KB
/
config.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
package main
import (
"encoding/json"
"github.com/miekg/dns"
"io/ioutil"
"log"
"net"
"os"
"path"
"runtime/debug"
"sort"
"strconv"
"strings"
"time"
)
var configLastRead = map[string]time.Time{}
func configReader(dirName string, Zones Zones) {
go func() {
for {
configReadDir(dirName, Zones)
time.Sleep(5 * time.Second)
}
}()
}
func configReadDir(dirName string, Zones Zones) {
dir, err := ioutil.ReadDir(dirName)
if err != nil {
panic(err)
}
var seenFiles = map[string]bool{}
for _, file := range dir {
fileName := file.Name()
if !strings.HasSuffix(strings.ToLower(fileName), ".json") {
continue
}
seenFiles[fileName] = true
if lastRead, ok := configLastRead[fileName]; !ok || file.ModTime().After(lastRead) {
log.Println("Updated file, going to read", fileName)
configLastRead[fileName] = file.ModTime()
zoneName := fileName[0:strings.LastIndex(fileName, ".")]
//log.Println("FILE:", i, file, zoneName)
config, err := readZoneFile(zoneName, path.Join(dirName, fileName))
if config == nil || err != nil {
log.Println("error reading file: ", err)
}
if config != nil && err == nil {
Zones[zoneName] = config
dns.HandleFunc(zoneName, setupServerFunc(config))
}
}
// TODO(ask) Disable zones not seen in two subsequent runs
}
}
func readZoneFile(zoneName, fileName string) (*Zone, error) {
defer func() {
if err := recover(); err != nil {
log.Printf("reading %s failed: %s", zoneName, err)
debug.PrintStack()
}
}()
fh, err := os.Open(fileName)
if err != nil {
log.Println("Could not read ", fileName, ": ", err)
panic(err)
}
Zone := new(Zone)
Zone.Labels = make(labels)
Zone.LenLabels = dns.LenLabels(Zone.Origin)
Zone.Origin = zoneName
if err == nil {
var objmap map[string]interface{}
decoder := json.NewDecoder(fh)
err := decoder.Decode(&objmap)
if err != nil {
panic(err)
}
//log.Println(objmap)
var data map[string]interface{}
for k, v := range objmap {
//log.Printf("k: %s v: %#v, T: %T\n", k, v, v)
switch k {
case "ttl", "serial":
switch option := k; option {
case "ttl":
Zone.Options.Ttl = int(v.(float64))
case "serial":
Zone.Options.Serial = int(v.(float64))
}
continue
case "data":
data = v.(map[string]interface{})
}
}
setupZoneData(data, Zone)
}
//log.Printf("ZO T: %T %s\n", Zones["0.us"], Zones["0.us"])
//log.Println("IP", string(Zone.Regions["0.us"].IPv4[0].ip))
return Zone, nil
}
func setupZoneData(data map[string]interface{}, Zone *Zone) {
var recordTypes = map[string]uint16{
"a": dns.TypeA,
"aaaa": dns.TypeAAAA,
"ns": dns.TypeNS,
"cname": dns.TypeCNAME,
"alias": dns.TypeMF,
}
for dk, dv := range data {
//log.Printf("K %s V %s TYPE-V %T\n", dk, dv, dv)
Zone.Labels[dk] = new(Label)
label := Zone.Labels[dk]
label.Label = dk
// BUG(ask) Read 'ttl' value in label data
for rType, dnsType := range recordTypes {
var rdata = dv.(map[string]interface{})[rType]
if rdata == nil {
//log.Printf("No %s records for label %s\n", rType, dk)
continue
}
log.Printf("rdata %s TYPE-R %T\n", rdata, rdata)
records := make(map[string][]interface{})
switch rdata.(type) {
case map[string]interface{}:
// Handle NS map syntax, map[ns2.example.net:<nil> ns1.example.net:<nil>]
tmp := make([]interface{}, 0)
for rdata_k, rdata_v := range rdata.(map[string]interface{}) {
if rdata_v == nil {
rdata_v = ""
}
tmp = append(tmp, []string{rdata_k, rdata_v.(string)})
}
records[rType] = tmp
case string:
// CNAME and alias
tmp := make([]interface{}, 1)
tmp[0] = rdata.(string)
records[rType] = tmp
default:
records[rType] = rdata.([]interface{})
}
//log.Printf("RECORDS %s TYPE-REC %T\n", Records, Records)
if label.Records == nil {
label.Records = make(map[uint16]Records)
label.Weight = make(map[uint16]int)
}
label.Records[dnsType] = make(Records, len(records[rType]))
for i := 0; i < len(records[rType]); i++ {
//log.Printf("RT %T %#v\n", records[rType][i], records[rType][i])
record := new(Record)
var h dns.RR_Header
// log.Println("TTL OPTIONS", Zone.Options.Ttl)
h.Ttl = uint32(Zone.Options.Ttl)
h.Class = dns.ClassINET
h.Rrtype = dnsType
h.Name = label.Label + "." + Zone.Origin + "."
switch dnsType {
case dns.TypeA, dns.TypeAAAA:
rec := records[rType][i].([]interface{})
ip := rec[0].(string)
var err error
switch rec[1].(type) {
case string:
record.Weight, err = strconv.Atoi(rec[1].(string))
if err != nil {
panic("Error converting weight to integer")
}
label.Weight[dnsType] += record.Weight
case float64:
record.Weight = int(rec[1].(float64))
}
switch dnsType {
case dns.TypeA:
rr := &dns.RR_A{Hdr: h}
rr.A = net.ParseIP(ip)
if rr.A == nil {
panic("Bad A record")
}
record.RR = rr
case dns.TypeAAAA:
rr := &dns.RR_AAAA{Hdr: h}
rr.AAAA = net.ParseIP(ip)
if rr.AAAA == nil {
panic("Bad AAAA record")
}
record.RR = rr
}
case dns.TypeCNAME:
rec := records[rType][i]
rr := &dns.RR_CNAME{Hdr: h}
rr.Target = rec.(string)
record.RR = rr
case dns.TypeMF:
rec := records[rType][i]
rr := &dns.RR_MF{Hdr: h}
rr.Mf = rec.(string)
record.RR = rr
case dns.TypeNS:
rec := records[rType][i]
rr := &dns.RR_NS{Hdr: h}
switch rec.(type) {
case string:
rr.Ns = rec.(string)
case []string:
recl := rec.([]string)
log.Println("RECL:", recl)
rr.Ns = recl[0]
if len(recl[1]) > 0 {
log.Println("NS records with names syntax not supported")
}
default:
log.Printf("Data: %T %#v\n", rec, rec)
panic("Unrecognized NS format/syntax")
}
if h.Ttl < 43000 {
h.Ttl = 43200
}
record.RR = rr
default:
log.Println("type:", rType)
panic("Don't know how to handle this type")
}
if record.RR == nil {
panic("record.RR is nil")
}
label.Records[dnsType][i] = *record
}
if label.Weight[dnsType] > 0 {
sort.Sort(RecordsByWeight{label.Records[dnsType]})
}
}
}
setupSOA(Zone)
//log.Println(Zones[k])
}
func setupSOA(Zone *Zone) {
label := Zone.Labels[""]
primaryNs := "ns"
if record, ok := label.Records[dns.TypeNS]; ok {
primaryNs = record[0].RR.(*dns.RR_NS).Ns
}
s := Zone.Origin + ". 3600 IN SOA " +
primaryNs + " support.bitnames.com. " +
strconv.Itoa(Zone.Options.Serial) +
" 5400 5400 2419200 " +
strconv.Itoa(Zone.Options.Ttl)
log.Println("SOA: ", s)
rr, err := dns.NewRR(s)
if err != nil {
log.Println("SOA Error", err)
panic("Could not setup SOA")
}
record := Record{RR: rr}
label.Records[dns.TypeSOA] = make([]Record, 1)
label.Records[dns.TypeSOA][0] = record
}