-
Notifications
You must be signed in to change notification settings - Fork 0
/
dns_edit.go
197 lines (171 loc) · 4.39 KB
/
dns_edit.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
package lbapi
import (
"errors"
"fmt"
"net/url"
)
// EditARecord modifies A or AAAA records.
func (c *Client) EditARecord(domain, oldip, newip, host string, ttl int64, six bool) error {
if six {
return c.editRecord(APIDNSUpdateIPv6, domain, oldip, newip, host, ttl)
}
return c.editRecord(APIDNSUpdateIPv4, domain, oldip, newip, host, ttl)
}
// EditCNAME modifies CNAME (canonical name) records.
func (c *Client) EditCNAME(domain, oldip, newip, host string, ttl int64) error {
return c.editRecord(APIDNSUpdateCNAME, domain, oldip, newip, host, ttl)
}
// EditMX modifies MX (mail server) records.
func (c *Client) EditMX(domain, oldip, newip, host string, ttl int64, priority uint16) error {
return c.editRecordPri(APIDNSUpdateMX, domain, oldip, newip, host, ttl, priority)
}
// EditNS modifies NS (nameserver) records.
func (c *Client) EditNS(domain, oldip, newip, host string, ttl int64) error {
return c.editRecord(APIDNSUpdateNS, domain, oldip, newip, host, ttl)
}
// EditTXT modfies TXT records.
func (c *Client) EditTXT(domain, oldip, newip, host string, ttl int64) error {
return c.editRecord(APIDNSUpdateTXT, domain, oldip, newip, host, ttl)
}
func (c *Client) editRecord(call, domain, oldip, newip, host string, ttl int64) error {
var err error
u, err := url.Parse(c.URL)
if err != nil {
return err
}
u.Path = call
q := u.Query()
q.Set("auth-userid", c.ID)
q.Set("api-key", c.Key)
q.Set("domain-name", domain)
q.Set("current-value", oldip)
q.Set("new-value", newip)
if host != "" {
q.Set("host", host)
}
if ttl == 0 || ttl < 7200 {
ttl = 7200
}
q.Set("ttl", fmt.Sprintf("%d", ttl))
u.RawQuery = q.Encode()
res, err := PostResponse(c.Client, u.String())
if err != nil {
return err
}
list := *res
if list["status"] == "ERROR" {
return errors.New((fmt.Sprintf("%v", list["message"])))
}
return nil
}
func (c *Client) editRecordPri(call, domain, oldip, newip, host string, ttl int64, priority uint16) error {
var err error
u, err := url.Parse(c.URL)
if err != nil {
return err
}
u.Path = call
q := u.Query()
q.Set("auth-userid", c.ID)
q.Set("api-key", c.Key)
q.Set("domain-name", domain)
q.Set("current-value", oldip)
q.Set("new-value", newip)
if host != "" {
q.Set("host", host)
}
if ttl == 0 || ttl < 7200 {
ttl = 7200
}
q.Set("ttl", fmt.Sprintf("%d", ttl))
q.Set("priority", fmt.Sprintf("%d", priority))
u.RawQuery = q.Encode()
res, err := PostResponse(c.Client, u.String())
if err != nil {
return err
}
list := *res
if list["status"] == "ERROR" {
return errors.New((fmt.Sprintf("%v", list["message"])))
}
return nil
}
// EditSRV modifies a SRV (service) record.
func (c *Client) EditSRV(domain, oldval, newval, host string, ttl int64, priority, port, weight uint) error {
var err error
u, err := url.Parse(c.URL)
if err != nil {
return err
}
u.Path = APIDNSUpdateSRV
q := u.Query()
q.Set("auth-userid", c.ID)
q.Set("api-key", c.Key)
q.Set("domain-name", domain)
q.Set("current-value", oldval)
q.Set("new-value", newval)
q.Set("host", host)
if ttl < 7200 {
ttl = 7200
}
q.Set("ttl", fmt.Sprintf("%d", ttl))
if priority > 0 {
q.Set("priority", fmt.Sprintf("%d", priority))
}
if port > 0 {
q.Set("port", fmt.Sprintf("%d", port))
}
if weight > 0 {
q.Set("weight", fmt.Sprintf("%d", weight))
}
u.RawQuery = q.Encode()
res, err := PostResponse(c.Client, u.String())
if err != nil {
return err
}
list := *res
if list["status"] == "ERROR" {
return errors.New((fmt.Sprintf("%v", list["message"])))
}
return nil
}
// EditSOA modifies a SOA (Start of Authority) record.
func (c *Client) EditSOA(domain, person string, refresh, retry, expire, ttl int64) error {
var err error
u, err := url.Parse(c.URL)
if err != nil {
return err
}
u.Path = APIDNSUpdateSOA
q := u.Query()
q.Set("auth-userid", c.ID)
q.Set("api-key", c.Key)
q.Set("domain-name", domain)
q.Set("responsible-person", person)
if refresh < 7200 {
refresh = 7200
}
q.Set("refresh", fmt.Sprintf("%d", refresh))
if retry < 7200 {
retry = 7200
}
q.Set("retry", fmt.Sprintf("%d", retry))
if expire < 172800 {
expire = 172800
}
q.Set("expire", fmt.Sprintf("%d", expire))
if ttl < 14400 {
ttl = 14400
}
q.Set("ttl", fmt.Sprintf("%d", ttl))
u.RawQuery = q.Encode()
res, err := PostResponse(c.Client, u.String())
if err != nil {
return err
}
list := *res
if list["status"] == "ERROR" {
return errors.New((fmt.Sprintf("%v", list["message"])))
}
return nil
}