aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2026-01-26 16:38:59 +0700
committerShulhan <ms@kilabit.info>2026-02-02 16:28:11 +0700
commit85c11aee794ae488a41c5b718b6b266afb617b7d (patch)
tree548bfea44569da8956b2f7120d8fddb689e59071 /lib
parentd4ff81a5e454bdbe0346beb9d09f9055dd2f07ed (diff)
downloadpakakeh.go-85c11aee794ae488a41c5b718b6b266afb617b7d.tar.xz
lib/dns: add option to set hook on server when receiving answer
The hook function, OnAnswerReceived, will be triggered when server receive valid answer but before its put to caches.
Diffstat (limited to 'lib')
-rw-r--r--lib/dns/server.go5
-rw-r--r--lib/dns/server_options.go7
2 files changed, 12 insertions, 0 deletions
diff --git a/lib/dns/server.go b/lib/dns/server.go
index ca2baa5b..438c36dd 100644
--- a/lib/dns/server.go
+++ b/lib/dns/server.go
@@ -690,6 +690,11 @@ func (srv *Server) processResponse(req *request, res *Message) (an *Answer, inse
}
an = newAnswer(res, false)
+
+ if srv.opts.OnAnswerReceived != nil {
+ srv.opts.OnAnswerReceived(an)
+ }
+
an, inserted = srv.Caches.upsert(an)
return an, inserted, nil
diff --git a/lib/dns/server_options.go b/lib/dns/server_options.go
index 20629c2b..9c042b5a 100644
--- a/lib/dns/server_options.go
+++ b/lib/dns/server_options.go
@@ -31,6 +31,9 @@ const (
DebugLevelConnPacket = 4
)
+// HookFunc define type function that will be triggered on specific event.
+type HookFunc func(answer *Answer)
+
// ServerOptions describes options for running a DNS server.
type ServerOptions struct {
primaryUDP []net.Addr // List of parent name server addresses using UDP.
@@ -53,6 +56,10 @@ type ServerOptions struct {
// TLSPrivateKey contains path to certificate private key file.
TLSPrivateKey string `ini:"dns:server:tls.private_key"`
+ // OnAnswerReceived define the hook to be triggered when server
+ // receive valid answer, before its put to caches.
+ OnAnswerReceived HookFunc `json:"-" ini:"-"`
+
// NameServers contains list of parent name servers.
//
// Answer that does not exist on local will be forwarded to parent