-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbogus.go
53 lines (41 loc) · 1.06 KB
/
bogus.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
package bogus
import (
"context"
"net"
"github.com/coredns/coredns/core/dnsserver"
"github.com/coredns/coredns/plugin"
"github.com/coredns/caddy"
"github.com/miekg/dns"
)
// N implements the plugin interface.
type N struct {
Next plugin.Handler
ips []net.IP
}
func init() { plugin.Register("bogus", setup) }
func setup(c *caddy.Controller) error {
ips := []net.IP{}
for c.Next() {
args := c.RemainingArgs()
for _, a := range args {
ips = append(ips, net.ParseIP(a))
}
}
dnsserver.GetConfig(c).AddPlugin(func(next plugin.Handler) plugin.Handler {
return N{Next: next, ips: ips}
})
c.OnStartup(func() error {
return nil
})
return nil
}
// ServeDNS implements the plugin.Handler interface.
func (n N) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
wr := NewResponseReverter(w, r, n.ips)
if len(n.ips) == 0 {
return plugin.NextOrFailure(n.Name(), n.Next, ctx, w, r)
}
return plugin.NextOrFailure(n.Name(), n.Next, ctx, wr, r)
}
// Name implements the Handler interface.
func (n N) Name() string { return "bogus" }