Refactor wifi lib into Typescript. Only used one "any" on a custom module require that I need to revisit. (log). Yeeha!

This commit is contained in:
mmcwilliams 2019-11-26 11:41:15 -05:00
parent 84ee49a71c
commit 8986014a82
3 changed files with 72 additions and 66 deletions

View File

@ -1,4 +1,5 @@
'use strict'; 'use strict';
Object.defineProperty(exports, "__esModule", { value: true });
const networkPattern = /network[\s\S]*?=[\s\S]*?{([\s\S]*?)}/gi; const networkPattern = /network[\s\S]*?=[\s\S]*?{([\s\S]*?)}/gi;
const quoteRe = new RegExp('"', 'g'); const quoteRe = new RegExp('"', 'g');
const filePath = '/etc/wpa_supplicant/wpa_supplicant.conf'; const filePath = '/etc/wpa_supplicant/wpa_supplicant.conf';
@ -7,14 +8,14 @@ const refresh = 'ip link set wlan0 down && ip link set wlan0 up';
const iwlist = '/sbin/iwlist wlan0 scanning | grep "ESSID:"'; const iwlist = '/sbin/iwlist wlan0 scanning | grep "ESSID:"';
const iwgetid = '/sbin/iwgetid'; const iwgetid = '/sbin/iwgetid';
const log = require('../log')('wifi'); const log = require('../log')('wifi');
const exec = require('child_process').exec; const child_process_1 = require("child_process");
const fs = require('fs'); const fs_1 = require("fs");
let _entry = null; let _entry = null;
let _ssid = null; let _ssid = null;
let _cb = null;
/** Class representing the wifi features */ /** Class representing the wifi features */
class Wifi { class Wifi {
constructor() { constructor() {
this._cb = null;
} }
/** /**
* List available wifi access points * List available wifi access points
@ -22,7 +23,7 @@ class Wifi {
* @param {function} callback Function which gets invoked after list is returned * @param {function} callback Function which gets invoked after list is returned
*/ */
list(callback) { list(callback) {
exec(iwlist, (err, stdout, stderr) => { child_process_1.exec(iwlist, (err, stdout, stderr) => {
if (err) { if (err) {
console.error(err); console.error(err);
return callback(err); return callback(err);
@ -59,10 +60,10 @@ class Wifi {
let current; let current;
if (err) { if (err) {
console.error(err); console.error(err);
return _cb(err); return this._cb(err);
} }
parsed = this._parseConfig(data); parsed = this._parseConfig(data);
current = parsed.find(network => { current = parsed.find((network) => {
return network.ssid === _ssid; return network.ssid === _ssid;
}); });
if (typeof current !== 'undefined') { if (typeof current !== 'undefined') {
@ -72,7 +73,7 @@ class Wifi {
data += '\n\n' + _entry; data += '\n\n' + _entry;
} }
_entry = null; _entry = null;
fs.writeFile(filePath, data, 'utf8', this._writeConfigCb.bind(this)); fs_1.writeFile(filePath, data, 'utf8', this._writeConfigCb.bind(this));
} }
/** /**
* (internal function) Invoked after config file is written, * (internal function) Invoked after config file is written,
@ -83,9 +84,9 @@ class Wifi {
_writeConfigCb(err) { _writeConfigCb(err) {
if (err) { if (err) {
console.error(err); console.error(err);
return _cb(err); return this._cb(err);
} }
exec(reconfigure, this._reconfigureCb.bind(this)); child_process_1.exec(reconfigure, this._reconfigureCb.bind(this));
} }
/** /**
* (internal function) Invoked after reconfiguration command is complete * (internal function) Invoked after reconfiguration command is complete
@ -97,10 +98,10 @@ class Wifi {
_reconfigureCb(err, stdout, stderr) { _reconfigureCb(err, stdout, stderr) {
if (err) { if (err) {
console.error(err); console.error(err);
return _cb(err); return this._cb(err);
} }
console.log('Wifi reconfigured'); log.info('Wifi reconfigured');
exec(refresh, this._refreshCb.bind(this)); child_process_1.exec(refresh, this._refreshCb.bind(this));
} }
/** /**
* (internal function) Invoked after wifi refresh command is complete * (internal function) Invoked after wifi refresh command is complete
@ -112,11 +113,11 @@ class Wifi {
_refreshCb(err, stdout, stderr) { _refreshCb(err, stdout, stderr) {
if (err) { if (err) {
console.error(err); console.error(err);
return _cb(err); return this._cb(err);
} }
console.log('Wifi refreshed'); log.info('Wifi refreshed');
_cb(null, { ssid: _ssid }); this._cb(null, { ssid: _ssid });
_cb = () => { }; this._cb = () => { };
} }
_parseConfig(str) { _parseConfig(str) {
const networks = []; const networks = [];
@ -163,7 +164,7 @@ class Wifi {
let lines; let lines;
let hash; let hash;
let plaintext; let plaintext;
exec(cmd, (err, stdout, stderr) => { child_process_1.exec(cmd, (err, stdout, stderr) => {
if (err) { if (err) {
return callback(err); return callback(err);
} }
@ -184,9 +185,9 @@ class Wifi {
setNetwork(ssid, pwd, hash, callback) { setNetwork(ssid, pwd, hash, callback) {
let masked = pwd.split('').map(char => { return char !== '"' ? '*' : '"'; }).join(''); let masked = pwd.split('').map(char => { return char !== '"' ? '*' : '"'; }).join('');
_entry = `network={\n\tssid="${ssid}"\n\t#psk=${masked}\n\tpsk=${hash}\n}\n`; _entry = `network={\n\tssid="${ssid}"\n\t#psk=${masked}\n\tpsk=${hash}\n}\n`;
_cb = callback; this._cb = callback;
_ssid = ssid; _ssid = ssid;
fs.readFile(filePath, 'utf8', this._readConfigCb.bind(this)); fs_1.readFile(filePath, 'utf8', this._readConfigCb.bind(this));
} }
/** /**
* Executes command which gets the currently connected network * Executes command which gets the currently connected network
@ -195,7 +196,7 @@ class Wifi {
*/ */
getNetwork(callback) { getNetwork(callback) {
let output; let output;
exec(iwgetid, (err, stdout, stderr) => { child_process_1.exec(iwgetid, (err, stdout, stderr) => {
if (err) { if (err) {
return callback(err); return callback(err);
} }

File diff suppressed because one or more lines are too long

View File

@ -1,24 +1,29 @@
'use strict' 'use strict'
const networkPattern = /network[\s\S]*?=[\s\S]*?{([\s\S]*?)}/gi const networkPattern : RegExp = /network[\s\S]*?=[\s\S]*?{([\s\S]*?)}/gi
const quoteRe = new RegExp('"', 'g') const quoteRe : RegExp = new RegExp('"', 'g')
const filePath = '/etc/wpa_supplicant/wpa_supplicant.conf' const filePath : string = '/etc/wpa_supplicant/wpa_supplicant.conf'
const reconfigure = '/sbin/wpa_cli reconfigure' const reconfigure : string = '/sbin/wpa_cli reconfigure'
const refresh = 'ip link set wlan0 down && ip link set wlan0 up' const refresh : string = 'ip link set wlan0 down && ip link set wlan0 up'
const iwlist = '/sbin/iwlist wlan0 scanning | grep "ESSID:"' const iwlist : string = '/sbin/iwlist wlan0 scanning | grep "ESSID:"'
const iwgetid = '/sbin/iwgetid' const iwgetid : string = '/sbin/iwgetid'
const log = require('../log')('wifi') const log : any = require('../log')('wifi')
const exec = require('child_process').exec import { exec } from 'child_process'
const fs = require('fs') import { readFile, writeFile } from 'fs'
let _entry = null let _entry : string = null
let _ssid = null let _ssid : string = null
let _cb = null
interface Network {
raw : string
ssid : string
}
/** Class representing the wifi features */ /** Class representing the wifi features */
class Wifi { class Wifi {
private _cb : Function = null
constructor () { constructor () {
} }
@ -27,7 +32,7 @@ class Wifi {
* *
* @param {function} callback Function which gets invoked after list is returned * @param {function} callback Function which gets invoked after list is returned
*/ */
list (callback) { list (callback : Function) {
exec(iwlist, (err, stdout, stderr) => { exec(iwlist, (err, stdout, stderr) => {
if (err) { if (err) {
console.error(err) console.error(err)
@ -59,15 +64,15 @@ class Wifi {
* @param {object} err (optional) Error object only present if problem reading config file * @param {object} err (optional) Error object only present if problem reading config file
* @param {string} data Contents of the config file * @param {string} data Contents of the config file
*/ */
_readConfigCb (err, data) { _readConfigCb (err : Error, data : string) {
let parsed let parsed : Network[]
let current let current : Network
if (err) { if (err) {
console.error(err) console.error(err)
return _cb(err) return this._cb(err)
} }
parsed = this._parseConfig(data) parsed = this._parseConfig(data)
current = parsed.find(network => { current = parsed.find((network : Network) => {
return network.ssid === _ssid return network.ssid === _ssid
}) })
if (typeof current !== 'undefined') { if (typeof current !== 'undefined') {
@ -76,7 +81,7 @@ class Wifi {
data += '\n\n' + _entry data += '\n\n' + _entry
} }
_entry = null _entry = null
fs.writeFile(filePath, data, 'utf8', this._writeConfigCb.bind(this)) writeFile(filePath, data, 'utf8', this._writeConfigCb.bind(this))
} }
/** /**
* (internal function) Invoked after config file is written, * (internal function) Invoked after config file is written,
@ -84,10 +89,10 @@ class Wifi {
* *
* @param {object} err (optional) Error object only present if problem writing config file * @param {object} err (optional) Error object only present if problem writing config file
*/ */
_writeConfigCb (err) { _writeConfigCb (err : Error) {
if (err) { if (err) {
console.error(err) console.error(err)
return _cb(err) return this._cb(err)
} }
exec(reconfigure, this._reconfigureCb.bind(this)) exec(reconfigure, this._reconfigureCb.bind(this))
} }
@ -98,12 +103,12 @@ class Wifi {
* @param {string} stdout Standard output from reconfiguration command * @param {string} stdout Standard output from reconfiguration command
* @param {string} stderr Error output from command if fails * @param {string} stderr Error output from command if fails
*/ */
_reconfigureCb (err, stdout, stderr) { _reconfigureCb (err : Error, stdout : string, stderr : string) {
if (err) { if (err) {
console.error(err) console.error(err)
return _cb(err) return this._cb(err)
} }
console.log('Wifi reconfigured') log.info('Wifi reconfigured')
exec(refresh, this._refreshCb.bind(this)) exec(refresh, this._refreshCb.bind(this))
} }
/** /**
@ -113,22 +118,22 @@ class Wifi {
* @param {string} stdout Standard output from refresh command * @param {string} stdout Standard output from refresh command
* @param {string} stderr Error output from command if fails * @param {string} stderr Error output from command if fails
*/ */
_refreshCb (err, stdout, stderr) { _refreshCb (err : Error, stdout : string, stderr : string) {
if (err) { if (err) {
console.error(err) console.error(err)
return _cb(err) return this._cb(err)
} }
console.log('Wifi refreshed') log.info('Wifi refreshed')
_cb(null, { ssid : _ssid }) this._cb(null, { ssid : _ssid })
_cb = () => {} this._cb = () => {}
} }
_parseConfig (str) { _parseConfig (str : string) : Network[] {
const networks = [] const networks : Network[] = []
const lines = str.split('\n') const lines = str.split('\n')
let network = {} let network : Network = {} as Network
for (let line of lines) { for (let line of lines) {
if (line.substring(0, 9) === 'network={') { if (line.substring(0, 9) === 'network={') {
network = {} network = {} as Network
network.raw = line network.raw = line
} else if (network.raw && line.indexOf('ssid=') !== -1) { } else if (network.raw && line.indexOf('ssid=') !== -1) {
network.ssid = line.replace('ssid=', '').trim().replace(quoteRe, '') network.ssid = line.replace('ssid=', '').trim().replace(quoteRe, '')
@ -138,7 +143,7 @@ class Wifi {
} else if (network.raw && line.substring(0, 1) === '}') { } else if (network.raw && line.substring(0, 1) === '}') {
network.raw += '\n' + line network.raw += '\n' + line
networks.push(network) networks.push(network)
network = {} network = {} as Network
} else if (network.raw) { } else if (network.raw) {
network.raw += '\n' + line network.raw += '\n' + line
} }
@ -159,11 +164,11 @@ class Wifi {
* @param {string} pwd Plaintext passphrase of wifi network * @param {string} pwd Plaintext passphrase of wifi network
* @param {function} callback Function called after psk hash is generated * @param {function} callback Function called after psk hash is generated
*/ */
createPSK (ssid, pwd, callback) { createPSK (ssid : string, pwd : string, callback : Function) {
const cmd = `wpa_passphrase '${ssid.replace(/'/g, `'\\''`)}' '${pwd.replace(/'/g, `'\\''`)}' | grep "psk="` const cmd : string = `wpa_passphrase '${ssid.replace(/'/g, `'\\''`)}' '${pwd.replace(/'/g, `'\\''`)}' | grep "psk="`
let lines let lines : string[]
let hash let hash : string
let plaintext let plaintext : string
exec(cmd, (err, stdout, stderr) => { exec(cmd, (err, stdout, stderr) => {
if (err) { if (err) {
return callback(err) return callback(err)
@ -182,19 +187,19 @@ class Wifi {
* @param {string} hash Password/SSID of access point, securely hashed * @param {string} hash Password/SSID of access point, securely hashed
* @param {function} callback Function invoked after process is complete, or fails * @param {function} callback Function invoked after process is complete, or fails
*/ */
setNetwork (ssid, pwd, hash, callback) { setNetwork (ssid : string, pwd : string, hash : string, callback : Function) {
let masked = pwd.split('').map(char => { return char !== '"' ? '*' : '"' }).join('') let masked : string = pwd.split('').map(char => { return char !== '"' ? '*' : '"' }).join('')
_entry = `network={\n\tssid="${ssid}"\n\t#psk=${masked}\n\tpsk=${hash}\n}\n` _entry = `network={\n\tssid="${ssid}"\n\t#psk=${masked}\n\tpsk=${hash}\n}\n`
_cb = callback this._cb = callback
_ssid = ssid _ssid = ssid
fs.readFile(filePath, 'utf8', this._readConfigCb.bind(this)) readFile(filePath, 'utf8', this._readConfigCb.bind(this))
} }
/** /**
* Executes command which gets the currently connected network * Executes command which gets the currently connected network
* *
* @param {function} callback Function which is invoked after command is completed * @param {function} callback Function which is invoked after command is completed
*/ */
getNetwork (callback) { getNetwork (callback : Function) {
let output let output
exec(iwgetid, (err, stdout, stderr) => { exec(iwgetid, (err, stdout, stderr) => {
if (err) { if (err) {