forked from abandonware/wireless-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ap.js
66 lines (58 loc) · 1.93 KB
/
ap.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
63
64
65
66
import {Shell as _Shell} from 'xeue-shell';
export default class AP {
constructor(Logs, sudo = false) {
this.Logs = Logs;
this.Shell = new _Shell(this.Logs, 'NETWRK', 'W', 'bash');
this.sudo = sudo ? 'sudo ' : '';
}
async enable(options) {
const file = options.interface + '-hostapd.conf';
const commands = ['cat <<EOF >' + file + ' && hostapd -B ' + file + ' && rm -f ' + file];
Object.getOwnPropertyNames(options).forEach(key => {
commands.push(key + '=' + options[key]);
});
const {stdout} = await this.Shell.run(this.sudo+commands.join('\n'), false);
return stdout[0];
}
async disable(iface) {
const file = iface + '-hostapd.conf';
const {stdout} = await this.Shell.run(this.sudo+'kill `pgrep -f "^hostapd -B ' + file + '"` || true', false);
return stdout[0];
}
async startDHCP(options) {
const file = options.interface + '-udhcpd.conf';
const commands = [].concat(
'cat <<EOF >' + file + ' && udhcpd ' + file + ' && rm -f ' + file,
this.#expand(options)
);
const {stdout} = await this.Shell.run(this.sudo+commands.join('\n'), false);
return stdout[0];
}
async stopDHCP(iface) {
const file = iface + '-udhcpd.conf';
const {stdout} = this.Shell.run(this.sudo+'kill `pgrep -f "^udhcpd ' + file + '"` || true', false);
return stdout[0];
}
#expand_r(options, lines, prefix) {
Object.getOwnPropertyNames(options).forEach(function(key) {
let full = prefix.concat(key);
const value = options[key];
if (Array.isArray(value)) {
value.forEach(function(val) {
lines.push(full.concat(val).join(' '));
});
}
else if (typeof(value) == 'object') {
this.#expand_r(value, lines, full);
}
else {
lines.push(full.concat(value).join(' '));
}
});
}
#expand(options) {
const lines = [];
this.#expand_r(options, lines, []);
return lines;
}
}