From 001481e256aaca2f725760e6762f9e23962f539b Mon Sep 17 00:00:00 2001 From: Ale Date: Sat, 16 Nov 2019 23:51:22 +0100 Subject: [PATCH 1/5] chore(release): 2.0.13 --- CHANGELOG.md | 2 ++ package-lock.json | 2 +- package.json | 2 +- 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d489d1d..298810e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines. +### [2.0.13](https://github.com/friedrith/node-wifi/compare/v2.0.12...v2.0.13) (2019-11-16) + ### [2.0.12](https://github.com/friedrith/node-wifi/compare/v2.0.11...v2.0.12) (2019-09-06) diff --git a/package-lock.json b/package-lock.json index 247c1cf..9bb6184 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "node-wifi", - "version": "2.0.12", + "version": "2.0.13", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index fc1ddff..b51cc12 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "node-wifi", - "version": "2.0.12", + "version": "2.0.13", "description": "NodeJS tool to manage wifi", "bin": { "wifi": "bin/wifi.js" From dbb6c0217b5aa98808035404bec0e8f768676426 Mon Sep 17 00:00:00 2001 From: Ale Date: Sat, 16 Nov 2019 23:52:33 +0100 Subject: [PATCH 2/5] chore(release): 2.0.14 --- CHANGELOG.md | 2 ++ package-lock.json | 2 +- package.json | 2 +- 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 298810e..34fb943 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines. +### [2.0.14](https://github.com/friedrith/node-wifi/compare/v2.0.13...v2.0.14) (2019-11-16) + ### [2.0.13](https://github.com/friedrith/node-wifi/compare/v2.0.12...v2.0.13) (2019-11-16) ### [2.0.12](https://github.com/friedrith/node-wifi/compare/v2.0.11...v2.0.12) (2019-09-06) diff --git a/package-lock.json b/package-lock.json index 9bb6184..96f02a7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "node-wifi", - "version": "2.0.13", + "version": "2.0.14", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index b51cc12..00ddae9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "node-wifi", - "version": "2.0.13", + "version": "2.0.14", "description": "NodeJS tool to manage wifi", "bin": { "wifi": "bin/wifi.js" From 806591ec538813d24aea99a66d68cc6c8467fa4b Mon Sep 17 00:00:00 2001 From: Ale Date: Sun, 24 Nov 2019 19:15:59 +0100 Subject: [PATCH 3/5] Mac Disconnect function added - Added a mac-disconnect function (based on wifi-control), which makes possible to disconnect from the actual network without running 'root' commands - Configs expanded to handle the possibility of waiting a fixed time between the two OSX required command --- src/mac-disconnect.js | 62 +++++++++++++++++++++++++++++++++++++++++++ src/wifi.js | 9 ++++++- 2 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 src/mac-disconnect.js diff --git a/src/mac-disconnect.js b/src/mac-disconnect.js new file mode 100644 index 0000000..45540b3 --- /dev/null +++ b/src/mac-disconnect.js @@ -0,0 +1,62 @@ +var execFile = require('child_process').execFile; +var env = require('./env'); + +// sleep function is required to delay the two commands without breaking into a promise (like with setTimeout) +function sleep(milliseconds) { + var start = new Date().getTime(); + for (var i = 0; i < 1e7; i++) { + if ((new Date().getTime() - start) > milliseconds){ + break; + } + } +} + +async function disconnect(config, callback) { + var iface = 'en0'; + var delay = 0; + var option = '-setairportpower'; + + if (config.iface) { + iface = config.iface.toString(); + } + if (config.delay) { + delay = parseInt(config.delay); + } + + let commands = [ + [option, iface, 'off'], + [option, iface, 'on'] + ]; + + let resp; + try { + resp = await execFile('networksetup', commands[0], { env }); + } catch(err) { + callback && callback(err); + } + + if (delay!==0) + await sleep(delay*1000); + + execFile('networksetup', commands[1], { env }, function(err) { + callback && callback(err); + }); +} + +module.exports = function(config) { + 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(); + } + }); + }); + } + }; +}; diff --git a/src/wifi.js b/src/wifi.js index dff9f1c..e4b6d9d 100644 --- a/src/wifi.js +++ b/src/wifi.js @@ -8,13 +8,15 @@ var linuxDelete = require('./linux-delete'); var linuxGetCurrentConnections = require('./linux-current-connections'); var linuxScan = require('./linux-scan.js'); var macConnect = require('./mac-connect.js'); +var macDisconnect = require('./mac-disconnect.js'); var macScan = require('./mac-scan.js'); var macDelete = require('./mac-delete'); var macGetCurrentConnections = require('./mac-current-connections'); var config = { debug: false, - iface: null + iface: null, + delay: 0 }; function init(options) { @@ -25,6 +27,10 @@ function init(options) { if (options && options.iface) { config.iface = options.iface; } + + if (options && options.delay) { + config.delay = options.delay; + } var scan = function() { throw new Error('ERROR : not available for this OS'); @@ -53,6 +59,7 @@ function init(options) { case 'darwin': connect = macConnect(config); scan = macScan(config); + disconnect = macDisconnect(config); deleteConnection = macDelete(config); getCurrentConnections = macGetCurrentConnections(config); break; From 0b4ba4591551cca622a0ef95e4a480aca90fef55 Mon Sep 17 00:00:00 2001 From: Ale Date: Tue, 3 Dec 2019 14:20:41 +0100 Subject: [PATCH 4/5] fix: removed tab and unused variable to fit the code style requirements --- src/mac-disconnect.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/mac-disconnect.js b/src/mac-disconnect.js index 45540b3..4909c90 100644 --- a/src/mac-disconnect.js +++ b/src/mac-disconnect.js @@ -24,13 +24,12 @@ async function disconnect(config, callback) { } let commands = [ - [option, iface, 'off'], - [option, iface, 'on'] + [option, iface, 'off'], + [option, iface, 'on'] ]; - let resp; try { - resp = await execFile('networksetup', commands[0], { env }); + await execFile('networksetup', commands[0], { env }); } catch(err) { callback && callback(err); } From ebc91d45d8184d22ff96aa0e0992ddf80e9d5aa8 Mon Sep 17 00:00:00 2001 From: Ale Date: Tue, 3 Dec 2019 14:23:25 +0100 Subject: [PATCH 5/5] fix: prettier fixes to meet the standard --- src/mac-disconnect.js | 26 +++++++++++--------------- src/wifi.js | 2 +- 2 files changed, 12 insertions(+), 16 deletions(-) diff --git a/src/mac-disconnect.js b/src/mac-disconnect.js index 4909c90..e53d30d 100644 --- a/src/mac-disconnect.js +++ b/src/mac-disconnect.js @@ -5,7 +5,7 @@ var env = require('./env'); function sleep(milliseconds) { var start = new Date().getTime(); for (var i = 0; i < 1e7; i++) { - if ((new Date().getTime() - start) > milliseconds){ + if (new Date().getTime() - start > milliseconds) { break; } } @@ -23,23 +23,19 @@ async function disconnect(config, callback) { delay = parseInt(config.delay); } - let commands = [ - [option, iface, 'off'], - [option, iface, 'on'] - ]; - + let commands = [[option, iface, 'off'], [option, iface, 'on']]; + try { - await execFile('networksetup', commands[0], { env }); - } catch(err) { - callback && callback(err); + await execFile('networksetup', commands[0], { env }); + } catch (err) { + callback && callback(err); } - - if (delay!==0) - await sleep(delay*1000); - + + if (delay !== 0) await sleep(delay * 1000); + execFile('networksetup', commands[1], { env }, function(err) { - callback && callback(err); - }); + callback && callback(err); + }); } module.exports = function(config) { diff --git a/src/wifi.js b/src/wifi.js index e4b6d9d..f3f8758 100644 --- a/src/wifi.js +++ b/src/wifi.js @@ -27,7 +27,7 @@ function init(options) { if (options && options.iface) { config.iface = options.iface; } - + if (options && options.delay) { config.delay = options.delay; }