-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
194 lines (154 loc) · 3.98 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
package main
import (
"errors"
"log"
"net"
"context"
"time"
"flag"
"io/ioutil"
"strings"
hetzner_dns "github.com/panta/go-hetzner-dns"
)
type Rule struct {
CurrentIp net.IP
IfaceName string
DnsRecord string
}
type DnsClient struct {
client hetzner_dns.Client
zoneId string
}
func main() {
var iface string
var record string
var secretPath string
var timeBetweanChecks int
var timeBetweanUpdates int
flag.StringVar(&iface, "i", "", "iface")
flag.StringVar(&record, "r", "", "record")
flag.StringVar(&secretPath, "s", "/etc/node-ddns-controller/key", "secret path")
flag.IntVar(&timeBetweanChecks, "c", 30, "timeBetweanChecks")
flag.IntVar(&timeBetweanUpdates, "u", 60*60, "timeBetweanUpdates")
flag.Parse()
if (iface == "" || record == "") {
log.Fatal("-i and -r must be set")
}
log.Printf("loading secret from %s", secretPath)
secret, err := ioutil.ReadFile(secretPath)
if err != nil {
log.Fatal(err)
}
rule := Rule{nil, iface, record}
client, err := NewDnsClient("d4rk.io", strings.TrimSuffix(string(secret), "\n"))
if err != nil {
log.Fatal(err)
}
tickerCheckAndUpdate := time.NewTicker(time.Duration(timeBetweanChecks) * time.Second)
tickerUpdate := time.NewTicker(time.Duration(timeBetweanUpdates) * time.Second)
if rule.updateLocalIp() {
client.updateRecord(rule.CurrentIp.String(), rule.DnsRecord)
}
log.Println("waiting ...")
for {
select {
case <- tickerCheckAndUpdate.C:
if rule.updateLocalIp() {
client.updateRecord(rule.CurrentIp.String(), rule.DnsRecord)
}
log.Println("waiting ...")
case <- tickerUpdate.C:
if rule.CurrentIp != nil {
client.updateRecord(rule.CurrentIp.String(), rule.DnsRecord)
}
}
}
}
func NewDnsClient(name string, apiKey string) (*DnsClient, error) {
client := hetzner_dns.Client{ApiKey: apiKey}
zonesResponse, err := client.GetZones(context.Background(), "", "", 1, 100)
if err != nil {
return nil, err
}
for _, zone := range zonesResponse.Zones {
if zone.Name == name {
log.Printf("found zoneid: %s for %s", zone.ID, zone.Name)
return &DnsClient{client, zone.ID}, nil
}
}
return nil, errors.New("Zone not found")
}
func (dc *DnsClient) updateRecord(value string, name string) error {
log.Printf("Update dns record, zoneid: %s, name: %s, value %s", dc.zoneId, value, name)
recordResponse, err := dc.client.CreateOrUpdateRecord(context.Background(), hetzner_dns.RecordRequest{
ZoneID: dc.zoneId,
Type: "AAAA",
Name: name,
Value: value,
TTL: 60,
})
if err != nil {
return err
}
log.Println("Updated dns")
log.Printf("Created: %v\n", recordResponse.Record)
return nil
}
func (rule *Rule) updateLocalIp() bool {
log.Println("Check if ip has changed")
iface, err := getInterfaceByName(rule.IfaceName)
if err != nil {
log.Fatalln(err)
}
ip, err := getGlobalPublicIPv6(iface)
if err != nil {
log.Fatalln(err)
}
if ip.Equal(rule.CurrentIp) {
return false
}
rule.CurrentIp = ip
return true
}
func getInterfaceByName(name string) (*net.Interface, error) {
ifaces, err := net.Interfaces()
if err != nil {
return nil, err
}
for _, iface := range ifaces {
if iface.Name == name {
if iface.Flags&net.FlagUp == 0 {
return nil, errors.New("interface is down")
}
return &iface, nil
}
}
return nil, errors.New("Interface not found")
}
func getGlobalPublicIPv6(iface *net.Interface) (net.IP, error) {
addrs, err := iface.Addrs()
if err != nil {
return nil, err
}
for _, addr := range addrs {
var ip net.IP
switch v := addr.(type) {
case *net.IPNet:
ip = v.IP
case *net.IPAddr:
ip = v.IP
default:
log.Printf("Unsupported addrs type")
continue
}
var isGlobal = ip.IsGlobalUnicast()
var isPublic = !ip.IsPrivate()
var isIPv6 = nil == ip.To4()
log.Printf("%s %t %t %t", ip, isGlobal, isPublic, isIPv6)
if isGlobal && isPublic && isIPv6 {
log.Printf("Stoppin search found %s", ip)
return ip, nil
}
}
return nil, errors.New("No Global Public ipv6 not found")
}