load blackhole files

This commit is contained in:
core 2023-10-03 13:54:57 -04:00
parent d7e5b7d062
commit b11d4ede6c
Signed by: core
GPG Key ID: FDBF740DADDCEECF
5 changed files with 147 additions and 0 deletions

15
blackhole.go Normal file
View File

@ -0,0 +1,15 @@
package pancheri
import "slices"
type BlackholeFile struct {
DenyDomains []string `yaml:"deny_domains"`
}
type Blackholer struct {
DenyDomains []string
}
func (b *Blackholer) ShouldBlock(domain string) bool {
return slices.Contains(b.DenyDomains, domain)
}

View File

@ -0,0 +1,81 @@
package main
import (
"flag"
"fmt"
"git.e3t.cc/e3team/pancheri"
"gopkg.in/yaml.v2"
"io"
"os"
"strings"
"time"
)
func main() {
hostsPath := flag.String("hostsfile", "", "Hosts file to convert to a dnsbl")
voidIp := flag.String("voidip", "0.0.0.0", "IP that this hosts file uses as a void IP")
printUsage := flag.Bool("help", false, "Print command line usage")
flag.Parse()
if *printUsage {
flag.Usage()
os.Exit(0)
}
f, err := os.Open(*hostsPath)
if err != nil {
fmt.Printf("error opening hosts file: %s", err)
os.Exit(1)
}
buf := new(strings.Builder)
_, err = io.Copy(buf, f)
if err != nil {
fmt.Printf("error reading hosts file: %s", err)
os.Exit(1)
}
err = f.Close()
if err != nil {
fmt.Printf("error closing hosts file: %s", err)
os.Exit(1)
}
split := strings.Split(buf.String(), "\n")
blackhole := pancheri.BlackholeFile{
DenyDomains: *new([]string),
}
for _, line := range split {
if strings.HasPrefix(line, "#") {
continue
}
if line == "" {
continue
}
if !strings.HasPrefix(line, *voidIp) {
continue
}
lineSplit := strings.Split(line, " ")
if strings.Join(lineSplit[1:], " ") == *voidIp {
continue
}
blackhole.DenyDomains = append(blackhole.DenyDomains, strings.Join(lineSplit[1:], " "))
}
marshaled, err := yaml.Marshal(blackhole)
if err != nil {
fmt.Printf("error saving blacklist file: %s", err)
os.Exit(1)
}
fmt.Printf("# Compiled by pancheri-compile at %s\n", time.Now().Format(time.RFC3339))
fmt.Printf("# %d hosts loaded from %s\n", len(blackhole.DenyDomains), *hostsPath)
fmt.Printf("%s", marshaled)
}

View File

@ -6,6 +6,7 @@ import (
"git.e3t.cc/e3team/pancheri" "git.e3t.cc/e3team/pancheri"
"github.com/miekg/dns" "github.com/miekg/dns"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
"os" "os"
) )
@ -65,6 +66,51 @@ func main() {
r = pancheri.NewResolver(c.Resolver.Upstream) r = pancheri.NewResolver(c.Resolver.Upstream)
} }
var b *pancheri.Blackholer
if c.Blackhole.Enable {
logrus.WithFields(logrus.Fields{
"denyfiles": len(c.Blackhole.BlockLists),
}).Info("enabling blackholer")
b = &pancheri.Blackholer{
DenyDomains: *new([]string),
}
for _, file := range c.Blackhole.BlockLists {
logrus.WithFields(logrus.Fields{
"file": file,
}).Info("loading blocklist")
f, err := os.Open(file)
if err != nil {
logrus.Errorf("error loading blocklist file: %s", err)
}
var cfg pancheri.BlackholeFile
decoder := yaml.NewDecoder(f)
err = decoder.Decode(&cfg)
if err != nil {
logrus.Errorf("error decoding blocklist file: %s", err)
}
err = f.Close()
if err != nil {
logrus.Errorf("error closing blocklist file: %s", err)
}
b.DenyDomains = append(b.DenyDomains, cfg.DenyDomains...)
logrus.WithFields(logrus.Fields{
"file": file,
}).Infof("loaded %d hosts", len(cfg.DenyDomains))
}
logrus.WithFields(logrus.Fields{
"hosts": len(b.DenyDomains),
}).Info("blackhole enabled")
}
handler := pancheri.Handler{ handler := pancheri.Handler{
C: c, C: c,
R: r, R: r,

View File

@ -22,6 +22,10 @@ type Config struct {
Zone struct { Zone struct {
LoadFiles []string `yaml:"load_files"` LoadFiles []string `yaml:"load_files"`
} `yaml:"zone"` } `yaml:"zone"`
Blackhole struct {
Enable bool `yaml:"enable"`
BlockLists []string `yaml:"block_lists"`
}
} }
func LoadConfig(path string) (*Config, error) { func LoadConfig(path string) (*Config, error) {

View File

@ -10,6 +10,7 @@ type Handler struct {
C *Config C *Config
R *Resolver R *Resolver
A *Authority A *Authority
B *Blackholer
} }
func (h *Handler) ServeDNS(w dns.ResponseWriter, r *dns.Msg) { func (h *Handler) ServeDNS(w dns.ResponseWriter, r *dns.Msg) {