-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathomada.go
More file actions
111 lines (94 loc) · 2.75 KB
/
omada.go
File metadata and controls
111 lines (94 loc) · 2.75 KB
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
package coredns_omada
import (
"context"
"sync"
"github.com/coredns/coredns/plugin"
"github.com/coredns/coredns/plugin/file"
"github.com/coredns/coredns/plugin/pkg/fall"
"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
sites []string
zoneNames []string
zones map[string]*file.Zone
zMu sync.RWMutex
records map[string]DnsRecords
Next plugin.Handler
Fall fall.F
}
func NewOmada(ctx context.Context, url string, u string, p string) (*Omada, error) {
omada := omada.New(url)
zones := make(map[string]*file.Zone)
records := make(map[string]DnsRecords)
return &Omada{
controller: omada,
zones: zones,
records: records,
}, nil
}
const ptrZone string = "in-addr.arpa."
// 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', 'PTR' and 'SOA' queries
var qzone string
switch qtype {
case 1: // A
qzone = qname
case 6: // SOA
qzone = qname
case 12: // PTR
qzone = ptrZone
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 && o.Fall.Through(qname) {
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:
log.Debugf("-- RcodeNameError")
m.Rcode = dns.RcodeNameError
case file.Delegation:
m.Authoritative = false
case file.ServerFailure:
log.Debugf("-- RcodeServerFailure")
return dns.RcodeServerFailure, nil
}
err := w.WriteMsg(m)
if err != nil {
log.Debugf("-- error writing message: %v\n", err)
return dns.RcodeServerFailure, err
}
return dns.RcodeSuccess, nil
}
func (o *Omada) Name() string { return "omada" }