summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorradhitya <alif@radhitya.org>2026-06-14 22:24:54 +0700
committerradhitya <alif@radhitya.org>2026-06-14 22:24:54 +0700
commite05835493f821055e517a3988c6f9256abbc5c24 (patch)
tree41c8f393df0a744e95d1eb7a91dc83f28d834941
parent55d452e8cc8e782345ab36f2f5e57a45068067f1 (diff)
fix block domain
-rw-r--r--internal/blocklist/blocklist.go14
-rw-r--r--internal/server/handler.go8
-rw-r--r--main.go4
3 files changed, 12 insertions, 14 deletions
diff --git a/internal/blocklist/blocklist.go b/internal/blocklist/blocklist.go
index 364c492..f6e0592 100644
--- a/internal/blocklist/blocklist.go
+++ b/internal/blocklist/blocklist.go
@@ -94,16 +94,16 @@ func (b *Blocklist) parseRule(line string, blocked, exceptions *trie) bool {
return false
}
func (b *Blocklist) load(scanner *bufio.Scanner) error {
- blocked := newTrie()
- exceptions := newTrie()
- var n int32
+ b.mu.Lock()
+ defer b.mu.Unlock()
+ var n int32
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if line == "" || line[0] == '#' || line[0] == '!' {
continue
}
- if b.parseRule(line, blocked, exceptions) {
+ if b.parseRule(line, b.blocked, b.exceptions) {
n++
}
}
@@ -111,11 +111,7 @@ func (b *Blocklist) load(scanner *bufio.Scanner) error {
return err
}
- b.mu.Lock()
- b.blocked = blocked
- b.exceptions = exceptions
- b.mu.Unlock()
- atomic.StoreInt32(&b.TotalRules, n)
+ b.TotalRules += n
return nil
}
func (b *Blocklist) LoadURL(url string) error {
diff --git a/internal/server/handler.go b/internal/server/handler.go
index e996270..5f873d4 100644
--- a/internal/server/handler.go
+++ b/internal/server/handler.go
@@ -43,6 +43,10 @@ func (s *Server) buildResponse(req *dns.Msg) (*dns.Msg, bool) {
}
q := req.Question[0]
+ if s.blocklist != nil && s.blocklist.IsBlocked(q.Name) {
+ return s.blockedResponse(req), true
+ }
+
if s.cache != nil {
key := cache.Key{Name: q.Name, Qtype: q.Qtype, Class: q.Qclass}
if cached, ok := s.cache.Get(key); ok {
@@ -51,10 +55,6 @@ func (s *Server) buildResponse(req *dns.Msg) (*dns.Msg, bool) {
}
}
- if s.blocklist != nil && s.blocklist.IsBlocked(q.Name) {
- return s.blockedResponse(req), true
- }
-
ctx, cancel := context.WithTimeout(s.baseCtx, 10*time.Second)
defer cancel()
diff --git a/main.go b/main.go
index afb41c2..70b5c0e 100644
--- a/main.go
+++ b/main.go
@@ -3,6 +3,7 @@ package main
import (
"context"
"log/slog"
+ "time"
"os"
"os/signal"
"path/filepath"
@@ -45,7 +46,8 @@ func main() {
logger.Info("config loaded", "file", flags.Config)
r := resolver.New(
- resolver.WithTimeout(2 * 1000 * 1000 * 1000),)
+ resolver.WithTimeout(2 * time.Second),
+ )
c, err := cache.NewCache(cfg.Cache.MaxEntries, cfg.Cache.DBPath)
if err != nil {