-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
198 lines (171 loc) · 4.75 KB
/
index.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
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
const {
Client,
GatewayIntentBits,
Events,
SlashCommandBuilder,
REST,
Routes,
EmbedBuilder,
} = require("discord.js");
const fs = require("fs").promises;
const { spawn } = require("child_process");
const path = require("path");
const crypto = require("crypto");
const client = new Client({
intents: [GatewayIntentBits.Guilds],
});
const inviteLink = `https://discord.com/oauth2/authorize?client_id=${process.env.CLIENT_ID}`;
async function executeCode(userCode, options = {}) {
const {
maxTime = 5000,
maxMemory = 20 * 1024 * 1024,
maxStorage = 5 * 1024 * 1024,
} = options;
const tempDir = path.join(
"/tmp",
`bun-run-${crypto.randomBytes(16).toString("hex")}`
);
try {
await fs.mkdir(tempDir, { mode: 0o700 });
const scriptPath = path.join(tempDir, "script.js");
await fs.writeFile(scriptPath, userCode, { mode: 0o400 });
const dockerCommand = [
"docker",
"run",
"--rm",
"--network",
"none",
"--security-opt",
"no-new-privileges",
"--cap-drop",
"ALL",
"--read-only",
"--memory",
"10m",
"--memory-swap",
"20m",
"--user",
"1000:1000",
"--cpus",
"0.5",
"--ulimit",
"nofile=64:64",
"--ulimit",
"nproc=64:64",
"--tmpfs",
`/tmp:rw,size=${maxStorage},noexec,nosuid`,
"-v",
`${tempDir}:/app:ro`,
"oven/bun:alpine",
"/bin/sh",
"-c",
`bun run /app/script.js`,
];
return await new Promise((resolve, reject) => {
const process = spawn(dockerCommand[0], dockerCommand.slice(1), {
stdio: ["ignore", "pipe", "pipe"],
});
const timeoutId = setTimeout(() => {
process.kill("SIGKILL");
reject(new Error("Execution timed out"));
}, maxTime);
let output = "";
let errorOutput = "";
process.stdout.on("data", (data) => {
output += data.toString();
});
process.stderr.on("data", (data) => {
errorOutput += data.toString();
});
process.on("close", (code) => {
clearTimeout(timeoutId);
if (code === 0) {
resolve(output.trim());
} else {
reject(new Error(`Execution failed: ${errorOutput}`));
}
});
process.on("error", (err) => {
clearTimeout(timeoutId);
reject(err);
});
});
} catch (error) {
throw new Error(error.message);
} finally {
fs.rm(tempDir, { recursive: true, force: true }).catch(console.error);
}
}
const command = new SlashCommandBuilder()
.setName("bun")
.setDescription("Run code using Bun from a text input")
.addStringOption((option) =>
option.setName("code").setDescription("Code to execute").setRequired(true)
)
.toJSON();
client.once(Events.ClientReady, async () => {
console.log(`Logged in as ${client.user.tag}\nInvite link: ${inviteLink}`);
const rest = new REST({ version: "10" }).setToken(process.env.BOT_TOKEN);
try {
await rest.put(Routes.applicationCommands(process.env.CLIENT_ID), {
body: [command],
});
} catch (error) {
console.error("Command registration failed:", error);
}
});
client.on(Events.InteractionCreate, async (interaction) => {
if (!interaction.isChatInputCommand()) return;
if (interaction.commandName !== "bun") {
return;
}
if (interaction.user.bot) {
await interaction.reply({
content: "❌ Bots are not allowed to use this command",
});
return;
}
if (!process.env.WHITELIST.split(",").includes(interaction.user.id)) {
await interaction.reply({
content:
"❌ You are not whitelisted to use this command.\nPlease request permission by contacting me: https://tiagorangel.com/contact",
});
return;
}
let code = interaction.options.getString("code");
let output, success;
try {
output = (await executeCode(code)).toString();
success = true;
} catch (error) {
output = error?.message || error;
success = false;
console.log(output);
}
if (output.length > 1000) {
output = output.slice(0, 1000) + "...";
}
if (code.length > 700) {
code = code.slice(0, 1000) + "...";
}
try {
const exampleEmbed = new EmbedBuilder()
.setColor(success ? 0x57f287 : 0xed4245)
.setTitle(success ? "✅ Code executed" : "❌ Code failed")
.addFields({ name: "Code", value: `\`\`\`javascript\n${code}\n\`\`\`` })
.addFields({
name: success ? "Output" : "Error",
value: `\`\`\`\n${output || " "}\n\`\`\``,
});
await interaction.reply({
embeds: [exampleEmbed],
content: ``,
});
} catch {
await interaction.reply({
content: "❌ An error occured while executing the code",
});
return;
}
});
client.login(process.env.BOT_TOKEN);