diff options
| -rw-r--r-- | CHANGELOG.adoc | 5 | ||||
| -rw-r--r-- | lib/dns/server.go | 5 | ||||
| -rw-r--r-- | lib/dns/server_options.go | 7 |
3 files changed, 17 insertions, 0 deletions
diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc index 41141827..805fd45f 100644 --- a/CHANGELOG.adoc +++ b/CHANGELOG.adoc @@ -38,6 +38,11 @@ Legend, [#v0_61_0] == pakakeh.go v0.61.0 (2026-xx-xx) +**🌼 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. + **🌼 lib/dns: add method to set TTL on Message** The SetTTL method set all RRs answer time to live. 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 |
