-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
155 lines (140 loc) · 4.31 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
package main
import (
"bytes"
"encoding/json"
"flag"
"io/ioutil"
"log"
"net/http"
"os"
"strconv"
"sync"
"time"
)
type IPRecord struct {
CurrentIP string `json:"ip"`
}
type DomainRecord struct {
Id int `json:"id"`
RecordType string `json:"type"`
Name string `json:"name"`
Data string `json:"data"`
Priority int `json:"priority"`
Port int `json:"port"`
Weight int `json:"weight"`
}
type DomainRecords struct {
ExistingDomainRecords []DomainRecord `json:"domain_records"`
Links struct{} `json:"links"`
Meta struct {
Total int64 `json:"total"`
} `json:"meta"`
}
var (
apiKey = flag.String("k", "", "DO API key")
domainName = flag.String("d", "", "base domain")
subDomain = flag.String("s", "", "subdomain to update")
)
func main() {
flag.Parse()
var initData sync.WaitGroup
// Get current public IP
if (*subDomain == "") || (*domainName == "") || (*apiKey == "") {
log.Println("Must provide a domain, subdomain and api key. Exiting...")
os.Exit(1)
}
var currentIPJSON IPRecord
initData.Add(1)
go func(currentIPRecord IPRecord) {
defer initData.Done()
currentIP, err := httpReq("GET", "https://api.ipify.org?format=json", nil)
contentsIP, err := ioutil.ReadAll(currentIP.Body)
if err != nil {
log.Println("Error reading response body for current IP: ", err)
}
err = json.Unmarshal(contentsIP, ¤tIPJSON)
if err != nil {
log.Println("Error decoding JSON for current IP: ", err)
os.Exit(1)
}
log.Println("Current IP is: ", currentIPJSON.CurrentIP)
}(currentIPJSON)
var m DomainRecords
initData.Add(1)
go func(dr DomainRecords) {
defer initData.Done()
recordListResp, err := httpReq("GET", "https://api.digitalocean.com/v2/domains/"+*domainName+"/records", nil)
if err != nil {
log.Println("Error getting domain records: ", err)
}
defer recordListResp.Body.Close()
contents, err := ioutil.ReadAll(recordListResp.Body)
if err != nil {
log.Println("Error reading response body for domain records: ", err)
}
err = json.Unmarshal(contents, &m)
if err != nil {
log.Println("Error decoding JSON for domain records: ", err)
os.Exit(1)
}
}(m)
initData.Wait()
subExistsResult, recordID := subExists(m.ExistingDomainRecords)
if subExistsResult {
// update
log.Println("subdomain exists, updating record ID: ", strconv.Itoa(recordID))
jsonStr := []byte(`{"data":"` + currentIPJSON.CurrentIP + `"}`)
updateResp, err := httpReq("PUT", "https://api.digitalocean.com/v2/domains/"+*domainName+"/records/"+strconv.Itoa(recordID), jsonStr)
if err != nil {
log.Println("Error updating subdomain record: ", err)
}
defer updateResp.Body.Close()
contents, err := ioutil.ReadAll(updateResp.Body)
if err != nil {
log.Println("Error reading response body for update subdomain record: ", err)
}
log.Println("update results: ", string(contents))
} else {
// add
log.Println("subdomain does not exist, adding...")
jsonStr := []byte(`{"type":"A","name":"` + *subDomain + `","data":"` + currentIPJSON.CurrentIP + `","priority":null,"port":null,"weight":null, "ttl":300}`)
addResp, err := httpReq("POST", "https://api.digitalocean.com/v2/domains/"+*domainName+"/records", jsonStr)
if err != nil {
log.Println("Error adding subdomain record: ", err)
os.Exit(1)
}
defer addResp.Body.Close()
contents, err := ioutil.ReadAll(addResp.Body)
if err != nil {
log.Println("Error reading response body for add subdomain record: ", err)
}
log.Println("add results: ", string(contents))
}
}
func httpReq(method string, URL string, postData []byte) (*http.Response, error) {
client := &http.Client{
Timeout: 20 * time.Second,
}
req, err := http.NewRequest(method, URL, bytes.NewBuffer(postData))
if err != nil {
log.Println("Error with building request for "+URL+": ", err)
return &http.Response{}, err
}
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Authorization", "Bearer "+*apiKey)
resp, err := client.Do(req)
if err != nil {
log.Println("Error with request for "+URL+": ", err)
return &http.Response{}, err
}
return resp, nil
}
func subExists(records []DomainRecord) (bool, int) {
for _, record := range records {
if record.Name == *subDomain {
log.Println("Found record: ", record.Name)
return true, record.Id
}
}
return false, 0
}