diff --git a/CHANGELOG.md b/CHANGELOG.md index d489d1d..34fb943 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ 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 247c1cf..96f02a7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "node-wifi", - "version": "2.0.12", + "version": "2.0.14", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index fc1ddff..00ddae9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "node-wifi", - "version": "2.0.12", + "version": "2.0.14", "description": "NodeJS tool to manage wifi", "bin": { "wifi": "bin/wifi.js" diff --git a/src/mac-disconnect.js b/src/mac-disconnect.js new file mode 100644 index 0000000..e53d30d --- /dev/null +++ b/src/mac-disconnect.js @@ -0,0 +1,57 @@ +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']]; + + try { + 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..f3f8758 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) { @@ -26,6 +28,10 @@ function init(options) { 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;