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 { return new Promise((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((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((resolve, reject) => { let p = path.resolve(process.cwd(), "/scripts/connectToWifi.sh"); exec(p + ``) }); } static disableAllWifis() { return new Promise((resolve, reject) => { }); } static scanWifis() { return new Promise((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((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((resolve) => { dns.lookup("google.de", 4, (err, address, family) => { if (err) resolve(false); else resolve(true); }) }); } }