-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathudp.go
More file actions
159 lines (133 loc) · 3.27 KB
/
udp.go
File metadata and controls
159 lines (133 loc) · 3.27 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
package checkhost
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"strings"
"time"
)
type UDPResultItem struct {
Time float64
IP string
Error string
}
type UDPResult map[string]*UDPResultItem
type CheckUDPResponse struct {
OK int `json:"ok"`
RequestID string `json:"request_id"`
PermanentLink string `json:"permanent_link"`
Nodes map[string][]string `json:"nodes"`
}
func (c *Client) CheckUDP(d RequestData) (*CheckUDPResponse, 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-udp?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 CheckUDPResponse
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) UDPResult(requestID string) (UDPResult, 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 UDPResult
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) WaitUDPResult(requestID string, timeout, interval time.Duration) (UDPResult, error) {
deadline := time.Now().Add(timeout)
for time.Now().Before(deadline) {
res, err := c.UDPResult(requestID)
if err != nil {
return nil, err
}
done := true
for _, v := range res {
if v == nil || (v.Error == "" && v.IP == "") {
done = false
break
}
}
if done {
return res, nil
}
time.Sleep(interval)
}
return nil, ErrTimeout
}
func (r *UDPResultItem) UnmarshalJSON(data []byte) error {
if bytes.Equal(data, []byte("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["time"].(float64); ok {
r.Time = v
}
if s, ok := m["address"].(string); ok {
r.IP = s
}
if s, ok := m["error"].(string); ok {
r.Error = s
}
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 *UDPResult) UnmarshalJSON(data []byte) error {
raw := map[string]json.RawMessage{}
if err := json.Unmarshal(data, &raw); err != nil {
return err
}
res := make(UDPResult)
for node, rawNode := range raw {
if string(rawNode) == "null" {
res[node] = nil
continue
}
item := &UDPResultItem{}
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
}