From b52c860a7b1d3f57abc442bb8c1257f228d49536 Mon Sep 17 00:00:00 2001 From: Mel Date: Thu, 20 Aug 2026 06:18:05 +0200 Subject: ddnnss Signed-off-by: Mel --- source/static.go | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 source/static.go (limited to 'source/static.go') diff --git a/source/static.go b/source/static.go new file mode 100644 index 0000000..d2645d6 --- /dev/null +++ b/source/static.go @@ -0,0 +1,44 @@ +package source + +import ( + "fmt" + "log/slog" + "net/netip" +) + +// a simple source that returns the configured address. +// useful when a record needs to have one specific value, +// that is known in advance, and for debugging. +type staticSource struct { + log *slog.Logger + addr netip.Addr +} + +var _ Source = (*staticSource)(nil) + +func newStatic(log *slog.Logger, cfg Config) (*staticSource, error) { + log = log.With("source", "static") + addr, err := parse(cfg.Value, cfg.Family) + if err != nil { + log.Error( + "static source can't be created from value", + slog.String("family", string(cfg.Family)), + slog.String("value", cfg.Value), + ) + return nil, fmt.Errorf("can't create static source from value: %w", err) + } + + return &staticSource{log: log, addr: addr}, nil +} + +func (s *staticSource) Supports(f Family) bool { + return (s.addr.Is4() && f == IPv4) || (s.addr.Is6() && f == IPv6) +} + +func (s *staticSource) Lookup(Family) (netip.Addr, error) { + s.log.Info( + "looked up address from static source", + slog.String("addr", s.addr.String()), + ) + return s.addr, nil +} -- cgit v1.3.1