forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnection.go
416 lines (336 loc) Β· 9.36 KB
/
connection.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
package tapo
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"crypto/rsa"
"crypto/sha1"
"crypto/x509"
"encoding/base64"
"encoding/hex"
"encoding/json"
"encoding/pem"
"errors"
"fmt"
"net/http"
"strings"
"time"
"github.com/evcc-io/evcc/util"
"github.com/evcc-io/evcc/util/request"
"github.com/mergermarket/go-pkcs7"
)
// Tapo homepage + api reverse engineering results
// https://www.tapo.com/de/
// Credits to & inspired by:
// https://k4czp3r.xyz/reverse-engineering/tp-link/tapo/2020/10/15/reverse-engineering-tp-link-tapo.html
// https://github.com/fishbigger/TapoP100
// https://github.com/artemvang/p100-go
const Timeout = time.Second * 15
// Connection is the Tapo connection
type Connection struct {
*request.Helper
log *util.Logger
URI string
EncodedUser string
EncodedPassword string
Cipher *ConnectionCipher
SessionID string
Token string
TerminalUUID string
updated time.Time
lasttodayenergy int64
energy int64
}
// NewConnection creates a new Tapo device connection.
// User is encoded by using MessageDigest of SHA1 which is afterwards B64 encoded.
// Password is directly B64 encoded.
func NewConnection(uri, user, password string) (*Connection, error) {
if uri == "" {
return nil, errors.New("missing uri")
}
if user == "" || password == "" {
return nil, fmt.Errorf("missing user or password")
}
for _, suffix := range []string{"/", "/app"} {
uri = strings.TrimSuffix(uri, suffix)
}
log := util.NewLogger("tapo")
// nosemgrep:go.lang.security.audit.crypto.use_of_weak_crypto.use-of-sha1
h := sha1.New()
_, err := h.Write([]byte(user))
userhash := hex.EncodeToString(h.Sum(nil))
conn := &Connection{
log: log,
Helper: request.NewHelper(log),
URI: fmt.Sprintf("%s/app", util.DefaultScheme(uri, "http")),
EncodedUser: base64.StdEncoding.EncodeToString([]byte(userhash)),
EncodedPassword: base64.StdEncoding.EncodeToString([]byte(password)),
}
conn.Client.Timeout = Timeout
return conn, err
}
// Login provides the Tapo device session token and MAC address (TerminalUUID).
func (d *Connection) Login() error {
err := d.Handshake()
if err != nil {
return err
}
req := map[string]interface{}{
"method": "login_device",
"params": map[string]interface{}{
"username": d.EncodedUser,
"password": d.EncodedPassword,
},
}
res, err := d.DoSecureRequest(d.URI, req)
if err != nil {
return err
}
if err := d.CheckErrorCode(res.ErrorCode); err != nil {
return err
}
d.Token = res.Result.Token
deviceResponse, err := d.ExecMethod("get_device_info", false)
if err != nil {
return err
}
d.TerminalUUID = deviceResponse.Result.MAC
return nil
}
// Handshake provides the Tapo device session cookie and encryption cipher.
func (d *Connection) Handshake() error {
privKey, pubKey, err := GenerateRSAKeys()
if err != nil {
return err
}
pubPEM, err := DumpRSAPEM(pubKey)
if err != nil {
return err
}
req, err := json.Marshal(map[string]interface{}{
"method": "handshake",
"params": map[string]interface{}{
"key": string(pubPEM),
"requestTimeMils": 0,
},
})
if err != nil {
return err
}
resp, err := http.Post(d.URI, "application/json", bytes.NewBuffer(req))
if err != nil {
return err
}
defer resp.Body.Close()
var res DeviceResponse
if err = json.NewDecoder(resp.Body).Decode(&res); err != nil {
return err
}
if err = d.CheckErrorCode(res.ErrorCode); err != nil {
return err
}
encryptedEncryptionKey, err := base64.StdEncoding.DecodeString(res.Result.Key)
if err != nil {
return err
}
encryptionKey, err := rsa.DecryptPKCS1v15(rand.Reader, privKey, encryptedEncryptionKey)
if err != nil {
return err
}
d.Cipher = &ConnectionCipher{
Key: encryptionKey[:16],
Iv: encryptionKey[16:],
}
cookie := strings.Split(resp.Header.Get("Set-Cookie"), ";")
if len(cookie) == 0 {
return errors.New("missing session cookie")
}
d.SessionID = cookie[0]
return nil
}
// ExecMethod executes a Tapo device command method and provides the corresponding response.
func (d *Connection) ExecMethod(method string, deviceOn bool) (*DeviceResponse, error) {
var req map[string]interface{}
switch method {
case "set_device_info":
req = map[string]interface{}{
"method": method,
"params": map[string]interface{}{
"device_on": deviceOn,
},
"requestTimeMils": int(time.Now().Unix() * 1000),
"terminalUUID": d.TerminalUUID,
}
default:
req = map[string]interface{}{
"method": method,
"requestTimeMils": int(time.Now().Unix() * 1000),
}
}
res, err := d.DoSecureRequest(fmt.Sprintf("%s?token=%s", d.URI, d.Token), req)
if err != nil {
return nil, err
}
if method == "get_device_info" {
res.Result.Nickname, err = base64Decode(res.Result.Nickname)
if err != nil {
return nil, err
}
res.Result.SSID, err = base64Decode(res.Result.SSID)
if err != nil {
return nil, err
}
}
return res, nil
}
// ExecCmd executes a Tapo api command and provides the response
func (d *Connection) ExecCmd(method string, enable bool) (*DeviceResponse, error) {
// refresh session id
if time.Since(d.updated) >= 600*time.Minute {
if err := d.Login(); err != nil {
return nil, err
}
d.updated = time.Now()
}
return d.ExecMethod(method, enable)
}
// CurrentPower provides current power consuption
func (d *Connection) CurrentPower() (float64, error) {
resp, err := d.ExecCmd("get_energy_usage", false)
if err != nil {
return 0, err
}
return float64(resp.Result.Current_Power) / 1e3, nil
}
// ChargedEnergy collects the daily charged energy
func (d *Connection) ChargedEnergy() (float64, error) {
resp, err := d.ExecCmd("get_energy_usage", false)
if err != nil {
return 0, err
}
if resp.Result.Today_Energy > d.lasttodayenergy {
d.energy = d.energy + (resp.Result.Today_Energy - d.lasttodayenergy)
}
d.lasttodayenergy = resp.Result.Today_Energy
return float64(d.energy) / 1000, nil
}
// DoSecureRequest executes a Tapo device request by encding the request and decoding its response.
func (d *Connection) DoSecureRequest(uri string, taporequest map[string]interface{}) (*DeviceResponse, error) {
payload, err := json.Marshal(taporequest)
if err != nil {
return nil, err
}
d.log.TRACE.Printf("request: %s", string(payload))
encryptedRequest, err := d.Cipher.Encrypt(payload)
if err != nil {
return nil, err
}
data := map[string]interface{}{
"method": "securePassthrough",
"params": map[string]interface{}{
"request": base64.StdEncoding.EncodeToString(encryptedRequest),
},
}
req, err := request.New(http.MethodPost, uri, request.MarshalJSON(data), map[string]string{
"Cookie": d.SessionID,
})
if err != nil {
return nil, err
}
var res *DeviceResponse
if err := d.DoJSON(req, &res); err != nil {
return nil, err
}
// Login atempt in case of tapo switch connection hicups
if res.ErrorCode == 9999 {
if err := d.Login(); err != nil {
return nil, err
}
if err := d.DoJSON(req, &res); err != nil {
return nil, err
}
}
if err := d.CheckErrorCode(res.ErrorCode); err != nil {
return nil, err
}
decodedResponse, err := base64.StdEncoding.DecodeString(res.Result.Response)
if err != nil {
return nil, err
}
decryptedResponse, err := d.Cipher.Decrypt(decodedResponse)
if err != nil {
return nil, err
}
d.log.TRACE.Printf("decrypted result: %v", string(decryptedResponse))
var deviceResp *DeviceResponse
err = json.Unmarshal(decryptedResponse, &deviceResp)
return deviceResp, err
}
// Tapo helper functions
func (d *Connection) CheckErrorCode(errorCode int) error {
errorDesc := map[int]string{
0: "Success",
9999: "Login failed, invalid user or password",
-1002: "Incorrect Request/Method",
-1003: "JSON formatting error ",
-1010: "Invalid Public Key Length",
-1012: "Invalid terminalUUID",
-1501: "Invalid Request or Credentials",
}
if errorCode != 0 {
return fmt.Errorf("tapo error %d: %s", errorCode, errorDesc[errorCode])
}
return nil
}
func (c *ConnectionCipher) Encrypt(payload []byte) ([]byte, error) {
paddedPayload, err := pkcs7.Pad(payload, aes.BlockSize)
if err != nil {
return nil, err
}
block, err := aes.NewCipher(c.Key)
if err != nil {
return nil, err
}
encrypter := cipher.NewCBCEncrypter(block, c.Iv)
encryptedPayload := make([]byte, len(paddedPayload))
encrypter.CryptBlocks(encryptedPayload, paddedPayload)
return encryptedPayload, nil
}
func (c *ConnectionCipher) Decrypt(payload []byte) ([]byte, error) {
block, err := aes.NewCipher(c.Key)
if err != nil {
return nil, err
}
encrypter := cipher.NewCBCDecrypter(block, c.Iv)
decryptedPayload := make([]byte, len(payload))
encrypter.CryptBlocks(decryptedPayload, payload)
return pkcs7.Unpad(decryptedPayload, aes.BlockSize)
}
func DumpRSAPEM(pubKey *rsa.PublicKey) ([]byte, error) {
pubKeyPKIX, err := x509.MarshalPKIXPublicKey(pubKey)
if err != nil {
return nil, err
}
pubPEM := pem.EncodeToMemory(
&pem.Block{
Type: "PUBLIC KEY",
Bytes: pubKeyPKIX,
},
)
return pubPEM, nil
}
func GenerateRSAKeys() (*rsa.PrivateKey, *rsa.PublicKey, error) {
key, err := rsa.GenerateKey(rand.Reader, 1024)
if err != nil {
return nil, nil, err
}
return key, key.Public().(*rsa.PublicKey), nil
}
func base64Decode(base64String string) (string, error) {
decodedString, err := base64.StdEncoding.DecodeString(base64String)
if err != nil {
return "", err
}
return string(decodedString), nil
}