From 15ded20735430a558b0edbf418de38c81f045def Mon Sep 17 00:00:00 2001 From: mmcwilliams Date: Sun, 31 Dec 2017 23:45:40 -0500 Subject: [PATCH] Parse wpa_supplicant file --- lib/ble/index.js | 2 -- lib/wifi/index.js | 31 ++++++++++++++++++++++++++++--- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/lib/ble/index.js b/lib/ble/index.js index 41d9e12..2be0f0c 100644 --- a/lib/ble/index.js +++ b/lib/ble/index.js @@ -71,8 +71,6 @@ function onWifiWrite (data, offset, withoutResponse, callback) { result = bleno.Characteristic.RESULT_UNLIKELY_ERROR return callback(result) } - console.log(hash) - console.log(plaintext) return wifi.setNetwork(ssid, plaintext, hash, (err, data) => { if (err) { log.error('Error configuring wifi', err) diff --git a/lib/wifi/index.js b/lib/wifi/index.js index 5ddb54a..8f1c4b3 100644 --- a/lib/wifi/index.js +++ b/lib/wifi/index.js @@ -57,13 +57,18 @@ class Wifi { * @param {string} data Contents of the config file */ _readConfigCb (err, data) { + let parsed if (err) { console.error(err) return _cb(err) } - console.dir(data) + + parsed = this._parseConfig(data) + console.dir(parsed) + console.log(data) console.log(data.search(networkPattern)) console.log(_entry) + if (data.search(networkPattern) === -1) { data += `\n${_entry}` } else { @@ -117,8 +122,28 @@ class Wifi { //this._callback(null, { ssid : ssid, pwd : pwd.length }) _cb = () => {} } - _parseConfig () { - + _parseConfig (str) { + const networks = [] + const lines = str.split('\n') + let network = {} + for (let line of lines) { + if (line.indexOf('network={') !== -1) { + network = {} + network.raw = line + } else if (line.indexOf('ssid=') !== -1) { + network.ssid = line.replace('ssid=', '').trim().replace(quoteRe, '') + if (network.raw) { + network.raw += '\n' + line + } + } else if (line.indexOf('}') !== -1) { + network.raw += '\n' + line + networks.push(network) + network = {} + } else if (network.ssid) { + network.raw += '\n' + line + } + } + return networks } /** * Create sanitized wpa_supplicant.conf stanza for