1
0

Run updates on a timer loop

The config yaml provides an interval period which is used to determine
how often to update the IP addresses on the domains. The local IP
address is checked each time before updating the domains.
This commit is contained in:
Tony Grosinger 2022-12-06 08:40:19 -08:00
parent 6b64253b7a
commit a83b135d14
3 changed files with 36 additions and 12 deletions

View File

@ -32,6 +32,7 @@ Create a file `.digitalocean-dynamic-ip.json` (dot prefix to hide the file) and
```json
{
"interval": "30m",
"apikey": "samplekeydasjkdhaskjdhrwofihsamplekey",
"doPageSize": 20,
"useIPv4": true,
@ -90,13 +91,3 @@ By default the tool will not output anything on success. If you wish to see debu
#run:
./digitalocean-dynamic-ip -debug /path/to/my/config.json
```
You can either set this to run periodically with a cronjob or use your own method.
```bash
# run `crontab -e` to edit your crontab
# sample cron job task
# m h dom mon dow command
*/5 * * * * /home/user/digitalocean-dynamic-dns-ip/digitalocean-dynamic-ip
```

View File

@ -12,7 +12,10 @@ import (
"net/http"
"net/url"
"os"
"os/signal"
"strconv"
"syscall"
"time"
)
func checkError(err error) {
@ -48,6 +51,7 @@ var config ClientConfig
// ClientConfig : configuration json
type ClientConfig struct {
Interval string `json:"interval"`
APIKey string `json:"apiKey"`
DOPageSize int `json:"doPageSize"`
UseIPv4 *bool `json:"useIPv4"`
@ -366,8 +370,9 @@ func toIPv6String(ip net.IP) (currentIP string) {
// return true
// }
func main() {
config = GetConfig()
// updateAllDomains retrieves the current IP addresses and loops through all
// domains in the config to update them with the current IPs.
func updateAllDomains(config ClientConfig) {
currentIPv4, currentIPv6 := CheckLocalIPs()
if currentIPv4 == nil && currentIPv6 == nil {
logError("Current IP addresses are not valid, or both are disabled in the config. Check your configuration and internet connection.")
@ -379,3 +384,30 @@ func main() {
log.Printf("%s: END", domain.Domain)
}
}
func main() {
config = GetConfig()
interval, err := time.ParseDuration(config.Interval)
checkError(err)
ticker := time.NewTicker(interval)
cancelChan := make(chan os.Signal)
signal.Notify(cancelChan, syscall.SIGTERM, syscall.SIGINT)
// Run once immediately before waiting for the ticker
updateAllDomains(config)
func() {
for {
select {
case <-cancelChan:
return
case <-ticker.C:
updateAllDomains(config)
}
}
}()
ticker.Stop()
}

View File

@ -1,4 +1,5 @@
{
"interval": "30m",
"apikey": "samplekeydasjkdhaskjdhrwofihsamplekey",
"doPageSize": 20,
"useIPv4": true,