CoreDNS plugin
fabriziosalmi opened this issue · 1 comments
fabriziosalmi commented
-
Setup Environment for CoreDNS Plugin Development:
- Make sure you have Go installed on your machine.
- Clone the CoreDNS repository:
git clone https://github.com/coredns/coredns
-
Create Your Plugin:
- Navigate to the
plugin
directory of the cloned repository. - Create a new directory for your plugin, e.g.,
cd plugin && mkdir blacklist
. - In this directory, create a basic structure of files for your plugin, such as
setup.go
(for setup logic) andblacklist.go
(for core logic).
- Navigate to the
-
Develop the Plugin:
- Your plugin needs to periodically download the blacklist, parse it, and then use the FQDNs to block queries.
// blacklist.go package blacklist import ( "github.com/coredns/coredns/request" "github.com/miekg/dns" "net/http" "time" ) type Blacklist struct { Next plugin.Handler Blacklist map[string]bool URL string } func (b *Blacklist) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) { state := request.Request{W: w, Req: r} domain := state.Name() if _, found := b.Blacklist[domain]; found { m := new(dns.Msg) m.SetRcode(r, dns.RcodeNameError) w.WriteMsg(m) return dns.RcodeNameError, nil } return plugin.NextOrFailure(b.Name(), b.Next, ctx, w, r) } func (b *Blacklist) Name() string { return "blacklist" } func (b *Blacklist) UpdateBlacklist() { for { resp, err := http.Get(b.URL) if err != nil { // Handle error, maybe with some logging. continue } newBlacklist := make(map[string]bool) scanner := bufio.NewScanner(resp.Body) for scanner.Scan() { line := scanner.Text() newBlacklist[line] = true } b.Blacklist = newBlacklist resp.Body.Close() time.Sleep(24 * time.Hour) // Update every 24 hours. } }
// setup.go package blacklist import ( "github.com/coredns/coredns/core/dnsserver" "github.com/coredns/coredns/plugin" clog "github.com/coredns/coredns/plugin/pkg/log" "github.com/mholt/caddy" ) var log = clog.NewWithPlugin("blacklist") func init() { plugin.Register("blacklist", setup) } func setup(c *caddy.Controller) error { b := &Blacklist{ URL: "https://github.com/fabriziosalmi/blacklists/releases/download/latest/blacklist.txt", } go b.UpdateBlacklist() dnsserver.GetConfig(c).AddPlugin(func(next plugin.Handler) plugin.Handler { b.Next = next return b }) return nil }
-
Update CoreDNS to Recognize Your Plugin:
- Add your plugin to
plugin.cfg
in the root of the CoreDNS repo:blacklist:github.com/coredns/coredns/plugin/blacklist
.
- Add your plugin to
-
Compile CoreDNS with Your Plugin:
- From the root of the CoreDNS repository, run:
make
- From the root of the CoreDNS repository, run:
-
Configuration:
- In your
Corefile
, simply useblacklist
.
- In your
-
Run Your Custom CoreDNS Build:
- Use the built binary to start CoreDNS.
fabriziosalmi commented