From f896a9775c34c0e095273c193806ca245dfae940 Mon Sep 17 00:00:00 2001 From: Mel Date: Thu, 20 Aug 2026 22:05:41 +0200 Subject: Add IPv4/IPv6 source from 'icanhazip.com' Signed-off-by: Mel --- source/icanhazip.go | 29 +++++++++++++++++++++++++++++ source/source.go | 13 +++++++++---- 2 files changed, 38 insertions(+), 4 deletions(-) create mode 100644 source/icanhazip.go diff --git a/source/icanhazip.go b/source/icanhazip.go new file mode 100644 index 0000000..62f2d9f --- /dev/null +++ b/source/icanhazip.go @@ -0,0 +1,29 @@ +package source + +import ( + "log/slog" + "net/netip" +) + +type icanhazip struct { + log *slog.Logger +} + +var _ Source = (*icanhazip)(nil) + +func newICanHazIp(log *slog.Logger) (*icanhazip, error) { + return &icanhazip{log: log}, nil +} + +func (icanhazip) Supports(Family) bool { return true } + +func (s icanhazip) Lookup(family Family) (netip.Addr, error) { + s.log.Info("will look up address from the icanhazip.com service", + slog.String("family", string(family)), + ) + + if family == IPv6 { + return fetch("https://ipv6.icanhazip.com", IPv6) + } + return fetch("https://ipv4.icanhazip.com", IPv4) +} diff --git a/source/source.go b/source/source.go index ffa380a..571abda 100644 --- a/source/source.go +++ b/source/source.go @@ -13,10 +13,10 @@ import ( type Kind string const ( - // only the simple generic sources for now! - KindStatic Kind = "static" - KindURL Kind = "url" - KindFile Kind = "file" + KindStatic Kind = "static" + KindURL Kind = "url" + KindFile Kind = "file" + KindICanHazIp Kind = "icanhazip" ) type Family string @@ -49,6 +49,11 @@ func New(log *slog.Logger, name string, cfg Config) (Source, error) { ), ) + if cfg.Kind == KindICanHazIp { + return newICanHazIp(log) + } + + // all sources below need an explicit family if cfg.Family != IPv4 && cfg.Family != IPv6 { return nil, fmt.Errorf("%s source needs family set to ipv4 or ipv6", cfg.Kind) } -- cgit v1.3.1