-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexampleplugin.go
81 lines (61 loc) · 1.57 KB
/
exampleplugin.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
package main
import (
"context"
"net"
"github.com/miekg/dns"
"github.com/semihalev/log"
"github.com/semihalev/sdns/config"
"github.com/semihalev/sdns/middleware"
)
// Example type
type Example struct {
cfg map[string]interface{}
plog log.Logger
}
// New return example
func New(cfg *config.Config) middleware.Handler {
plog := log.New("plugin", name)
plog.Info("It's example plugin, loaded success")
return &Example{
cfg: cfg.Plugins[name].Config,
plog: plog,
}
}
// Name return middleware name
func (e *Example) Name() string { return name }
// ServeDNS implements the Handle interface.
func (e *Example) ServeDNS(ctx context.Context, ch *middleware.Chain) {
/*
$ dig value_1 @127.0.0.1
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 4718
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 1232
;; QUESTION SECTION:
;value_1. IN A
;; ANSWER SECTION:
value_1. 3600 IN A 0.0.0.0
*/
req, w := ch.Request, ch.Writer
testValue := dns.Fqdn(e.cfg["key_1"].(string))
q := req.Question[0]
if q.Name == testValue && q.Qtype == dns.TypeA {
msg := new(dns.Msg)
msg.SetReply(req)
msg.Authoritative, msg.RecursionAvailable = true, true
rrHeader := dns.RR_Header{
Name: testValue,
Rrtype: dns.TypeA,
Class: dns.ClassINET,
Ttl: 3600,
}
a := &dns.A{Hdr: rrHeader, A: net.IPv4zero}
msg.Answer = append(msg.Answer, a)
w.WriteMsg(msg)
ch.Cancel()
e.plog.Info("Example request received")
}
ch.Next(ctx)
}
const name = "example"