smart-monopoly/src/OSHandler.ts
2024-03-29 01:29:30 +01:00

168 lines
5.2 KiB
TypeScript

import {WiFiNetwork} from "./IPCConstants";
import {spawn, exec} from 'node:child_process';
import * as dns from "dns";
import * as process from "process";
import * as path from "path";
const wifiScan = require("node-wifi-scanner");
export default class OSHandler {
static getKnownWifis(): Promise<WiFiNetwork[]> {
return new Promise<WiFiNetwork[]>((resolve, reject) => {
exec("sudo touch /etc/wpa_supplicant/wpa_supplicant-monopoly.conf && sudo cat /etc/wpa_supplicant/wpa_supplicant-monopoly.conf", (err, stdout) => {
if(err)
return reject(err);
let lines = stdout.split("\n");
let wifis: WiFiNetwork[] = [];
let inBlock = false;
let currentNetwork: WiFiNetwork = {ssid: "", psk: "", isSecured: false};
for(let line of lines)
{
if(line.includes("network={"))
inBlock = true;
if(line.includes("}")) {
inBlock = false;
currentNetwork.isSecured = !!currentNetwork.psk;
if(currentNetwork.ssid)
wifis.push(currentNetwork);
currentNetwork.ssid = "";
currentNetwork.psk = "";
currentNetwork.isSecured = false;
}
if(inBlock && line.includes("ssid"))
currentNetwork.ssid = line.substring(line.indexOf('"')+1, line.lastIndexOf('"'));
if(inBlock && line.includes("psk"))
currentNetwork.psk = line.substring(line.indexOf('"')+1, line.lastIndexOf('"'));
}
resolve(wifis);
})
});
}
static addWifi(wifi: string, passkey: string | null) {
return new Promise<void>((resolve, reject) => {
let p = path.resolve(process.cwd(), "/scripts/addWifi.sh");
exec(p + ` -s "${wifi}" -p "${passkey}"`, (err, stdout) => {
if(err)
return reject(err);
if(stdout == "ok")
resolve();
else
reject("no-return");
})
});
}
static connectToWifi(ssid: string)
{
return new Promise<boolean>((resolve, reject) => {
let p = path.resolve(process.cwd(), "/scripts/connectToWifi.sh");
exec(p + ``)
});
}
static disableAllWifis() {
return new Promise<void>((resolve, reject) => {
});
}
static scanWifis() {
return new Promise<WiFiNetwork[]>((resolve, reject) => {
wifiScan.scan(true).then((result: {
ssid: string,
mac: string,
channel: number,
rssi: number,
encrypted: boolean
}[]) => {
if (!result)
return {status: false};
let networks: WiFiNetwork[] = [];
for (let wifi of result) {
networks.push({
ssid: wifi.ssid,
isKnown: true,
isSecured: wifi.encrypted,
rssi: wifi.rssi
})
}
// Sort best rssi to top
networks.sort((a, b) => {
return b.rssi - a.rssi;
});
// Sort best rssi to top
networks.sort((a, b) => {
if (b.isSecured && !a.isSecured)
return 1;
else if (b.isSecured == a.isSecured)
return 0;
else
return -1;
});
console.log(networks);
networks = networks.filter((ele, index) => {
// If empty ssid
if (!ele.ssid || ele.ssid == "")
return false;
// Remove duplicates
let i = 0;
for (let x of networks) {
if (x.ssid == ele.ssid) {
return i == index;
}
i++;
}
return true;
});
resolve(networks);
}).catch(reject);
})
}
static checkForSudo() {
return new Promise<boolean>((resolve) => {
const test = spawn("sudo", ["-v"], {detached: true, stdio: ["ignore", "pipe", "pipe"]});
test.on("close", (code: number) => {
if (code != 0)
resolve(false);
else
resolve(true);
});
test.on("error", (err) => {
resolve(false);
})
})
}
static checkForInternet() {
return new Promise<boolean>((resolve) => {
dns.lookup("google.de", 4, (err, address, family) => {
if (err)
resolve(false);
else
resolve(true);
})
});
}
}