homebridge-tplink-accesspoint
Version:
Exposes TP-Link WIFI Access Point to Homebridge
49 lines • 1.97 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.DataLoader = void 0;
const axios = require('axios').default;
const md5_1 = require("ts-md5/dist/md5");
class DataLoader {
constructor(ip, user, pass) {
this.cookie = '';
this.ip = ip;
this.user = user;
this.pass = pass;
}
async load() {
if (this.cookie.length == 0) {
let result = await this.login();
}
if (this.cookie.length > 0) {
let result = (await axios.post('http://' + this.ip + '/data/status.json', 'operation=read', { headers: this.buildHeaders(this.cookie), })).data;
if (!result.success) {
this.cookie = '';
}
return result;
}
else {
this.cookie = '';
return Promise.reject("login failed");
}
}
async login() {
const initialResponse = await axios.get('http://' + this.ip);
const setCookie = initialResponse.headers["set-cookie"] || [""];
const cookieValue = setCookie[0].split(',')[0].split('=')[1].split(";")[0];
this.cookie = cookieValue;
const encodedPass = md5_1.Md5.hashStr(md5_1.Md5.hashStr(this.pass).toUpperCase() + ':' + cookieValue).toUpperCase();
const data = 'operation=login&encoded=' + this.user + ':' + encodedPass + '&nonce=' + cookieValue;
const loginResponse = await axios.post('http://' + this.ip + '/data/login.json', data, { headers: this.buildHeaders(cookieValue) });
}
buildHeaders(cookieValue) {
return {
'Cookie': 'COOKIE=' + cookieValue,
'Referer': 'http://' + this.ip,
'Origin': 'http://' + this.ip,
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
'Accept': 'application/json, text/javascript, */*; q=0.01'
};
}
}
exports.DataLoader = DataLoader;
//# sourceMappingURL=dataLoader.js.map