generated from vendetta-mod/plugin-template
-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathws-server.mjs
89 lines (83 loc) · 2.74 KB
/
ws-server.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import { spawn } from "child_process";
import readline from "readline";
import { WebSocketServer } from "ws";
import chalk from "chalk";
const PORT = 9090
let connected = false;
const logUtils = {
incoming: (message) => {
logUtils.info(chalk.greenBright("<-- ") + message);
},
info: (message) => {
logUtils.clearLine();
console.info(message);
if (connected) process.stdout.write(chalk.cyanBright("--> "));
},
success: (message) => {
logUtils.clearLine();
console.info(chalk.greenBright(message));
},
error: (message) => {
logUtils.clearLine();
console.error(chalk.redBright(message));
},
clearLine: () => {
process.stdout.clearLine();
process.stdout.cursorTo(0);
}
};
const wss = new WebSocketServer({ port: PORT });
wss.on("connection", async (ws) => {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
ws.send("(()=>{window.vd=window.vendetta})()")
ws.on("message", data => {
const parsed = JSON.parse(data.toString());
switch (parsed.level) {
case 0:
logUtils.info(chalk.bold(parsed.message));
break;
case 1:
logUtils.info(chalk.greenBright(parsed.message));
break;
case 2:
logUtils.info(chalk.yellow(parsed.message));
break;
case 3:
logUtils.info(chalk.redBright(parsed.message));
break;
}
});
ws.on("close", () => {
connected = false;
rl.close();
logUtils.error("Websocket connection closed, waiting for reconnection");
});
logUtils.incoming("Discord client connected to websocket");
connected = true;
while (connected) {
await new Promise(r => {
rl.question(chalk.cyanBright("--> "), (cmd) => {
if (!connected) return;
else if (["exit", "quit"].includes(cmd)) {
ws.close();
process.exit();
} else if (cmd == "clear") {
console.clear();
process.stdout.write(chalk.cyanBright("--> "));
} else if (/^\s*$/.test(cmd)) {
r();
} else {
ws.send(cmd);
r();
}
});
});
}
});
spawn("adb", ["reverse", `tcp:${PORT}`, `tcp:${PORT}`], { stdio: "ignore" }).on("exit", (code) => {
if (code !== 0) logUtils.error(`Port forwarding port ${PORT} with adb exited with code ${code}, aliucord may not load`);
else logUtils.success(`Successfully forwarded port ${PORT} to phone with adb`);
});