summaryrefslogtreecommitdiff
path: root/internal/blocklist/blocklist.go
diff options
context:
space:
mode:
authorradhitya <alif@radhitya.org>2026-07-05 14:05:24 +0700
committerradhitya <alif@radhitya.org>2026-07-05 14:05:24 +0700
commit1799ef47cedcfed8c979e145783fff7c9d1944cc (patch)
tree4634846c9035339d03f783543ac83056686a9f5b /internal/blocklist/blocklist.go
parent52a0d3845ead07f840e9e99cb4a8c3507c84ab30 (diff)
foreward upstream, in memory cache, udp listener, handler, cli flags
Diffstat (limited to 'internal/blocklist/blocklist.go')
-rw-r--r--internal/blocklist/blocklist.go53
1 files changed, 53 insertions, 0 deletions
diff --git a/internal/blocklist/blocklist.go b/internal/blocklist/blocklist.go
new file mode 100644
index 0000000..34a456d
--- /dev/null
+++ b/internal/blocklist/blocklist.go
@@ -0,0 +1,53 @@
+package blocklist
+
+import (
+ "bufio"
+ "os"
+ "strings"
+)
+
+type Blocklist struct {
+ domains map[string]struct{}
+}
+
+func Load(path string) (*Blocklist, error) {
+ bl := &Blocklist{domains: make(map[string]struct{})}
+ f, err := os.Open(path)
+ if err != nil {
+ if os.IsNotExist(err) {
+ return bl, nil
+ }
+ return nil, err
+ }
+ defer f.Close()
+
+ sc := bufio.NewScanner(f)
+ for sc.Scan() {
+ line := strings.TrimSpace(sc.Text())
+ if line == "" || line[0] == '#' {
+ continue
+ }
+ line = strings.ToLower(line)
+ line = strings.TrimSuffix(line, ".")
+ bl.domains[line] = struct{}{}
+ }
+ return bl, sc.Err()
+}
+
+func (bl *Blocklist) Blocked(qname string) bool {
+ qname = strings.ToLower(qname)
+ qname = strings.TrimSuffix(qname, ".")
+
+ if _, ok := bl.domains[qname]; ok {
+ return true
+ }
+
+ for i := 0; i < len(qname); i++ {
+ if qname[i] == '.' {
+ if _, ok := bl.domains[qname[i+1:]]; ok {
+ return true
+ }
+ }
+ }
+ return false
+}