Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion conf/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

var (
ConfigFile string // Path to the config file, if any
DefaultConfigFilename = "cloudflare-ddns.toml"
DefaultConfigFilename = "cloudflare-ddns"

Config = StringOption{
Name: "config",
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/mattolenik/cloudflare-ddns-client

go 1.17
go 1.20

require (
github.com/cloudflare/cloudflare-go v0.30.0
Expand Down
22 changes: 14 additions & 8 deletions ip/ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/rs/zerolog/log"
)

var apiURLs = []string{"http://whatismyip.akamai.com", "https://ipecho.net/plain", "https://wtfismyip.com/text"}
var apiURLs = []string{"http://whatismyip.akamai.com", "http://ipecho.net/plain", "http://ipv4.icanhazip.com"}

type dnsLookup struct {
// DNS server to dig
Expand Down Expand Up @@ -51,7 +51,15 @@ func GetPublicIPWithRetry(numRetries int, delay time.Duration) (string, error) {
// GetPublicIP tries to detect the public IP address of this machine. First using DNS, then using several public HTTP APIs.
func GetPublicIP() (string, error) {
ip, success, errs := getPublicIPFromDNS(dnsLookupOpenDNS, dnsLookupGoogle)
if !success && len(errs) > 0 {
var isGoodIP bool
if isGoodIP = isValidIP(ip); !isGoodIP {
// errs = append(errs, errors.Errorf("invalid IP '%s' from DNS lookup of record '%s' at '%s'", record, lookup.RecordName, lookup.Address))
log.Error().Msgf("Appears to not be an IPv4 record: %s", ip)
}

log.Debug().Msgf("retrieved IP from DNS: %+v", ip)

if !isGoodIP || (!success && len(errs) > 0) {
log.Warn().Msgf("failed to retrieve IP from DNS: %+v", errs)
ip, success, errs := getPublicIPFromAPIs(apiURLs...)
if !success && len(errs) > 0 {
Expand Down Expand Up @@ -95,9 +103,7 @@ func getPublicIPFromDNS(lookups ...dnsLookup) (string, bool, []error) {
errs = append(errs, errors.Annotatef(err, "DNS lookup of record '%s' at '%s' failed", lookup.RecordName, lookup.Address))
continue
}
if !isValidIP(record) {
errs = append(errs, errors.Errorf("invalid IP '%s' from DNS lookup of record '%s' at '%s'", record, lookup.RecordName, lookup.Address))
}
log.Debug().Msgf("invalid IP '%s' from DNS lookup of record '%s' at '%s'", record, lookup.RecordName, lookup.Address)
return record, true, nil
}
return "", false, errs
Expand Down Expand Up @@ -158,7 +164,7 @@ func getDNSTXTRecord(addr, record string) (string, error) {
return out[0].Txt[0], nil
}

// getDNSARecord gets a DNS A record
// getDNSARecord gets a DNS A record
func getDNSARecord(address, record string) (string, error) {
dig := dnsutil.Dig{}
dig.RemoteAddr = address
Expand All @@ -175,7 +181,7 @@ func getDNSARecord(address, record string) (string, error) {
return out[0].A.String(), nil
}

// isValidIP returns whether or not an IP address is valid
// isValidIP returns whether or not an IP address is a valid IPv4 address
func isValidIP(ip string) bool {
return net.ParseIP(ip) != nil
return net.ParseIP(ip).To4() != nil
}