-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
160 lines (128 loc) · 3.25 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
package main
import (
"context"
"flag"
"fmt"
"io"
"log"
"net"
"net/http"
"net/url"
"os"
"strings"
"time"
)
const (
IPv4API = "https://api.ipify.org/"
MaxIPV4StringLen = 15
MaxDynHostResponseLen = 64
DefaultDaemonDelay = 10
)
func whatsMyIPv4(ctx context.Context) (net.IP, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, IPv4API, nil)
if err != nil {
return nil, err
}
res, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
data, err := io.ReadAll(http.MaxBytesReader(nil, res.Body, MaxIPV4StringLen))
if err != nil {
return nil, err
}
ip := net.ParseIP(string(data))
if ip == nil {
return nil, fmt.Errorf("failed to decode IP: %s", string(data))
}
if ip.To4() == nil {
return nil, fmt.Errorf("not an IPv4: %s", string(data))
}
return ip, nil
}
func updateDynHostRecord(ctx context.Context, login, password, hostname string, ip net.IP) error {
params := url.Values{
"system": {"dyndns"},
"hostname": {hostname},
"ip": {ip.String()},
}
req, err := http.NewRequestWithContext(
ctx,
http.MethodGet,
fmt.Sprintf("https://www.ovh.com/nic/update?%s", params.Encode()),
nil,
)
if err != nil {
return err
}
req.SetBasicAuth(login, password)
res, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return fmt.Errorf("unexpected status code %d", res.StatusCode)
}
data, err := io.ReadAll(http.MaxBytesReader(nil, res.Body, MaxDynHostResponseLen))
if err != nil {
return err
}
sd := string(data)
if !strings.HasPrefix(sd, "nochg") && !strings.HasPrefix(sd, "good") {
return fmt.Errorf("failed to update DynHost record: %s", sd)
}
return nil
}
func main() {
ctx := context.Background()
// Flag arguments
hostname := flag.String("hostname", "", "Hostname of the DynHost record to update.")
delay := flag.Uint("delay", DefaultDaemonDelay, "If the program is run as a daemon, number "+
"of minutes to wait between updates.")
daemon := flag.Bool("daemon", false, "Run the program as a daemon.")
flag.Parse()
if *hostname == "" {
log.Fatal("Missing -hostname flag.")
}
// Env arguments
login := os.Getenv("LOGIN")
password := os.Getenv("PASSWORD")
if login == "" {
log.Fatal("Missing LOGIN environment variable.")
}
if password == "" {
log.Fatal("Missing PASSWORD environment variable.")
}
// Lookup current DynHost IP
var dynHostIP net.IP
ips, err := net.LookupIP(*hostname)
if err != nil {
log.Fatalf("Failed to lookup IP: %v", err)
}
if len(ips) == 1 {
dynHostIP = ips[0]
log.Printf("Current IP of %s is %s\n", *hostname, dynHostIP)
}
// Run main logic.
for {
currentIP, err := whatsMyIPv4(ctx)
if err != nil {
log.Fatalf("Failed to get current IP: %v", err)
}
log.Println("Current IP of client is", currentIP)
if !currentIP.Equal(dynHostIP) {
log.Println("DynHost record needs to be updated")
if err := updateDynHostRecord(ctx, login, password, *hostname, currentIP); err != nil {
log.Fatalf("Failed to update DynHost record: %v", err)
}
dynHostIP = currentIP
}
// Escape early when not running as a daemon.
if !*daemon {
break
}
time.Sleep(time.Duration(*delay) * time.Minute)
}
}