-
Notifications
You must be signed in to change notification settings - Fork 2
/
dyndns.go
179 lines (147 loc) · 4.08 KB
/
dyndns.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
package main
import (
"fmt"
"io"
"log"
"net"
"github.com/pkg/errors"
)
// DynDNS holds all the required dependencies
type DynDNS struct {
gandiClient *gandiClient
discordClient *discordClient
}
type IPAddrs struct {
V4 *net.IP `json:"IPAddress"`
V6 *net.IP `json:"IPv6Address"`
}
func (ipAddrs *IPAddrs) String() string {
str := "["
if ipAddrs.V4 != nil {
str += ipAddrs.V4.String()
}
if ipAddrs.V6 != nil {
str += " " + ipAddrs.V6.String()
}
str += "]"
return str
}
func (ipAddrs *IPAddrs) values() []*net.IP {
values := make([]*net.IP, 0, 2)
if ipAddrs.V4 != nil {
values = append(values, ipAddrs.V4)
}
if ipAddrs.V6 != nil {
values = append(values, ipAddrs.V6)
}
return values
}
// resolveIPs finds the current IP(s) addresses pointu
func (d *DynDNS) resolveIPs() (*IPAddrs, error) {
res, err := defaultHTTP.Get("https://api64.ipify.org")
if err != nil {
return nil, err
}
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
return nil, err
}
ip := net.ParseIP(string(body))
if ip == nil {
return nil, fmt.Errorf("failed to parse ip: %s", body)
}
if ip.To4() != nil {
// if ipv4 return here because there are not IPv6
return &IPAddrs{V4: &ip}, nil
}
res, err = defaultHTTP.Get("https://api.ipify.org")
if err != nil {
return nil, err
}
defer res.Body.Close()
body, err = io.ReadAll(res.Body)
if err != nil {
return nil, err
}
ip2 := net.ParseIP(string(body))
if ip2 == nil {
return nil, fmt.Errorf("failed to parse ip: %s", body)
}
return &IPAddrs{V6: &ip, V4: &ip2}, nil
}
// execute check the current IPs, and the one defines in the DNS records.
// If necessary, it updates the DNS records and notify Discord.
func (dyndns *DynDNS) execute(domain string, record string, ttl int, alwaysNotify bool) error {
resolvedIPs, err := dyndns.resolveIPs()
if err != nil {
return err
}
log.Printf("Current dynamic IP(s): %s\n", resolvedIPs)
dnsRecords, err := dyndns.gandiClient.get(domain, record)
if err != nil {
return err
}
needUpdate := dyndns.matchIPs(resolvedIPs, dnsRecords)
if !needUpdate {
log.Println("IP address(es) match - no further action")
if alwaysNotify {
err := dyndns.discordClient.postInfo(&Webhook{
Embeds: []Embed{
{
Title: fmt.Sprintf("IP address(es) match for record %s.%s - no further action", record, domain),
Description: "To disable notifications when nothing happens, remove the `--always-notify` flag",
},
},
})
return errors.Wrap(err, "failed to send message to discord")
}
return nil
}
err = dyndns.gandiClient.put(domain, record, []*net.IP{resolvedIPs.V4, resolvedIPs.V6}, ttl)
if err != nil {
return err
}
log.Printf("DNS record for %s.%s updated\n", record, domain)
err = dyndns.notifyDiscord(domain, record, resolvedIPs.values())
return err
}
func (dyndns *DynDNS) notifyDiscord(domain string, record string, ips []*net.IP) error {
fields := make([]Field, 0, len(ips))
for _, ip := range ips {
field := &Field{Inline: true, Value: ip.String()}
if ip.To4() != nil {
field.Name = "v4"
} else {
field.Name = "v6"
}
fields = append(fields, *field)
}
err := dyndns.discordClient.postSuccess(&Webhook{
Embeds: []Embed{
{
Title: fmt.Sprintf("DNS record for %s.%s updated with the new IP adresses", record, domain),
Description: fmt.Sprintf("See [Gandi Live DNS](https://admin.gandi.net/domain/%s/records)", domain),
Fields: fields,
},
},
})
return errors.Wrap(err, "failed to post success message to Discord")
}
func (dyndns *DynDNS) matchIPs(resolvedIPs *IPAddrs, dnsRecords []*domainRecord) bool {
ipsFromDNS := make([]*net.IP, 0, 2)
var foundIPV4 bool
var foundIPV6 bool
for _, records := range dnsRecords {
for _, rrsetValue := range records.RrsetValues {
ipsFromDNS = append(ipsFromDNS, rrsetValue)
if resolvedIPs.V4 != nil && rrsetValue.Equal(*resolvedIPs.V4) {
foundIPV4 = true
} else if resolvedIPs.V6 != nil && rrsetValue.Equal(*resolvedIPs.V6) {
foundIPV6 = true
}
}
}
log.Printf("IP(s) from DNS: %s", ipsFromDNS)
return !foundIPV4 || !foundIPV6
}