UNPKG

homebridge-plugin-asus-vpn

Version:
64 lines 2.87 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.AsusRouterHttpService = void 0; const axios_1 = require("axios"); const constant_1 = require("../../../domain/constant"); class AsusRouterHttpService { constructor(routerIp, loginAuthorizationToken, vpnConnectionName, vpnConnectionType) { this.loginAuthorizationToken = loginAuthorizationToken; this.vpnConnectionName = vpnConnectionName; this.vpnConnectionType = vpnConnectionType; this.api = axios_1.default.create({ baseURL: `http://${routerIp}`, headers: { 'user-agent': 'asusrouter--DUTUtil-', }, withCredentials: true, }); } async login() { const { data: { asus_token: token } } = await this.api.post('login.cgi', `login_authorization=${this.loginAuthorizationToken}`); this.api.defaults.headers = Object.assign(Object.assign({}, this.api.defaults.headers), { Cookie: `asus_token=${token};` }); } async toggleVpn(connect) { const data = new URLSearchParams({ 'action_mode': 'apply', 'rc_service': connect ? constant_1.VpnRcService.CONNECT : constant_1.VpnRcService.DISCONNECT, 'vpnc_unit': '0', 'vpnc_clientlist': `${this.vpnConnectionName}>${this.vpnConnectionType}>5>>>${connect ? constant_1.VpnActionStatus.CONNECT : constant_1.VpnActionStatus.DISCONNECT}>5>>>0>0>Web`, 'vpnc_pptp_options_x_list': '<auto', }).toString(); await this.api.post('applyapp.cgi', data); return await new Promise(async (resolve) => { setTimeout(() => { resolve(false); return; }, 10000); while (true) { const vpnStatus = await this.getVpnConnectionStatus(); if (vpnStatus === (connect ? constant_1.VpnConnectionStatus.CONNECTED : constant_1.VpnConnectionStatus.DISCONNECTED)) { resolve(true); return; } } }); } async getVpnConnectionStatus(count = 0) { if (count >= 5) { throw new Error('Failed to get VPN connection status'); } const { data: { get_vpnc_status: _vpnStatus } } = await this.api.get('/appGet.cgi', { params: { hook: 'get_vpnc_status()', }, }); if (_vpnStatus === null || _vpnStatus === undefined || _vpnStatus === 'undefined') { console.log('VPN status is undefined, trying to login again'); await this.login(); return await this.getVpnConnectionStatus(count + 1); } return parseInt(_vpnStatus.split('>')[0]); } } exports.AsusRouterHttpService = AsusRouterHttpService; //# sourceMappingURL=asus-router.http-service.js.map