-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdns.go
More file actions
172 lines (142 loc) · 3.47 KB
/
dns.go
File metadata and controls
172 lines (142 loc) · 3.47 KB
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
package checkhost
import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"strings"
"time"
)
type DNSResultItem struct {
A []string
AAAA []string
TTL *int
Error string
}
type DNSResult map[string]*DNSResultItem
type CheckDNSResponse struct {
OK int `json:"ok"`
RequestID string `json:"request_id"`
PermanentLink string `json:"permanent_link"`
Nodes map[string][]string `json:"nodes"`
}
func (c *Client) CheckDNS(d RequestData) (*CheckDNSResponse, error) {
var reqData string
if len(d.Nodes) != 0 {
reqData = "node=" + strings.Join(d.Nodes, "&node=")
} else if d.MaxNodes != 0 {
reqData = fmt.Sprintf("max_nodes=%d", d.MaxNodes)
} else {
reqData = "max_nodes=3"
}
req, _ := http.NewRequest(
http.MethodGet,
fmt.Sprintf("%s/check-dns?host=%s&%s", c.baseURL, url.QueryEscape(d.Host), reqData),
nil,
)
req.Header.Set("Accept", "application/json")
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
var result CheckDNSResponse
if err := json.Unmarshal(body, &result); err != nil {
return nil, fmt.Errorf("invalid JSON: %v\nBody: %s", err, string(body))
}
return &result, nil
}
func (c *Client) DNSResult(requestID string) (DNSResult, error) {
urlStr := fmt.Sprintf("%s/check-result/%s", c.baseURL, requestID)
req, _ := http.NewRequest(http.MethodGet, urlStr, nil)
req.Header.Set("Accept", "application/json")
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
var result DNSResult
if err := json.Unmarshal(body, &result); err != nil {
return nil, fmt.Errorf("invalid JSON: %v\nBody: %s", err, string(body))
}
return result, nil
}
func (c *Client) WaitDNSResult(requestID string, timeout, interval time.Duration) (DNSResult, error) {
deadline := time.Now().Add(timeout)
for time.Now().Before(deadline) {
res, err := c.DNSResult(requestID)
if err != nil {
return nil, err
}
done := true
for _, v := range res {
if v == nil || (len(v.A) == 0 && len(v.AAAA) == 0 && v.Error == "") {
done = false
break
}
}
if done {
return res, nil
}
time.Sleep(interval)
}
return nil, ErrTimeout
}
func (r *DNSResultItem) UnmarshalJSON(data []byte) error {
if string(data) == "null" {
return nil
}
var arr []map[string]any
if err := json.Unmarshal(data, &arr); err == nil && len(arr) > 0 {
m := arr[0]
if v, ok := m["A"].([]any); ok {
for _, a := range v {
if s, ok := a.(string); ok {
r.A = append(r.A, s)
}
}
}
if v, ok := m["AAAA"].([]any); ok {
for _, a := range v {
if s, ok := a.(string); ok {
r.AAAA = append(r.AAAA, s)
}
}
}
if ttl, ok := m["TTL"].(float64); ok {
i := int(ttl)
r.TTL = &i
}
return nil
}
var obj map[string]any
if err := json.Unmarshal(data, &obj); err == nil {
if s, ok := obj["error"].(string); ok {
r.Error = s
}
}
return nil
}
func (r *DNSResult) UnmarshalJSON(data []byte) error {
raw := map[string]json.RawMessage{}
if err := json.Unmarshal(data, &raw); err != nil {
return err
}
res := make(DNSResult)
for node, rawNode := range raw {
if string(rawNode) == "null" {
res[node] = nil
continue
}
item := &DNSResultItem{}
if err := item.UnmarshalJSON(rawNode); err != nil {
return fmt.Errorf("failed to unmarshal node %s: %w", node, err)
}
res[node] = item
}
*r = res
return nil
}