-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.go
58 lines (48 loc) · 1.07 KB
/
app.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"time"
)
func main() {
username := os.Getenv("username")
password := os.Getenv("password")
for {
ip, err := getIPv4Address()
if err != nil {
continue
}
updateInwxDynDns(ip, username, password)
time.Sleep(30 * time.Second)
}
}
type IPv4Response struct {
Ip string `json:"ip"`
}
func getIPv4Address() (string, error) {
resp, err := http.Get("https://api.ipify.org?format=json")
if err != nil {
return "", err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
response := IPv4Response{}
json.Unmarshal(body, &response)
fmt.Println("router ip address: ", response.Ip)
return response.Ip, nil
}
func updateInwxDynDns(ip string, username string, password string) error {
url := "https://dyndns.inwx.com/nic/update?myip=" + ip
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return err
}
req.SetBasicAuth(username, password)
client := &http.Client{}
resp, err := client.Do(req)
fmt.Println("updating dyndns:", resp.StatusCode)
return nil
}