-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.mjs
51 lines (43 loc) · 1.15 KB
/
utils.mjs
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
export function parseFlipperFile(contents) {
return new Map(
contents
.split("\n")
.filter((l) => l.length)
.map((l) => l.split(": "))
);
}
export async function readUntil(port, separator) {
return new Promise((resolve) => {
const sepSequence = Buffer.from(separator, "ascii").toString("hex");
let buf = "";
const waitForPrompt = (chunk) => {
buf += chunk.toString("hex");
if (buf.indexOf(sepSequence) > -1) {
port.removeListener("data", waitForPrompt);
resolve(buf);
}
};
port.on("data", waitForPrompt);
});
}
export async function openCli(port) {
return new Promise((resolve) => {
port.on("open", () => {
console.log("🔥 Whoa, we're in the CLI. Executing payload...");
let buf = "";
const waitForPrompt = (chunk) => {
buf += chunk.toString("ascii");
if (buf.indexOf(">:") > -1) {
port.removeListener("data", waitForPrompt);
resolve();
}
};
port.on("data", waitForPrompt);
});
port.on("error", () => {
if (!port.isOpen) {
setTimeout(() => port.open(), 10);
}
});
});
}