forked from dougbw/coredns_omada
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate.go
225 lines (185 loc) · 6.55 KB
/
update.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
package coredns_omada
import (
"context"
"fmt"
"net"
"time"
"github.com/coredns/coredns/plugin/file"
"github.com/miekg/dns"
)
// Run starts the update loops which:
// - refresh login session token
// - update the dns zones
func (o *Omada) updateLoop(ctx context.Context) error {
// update zones
go updateSessionLoop(ctx, o)
// update zones
go updateZoneLoop(ctx, o)
return nil
}
func updateZoneLoop(ctx context.Context, o *Omada) {
delay := time.Duration(o.config.refresh_minutes) * time.Minute
timer := time.NewTimer(delay)
defer timer.Stop()
for {
timer.Reset(delay)
select {
case <-ctx.Done():
log.Debugf("Breaking out of zone update loop: %v", ctx.Err())
return
case <-timer.C:
if err := o.updateZones(ctx); err != nil && ctx.Err() == nil {
log.Errorf("Failed to update zones: %v", err)
}
}
}
}
func updateSessionLoop(ctx context.Context, o *Omada) {
// delay := 24 * time.Hour
delay := time.Duration(o.config.refresh_login_hours) * time.Hour
timer := time.NewTimer(delay)
defer timer.Stop()
for {
timer.Reset(delay)
select {
case <-ctx.Done():
log.Debugf("Breaking out of login update loop: %v", ctx.Err())
return
case <-timer.C:
if err := o.login(ctx); err != nil && ctx.Err() == nil {
log.Errorf("Failed to login to controller : %v", err)
}
}
}
}
// login to the controller
func (o *Omada) login(ctx context.Context) error {
log.Info("logging in...")
u := o.config.Username
p := o.config.Password
s := o.config.Site
err := o.controller.Login(u, p, s)
if err != nil {
return err
}
return nil
}
// update dns zones
func (o *Omada) updateZones(ctx context.Context) error {
log.Info("update: updating zones...")
zones := make(map[string]*file.Zone)
networks, err := o.controller.GetAllNetworks()
if err != nil {
return fmt.Errorf("updateZones: error GetAllNetworks from omada controller: %w", err)
}
log.Debugf("updateZones: GetAllNetworks found '%d' omada networks\n", len(networks))
clients, err := o.controller.GetAllClients()
if err != nil {
return fmt.Errorf("updateZones: error calling GetAllClients from omada controller: %w", err)
}
log.Debugf("updateZones: GetAllClients found '%d' omada clients\n", len(clients))
devices, err := o.controller.GetAllDevices()
if err != nil {
return fmt.Errorf("updateZones: error calling GetAllDevices from omada controller: %w", err)
}
log.Debugf("updateZones: GetAllDevices found '%d' omada devices\n", len(devices))
log.Debugf("******************** START CLIENT LIST **************************\n")
for _, client := range clients {
log.Debugf("Name: %s, DNS name: %s, Hostname: %s, IP: %s, MAC: %s\n", client.Name, client.DnsName, client.HostName, client.Ip, client.MAC)
}
log.Debugf("******************** END CLIENT LIST **************************\n")
log.Debugf("******************** ================ **************************\n")
log.Debugf("******************** START DEVICES LIST **************************\n")
for _, device := range devices {
log.Debugf("Name: %s, DNS name: %s, Model: %s, IP: %s, MAC: %s, Site: %s\n", device.Name, device.DnsName, device.Model, device.IP, device.Mac, device.Site)
}
log.Debugf("******************** END DEVICES LIST **************************\n")
log.Debugf("******************** ================ **************************\n")
log.Debugf("******************** START NETWORK LIST **************************\n")
for _, network := range networks {
log.Debugf("Domain: %s, ID: %s, Name: %s, Subnet: %s\n", network.Domain, network.Id, network.Name, network.Subnet)
}
log.Debugf("******************** END NETWORK LIST **************************\n")
// reverse zones
log.Debugf("******************** CREATE ZONES **************************\n")
for _, network := range networks {
dnsDomain := network.Domain
_, subnet, _ := net.ParseCIDR(network.Subnet)
log.Debugf("ParseCIDR subnet found: %s\n", subnet)
for _, client := range clients {
// get PTR zone
ptrZone := getParentPtrZoneFromIp(client.Ip)
log.Debugf("parentPtrZoneFromIp: %s for IP: %s\n", ptrZone, client.Ip)
// create PTR zone
_, ok := zones[ptrZone]
if !ok {
log.Debugf("update ptr: creating PTR zone: %s", ptrZone)
zones[ptrZone] = file.NewZone(ptrZone, "")
addSoaRecord(zones[ptrZone], ptrZone)
}
// if client is in this networks subnet then we can determine the fqdn
// and create ptr record
ip := net.ParseIP(client.Ip)
if subnet.Contains(ip) {
ptrName := getPtrZoneFromIp(client.Ip)
ptrRecord := fmt.Sprintf("%s.%s", client.DnsName, dnsDomain)
ptr := &dns.PTR{Hdr: dns.RR_Header{Name: ptrName, Rrtype: dns.TypePTR, Class: dns.ClassINET, Ttl: 60},
Ptr: dns.Fqdn(ptrRecord)}
log.Debugf("update ptr: -- adding record to zone: %s, %s", ptrRecord, ptrZone)
zones[ptrZone].Insert(ptr)
}
}
}
// forward zones
for _, network := range networks {
log.Debugf("update: -- processing network: %s", network.Name)
// skip network if no dns search domain is set
if network.Domain == "" {
log.Debugf("update: skipping network: %s because not DNS search domain is set", network.Name)
continue
}
dnsDomain := network.Domain + "."
// create zone
_, ok := zones[dnsDomain]
if !ok {
log.Debugf("update: creating zone: %s", dnsDomain)
zones[dnsDomain] = file.NewZone(dnsDomain, "")
addSoaRecord(zones[dnsDomain], dnsDomain)
}
// add client records to zone
// todo: if o.config.resolve_client_names...
log.Debugf("update: adding records to zone: %s\n", dnsDomain)
_, subnet, _ := net.ParseCIDR(network.Subnet)
for _, client := range clients {
// if client is in this networks subnet then add record to zone
ip := net.ParseIP(client.Ip)
if subnet.Contains(ip) {
clientFqdn := fmt.Sprintf("%s.%s", client.DnsName, dnsDomain)
a := &dns.A{Hdr: dns.RR_Header{Name: clientFqdn, Rrtype: dns.TypeA, Class: dns.ClassINET, Ttl: 60},
A: net.ParseIP(client.Ip)}
zones[dnsDomain].Insert(a)
}
}
// add device records to zone
for _, device := range devices {
ip := net.ParseIP(device.IP)
if subnet.Contains(ip) {
deviceFqdn := fmt.Sprintf("%s.%s", device.DnsName, dnsDomain)
a := &dns.A{Hdr: dns.RR_Header{Name: deviceFqdn, Rrtype: dns.TypeA, Class: dns.ClassINET, Ttl: 60},
A: net.ParseIP(device.IP)}
zones[dnsDomain].Insert(a)
}
}
log.Debugf("update: zone %s contains %d records", dnsDomain, zones[dnsDomain].Count)
}
// get list of zone names
zoneNames := make([]string, 0, len(zones))
for k := range zones {
zoneNames = append(zoneNames, k)
}
o.zMu.Lock()
o.zones = zones
o.zoneNames = zoneNames
o.zMu.Unlock()
return nil
}