-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
382 lines (364 loc) · 14.8 KB
/
server.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
require("dotenv").config();
const express = require("express");
const app = express();
const Sentry = require("@sentry/node");
const path = require("path");
const fs = require("fs-extra");
const cheerio = require("cheerio");
const cookieParser = require('cookie-parser');
const cookieSession = require('cookie-session');
//const crypto = require('crypto');
const authentication = require('./handlers/authentication');
const database = require('./handlers/database');
const gameactivity = require('./handlers/gameactivity');
//const flashpoint = require('./providers/flashpoint');
//const armorgames = require('./providers/armorgames');
const port = process.env.PORT || 3000;
if (process.env.DISCORD_CLIENT_ID === undefined) {
throw new Error("DISCORD_CLIENT_ID environment variable is required!");
} else if (process.env.DISCORD_CLIENT_SECRET === undefined) {
throw new Error("DISCORD_CLIENT_SECRET environment variable is required!");
} else if (process.env.BASE_PATH === undefined) {
throw new Error("BASE_PATH environment variable is required!");
} else if (process.env.DB_HOST === undefined) {
throw new Error("DB_HOST environment variable is required!");
} else if (process.env.DB_USER === undefined) {
throw new Error("DB_USER environment variable is required!");
} else if (process.env.DB_PASS === undefined) {
throw new Error("DB_PASS environment variable is required!");
} else if (process.env.DB_NAME === undefined) {
throw new Error("DB_NAME environment variable is required!");
} else if (process.env.EMAIL === undefined) {
throw new Error("EMAIL environment variable is required!");
} else if (process.env.SECURE === undefined) {
throw new Error("SECURE environment variable is required!");
} else if (process.env.SECURE !== "true" && process.env.SECURE !== "false") {
throw new Error("SECURE environment variable must be either 'true' or 'false'!");
} else {
console.log("Environment variables are ok");
}
//function generateSessionId() {
// return crypto.randomBytes(32).toString('base64');
//}
if (process.env.SENTRY_DSN !== undefined) {
Sentry.init({
dsn: process.env.SENTRY_DSN,
tracesSampleRate: 1.0
});
}
app.set('view engine', 'ejs');
app.use(cookieSession({
name: 'session',
keys: process.env.SESSION_SECRET,
maxAge: 30 * 24 * 60 * 60 * 1000, // 30 days
cookie: {
sameSite: 'strict',
signed: true
}
}));
app.use(cookieParser(process.env.SESSION_SECRET));
async function fetchGame(provider, id, url) {
console.log(`Fetching game file from ${provider} with id ${id}`);
if (provider === "armorgames") {
if (fs.existsSync(`debug/${id}.html`)) {
console.log("File exists");
const gameFile = cheerio.load(await fs.readFile(`debug/${id}.html`))('param[name="movie"]').attr("value");
return gameFile;
} else if (!fs.existsSync(`debug/${id}.json`)) {
const response = await fetch(url);
if (!response.ok) {
throw new Error(`Failed to fetch game file from Armor Games ${response.status} ${response.statusText}`);
} else {
const html = await response.text();
await fs.ensureDir("debug");
await fs.writeFile(`debug/armorgames-${id}.html`, html);
const gameFile = cheerio.load(html)('param[name="movie"]').attr("value");
return gameFile;
}
}
} else {
throw new Error("Invalid provider");
}
}
app.get("/auth/discord", (req, res) => {
res.redirect(`https://discord.com/api/oauth2/authorize?client_id=${process.env.DISCORD_CLIENT_ID}&redirect_uri=${process.env.BASE_PATH}/auth/discord/callback&response_type=code&scope=identify`);
});
app.get("/auth/discord/callback", async (req, res) => {
const code = req.query.code;
if (!code) {
return res.status(400).send("Code is required");
}
const response = await fetch("https://discord.com/api/oauth2/token", {
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
body: new URLSearchParams({
client_id: process.env.DISCORD_CLIENT_ID,
client_secret: process.env.DISCORD_CLIENT_SECRET,
grant_type: "authorization_code",
code,
redirect_uri: `${process.env.BASE_PATH}/auth/discord/callback`,
scope: "identify",
}),
});
if (!response.ok) {
return res.status(400).send("Token invalid");
} else {
const json = await response.json();
console.log(json);
const userResponse = await fetch("https://discord.com/api/users/@me", {
headers: {
Authorization: `${json.token_type} ${json.access_token}`,
},
});
const userJson = await userResponse.json();
console.log(userJson);
try {
if (process.env.SECURE === "true") {
res.cookie("token", await authentication.authenticateUser(userJson), { maxAge: 1000 * 60 * 60 * 24 * 30, signed: true, secure: true, sameSite: "strict"});
} else if (process.env.SECURE === "false") {
res.cookie("token", await authentication.authenticateUser(userJson), { maxAge: 1000 * 60 * 60 * 24 * 30, signed: true, secure: false, sameSite: "strict"});
}
res.redirect("/");
//res.send(`
// <div style="margin: 300px auto; max-width: 400px; display: flex; flex-direction: column; align-items: center;">
// <h3>Welcome, ${userJson.global_name}</h3>
// <script>
// window.location.replace('/');
// </script>
// </div>
//`);
} catch (error) {
console.error(error);
res.status(500).send("Internal server error");
}
}
});
app.get('/login', (req, res) => {
res.redirect('/auth/discord');
});
app.get('/', async (req, res) => {
const token = req.signedCookies['token'];
try {
const userinfo = await authentication.quickVerifyUser(token);
console.log("Detected", userinfo);
res.render(path.join(__dirname, 'views', 'index.ejs'), { username: userinfo.username, avatar: userinfo.avatar });
} catch {
res.render(path.join(__dirname, 'views', 'index.ejs'), { username: undefined, avatar: undefined });
}
})
app.get('/gameactivity', async (req, res) => {
const token = req.signedCookies['token'];
if (token === undefined) {
res.redirect('/login');
} else {
try {
const userinfo = await authentication.quickVerifyUser(token);
const activities = await gameactivity.fetchGameActivity(token);
console.log("Detected", userinfo);
res.render(path.join(__dirname, 'views', 'gameactivity.ejs'), { username: userinfo.username, avatar: userinfo.avatar, activities: activities });
} catch {
res.redirect('/login');
}
}
});
app.get('/:page', async (req, res, next) => {
const pagePath = path.join(__dirname, 'views', `${req.params.page}.ejs`)
const token = req.signedCookies['token'];
fs.access(pagePath, fs.constants.F_OK, async (err) => {
if (err) {
next();
} else {
try {
const userinfo = await authentication.quickVerifyUser(token);
res.render(pagePath, { username: userinfo.username, avatar: userinfo.avatar, email: process.env.EMAIL });
} catch {
res.render(pagePath, { username: undefined, avatar: undefined, email: process.env.EMAIL });
}
}
});
});
app.get("/api/search", async (req, res) => {
const searchTerm = req.query.q;
const provider = req.query.provider;
//const filter = req.query.filter;
//if (provider === undefined) {
const flashpointAPI = `https://db-api.unstable.life/search?smartSearch=${searchTerm}&fields=id,title,developer,publisher,platform,library,tags,originalDescription,dateAdded,dateModified`;//&filter=true
const armorgamesAPI = "https://armorgames.com/service/game-search";
if (!searchTerm) {
return res.status(400).send("Search term is required");
} else {
console.log(req.query);
console.log(`Fetching from Flashpoint API...`);
const flashpointResponse = await fetch(flashpointAPI);
if (!flashpointResponse.ok) {
throw new Error(
`Failed to fetch data from Flashpoint API (${flashpointResult.status} ${flashpointResult.statusText})`
);
}
const flashpointSearchResultJson = await flashpointResponse.json();
const flashpointSearchResult = flashpointSearchResultJson
.filter((result) => result.platform === "Flash")
.map((result) => ({
id: result.id,
title: result.title,
developer: result.developer,
publisher: result.publisher,
description: result.originalDescription,
cover: `https://infinity.unstable.life/images/Logos/${result.id.substring(0,2)}/${result.id.substring(2,4)}/${result.id}.png?type=jpg`,
getInfo: `https://ooooooooo.ooo/get?id=${result.id}`, //{"uuid":"06695a49-dd02-4902-ae18-aa13d8b50c20","title":"The Sigworminator 6000","launchCommand":"http://uploads.ungrounded.net/231000/231585_Create_a_worm.swf?123","utcMilli":"1616732063351","extreme":false,"votesWorking":0,"votesBroken":0,"isZipped":true}
provider: "Flashpoint",
}));
console.log("Finished fetching from Flashpoint API\nFetching from Armor Games API...");
const armorgamesResponse = await fetch(armorgamesAPI);
const armorgamesResultJson = await armorgamesResponse.json();
await fs.ensureDir("debug");
await fs.writeFile(`debug/armorgames.json`,JSON.stringify(armorgamesResultJson));
var armorgamesSearchResult = await armorgamesResultJson;
armorgamesSearchResult = armorgamesSearchResult.filter((game) => game.label && game.label.toLowerCase().includes(searchTerm.toLowerCase()));
armorgamesSearchResult = armorgamesSearchResult.filter((game) => game.url.split("/")[1] === "play");
armorgamesSearchResult = armorgamesSearchResult.map((game) => ({
id: game.game_id,
title: game.label,
cover: game.thumbnail,
gameUrl: `https://armorgames.com${game.url}`,
provider: "Armor Games",
}));
console.log("Finished fetching from Armor Games API");
res.json({ ...flashpointSearchResult, ...armorgamesSearchResult });
}
//}
});
app.get("/api/userprofile", async (req, res) => {
const token = req.signedCookies['token'];
console.log("Recieved signed cookie:", req.signedCookies, " and unsigned cookie:", req.cookies);
if (token === undefined) {
return res.status(401).send("Unauthorized");
} else {
try {
user = await authentication.quickVerifyUser(token);
res.json({
userid: user.userid,
username: user.username,
avatar: user.avatar
});
} catch (error) {
console.error(error);
res.status(500).json({ error: "Internal server error" });
}
}
});
app.get('/api/getgame', async (req, res) => {
const provider = req.query.provider;
const id = req.query.id;
const token = req.signedCookies['token'];
gamefile = undefined;
if (!provider || !id) {
return res.status(400).json({ error: "Provider and ID are required" });
} else if (provider === "armorgames") {
try {
gameFile = await fetchGame("armorgames", id, `https://armorgames.com/play/${id}`);
if (token !== undefined) {
fs.readFile('debug/armorgames.json', 'utf8', async (err, data) => {
if (err !== null) {
console.warn("An error occured while reading the Armor Games cache:", err)
} else if (data !== undefined){
const gameResult = JSON.parse(data).filter(game => Number(game.game_id) === Number(req.query.id));
if (gameResult.length > 0) {
try {
await gameactivity.storeGameActivity(token, gameResult[0].label, provider, id);
} catch (error) {
console.warn("An error occured while storing gaming activity:", error);
}
} else {
console.warn("An error occured while storing game activity because the game wasn't found in the Armor Games cache")
}
}
});
}
res.json({ gameFile: gameFile });
} catch (error) {
console.error(error);
return res.status(404).json({ error: "Game not found" });
}
} else if (provider === "flashpoint") {
const gameinfo = await fetch(`https://ooooooooo.ooo/get?id=${id}`)
if (gameinfo.ok) {
const gameinfojson = await gameinfo.json();
console.log(gameinfojson);
if (token !== undefined) {
try {
await gameactivity.storeGameActivity(token, gameinfojson.title, provider, id);
} catch (error) {
console.error(error);
}
}
try {
res.json({
uuid: gameinfojson.id,
title: gameinfojson.title,
utcMilli: gameinfojson.utcMilli,
extreme: gameinfojson.extreme,
gameFile: gameinfojson.launchCommand,
gameLocationOnZip: decodeURIComponent('content/' + new URL(gameinfojson.launchCommand).hostname + new URL(gameinfojson.launchCommand).pathname),
gameFile2: `https://download.unstable.life/gib-roms/Games/${gameinfojson.uuid}-${gameinfojson.utcMilli}.zip`,//https://download.unstable.life/gib-roms/Games/001485ad-b206-4e72-a44d-605d836afe6c-1630664499395.zip
});
} catch (error) {
console.error(`Game URL retrieved from Flashpoint API is invalid, the game id is ${gameinfojson.id}:`, error);
res.status(500).json({ error: "Game URL is invalid, please report this error to the developer." });
}
} else {
res.status(404).json({ error: "Failed to fetch game, game might not exist" });
}
} else {
res.status(400).json({ error: "Invalid provider" });
};
});
app.get('/api/gameactivity', async (req, res) => {
const token = req.signedCookies['token'];
if (token === undefined) {
return res.status(401).json({ error: "Unauthorized" });
} else {
try {
res.json(await gameactivity.fetchGameActivity(token));
} catch (error) {
console.error(error);
res.status(500).json({ error: "Internal server error" });
}
}
});
app.get('/proxy', async (req, res) => {
const url = req.query.url;
if (url === undefined) {
return res.status(400).json({ error: "URL is required" });
} else if (!url.startsWith("https://download.unstable.life/gib-roms/Games/")) {
return res.status(400).json({ error: "URL isn't invalid" });
} else if (!url.endsWith(".zip")) {
return res.status(400).json({ error: "URL isn't invalid" });
} else {
try {
console.log(`Fetching file from ${url}`)
response = await fetch(url);
if (response.ok) {
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader("Content-Disposition", `attachment; filename=${url.split("/").pop()}`);
res.send(Buffer.from(await response.arrayBuffer()));
} else {
res.status(500).json({ error: "Failed to fetch file" });
}
} catch (error) {
res.status(500).json({ error: error });
console.error(error);
}
}
});
app.get("/logout", (req, res) => {
res.clearCookie("token");
res.redirect("/");
})
app.use(express.static(path.join(__dirname, "public")));
app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
database.dbInit();
});