Skip to content

Commit

Permalink
Improve handling of networks that have special characters in passwords
Browse files Browse the repository at this point in the history
  • Loading branch information
tuomashamalainen committed Dec 16, 2020
1 parent dd1d0b5 commit ed211fe
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
33 changes: 31 additions & 2 deletions beocreate_essentials/networking.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ var iwconfig = require('wireless-tools/iwconfig');
var ifconfig = require('wireless-tools/ifconfig');
//var udhcpd = require('wireless-tools/udhcpd');
//var hostapd = require('wireless-tools/hostapd');
const util = require('util');
const execPromise = util.promisify(exec);

var networking = module.exports = {
getWifiStatus: getWifiStatus,
Expand Down Expand Up @@ -286,7 +288,7 @@ function performWifiScan(callback) {

// ADD OR REMOVE NETWORKS

function addNetwork(options, update) {
async function addNetwork(options, update) {
if (!options.ssid) return false;
readWifiConfiguration();

Expand All @@ -311,7 +313,8 @@ function addNetwork(options, update) {
if (options.username) {
// RADIUS network configuration here.
} else {
wifiConfiguration.networks[networkIndex].psk = options.password;
password = await hashPassword(options.ssid, options.password);
wifiConfiguration.networks[networkIndex].psk = password;
}
delete wifiConfiguration.networks[networkIndex].key_mgmt;
} else {
Expand Down Expand Up @@ -345,6 +348,32 @@ function addNetwork(options, update) {
}
}

async function hashPassword(ssid, password) {
var escapedSSID = "";
for (var i = 0; i < ssid.length; i++) {
escapedSSID += "\\"+ssid[i];
}

var escapedPassword = "";
for (var i = 0; i < password.length; i++) {
escapedPassword += "\\"+password[i];
}
var psk = null;
try {
var hashResults = null;
console.log("Creating hashed password for '"+ssid+"'...");
hashResults = await execPromise("wpa_passphrase "+escapedSSID+" "+escapedPassword);
lines = hashResults.stdout.trim().split("\n");
for (l in lines) {
if (lines[l].indexOf("\tpsk=") == 0) psk = lines[l].split("=")[1];
}
console.log("Password hash created.");
} catch (error) {
console.log("Error creating hashed password:", error);
}
return psk;
}

function removeNetwork(ssid) {
if (!ssid) return false;
readWifiConfiguration();
Expand Down
2 changes: 1 addition & 1 deletion beocreate_essentials/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "beocreate_essentials",
"version": "1.2.1",
"version": "1.2.2",
"description": "Essential tools for sound systems based on BeoCreate 4-Channel Amplifier.",
"main": "beocreate_essentials.js",
"scripts": {
Expand Down

0 comments on commit ed211fe

Please sign in to comment.