-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
client.go
247 lines (221 loc) · 6.92 KB
/
client.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
package deco
import (
"crypto/md5"
"crypto/rsa"
"encoding/base64"
"encoding/json"
"fmt"
"net/http"
"net/http/cookiejar"
"net/url"
"strings"
"time"
"github.com/mrmarble/deco/utils"
)
const (
// userName is the default (hardcoded) username
userName = "admin"
)
var baseURL = url.URL{
Scheme: "http",
Path: "/cgi-bin/luci/",
}
// Client is a client for sending requests to the Deco-m4 API
type Client struct {
c *http.Client
aes *utils.AESKey
rsa *rsa.PublicKey
hash string
stok string
sequence uint
}
// ClientListResp is the structure of the client_list endpoint
type ClientListResp struct {
ErrorCode int `json:"error_code"`
Result struct {
ClientList []struct {
AccessHost string `json:"access_host"`
ClientMesh bool `json:"client_mesh"`
ClientType string `json:"client_type"`
ConnectionType string `json:"band5"`
DownSpeed uint `json:"down_speed"`
EnablePriority bool `json:"enable_priority"`
Interface string `json:"interface"`
IP string `json:"ip"`
MAC string `json:"mac"`
Name string `json:"name"`
Online bool `json:"online"`
OwnerID string `json:"owner_id"`
RemainTime int `json:"remain_time"`
SpaceID string `json:"space_id"`
UpSpeed uint `json:"up_speed"`
WireType string `json:"wire_type"`
} `json:"client_list"`
} `json:"result"`
}
// DeviceListResp is the structure of the device_list endpoint
type DeviceListResp struct {
ErrorCode int `json:"error_code"`
Result struct {
DeviceList []struct {
DeviceIP string `json:"device_ip"`
DeviceID string `json:"device_id,omitempty"`
DeviceType string `json:"device_type"`
NandFlash bool `json:"nand_flash"`
OwnerTransfer bool `json:"owner_transfer,omitempty"`
Previous string `json:"previous"`
BSSID5G string `json:"bssid_5g"`
BSSID2G string `json:"bssid_2g"`
BSSIDSta5G string `json:"bssid_sta_5g"`
BSSIDSta2G string `json:"bssid_sta_2g"`
ParentDeviceID string `json:"parent_device_id,omitempty"`
SoftwareVer string `json:"software_ver"`
Role string `json:"role"`
ProductLevel int `json:"product_level"`
HardwareVer string `json:"hardware_ver"`
InetStatus string `json:"inet_status"`
SupportPLC bool `json:"support_plc"`
MAC string `json:"mac"`
SetGatewaySupport bool `json:"set_gateway_support"`
InetErrorMsg string `json:"inet_error_msg"`
ConnectionType []string `json:"connection_type,omitempty"`
CustomNickname string `json:"custom_nickname,omitempty"`
Nickname string `json:"nickname"`
GroupStatus string `json:"group_status"`
OemID string `json:"oem_id"`
SignalLevel struct {
Band24 string `json:"band2_4"`
Band5 string `json:"band5"`
} `json:"signal_level"`
DeviceModel string `json:"device_model"`
OversizedFirmware bool `json:"oversized_firmware"`
SpeedGetSupport bool `json:"speed_get_support,omitempty"`
HwID string `json:"hw_id"`
} `json:"device_list"`
} `json:"result"`
}
// PerfResp is the structure of the performance endpoint
type PerfResp struct {
ErrorCode int `json:"error_code"`
Result struct {
CPU float32 `json:"cpu_usage"`
MEM float32 `json:"mem_usage"`
} `json:"result"`
}
// New creates a new Go client for the Deco-m4 API
func New(target string) *Client {
jar, _ := cookiejar.New(nil)
c := &http.Client{Timeout: 10 * time.Second, Jar: jar}
baseURL.Host = target
return &Client{
c: c,
}
}
// Authenticate will generate the keys needed for the communication with the router.
func (c *Client) Authenticate(password string) error {
c.aes = utils.GenerateAESKey()
c.hash = fmt.Sprintf("%x", md5.Sum([]byte(fmt.Sprintf("%s%s", userName, password))))
passwordKey, err := c.getPasswordKey()
if err != nil {
return err
}
sessionKey, sequence, err := c.getSessionKey()
if err != nil {
return err
}
c.rsa = sessionKey
c.sequence = sequence
encryptedPassword, err := utils.EncryptRsa(password, passwordKey)
if err != nil {
return err
}
loginReq := loginRequest{
Operation: "login",
Params: loginParams{
Password: string(encryptedPassword),
},
}
loginJSON, err := json.Marshal(loginReq)
if err != nil {
return err
}
args := EndpointArgs{
form: "login",
}
var result loginResponse
err = c.doEncryptedPost(";stok=/login", args, loginJSON, true, &result)
if err != nil {
return err
}
c.stok = result.Result.Stok
return nil
}
// Performance returns the current cpu and mem usage.
func (c *Client) Performance() (*PerfResp, error) {
var result PerfResp
err := c.doEncryptedPost(fmt.Sprintf(";stok=%s/admin/network", c.stok), EndpointArgs{form: "performance"}, readBody, false, &result)
if err != nil {
return nil, err
}
return &result, nil
}
// DeviceList returns the list of connected deco APs
func (c *Client) DeviceList() (*DeviceListResp, error) {
var result DeviceListResp
err := c.doEncryptedPost(fmt.Sprintf(";stok=%s/admin/device", c.stok), EndpointArgs{form: "device_list"}, readBody, false, &result)
if err != nil {
return nil, err
}
return &result, nil
}
// ClientList returns the list of connected devices
func (c *Client) ClientList() (*ClientListResp, error) {
var result ClientListResp
request := request{
Operation: "read",
Params: map[string]interface{}{"device_mac": "default"},
}
jsonRequest, _ := json.Marshal(request)
err := c.doEncryptedPost(fmt.Sprintf(";stok=%s/admin/client", c.stok), EndpointArgs{form: "client_list"}, jsonRequest, false, &result)
if err != nil {
return nil, err
}
for index := range result.Result.ClientList {
name, err := base64.StdEncoding.DecodeString(result.Result.ClientList[index].Name)
if err == nil {
result.Result.ClientList[index].Name = string(name)
}
}
return &result, nil
}
// Reboot the deco nodes by mac address
func (c *Client) Reboot(macAddrs ...string) (map[string]interface{}, error) {
var result map[string]interface{}
var macList []map[string]string
for _, mac := range macAddrs {
macList = append(macList, map[string]string{
"mac": strings.ToUpper(mac),
})
}
request := request{
Operation: "reboot",
Params: map[string]interface{}{
"mac_list": macList,
},
}
jsonRequest, _ := json.Marshal(request)
err := c.doEncryptedPost(fmt.Sprintf(";stok=%s/admin/device", c.stok), EndpointArgs{form: "system"}, jsonRequest, false, &result)
if err != nil {
return nil, err
}
return result, nil
}
// Custom lets you make a custom request
func (c *Client) Custom(path string, params EndpointArgs, body []byte) (interface{}, error) {
var result interface{}
err := c.doEncryptedPost(fmt.Sprintf(";stok=%s%s", c.stok, path), params, body, false, &result)
if err != nil {
return nil, err
}
return &result, nil
}