forked from dougbw/coredns_omada
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathomada.go
101 lines (84 loc) · 2.42 KB
/
omada.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
package coredns_omada
import (
"context"
"sync"
"github.com/coredns/coredns/plugin"
"github.com/coredns/coredns/plugin/file"
"github.com/coredns/coredns/request"
omada "github.com/dougbw/go-omada"
"github.com/miekg/dns"
)
// Omada is the core struct of the omada plugin.
type Omada struct {
config config
controller omada.Controller
zoneNames []string
zones map[string]*file.Zone
zMu sync.RWMutex
Next plugin.Handler
}
func NewOmada(ctx context.Context, url string, u string, p string) (*Omada, error) {
omada := omada.New(url)
err := omada.GetControllerInfo()
if err != nil {
return nil, plugin.Error("omada", err)
}
zones := make(map[string]*file.Zone)
return &Omada{
controller: omada,
zones: zones,
}, nil
}
// ServeDNS implements the plugin.Handler interface.
func (o *Omada) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
state := request.Request{W: w, Req: r}
qname := state.Name()
qtype := state.QType()
log.Debugf("query; type: %d, name: %s\n", qtype, qname)
// this plugin can only handle 'A' and 'PTR' queries
var qzone string
switch qtype {
case 1: // A
qzone = qname
case 12: // PTR
qzone = getPtrParent(qname)
default:
return plugin.NextOrFailure(o.Name(), o.Next, ctx, w, r)
}
// check zone
log.Debugf("checking if zone is managed: %s", qzone)
zoneName := plugin.Zones(o.zoneNames).Matches(qzone)
if zoneName == "" {
log.Debugf("-- ❌ query is not in managed zones: %s\n", qname)
return plugin.NextOrFailure(o.Name(), o.Next, ctx, w, r)
}
log.Debugf("-- ✅ zone name: %s\n", zoneName)
m := new(dns.Msg)
m.SetReply(r)
m.Authoritative = true
var result file.Result
// lookup record in zones
o.zMu.RLock()
m.Answer, m.Ns, m.Extra, result = o.zones[zoneName].Lookup(ctx, state, qname)
o.zMu.RUnlock()
// no answer
if len(m.Answer) == 0 && result != file.NoData {
log.Debugf("-- ❌ answer len: %d, result: %v\n", len(m.Answer), result)
return plugin.NextOrFailure(o.Name(), o.Next, ctx, w, r)
}
log.Debugf("-- ✅ answer len: %d, result: %v\n", len(m.Answer), result)
switch result {
case file.Success:
case file.NoData:
case file.NameError:
m.Rcode = dns.RcodeNameError
case file.Delegation:
m.Authoritative = false
case file.ServerFailure:
log.Debugf("RcodeServerFailure")
return dns.RcodeServerFailure, nil
}
w.WriteMsg(m)
return dns.RcodeSuccess, nil
}
func (o *Omada) Name() string { return "omada" }