forked from colin273/enmity-debug
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
281 lines (268 loc) · 8.15 KB
/
index.js
File metadata and controls
281 lines (268 loc) · 8.15 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
// Suppress Node.js warning about experimental fetch API
// Ref: https://github.com/nodejs/node/issues/30810#issuecomment-1383184769
let WebSocketServer, colors;
try {
WebSocketServer = (await import("ws")).WebSocketServer;
colors = (await import("ansi-colors")).default;
} catch (err) {
console.error(
"You forgot to install the dependencies.\n" +
"https://github.com/meqativ/vendetta-debug#installing"
);
process.exit(1);
}
import { hostname } from "node:os";
import repl from "node:repl";
import { parseArgs } from "node:util";
import * as fs from "node:fs/promises";
let defaults = {};
try {
defaults = JSON.parse(await fs.readFile("./defaults.json"))
} catch (e) {
console.error(
"Invalid defaults.json"
)
console.error(e)
process.exit(1)
}
let isPrompting = false;
const args = parseArgs({
options: {
h: {
type: "boolean",
},
help: {
type: "boolean",
},
silent: {
type: "string",
default: `${defaults?.silent ?? "0"}`,
},
port: {
type: "string",
default: `${defaults?.port ?? "9090"}`,
},
onConnectedPath: {
type: "string",
default: defaults?.onConnectedPath,
},
client: {
type: "string",
default: defaults?.client ?? "Vendetta",
},
clientName: {
type: "string",
default: defaults?.clientName ?? "Vendetta",
},
clientColor: {
type: "string",
default: defaults?.clientColor ?? "cyan",
},
noStyle: {
type: "boolean",
default: defaults?.noStyle ?? false,
},
},
});
if (args.values.noStyle) colors.enabled = false;
if (args?.values.help || args?.values?.h) {
let cmdlu = (await import("command-line-usage")).default;
const { generate } = await import("./help.js");
console.log(generate(cmdlu));
process.exit(0);
}
// parse client,, stuff; TODO: decide whether COLORS below args parsing part looks better
let client = args.values.client;
const supportedClients = ["enmity", "vendetta", "none"];
if (!supportedClients.includes(client.toLowerCase())) {
throw new Error(
`The option "client" has a unsupported client. It should be: ${
supportedClients.length === 2
? supportedClients.join(" or ")
: supportedClients.slice(0, -1).join(", ") +
" or " +
supportedClients.slice(-1)
}`
);
}
let clientColor;
if (
args.values.clientColor !== "none" &&
!Object.values(colors.keys)
.filter((prop) => Array.isArray(prop))
.flat()
.includes(args.values.clientColor)
) {
throw new Error(`The option "clientColor" has an invalid color`);
} else {
if (client.toLowerCase() === "vendetta") clientColor = "cyan";
if (client.toLowerCase() === "enmity") clientColor = "blue";
clientColor = args.values.clientColor;
}
let clientName;
if (client.toLowerCase() === "vendetta") clientName = "Vendetta";
if (client.toLowerCase() === "enmity") clientName = "Enmity";
clientName = args.values.clientName;
const COLORS = {
client: {
info: clientColor === "none" ? (t) => t : colors[clientColor],
warning: colors.yellow,
error: colors.red,
},
debugger: {
info: colors.magenta.bold,
warning: colors.yellow.bold,
error: colors.red.bold,
},
};
// Parse arguments
const silentLvl = Number(args?.values?.silent ?? 0);
if (Number.isNaN(silentLvl))
throw new Error('The option "silent" should be a number.');
if (silentLvl > 2 || silentLvl < 0)
throw new Error('The option "silent" should in range 0-2.');
const wssPort = Number(args?.values?.port ?? 9090);
if (Number.isNaN(wssPort))
throw new Error('The option "port" should be a number.');
const onConnectedPath = args?.values?.onConnectedPath;
let onConnectedCode = undefined;
if (typeof onConnectedPath !== "undefined") {
await fs.access(onConnectedPath, fs.constants.R_OK).catch((e) => {
console.log(`The path in "onConnectedPath" is not accessible`);
console.error(e);
process.exit(e.errno);
});
onConnectedCode = await fs.readFile(onConnectedPath, "utf-8");
if (onConnectedCode === "")
debuggerWarning(`The file in "onConnectedPath" is empty`);
}
// Utility functions for more visually pleasing logs
// Get out of user input area first if prompt is currently being shown
function colorise(message, source, color) {
return color(`[${source}] `) + message;
}
function safeLog(data) {
console.log((isPrompting ? "\n" : "") + data);
}
function discordColorise(data) {
let { message, level } = JSON.parse(data);
// Normal logs don't need extra colorization
switch (level) {
case 0: // Info
message = COLORS.client.info(message);
break;
case 2: // Warning
message = COLORS.client.warning(message);
break;
case 3: // Error
message = COLORS.client.error(message);
break;
}
return colorise(message, clientName, COLORS.client.info);
}
function discordLog(message) {
return safeLog(silentLvl === 2 ? message : discordColorise(message));
}
function debuggerColorise(message) {
return colorise(message, "Debugger", COLORS.debugger.info);
}
function debuggerLog(message) {
safeLog(silentLvl === 2 ? message : debuggerColorise(message));
}
function debuggerWarning(message) {
safeLog(colorise(message, "Debugger", COLORS.debugger.warning));
}
function debuggerError(error, isReturning) {
safeLog(colorise("Error", "Debugger", COLORS.debugger.error));
if (isReturning) {
return error;
}
console.error(error);
}
// Display welcome message and basic instructions
if (silentLvl < 1)
console.log(
colors.bold(
`👉 Welcome to the debugger. (${client}; ${COLORS.client.info(
clientName
)})\n`
) +
"Press Ctrl+C to exit.\n" +
"How to connect to the debugger: https://github.com/meqativ/vendetta-debug/blob/master/README.md#connecting"
);
// Create websocket server and REPL, and wait for connection
const wss = new WebSocketServer({ port: wssPort });
wss.on("listening", (ws) => {
if (silentLvl < 2)
debuggerLog(`Listening for connections on port ${wss.address().port}`);
});
wss.on("connection", (ws) => {
if (silentLvl < 2)
debuggerLog(
`Connected to ${client} over websocket, starting debug session`
);
console.log("meow?")
isPrompting = false; // REPL hasn't been created yet
let finishCallback;
// Handle logs returned from Discord client via the websocket
ws.on("message", (data) => {
try {
if (finishCallback) {
finishCallback(null, data);
finishCallback = undefined;
} else {
discordLog(data);
}
} catch (e) {
debuggerError(e, false);
}
isPrompting = true;
rl.displayPrompt();
});
// Create the REPL
console.log(Object.getOwnPropertyNames(repl))
const rl = repl.start({
eval: (inputRaw, ctx, filename, cb) => {
try {
if (!inputRaw.trim()) {
cb();
} else {
isPrompting = false;
let input = inputRaw;
if (client.toLowerCase() === "vendetta") {
input = `const res=(0, eval)(${JSON.stringify(
inputRaw
)});let out=vendetta.metro.findByProps("inspect").inspect(res,{showHidden:true});if(out!=="undefined")console.log(out);res`;
} else if (client.toLowerCase() === "enmity") {
input = `const res=(0, eval)(${JSON.stringify(
inputRaw
)});console.log(enmity.modules.getByProps("inspect").inspect(res,{showHidden:true}));if(res!=="undefined")console.log(res);res`;
}
ws.send(input);
finishCallback = cb;
}
} catch (e) {
cb(e);
}
},
writer: (data) => {
return data instanceof Error
? debuggerError(data, true)
: discordColorise(data);
},
});
rl.setupHistory("~/.config/vendetta-debug/.replHistory", (err, repl) => {
if (err) return debuggerError(err);
if (silentLvl < 2) debuggerLog("Prompt history loaded");
});
isPrompting = true; // Now the REPL exists and is prompting the user for input
rl.on("close", () => {
if (silentLvl < 2) debuggerLog("Closing debugger, press Ctrl+C to exit");
});
ws.on("close", () => {
if (silentLvl < 2) debuggerLog("Websocket has been closed");
isPrompting = false;
rl.close();
});
if (onConnectedCode) ws.send(onConnectedCode);
});