-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
79 lines (63 loc) · 1.94 KB
/
index.js
File metadata and controls
79 lines (63 loc) · 1.94 KB
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
const express = require("express");
const cors = require("cors");
const path = require("path");
const rateLimit = require("express-rate-limit");
const app = express();
let totalRequests = 0;
let clients = [];
const limiter = rateLimit({
windowMs: 60 * 1000,
max: 25,
message: { error: "Terlalu banyak permintaan, coba 60 detik lagi." },
keyGenerator: (req) => {
return req.headers["x-forwarded-for"]?.split(",")[0] || req.ip;
},
standardHeaders: true,
legacyHeaders: false
});
app.use(cors());
app.use(express.static(path.join(__dirname, "public")));
app.use((req, res, next) => {
totalRequests++;
sendUpdateToClients();
next();
});
app.get("/", (req, res) => {
res.sendFile(path.join(__dirname, "public", "index.html"));
});
app.get('/download/:file', async (req, res) => {
const file = req.params.file;
const originalUrl = `https://ytdown.siputzx.my.id/download/${file}`;
res.redirect(originalUrl);
});
app.get("/monitor-page", (req, res) => {
res.sendFile(path.join(__dirname, "public", "monitor", "monitor.html"));
});
app.get("/monitor", (req, res) => {
res.set({
"Content-Type": "text/event-stream",
"Cache-Control": "no-cache",
"Connection": "keep-alive",
});
res.flushHeaders();
const clientId = Date.now();
const newClient = { id: clientId, res };
clients.push(newClient);
res.write(`data: ${JSON.stringify({ totalRequests })}\n\n`);
req.on("close", () => {
clients = clients.filter(client => client.id !== clientId);
});
});
function sendUpdateToClients() {
clients.forEach(client => {
client.res.write(`data: ${JSON.stringify({ totalRequests })}\n\n`);
});
}
const routes = ["download"];
routes.forEach(route => {
app.use(`/api/${route}`, limiter, require(`./api/${route}`));
});
app.get("/api/endpoint-count", (req, res) => {
res.json({ totalEndpoints: routes.length });
});
module.exports = app;