UNPKG

@hzabala/tplinkcloud-api-client

Version:

<p align="left"> <a href="./LICENSE.md"><img src="https://img.shields.io/badge/license-MIT-blue.svg" alt="License MIT"/></a> <p>

149 lines 6.14 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.P105 = void 0; const axios_1 = require("axios"); const crypto = require("crypto"); const util_1 = require("./util"); class P105 { constructor(ip, username, password, keyPair) { this.ip = ip; this.url = `http://${this.ip}/app`; this.username = username; this.password = password; this.resolveKeyPair(keyPair); } resolveKeyPair(keyPair) { if (!keyPair) { console.log('No keyPair provided. Generating.'); const pair = (0, util_1.generateKeyPair)(); this.privateKey = pair.privateKey; this.publicKey = pair.publicKey; } else { this.privateKey = keyPair.privateKey; this.publicKey = keyPair.publicKey; } } handshake() { return __awaiter(this, void 0, void 0, function* () { try { const payload = { method: 'handshake', params: { key: this.publicKey.toString('utf8'), requestTimeMils: Math.round(Date.now() * 1000), }, }; const res = yield axios_1.default.post(this.url, payload); if (res.data.error_code !== 0) { throw new Error(`Received error: ${JSON.stringify(res.data)}`); } const cipherParam = (0, util_1.decryptHandshakeKey)(res.data.result.key, this.privateKey); this.cipherParam = cipherParam; this.cookie = res.headers['set-cookie'][0].split(';')[0]; return { axiosResponse: res, cipherParam, }; } catch (err) { throw err; } }); } login() { return __awaiter(this, void 0, void 0, function* () { try { const hashedUsername = crypto.createHash('sha1').update(this.username).digest('hex'); const encodedUsername = Buffer.from(hashedUsername).toString('base64'); const encodedPassword = Buffer.from(this.password).toString('base64'); const payload = { method: 'login_device', params: { username: encodedUsername, password: encodedPassword, }, requestTimeMils: Math.round(Date.now() * 1000), }; const encryptedPayload = (0, util_1.encrypt)(this.cipherParam, JSON.stringify(payload)); const securePayload = { method: 'securePassthrough', params: { request: encryptedPayload, }, }; const config = { headers: { Cookie: this.cookie, }, }; const res = yield axios_1.default.post(this.url, securePayload, config); if (res.data.error_code !== 0) { throw new Error(`Received error: ${JSON.stringify(res.data)}`); } const decrypted = (0, util_1.decrypt)(this.cipherParam, res.data.result.response); const parsed = JSON.parse(decrypted); if (parsed.error_code !== 0) { throw new Error(`Received inner error: ${JSON.stringify(parsed)}`); } const token = parsed.result.token; this.token = token; return { axiosResponse: res, token, }; } catch (err) { throw err; } }); } toggle(status) { return __awaiter(this, void 0, void 0, function* () { try { const payload = { method: 'set_device_info', params: { device_on: status, }, requestTimeMils: Math.round(Date.now() * 1000), }; const encryptedPayload = (0, util_1.encrypt)(this.cipherParam, JSON.stringify(payload)); const securePayload = { method: 'securePassthrough', params: { request: encryptedPayload, }, }; const config = { headers: { Cookie: this.cookie, }, }; const res = yield axios_1.default.post(`${this.url}?token=${this.token}`, securePayload, config); if (res.data.error_code !== 0) { throw new Error(`Received error: ${JSON.stringify(res.data)}`); } const decrypted = (0, util_1.decrypt)(this.cipherParam, res.data.result.response); const parsed = JSON.parse(decrypted); if (parsed.error_code !== 0) { throw new Error(`Received inner error: ${JSON.stringify(parsed)}`); } } catch (err) { throw err; } }); } } exports.P105 = P105; //# sourceMappingURL=p105.js.map