Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
57 changes: 57 additions & 0 deletions src/mac-disconnect.js
Original file line number Diff line number Diff line change
@@ -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();
}
});
});
}
};
};
9 changes: 8 additions & 1 deletion src/wifi.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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');
};
Expand Down Expand Up @@ -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;
Expand Down