-
Notifications
You must be signed in to change notification settings - Fork 378
/
api.go
221 lines (178 loc) · 4.5 KB
/
api.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package realtimeregister
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
)
type realtimeregisterAPI struct {
apikey string
endpoint string
Zones map[string]*Zone //cache
ServiceType string
}
type Zones struct {
Entities []Zone `json:"entities"`
}
type Domain struct {
Nameservers []string `json:"ns"`
}
type Zone struct {
Name string `json:"name,omitempty"`
Service string `json:"service,omitempty"`
ID int `json:"id,omitempty"`
Records []Record `json:"records"`
Dnssec bool `json:"dnssec"`
}
type Record struct {
Name string `json:"name"`
Type string `json:"type"`
Content string `json:"content"`
Priority int `json:"prio,omitempty"`
TTL int `json:"ttl"`
}
const (
endpoint = "https://api.yoursrs.com/v2"
endpointSandbox = "https://api.yoursrs-ote.com/v2"
)
func (api *realtimeregisterAPI) request(method string, url string, body io.Reader) ([]byte, error) {
client := &http.Client{}
req, _ := http.NewRequest(
method,
url,
body,
)
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "application/json")
req.Header.Set("Authorization", "ApiKey "+api.apikey)
resp, err := client.Do(req)
if err != nil {
return nil, err
}
bodyString, _ := io.ReadAll(resp.Body)
if resp.StatusCode != 200 {
return nil, fmt.Errorf("realtime Register API error on request to %s: %d, %s", url, resp.StatusCode,
string(bodyString))
}
return bodyString, nil
}
func (api *realtimeregisterAPI) getZone(domain string) (*Zone, error) {
zones, err := api.getDomainZones(domain)
if err != nil {
return nil, err
}
if len(zones.Entities) == 0 {
return nil, fmt.Errorf("zone %s does not exist", domain)
}
api.Zones[domain] = &zones.Entities[0]
return &zones.Entities[0], nil
}
func (api *realtimeregisterAPI) getDomainZones(domain string) (*Zones, error) {
url := fmt.Sprintf(api.endpoint+"/dns/zones?name=%s&service=%s", domain, api.ServiceType)
return api.getZones(url)
}
func (api *realtimeregisterAPI) getAllZones() ([]string, error) {
url := fmt.Sprintf(api.endpoint+"/dns/zones?service=%s&export=true&fields=id,name", api.ServiceType)
zones, err := api.getZones(url)
if err != nil {
return nil, err
}
zoneNames := make([]string, len(zones.Entities))
for i, zone := range zones.Entities {
zoneNames[i] = zone.Name
}
return zoneNames, nil
}
func (api *realtimeregisterAPI) getZones(url string) (*Zones, error) {
bodyBytes, err := api.request(
"GET",
url,
nil,
)
if err != nil {
return nil, err
}
respData := &Zones{}
err = json.Unmarshal(bodyBytes, &respData)
if err != nil {
return nil, err
}
return respData, nil
}
func (api *realtimeregisterAPI) createZone(domain string) error {
zone := &Zone{
Records: []Record{},
Name: domain,
Service: api.ServiceType,
}
err := api.createOrUpdateZone(zone, api.endpoint+"/dns/zones")
if err != nil {
return err
}
return nil
}
func (api *realtimeregisterAPI) zoneExists(domain string) (bool, error) {
if api.Zones[domain] != nil {
return true, nil
}
zones, err := api.getDomainZones(domain)
if err != nil {
return false, err
}
return len(zones.Entities) > 0, nil
}
func (api *realtimeregisterAPI) getDomainNameservers(domainName string) ([]string, error) {
respData, err := api.request(
"GET",
fmt.Sprintf(api.endpoint+"/domains/%s", domainName),
nil,
)
if err != nil {
return nil, err
}
domain := &Domain{}
err = json.Unmarshal(respData, &domain)
if err != nil {
return nil, err
}
return domain.Nameservers, nil
}
func (api *realtimeregisterAPI) updateZone(domain string, body *Zone) error {
return api.createOrUpdateZone(
body,
fmt.Sprintf(api.endpoint+"/dns/zones/%d/update", api.Zones[domain].ID),
)
}
func (api *realtimeregisterAPI) updateNameservers(domainName string, nameservers []string) error {
domain := &Domain{
Nameservers: nameservers,
}
bodyBytes, err := json.Marshal(domain)
if err != nil {
return err
}
_, err = api.request(
"POST",
fmt.Sprintf(api.endpoint+"/domains/%s/update", domainName),
bytes.NewReader(bodyBytes),
)
if err != nil {
return err
}
return nil
}
func (api *realtimeregisterAPI) createOrUpdateZone(body *Zone, url string) error {
bodyBytes, err := json.Marshal(body)
if err != nil {
return err
}
//Ugly hack for MX records with null target
requestBody := strings.Replace(string(bodyBytes), "\"prio\":-1", "\"prio\":0", -1)
_, err = api.request("POST", url, strings.NewReader(requestBody))
if err != nil {
return err
}
return nil
}