-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
206 lines (179 loc) · 4.84 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package main
import (
"context"
"fmt"
"io"
"log"
"net/http"
"os"
"strconv"
"strings"
"time"
"github.com/bugsnag/bugsnag-go/v2"
"github.com/cenkalti/backoff/v4"
"github.com/joho/godotenv"
"github.com/pkg/errors"
)
type DynDNSRequest struct {
IPAddress string
DomainName string
Username string
Password string
}
func main() {
ctx := context.Background()
err := godotenv.Load()
if err != nil {
log.Println(".env not provided, using environment variables instead")
}
bugsnagAPIKey, ok := os.LookupEnv("BUGSNAG_API_KEY")
if ok {
bugsnag.Configure(bugsnag.Configuration{
APIKey: bugsnagAPIKey,
AppVersion: "v1.4.1",
})
}
domains, ok := getDomains()
if !ok {
log.Fatal("DOMAINS not set")
}
username, ok := os.LookupEnv("OVH_USERNAME")
if !ok {
log.Fatal("OVH_USERNAME not set")
}
password, ok := os.LookupEnv("OVH_PASSWORD")
if !ok {
log.Fatal("OVH_PASSWORD not set")
}
sleepDuration := envInt("SLEEP_DURATION", 3600)
client := &http.Client{
Timeout: time.Second * 30,
}
ipAddress := ""
var prevIPAddress string
for {
prevIPAddress = ipAddress
ipAddress, err := getIPAddressWithRetry(ctx, client)
switch {
case err != nil:
notify(err)
case prevIPAddress != ipAddress:
for _, domainName := range domains {
log.Printf("Settings domain: %s to ip: %s\n", domainName, ipAddress)
requestArgs := DynDNSRequest{
IPAddress: ipAddress,
DomainName: domainName,
Username: username,
Password: password,
}
err = setDyndnsIPAddressWithRetry(ctx, client, requestArgs)
if err != nil {
notify(err)
}
}
default:
log.Println("IP address is the same, skipping OVH set")
}
time.Sleep(time.Duration(sleepDuration) * time.Second)
}
}
func getIPAddressWithRetry(ctx context.Context, client *http.Client) (string, error) {
ip, err := backoff.RetryNotifyWithData(
func() (string, error) {
return getIPAddress(ctx, client)
},
backoff.NewExponentialBackOff(),
func(err error, d time.Duration) {
log.Printf("Problem getting IP address: %s, retrying in %s\n", err, d)
},
)
if err != nil {
return "", errors.Wrapf(err, "Unable to get IP address, retries exhausted")
}
return ip, nil
}
func getIPAddress(ctx context.Context, client *http.Client) (string, error) {
req, err := http.NewRequestWithContext(ctx, "GET", "https://api.ipify.org", nil)
if err != nil {
return "", errors.Wrap(err, "Unable to create request to api.ipify.org")
}
resp, err := client.Do(req)
if err != nil {
return "", errors.Wrap(err, "Unable to get IP address")
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return "", fmt.Errorf("Unable to get IP address, got code: %d", resp.StatusCode)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return "", errors.Wrap(err, "Unable to read api.ipify.org body")
}
return string(body), nil
}
func setDyndnsIPAddressWithRetry(ctx context.Context, client *http.Client, r DynDNSRequest) error {
err := backoff.RetryNotify(
func() error {
return setDyndnsIPAddress(ctx, client, r)
},
backoff.NewExponentialBackOff(),
func(err error, d time.Duration) {
log.Printf("Unable to set dyndns ip for domain %s: %s, retrying in %s\n", r.DomainName, err, d)
},
)
if err != nil {
return errors.Wrapf(err, "Unable to set dyndns ip for domain %s, retries exhausted", r.DomainName)
}
return nil
}
func setDyndnsIPAddress(ctx context.Context, client *http.Client, r DynDNSRequest) error {
url := fmt.Sprintf("https://www.ovh.com/nic/update?system=dyndns&hostname=%s&myip=%s", r.DomainName, r.IPAddress)
req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
if err != nil {
return errors.Wrapf(err, "Unable to create request to set IP Address for domain: %s", r.DomainName)
}
req.SetBasicAuth(r.Username, r.Password)
resp, err := client.Do(req)
if err != nil {
return errors.Wrapf(err, "Unable to set IP Address for domain: %s", r.DomainName)
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return fmt.Errorf("Unable to set IP Address for domain, got code: %d", resp.StatusCode)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return errors.Wrapf(err, "Unable to read response body for domain: %s", r.DomainName)
}
log.Println(string(body))
return nil
}
func getDomains() ([]string, bool) {
domainsEnv, ok := os.LookupEnv("DOMAINS")
if !ok {
return nil, false
}
domains := strings.Split(domainsEnv, ",")
return domains, true
}
func notify(err error) {
_, ok := os.LookupEnv("BUGSNAG_API_KEY")
if ok {
bugsnag.Notify(err)
} else {
log.Println("Error: ", err)
}
}
// Gets the environment variable with the specified key and parses as an integer
// if not set then returns the fallback value
func envInt(key string, fallback int) int {
strValue, ok := os.LookupEnv(key)
if !ok {
return fallback
}
value, err := strconv.Atoi(strValue)
if err != nil {
return fallback
}
return value
}