Skip to content

Commit

Permalink
feat(monitor): send "Failed to detect the IPv4/6 address" to monitors (
Browse files Browse the repository at this point in the history
  • Loading branch information
favonia committed Oct 24, 2023
1 parent 040ed1e commit f1793ad
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 15 deletions.
20 changes: 14 additions & 6 deletions internal/updater/updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package updater

import (
"context"
"fmt"
"net/netip"
"strings"

Expand Down Expand Up @@ -78,15 +79,19 @@ var ShouldDisplayHelpMessages = map[ipnet.Type]bool{
ipnet.IP6: true,
}

func detectIP(ctx context.Context, ppfmt pp.PP, c *config.Config, ipNet ipnet.Type, use1001 bool) (netip.Addr, bool) {
func detectIP(ctx context.Context, ppfmt pp.PP,
c *config.Config, ipNet ipnet.Type, use1001 bool,
) (netip.Addr, bool, string) {
ctx, cancel := context.WithTimeout(ctx, c.DetectionTimeout)
defer cancel()

msg := ""
ip, ok := c.Provider[ipNet].GetIP(ctx, ppfmt, ipNet, use1001)
if ok {
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())
msg = fmt.Sprintf("Failed to detect the %s address", ipNet.Describe())
ppfmt.Errorf(pp.EmojiError, "%s", msg)
if ShouldDisplayHelpMessages[ipNet] {
switch ipNet {
case ipnet.IP6:
Expand All @@ -99,7 +104,7 @@ func detectIP(ctx context.Context, ppfmt pp.PP, c *config.Config, ipNet ipnet.Ty
}
}
ShouldDisplayHelpMessages[ipNet] = false
return ip, ok
return ip, ok, msg
}

// UpdateIPs detect IP addresses and update DNS records of managed domains.
Expand All @@ -109,18 +114,21 @@ func UpdateIPs(ctx context.Context, ppfmt pp.PP, c *config.Config, s setter.Sett

for _, ipNet := range [...]ipnet.Type{ipnet.IP4, ipnet.IP6} {
if c.Provider[ipNet] != nil {
ip, ok := detectIP(ctx, ppfmt, c, ipNet, c.Use1001)
ip, ok, msg := detectIP(ctx, ppfmt, c, ipNet, c.Use1001)
if msg != "" {
msgs = append(msgs, msg)
}
if !ok {
// We can't detect the new IP address. It's probably better to leave existing IP addresses alone.
allOk = false
continue
}

ok, msg := setIP(ctx, ppfmt, c, s, ipNet, ip)
allOk = allOk && ok
ok, msg = setIP(ctx, ppfmt, c, s, ipNet, ip)
if msg != "" {
msgs = append(msgs, msg)
}
allOk = allOk && ok
}
}

Expand Down
18 changes: 9 additions & 9 deletions internal/updater/updater_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,11 @@ func TestUpdateIPs(t *testing.T) {
api.TTLAuto,
proxiedBoth,
false,
"looking good",
"Failed to detect the IPv4 address\nlooking good",
map[ipnet.Type]bool{ipnet.IP4: true, ipnet.IP6: true},
func(m *mocks.MockPP) {
gomock.InOrder(
m.EXPECT().Errorf(pp.EmojiError, "Failed to detect the %s address", "IPv4"),
m.EXPECT().Errorf(pp.EmojiError, "%s", "Failed to detect the IPv4 address"),
m.EXPECT().Infof(pp.EmojiConfig, "If your network does not support IPv4, you can disable it with IP4_PROVIDER=none"), //nolint:lll
m.EXPECT().Infof(pp.EmojiInternet, "Detected the %s address: %v", "IPv6", ip6),
)
Expand All @@ -183,12 +183,12 @@ func TestUpdateIPs(t *testing.T) {
api.TTLAuto,
proxiedNone,
false,
"good",
"good\nFailed to detect the IPv6 address",
map[ipnet.Type]bool{ipnet.IP4: true, ipnet.IP6: true},
func(m *mocks.MockPP) {
gomock.InOrder(
m.EXPECT().Infof(pp.EmojiInternet, "Detected the %s address: %v", "IPv4", ip4),
m.EXPECT().Errorf(pp.EmojiError, "Failed to detect the %s address", "IPv6"),
m.EXPECT().Errorf(pp.EmojiError, "%s", "Failed to detect the IPv6 address"),
m.EXPECT().Infof(pp.EmojiConfig, "If you are using Docker or Kubernetes, IPv6 often requires additional setups"), //nolint:lll
m.EXPECT().Infof(pp.EmojiConfig, "Read more about IPv6 networks at https://github.com/favonia/cloudflare-ddns"), //nolint:lll
m.EXPECT().Infof(pp.EmojiConfig, "If your network does not support IPv6, you can disable it with IP6_PROVIDER=none"), //nolint:lll
Expand All @@ -208,12 +208,12 @@ func TestUpdateIPs(t *testing.T) {
api.TTLAuto,
proxiedBoth,
false,
"",
"Failed to detect the IPv6 address",
map[ipnet.Type]bool{ipnet.IP4: true, ipnet.IP6: false},
func(m *mocks.MockPP) {
gomock.InOrder(
m.EXPECT().Infof(pp.EmojiInternet, "Detected the %s address: %v", "IPv4", ip4),
m.EXPECT().Errorf(pp.EmojiError, "Failed to detect the %s address", "IPv6"),
m.EXPECT().Errorf(pp.EmojiError, "%s", "Failed to detect the IPv6 address"),
)
},
mockproviders{
Expand All @@ -230,13 +230,13 @@ func TestUpdateIPs(t *testing.T) {
api.TTLAuto,
proxiedNone,
false,
"",
"Failed to detect the IPv4 address\nFailed to detect the IPv6 address",
map[ipnet.Type]bool{ipnet.IP4: true, ipnet.IP6: true},
func(m *mocks.MockPP) {
gomock.InOrder(
m.EXPECT().Errorf(pp.EmojiError, "Failed to detect the %s address", "IPv4"),
m.EXPECT().Errorf(pp.EmojiError, "%s", "Failed to detect the IPv4 address"),
m.EXPECT().Infof(pp.EmojiConfig, "If your network does not support IPv4, you can disable it with IP4_PROVIDER=none"), //nolint:lll
m.EXPECT().Errorf(pp.EmojiError, "Failed to detect the %s address", "IPv6"),
m.EXPECT().Errorf(pp.EmojiError, "%s", "Failed to detect the IPv6 address"),
m.EXPECT().Infof(pp.EmojiConfig, "If you are using Docker or Kubernetes, IPv6 often requires additional setups"), //nolint:lll
m.EXPECT().Infof(pp.EmojiConfig, "Read more about IPv6 networks at https://github.com/favonia/cloudflare-ddns"), //nolint:lll
m.EXPECT().Infof(pp.EmojiConfig, "If your network does not support IPv6, you can disable it with IP6_PROVIDER=none"), //nolint:lll
Expand Down

0 comments on commit f1793ad

Please sign in to comment.