Compare commits
8 Commits
c16a65dbcd
...
master
Author | SHA1 | Date | |
---|---|---|---|
e5f9390ba1 | |||
c5cbde1a3c | |||
e69c735d64 | |||
40ab464995 | |||
0672168541 | |||
4b379b106a | |||
1f72f00d2d | |||
a5ad7ea8a5 |
52
index.js
52
index.js
@ -1,18 +1,20 @@
|
|||||||
/**
|
/**
|
||||||
* @typedef {Object} WifiNetwork
|
* @typedef {Object} WifiNetwork
|
||||||
* @property {string} ssid - SSID des WLAN-Netzwerks
|
* @property {string} ssid - SSID of the wifi
|
||||||
* @property {string} mac - MAC-Adresse des WLAN-Netzwerks
|
* @property {string} mac - MAC-Address of the wifi
|
||||||
* @property {number} channel - Kanal des WLAN-Netzwerks
|
* @property {number} channel - Channel of the wifi
|
||||||
* @property {number} rssi - Signalstärke des WLAN-Netzwerks (RSSI)
|
* @property {number} rssi - Signalstrength of Wifi-Network (RSSI)
|
||||||
|
* @property {boolean} encrypted - Encrypted
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* node-wifi-scanner
|
* node-wifi-scanner
|
||||||
* Created by kc on 04.04.16.
|
* Created by kc on 04.04.16. - Forked and updated by Tobias Hopp 27.03.2024
|
||||||
*/
|
*/
|
||||||
|
|
||||||
const exec = require('child_process').exec;
|
const {exec, spawn} = require('child_process');
|
||||||
|
|
||||||
const async = require('async');
|
const async = require('async');
|
||||||
const _ = require('lodash');
|
const _ = require('lodash');
|
||||||
// The tools
|
// The tools
|
||||||
@ -70,18 +72,42 @@ function initTools(callback) {
|
|||||||
* @param {boolean} useSudo - Use sudo for access?
|
* @param {boolean} useSudo - Use sudo for access?
|
||||||
*/
|
*/
|
||||||
function scanNetworks(callback, useSudo) {
|
function scanNetworks(callback, useSudo) {
|
||||||
exec((useSudo ? "sudo ":"") + scanner.cmdLine, function (err, stdout) {
|
const cmd = scanner.cmdLine.split(" ");
|
||||||
|
let args = cmd.slice(1);
|
||||||
|
if(useSudo)
|
||||||
|
args.unshift(cmd[0]);
|
||||||
|
const child = spawn(useSudo ? "sudo" : cmd[0], args, { stdio: ['ignore', 'pipe', 'pipe'], detached: true });
|
||||||
|
|
||||||
|
child.on("error", (err) => callback(err, null) );
|
||||||
|
|
||||||
|
let stdoutData = '';
|
||||||
|
child.stdout.on('data', (data) => {
|
||||||
|
stdoutData += data;
|
||||||
|
});
|
||||||
|
|
||||||
|
child.on('close', (code) => {
|
||||||
|
if (code === 0) {
|
||||||
|
// Prozess erfolgreich beendet, Ausgabe verarbeiten
|
||||||
|
scanner.parseOutput(stdoutData, callback);
|
||||||
|
} else {
|
||||||
|
// Prozess mit Fehler beendet
|
||||||
|
callback(new Error(`Exited with code ${code}`), null);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
/*exec((useSudo ? "sudo ":"") + scanner.cmdLine, { shell: true }, function (err, stdout) {
|
||||||
if (err) {
|
if (err) {
|
||||||
callback(err, null);
|
callback(err, null);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
scanner.parseOutput(stdout, callback);
|
scanner.parseOutput(stdout, callback);
|
||||||
});
|
});*/
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
/**
|
/**
|
||||||
* Funktion zum Scannen von WLAN-Netzwerken
|
* Scan for wifi networks
|
||||||
* @param {boolean} useSudo? - Defaults to false | Should sudo be used to get the output?
|
* @param {boolean} useSudo? - Defaults to false | Should sudo be used to get the output?
|
||||||
* @return {Promise<WifiNetwork[]|null>} WiFinetwork Array or null
|
* @return {Promise<WifiNetwork[]|null>} WiFinetwork Array or null
|
||||||
* @rejects Returns error on reject
|
* @rejects Returns error on reject
|
||||||
@ -97,8 +123,10 @@ module.exports = {
|
|||||||
scanNetworks((err, result) => {
|
scanNetworks((err, result) => {
|
||||||
if(err)
|
if(err)
|
||||||
return reject(err);
|
return reject(err);
|
||||||
|
if(!result)
|
||||||
|
result = [];
|
||||||
resolve(result);
|
resolve(result);
|
||||||
}, useSudo);
|
}, useSudo && scanner == iwlist);
|
||||||
|
|
||||||
});
|
});
|
||||||
return;
|
return;
|
||||||
@ -106,8 +134,10 @@ module.exports = {
|
|||||||
scanNetworks((err, result) => {
|
scanNetworks((err, result) => {
|
||||||
if(err)
|
if(err)
|
||||||
return reject(err);
|
return reject(err);
|
||||||
|
if(!result)
|
||||||
|
result = [];
|
||||||
resolve(result);
|
resolve(result);
|
||||||
}, useSudo);
|
}, useSudo && scanner == iwlist);
|
||||||
} );
|
} );
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
Reference in New Issue
Block a user