-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
187 lines (155 loc) · 4.49 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
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package main
import (
"fmt"
"io/ioutil"
"net/http"
"os"
"strings"
"github.com/invisiblethreat/env"
"github.com/sirupsen/logrus"
"github.com/spf13/pflag"
yaml "gopkg.in/yaml.v2"
)
// Update contains everything that DuckDNS will need to update a record
type Update struct {
Token string `yaml:"token"`
Names []string `yaml:"domains"`
}
// CLIOptions are to set things via CLI
type CLIOptions struct {
Debug bool
File string
Token string
Log string
Names []string
}
// Valid checks that all parameters are set for an update
func (u *Update) Valid() bool {
if len(u.Names) > 0 && u.Token != "" {
return true
}
return false
}
// GetConfigCLI sets the arguments for an update if they have been passed in on
// the CLI
func getConfigCLI(c CLIOptions, log *logrus.Logger) Update {
var u Update
u.Token = c.Token
log.Debugf("Set token from CLI to %s", c.Token)
u.Names = c.Names
log.Debugf("Set names from CLI to %s", strings.Join(c.Names, ", "))
return u
}
// GetConfigFile reads the config for DuckDNS
func getConfigFile(existing *Update, file string, log *logrus.Logger) {
var update Update
yamlFile, err := ioutil.ReadFile(file)
if err != nil {
log.WithError(err).Debug("Error reading file")
return
}
err = yaml.Unmarshal(yamlFile, &update)
if err != nil {
log.WithError(err).Debug("Error unmarshaling YAML file")
return
}
// Set the token if it's not empty and doesn't already exist
if update.Token == "" {
log.Debugf("The token is empty after trying to parse %s", file)
} else if existing.Token == "" {
existing.Token = update.Token
}
// Set names to if they exist and value is not already set
if len(update.Names) == 0 {
log.Debugf("No names/subdomains specified to update from %s", file)
} else if len(existing.Names) == 0 {
existing.Names = update.Names
}
}
// GetConfigEnv is for reading items out of the environment if you didn't want
// to set them on the CLI
func getConfigEnv(u *Update, log *logrus.Logger) {
token := env.String("DUCK_TOKEN", "")
name := env.String("DUCK_NAMES", "")
// Set the token if not already set
if u.Token == "" {
u.Token = token
log.Debugf("Set token from environment to %s", token)
}
if len(u.Names) == 0 && name != "" {
// support DUCK_NAME="domain1 domain2" from the environment
u.Names = strings.Split(name, " ")
log.Debugf("Set names from environment to %s",
strings.Join(u.Names, ", "))
}
}
func makeUpdate(update Update, log *logrus.Logger) error {
log.Debugf("Dumping update params: %#v", update)
if !update.Valid() {
log.Fatal("Arguments not set for update!")
os.Exit(1)
}
stub := "https://www.duckdns.org/update?domains="
tokenStub := "&token="
ipStub := "&ip="
subdodomains := strings.Join(update.Names, ",")
url := fmt.Sprintf("%s%s%s%s%s", stub, subdodomains, tokenStub, update.Token, ipStub)
log.Debugf("Update string: %s", url)
res, err := http.Get(url)
if err != nil {
log.WithError(err).Error("Error contacting DuckDNS server")
return err
}
defer res.Body.Close()
bodyBytes, err := ioutil.ReadAll(res.Body)
if err != nil {
log.WithError(err).Error("Error reading body response")
return err
}
if strings.Contains(string(bodyBytes), "KO") {
log.WithError(err).Error("Error returned from DuckDNS")
return err
}
log.Debugf("updated DuckDNS for name %s", subdodomains)
return nil
}
func main() {
var cli CLIOptions
pflag.BoolVarP(&cli.Debug, "debug", "d", false, "Use debug mode")
pflag.StringVarP(&cli.File, "config", "c", "duckdns.yaml",
"Config file location")
pflag.StringSliceVarP(&cli.Names, "names", "n", nil,
"Names to update with DuckDNS. Just the subdomain section. "+
"Use the flag multiple times to set multiple values.")
pflag.StringVarP(&cli.Token, "token", "t", "",
"Token for updating DuckDNS")
pflag.StringVarP(&cli.Log, "log", "l", "",
"Log file location")
pflag.Parse()
log := logrus.New()
path, err := os.OpenFile(cli.Log, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err == nil {
log.Out = path
} else {
log.Info("No logging path set, using stderr")
}
if cli.Debug {
log.SetLevel(logrus.DebugLevel)
}
log.Debugf("Logging level: %s", log.GetLevel().String())
// CLI vars
update := getConfigCLI(cli, log)
// Set things that weren't set by the CLI
if !update.Valid() {
getConfigEnv(&update, log)
}
// File vars
if !update.Valid() {
getConfigFile(&update, cli.File, log)
}
if err := makeUpdate(update, log); err != nil {
log.WithError(err).Fatal("error updating IP address")
os.Exit(1)
}
log.Info("IP address updated successfully")
}