@@ -10,76 +10,25 @@ import (
1010 "regexp"
1111 "slices"
1212 "strings"
13- "time"
14-
15- "github.com/TecharoHQ/anubis/lib/store"
1613)
1714
1815var (
1916 DNSLookupAddr = net .LookupAddr
2017 DNSLookupHost = net .LookupHost
2118)
2219
23- type DnsCache struct {
24- forward store.JSON [[]string ]
25- reverse store.JSON [[]string ]
26- forwardTTL time.Duration
27- reverseTTL time.Duration
28- }
29-
3020type Dns struct {
3121 cache * DnsCache
3222 ctx context.Context
3323}
3424
35- func NewDNSCache (forwardTTL int , reverseTTL int , backend store.Interface ) * DnsCache {
36- return & DnsCache {
37- forward : store.JSON [[]string ]{
38- Underlying : backend ,
39- Prefix : "forwardDNS" ,
40- },
41- reverse : store.JSON [[]string ]{
42- Underlying : backend ,
43- Prefix : "reverseDNS" ,
44- },
45- forwardTTL : time .Duration (forwardTTL ) * time .Second ,
46- reverseTTL : time .Duration (reverseTTL ) * time .Second ,
47- }
48- }
49-
5025func New (ctx context.Context , cache * DnsCache ) * Dns {
5126 return & Dns {
5227 cache : cache ,
5328 ctx : ctx ,
5429 }
5530}
5631
57- func (d * Dns ) getCachedForward (host string ) ([]string , bool ) {
58- if cached , err := d .cache .forward .Get (d .ctx , host ); err == nil {
59- slog .Debug ("DNS: forward cache hit" , "name" , host , "ips" , cached )
60- return cached , true
61- }
62- slog .Debug ("DNS: forward cache miss" , "name" , host )
63- return nil , false
64- }
65-
66- func (d * Dns ) getCachedReverse (addr string ) ([]string , bool ) {
67- if cached , err := d .cache .reverse .Get (d .ctx , addr ); err == nil {
68- slog .Debug ("DNS: reverse cache hit" , "addr" , addr , "names" , cached )
69- return cached , true
70- }
71- slog .Debug ("DNS: reverse cache miss" , "addr" , addr )
72- return nil , false
73- }
74-
75- func (d * Dns ) forwardCachePut (host string , entries []string ) {
76- d .cache .forward .Set (d .ctx , host , entries , d .cache .forwardTTL )
77- }
78-
79- func (d * Dns ) reverseCachePut (addr string , entries []string ) {
80- d .cache .reverse .Set (d .ctx , addr , entries , d .cache .reverseTTL )
81- }
82-
8332// ReverseDNS performs a reverse DNS lookup for the given IP address and trims the trailing dot from the results.
8433func (d * Dns ) ReverseDNS (addr string ) ([]string , error ) {
8534 slog .Debug ("DNS: performing reverse lookup" , "addr" , addr )
0 commit comments