55 "time"
66
77 "github.com/juju/errors"
8+ "github.com/mattolenik/cloudflare-ddns-client/cloudflare"
89 "github.com/mattolenik/cloudflare-ddns-client/conf"
9- "github.com/mattolenik/cloudflare-ddns-client/dns"
1010 "github.com/mattolenik/cloudflare-ddns-client/ip"
1111 "github.com/rs/zerolog/log"
1212)
@@ -18,7 +18,7 @@ func Run(ctx context.Context) error {
1818 return errors .Annotate (err , "unable to retrieve public IP" )
1919 }
2020 log .Info ().Msgf ("Found public IP '%s'" , ip )
21- err = dns . UpdateCloudFlare (
21+ err = cloudflare . Update (
2222 ctx ,
2323 conf .Token .Get (),
2424 conf .Domain .Get (),
@@ -45,13 +45,23 @@ func Daemon(ctx context.Context, updatePeriod, failureRetryDelay time.Duration)
4545 log .Info ().Msgf ("Daemon running, will now monitor for IP updates every %d seconds" , int (updatePeriod .Seconds ()))
4646
4747 for {
48+ dnsRecordIP , err := cloudflare .Get (
49+ ctx ,
50+ conf .Token .Get (),
51+ conf .Domain .Get (),
52+ conf .Record .Get ())
53+ if err != nil {
54+ log .Error ().Msgf ("unable to look up current DNS record, will retry in %d seconds" , int (updatePeriod .Seconds ()))
55+ time .Sleep (failureRetryDelay )
56+ continue
57+ }
4858 newIP , err := ip .GetPublicIPWithRetry (10 , 5 * time .Second )
4959 if err != nil {
5060 log .Error ().Msgf ("unable to retrieve public IP, will retry in %d seconds" , int (updatePeriod .Seconds ()))
5161 time .Sleep (failureRetryDelay )
5262 continue
5363 }
54- if newIP == lastIP {
64+ if newIP == lastIP && newIP == dnsRecordIP {
5565 log .Info ().Msgf (
5666 "No IP change detected since %s (%d seconds ago)" ,
5767 lastIPUpdate .Format (time .RFC1123Z ),
@@ -60,14 +70,19 @@ func Daemon(ctx context.Context, updatePeriod, failureRetryDelay time.Duration)
6070 continue
6171 }
6272 if lastIP == "" {
63- log .Info ().Msgf ("Found public IP '%s'" , lastIP )
73+ // Log line for first time
74+ log .Info ().Msgf ("Found public IP '%s'" , newIP )
6475 } else if newIP != lastIP {
76+ // Log line for IP change
6577 log .Info ().Msgf ("Detected new public IP address, it changed from '%s' to '%s'" , lastIP , newIP )
78+ } else if dnsRecordIP != newIP {
79+ // Log line for no new IP, but mismatch with DNS record
80+ log .Info ().Msgf ("Public IP address did not change, but DNS record did match, is '%s' but expected '%s', correcting" , dnsRecordIP , newIP )
6681 }
6782 lastIP = newIP
6883 lastIPUpdate = time .Now ()
6984
70- err = dns . UpdateCloudFlare (
85+ err = cloudflare . Update (
7186 ctx ,
7287 conf .Token .Get (),
7388 conf .Domain .Get (),
0 commit comments