From 747146ee8e3b2a33d5604a3d5753d01ba25a68d9 Mon Sep 17 00:00:00 2001 From: Ben Yang Date: Wed, 27 Nov 2019 16:26:35 +0800 Subject: [PATCH 01/22] Fix bug about getwifiinterface when the language of cmd is not english --- .gitignore | 2 ++ lib/win32.js | 2 +- package.json | 10 ++++++---- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index b38069d..6458677 100644 --- a/.gitignore +++ b/.gitignore @@ -26,3 +26,5 @@ 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 diff --git a/lib/win32.js b/lib/win32.js index b347f42..ba327e6 100644 --- a/lib/win32.js +++ b/lib/win32.js @@ -66,7 +66,7 @@ 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"); + connectionData = this.execSync("chcp 437&netsh " + this.WiFiControlSettings.iface + " show interface"); ref = connectionData.split('\n'); for (k = j = 0, len = ref.length; j < len; k = ++j) { ln = ref[k]; diff --git a/package.json b/package.json index 992b756..874c089 100644 --- a/package.json +++ b/package.json @@ -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" }, From 82a2a4d950aa72e0111f008654c787007e323f04 Mon Sep 17 00:00:00 2001 From: Ben Yang Date: Mon, 2 Dec 2019 16:19:23 +0800 Subject: [PATCH 02/22] Connect Wifi using usb wifi dongle --- lib/wifi-control.js | 5 +++-- lib/win32.js | 13 ++++++++++--- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/lib/wifi-control.js b/lib/wifi-control.js index e25548b..b65c9fc 100644 --- a/lib/wifi-control.js +++ b/lib/wifi-control.js @@ -8,6 +8,7 @@ CXT = { WiFiControlSettings: { + adpater: null, iface: null, debug: false, connectionTimeout: 5000 @@ -30,7 +31,7 @@ error = false; } if (error) { - return console.error("WiFiControl: " + msg); + return console.error("WiFiControl: " , msg); } else { if (this.WiFiControlSettings.debug) { return console.log("WiFiControl: " + msg); @@ -294,7 +295,7 @@ 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 ba327e6..dc40a99 100644 --- a/lib/win32.js +++ b/lib/win32.js @@ -64,9 +64,11 @@ } }, getIfaceState: function() { - var KEY, VALUE, connectionData, error, error1, interfaceState, j, k, len, ln, ln_trim, parsedLine, ref; + var KEY, VALUE, connectionData, error, error1, interfaceState, j, k, l, len, ln, ln_trim, parsedLine, ref; + l = 0 interfaceState = {}; connectionData = this.execSync("chcp 437&netsh " + this.WiFiControlSettings.iface + " show interface"); + console.log("connectionData" + connectionData) ref = connectionData.split('\n'); for (k = j = 0, len = ref.length; j < len; k = ++j) { ln = ref[k]; @@ -90,6 +92,9 @@ } interfaceState.power = true; switch (KEY) { + case "Name": + interfaceState.adapterName = VALUE + this.WiFiControlSettings.adapter = VALUE case "State": interfaceState.connection = connectionStateMap[VALUE]; break; @@ -107,7 +112,9 @@ } } if (KEY === "SSID") { - break; + l ++ + if (l>2) + break; } } return interfaceState; @@ -141,7 +148,7 @@ } COMMANDS = { loadProfile: "netsh " + this.WiFiControlSettings.iface + " add profile filename=\"" + _ap.ssid + ".xml\"", - connect: "netsh " + this.WiFiControlSettings.iface + " connect ssid=\"" + _ap.ssid + "\" name=\"" + _ap.ssid + "\"" + connect: "netsh " + this.WiFiControlSettings.iface + " connect ssid=\"" + _ap.ssid + "\" name=\"" + _ap.ssid + "\" interface=\"" + this.WiFiControlSettings.adapter + "\"" }; connectToAPChain = ["loadProfile", "connect"]; for (l = 0, len = connectToAPChain.length; l < len; l++) { From e001e558cb7075d3eee94d363350c8e0f8150bd5 Mon Sep 17 00:00:00 2001 From: Ben Yang Date: Thu, 12 Dec 2019 14:24:52 +0800 Subject: [PATCH 03/22] Detect Wi-Fi dongle by driver name --- lib/wifi-control.js | 1 + lib/win32.js | 52 ++++++++++++++++++++++++++++++++------------- test/test.js | 44 ++++++-------------------------------- 3 files changed, 44 insertions(+), 53 deletions(-) diff --git a/lib/wifi-control.js b/lib/wifi-control.js index b65c9fc..af59516 100644 --- a/lib/wifi-control.js +++ b/lib/wifi-control.js @@ -292,6 +292,7 @@ interfaceState.success = true; interfaceState.msg = "Successfully acquired state of network interface " + CXT.WiFiControlSettings.iface + "."; } + console.log(interfaceState) return interfaceState; } catch (error1) { error = error1; diff --git a/lib/win32.js b/lib/win32.js index dc40a99..a4556c0 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,8 +63,9 @@ }; } }, - getIfaceState: function() { - var KEY, VALUE, connectionData, error, error1, interfaceState, j, k, l, len, ln, ln_trim, parsedLine, ref; + 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 437&netsh " + this.WiFiControlSettings.iface + " show interface"); @@ -93,13 +94,25 @@ interfaceState.power = true; switch (KEY) { case "Name": - interfaceState.adapterName = VALUE - this.WiFiControlSettings.adapter = VALUE + name = VALUE + name = VALUE case "State": - interfaceState.connection = connectionStateMap[VALUE]; + if (driver) { + if (driver.includes("USB") && interfaceState.connection === undefined) { + interfaceState.connection = connectionStateMap[VALUE]; + } + } else { + tmpState = connectionStateMap[VALUE] + } break; case "SSID": - interfaceState.ssid = VALUE; + if (driver) { + if (driver.includes("USB") && interfaceState.ssid === undefined) { + interfaceState.ssid = VALUE; + } + } else { + tmpSSID = VALUE; + } break; case "Radio status": if (VALUE === "Hardware Off") { @@ -110,16 +123,25 @@ }; break; } - } - if (KEY === "SSID") { - l ++ - if (l>2) + case "Description": + if (VALUE.includes("USB")) { + interfaceState.adapterName = name + this.WiFiControlSettings.adapter = name + driver = VALUE + } break; } } + if (interfaceState.connection === undefined) { + interfaceState.connection = tmpState + interfaceState.ssid = tmpSSID + interfaceState.dongle = false + } else { + interfaceState.dongle = true + } return interfaceState; }, - connectToAP: function(_ap) { + connectToAP: function (_ap) { var COMMANDS, _msg, com, connectToAPChain, error, error1, error2, i, j, l, len, ref, ssid, stdout, xmlContent; this.WiFiLog("Generating win32 wireless profile..."); ssid = { @@ -164,7 +186,7 @@ this.WiFiLog("Removing temporary WiFi config file..."); return this.execSync("del \".\\" + _ap.ssid + ".xml\""); }, - resetWiFi: function() { + resetWiFi: function () { var COMMANDS, _msg, com, j, len, resetWiFiChain, results, stdout; COMMANDS = { disconnect: "netsh " + this.WiFiControlSettings.iface + " disconnect" diff --git a/test/test.js b/test/test.js index dec26d6..52330dd 100644 --- a/test/test.js +++ b/test/test.js @@ -1,45 +1,13 @@ +'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); -}); +setInterval(() => { + WiFiControl.getIfaceState() +}, 1000) \ No newline at end of file From 2b8503893924bf515ad4ad5f609ab5ef7038b2ee Mon Sep 17 00:00:00 2001 From: Ben Yang Date: Thu, 12 Dec 2019 14:42:31 +0800 Subject: [PATCH 04/22] Fix error about getting adaptname --- lib/win32.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/win32.js b/lib/win32.js index a4556c0..bb428fb 100644 --- a/lib/win32.js +++ b/lib/win32.js @@ -100,7 +100,7 @@ if (driver) { if (driver.includes("USB") && interfaceState.connection === undefined) { interfaceState.connection = connectionStateMap[VALUE]; - } + } } else { tmpState = connectionStateMap[VALUE] } @@ -135,6 +135,8 @@ if (interfaceState.connection === undefined) { interfaceState.connection = tmpState interfaceState.ssid = tmpSSID + interfaceState.adapterName = name + this.WiFiControlSettings.adapter = name interfaceState.dongle = false } else { interfaceState.dongle = true From 72ebfa8404e1f3da2b003069a2c4a3d8d8b5f3ff Mon Sep 17 00:00:00 2001 From: Ben Yang Date: Thu, 12 Dec 2019 17:36:23 +0800 Subject: [PATCH 05/22] Fix bug about resetting wifi --- lib/win32.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/win32.js b/lib/win32.js index bb428fb..90218d6 100644 --- a/lib/win32.js +++ b/lib/win32.js @@ -191,7 +191,7 @@ resetWiFi: function () { var COMMANDS, _msg, com, j, len, resetWiFiChain, results, stdout; COMMANDS = { - disconnect: "netsh " + this.WiFiControlSettings.iface + " disconnect" + disconnect: "netsh " + this.WiFiControlSettings.iface + " disconnect interface=\"" + this.WiFiControlSettings.adapter + "\"" }; resetWiFiChain = ["disconnect"]; results = []; From 232ed9938d4b86f478f1456c2acff0de0e9710b7 Mon Sep 17 00:00:00 2001 From: Ben Yang Date: Fri, 13 Dec 2019 16:33:13 +0800 Subject: [PATCH 06/22] Support multi dongle --- lib/win32.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/win32.js b/lib/win32.js index 90218d6..2adc44e 100644 --- a/lib/win32.js +++ b/lib/win32.js @@ -98,7 +98,7 @@ name = VALUE case "State": if (driver) { - if (driver.includes("USB") && interfaceState.connection === undefined) { + if (driver.includes("USB")) { interfaceState.connection = connectionStateMap[VALUE]; } } else { @@ -107,7 +107,7 @@ break; case "SSID": if (driver) { - if (driver.includes("USB") && interfaceState.ssid === undefined) { + if (driver.includes("USB")) { interfaceState.ssid = VALUE; } } else { @@ -128,6 +128,8 @@ interfaceState.adapterName = name this.WiFiControlSettings.adapter = name driver = VALUE + } else { + driver = undefined } break; } @@ -141,6 +143,10 @@ } else { interfaceState.dongle = true } + console.log(interfaceState) + if (interfaceState.connection !== "connected") { + interfaceState.ssid = undefined + } return interfaceState; }, connectToAP: function (_ap) { From a34a723ece8c828216d1ebb15bcd0cfb6061294e Mon Sep 17 00:00:00 2001 From: Ben Yang Date: Mon, 16 Dec 2019 14:30:19 +0800 Subject: [PATCH 07/22] Show all wifi states including usb adapter --- .gitignore | 2 + lib/wifi-control.js | 11 ++-- lib/win32.js | 138 +++++++++++++++++++++++++++++++------------- test/test.js | 20 ++++++- 4 files changed, 124 insertions(+), 47 deletions(-) diff --git a/.gitignore b/.gitignore index 6458677..b9c1ee5 100644 --- a/.gitignore +++ b/.gitignore @@ -28,3 +28,5 @@ build/Release node_modules package-lock.json package.json + +test/* diff --git a/lib/wifi-control.js b/lib/wifi-control.js index af59516..991100e 100644 --- a/lib/wifi-control.js +++ b/lib/wifi-control.js @@ -163,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."; @@ -183,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"))) { + 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); diff --git a/lib/win32.js b/lib/win32.js index 2adc44e..194d53f 100644 --- a/lib/win32.js +++ b/lib/win32.js @@ -67,10 +67,89 @@ var KEY, VALUE, connectionData, error, error1, interfaceState, j, k, l, len, ln, ln_trim, parsedLine, ref, driver, name; var tmpState, tmpSSID l = 0 - interfaceState = {}; + // interfaceState = {}; connectionData = this.execSync("chcp 437&netsh " + this.WiFiControlSettings.iface + " show interface"); console.log("connectionData" + connectionData) ref = connectionData.split('\n'); + // for (k = j = 0, len = ref.length; j < len; k = ++j) { + // ln = ref[k]; + // try { + // ln_trim = ln.trim(); + // if (ln_trim === "Software Off") { + // interfaceState = { + // ssid: null, + // connected: false, + // power: false + // }; + // break; + // } else { + // parsedLine = parsePatterns.netsh_line.exec(ln_trim); + // KEY = parsedLine[1].trim(); + // VALUE = parsedLine[2].trim(); + // } + // } catch (error1) { + // error = error1; + // continue; + // } + // interfaceState.power = true; + // switch (KEY) { + // case "Name": + // name = VALUE + // name = VALUE + // case "State": + // if (driver) { + // if (driver.includes("USB")) { + // interfaceState.connection = connectionStateMap[VALUE]; + // } + // } else { + // tmpState = connectionStateMap[VALUE] + // } + // break; + // case "SSID": + // if (driver) { + // if (driver.includes("USB")) { + // interfaceState.ssid = VALUE; + // } + // } else { + // tmpSSID = VALUE; + // } + // break; + // case "Radio status": + // if (VALUE === "Hardware Off") { + // interfaceState = { + // ssid: null, + // connected: false, + // power: false + // }; + // break; + // } + // case "Description": + // if (VALUE.includes("USB")) { + // interfaceState.adapterName = name + // this.WiFiControlSettings.adapter = name + // driver = VALUE + // } else { + // driver = undefined + // } + // break; + // } + // } + // if (interfaceState.connection === undefined) { + // interfaceState.connection = tmpState + // interfaceState.ssid = tmpSSID + // interfaceState.adapterName = name + // this.WiFiControlSettings.adapter = name + // interfaceState.dongle = false + // } else { + // interfaceState.dongle = true + // } + // console.log(interfaceState) + // if (interfaceState.connection !== "connected") { + // interfaceState.ssid = undefined + // } + var allinterface = [] + interfaceState = {} + var adapterNumber = -1 for (k = j = 0, len = ref.length; j < len; k = ++j) { ln = ref[k]; try { @@ -94,27 +173,18 @@ interfaceState.power = true; switch (KEY) { case "Name": - name = VALUE - name = VALUE + adapterNumber++ + interfaceState.adapterName = VALUE + interfaceState.ssid = undefined + break; case "State": - if (driver) { - if (driver.includes("USB")) { - interfaceState.connection = connectionStateMap[VALUE]; - } - } else { - tmpState = connectionStateMap[VALUE] - } + interfaceState.connection = connectionStateMap[VALUE] break; case "SSID": - if (driver) { - if (driver.includes("USB")) { - interfaceState.ssid = VALUE; - } - } else { - tmpSSID = VALUE; - } + interfaceState.ssid = VALUE break; case "Radio status": + if (VALUE === "Hardware Off") { interfaceState = { ssid: null, @@ -124,32 +194,22 @@ break; } case "Description": - if (VALUE.includes("USB")) { - interfaceState.adapterName = name - this.WiFiControlSettings.adapter = name - driver = VALUE - } else { - driver = undefined + if (VALUE !== "Hardware On") { + if (VALUE.includes("USB")) { + interfaceState.dongle = true + } else { + interfaceState.dongle = false + } } break; } + if (adapterNumber >= 0) { + allinterface[adapterNumber] = JSON.parse(JSON.stringify(interfaceState)) + } } - if (interfaceState.connection === undefined) { - interfaceState.connection = tmpState - interfaceState.ssid = tmpSSID - interfaceState.adapterName = name - this.WiFiControlSettings.adapter = name - interfaceState.dongle = false - } else { - interfaceState.dongle = true - } - console.log(interfaceState) - if (interfaceState.connection !== "connected") { - interfaceState.ssid = undefined - } - return interfaceState; + return allinterface; }, - connectToAP: function (_ap) { + connectToAP: function (_ap, adapter) { var COMMANDS, _msg, com, connectToAPChain, error, error1, error2, i, j, l, len, ref, ssid, stdout, xmlContent; this.WiFiLog("Generating win32 wireless profile..."); ssid = { @@ -178,7 +238,7 @@ } COMMANDS = { loadProfile: "netsh " + this.WiFiControlSettings.iface + " add profile filename=\"" + _ap.ssid + ".xml\"", - connect: "netsh " + this.WiFiControlSettings.iface + " connect ssid=\"" + _ap.ssid + "\" name=\"" + _ap.ssid + "\" interface=\"" + this.WiFiControlSettings.adapter + "\"" + 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++) { diff --git a/test/test.js b/test/test.js index 52330dd..0015ed8 100644 --- a/test/test.js +++ b/test/test.js @@ -8,6 +8,20 @@ WiFiControl.init({ connectionTimeout: 2000 }); -setInterval(() => { - WiFiControl.getIfaceState() -}, 1000) \ No newline at end of file +var a = WiFiControl.getIfaceState() +console.log(a.find(interface => interface.adapterName === 'Wi-Fi')) + +a.forEach(state => { + if (state && state.ssid.startsWith('MediCam') && state.connection === 'connected') { + console.log(state) + } +}); + +// ap = { +// ssid: 'UNICORNWORKING-5G', +// password: '25175089' +// } + +// WiFiControl.connectToAP(ap, a[0].adapterName, () => { +// console.log("RRRRRRRRRRRRRRRRRRRR") +// }) \ No newline at end of file From 1113f86423557d7bcfe3d028099b20e5329f9ee9 Mon Sep 17 00:00:00 2001 From: Ben Yang Date: Mon, 16 Dec 2019 16:56:48 +0800 Subject: [PATCH 08/22] Fix bug about reseting wifi adapter --- lib/wifi-control.js | 9 +++++---- lib/win32.js | 4 ++-- test/test.js | 1 - 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/wifi-control.js b/lib/wifi-control.js index 991100e..ee071c7 100644 --- a/lib/wifi-control.js +++ b/lib/wifi-control.js @@ -244,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) { 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, diff --git a/lib/win32.js b/lib/win32.js index 194d53f..caaac9e 100644 --- a/lib/win32.js +++ b/lib/win32.js @@ -254,10 +254,10 @@ this.WiFiLog("Removing temporary WiFi config file..."); return this.execSync("del \".\\" + _ap.ssid + ".xml\""); }, - resetWiFi: function () { + resetWiFi: function (adapterName) { var COMMANDS, _msg, com, j, len, resetWiFiChain, results, stdout; COMMANDS = { - disconnect: "netsh " + this.WiFiControlSettings.iface + " disconnect interface=\"" + this.WiFiControlSettings.adapter + "\"" + disconnect: "netsh " + this.WiFiControlSettings.iface + " disconnect interface=\"" + adapterName + "\"" }; resetWiFiChain = ["disconnect"]; results = []; diff --git a/test/test.js b/test/test.js index 0015ed8..d848316 100644 --- a/test/test.js +++ b/test/test.js @@ -16,7 +16,6 @@ a.forEach(state => { console.log(state) } }); - // ap = { // ssid: 'UNICORNWORKING-5G', // password: '25175089' From 4a77593e86f6bac3433da2e5a1bd43822e15cb8a Mon Sep 17 00:00:00 2001 From: Ben Yang Date: Tue, 17 Dec 2019 10:18:52 +0800 Subject: [PATCH 09/22] Update test.js --- test/test.js | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/test/test.js b/test/test.js index d848316..7efb3c3 100644 --- a/test/test.js +++ b/test/test.js @@ -4,23 +4,20 @@ const sleep = (millis) => new Promise(resolve => setTimeout(resolve, millis)); WiFiControl.init({ - debug: true, - connectionTimeout: 2000 + debug: true, + connectionTimeout: 2000 }); +let iface = WiFiControl.getIfaceState() -var a = WiFiControl.getIfaceState() -console.log(a.find(interface => interface.adapterName === 'Wi-Fi')) +WiFiControl.resetWiFi(iface[0].adapterName) -a.forEach(state => { - if (state && state.ssid.startsWith('MediCam') && state.connection === 'connected') { - console.log(state) - } -}); -// ap = { -// ssid: 'UNICORNWORKING-5G', -// password: '25175089' -// } +let ap = { ssid: 'your ap name', password: 'your ap password' } -// WiFiControl.connectToAP(ap, a[0].adapterName, () => { -// console.log("RRRRRRRRRRRRRRRRRRRR") -// }) \ No newline at end of file +WiFiControl.connectToAP(ap, iface[0].adapterName, (err, resp) => { + if (resp) { + console.log("connected: " + resp) + WiFiControl.resetWiFi(iface[0].adapterName) + } + if (err) + console.log("error" + ReferenceError) +}) \ No newline at end of file From 4f38e1d8e601144b44c1af50c44517dca8672e16 Mon Sep 17 00:00:00 2001 From: Ben Yang Date: Fri, 3 Jan 2020 12:07:10 +0800 Subject: [PATCH 10/22] Get wifi driver name --- lib/win32.js | 1 + package.json | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/win32.js b/lib/win32.js index caaac9e..d074804 100644 --- a/lib/win32.js +++ b/lib/win32.js @@ -195,6 +195,7 @@ } case "Description": if (VALUE !== "Hardware On") { + interfaceState.driver = VALUE if (VALUE.includes("USB")) { interfaceState.dongle = true } else { diff --git a/package.json b/package.json index 874c089..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", From 897bf4c7366ef8cb15e18b07742201674af9dfc0 Mon Sep 17 00:00:00 2001 From: watson-yu Date: Fri, 17 Jan 2020 15:41:22 +0800 Subject: [PATCH 11/22] Fix question marks in adapter names --- lib/win32.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/win32.js b/lib/win32.js index d074804..fd31a41 100644 --- a/lib/win32.js +++ b/lib/win32.js @@ -68,8 +68,8 @@ var tmpState, tmpSSID l = 0 // interfaceState = {}; - connectionData = this.execSync("chcp 437&netsh " + this.WiFiControlSettings.iface + " show interface"); - console.log("connectionData" + connectionData) + connectionData = this.execSync("chcp 65001&netsh " + this.WiFiControlSettings.iface + " show interface"); + this.WiFiLog("connectionData:", connectionData); ref = connectionData.split('\n'); // for (k = j = 0, len = ref.length; j < len; k = ++j) { // ln = ref[k]; From 9aaa86bbe07a90a422e5ec28db5952aa06ccb472 Mon Sep 17 00:00:00 2001 From: Ben Yang Date: Fri, 7 Feb 2020 15:54:12 +0800 Subject: [PATCH 12/22] Comment log --- lib/wifi-control.js | 2 +- lib/win32.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/wifi-control.js b/lib/wifi-control.js index ee071c7..e137d29 100644 --- a/lib/wifi-control.js +++ b/lib/wifi-control.js @@ -294,7 +294,7 @@ interfaceState.success = true; interfaceState.msg = "Successfully acquired state of network interface " + CXT.WiFiControlSettings.iface + "."; } - console.log(interfaceState) + // console.log(interfaceState) return interfaceState; } catch (error1) { error = error1; diff --git a/lib/win32.js b/lib/win32.js index fd31a41..f4ae317 100644 --- a/lib/win32.js +++ b/lib/win32.js @@ -69,7 +69,7 @@ l = 0 // interfaceState = {}; connectionData = this.execSync("chcp 65001&netsh " + this.WiFiControlSettings.iface + " show interface"); - this.WiFiLog("connectionData:", connectionData); + // this.WiFiLog("connectionData:", connectionData); ref = connectionData.split('\n'); // for (k = j = 0, len = ref.length; j < len; k = ++j) { // ln = ref[k]; From 8477f55f70b6ce9a9fff87a5b052d5a9c4686494 Mon Sep 17 00:00:00 2001 From: ssyang Date: Wed, 19 Feb 2020 11:27:39 +0800 Subject: [PATCH 13/22] Create connection profile if the directory is not writable --- lib/win32.js | 95 +++++++++------------------------------------------- test/test.js | 18 +++++++--- 2 files changed, 29 insertions(+), 84 deletions(-) diff --git a/lib/win32.js b/lib/win32.js index f4ae317..e01dcaa 100644 --- a/lib/win32.js +++ b/lib/win32.js @@ -71,82 +71,6 @@ connectionData = this.execSync("chcp 65001&netsh " + this.WiFiControlSettings.iface + " show interface"); // this.WiFiLog("connectionData:", connectionData); ref = connectionData.split('\n'); - // for (k = j = 0, len = ref.length; j < len; k = ++j) { - // ln = ref[k]; - // try { - // ln_trim = ln.trim(); - // if (ln_trim === "Software Off") { - // interfaceState = { - // ssid: null, - // connected: false, - // power: false - // }; - // break; - // } else { - // parsedLine = parsePatterns.netsh_line.exec(ln_trim); - // KEY = parsedLine[1].trim(); - // VALUE = parsedLine[2].trim(); - // } - // } catch (error1) { - // error = error1; - // continue; - // } - // interfaceState.power = true; - // switch (KEY) { - // case "Name": - // name = VALUE - // name = VALUE - // case "State": - // if (driver) { - // if (driver.includes("USB")) { - // interfaceState.connection = connectionStateMap[VALUE]; - // } - // } else { - // tmpState = connectionStateMap[VALUE] - // } - // break; - // case "SSID": - // if (driver) { - // if (driver.includes("USB")) { - // interfaceState.ssid = VALUE; - // } - // } else { - // tmpSSID = VALUE; - // } - // break; - // case "Radio status": - // if (VALUE === "Hardware Off") { - // interfaceState = { - // ssid: null, - // connected: false, - // power: false - // }; - // break; - // } - // case "Description": - // if (VALUE.includes("USB")) { - // interfaceState.adapterName = name - // this.WiFiControlSettings.adapter = name - // driver = VALUE - // } else { - // driver = undefined - // } - // break; - // } - // } - // if (interfaceState.connection === undefined) { - // interfaceState.connection = tmpState - // interfaceState.ssid = tmpSSID - // interfaceState.adapterName = name - // this.WiFiControlSettings.adapter = name - // interfaceState.dongle = false - // } else { - // interfaceState.dongle = true - // } - // console.log(interfaceState) - // if (interfaceState.connection !== "connected") { - // interfaceState.ssid = undefined - // } var allinterface = [] interfaceState = {} var adapterNumber = -1 @@ -211,6 +135,7 @@ return allinterface; }, 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 = { @@ -227,7 +152,12 @@ xmlContent = win32WirelessProfileBuilder(ssid); } try { - fs.writeFileSync(_ap.ssid + ".xml", xmlContent); + fs.promises.access('./', fs.constants.W_OK).then(() => { + fs.writeFileSync(_ap.ssid + ".xml", xmlContent); + }).catch(() => { + fs.writeFileSync(`${require('os').homedir}\\${_ap.ssid}.xml`, xmlContent) + isWritable = false + }) } catch (error1) { error = error1; _msg = "Encountered an error connecting to AP: " + error; @@ -238,7 +168,10 @@ }; } COMMANDS = { - loadProfile: "netsh " + this.WiFiControlSettings.iface + " add profile filename=\"" + _ap.ssid + ".xml\"", + 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"]; @@ -253,7 +186,11 @@ this.WiFiLog("Success!"); } this.WiFiLog("Removing temporary WiFi config file..."); - return this.execSync("del \".\\" + _ap.ssid + ".xml\""); + return isWritable ? + this.execSync("del \".\\" + _ap.ssid + ".xml\"") + : + this.execSync("del \".\\" + `${require('os').homedir}\\${_ap.ssid}` + ".xml\""); + }, resetWiFi: function (adapterName) { var COMMANDS, _msg, com, j, len, resetWiFiChain, results, stdout; diff --git a/test/test.js b/test/test.js index 7efb3c3..ed3c425 100644 --- a/test/test.js +++ b/test/test.js @@ -9,15 +9,23 @@ WiFiControl.init({ }); let iface = WiFiControl.getIfaceState() -WiFiControl.resetWiFi(iface[0].adapterName) +WiFiControl.resetWiFi(iface[0].adapterName, (err) => { + if (err) { + console.log(err) + } +}) -let ap = { ssid: 'your ap name', password: 'your ap password' } +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) + console.log("connected: ", resp) + // WiFiControl.resetWiFi(iface[0].adapterName, (err) => { + // if (err) { + // console.log(err) + // } + // }) } if (err) - console.log("error" + ReferenceError) + console.log("error", err) }) \ No newline at end of file From ea819e6fbe1969b5de5ddb75d64a80d72979f57a Mon Sep 17 00:00:00 2001 From: ssyang Date: Wed, 19 Feb 2020 11:53:16 +0800 Subject: [PATCH 14/22] Fix bug about deleting profile --- lib/win32.js | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/lib/win32.js b/lib/win32.js index e01dcaa..84d7046 100644 --- a/lib/win32.js +++ b/lib/win32.js @@ -186,11 +186,13 @@ this.WiFiLog("Success!"); } this.WiFiLog("Removing temporary WiFi config file..."); - return isWritable ? - this.execSync("del \".\\" + _ap.ssid + ".xml\"") - : - this.execSync("del \".\\" + `${require('os').homedir}\\${_ap.ssid}` + ".xml\""); - + setTimeout(() => { + if (isWritable) { + return this.execSync("del \".\\" + _ap.ssid + ".xml\"") + } else { + return this.execSync("del \"" + `${require('os').homedir}\\${_ap.ssid}` + ".xml\""); + } + }, 2000) }, resetWiFi: function (adapterName) { var COMMANDS, _msg, com, j, len, resetWiFiChain, results, stdout; From 1d76800eee7b2e271c8b3c254c33432a39f5dd1c Mon Sep 17 00:00:00 2001 From: ssyang Date: Wed, 19 Feb 2020 12:01:26 +0800 Subject: [PATCH 15/22] Hide unnecessary information --- test/test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test.js b/test/test.js index ed3c425..174750e 100644 --- a/test/test.js +++ b/test/test.js @@ -15,7 +15,7 @@ WiFiControl.resetWiFi(iface[0].adapterName, (err) => { } }) -let ap = { ssid: 'MediCam_DFBAD1', password: '1234567890' } +let ap = { ssid: 'your_ap_ssid', password: 'your_ap_password' } WiFiControl.connectToAP(ap, iface[0].adapterName, (err, resp) => { if (resp) { From d6eac7d176795749c6aae21b8588d3ff0a816bdc Mon Sep 17 00:00:00 2001 From: ssyang Date: Sat, 28 Mar 2020 15:03:34 +0800 Subject: [PATCH 16/22] Fix bug about deleting profile --- lib/win32.js | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/lib/win32.js b/lib/win32.js index 84d7046..9b1b440 100644 --- a/lib/win32.js +++ b/lib/win32.js @@ -152,21 +152,25 @@ xmlContent = win32WirelessProfileBuilder(ssid); } try { - fs.promises.access('./', fs.constants.W_OK).then(() => { - fs.writeFileSync(_ap.ssid + ".xml", xmlContent); - }).catch(() => { - fs.writeFileSync(`${require('os').homedir}\\${_ap.ssid}.xml`, xmlContent) - isWritable = false - }) + 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: isWritable ? "netsh " + this.WiFiControlSettings.iface + " add profile filename=\"" + _ap.ssid + ".xml\"" @@ -186,13 +190,9 @@ this.WiFiLog("Success!"); } this.WiFiLog("Removing temporary WiFi config file..."); - setTimeout(() => { - if (isWritable) { - return this.execSync("del \".\\" + _ap.ssid + ".xml\"") - } else { - return this.execSync("del \"" + `${require('os').homedir}\\${_ap.ssid}` + ".xml\""); - } - }, 2000) + + return this.execSync(isWritable ? "del \".\\" + _ap.ssid + ".xml\"" + : "del \"" + `${require('os').homedir}\\${_ap.ssid}` + ".xml\"") }, resetWiFi: function (adapterName) { var COMMANDS, _msg, com, j, len, resetWiFiChain, results, stdout; From 05494eb7e06cef855898901b1c5622090852df37 Mon Sep 17 00:00:00 2001 From: wyu Date: Mon, 13 Apr 2020 11:48:00 +0800 Subject: [PATCH 17/22] Fix for darwin, change getIfaceState to return array --- lib/darwin.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/darwin.js b/lib/darwin.js index 5745259..c0eec3b 100644 --- a/lib/darwin.js +++ b/lib/darwin.js @@ -46,6 +46,8 @@ }, 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'); @@ -65,6 +67,7 @@ break; case "SSID": interfaceState.ssid = VALUE; + adapterNumber++; } if (KEY === "SSID") { break; @@ -83,7 +86,10 @@ }; } interfaceState.power = powerStateMap[VALUE]; - return interfaceState; + if (adapterNumber >= 0) { + allInterfaces[adapterNumber] = interfaceState; + } + return allInterfaces; }, connectToAP: function(_ap) { var COMMANDS, _msg, com, connectToAPChain, error, error1, i, len, stdout; From bd7c6e8e01614baa05e9c28b30782e4cd5e688a2 Mon Sep 17 00:00:00 2001 From: ssyang Date: Mon, 20 Apr 2020 12:48:50 +0800 Subject: [PATCH 18/22] Get guid --- lib/win32.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/win32.js b/lib/win32.js index 9b1b440..ca1db6a 100644 --- a/lib/win32.js +++ b/lib/win32.js @@ -127,6 +127,9 @@ } } break; + case "GUID": + interfaceState.guid = VALUE; + break; } if (adapterNumber >= 0) { allinterface[adapterNumber] = JSON.parse(JSON.stringify(interfaceState)) From 075a745532ed48010beac723d664b9d6ddfa00bb Mon Sep 17 00:00:00 2001 From: fasmedo Date: Thu, 23 Apr 2020 17:19:13 +0800 Subject: [PATCH 19/22] WIP: Connect wi-fi with usb adapter --- lib/darwin.js | 49 ++++++++++++++++++++++++++++++++++++--------- lib/wifi-control.js | 38 +++++++++++++++++------------------ test/test.js | 38 ++++++++++++++++++----------------- 3 files changed, 79 insertions(+), 46 deletions(-) diff --git a/lib/darwin.js b/lib/darwin.js index c0eec3b..83fe4d3 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\" -A 1"; this.WiFiLog("Executing: " + findInterfaceCom); _interface = this.execSync(findInterfaceCom); if (_interface) { + let ifaceObj = { + "name": 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.name = 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,12 +74,13 @@ }; } }, - 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"); + console.log(connectionData); ref = connectionData.split('\n'); for (k = i = 0, len = ref.length; i < len; k = ++i) { ln = ref[k]; @@ -91,10 +122,10 @@ } 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 + "\""; @@ -119,7 +150,7 @@ this.WiFiLog("Success!"); } }, - resetWiFi: function() { + resetWiFi: function () { var COMMANDS, _msg, com, i, len, resetWiFiChain, results, stdout; COMMANDS = { enableAirport: "networksetup -setairportpower " + this.WiFiControlSettings.iface + " on", diff --git a/lib/wifi-control.js b/lib/wifi-control.js index e137d29..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'); @@ -13,7 +13,7 @@ debug: false, connectionTimeout: 5000 }, - execSync: function(command, options) { + execSync: function (command, options) { var results; if (options == null) { options = {}; @@ -26,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); } } } @@ -56,7 +56,7 @@ } module.exports = { - init: function(settings) { + init: function (settings) { if (settings == null) { settings = {}; } @@ -65,7 +65,7 @@ return this.findInterface(settings.iface); } }, - configure: function(settings) { + configure: function (settings) { if (settings == null) { settings = {}; } @@ -82,7 +82,7 @@ return this.findInterface(settings.iface); } }, - findInterface: function(iface) { + findInterface: function (iface) { var _msg, error, error1, interfaceResults; if (iface == null) { iface = null; @@ -112,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."; @@ -134,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); @@ -163,7 +163,7 @@ }); } }, - connectToAP: function(_ap, adpater, 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."; @@ -187,8 +187,8 @@ 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) { + 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) @@ -222,7 +222,7 @@ return; } if ((new Date() - t0) < CXT.WiFiControlSettings.connectionTimeout) { - return setTimeout(function() { + return setTimeout(function () { return check_iface(_ap, cb); }, 250); } else { @@ -244,14 +244,14 @@ }); } }, - resetWiFi: function(adpater ,cb) { + resetWiFi: function (adpater, cb) { var _msg, check_iface, error, error1, t0; try { 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; ifaceStates = _this.getIfaceState(); ifaceState = ifaceStates.find(interface => interface.adapterName === adpater) @@ -264,7 +264,7 @@ return; } if ((new Date() - t0) < CXT.WiFiControlSettings.connectionTimeout) { - return setTimeout(function() { + return setTimeout(function () { return check_iface(cb); }, 250); } else { @@ -286,7 +286,7 @@ }); } }, - getIfaceState: function() { + getIfaceState: function () { var _msg, error, error1, interfaceState; try { interfaceState = os_instructions.getIfaceState.call(CXT); diff --git a/test/test.js b/test/test.js index 174750e..b1263dc 100644 --- a/test/test.js +++ b/test/test.js @@ -9,23 +9,25 @@ WiFiControl.init({ }); let iface = WiFiControl.getIfaceState() -WiFiControl.resetWiFi(iface[0].adapterName, (err) => { - if (err) { - console.log(err) - } -}) +console.log("getInterfaceState", iface); -let ap = { ssid: 'your_ap_ssid', password: 'your_ap_password' } +// WiFiControl.resetWiFi(iface[0].adapterName, (err) => { +// if (err) { +// console.log(err) +// } +// }) -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 +// let ap = { ssid: 'your_ap_ssid', password: 'your_ap_password' } + +// 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 From 4aad88be1803a48d8c277d90f531782cec9bb465 Mon Sep 17 00:00:00 2001 From: fasmedo Date: Fri, 24 Apr 2020 11:27:12 +0800 Subject: [PATCH 20/22] Connect device with native adapter of PC --- .gitignore | 1 + lib/darwin.js | 23 ++++++++++++----------- test/test.js | 38 +++++++++++++++++++------------------- 3 files changed, 32 insertions(+), 30 deletions(-) diff --git a/.gitignore b/.gitignore index b9c1ee5..68cb4b8 100644 --- a/.gitignore +++ b/.gitignore @@ -30,3 +30,4 @@ package-lock.json package.json test/* +.vs* diff --git a/lib/darwin.js b/lib/darwin.js index 83fe4d3..3ce04b1 100644 --- a/lib/darwin.js +++ b/lib/darwin.js @@ -22,7 +22,7 @@ autoFindInterface: function () { var _iface, _interface, _msg, findInterfaceCom; this.WiFiLog("Host machine is MacOS."); - findInterfaceCom = "networksetup -listallhardwareports | grep \"Wi-Fi\" -A 1"; + findInterfaceCom = "networksetup -listallhardwareports | grep \"Wi-Fi\\|WLAN\\|wi-fi\\|wlan\\|Wlan\" -A 1"; this.WiFiLog("Executing: " + findInterfaceCom); _interface = this.execSync(findInterfaceCom); if (_interface) { @@ -35,8 +35,8 @@ _iface = _iface.replace("--", ""); } _iface = _iface.split('\n'); - _iface = _iface.filter((e) => { - return e != ""; + _iface = _iface.filter((e) => { + return e != ""; }); let ifaceArr = []; let ifaceNum = 0; @@ -80,8 +80,9 @@ let adapterNumber = -1; interfaceState = {}; connectionData = this.execSync(AirPortBinary + " -I"); - console.log(connectionData); ref = connectionData.split('\n'); + let ifaceName = this.WiFiControlSettings.iface.find(interface => interface.driver === "Wi-Fi") + interfaceState.adapterName = ifaceName.name; for (k = i = 0, len = ref.length; i < len; k = ++i) { ln = ref[k]; try { @@ -95,16 +96,16 @@ switch (KEY) { case "state": interfaceState.connection = connectionStateMap[VALUE]; + adapterNumber++; break; case "SSID": interfaceState.ssid = VALUE; - adapterNumber++; } if (KEY === "SSID") { break; } } - powerData = this.execSync("networksetup -getairportpower " + this.WiFiControlSettings.iface); + powerData = this.execSync("networksetup -getairportpower " + ifaceName.name); try { parsedLine = parsePatterns.airport_line.exec(powerData.trim()); KEY = parsedLine[1]; @@ -113,12 +114,12 @@ 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]; if (adapterNumber >= 0) { - allInterfaces[adapterNumber] = interfaceState; + allInterfaces[adapterNumber] = JSON.parse(JSON.stringify(interfaceState)); } return allInterfaces; }, @@ -150,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/test/test.js b/test/test.js index b1263dc..33ca554 100644 --- a/test/test.js +++ b/test/test.js @@ -5,29 +5,29 @@ const sleep = (millis) => new Promise(resolve => setTimeout(resolve, millis)); WiFiControl.init({ debug: true, - connectionTimeout: 2000 + connectionTimeout: 7000 }); let iface = WiFiControl.getIfaceState() console.log("getInterfaceState", iface); -// WiFiControl.resetWiFi(iface[0].adapterName, (err) => { -// if (err) { -// console.log(err) -// } -// }) +WiFiControl.resetWiFi(iface[0].adapterName, (err) => { + if (err) { + console.log(err) + } +}) -// let ap = { ssid: 'your_ap_ssid', password: 'your_ap_password' } +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 +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 From d98df0e22f663700335d416d0d9d784202da5747 Mon Sep 17 00:00:00 2001 From: fasmedo Date: Fri, 24 Apr 2020 13:40:45 +0800 Subject: [PATCH 21/22] Fix typo error --- lib/darwin.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/darwin.js b/lib/darwin.js index 3ce04b1..730c0dd 100644 --- a/lib/darwin.js +++ b/lib/darwin.js @@ -27,7 +27,7 @@ _interface = this.execSync(findInterfaceCom); if (_interface) { let ifaceObj = { - "name": null, + "adapterName": null, "driver": null } _iface = _interface.trim(); @@ -49,7 +49,7 @@ ifaceObj.driver = VALUE; break; case "Device": - ifaceObj.name = VALUE; + ifaceObj.adapterName = VALUE; ifaceArr[ifaceNum] = JSON.parse(JSON.stringify(ifaceObj)); ifaceNum++; break; From dc0c29b2884fd1758db461eedc8bbbc94563f172 Mon Sep 17 00:00:00 2001 From: fasmedo Date: Fri, 24 Apr 2020 13:56:15 +0800 Subject: [PATCH 22/22] Fix typo error --- lib/darwin.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/darwin.js b/lib/darwin.js index 730c0dd..425e9ea 100644 --- a/lib/darwin.js +++ b/lib/darwin.js @@ -82,7 +82,7 @@ connectionData = this.execSync(AirPortBinary + " -I"); ref = connectionData.split('\n'); let ifaceName = this.WiFiControlSettings.iface.find(interface => interface.driver === "Wi-Fi") - interfaceState.adapterName = ifaceName.name; + interfaceState.adapterName = ifaceName.adapterName; for (k = i = 0, len = ref.length; i < len; k = ++i) { ln = ref[k]; try { @@ -105,7 +105,7 @@ break; } } - powerData = this.execSync("networksetup -getairportpower " + ifaceName.name); + powerData = this.execSync("networksetup -getairportpower " + ifaceName.adapterName); try { parsedLine = parsePatterns.airport_line.exec(powerData.trim()); KEY = parsedLine[1];