-
Notifications
You must be signed in to change notification settings - Fork 0
/
wifi.js
62 lines (52 loc) · 1.48 KB
/
wifi.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import fs from "fs";
import { bashCmd, reboot } from "./utils.js";
export const wifiSettings = {
path: "/etc/wpa_supplicant/wpa_supplicant.conf",
parseConfigContent: (content) => {
try {
// Define the regex pattern to match the wifi configuration
// The pattern looks for "ssid" and "psk" keys followed by "=" and captures their respective values
const regex = /ssid="([^"]+)"\s+psk="([^"]+)"/g;
let match;
const configs = [];
// Use a while loop to find all matches in the input text
while ((match = regex.exec(content)) !== null) {
// Push the captured groups into the configs array with the desired object structure
configs.push({ ssid: match[1], psk: match[2] });
}
return configs;
} catch (e) {
console.log(e);
return [];
}
},
toConfigContent: (config) => {
return `ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
country=NO
${config
.map(
(c) => `network={
ssid="${c.ssid}"
psk="${c.psk}"
}
`
)
.join("")}`;
},
get() {
const content = fs.readFileSync(this.path, "utf-8");
return this.parseConfigContent(content);
},
set(config) {
const content = this.toConfigContent(
Array.isArray(config) ? config : [config]
);
fs.writeFileSync(this.path, content);
setTimeout(reboot, 3000);
},
};
// Ensure file access for wifi settings
if (process.env.NODE_ENV !== "test") {
bashCmd(`sudo chmod 666 ${wifiSettings.path}`);
}