forked from yalp/gandyn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
180 lines (158 loc) · 4.13 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
package main
import (
"bytes"
"encoding/json"
"errors"
"flag"
"fmt"
"io"
"log"
"net"
"net/http"
"os"
"os/exec"
"time"
)
var (
apiKey string
zoneID string
recordName string
refresh time.Duration
resolver string
hostname string
)
const (
baseURL = "https://dns.api.gandi.net/api/v5/zones"
)
// Define and parse flags
func init() {
flag.StringVar(&apiKey, "apikey", "", "Mandatory. API key to access server platform")
flag.StringVar(&zoneID, "zone", "", "Mandatory. Zone uuid")
flag.StringVar(&recordName, "record", "", "Mandatory. Record to update")
flag.DurationVar(&refresh, "refresh", 5*time.Minute, "Delay between checks for public IP address updates")
flag.StringVar(&resolver, "resolver", "resolver1.opendns.com", "The resolver to check use for `myip` record")
flag.StringVar(&hostname, "myip", "myip.opendns.com", "The hostname of the record to use to check for current IP")
}
type publicIPResolver struct {
Hostname string
Server string
}
// Resolve gets the current pblic IP
func (p *publicIPResolver) Resolve() (string, error) {
output, err := exec.Command("dig", "+time=1", "+short", p.Hostname, "@"+p.Server).Output()
if err != nil {
return "", err
}
if len(output) == 0 {
//fail.
return "", errors.New("no ipv4 valid address")
}
stringIP := string(output[0 : len(output)-1]) //output has a trailing newline
ip := net.ParseIP(stringIP)
if ip == nil || ip.To4() == nil {
return "", errors.New("no ipv4 valid address")
}
return stringIP, nil
}
type liveDNSRecord struct {
Kind string `json:"rrset_type,omitempty"`
Name string `json:"rrset_name,omitempty"`
TTL uint `json:"rrset_ttl,omitempty"`
Values []string `json:"rrset_values,omitempty"`
}
type liveDNSConfig struct {
Key string
Zone string
Record string
}
func (l *liveDNSConfig) req(method string, body io.Reader) (*http.Response, error) {
url := fmt.Sprintf("%s/%s/records/%s/A", baseURL, l.Zone, l.Record)
//log.Println(url)
req, err := http.NewRequest(method, url, body)
if err != nil {
return nil, err
}
req.Header.Set("X-Api-Key", l.Key)
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "application/json")
return http.DefaultClient.Do(req)
}
// Get gets the Current value of the record
func (l *liveDNSConfig) Get() (string, error) {
res, err := l.req("GET", nil)
if err != nil {
return "", err
}
record := &liveDNSRecord{}
defer res.Body.Close()
if err := json.NewDecoder(res.Body).Decode(record); err != nil {
return "", err
}
if record.Values == nil || len(record.Values) == 0 || record.Values[0] == "" {
//log.Println(record)
return "", errors.New("Invalid Record Response")
}
return record.Values[0], nil
}
// Set sets the value of the Record
func (l *liveDNSConfig) Set(ip string) error {
body := &bytes.Buffer{}
err := json.NewEncoder(body).Encode(&liveDNSRecord{TTL: 300, Values: []string{ip}})
if err != nil {
return err
}
res, err := l.req("PUT", body)
if err != nil {
return err
}
// we should get a created code
if res.StatusCode != http.StatusCreated {
return fmt.Errorf("Unexpected Response Status Code [%d]", res.StatusCode)
}
return nil
}
func main() {
flag.Parse()
if apiKey == "" || recordName == "" || zoneID == "" {
fmt.Println("Missing one or more command line options.")
flag.PrintDefaults()
os.Exit(2)
}
dyndns := &liveDNSConfig{
Key: apiKey,
Zone: zoneID,
Record: recordName,
}
publicip := &publicIPResolver{
Hostname: hostname,
Server: resolver,
}
var registeredIP, currentIP string
var err error
loop := func() {
// Get the current public address
currentIP, err = publicip.Resolve()
if err != nil {
log.Println("Error: failed to get pulic IP:", err)
return
}
if registeredIP == "" {
registeredIP, err = dyndns.Get()
if err != nil {
log.Println("Error: failed to to get current dyndns record:", err)
return
}
if registeredIP != currentIP {
if err = dyndns.Set(currentIP); err != nil {
log.Println("Error: updating DNS record:", err)
return
}
log.Print("Info: updated Gandi records with IP:", currentIP)
}
}
}
for {
loop()
time.Sleep(refresh)
}
}