This repository has been archived by the owner on Oct 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
170 lines (128 loc) · 3.29 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
package main
import (
"errors"
"flag"
"fmt"
"io/ioutil"
"log"
"net"
"net/http"
"strings"
"gopkg.in/ini.v1"
)
const OVHAPIEndpoint = "https://www.ovh.com/nic/update"
func main() {
configFile := flag.String(
"config",
"./config.cfg",
"path to the configuration file to uses")
dryRun := flag.Bool(
"dry",
false,
"do not actually configure the new DynHost")
showVersion := flag.Bool(
"version",
false,
"show the version of this software")
flag.Parse()
if *showVersion {
println("go-dynhost 1.0.0")
return
}
cfg, err := ini.Load(*configFile)
if err != nil {
log.Fatalf("Could not open %s: %v", *configFile, err)
}
publicIP, err := getPublicIPv4()
if err != nil {
log.Fatalf("Could not get my public IPv4 address: %v", err)
}
log.Printf("Public IP: %s", publicIP.String())
cfgSection := cfg.Section("ovh")
username := cfgSection.Key("username").String()
if username == "" {
log.Fatalf("%s: username cannot be empty", *configFile)
}
password := cfgSection.Key("password").String()
if password == "" {
log.Fatalf("%s: password cannot be empty", *configFile)
}
hostname := cfgSection.Key("hostname").String()
if hostname == "" {
log.Fatalf("%s: hostname cannot be empty", *configFile)
}
currentDynHostIP, err := getDynHostValue(hostname)
if err != nil {
log.Fatalf("Could not get the current DynHost value: %v", err)
}
log.Printf("Current DynHost value: %s", currentDynHostIP.String())
if publicIP.Equal(currentDynHostIP) {
log.Print("The current DynHost record is up-to-date; exiting.")
return
}
if *dryRun {
log.Print("Dry run; exiting.")
return
}
if err := updateDynHost(username, password, hostname, publicIP); err != nil {
log.Fatalf("Could not update the DynHost record: %v", err)
}
}
func getDynHostValue(hostname string) (net.IP, error) {
addrs, err := net.LookupIP(hostname)
if err != nil {
return nil, err
}
for _, a := range addrs {
if a.To4() != nil {
return a, nil
}
}
return nil, errors.New("no IPv4 found")
}
func getPublicIPv4() (net.IP, error) {
errIP := net.IPv4zero
res, err := http.Get("https://api.ipify.org")
if err != nil {
return errIP, err
}
defer res.Body.Close()
resCode := res.StatusCode
if resCode != http.StatusOK {
return errIP, fmt.Errorf("returned %d", resCode)
}
ipStrBytes, err := ioutil.ReadAll(res.Body)
if err != nil {
return errIP, fmt.Errorf("could not read the response: %v", err)
}
return net.ParseIP(string(ipStrBytes)).To4(), nil
}
func updateDynHost(username, password, hostname string, address net.IP) error {
req, err := http.NewRequest(http.MethodGet, OVHAPIEndpoint, nil)
if err != nil {
return err
}
req.SetBasicAuth(username, password)
q := req.URL.Query()
q.Add("system", "dyndns")
q.Add("hostname", hostname)
q.Add("myip", address.String())
req.URL.RawQuery = q.Encode()
res, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return fmt.Errorf("the OVH API replied %s", res.Status)
}
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return fmt.Errorf("could not read the response body: %v", err)
}
bodyStr := strings.Split(string(body), " ")
if len(bodyStr) < 1 || bodyStr[0] != "good" {
return fmt.Errorf("response body: %s", bodyStr)
}
return nil
}