-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
253 lines (230 loc) · 7.08 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
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
/**
* API for Réseau Discord Artivain
* @version 1.1.0
* @author Thomas Fournier <[email protected]>
*/
// KV names:
// BLACKLIST
// SUSLIST
// SECRETS
// Environment variables
// DISCORD_WEBHOOK
const apiName = "Réseau Discord Artivain (official)";
const apiVersion = "1.1.0";
const headers = {
"content-type": "application/json;charset=UTF-8",
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET",
};
const kvParameters = {
type: "json",
cacheTtl: 600 // 10 minutes
};
const enableDiscordNotification = true;
addEventListener("fetch", event => {
event.respondWith(handleRequest(event.request));
});
/**
* Handle the request to make an API response
* @param {Request} request
* @returns {Response}
*/
async function handleRequest(request) {
const requestURL = new URL(request.url);
if (requestURL.pathname.split("/")[1] != "v1") return new Response("{}", { status: 400, headers });
const action = requestURL.pathname.split("/")[2];
const parameters = requestURL.searchParams;
let status = 200;
const response = {
dbName: apiName,
apiVersion,
action
};
if (action == "ping") {
response.online = true;
return new Response(JSON.stringify(response), { headers });
}
if (request.method != "GET") return new Response("{}", { status: 405, headers });
if (action == "check" && parameters.has("id")) {
const id = parameters.get("id").trim();
response.id = id;
if (isValidId(id)) {
const sus = await SUSLIST.get(id, kvParameters);
const bl = await BLACKLIST.get(id, kvParameters);
if (sus) {
response.suspect = {
addedBy: sus.addedBy,
since: sus.since
};
} else response.suspect = false;
if (bl) {
response.blacklist = {
addedBy: bl.addedBy,
since: bl.since
};
} else response.blacklist = false;
} else status = 400;
} else if (action == "add-suspect" && parameters.has("id") && parameters.has("username") && parameters.has("token")) {
const id = parameters.get("id").trim(),
username = parameters.get("username"),
token = parameters.get("token");
response.id = id;
response.auth = {
username,
token: "hidden"
};
if (isValidId(id) && username && token) {
const user = await SECRETS.get(username, kvParameters);
if (user && user.token == token) {
if (user.perms.includes("suslist") || user.perms.includes("suslist.add")) {
const alreadySus = await SUSLIST.get(id, kvParameters);
if (alreadySus) {
response.added = false;
response.suspect = {
addedBy: alreadySus.addedBy,
since: alreadySus.since
};
} else {
const since = Date.now();
const newSus = await SUSLIST.put(id, JSON.stringify({
addedBy: username,
since
}));
if (typeof newSus == "undefined") {
response.added = true;
response.suspect = {
addedBy: username,
since
};
await sendDiscordNotification("added to suspects", username, id);
} else status = 500;
}
} else status = 403;
} else status = 401;
} else status = 400;
} else if (action == "remove-suspect" && parameters.has("id") && parameters.has("username") && parameters.has("token")) {
const id = parameters.get("id").trim(),
username = parameters.get("username"),
token = parameters.get("token");
response.id = id;
response.auth = {
username,
token: "hidden"
};
if (isValidId(id) && username && token) {
const user = await SECRETS.get(username, kvParameters);
if (user && user.token == token) {
if (user.perms.includes("suslist")) {
const suslistEntry = await SUSLIST.get(id, kvParameters);
if (suslistEntry) {
const removedSuslistEntry = await SUSLIST.delete(id);
if (typeof removedSuslistEntry == "undefined") {
response.removed = true;
await sendDiscordNotification("removed from suspects", username, id);
} else status = 500;
} else status = 404;
} else status = 403;
} else status = 401;
} else status = 400;
} else if (action == "add-blacklist" && parameters.has("id") && parameters.has("username") && parameters.has("token")) {
const id = parameters.get("id").trim(),
username = parameters.get("username"),
token = parameters.get("token");
response.id = id;
response.auth = {
username,
token: "hidden"
};
if (isValidId(id) && username && token) {
const user = await SECRETS.get(username, kvParameters);
if (user && user.token == token) {
if (user.perms.includes("blacklist") || user.perms.includes("blacklist.add")) {
const alreadyBlacklisted = await BLACKLIST.get(id, kvParameters);
if (alreadyBlacklisted) {
response.added = false;
response.blacklist = {
addedBy: alreadyBlacklisted.addedBy,
since: alreadyBlacklisted.since
};
} else {
const since = Date.now();
const newBlacklistEntry = await BLACKLIST.put(id, JSON.stringify({
addedBy: username,
since
}));
if (typeof newBlacklistEntry == "undefined") {
response.added = true;
response.blacklist = {
addedBy: username,
since
};
await sendDiscordNotification("added to blacklist", username, id);
} else status = 500;
}
} else status = 403;
} else status = 401;
} else status = 400;
} else if (action == "remove-blacklist" && parameters.has("id") && parameters.has("username") && parameters.has("token")) {
const id = parameters.get("id").trim(),
username = parameters.get("username"),
token = parameters.get("token");
response.id = id;
response.auth = {
username,
token: "hidden"
};
if (isValidId(id) && username && token) {
const user = await SECRETS.get(username, kvParameters);
if (user && user.token == token) {
if (user.perms.includes("blacklist")) {
const blacklistEntry = await BLACKLIST.get(id, kvParameters);
if (blacklistEntry) {
const removedBlacklistEntry = await BLACKLIST.delete(id);
if (typeof removedBlacklistEntry == "undefined") {
response.removed = true;
await sendDiscordNotification("removed from blacklist", username, id);
} else status = 500;
} else status = 404;
} else status = 403;
} else status = 401;
} else status = 400;
} else {
status = 400; // Bad request
}
response.status = status;
return new Response(JSON.stringify(response), {
status,
headers
});
};
/**
* Send notification to a Discord channel through a webhook
* @param {string} action - The action the user just did
* @param {string} by - The said user
* @param {string} on - Timestamp
* @param {string} id - The ID added to the list
* @returns {Response}
* @async
* @since 1.1.0
*/
function sendDiscordNotification(action, by, id) {
if (enableDiscordNotification) return fetch(DISCORD_WEBHOOK, {
method: "POST",
headers: {
"Accept": "application/json",
"Content-Type": "application/json"
},
body: JSON.stringify({
content: `${by}: ${action} \`${id}\``
})
});
}
/**
* Check if a string is a valid Discord ID
* @param {string} id - String to verify
* @returns {boolean}
*/
function isValidId(id) {
if (isNaN(id) || id.length < 18) return false;
return true;
};