AdGuardHome/internal/rdns/rdns.go

148 lines
3.9 KiB
Go
Raw Normal View History

2023-07-12 15:13:31 +03:00
// Package rdns processes reverse DNS lookup queries.
package rdns
import (
2024-09-30 20:17:20 +03:00
"context"
"log/slog"
2023-07-12 15:13:31 +03:00
"net/netip"
"time"
"github.com/AdguardTeam/golibs/errors"
2024-09-30 20:17:20 +03:00
"github.com/AdguardTeam/golibs/logutil/slogutil"
2023-07-12 15:13:31 +03:00
"github.com/bluele/gcache"
)
// Interface processes rDNS queries.
type Interface interface {
// Process makes rDNS request and returns domain name. changed indicates
// that domain name was updated since last request.
2024-09-30 20:17:20 +03:00
Process(ctx context.Context, ip netip.Addr) (host string, changed bool)
2023-07-12 15:13:31 +03:00
}
2023-07-26 13:18:44 +03:00
// Empty is an empty [Interface] implementation which does nothing.
2023-07-12 15:13:31 +03:00
type Empty struct{}
// type check
var _ Interface = (*Empty)(nil)
// Process implements the [Interface] interface for Empty.
2024-09-30 20:17:20 +03:00
func (Empty) Process(_ context.Context, _ netip.Addr) (host string, changed bool) {
2023-07-12 15:13:31 +03:00
return "", false
}
// Exchanger is a resolver for clients' addresses.
type Exchanger interface {
// Exchange tries to resolve the ip in a suitable way, i.e. either as local
// or as external.
2023-07-26 13:18:44 +03:00
Exchange(ip netip.Addr) (host string, ttl time.Duration, err error)
2023-07-12 15:13:31 +03:00
}
// Config is the configuration structure for Default.
type Config struct {
2024-09-30 20:17:20 +03:00
// Logger is used for logging the operation of the reverse DNS lookup
// queries. It must not be nil.
Logger *slog.Logger
2023-07-12 15:13:31 +03:00
// Exchanger resolves IP addresses to domain names.
Exchanger Exchanger
// CacheSize is the maximum size of the cache. It must be greater than
// zero.
CacheSize int
// CacheTTL is the Time to Live duration for cached IP addresses.
CacheTTL time.Duration
}
// Default is the default rDNS query processor.
type Default struct {
2024-09-30 20:17:20 +03:00
// logger is used for logging the operation of the reverse DNS lookup
// queries. It must not be nil.
logger *slog.Logger
2023-07-12 15:13:31 +03:00
// cache is the cache containing IP addresses of clients. An active IP
// address is resolved once again after it expires. If IP address couldn't
// be resolved, it stays here for some time to prevent further attempts to
// resolve the same IP.
cache gcache.Cache
// exchanger resolves IP addresses to domain names.
exchanger Exchanger
// cacheTTL is the Time to Live duration for cached IP addresses.
cacheTTL time.Duration
}
// New returns a new default rDNS query processor. conf must not be nil.
func New(conf *Config) (r *Default) {
return &Default{
2024-09-30 20:17:20 +03:00
logger: conf.Logger,
2023-07-12 15:13:31 +03:00
cache: gcache.New(conf.CacheSize).LRU().Build(),
exchanger: conf.Exchanger,
cacheTTL: conf.CacheTTL,
}
}
// type check
var _ Interface = (*Default)(nil)
// Process implements the [Interface] interface for Default.
2024-09-30 20:17:20 +03:00
func (r *Default) Process(ctx context.Context, ip netip.Addr) (host string, changed bool) {
fromCache, expired := r.findInCache(ctx, ip)
2023-07-12 15:13:31 +03:00
if !expired {
return fromCache, false
}
2023-07-26 13:18:44 +03:00
host, ttl, err := r.exchanger.Exchange(ip)
2023-07-12 15:13:31 +03:00
if err != nil {
2024-09-30 20:17:20 +03:00
r.logger.DebugContext(ctx, "resolving", "ip", ip, slogutil.KeyError, err)
2023-07-12 15:13:31 +03:00
}
ttl = max(ttl, r.cacheTTL)
2023-07-26 13:18:44 +03:00
2023-07-12 15:13:31 +03:00
item := &cacheItem{
2023-07-26 13:18:44 +03:00
expiry: time.Now().Add(ttl),
2023-07-12 15:13:31 +03:00
host: host,
}
err = r.cache.Set(ip, item)
if err != nil {
2024-09-30 20:17:20 +03:00
r.logger.DebugContext(ctx, "adding item to cache", "key", ip, slogutil.KeyError, err)
2023-07-12 15:13:31 +03:00
}
2023-08-02 16:26:34 +03:00
// TODO(e.burkov): The name doesn't change if it's neither stored in cache
// nor resolved successfully. Is it correct?
2023-07-12 15:13:31 +03:00
return host, fromCache == "" || host != fromCache
}
// findInCache finds domain name in the cache. expired is true if host is not
// valid anymore.
2024-09-30 20:17:20 +03:00
func (r *Default) findInCache(ctx context.Context, ip netip.Addr) (host string, expired bool) {
2023-07-12 15:13:31 +03:00
val, err := r.cache.Get(ip)
if err != nil {
if !errors.Is(err, gcache.KeyNotFoundError) {
2024-09-30 20:17:20 +03:00
r.logger.DebugContext(
ctx,
"retrieving item from cache",
"key", ip,
slogutil.KeyError, err,
)
2023-07-12 15:13:31 +03:00
}
return "", true
}
2024-09-30 20:17:20 +03:00
item := val.(*cacheItem)
2023-07-12 15:13:31 +03:00
return item.host, time.Now().After(item.expiry)
}
// cacheItem represents an item that we will store in the cache.
type cacheItem struct {
// expiry is the time when cacheItem will expire.
expiry time.Time
// host is the domain name of a runtime client.
host string
}