Skip to content

Commit

Permalink
version 2.0.0 in preparation
Browse files Browse the repository at this point in the history
  • Loading branch information
Thibault Friedrich committed Sep 30, 2017
1 parent 6b1c9e2 commit e0497fb
Show file tree
Hide file tree
Showing 9 changed files with 197 additions and 168 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,9 @@
# Version 1.2.5

* fixed bug #13

# Version 2.0.0

* add disconnection feature on windows (issue #16)
* add new fields in scan results (bssid equals to mac, channel, security_flags)
* improve stability of linux scan
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
node-wifi
===================

![node-wifi](https://raw.githubusercontent.com/friedrith/assets/master/node-wifi/logo.png)


The node-wifi module allows mac, windows and linux users to interact with surrounding wifi networks through various methods.

Expand All @@ -13,8 +15,8 @@ The module manages :

* Connect for linux | mac | windows
* Scan for linux | mac | windows
* List the current wifi connections for linux | mac
* Disconnect for linux
* List the current wifi connections for linux | mac | windows
* Disconnect for linux | windows

> As everything with hardware dependency, weird behaviors may happen depending of your configuration. You should never hesitate to notify us about a specificity of your OS/Hardware/Wifi card/whatever.
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "node-wifi",
"version": "1.2.5",
"version": "2.0.0",
"description": "NodeJS tool to manage wifi",
"bin": {
"wifi": "bin/wifi.js"
Expand All @@ -20,10 +20,10 @@
"wifi",
"multiplatform"
],
"author": "",
"author": "Thibault Friedrich",
"license": "LGPL-3.0",
"bugs": {
"url": "https://github.com/elqui-app/node-wifi/issues"
"url": "https://github.com/friedrith/node-wifi/issues"
},
"dependencies": {
"command-line-args": "^3.0.1",
Expand Down
38 changes: 26 additions & 12 deletions src/linux-connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,37 @@ var escapeShell = function(cmd) {
return '"'+cmd.replace(/(["\s'$`\\])/g,'\\$1')+'"';
};

function connectToWifi(config, ap, callback) {
var commandStr = "nmcli -w 10 device wifi connect '" + ap.ssid + "'" +
" password " + "'" + ap.password + "'" ;

module.exports = function (config) {

return function(ap, callback) {
if (config.iface) {
commandStr = commandStr + " ifname " + config.iface;
}

// commandStr = escapeShell(commandStr);

var commandStr = "nmcli dev wifi connect '" + ap.ssid + "'" +
" password " + "'" + ap.password + "'" ;
exec(commandStr, env, function(err, resp) {
callback && callback(err);
});
}

if (config.iface) {
commandStr = commandStr + " ifname " + config.iface;
}

// commandStr = escapeShell(commandStr);
module.exports = function (config) {

exec(commandStr, env, function(err, resp) {
callback && callback(err);
});
return function(ap, callback) {
if (callback) {
connectToWifi(config, ap, callback);
} else {
return new Promise(function (resolve, reject) {
connectToWifi(ap, function (err) {
if (err) {
reject(err);
} else {
resolve();
}
})
});
}
}
}
95 changes: 48 additions & 47 deletions src/linux-current-connections.js
Original file line number Diff line number Diff line change
@@ -1,61 +1,62 @@
var exec = require('child_process').exec;
var networkUtils = require('./network-utils');
var env = require('./env');

function parseIwconfig (stdout) {
var ifaces = stdout.split('\n\n');
var connections = [];
ifaces.forEach(function (iface) {
var inputs = iface.split(' ');

if (inputs.length > 0) {
var connection = {
iface: '',
ssid: '',
mac: '',
frequency: 0,
signal_level: 0
};

connection.iface = inputs.splice(0, 1)[0];
// console.log(inputs);
inputs.forEach(function (input) {
if (input != '' && input != '\n') {
if (input.match(/ESSID:\"(.*)\"/)){
connection.ssid = input.match(/ESSID:\"(.*)\"/)[1];
} else if (input.match(/Frequency:(.*) GHz/)) {
connection.frequency = parseInt(parseFloat(input.match(/Frequency:(.*) GHz/)[1])*1000);
} else if (input.match(/Access Point: (.*)/)) {
connection.mac = input.match(/Access Point: (.*)/)[1];
} else if (input.match(/Signal level=(.*) dBm/)) {
connection.signal_level = input.match(/Signal level=(.*) dBm/)[1];
}
}
});

if (connection.iface && connection.ssid && connection.frequency && connection.mac && connection.signal_level) {
connections.push(connection);
}
function getCurrentConnection(config, callback) {
var commandStr = "nmcli --terse --fields active,ssid,bssid,mode,chan,freq,signal,security,wpa-flags,rsn-flags device wifi";
if (config.iface) {
commandStr += ' ifname '+config.iface;
}

exec(commandStr, env, function(err, scanResults) {
if (err) {
callback && callback(err);
return;
}

var lines = scanResults.split('\n');
var networks = [];
for (var i = 0 ; i < lines.length ; i++) {
if (lines[i] != '') {
var fields = lines[i].replace(/\\\:/g, '&&').split(':');
if (fields[0] == 'yes') {
callback && callback(null, {
ssid: fields[1].replace(/\&\&/g, ':'),
bssid: fields[2].replace(/\&\&/g, ':'),
mac: fields[2].replace(/\&\&/g, ':'), // for retrocompatibility with version 1.x
mode: fields[3].replace(/\&\&/g, ':'),
channel: parseInt(fields[4].replace(/\&\&/g, ':')),
frequency: parseInt(fields[5].replace(/\&\&/g, ':')),
signal_level: networkUtils.dBFromQuality(fields[6].replace(/\&\&/g, ':')),
security: fields[7].replace(/\&\&/g, ':'),
security_flags: {
wpa: fields[8].replace(/\&\&/g, ':'),
rsn: fields[9].replace(/\&\&/g, ':'),
}
})
return;
}
}
});
return connections;
}
callback && callback('no-connection');
});
}

module.exports = function (config) {

return function(callback) {

var commandStr = "iwconfig" ;

if (config.iface) {
commandStr += " " + config.iface;
}

exec(commandStr, env, function(err, stdout) {
if (callback) {
getCurrentConnection(config, callback);
} else {
return new Promise(function (resolve, reject) {
getCurrentConnection(config, function (err, connection) {
if (err) {
callback && callback(err);
reject(err);
} else {
callback && callback(null, parseIwconfig(stdout));
resolve(connection);
}
});
})
});
}
}
}
34 changes: 25 additions & 9 deletions src/linux-disconnect.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,34 @@
var exec = require('child_process').exec;
var env = require('./env');

module.exports = function (config) {
function disconnect (config, callback) {
var commandStr = "nmcli device disconnect" ;

return function(callback) {
if (config.iface) {
commandStr += " " + config.iface;
}

exec(commandStr, env, function(err, resp) {
callback && callback(err);
});

var commandStr = "nmcli dev disconnect" ;
}

if (config.iface) {
commandStr += " " + config.iface;
}
module.exports = function (config) {

exec(commandStr, env, function(err, resp) {
callback && callback(err);
});
return function(callback) {
if (callback) {
disconnect(config, callback);
} else {
return new Promise(function (resolve, reject) {
disconnect(config, function (err) {
if (err) {
reject(err);
} else {
resolve();
}
})
})
}
}
}
132 changes: 51 additions & 81 deletions src/linux-scan.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,87 +2,57 @@ var exec = require('child_process').exec;
var networkUtils = require('./network-utils');
var env = require('./env');

function scanWifi(config) {

return function(callback) {

var networks = [];
var network = {};

var commandStr = "nmcli -f all -m multiline dev wifi list";

if (config.iface) {
commandStr += ' ifname '+config.iface;
function scanWifi(config, callback) {
var commandStr = "nmcli --terse --fields active,ssid,bssid,mode,chan,freq,signal,security,wpa-flags,rsn-flags device wifi list";
if (config.iface) {
commandStr += ' ifname '+config.iface;
}

exec(commandStr, env, function(err, scanResults) {
if (err) {
callback && callback(err);
return;
}

var lines = scanResults.split('\n');
var networks = [];
for (var i = 0 ; i < lines.length ; i++) {
if (lines[i] != '') {
var fields = lines[i].replace(/\\\:/g, '&&').split(':');
networks.push({
ssid: fields[1].replace(/\&\&/g, ':'),
bssid: fields[2].replace(/\&\&/g, ':'),
mac: fields[2].replace(/\&\&/g, ':'), // for retrocompatibility with version 1.x
mode: fields[3].replace(/\&\&/g, ':'),
channel: parseInt(fields[4].replace(/\&\&/g, ':')),
frequency: parseInt(fields[5].replace(/\&\&/g, ':')),
signal_level: networkUtils.dBFromQuality(fields[6].replace(/\&\&/g, ':')),
security: fields[7].replace(/\&\&/g, ':'),
security_flags: {
wpa: fields[8].replace(/\&\&/g, ':'),
rsn: fields[9].replace(/\&\&/g, ':'),
}
});
}

exec(commandStr, env, function(err, scanResults) {

if (err) {

callback && callback(err);
return;

}

var ssid = false;
var freq = false;
var signal = false;
var sec = false;
var mac = false;

// scanResults = scanResults.toString('utf8').split(' ').join('').split('\n');
scanResults = scanResults.toString('utf8').replace(/\:[ ]*/g, ':').split('\n');
// console.log(scanResults);

for (var i = 0; i < scanResults.length; i++) {

scanResults[i] = scanResults[i].split(":");
if (scanResults[i].length == 2) {
scanResults[i][1] = scanResults[i][1].split("'").join("");
}
switch(scanResults[i][0]) {
case 'SSID':
network.ssid = scanResults[i][1];
ssid = true;
break;
case 'FREQ':
network.frequency = parseInt(scanResults[i][1]);
freq = true;
break;
case 'SIGNAL':
network.signal_level = networkUtils.dBFromQuality(scanResults[i][1]);
signal = true;
break;
case 'SECURITY':
network.security = scanResults[i][1];
sec = true;
break;
case 'BSSID':
var macAdress = '';
for (var j = 1; j < 6; j++) {
macAdress = macAdress + scanResults[i][j] + ':';
}
macAdress = macAdress + scanResults[i][6];
network.mac = macAdress;
mac = true;
break;
default:
break;
}
if ((ssid) && (freq) && (signal) && (sec) && (mac)) {
networks.push(network);
network = {};
ssid = false;
freq = false;
signal = false;
sec = false;
mac = false;
}
}
var resp = networks;
callback && callback(null, resp);
});
}
}
callback && callback(null, networks);
});
}

exports.scanWifi = scanWifi;
exports.scanWifi = function (config) {
return function (callback) {
if (callback) {
scanWifi(config, callback);
} else {
return new Promise(function (resolve, reject) {
scanWifi(config, function (err, networks) {
if (err) {
reject(err);
} else {
resolve(networks);
}
});
});
}
}
};
Loading

0 comments on commit e0497fb

Please sign in to comment.