-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhitron.go
250 lines (205 loc) · 4.75 KB
/
hitron.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package hitron
import (
"context"
"encoding/json"
"fmt"
"io"
"log/slog"
"net/http"
"net/http/cookiejar"
"net/http/httputil"
"net/url"
"strconv"
"strings"
)
//go:generate gomplate -c .=apilist.yaml -f methods.go.tmpl -o methods.go
// CableModem represents the Hitron CODA Cable Modem/Router
type CableModem struct {
base *url.URL
hc *http.Client
credentials credentials
}
// debugTransport - logs the request and response if debug is enabled
type debugTransport struct {
rt http.RoundTripper
}
func (t *debugTransport) RoundTrip(req *http.Request) (*http.Response, error) {
if !slog.Default().Enabled(req.Context(), slog.LevelDebug) {
return t.rt.RoundTrip(req)
}
drq, _ := httputil.DumpRequest(req, true)
slog.DebugContext(req.Context(), "dumping request", slog.String("request", string(drq)))
resp, err := t.rt.RoundTrip(req)
if err == nil {
drs, _ := httputil.DumpResponse(resp, true)
slog.DebugContext(req.Context(), "dumping response", slog.String("response", string(drs)))
}
return resp, err
}
// New instantiates a default CableModem struct
func New(host, username, password string) (*CableModem, error) {
u, err := url.Parse(fmt.Sprintf("http://%s/1/Device/", host))
if err != nil {
return nil, err
}
jar, err := cookiejar.New(nil)
if err != nil {
return nil, err
}
tr := http.DefaultTransport
if slog.Default().Enabled(context.Background(), slog.LevelDebug) {
tr = &debugTransport{tr}
}
client := &http.Client{
Jar: jar,
// Ignore redirects
CheckRedirect: func(_ *http.Request, _ []*http.Request) error {
return http.ErrUseLastResponse
},
Transport: tr,
}
creds := credentials{username, password}
return &CableModem{
credentials: creds,
base: u,
hc: client,
}, nil
}
func (c *CableModem) url(s string) *url.URL {
if len(s) == 0 || c.base == nil {
return c.base
}
if s[0] == '/' {
s = s[1:]
}
p, err := url.Parse(s)
if err != nil {
panic(err)
}
return c.base.ResolveReference(p)
}
func (c *CableModem) getJSON(ctx context.Context, path string, o interface{}) error {
return c.sendRequest(ctx, http.MethodGet, path, http.NoBody, o)
}
func (c *CableModem) sendRequest(ctx context.Context, method, path string, body, o interface{}) error {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
u := c.url(path).String()
contentType := ""
var reqBody io.Reader
switch b := body.(type) {
case io.Reader:
reqBody = b
case url.Values:
contentType = "application/x-www-form-urlencoded"
reqBody = strings.NewReader(b.Encode())
default:
return fmt.Errorf("unsupported body type %T", body)
}
req, err := http.NewRequestWithContext(ctx, method, u, reqBody)
if err != nil {
return err
}
if contentType != "" {
req.Header.Set("Content-Type", contentType)
}
resp, err := c.hc.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
b, err := io.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("failed to read body: %w", err)
}
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("failed with status %d: %s (Header: %v)", resp.StatusCode, string(b), resp.Header)
}
err = json.Unmarshal(b, o)
if err != nil {
return fmt.Errorf("JSON decoding failed: %w", err)
}
return nil
}
func atoi64(s string) int64 {
i, _ := strconv.ParseInt(strings.TrimSpace(s), 10, 64)
return i
}
func atoui64(s string) uint64 {
i, _ := strconv.ParseUint(strings.TrimSpace(s), 10, 64)
return i
}
func atof64(s string) float64 {
f, _ := strconv.ParseFloat(strings.TrimSpace(s), 64)
return f
}
//nolint:gomnd
const (
_byte = 1 << (10 * iota)
kib
mib
gib
tib
pib
eib
)
func formattedBytesToUint64(s string) uint64 {
i, err := strconv.ParseUint(strings.TrimSpace(s), 10, 64)
if err == nil {
return i
}
s = strings.TrimSuffix(s, " Bytes")
if len(s) <= 1 {
return atoui64(s)
}
switch s[len(s)-1] {
case 'B':
i = uint64(atof64(s[:len(s)-1]))
case 'K':
i = uint64(atof64(s[:len(s)-1]) * kib)
case 'M':
i = uint64(atof64(s[:len(s)-1]) * mib)
case 'G':
i = uint64(atof64(s[:len(s)-1]) * gib)
case 'T':
i = uint64(atof64(s[:len(s)-1]) * tib)
case 'P':
i = uint64(atof64(s[:len(s)-1]) * pib)
case 'E':
i = uint64(atof64(s[:len(s)-1]) * eib)
default:
i = uint64(atof64(s))
}
return i
}
func byteSize(bytes uint64) string {
unit := ""
value := float64(bytes)
switch {
case bytes >= eib:
unit = "E"
value /= eib
case bytes >= pib:
unit = "P"
value /= pib
case bytes >= tib:
unit = "T"
value /= tib
case bytes >= gib:
unit = "G"
value /= gib
case bytes >= mib:
unit = "M"
value /= mib
case bytes >= kib:
unit = "K"
value /= kib
case bytes >= _byte:
unit = "B"
case bytes == 0:
return "0B"
}
result := strconv.FormatFloat(value, 'f', 1, 64)
result = strings.TrimSuffix(result, ".0")
return result + unit
}