-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
115 lines (91 loc) · 2.48 KB
/
main.go
File metadata and controls
115 lines (91 loc) · 2.48 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package main
import (
"flag"
"fmt"
"io"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/pkg/errors"
"go.uber.org/zap"
"gopkg.in/yaml.v3"
)
func main() {
configPathPtr := flag.String("config", "ddns-go.yaml", "Path to the ddns-go config")
flag.Parse()
config, err := readConfig(*configPathPtr)
if err != nil {
panic(errors.Wrap(err, "failed to get config"))
}
level, err := zap.ParseAtomicLevel(config.LogLevel)
if err != nil {
panic(errors.Wrap(err, "failed to parse logger level"))
}
loggerConfig := zap.NewProductionConfig()
loggerConfig.Encoding = "json"
loggerConfig.Level = level
loggerConfig.DisableCaller = true
logger, err := loggerConfig.Build()
if err != nil {
panic(errors.Wrap(err, "failed to create logger"))
}
logger = logger.With(
zap.String("host", config.Host),
zap.String("domain_name", config.DomainName),
)
ticker := time.NewTicker(config.UpdatePeriod)
url := fmt.Sprintf("https://dynamicdns.park-your-domain.com/update?host=%s&domain=%s&password=%s",
config.Host,
config.DomainName,
config.DDNSPassword)
signalChannel := make(chan os.Signal, 1)
signal.Notify(signalChannel, syscall.SIGTERM, syscall.SIGINT)
for {
select {
case <-ticker.C:
resp, err := http.Get(url)
if err != nil {
logger.Error("failed to send DNS update request", zap.Error(err))
continue
}
if resp.StatusCode >= 400 {
body, readErr := io.ReadAll(resp.Body)
if readErr != nil {
logger.Error("failed to read response body", zap.Error(readErr))
}
logger.Error("got error response from DNS server",
zap.Int("status_code", resp.StatusCode),
zap.String("status", resp.Status),
zap.ByteString("response_body", body))
continue
}
logger.Info("successfully updated DNS entry",
zap.String("status", resp.Status),
)
case <-signalChannel:
logger.Info("shutting down")
ticker.Stop()
os.Exit(0)
}
}
}
type Config struct {
UpdatePeriod time.Duration `yaml:"update_period"`
Host string `yaml:"host"`
DomainName string `yaml:"domain_name"`
DDNSPassword string `yaml:"ddns_password"`
LogLevel string `yaml:"log_level"`
}
func readConfig(path string) (*Config, error) {
configBytes, err := os.ReadFile(path)
if err != nil {
return nil, errors.Wrap(err, "failed to read config")
}
result := &Config{}
if err = yaml.Unmarshal(configBytes, result); err != nil {
return nil, errors.Wrap(err, "failed to parse config")
}
return result, nil
}