diff --git a/.gitignore b/.gitignore index b38069d..68cb4b8 100644 --- a/.gitignore +++ b/.gitignore @@ -26,3 +26,8 @@ build/Release # Dependency directory # https://docs.npmjs.com/misc/faq#should-i-check-my-node-modules-folder-into-git node_modules +package-lock.json +package.json + +test/* +.vs* diff --git a/lib/darwin.js b/lib/darwin.js index 5745259..425e9ea 100644 --- a/lib/darwin.js +++ b/lib/darwin.js @@ -1,5 +1,5 @@ // Generated by CoffeeScript 1.10.0 -(function() { +(function () { var AirPortBinary, connectionStateMap, parsePatterns, powerStateMap; AirPortBinary = "/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport"; @@ -19,20 +19,50 @@ }; module.exports = { - autoFindInterface: function() { + autoFindInterface: function () { var _iface, _interface, _msg, findInterfaceCom; this.WiFiLog("Host machine is MacOS."); - findInterfaceCom = "networksetup -listallhardwareports | awk '/^Hardware Port: (Wi-Fi|AirPort)$/{getline;print $2}'"; + findInterfaceCom = "networksetup -listallhardwareports | grep \"Wi-Fi\\|WLAN\\|wi-fi\\|wlan\\|Wlan\" -A 1"; this.WiFiLog("Executing: " + findInterfaceCom); _interface = this.execSync(findInterfaceCom); if (_interface) { + let ifaceObj = { + "adapterName": null, + "driver": null + } _iface = _interface.trim(); - _msg = "Automatically located wireless interface " + _iface + "."; + if (_iface.includes("--")) { + _iface = _iface.replace("--", ""); + } + _iface = _iface.split('\n'); + _iface = _iface.filter((e) => { + return e != ""; + }); + let ifaceArr = []; + let ifaceNum = 0; + for (let i = 0; i < _iface.length; i++) { + let paresline = parsePatterns.airport_line.exec(_iface[i]); + let KEY = paresline[1]; + let VALUE = paresline[2]; + switch (KEY) { + case "Hardware Port": + ifaceObj.driver = VALUE; + break; + case "Device": + ifaceObj.adapterName = VALUE; + ifaceArr[ifaceNum] = JSON.parse(JSON.stringify(ifaceObj)); + ifaceNum++; + break; + default: + break; + } + } + _msg = ifaceArr; this.WiFiLog(_msg); return { success: true, msg: _msg, - "interface": _iface + "interface": ifaceArr // return array }; } else { _msg = "Error: No network interface found."; @@ -44,11 +74,15 @@ }; } }, - getIfaceState: function() { + getIfaceState: function () { var KEY, VALUE, connectionData, error, error1, error2, i, interfaceState, k, len, ln, parsedLine, powerData, ref; + let allInterfaces = []; + let adapterNumber = -1; interfaceState = {}; connectionData = this.execSync(AirPortBinary + " -I"); ref = connectionData.split('\n'); + let ifaceName = this.WiFiControlSettings.iface.find(interface => interface.driver === "Wi-Fi") + interfaceState.adapterName = ifaceName.adapterName; for (k = i = 0, len = ref.length; i < len; k = ++i) { ln = ref[k]; try { @@ -62,6 +96,7 @@ switch (KEY) { case "state": interfaceState.connection = connectionStateMap[VALUE]; + adapterNumber++; break; case "SSID": interfaceState.ssid = VALUE; @@ -70,7 +105,7 @@ break; } } - powerData = this.execSync("networksetup -getairportpower " + this.WiFiControlSettings.iface); + powerData = this.execSync("networksetup -getairportpower " + ifaceName.adapterName); try { parsedLine = parsePatterns.airport_line.exec(powerData.trim()); KEY = parsedLine[1]; @@ -79,16 +114,19 @@ error = error2; return { success: false, - msg: "Unable to retrieve state of network interface " + this.WiFiControlSettings.iface + "." + msg: "Unable to retrieve state of network interface " + ifaceName.name + "." }; } interfaceState.power = powerStateMap[VALUE]; - return interfaceState; + if (adapterNumber >= 0) { + allInterfaces[adapterNumber] = JSON.parse(JSON.stringify(interfaceState)); + } + return allInterfaces; }, - connectToAP: function(_ap) { + connectToAP: function (_ap, iface) { var COMMANDS, _msg, com, connectToAPChain, error, error1, i, len, stdout; COMMANDS = { - connect: "networksetup -setairportnetwork " + this.WiFiControlSettings.iface + " \"" + _ap.ssid + "\"" + connect: "networksetup -setairportnetwork " + iface + " \"" + _ap.ssid + "\"" }; if (_ap.password.length) { COMMANDS.connect += " \"" + _ap.password + "\""; @@ -113,11 +151,11 @@ this.WiFiLog("Success!"); } }, - resetWiFi: function() { + resetWiFi: function (_iface) { var COMMANDS, _msg, com, i, len, resetWiFiChain, results, stdout; COMMANDS = { - enableAirport: "networksetup -setairportpower " + this.WiFiControlSettings.iface + " on", - disableAirport: "networksetup -setairportpower " + this.WiFiControlSettings.iface + " off" + enableAirport: "networksetup -setairportpower " + _iface + " on", + disableAirport: "networksetup -setairportpower " + _iface + " off" }; resetWiFiChain = ["disableAirport", "enableAirport"]; results = []; diff --git a/lib/wifi-control.js b/lib/wifi-control.js index e25548b..603825f 100644 --- a/lib/wifi-control.js +++ b/lib/wifi-control.js @@ -1,5 +1,5 @@ // Generated by CoffeeScript 1.10.0 -(function() { +(function () { var CXT, WiFiScanner, execSyncToBuffer, os_instructions; WiFiScanner = require('node-wifiscanner2'); @@ -8,11 +8,12 @@ CXT = { WiFiControlSettings: { + adpater: null, iface: null, debug: false, connectionTimeout: 5000 }, - execSync: function(command, options) { + execSync: function (command, options) { var results; if (options == null) { options = {}; @@ -25,15 +26,15 @@ stderr: results.stderr }; }, - WiFiLog: function(msg, error) { + WiFiLog: function (msg, error) { if (error == null) { error = false; } if (error) { - return console.error("WiFiControl: " + msg); + return console.error("WiFiControl: ", msg); } else { if (this.WiFiControlSettings.debug) { - return console.log("WiFiControl: " + msg); + return console.log("WiFiControl:", msg); } } } @@ -55,7 +56,7 @@ } module.exports = { - init: function(settings) { + init: function (settings) { if (settings == null) { settings = {}; } @@ -64,7 +65,7 @@ return this.findInterface(settings.iface); } }, - configure: function(settings) { + configure: function (settings) { if (settings == null) { settings = {}; } @@ -81,7 +82,7 @@ return this.findInterface(settings.iface); } }, - findInterface: function(iface) { + findInterface: function (iface) { var _msg, error, error1, interfaceResults; if (iface == null) { iface = null; @@ -111,7 +112,7 @@ }; } }, - scanForWiFi: function(cb) { + scanForWiFi: function (cb) { var _msg, error, error1, networks; if (CXT.WiFiControlSettings.iface == null) { _msg = "You cannot scan for nearby WiFi networks without a valid wireless interface."; @@ -133,7 +134,7 @@ networks: networks }); } else { - return WiFiScanner.scan(function(err, networks) { + return WiFiScanner.scan(function (err, networks) { if (err) { _msg = "We encountered an error while scanning for WiFi APs: " + error; CXT.WiFiLog(_msg, true); @@ -162,7 +163,7 @@ }); } }, - connectToAP: function(_ap, cb) { + connectToAP: function (_ap, adpater, cb) { var _msg, check_iface, error, error1, request_msg, t0; if (CXT.WiFiControlSettings.iface == null) { _msg = "You cannot connect to a WiFi network without a valid wireless interface."; @@ -182,15 +183,16 @@ if (_ap.password == null) { _ap.password = ""; } - os_instructions.connectToAP.call(CXT, _ap); + os_instructions.connectToAP.call(CXT, _ap, adpater); request_msg = "WiFi connection request to \"" + _ap.ssid + "\" has been processed."; CXT.WiFiLog(request_msg); t0 = new Date(); - check_iface = (function(_this) { - return function(_ap, cb) { - var connect_to_ap_result, ifaceState; - ifaceState = _this.getIfaceState(); - if (ifaceState.success && ((ifaceState.connection === "connected") || (ifaceState.connection === "disconnected"))) { + check_iface = (function (_this) { + return function (_ap, cb) { + var connect_to_ap_result, ifaceStates, ifaceState; + ifaceStates = _this.getIfaceState(); + ifaceState = ifaceStates.find(interface => interface.adapterName === adpater) + if (ifaceStates.success && ((ifaceState.connection === "connected") || (ifaceState.connection === "disconnected"))) { if (ifaceState.ssid === _ap.ssid) { _msg = "Successfully connected to \"" + _ap.ssid + "\""; CXT.WiFiLog(_msg); @@ -220,7 +222,7 @@ return; } if ((new Date() - t0) < CXT.WiFiControlSettings.connectionTimeout) { - return setTimeout(function() { + return setTimeout(function () { return check_iface(_ap, cb); }, 250); } else { @@ -242,17 +244,18 @@ }); } }, - resetWiFi: function(cb) { + resetWiFi: function (adpater, cb) { var _msg, check_iface, error, error1, t0; try { - os_instructions.resetWiFi.call(CXT); + os_instructions.resetWiFi.call(CXT, adpater); CXT.WiFiLog("Waiting for interface to finish resetting..."); t0 = new Date(); - check_iface = (function(_this) { - return function(cb) { + check_iface = (function (_this) { + return function (cb) { var _msg, ifaceState; - ifaceState = _this.getIfaceState(); - if (ifaceState.success && ((ifaceState.connection === "connected") || (ifaceState.connection === "disconnected"))) { + ifaceStates = _this.getIfaceState(); + ifaceState = ifaceStates.find(interface => interface.adapterName === adpater) + if (ifaceStates.success && ((ifaceState.connection === "connected") || (ifaceState.connection === "disconnected"))) { _msg = "Success! Wireless interface is now reset."; cb(null, { success: true, @@ -261,7 +264,7 @@ return; } if ((new Date() - t0) < CXT.WiFiControlSettings.connectionTimeout) { - return setTimeout(function() { + return setTimeout(function () { return check_iface(cb); }, 250); } else { @@ -283,7 +286,7 @@ }); } }, - getIfaceState: function() { + getIfaceState: function () { var _msg, error, error1, interfaceState; try { interfaceState = os_instructions.getIfaceState.call(CXT); @@ -291,10 +294,11 @@ interfaceState.success = true; interfaceState.msg = "Successfully acquired state of network interface " + CXT.WiFiControlSettings.iface + "."; } + // console.log(interfaceState) return interfaceState; } catch (error1) { error = error1; - _msg = "Encountered an error while acquiring network interface connection state: " + error; + _msg = error; CXT.WiFiLog(_msg, true); return { success: false, diff --git a/lib/win32.js b/lib/win32.js index b347f42..ca1db6a 100644 --- a/lib/win32.js +++ b/lib/win32.js @@ -1,5 +1,5 @@ // Generated by CoffeeScript 1.10.0 -(function() { +(function () { var connectionStateMap, fs, parsePatterns, win32WirelessProfileBuilder; fs = require('fs'); @@ -14,7 +14,7 @@ associating: "connecting" }; - win32WirelessProfileBuilder = function(ssid, security, key) { + win32WirelessProfileBuilder = function (ssid, security, key) { var profile_content; if (security == null) { security = false; @@ -38,7 +38,7 @@ }; module.exports = { - autoFindInterface: function() { + autoFindInterface: function () { var _iface, _interface, _msg, findInterfaceCom; this.WiFiLog("Host machine is Windows."); findInterfaceCom = "echo wlan"; @@ -63,11 +63,17 @@ }; } }, - getIfaceState: function() { - var KEY, VALUE, connectionData, error, error1, interfaceState, j, k, len, ln, ln_trim, parsedLine, ref; - interfaceState = {}; - connectionData = this.execSync("netsh " + this.WiFiControlSettings.iface + " show interface"); + getIfaceState: function () { + var KEY, VALUE, connectionData, error, error1, interfaceState, j, k, l, len, ln, ln_trim, parsedLine, ref, driver, name; + var tmpState, tmpSSID + l = 0 + // interfaceState = {}; + connectionData = this.execSync("chcp 65001&netsh " + this.WiFiControlSettings.iface + " show interface"); + // this.WiFiLog("connectionData:", connectionData); ref = connectionData.split('\n'); + var allinterface = [] + interfaceState = {} + var adapterNumber = -1 for (k = j = 0, len = ref.length; j < len; k = ++j) { ln = ref[k]; try { @@ -90,13 +96,19 @@ } interfaceState.power = true; switch (KEY) { + case "Name": + adapterNumber++ + interfaceState.adapterName = VALUE + interfaceState.ssid = undefined + break; case "State": - interfaceState.connection = connectionStateMap[VALUE]; + interfaceState.connection = connectionStateMap[VALUE] break; case "SSID": - interfaceState.ssid = VALUE; + interfaceState.ssid = VALUE break; case "Radio status": + if (VALUE === "Hardware Off") { interfaceState = { ssid: null, @@ -105,14 +117,28 @@ }; break; } + case "Description": + if (VALUE !== "Hardware On") { + interfaceState.driver = VALUE + if (VALUE.includes("USB")) { + interfaceState.dongle = true + } else { + interfaceState.dongle = false + } + } + break; + case "GUID": + interfaceState.guid = VALUE; + break; } - if (KEY === "SSID") { - break; + if (adapterNumber >= 0) { + allinterface[adapterNumber] = JSON.parse(JSON.stringify(interfaceState)) } } - return interfaceState; + return allinterface; }, - connectToAP: function(_ap) { + connectToAP: function (_ap, adapter) { + let isWritable = true var COMMANDS, _msg, com, connectToAPChain, error, error1, error2, i, j, l, len, ref, ssid, stdout, xmlContent; this.WiFiLog("Generating win32 wireless profile..."); ssid = { @@ -132,16 +158,28 @@ fs.writeFileSync(_ap.ssid + ".xml", xmlContent); } catch (error1) { error = error1; - _msg = "Encountered an error connecting to AP: " + error; + _msg = "Encountered an error connecting to AP: " + error + ". Write in the home dir"; this.WiFiLog(_msg, true); - return { - success: false, - msg: _msg - }; + try { + fs.writeFileSync(`${require('os').homedir}\\${_ap.ssid}.xml`, xmlContent) + isWritable = false + } catch (error2) { + error = error1; + _msg = "Encountered an error connecting to AP: " + error; + this.WiFiLog(_msg, true); + return { + success: false, + msg: _msg + }; + } } + COMMANDS = { - loadProfile: "netsh " + this.WiFiControlSettings.iface + " add profile filename=\"" + _ap.ssid + ".xml\"", - connect: "netsh " + this.WiFiControlSettings.iface + " connect ssid=\"" + _ap.ssid + "\" name=\"" + _ap.ssid + "\"" + loadProfile: isWritable ? + "netsh " + this.WiFiControlSettings.iface + " add profile filename=\"" + _ap.ssid + ".xml\"" + : + "netsh " + this.WiFiControlSettings.iface + " add profile filename=\"" + `${require('os').homedir}\\${_ap.ssid}` + ".xml\"", + connect: "netsh " + this.WiFiControlSettings.iface + " connect ssid=\"" + _ap.ssid + "\" name=\"" + _ap.ssid + "\" interface=\"" + adapter + "\"" }; connectToAPChain = ["loadProfile", "connect"]; for (l = 0, len = connectToAPChain.length; l < len; l++) { @@ -155,12 +193,14 @@ this.WiFiLog("Success!"); } this.WiFiLog("Removing temporary WiFi config file..."); - return this.execSync("del \".\\" + _ap.ssid + ".xml\""); + + return this.execSync(isWritable ? "del \".\\" + _ap.ssid + ".xml\"" + : "del \"" + `${require('os').homedir}\\${_ap.ssid}` + ".xml\"") }, - resetWiFi: function() { + resetWiFi: function (adapterName) { var COMMANDS, _msg, com, j, len, resetWiFiChain, results, stdout; COMMANDS = { - disconnect: "netsh " + this.WiFiControlSettings.iface + " disconnect" + disconnect: "netsh " + this.WiFiControlSettings.iface + " disconnect interface=\"" + adapterName + "\"" }; resetWiFiChain = ["disconnect"]; results = []; diff --git a/package.json b/package.json index 992b756..2679099 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,7 @@ "sync-exec": "^0.6.2" }, "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" + "test": "node test/test.js" }, "repository": { "type": "git", @@ -24,10 +24,12 @@ "ssid" ], "author": "Mark Solters ", - "contributors": [{ - "name": "Aaronov", - "email": "" - }], + "contributors": [ + { + "name": "Aaronov", + "email": "" + } + ], "bugs": { "url": "https://github.com/msolters/wifi-control-node/issues" }, diff --git a/test/test.js b/test/test.js index dec26d6..33ca554 100644 --- a/test/test.js +++ b/test/test.js @@ -1,45 +1,33 @@ +'use-strict' WiFiControl = require("../lib/wifi-control.js"); +const sleep = (millis) => new Promise(resolve => setTimeout(resolve, millis)); -WiFiControl.init({ - debug: true, - connectionTimeout: 2000 -}); - - -/* - * Get info about wireless interface! - */ -console.log( WiFiControl.getIfaceState() ); - -/* - * Scan for nearby WiFi! - */ -WiFiControl.scanForWiFi( function(error, response) { - if (error) console.log(error); - console.log(response); -}); - -/* - * Connect to an Access Point! - */ -var open_ap = { - ssid: "And We Will Call It....THIS LAN!" -}; -var closed_ap = { - ssid: "And We Will Call It....THIS LAN!", - password: "hench4life" -}; - -WiFiControl.connectToAP( closed_ap, function(error, response) { - if (error) console.log(error); - console.log(response); -}); - -/* - * Reset the WiFi card! - */ -WiFiControl.resetWiFi( function(error, response) { - if (error) console.log(error); - console.log(response); +WiFiControl.init({ + debug: true, + connectionTimeout: 7000 }); +let iface = WiFiControl.getIfaceState() + +console.log("getInterfaceState", iface); + +WiFiControl.resetWiFi(iface[0].adapterName, (err) => { + if (err) { + console.log(err) + } +}) + +let ap = { ssid: 'MediCam_DFBAD1', password: '1234567890' } + +WiFiControl.connectToAP(ap, iface[0].adapterName, (err, resp) => { + if (resp) { + console.log("connected: ", resp) + // WiFiControl.resetWiFi(iface[0].adapterName, (err) => { + // if (err) { + // console.log(err) + // } + // }) + } + if (err) + console.log("error", err) +}) \ No newline at end of file