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/url.go | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 source/url.go (limited to 'source/url.go') diff --git a/source/url.go b/source/url.go new file mode 100644 index 0000000..067d48b --- /dev/null +++ b/source/url.go @@ -0,0 +1,49 @@ +package source + +import ( + "fmt" + "log/slog" + "net/netip" + nurl "net/url" +) + +// a simple source that fetches an address from a url. +// sends a http get request to url, and parses whatever it found there. +// can be used to fetch address values from generic "my ip" +// services which don't have direct support. +// note that the address does have to be returned in plain-text! +type urlSource struct { + log *slog.Logger + url *nurl.URL + family Family +} + +var _ Source = (*urlSource)(nil) + +func newURL(log *slog.Logger, cfg Config) (*urlSource, error) { + log = log.With("source", "url") + + url, err := nurl.ParseRequestURI(cfg.URL) + if err != nil { + return nil, fmt.Errorf("invalid url for source: %w", err) + } + + return &urlSource{log: log, url: url, family: cfg.Family}, nil +} + +func (s *urlSource) Supports(f Family) bool { return f == s.family } + +func (s *urlSource) Lookup(Family) (netip.Addr, error) { + s.log.Info( + "will look up address via fetching from url", + slog.String("url", s.url.String()), + slog.String("family", string(s.family)), + ) + + addr, err := fetch(s.url.String(), s.family) + if err != nil { + return netip.Addr{}, fmt.Errorf("failed to source address from url: %w", err) + } + + return addr, nil +} -- cgit v1.3.1