From b3fc8091f75617659f8463a0748317a1048b8d39 Mon Sep 17 00:00:00 2001 From: favonia Date: Sun, 8 May 2022 11:13:57 -0500 Subject: [PATCH] fix: updating was wrongly restricted by detection timeout (#159) The context used in the old updateIP for setting IPs already has the timeout set for detection. The new code uses a fresh context for setting IPs. --- cmd/ip.go | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/cmd/ip.go b/cmd/ip.go index 9abcfa27..e69230d7 100644 --- a/cmd/ip.go +++ b/cmd/ip.go @@ -30,29 +30,31 @@ func setIP(ctx context.Context, ppfmt pp.PP, c *config.Config, h api.Handle, ipN var ipv6MessageDisplayed = false //nolint:gochecknoglobals -func updateIP(ctx context.Context, ppfmt pp.PP, c *config.Config, h api.Handle, ipNet ipnet.Type) { +func detectIP(ctx context.Context, ppfmt pp.PP, c *config.Config, h api.Handle, ipNet ipnet.Type) netip.Addr { ctx, cancel := context.WithTimeout(ctx, c.DetectionTimeout) defer cancel() ip := c.Policy[ipNet].GetIP(ctx, ppfmt, ipNet) - if !ip.IsValid() { + if ip.IsValid() { + ppfmt.Infof(pp.EmojiInternet, "Detected the %s address: %v", ipNet.Describe(), ip) + } else { ppfmt.Errorf(pp.EmojiError, "Failed to detect the %s address", ipNet.Describe()) if !ipv6MessageDisplayed && ipNet == ipnet.IP6 { ipv6MessageDisplayed = true ppfmt.Infof(pp.EmojiConfig, "If you are using Docker, Kubernetes, or other frameworks, IPv6 networks often require additional setups.") //nolint:lll ppfmt.Infof(pp.EmojiConfig, "Read more about IPv6 networks in the README at https://github.com/favonia/cloudflare-ddns") //nolint:lll } - return } - - ppfmt.Infof(pp.EmojiInternet, "Detected the %s address: %v", ipNet.Describe(), ip) - setIP(ctx, ppfmt, c, h, ipNet, ip) + return ip } func updateIPs(ctx context.Context, ppfmt pp.PP, c *config.Config, h api.Handle) { for _, ipNet := range []ipnet.Type{ipnet.IP4, ipnet.IP6} { if c.Policy[ipNet] != nil { - updateIP(ctx, ppfmt, c, h, ipNet) + ip := detectIP(ctx, ppfmt, c, h, ipNet) + if ip.IsValid() { + setIP(ctx, ppfmt, c, h, ipNet, ip) + } } } }