-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdns.go
153 lines (131 loc) · 3.47 KB
/
dns.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
package lbapi
import (
"fmt"
"net/url"
)
// DNSRecordList is what client software gets.
// It's not guaranteed to hold all records, so check Count against MaxRecords.
type DNSRecordList struct {
// Count of records returned in this structure.
Count int64
// MaxRecords is the total available for this query.
MaxRecords int64
// Domains for the specified search query.
Records DNSRecords
}
// DNSRecords is a special sortable structure.
type DNSRecords []*DNSRecord
// Len reports the number of records.
func (slice DNSRecords) Len() int {
return len(slice)
}
// Less checks if host name i comes before host name j.
func (slice DNSRecords) Less(i, j int) bool {
return slice[i].Host < slice[j].Host
}
// Swap does what it says.
func (slice DNSRecords) Swap(i, j int) {
slice[i], slice[j] = slice[j], slice[i]
}
// DNSRecord is an individual record.
type DNSRecord struct {
TTL int64 // 7200 is a safe default
Priority uint16 // Only available for some record types
Port uint16 // Only available for some record types
Weight uint16 // Only available for some record types
Host string // subdomain or @ for the primary domain
Type string // A, AAAA, MX etc.
Value string // IPv4 or IPv6 address
Status string // Normally "Active"
}
// DNSActive reports if an order has activated DNS yet.
// This is normally on by default, but will be activated when
// this is called otherwise.
func (c *Client) DNSActive(id string) bool {
var err error
u, err := url.Parse(c.URL)
if err != nil {
return false
}
u.Path = "api/dns/activate.json"
q := u.Query()
q.Set("auth-userid", c.ID)
q.Set("api-key", c.Key)
q.Set("order-id", id)
u.RawQuery = q.Encode()
res, err := PostResponse(c.Client, u.String())
if err != nil {
return false
}
list := *res
return list["status"] == "Success"
}
// GetDNSRecords gets the first up to 50 records of one type for a domain.
// Pass a higher page number to get the next set of up to 50.
func (c *Client) GetDNSRecords(domain, value, host, t string, page int) (*DNSRecordList, error) {
var err error
u, err := url.Parse(c.URL)
if err != nil {
return nil, err
}
if page == 0 {
page = 1
}
u.Path = "api/dns/manage/search-records.json"
q := u.Query()
q.Set("auth-userid", c.ID)
q.Set("api-key", c.Key)
q.Set("domain-name", domain)
q.Set("type", t)
q.Set("no-of-records", "50")
q.Set("page-no", fmt.Sprintf("%d", page))
if host != "" {
q.Set("host", host)
}
if value != "" {
q.Set("value", value)
}
u.RawQuery = q.Encode()
res, err := GetResponse(c.Client, u.String())
if err != nil {
return nil, err
}
list := *res
rl := DNSRecordList{
Count: Atoi(fmt.Sprintf("%v", list["recsonpage"])),
MaxRecords: Atoi(fmt.Sprintf("%v", list["recsindb"])),
}
delete(list, "recsonpage")
delete(list, "recsindb")
for _, rec := range list {
r := ParseDNS(rec)
if r != nil {
rl.Records = append(rl.Records, r)
}
}
return &rl, nil
}
// ParseDNS converts a k-v table to a DNSRecord.
func ParseDNS(in interface{}) *DNSRecord {
data := in.(map[string]interface{})
dns := DNSRecord{
Host: data["host"].(string),
Type: data["type"].(string),
Value: data["value"].(string),
TTL: Atoi(data["timetolive"].(string)),
Status: data["status"].(string),
}
pri, ok := data["priority"].(string)
if ok {
dns.Priority = uint16(Atoi(pri))
}
port, ok := data["port"].(string)
if ok {
dns.Port = uint16(Atoi(port))
}
w, ok := data["weight"].(string)
if ok {
dns.Weight = uint16(Atoi(w))
}
return &dns
}