forked from abh/geodns-config
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dns.go
179 lines (152 loc) · 4.08 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
package dnsconfig
import (
"encoding/json"
"fmt"
"log"
"sort"
)
type ZoneLogging struct {
StatHat bool `json:"stathat"`
StatHatAPI string `json:"stathat_api"`
}
type zoneData map[string]*zoneLabel
type zoneJson struct {
Data zoneData `json:"data"`
Ttl int `json:"ttl"`
MaxHosts int `json:"max_hosts"`
Logging ZoneLogging `json:"logging"`
}
type jsonAddresses []interface{}
type zoneLabel struct {
Ns map[string]string `json:"ns,omitempty"`
Cname string `json:"cname,omitempty"`
Alias string `json:"alias,omitempty"`
A jsonAddresses `json:"a,omitempty"`
Aaaa jsonAddresses `json:"aaaa,omitempty"`
}
func (a jsonAddresses) Less(i, j int) bool {
// Really this should sort on thee IP address bytes, but this is good enough
// as we just need something to make them be in a consistent order
return a[i].([]interface{})[0].(string) < a[j].([]interface{})[0].(string)
}
func (s jsonAddresses) Len() int { return len(s) }
func (s jsonAddresses) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (z *Zone) BuildJSON() (string, error) {
zd, err := z.BuildZone()
if err != nil {
return "", err
}
js, err := zd.JSON()
if err != nil {
return "", err
}
return js, nil
}
func (js *zoneJson) JSON() (string, error) {
b, err := json.MarshalIndent(js, "", " ")
if err != nil {
return "", err
}
return string(b), nil
}
func (jd *zoneData) sortRecords() {
for _, v := range *jd {
sort.Sort(v.A)
}
}
func (z *Zone) BuildZone() (*zoneJson, error) {
// log.Println("BuildZone", spew.Sdump(z))
js := zoneJson{Data: zoneData{}}
js.MaxHosts = z.Options.MaxHosts
js.Ttl = z.Options.Ttl
js.Logging = z.Logging
js.Data[""] = new(zoneLabel)
js.Data[""].Ns = map[string]string{}
for _, ns := range z.Ns {
js.Data[""].Ns[ns] = ""
}
displayQueue := make([]string, 0)
for _, labelData := range z.Labels.All() {
if len(labelData.GroupName) > 0 {
label := new(zoneLabel)
label.Alias = labelData.GroupName
js.Data[labelData.Name] = label
continue
}
for _, labelNode := range labelData.LabelNodes {
if labelNode.Active == false {
log.Printf("Node '%s' is inactive in label '%s'\n", labelNode.Name, labelData.Name)
continue
}
node := z.Nodes.Get(labelNode.Name)
if node == nil {
log.Printf("Node '%s' not configured in master nodes config\n", labelNode.Name)
continue
}
geos := z.GeoMap.GetNodeGeos(labelNode.Name)
// Don't warn if there are no targets for the inactive node
if node.Active == false && len(geos) > 0 {
log.Printf("Node '%s' is inactive (used in '%s')\n", labelNode.Name, labelData.Name)
continue
}
for _, geo := range geos {
var geoName string
if geo.target == "@" {
geoName = labelData.Name
} else {
if len(labelData.Name) > 0 {
geoName = labelData.Name + "." + geo.target
} else {
geoName = geo.target
}
}
if _, ok := js.Data[geoName]; !ok {
js.Data[geoName] = new(zoneLabel)
}
ip := labelNode.IP
if ip == nil {
ip = node.Ip
}
trg := []interface{}{ip.String(), geo.weight}
js.Data[geoName].A = append(js.Data[geoName].A, trg)
fn := func(slice []string, s string) []string {
for _, e := range slice {
if e == s {
return slice
}
}
return append(slice, s)
}
displayQueue = fn(displayQueue, geoName)
}
}
if d, ok := js.Data[labelData.Name]; ok {
if len(d.A) == 0 {
log.Println("No global A records for", labelData.Name)
}
} else {
log.Println("No global A records for", labelData.Name)
}
displayQueue = append(displayQueue, "")
}
js.Data.sortRecords()
if z.Verbose {
for _, geoName := range displayQueue {
if geoName == "" {
fmt.Println("")
continue
}
fmt.Printf("%-40s: ", geoName)
for i, a := range js.Data[geoName].A {
// fmt.Printf("%#v\n%s\n", a, spew.Sdump(a))
fmt.Printf("%-15s/%-4d", a.([]interface{})[0].(string), a.([]interface{})[1].(int))
if i == len(js.Data[geoName].A)-1 {
fmt.Printf("\n")
} else {
fmt.Printf(" | ")
}
}
}
}
return &js, nil
}