-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
221 lines (183 loc) · 5.41 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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"math/rand"
"net/http"
"os"
"strings"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/route53"
)
var (
awsAccessKeyID string
awsSecretAccessKey string
domain string
hostedZoneID string
sleepDuration int
sleepSplay int
ipv4 string
ipv6 string
)
func main() {
// Parse command-line options
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "usage: ./go-r53-dyndns\n")
flag.PrintDefaults()
}
flag.StringVar(&awsAccessKeyID, "awsAccessKeyID", os.Getenv("AWS_ACCESS_KEY_ID"), "AWS access key ID (required)")
flag.StringVar(&awsSecretAccessKey, "awsSecretAccessKey", os.Getenv("AWS_SECRET_ACCESS_KEY"), "AWS secret access key (required)")
flag.StringVar(&hostedZoneID, "hostedZoneID", os.Getenv("HOSTED_ZONE_ID"), "Hosted zone ID that contains the domain (required)")
flag.StringVar(&domain, "domain", os.Getenv("HOSTED_DOMAIN"), "(sub)domain to manage (required)")
flag.IntVar(&sleepDuration, "sleepDuration", 60, "sleep duration between checks (in seconds)")
flag.IntVar(&sleepSplay, "sleepSplay", 5, "plus/minus seconds to splay sleep by")
flag.Parse()
if awsAccessKeyID == "" || awsSecretAccessKey == "" || domain == "" || hostedZoneID == "" {
flag.Usage()
os.Exit(2)
}
log.Println("Starting up")
// Setup the route53 stuff and figure out what we have
sess, err := session.NewSession(&aws.Config{
CredentialsChainVerboseErrors: aws.Bool(true),
Credentials: credentials.NewStaticCredentials(awsAccessKeyID, awsSecretAccessKey, ""),
})
if err != nil {
log.Fatalln("Failed to create session:", err)
}
svc := route53.New(sess)
existingIPV4, existingIPV6, err := findExistingRecords(svc)
if err != nil {
log.Fatalln("Could not fetch existing records:", err)
}
log.Printf("Existing IPV4 record is: %s", existingIPV4)
log.Printf("Existing IPV6 record is: %s", existingIPV6)
// loop until killed
var shouldExit bool
for ok := true; ok; ok = !shouldExit {
// Get ipv4 address
ipv4, err := getCurrentAddress("ipv4")
if err != nil {
log.Fatalln(err)
}
log.Printf("Current IPV4 address is: %s", ipv4)
// Get ipv6 address
ipv6, err := getCurrentAddress("ipv6")
if err != nil {
log.Fatalln(err)
}
log.Printf("Current IPV6 address is: %s", ipv6)
if ipv4 == "" && ipv6 == "" {
log.Println("Neither IPV4 nor IPV6 addresses found. Cowardly giving up with no updates.")
shouldExit = true
}
var hasChanges bool
if ipv4 != existingIPV4 {
log.Println("IPV4 addresses do not match. Updating...")
hasChanges = true
}
if ipv6 != existingIPV6 {
log.Println("IPV6 addresses do not match. Updating...")
hasChanges = true
}
if hasChanges {
// TODO: Delete records if they exist, but we no longer have an address
err = setRecords(svc, ipv4, ipv6)
if err != nil {
log.Fatalln(err)
}
log.Println("OK")
existingIPV4 = ipv4
existingIPV6 = ipv6
} else {
log.Println("No changes.")
}
// Sleep until next check
log.Println("Sleeping until next check...")
time.Sleep((time.Duration(sleepDuration) * time.Second) + (time.Duration(rand.Intn(sleepSplay*2)-sleepSplay) * time.Second))
}
log.Println("Shutting down")
}
func getCurrentAddress(addressType string) (address string, err error) {
resp, err := http.Get("http://" + addressType + ".icanhazip.com/")
if err == nil {
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
return strings.TrimSpace(string(body)), nil
}
return "", nil
}
func findExistingRecords(svc *route53.Route53) (existingIPV4, existingIPV6 string, err error) {
listParams := &route53.ListResourceRecordSetsInput{
HostedZoneId: aws.String(hostedZoneID),
}
respList, err := svc.ListResourceRecordSets(listParams)
if err != nil {
return "", "", err
}
for _, rs := range respList.ResourceRecordSets {
if aws.StringValue(rs.Name) != domain && aws.StringValue(rs.Name) != domain+"." {
continue
}
if aws.StringValue(rs.Type) == "A" {
existingIPV4 = aws.StringValue(rs.ResourceRecords[0].Value)
}
if aws.StringValue(rs.Type) == "AAAA" {
existingIPV6 = aws.StringValue(rs.ResourceRecords[0].Value)
}
}
return existingIPV4, existingIPV6, nil
}
func setRecords(svc *route53.Route53, ipv4Address, ipv6Address string) (err error) {
var changes []*route53.Change
if ipv4Address != "" {
changes = append(changes, &route53.Change{
Action: aws.String("UPSERT"),
ResourceRecordSet: &route53.ResourceRecordSet{
Name: aws.String(domain),
ResourceRecords: []*route53.ResourceRecord{
{
Value: aws.String(ipv4Address),
},
},
TTL: aws.Int64(300),
Type: aws.String("A"),
},
})
}
if ipv6Address != "" {
changes = append(changes, &route53.Change{
Action: aws.String("UPSERT"),
ResourceRecordSet: &route53.ResourceRecordSet{
Name: aws.String(domain),
ResourceRecords: []*route53.ResourceRecord{
{
Value: aws.String(ipv6Address),
},
},
TTL: aws.Int64(300),
Type: aws.String("AAAA"),
},
})
}
input := &route53.ChangeResourceRecordSetsInput{
ChangeBatch: &route53.ChangeBatch{
Changes: changes,
Comment: aws.String("Mainted by go-r53-dyndns"),
},
HostedZoneId: aws.String(hostedZoneID),
}
_, err = svc.ChangeResourceRecordSets(input)
if err != nil {
return err
}
return nil
}