fabriziosalmi / blacklists

Hourly updated domains blacklist 🚫
https://github.com/fabriziosalmi/blacklists/releases/download/latest/blacklist.txt
GNU General Public License v3.0
117 stars 5 forks source link

CoreDNS plugin #79

Closed fabriziosalmi closed 6 months ago

fabriziosalmi commented 11 months ago
  1. 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
  2. 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) and blacklist.go (for core logic).
  3. 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
    }
  4. 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.
  5. Compile CoreDNS with Your Plugin:

    • From the root of the CoreDNS repository, run: make
  6. Configuration:

    • In your Corefile, simply use blacklist.
  7. Run Your Custom CoreDNS Build:

    • Use the built binary to start CoreDNS.
fabriziosalmi commented 6 months ago

moved to docs: https://github.com/fabriziosalmi/blacklists/wiki/Documentation#coredns