-
Notifications
You must be signed in to change notification settings - Fork 1
/
commands.js
240 lines (221 loc) · 7.37 KB
/
commands.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
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
const Docker = require("dockerode");
const docker = new Docker();
const Transfer = require("./transfer");
const fs = require("fs");
const stripAnsi = require("strip-ansi");
const associations = require("./associations");
const limits = require('./limits')
const embed = (name, content) => {
return {
embed: {
color: 0x36393f,
author: {
name: name
},
description: content
}
};
};
const displayError = (content) => {
return embed("Error", content.message);
};
module.exports.create = async (channel, db, associationKey) => {
if (db.get("containers").find({ channel: channel.id }).value()) {
channel.send(
embed(
"Already Exists",
"A container already exists for this channel. You can run commands in it with `$ <command>`, or remove it with `$ remove`."
)
);
return;
}
let image = associations['linux'];
if (associationKey && associations[associationKey]) {
image = associations[associationKey];
}
const progress = await channel.send(embed("Creating...", "A container for this channel is being created with image `" + image + "`."));
docker.createContainer(
{
Image: image,
Tty: true,
Cmd: [ "/bin/sh" ],
...limits
},
async (error, container) => {
if (error) {
await progress.edit(displayError(error));
return;
}
container.start(async (error) => {
if (error) {
await progress.edit(displayError(error));
return;
}
db
.get("containers")
.push({
channel: channel.id,
id: container.id
})
.write();
await progress.edit(
embed(
"Done",
[
"Successfully created a container for this channel with image `" + image + "`.",
"You can run commands in it with `$ <command>`, or remove it with `$ remove`."
].join(' ')
)
);
});
}
);
};
module.exports.remove = async (channel, db) => {
const containerInDb = db.get("containers").find({ channel: channel.id }).value();
if (!containerInDb) {
await channel.send(embed("Not Found", "There isn't a container for this channel. You can create one with `$ create`."));
return;
}
const container = docker.getContainer(containerInDb.id);
let progress = await channel.send(embed("Stopping...", "This channel's container is being stopped."));
container.stop(async (error) => {
let knownAsDeleted = false;
if (error && error.statusCode !== 304) {
await progress.edit(displayError(error));
return;
} else {
knownAsDeleted = true;
progress = await progress.edit(
embed(
"Error",
"It looks like there used to be a container for this channel but it was deleted. You can create another one with `$ create`."
)
);
}
progress = await progress.edit(embed("Removing...", "This channel's container is being removed."));
container.remove(async (error) => {
if (error && error.statusCode !== 404) {
await progress.edit(displayError(error));
return;
} else if (!knownAsDeleted) {
progress = await progress.edit(
embed(
"Error",
"It looks like there used to be a container for this channel but it was deleted. You can create another one with `$ create`."
)
);
}
db.get("containers").remove({ channel: channel.id }).write();
await progress.edit(
embed("Done", "Successfully removed this channel's container. You can create another one with `$ create`.")
);
});
});
};
module.exports.run = async (channel, command, db) => {
const containerInDb = db.get("containers").find({ channel: channel.id }).value();
if (!containerInDb) {
await channel.send(embed("Not Found", "There isn't a container for this channel. You can create one with `$ create`."));
return;
}
const container = docker.getContainer(containerInDb.id);
if (!container) {
await channel.send(
embed(
"Error",
"It looks like there used to be a container for this channel but it was deleted. You can create another one with `$ create`."
)
);
return;
}
let progress = await channel.send(embed("Running...", "Your command is being run in this channel's container."));
let output = "";
const options = {
Cmd: [ "sh", "-c", command ],
AttachStdout: true,
AttachStderr: true
};
container.exec(options, async (error, exec) => {
if (error) {
await progress.edit(displayError(error));
return;
}
exec.start(async (error, stream) => {
if (error) {
await progress.edit(displayError(error));
return;
}
stream.on("data", (chunk) => {
output += stripAnsi(chunk.toString()).replace(/[\u0000-\u0008,\u000E-\u001F,\u0082-\u008D,\u0090-\u009F,\u05F5-\u05FF,\u0800-\u08FF,\u0EE4-\u0EFF,\uFFF0-\uFFFF]/gu, "");
});
stream.on("end", async () => {
if (output.length > 2000) {
progress = await progress.edit(embed("Uploading...", "The output of your command is being uploaded."));
const time = Date.now();
if (!fs.existsSync("uploads")) fs.mkdirSync("uploads");
fs.writeFile(`./uploads/${time}.txt`, output, async (error) => {
if (error) {
await progress.edit(displayError(error));
return;
}
new Transfer(`./uploads/${time}.txt`)
.upload()
.then(async (url) => {
console.log("URL", url);
await progress.edit(
embed(
"Done",
"The output of your command was greater than 2000 characters, so it was uploaded.\n" + url
)
);
})
.catch(async (error) => {
await progress.edit(displayError(error));
});
fs.unlink(`./uploads/${time}.txt`, async (error) => {
if (error) {
await progress.edit(displayError(error));
}
});
});
} else {
await progress.edit(embed("Done", "```\n" + output + "\n```"));
}
});
});
});
};
module.exports.info = async (channel, guildCount, containerCount) => {
await channel.send(
embed(
"Info",
[
"I can create containers for you to run commands in.",
"There are also containers with preinstalled tools, see the associations link at the bottom.",
`I currently have ${containerCount} container${containerCount != 1
? "s"
: ""} running, and I'm on ${guildCount} server${guildCount != 1 ? "s" : ""}.`,
"",
"Commands:",
"- `$ info`",
"- `$ create`",
"- `$ remove`",
"- `$ <command>` or `$ run <command>`",
"",
"Invite me:",
"https://bit.ly/termbot",
"",
"Support server: https://discord.gg/9PmwWrA",
"Associations: https://gitlab.com/pwnsquad/term#associations",
"Source code: https://gitlab.com/pwnsquad/term",
"Status: https://status.pwnsquad.net/"
].join("\n")
)
);
};
module.exports.hello = async (channel) => {
await channel.send(
embed("Hello", "Thanks for inviting me to your server! You can learn more about how to use me with `$ info`.")
);
};