2017-08-22 01:11:07 +00:00
|
|
|
'use strict'
|
|
|
|
|
|
|
|
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 = '/sbin/ifdown wlan0 && /sbin/ifup --force wlan0'
|
|
|
|
const iwlist = '/sbin/iwlist wlan0 scanning | grep "ESSID:"'
|
|
|
|
const iwgetid = '/sbin/iwgetid'
|
|
|
|
|
|
|
|
const exec = require('child_process').exec
|
|
|
|
const fs = require('fs')
|
|
|
|
|
2017-09-17 19:50:14 +00:00
|
|
|
/** Class representing the wifi features */
|
|
|
|
class Wifi {
|
2017-08-22 01:11:07 +00:00
|
|
|
constructor () {
|
|
|
|
this._callback = () => {}
|
|
|
|
this._entry = null
|
2017-08-22 02:48:03 +00:00
|
|
|
this._ssid = null
|
2017-08-22 01:11:07 +00:00
|
|
|
}
|
2017-09-17 19:50:14 +00:00
|
|
|
/**
|
|
|
|
* List available wifi access points
|
|
|
|
*
|
|
|
|
* @param {function} callback Function which gets invoked after list is returned
|
|
|
|
*/
|
2017-08-22 01:11:07 +00:00
|
|
|
list (callback) {
|
|
|
|
exec(iwlist, (err, stdout, stderr) => {
|
|
|
|
if (err) {
|
|
|
|
console.error(err)
|
|
|
|
return callback(err)
|
|
|
|
}
|
|
|
|
const lines = stdout.split('\n')
|
|
|
|
const output = []
|
|
|
|
let line
|
|
|
|
for (let l of lines) {
|
|
|
|
line = l.replace('ESSID:', '').trim()
|
2017-08-22 02:48:03 +00:00
|
|
|
if (line !== '""') {
|
2017-08-22 01:11:07 +00:00
|
|
|
line = line.replace(quoteRe, '')
|
|
|
|
output.push(line)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return callback(null, output)
|
|
|
|
})
|
|
|
|
}
|
2017-09-17 19:50:14 +00:00
|
|
|
/**
|
|
|
|
* (internal function) Invoked after config file is read,
|
|
|
|
* then invokes file write on the config file
|
|
|
|
*
|
|
|
|
* @param {object} err (optional) Error object only present if problem reading config file
|
|
|
|
* @param {string} data Contents of the config file
|
|
|
|
*/
|
2017-08-22 01:11:07 +00:00
|
|
|
_readConfigCb (err, data) {
|
|
|
|
if (err) {
|
|
|
|
console.error(err)
|
|
|
|
return this._callback(err)
|
|
|
|
}
|
|
|
|
if (data.search(networkPattern) === -1) {
|
|
|
|
data += `\n${this._entry}`
|
|
|
|
} else {
|
2017-08-22 02:48:03 +00:00
|
|
|
data = data.replace(networkPattern, this._entry)
|
2017-08-22 01:11:07 +00:00
|
|
|
}
|
2017-08-22 02:48:03 +00:00
|
|
|
this._entry = null
|
2017-08-22 01:11:07 +00:00
|
|
|
fs.writeFile(filePath, data, 'utf8', this._writeConfigCb)
|
|
|
|
}
|
2017-09-17 19:50:14 +00:00
|
|
|
/**
|
|
|
|
* (internal function) Invoked after config file is written,
|
|
|
|
* then executes reconfiguration command
|
|
|
|
*
|
|
|
|
* @param {object} err (optional) Error object only present if problem writing config file
|
|
|
|
*/
|
2017-08-22 01:11:07 +00:00
|
|
|
_writeConfigCb (err) {
|
|
|
|
if (err) {
|
|
|
|
console.error(err)
|
|
|
|
return this._callback(err)
|
|
|
|
}
|
|
|
|
exec(reconfigure, this._reconfigureCb)
|
|
|
|
}
|
2017-09-17 19:50:14 +00:00
|
|
|
/**
|
|
|
|
* (internal function) Invoked after reconfiguration command is complete
|
|
|
|
*
|
|
|
|
* @param {object} err (optional) Error object only present if configuration command fails
|
|
|
|
* @param {string} stdout Standard output from reconfiguration command
|
|
|
|
* @param {string} stderr Error output from command if fails
|
|
|
|
*/
|
2017-08-22 01:11:07 +00:00
|
|
|
_reconfigureCb (err, stdout, stderr) {
|
|
|
|
if (err) {
|
|
|
|
console.error(err)
|
|
|
|
return this._callback(err)
|
|
|
|
}
|
|
|
|
console.log('Wifi reconfigured')
|
|
|
|
exec(refresh, this._refreshCb)
|
|
|
|
}
|
2017-09-17 19:50:14 +00:00
|
|
|
/**
|
|
|
|
* (internal function) Invoked after wifi refresh command is complete
|
|
|
|
*
|
|
|
|
* @param {object} err (optional) Error object only present if refresh command fails
|
|
|
|
* @param {string} stdout Standard output from refresh command
|
|
|
|
* @param {string} stderr Error output from command if fails
|
|
|
|
*/
|
2017-08-22 01:11:07 +00:00
|
|
|
_refreshCb (err, stdout, stderr) {
|
|
|
|
if (err) {
|
|
|
|
console.error(err)
|
|
|
|
return this._callback(err)
|
|
|
|
}
|
|
|
|
console.log('Wifi refreshed')
|
2017-08-22 02:48:03 +00:00
|
|
|
//this._callback(null, { ssid : ssid, pwd : pwd.length })
|
2017-08-22 01:11:07 +00:00
|
|
|
this._callback = () => {}
|
|
|
|
}
|
2017-09-17 19:50:14 +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
|
|
|
|
* @param {function} callback Function invoked after process is complete, or fails
|
|
|
|
*/
|
2017-08-22 01:11:07 +00:00
|
|
|
setNetwork (ssid, pwd, callback) {
|
|
|
|
this._entry = `network={\n\tssid="${ssid}"\n\tpsk="${pwd}"\n}\n`
|
|
|
|
this._callback = callback
|
2017-08-22 02:48:03 +00:00
|
|
|
this._ssid = ssid
|
2017-08-22 01:11:07 +00:00
|
|
|
fs.readFile(filePath, 'utf8', this._readConfigCb)
|
|
|
|
}
|
2017-09-17 19:50:14 +00:00
|
|
|
/**
|
|
|
|
* Executes command which gets the currently connected network
|
|
|
|
*
|
|
|
|
* @param {function} callback Function which is invoked after command is completed
|
|
|
|
*/
|
2017-08-22 01:11:07 +00:00
|
|
|
getNetwork (callback) {
|
|
|
|
exec(iwgetid, (err, stdout, stderr) => {
|
|
|
|
if (err) {
|
|
|
|
return callback(err)
|
|
|
|
}
|
|
|
|
callback(null, stdout)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-17 19:50:14 +00:00
|
|
|
module.exports = new Wifi()
|