-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.ts
253 lines (206 loc) · 9.02 KB
/
bot.ts
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
import { ActivityType, Client, CommandInteraction, EmbedBuilder } from 'discord.js';
import { CronJob } from 'cron';
import express from 'express';
import bodyParser from 'body-parser';
// Modules
import { BuildStatusUpdateReq, formatCommitShort, formatPiStatus, statusToColor } from './modules/status';
import { fetchAndUpdateScoreboard, lastUpdated, scoreboard, top5 } from './modules/scoreboard';
import { fetchAndUpdateChallenges, challenges, ctfdClient } from './modules/challenges';
import { slack, initGitRepo } from './modules/slack';
// Config
import { DISCORD_TOKEN } from './auth';
import {
BOLT_PORT,
DESIGN_REPO_URL,
EXPRESS_PORT,
FAILURE_CHANNEL_ID,
SCOREBOARD_NOTIFY_CHANNEL_ID,
ATTACK_NOTIFY_CHANNEL_ID,
STATUS_CHANNEL_ID,
STATUS_MESSAGE_ID
} from './config';
const client = new Client({
intents: [
"Guilds",
"GuildMessages",
"GuildPresences",
"GuildMembers",
"GuildMessageReactions",
],
presence: { activities: [{ type: ActivityType.Watching, name: 'the eCTF scoreboard' }] },
allowedMentions: { repliedUser: false }
});
let broadcastDiffsJob: CronJob;
async function broadcastDiffs(interaction?: CommandInteraction) {
const totalDiffs: string[] = [];
for (const team of Object.values(scoreboard)) {
// Construct diff string if fields change
const diffs: string[] = [];
if (team.prevPoints !== team.points)
diffs.push(`[points: ${team.prevPoints} → ${team.points}]`)
if (diffs.length) {
// Push rank only if other diffs already exist so that one team jumping 15 ranks doesn't cause
// 14 other lines of diffs.
if (team.prevRank !== team.rank)
diffs.push(`[rank: ${team.prevRank} → ${team.rank}]`);
totalDiffs.push(`[${team.name}](${team.href}): ${diffs.join(' ')}`);
}
}
const diffEmbed = new EmbedBuilder()
.setTitle(`eCTF scoreboard report for ${new Date().toLocaleDateString()}`)
.setDescription(totalDiffs.length ? totalDiffs.join('\n') : '*No scoreboard changes detected.*')
.setColor('#C61130')
.setTimestamp();
if (interaction)
return await interaction.reply({ embeds: [diffEmbed] });
const channel = client.channels.cache.get(SCOREBOARD_NOTIFY_CHANNEL_ID);
if (!channel?.isTextBased()) return;
await channel.send({ embeds: [diffEmbed] });
}
export async function notifyTargetPush(messages: string[]) {
const pushEmbed = new EmbedBuilder()
.setTitle('New target pushed to targets repository')
.setDescription(messages.join('\n'))
.setColor('#C61130')
.setTimestamp();
const channel = client.channels.cache.get(ATTACK_NOTIFY_CHANNEL_ID);
if (!channel?.isTextBased()) return;
await channel.send({ embeds: [pushEmbed] });
}
async function updateBuildStatus(req: BuildStatusUpdateReq) {
const channel = client.channels.cache.get(STATUS_CHANNEL_ID);
if (!channel?.isTextBased())
return console.error('[BUILD] Could not find build status channel!');
const message = channel.messages.cache.get(STATUS_MESSAGE_ID)
|| await channel.messages.fetch(STATUS_MESSAGE_ID)
|| channel.lastMessage;
const queueStatus = req.build.queue.map((d, i) => `${i + 1}. ${formatCommitShort(d)}`).join('\n')
|| '*No commits queued.*'
const piStatus = req.test.activeTests.map((s, i) => `${i + 1}. ${formatPiStatus(s)}`).join('\n');
const buildStatus = req.build.active
? formatCommitShort(req.build.active)
: '*No commits loaded.*'
const color = req.status
? statusToColor(req.status)
: '#27272a'
const statusEmbed = new EmbedBuilder()
.setTitle('Secure design build status')
.setDescription(`**Status:** ${req.status || 'N/A'}`)
.addFields(
{ name: 'Pis', value: piStatus },
{ name: 'Building:', value: buildStatus },
{ name: 'Queued:', value: queueStatus }
)
.setColor(color)
.setTimestamp()
// Report build failures to the appropriate channel
if (
(req.update.type === 'BUILD' || req.update.type === 'TEST')
&& req.update.state.result === 'FAILED'
) {
const runHref = `${DESIGN_REPO_URL}/actions/runs/${req.update.state.commit.runId}`;
const failureChannel = client.channels.cache.get(FAILURE_CHANNEL_ID);
const failureEmbed = new EmbedBuilder()
.setTitle(`${req.update.type === 'BUILD' ? 'Build' : 'Tests'} failed for commit`)
.setColor(0xb50300)
.setDescription(`[\`${req.update.state.commit.hash.slice(0, 7)}\`]: ${req.update.state.commit.name} (@${req.update.state.commit.author})\n[[Jump to failed workflow]](${runHref})`)
.setTimestamp()
if (failureChannel?.isTextBased())
failureChannel.send({ embeds: [failureEmbed] })
}
if (!message?.editable) return channel.send({ embeds: [statusEmbed] });
return message.edit({ embeds: [statusEmbed] });
}
const server = express();
server.use(bodyParser.json());
server.post('/', async (req, res) => {
console.log(`[BUILD] Received webhook push:\n${JSON.stringify(req.body)}`);
try {
await updateBuildStatus(req.body);
res.status(200).json({ ok: true });
} catch {
res.status(400).json({ ok: false });
}
});
server.listen(EXPRESS_PORT, () => {
console.log(`[BUILD] Started express server on port ${EXPRESS_PORT}`);
});
client.once('ready', async () => {
console.log(`[DISC] Logged in as ${client.user?.tag}!`);
// Broadcast diffs daily
broadcastDiffsJob = CronJob.from({
cronTime: '0 0 0 * * *',
onTick: async () => {
await broadcastDiffs();
await fetchAndUpdateScoreboard(true); // Reset diffs after each day
},
start: true,
timeZone: 'America/Indiana/Indianapolis',
runOnInit: false
});
});
client.on('interactionCreate', async (interaction) => {
if (!interaction.isChatInputCommand()) return;
switch (interaction.commandName) {
case 'scoreboard':
const scoreboardDesc = top5
.map((name) => scoreboard[name])
.map((data) => `${data.rank}. [${data.name}](${data.href}) — ${data.points} points`)
.join('\n')
const scoreboardEmbed = new EmbedBuilder()
.setTitle('eCTF scoreboard')
.setDescription(scoreboardDesc)
.setColor('#C61130')
.setFooter({ text: `Last fetched ${lastUpdated.toLocaleString()}` })
.setTimestamp();
return void interaction.reply({ embeds: [scoreboardEmbed] });
case 'refresh':
await fetchAndUpdateScoreboard();
const refreshEmbed = new EmbedBuilder()
.setDescription('Refreshed eCTF scoreboard data.')
.setColor('#C61130');
return void interaction.reply({ embeds: [refreshEmbed] });
case 'report':
await broadcastDiffs(interaction);
return;
case 'challenges':
const challengesDesc = challenges
.filter((c) => !c.solved_by_me)
.toSorted((a, b) => (b.solves - a.solves) || (b.value - a.value))
.slice(0, 10)
.map((c, i) => `${i + 1}. **${c.name}** (${c.value} pts): solved by ${c.solves}`)
.join('\n');
const challengesEmbed = new EmbedBuilder()
.setTitle('eCTF challenges')
.setDescription(`Top 10 remaining challenges by solves and points:\n${challengesDesc}`)
.setColor('#C61130')
.setTimestamp();
return void interaction.reply({ embeds: [challengesEmbed] });
case 'submit':
const id = interaction.options.getInteger('challenge', true);
const flag = interaction.options.getString('flag', true);
const res = await ctfdClient.submitFlag(id, flag);
const challName = challenges.find((c) => c.id === id)!.name;
const submitEmbed = new EmbedBuilder()
.setTitle(`Flag submission for \`${challName}\``)
.setDescription(`**Flag:** \`${flag}\`\n**Status:** ${res.status}\n**Message:** ${res.message}`)
.setColor('#C61130')
.setTimestamp();
return void interaction.reply({ embeds: [submitEmbed] });
}
});
client.on('interactionCreate', async (interaction) => {
if (!interaction.isAutocomplete()) return;
const input = interaction.options.getFocused();
const res = challenges
.filter(((c) => !c.solved_by_me && c.name.toLowerCase().startsWith(input.toLowerCase())))
.map((c) => ({ name: c.name, value: c.id }))
await interaction.respond(res);
});
// void initGitRepo();
void fetchAndUpdateScoreboard(true);
setInterval(fetchAndUpdateScoreboard, 1000 * 60);
void fetchAndUpdateChallenges();
setInterval(fetchAndUpdateChallenges, 1000 * 60);
void client.login(DISCORD_TOKEN);
void slack.start(BOLT_PORT);