-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
135 lines (109 loc) · 3.93 KB
/
index.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
import dotenv from 'dotenv';
import axios from 'axios';
import get from 'lodash/get';
import sortBy from 'lodash/sortBy';
import storage from 'node-persist';
dotenv.config();
const {CTFD_HOST, CTFD_SESSION, DISCORD_RANDOM_WEBHOOK_URL, DISCORD_GENERAL_WEBHOOK_URL} = process.env;
interface Challenge {
solves: number,
id: number,
name: string,
}
interface Solve {
account_id: number,
name: string,
date: string,
}
const checkTeamSize = async () => {
console.log('[team-size] Getting CSRF token...');
const {data} = await axios.get(`${CTFD_HOST}/admin/statistics`, {
headers: {
Cookie: `session=${CTFD_SESSION}`,
},
});
const [, token] = data.match(/'csrfNonce'\s*:\s*"(.+?)"/);
console.log(`[team-size] Got CSRF token: ${token}`);
console.log('[team-size] Getting teams...');
const {data: teamsResult} = await axios.get(`${CTFD_HOST}/api/v1/teams`, {
headers: {
Cookie: `session=${CTFD_SESSION}`,
'CSRF-Token': token,
},
});
const teamCount = get(teamsResult, ['meta', 'pagination', 'total'], 0);
console.log({teamCount});
const previousTeamCount = (await storage.getItem('teamCount')) || 0;
console.log({previousTeamCount});
const prevAchievement = Math.floor(teamCount / 100) * 100;
console.log({prevAchievement});
if (prevAchievement > previousTeamCount) {
const {data: discordResult} = await axios.post(DISCORD_RANDOM_WEBHOOK_URL!, {
content: `We hit ${prevAchievement} registrants! <https://score.ctf.tsg.ne.jp/scoreboard>`,
});
console.log(discordResult);
}
await storage.setItem('teamCount', teamCount);
};
const checkFirstBlood = async () => {
console.log('[first-blood] Getting CSRF token...');
const {data} = await axios.get(`${CTFD_HOST}/admin/statistics`, {
headers: {
Cookie: `session=${CTFD_SESSION}`,
},
});
const [, token] = data.match(/'csrfNonce'\s*:\s*"(.+?)"/);
console.log(`[first-blood] Got CSRF token: ${token}`);
console.log('[first-blood] Getting challenges...');
const {data: {data: challenges}}: {data: {data: Challenge[]}} = await axios.get(`${CTFD_HOST}/api/v1/challenges`, {
headers: {
Cookie: `session=${CTFD_SESSION}`,
'CSRF-Token': token,
},
});
const solvedChallenges = challenges.filter(({solves}) => solves > 0).map(({id}) => id);
console.log({solvedChallenges});
const previousSolvedChallenges = new Set((await storage.getItem('solvedChallenges')) || []);
console.log({previousSolvedChallenges});
for (const challenge of solvedChallenges) {
if (previousSolvedChallenges.has(challenge)) {
continue;
}
console.log(`[first-blood] Getting solves for challenge ${challenge}...`);
const {data: {data: solves}}: {data: {data: Solve[]}} = await axios.get(`${CTFD_HOST}/api/v1/challenges/${challenge}/solves`, {
headers: {
Cookie: `session=${CTFD_SESSION}`,
'CSRF-Token': token,
},
});
const {data: teamsResult} = await axios.get(`${CTFD_HOST}/api/v1/teams`, {
headers: {
Cookie: `session=${CTFD_SESSION}`,
'CSRF-Token': token,
},
});
const teams = get(teamsResult, ['data'], []);
const visibleSolves = solves.filter(solve => {
const team = teams.find(team => team.name === solve.name);
return team && !team.hidden;
});
if (visibleSolves.length === 0) {
console.error(`[first-blood] Challenge ${challenge} has no visible solves!`);
continue;
}
const firstBloodSolve = sortBy(visibleSolves, ({date}) => new Date(date).getTime())[0]!;
console.log({firstBloodSolve});
const challengeDatum = challenges.find(({id}) => id === challenge)!;
console.log({challengeDatum});
const {data: discordResult} = await axios.post(DISCORD_GENERAL_WEBHOOK_URL!, {
content: `Team **${firstBloodSolve.name}** first-blooded🩸 **${challengeDatum.name}**!!! Congrats!`,
});
console.log(discordResult);
}
await storage.setItem('solvedChallenges', solvedChallenges);
};
storage.init({dir: '.cache'}).then(() => {
console.log('Started');
// setInterval(checkTeamSize, 1000 * 60);
setInterval(checkFirstBlood, 1000 * 60);
});