forked from skibish/ddns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
88 lines (72 loc) · 1.79 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package main
import (
"flag"
"net/http"
"os"
"time"
"github.com/skibish/ddns/ipprovider/ifconfig"
"github.com/skibish/ddns/ipprovider/ipify"
"github.com/skibish/ddns/ipprovider/wtfismyip"
"github.com/skibish/ddns/updater"
log "github.com/sirupsen/logrus"
"github.com/skibish/ddns/conf"
"github.com/skibish/ddns/ipprovider"
"github.com/skibish/ddns/notifier"
)
var (
reqTimeouts = flag.Duration("req-timeout", 10*time.Second, "Request timeout to external resources")
checkPeriod = flag.Duration("check-period", 5*time.Minute, "Check if IP has been changed period")
confFile = flag.String("conf-file", "$HOME/.ddns.yml", "Location of the configuration file")
)
func init() {
log.SetFormatter(&log.TextFormatter{
DisableColors: true,
})
log.SetLevel(log.DebugLevel)
log.SetOutput(os.Stdout)
}
func main() {
flag.Parse()
// read configuration
var errConf error
cf, errConf := conf.NewConfiguration(*confFile)
if errConf != nil {
log.Fatal(errConf.Error())
}
// try to register all provided hooks
for k, v := range cf.Notify {
hook, errGet := notifier.GetHook(k, v)
if errGet != nil {
log.Debugf("Notifier %q not added: %s", k, errGet.Error())
continue
}
log.AddHook(hook)
}
// setup http client
c := &http.Client{
Timeout: *reqTimeouts,
}
// initialize new ipprovider and register IP providers
provider := ipprovider.New()
providerList := []ipprovider.Provider{
ifconfig.New(c),
wtfismyip.New(c),
ipify.New(c),
}
if cf.ForceIPV6 {
for _, p := range providerList {
p.ForceIPV6()
}
}
provider.Register(providerList...)
// Initialize and start updater
upd, errUpdater := updater.New(c, provider, cf, *checkPeriod)
if errUpdater != nil {
log.Fatal(errUpdater)
}
errStart := upd.Start()
if errStart != nil {
log.Fatal(errStart)
}
select {}
}