This repository has been archived on 2024-04-02. You can view files and clone it, but cannot push or open issues or pull requests.
ddnser/main.go

58 lines
1.1 KiB
Go
Raw Permalink Normal View History

2023-01-10 01:09:35 +00:00
package main
2023-01-10 03:23:32 +00:00
import (
2023-01-11 23:38:19 +00:00
"context"
2023-01-10 03:23:32 +00:00
"log"
2023-01-11 23:38:19 +00:00
"os"
2023-01-12 00:59:37 +00:00
"sync"
2023-01-11 23:38:19 +00:00
"time"
2023-01-10 03:23:32 +00:00
)
2023-01-11 23:38:19 +00:00
type refreshChan chan string
2023-01-10 01:09:35 +00:00
func main() {
2023-01-11 23:38:19 +00:00
if len(os.Args) < 2 {
log.Fatal("Must provide config file path as first argument")
2023-01-10 03:23:32 +00:00
}
2023-01-11 23:38:19 +00:00
config, err := LoadConfig(os.Args[1])
2023-01-10 03:23:32 +00:00
if err != nil {
log.Fatal(err)
}
2023-01-10 01:09:35 +00:00
2023-01-11 23:38:19 +00:00
refr := make(refreshChan)
errch := make(chan error)
go poller(config.Every, refr)
for {
select {
case reason := <-refr:
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(10)*time.Second)
defer cancel()
log.Printf("Running because of %s", reason)
2023-01-12 00:59:37 +00:00
var wg sync.WaitGroup
wg.Add(len(config.Domains))
2023-01-11 23:38:19 +00:00
for _, d := range config.Domains {
2023-01-12 00:59:37 +00:00
go func(d Domain, wg *sync.WaitGroup) {
record, err := d.NameServer.SetRecord(ctx, d.Name, config.Ip)
if err != nil {
2023-05-09 12:46:17 +00:00
log.Printf("ERROR: [%s] %s", d.Name, err.Error())
2023-01-12 00:59:37 +00:00
// TODO: Reportar errores
return
}
2023-05-09 12:46:17 +00:00
log.Printf("info: [%s] Set to %s", d.Name, record)
2023-01-12 00:59:37 +00:00
wg.Done()
}(d, &wg)
2023-01-11 23:38:19 +00:00
}
case err := <-errch:
log.Fatal(err)
}
}
}
func poller(every int, refr refreshChan) {
for {
refr <- "poll"
time.Sleep(time.Duration(every) * time.Minute)
2023-01-11 23:38:19 +00:00
}
2023-01-10 01:09:35 +00:00
}