From 941e0541bfc54f36c6119611261df030856865e6 Mon Sep 17 00:00:00 2001 From: Leonid Nikitin Date: Sun, 15 Mar 2026 15:46:59 +0500 Subject: [PATCH] Add Spamhaus JSON example to demonstrate blocklist usage --- examples/spamhaus.go | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 examples/spamhaus.go diff --git a/examples/spamhaus.go b/examples/spamhaus.go new file mode 100644 index 0000000..76bab7d --- /dev/null +++ b/examples/spamhaus.go @@ -0,0 +1,40 @@ +package main + +import ( + "encoding/json" + "fmt" + + "git.kor-elf.net/kor-elf-shield/blocklist" + "git.kor-elf.net/kor-elf-shield/blocklist/parser" +) + +/** + * An example of how to get a list of bad IP addresses from the service https://www.spamhaus.org/blocklists/do-not-route-or-peer/ + */ + +type lineJson struct { + IP string `json:"cidr"` +} + +func main() { + url := "https://www.spamhaus.org/drop/drop_v4.json" + //url := "https://www.spamhaus.org/drop/drop_v6.json" + pars, err := parser.NewJsonLines(func(item json.RawMessage) (string, error) { + var line lineJson + if err := json.Unmarshal(item, &line); err != nil { + return "", fmt.Errorf("unmarshal json item: %w", err) + } + return line.IP, nil + }) + if err != nil { + panic(err) + } + // limit 0 - no limit + limit := uint(0) + config := blocklist.NewConfig(limit) + ips, err := blocklist.Get(url, pars, config) + if err != nil { + panic(err) + } + fmt.Println(ips) +}