-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharlo.go
427 lines (347 loc) · 9.3 KB
/
arlo.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
package arlo
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"log"
"math"
"math/rand"
"net/http"
"net/http/cookiejar"
"strconv"
"strings"
"time"
"github.com/r3labs/sse"
)
const baseURL = "https://arlo.netgear.com"
// Errors
var (
ErrRequestFailedStatusNotOK = errors.New("request failed")
ErrRequestUnsuccessful = errors.New("request unsuccessful")
ErrNotLoggedIn = errors.New("not logged in yet")
ErrNoBasestationFound = errors.New("no basestation found")
ErrNoCamerasFound = errors.New("no cameras found")
ErrRequestUnauthorized = errors.New("401 unauthorized")
)
// Device modes - assumes default mode listing order.
const (
ModeDeviceArm = "mode1"
ModeDeviceDisarm = "mode0"
)
// Client represents a client to the Arlo API.
type Client struct {
// For initial login
Username string
Password string
UserID string
// For request authorization
Token string
HTTPClient *http.Client
Devices []Device
EventStreams map[string]*EventStream
Verbose bool
}
// NewClient returns a new Arlo client.
func NewClient() *Client {
cookieJar, _ := cookiejar.New(nil)
return &Client{
HTTPClient: &http.Client{Jar: cookieJar},
EventStreams: make(map[string]*EventStream),
}
}
// These headers are used for authenticated APIs.
func (a *Client) getCommonHeaders() map[string]string {
return map[string]string{
"DNT": "1",
"Host": "arlo.netgear.com",
"Referer": "https://arlo.netgear.com",
"Authorization": a.Token,
}
}
func (a *Client) applyCommonHeaders(req *http.Request) {
for key, value := range a.getCommonHeaders() {
req.Header.Add(key, value)
}
}
func (a *Client) hasLoggedIn() bool {
return a.Token != ""
}
// Login logs in and retrieves an access token.
// The login response has been stripped down to values used by the library.
func (a *Client) Login() error {
type loginRequest struct {
Email string `json:"email"`
Password string `json:"password"`
}
type loginResponse struct {
Data struct {
UserID string `json:"userId"`
Email string `json:"email"`
Token string `json:"token"`
} `json:"data"`
Success bool `json:"success"`
}
b := new(bytes.Buffer)
err := json.NewEncoder(b).Encode(loginRequest{a.Username, a.Password})
if err != nil {
return err
}
req, err := http.NewRequest("POST", baseURL+"/hmsweb/login", b)
if err != nil {
return err
}
req.Header.Add("Content-Type", "application/json")
resp, err := a.HTTPClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
switch resp.StatusCode {
case http.StatusUnauthorized:
return ErrRequestUnauthorized
default:
a.verbose("Status code:", resp.StatusCode)
return ErrRequestFailedStatusNotOK
}
}
respBody := loginResponse{}
err = json.NewDecoder(resp.Body).Decode(&respBody)
if err != nil {
return err
}
if !respBody.Success {
return ErrRequestUnsuccessful
}
a.UserID = respBody.Data.UserID
a.Token = respBody.Data.Token
return nil
}
// Logout logs the client out.
func (a *Client) Logout() error {
req, err := http.NewRequest("PUT", baseURL+"/hmsweb/logout", nil)
if err != nil {
return err
}
a.applyCommonHeaders(req)
resp, err := a.HTTPClient.Do(req)
resp.Body.Close()
// Clear vars
a.Token = ""
a.UserID = ""
cookieJar, _ := cookiejar.New(nil)
a.HTTPClient.Jar = cookieJar
a.Devices = nil
a.EventStreams = make(map[string]*EventStream)
return err
}
// GetDevices returns a list of available devices.
func (a *Client) GetDevices() ([]Device, error) {
if !a.hasLoggedIn() {
return nil, ErrNotLoggedIn
}
type deviceResponse struct {
Devices []Device `json:"data"`
Success bool `json:"success"`
}
ts := time.Now().UnixNano() / (int64(time.Millisecond) / int64(time.Nanosecond))
req, err := http.NewRequest("GET", baseURL+fmt.Sprintf("/hmsweb/users/devices?t=%d", ts), nil)
if err != nil {
return nil, err
}
a.applyCommonHeaders(req)
resp, err := a.HTTPClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
switch resp.StatusCode {
case http.StatusUnauthorized:
return nil, ErrRequestUnauthorized
default:
a.verbose("Status code:", resp.StatusCode)
return nil, ErrRequestFailedStatusNotOK
}
}
respBody := deviceResponse{}
err = json.NewDecoder(resp.Body).Decode(&respBody)
if err != nil {
return nil, err
}
if !respBody.Success {
return nil, ErrRequestUnsuccessful
}
a.Devices = respBody.Devices
return respBody.Devices, nil
}
// GetBasestation attempts to retrieve the basestation device from memory.
// GetDevices() should be called beforehand.
func (a *Client) GetBasestation() (*Device, error) {
for _, device := range a.Devices {
if device.IsBasestation() {
return &device, nil
}
}
return nil, ErrNoBasestationFound
}
// GetCameras attempts to retrieve a list of camera devices from memory.
// GetDevices() should be called beforehand.
func (a *Client) GetCameras() ([]Device, error) {
var cameras []Device
for _, device := range a.Devices {
if device.IsCamera() {
cameras = append(cameras, device)
}
}
if len(cameras) == 0 {
return nil, ErrNoCamerasFound
}
return cameras, nil
}
// Subscribe connects to the event stream for the given device ID.
func (a *Client) Subscribe(deviceID, xCloudID string) error {
var err error
_, ok := a.EventStreams[deviceID]
if !ok {
c := sse.NewClient(baseURL + fmt.Sprintf("/hmsweb/client/subscribe?token=%s", a.Token))
c.Connection = a.HTTPClient
c.Headers = a.getCommonHeaders()
es := NewEventStream()
es.SSEClient = c
es.Verbose = a.Verbose
errCh := make(chan error, 1)
go func() {
errCh <- es.Listen()
}()
Loop:
for {
select {
case err = <-errCh:
a.verbose("error occurred", err)
break Loop
default:
if es.Connected {
a.verbose("event stream connected!")
a.EventStreams[deviceID] = es
break Loop
}
}
time.Sleep(time.Millisecond * 500)
}
if err != nil {
return err
}
err = a.Register(deviceID, xCloudID)
}
return err
}
// Register registers a device to receive event stream messages.
func (a *Client) Register(deviceID, xCloudID string) error {
properties := make(map[string][]string)
properties["devices"] = []string{deviceID}
np := NotifyPayload{
Action: "set",
Resource: fmt.Sprintf("subscriptions/%s_web", a.UserID),
PublishResponse: false,
Properties: properties,
}
_, err := a.Notify(deviceID, xCloudID, np)
if err != nil {
a.verbose("Register error")
return err
}
a.EventStreams[deviceID].Registered = true
return nil
}
// NotifyPayload represents the message that will be sent to the Arlo servers via the Notify API.
type NotifyPayload struct {
Action string `json:"action,omitempty"`
Resource string `json:"resource,omitempty"`
PublishResponse bool `json:"publishResponse,omitempty"`
Properties interface{} `json:"properties,omitempty"`
TransID string `json:"transId"`
From string `json:"from"`
To string `json:"to"`
}
func (a *Client) getTransID() string {
source := rand.NewSource(time.Now().UnixNano())
random := rand.New(source)
e := random.Float64() * math.Pow(2, 32)
ms := time.Now().UnixNano() / (int64(time.Millisecond) / int64(time.Nanosecond))
return fmt.Sprintf("web!%s!%s", strings.ToLower(floatToHex(e)), strconv.Itoa(int(ms)))
}
// Notify sends a message to the Arlo notify API.
// A response will be returned via the device's SSE stream.
func (a *Client) Notify(deviceID, xCloudID string, payload NotifyPayload) (string, error) {
payload.TransID = a.getTransID()
payload.From = fmt.Sprintf("%s_web", a.UserID)
payload.To = deviceID
b := new(bytes.Buffer)
err := json.NewEncoder(b).Encode(payload)
if err != nil {
return "", err
}
req, err := http.NewRequest("POST", fmt.Sprintf(baseURL+"/hmsweb/users/devices/notify/%s", deviceID), b)
if err != nil {
return "", err
}
req.Header.Add("Content-Type", "application/json")
req.Header.Add("xCloudId", xCloudID)
a.applyCommonHeaders(req)
resp, err := a.HTTPClient.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
switch resp.StatusCode {
case http.StatusUnauthorized:
return "", ErrRequestUnauthorized
default:
a.verbose("Status code:", resp.StatusCode)
return "", ErrRequestFailedStatusNotOK
}
}
return payload.TransID, nil
}
// Arm arms the basestation.
func (a *Client) Arm(deviceID, xCloudID string) error {
err := a.Subscribe(deviceID, xCloudID)
if err != nil {
return err
}
properties := make(map[string]string)
properties["active"] = ModeDeviceArm
np := NotifyPayload{
Action: "set",
Resource: "modes",
PublishResponse: true,
Properties: properties,
}
_, err = a.Notify(deviceID, xCloudID, np)
return err
}
// Disarm disarms the basestation.
func (a *Client) Disarm(deviceID, xCloudID string) error {
err := a.Subscribe(deviceID, xCloudID)
if err != nil {
return err
}
properties := make(map[string]string)
properties["active"] = ModeDeviceDisarm
np := NotifyPayload{
Action: "set",
Resource: "modes",
PublishResponse: true,
Properties: properties,
}
_, err = a.Notify(deviceID, xCloudID, np)
return err
}
func (a *Client) verbose(params ...interface{}) {
if a.Verbose {
log.Println(params...)
}
}