2019-11-26 15:44:08 +00:00
|
|
|
'use strict';
|
2019-11-26 16:41:15 +00:00
|
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
2019-11-26 15:44:08 +00:00
|
|
|
const networkPattern = /network[\s\S]*?=[\s\S]*?{([\s\S]*?)}/gi;
|
|
|
|
const quoteRe = new RegExp('"', 'g');
|
|
|
|
const filePath = '/etc/wpa_supplicant/wpa_supplicant.conf';
|
|
|
|
const reconfigure = '/sbin/wpa_cli reconfigure';
|
|
|
|
const refresh = 'ip link set wlan0 down && ip link set wlan0 up';
|
|
|
|
const iwlist = '/sbin/iwlist wlan0 scanning | grep "ESSID:"';
|
|
|
|
const iwgetid = '/sbin/iwgetid';
|
|
|
|
const log = require('../log')('wifi');
|
2019-11-26 16:41:15 +00:00
|
|
|
const child_process_1 = require("child_process");
|
2019-11-27 03:10:59 +00:00
|
|
|
const fs_extra_1 = require("fs-extra");
|
2017-09-17 19:50:14 +00:00
|
|
|
/** Class representing the wifi features */
|
|
|
|
class Wifi {
|
2019-11-26 15:44:08 +00:00
|
|
|
constructor() {
|
2019-11-27 02:16:13 +00:00
|
|
|
this._ssid = null;
|
|
|
|
this._entry = null;
|
2019-11-26 15:44:08 +00:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* List available wifi access points
|
|
|
|
*
|
|
|
|
* @param {function} callback Function which gets invoked after list is returned
|
|
|
|
*/
|
2019-11-27 02:16:13 +00:00
|
|
|
async list() {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
return child_process_1.exec(iwlist, (err, stdout, stderr) => {
|
|
|
|
if (err) {
|
|
|
|
log.error('list', err);
|
|
|
|
return reject(err);
|
2019-11-26 15:44:08 +00:00
|
|
|
}
|
2019-11-27 02:16:13 +00:00
|
|
|
const limit = 20;
|
|
|
|
const lines = stdout.split('\n');
|
|
|
|
let output = [];
|
|
|
|
let line;
|
|
|
|
let i = 0;
|
|
|
|
for (let l of lines) {
|
|
|
|
line = l.replace('ESSID:', '').trim();
|
|
|
|
if (line !== '""' && i < limit) {
|
|
|
|
line = line.replace(quoteRe, '');
|
|
|
|
output.push(line);
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
output = output.filter(ap => {
|
|
|
|
if (ap !== '')
|
|
|
|
return ap;
|
|
|
|
});
|
|
|
|
return resolve(output);
|
2019-11-26 15:44:08 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* (internal function) Invoked after config file is read,
|
|
|
|
* then invokes file write on the config file
|
|
|
|
*
|
|
|
|
*/
|
2019-11-27 02:16:13 +00:00
|
|
|
async _readConfig() {
|
|
|
|
let data;
|
2019-11-26 15:44:08 +00:00
|
|
|
let parsed;
|
|
|
|
let current;
|
2019-11-27 02:16:13 +00:00
|
|
|
try {
|
2019-11-27 03:10:59 +00:00
|
|
|
data = await fs_extra_1.readFile(filePath, 'utf8');
|
2019-11-27 02:16:13 +00:00
|
|
|
}
|
|
|
|
catch (err) {
|
|
|
|
log.error('_readConfig', err);
|
|
|
|
throw err;
|
2019-11-26 15:44:08 +00:00
|
|
|
}
|
|
|
|
parsed = this._parseConfig(data);
|
2019-11-26 16:41:15 +00:00
|
|
|
current = parsed.find((network) => {
|
2019-11-27 02:16:13 +00:00
|
|
|
return network.ssid === this._ssid;
|
2019-11-26 15:44:08 +00:00
|
|
|
});
|
|
|
|
if (typeof current !== 'undefined') {
|
2019-11-27 02:16:13 +00:00
|
|
|
data = data.replace(current.raw, this._entry);
|
2019-11-26 15:44:08 +00:00
|
|
|
}
|
|
|
|
else {
|
2019-11-27 02:16:13 +00:00
|
|
|
data += '\n\n' + this._entry;
|
2019-11-26 15:44:08 +00:00
|
|
|
}
|
2019-11-27 02:16:13 +00:00
|
|
|
this._entry = null;
|
|
|
|
return data;
|
2019-11-26 15:44:08 +00:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* (internal function) Invoked after config file is written,
|
|
|
|
* then executes reconfiguration command
|
|
|
|
*
|
|
|
|
*/
|
2019-11-27 02:16:13 +00:00
|
|
|
async _writeConfig(data) {
|
|
|
|
try {
|
2019-11-27 03:10:59 +00:00
|
|
|
await fs_extra_1.writeFile(filePath, data, 'utf8');
|
2019-11-27 02:16:13 +00:00
|
|
|
}
|
|
|
|
catch (err) {
|
|
|
|
log.error('_readConfigCb', err);
|
|
|
|
throw err;
|
2019-11-26 15:44:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* (internal function) Invoked after reconfiguration command is complete
|
|
|
|
*
|
|
|
|
*/
|
2019-11-27 02:16:13 +00:00
|
|
|
async _reconfigure() {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
return child_process_1.exec(reconfigure, (err, stdout, stderr) => {
|
|
|
|
if (err) {
|
|
|
|
return reject(err);
|
|
|
|
}
|
|
|
|
log.info('Wifi reconfigured');
|
|
|
|
return resolve(true);
|
|
|
|
});
|
|
|
|
});
|
2019-11-26 15:44:08 +00:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* (internal function) Invoked after wifi refresh command is complete
|
|
|
|
*
|
|
|
|
*/
|
2019-11-27 02:16:13 +00:00
|
|
|
async _refresh() {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
return child_process_1.exec(refresh, (err, stdout, stderr) => {
|
|
|
|
if (err) {
|
|
|
|
return reject(err);
|
|
|
|
}
|
|
|
|
log.info('Wifi refreshed');
|
|
|
|
return resolve({ ssid: this._ssid });
|
|
|
|
});
|
|
|
|
});
|
2019-11-26 15:44:08 +00:00
|
|
|
}
|
|
|
|
_parseConfig(str) {
|
|
|
|
const networks = [];
|
|
|
|
const lines = str.split('\n');
|
|
|
|
let network = {};
|
|
|
|
for (let line of lines) {
|
|
|
|
if (line.substring(0, 9) === 'network={') {
|
|
|
|
network = {};
|
|
|
|
network.raw = line;
|
|
|
|
}
|
|
|
|
else if (network.raw && line.indexOf('ssid=') !== -1) {
|
|
|
|
network.ssid = line.replace('ssid=', '').trim().replace(quoteRe, '');
|
|
|
|
if (network.raw) {
|
|
|
|
network.raw += '\n' + line;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (network.raw && line.substring(0, 1) === '}') {
|
|
|
|
network.raw += '\n' + line;
|
|
|
|
networks.push(network);
|
|
|
|
network = {};
|
|
|
|
}
|
|
|
|
else if (network.raw) {
|
|
|
|
network.raw += '\n' + line;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return networks;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Create sanitized wpa_supplicant.conf stanza for
|
|
|
|
* configuring wifi without storing plaintext passwords
|
|
|
|
* @example
|
|
|
|
* network={
|
|
|
|
* ssid="YOUR_SSID"
|
|
|
|
* #psk="YOUR_PASSWORD"
|
|
|
|
* psk=6a24edf1592aec4465271b7dcd204601b6e78df3186ce1a62a31f40ae9630702
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* @param {string} ssid SSID of wifi network
|
|
|
|
* @param {string} pwd Plaintext passphrase of wifi network
|
|
|
|
*/
|
2019-11-27 02:16:13 +00:00
|
|
|
createPSK(ssid, pwd) {
|
2019-11-26 15:44:08 +00:00
|
|
|
const cmd = `wpa_passphrase '${ssid.replace(/'/g, `'\\''`)}' '${pwd.replace(/'/g, `'\\''`)}' | grep "psk="`;
|
|
|
|
let lines;
|
|
|
|
let hash;
|
|
|
|
let plaintext;
|
2019-11-27 02:16:13 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
return child_process_1.exec(cmd, (err, stdout, stderr) => {
|
|
|
|
if (err) {
|
|
|
|
return reject(err);
|
|
|
|
}
|
|
|
|
lines = stdout.replace('#psk=', '').split('psk=');
|
|
|
|
hash = lines[1];
|
|
|
|
plaintext = lines[0];
|
|
|
|
return resolve({ hash: hash.trim(), plaintext: plaintext.trim() });
|
|
|
|
});
|
2019-11-26 15:44:08 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Function which initializes the processes for adding a wifi access point authentication
|
|
|
|
*
|
|
|
|
* @param {string} ssid SSID of network to configure
|
|
|
|
* @param {string} pwd Password of access point, plaintext to be masked
|
|
|
|
* @param {string} hash Password/SSID of access point, securely hashed
|
|
|
|
*/
|
2019-11-27 02:16:13 +00:00
|
|
|
async setNetwork(ssid, pwd, hash) {
|
2019-11-26 15:44:08 +00:00
|
|
|
let masked = pwd.split('').map(char => { return char !== '"' ? '*' : '"'; }).join('');
|
2019-11-27 02:16:13 +00:00
|
|
|
let data;
|
|
|
|
this._entry = `network={\n\tssid="${ssid}"\n\t#psk=${masked}\n\tpsk=${hash}\n}\n`;
|
|
|
|
this._ssid = ssid;
|
|
|
|
try {
|
|
|
|
data = await this._readConfig();
|
|
|
|
}
|
|
|
|
catch (err) {
|
|
|
|
log.error(err);
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
await this._writeConfig(data);
|
|
|
|
}
|
|
|
|
catch (err) {
|
|
|
|
log.error(err);
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
await this._reconfigure();
|
|
|
|
}
|
|
|
|
catch (err) {
|
|
|
|
log.error(err);
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
await this._refresh();
|
|
|
|
}
|
|
|
|
catch (err) {
|
|
|
|
log.error(err);
|
|
|
|
}
|
|
|
|
return { ssid: this._ssid };
|
2019-11-26 15:44:08 +00:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Executes command which gets the currently connected network
|
|
|
|
*
|
|
|
|
* @param {function} callback Function which is invoked after command is completed
|
|
|
|
*/
|
2019-11-27 02:16:13 +00:00
|
|
|
async getNetwork() {
|
2019-11-26 15:44:08 +00:00
|
|
|
let output;
|
2019-11-27 02:16:13 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
return child_process_1.exec(iwgetid, (err, stdout, stderr) => {
|
|
|
|
if (err) {
|
|
|
|
return reject(err);
|
|
|
|
}
|
|
|
|
output = stdout.split('ESSID:')[1].replace(quoteRe, '').trim();
|
|
|
|
return resolve(output);
|
|
|
|
});
|
2019-11-26 15:44:08 +00:00
|
|
|
});
|
|
|
|
}
|
2017-08-22 01:11:07 +00:00
|
|
|
}
|
2019-11-27 02:16:13 +00:00
|
|
|
exports.Wifi = Wifi;
|
|
|
|
module.exports.Wifi = Wifi;
|
2019-11-26 15:44:08 +00:00
|
|
|
//# sourceMappingURL=index.js.map
|