-
Notifications
You must be signed in to change notification settings - Fork 3
/
yandex-ddns.go
68 lines (53 loc) · 1.26 KB
/
yandex-ddns.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
package main
import (
"flag"
"fmt"
"log"
"os"
"path/filepath"
"github.com/nightlyone/lockfile"
)
func initLock(file string) (*lockfile.Lockfile, error) {
lock, err := lockfile.New(filepath.Join(os.TempDir(), file))
if err != nil {
return nil, err
}
err = lock.TryLock()
if err != nil {
return nil, err
}
return &lock, nil
}
func main() {
var (
configFile string
testConfigOnly bool
)
flag.StringVar(&configFile, "config", "yandex-ddns.toml", "configuration file")
flag.BoolVar(&testConfigOnly, "t", false, "only test configuration file")
flag.Parse()
conf := newConfigurationFromFile(configFile)
if testConfigOnly {
verifyConfiguration(conf)
fmt.Println("Configuration file Ok.")
os.Exit(0)
}
lock, err := initLock("yandex-ddns.lock")
if err != nil {
log.Fatalf("Couldn't init lock file: %v\n", err)
}
defer lock.Unlock()
if conf.LogFile != "" {
f, err := os.OpenFile(conf.LogFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0o666)
if err != nil {
log.Fatalf("error opening file: %v", err)
}
defer closeResource(f)
log.SetOutput(f)
}
log.SetFlags(log.LstdFlags | log.Lshortfile)
verifyConfiguration(conf)
extIPAddr := getExternalIP(conf)
domainInfo := getDomainInfo(conf)
updateDomainAddress(domainInfo, extIPAddr, conf)
}