-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
167 lines (133 loc) · 3.88 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
package main
import (
"flag"
"io"
"log"
"net/http"
"os"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/route53"
"gopkg.in/yaml.v3"
)
type ConfigurationFile struct {
GetIpAddressHost string `yaml:"get_ip_address_host"`
Sites []ConfigurationFileSite `yaml:"sites"`
}
type ConfigurationFileSite struct {
HostedZoneId string `yaml:"hosted_zone_id"`
Ttl int64 `yaml:"ttl"`
Ipv6 bool `yaml:"ipv6"`
Comment string `yaml:"comment"`
RecordNames []string `yaml:"record_names"`
}
var (
flagConfigurationFile string
)
func init() {
flag.StringVar(&flagConfigurationFile, "config", "conf/configuration.yaml", "Configuration file to use")
}
func main() {
flag.Parse()
cf := processConfiguration()
// Get our IP address
c := make(chan string)
go func() {
httpResponse, err := http.Get(cf.GetIpAddressHost)
if err != nil {
log.Fatal("Error retrieving IP address:", err)
}
defer httpResponse.Body.Close()
responseBodyIPAddress, err := io.ReadAll(httpResponse.Body)
if err != nil {
log.Fatal("Error reading IP address response:", err)
}
c <- string(responseBodyIPAddress)
}()
ipAddress := <-c
// Prepare the Route53 changes
for _, cfs := range cf.Sites {
var route53Changes []*route53.Change
var route53Type *string
if cfs.Ipv6 {
route53Type = aws.String("AAAA")
} else {
route53Type = aws.String("A")
}
for _, recordName := range cfs.RecordNames {
route53Changes = append(route53Changes, &route53.Change{
Action: aws.String("UPSERT"),
ResourceRecordSet: &route53.ResourceRecordSet{
Name: aws.String(recordName),
TTL: aws.Int64(cfs.Ttl),
Type: route53Type,
ResourceRecords: []*route53.ResourceRecord{
{
Value: aws.String(ipAddress),
},
},
},
})
}
route53Request := &route53.ChangeResourceRecordSetsInput{
ChangeBatch: &route53.ChangeBatch{
Changes: route53Changes,
Comment: aws.String(cfs.Comment),
},
HostedZoneId: aws.String(cfs.HostedZoneId),
}
// Make & handle the Route53 request (after the IP address has been collected)
awsSession, err := session.NewSession(&aws.Config{
Region: aws.String("us-east-1"), // Route 53 requires this region,
})
if err != nil {
log.Fatal("Error instantiating AWS session:", err)
}
route53Client := route53.New(awsSession)
if _, err = route53Client.ChangeResourceRecordSets(route53Request); err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
case route53.ErrCodeNoSuchHostedZone:
log.Println(route53.ErrCodeNoSuchHostedZone, aerr.Error())
case route53.ErrCodeNoSuchHealthCheck:
log.Println(route53.ErrCodeNoSuchHealthCheck, aerr.Error())
case route53.ErrCodeInvalidChangeBatch:
log.Println(route53.ErrCodeInvalidChangeBatch, aerr.Error())
case route53.ErrCodeInvalidInput:
log.Println(route53.ErrCodeInvalidInput, aerr.Error())
case route53.ErrCodePriorRequestNotComplete:
log.Println(route53.ErrCodePriorRequestNotComplete, aerr.Error())
default:
log.Fatal(aerr.Error())
}
} else {
log.Fatal(err.Error())
}
}
}
log.Println("IP address successfully updated to:", ipAddress)
}
func processConfiguration() *ConfigurationFile {
configurationFileBody, err := os.ReadFile(flagConfigurationFile)
if err != nil {
log.Fatal("Error reading configuration file:", err)
}
var cf ConfigurationFile
if err = yaml.Unmarshal(configurationFileBody, &cf); err != nil {
log.Fatal("Error decoding configuration YAML:", err)
}
if cf.GetIpAddressHost == "" {
cf.GetIpAddressHost = "https://api.ipify.org"
}
for index, cfs := range cf.Sites {
if cfs.Ttl == 0 {
cfs.Ttl = 300
}
if cfs.Comment == "" {
cfs.Comment = "AWS Route53 DDNS Client"
}
cf.Sites[index] = cfs
}
return &cf
}