-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.js
146 lines (125 loc) · 4.09 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
import @tomphttp/bare-server-node from "@tomphttp/bare-server-node"
import http from "node:http";
import path from "node:path";
import { createBareServer } from "@tomphttp/bare-server-node";
import chalk from "chalk";
import cookieParser from "cookie-parser";
import cors from "cors";
import express from "express";
import basicAuth from "express-basic-auth";
import mime from "mime";
import fetch from "node-fetch";
import { setupMasqr } from "./Masqr.js";
import config from "./config.js";
console.log(chalk.yellow("🚀 Starting server..."));
const __dirname = process.cwd();
const server = http.createServer();
const app = express();
const bareServer = createBareServer("/ov/");
const PORT = process.env.PORT || 8080;
const cache = new Map();
const CACHE_TTL = 30 * 24 * 60 * 60 * 1000; // Cache for 30 Days
if (config.challenge) {
console.log(
chalk.green("🔒 Password protection is enabled! Listing logins below"),
);
// biome-ignore lint/complexity/noForEach:
Object.entries(config.users).forEach(([username, password]) => {
console.log(chalk.blue(`Username: ${username}, Password: ${password}`));
});
app.use(basicAuth({ users: config.users, challenge: true }));
}
app.get("/e/*", async (req, res, next) => {
try {
if (cache.has(req.path)) {
const { data, contentType, timestamp } = cache.get(req.path);
if (Date.now() - timestamp > CACHE_TTL) {
cache.delete(req.path);
} else {
res.writeHead(200, { "Content-Type": contentType });
return res.end(data);
}
}
const baseUrls = {
"/e/1/": "https://raw.githubusercontent.com/v-5x/x/fixy/",
"/e/2/": "https://raw.githubusercontent.com/ypxa/y/main/",
"/e/3/": "https://raw.githubusercontent.com/ypxa/w/master/",
};
let reqTarget;
for (const [prefix, baseUrl] of Object.entries(baseUrls)) {
if (req.path.startsWith(prefix)) {
reqTarget = baseUrl + req.path.slice(prefix.length);
break;
}
}
if (!reqTarget) {
return next();
}
const asset = await fetch(reqTarget);
if (!asset.ok) {
return next();
}
const data = Buffer.from(await asset.arrayBuffer());
const ext = path.extname(reqTarget);
const no = [".unityweb"];
const contentType = no.includes(ext)
? "application/octet-stream"
: mime.getType(ext);
cache.set(req.path, { data, contentType, timestamp: Date.now() });
res.writeHead(200, { "Content-Type": contentType });
res.end(data);
} catch (error) {
console.error("Error fetching asset:", error);
res.setHeader("Content-Type", "text/html");
res.status(500).send("Error fetching the asset");
}
});
app.use(cookieParser());
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
if (process.env.MASQR === "true") {
setupMasqr(app);
}
app.use(express.static(path.join(__dirname, "static")));
app.use("/ov", cors({ origin: true }));
const routes = [
{ path: "/as", file: "apps.html" },
{ path: "/gm", file: "games.html" },
{ path: "/st", file: "settings.html" },
{ path: "/ta", file: "tabs.html" },
{ path: "/ts", file: "tools.html" },
{ path: "/", file: "index.html" },
{ path: "/tos", file: "tos.html" },
{ path: "/privacy", file: "privacy.html" },
];
// biome-ignore lint/complexity/noForEach:
routes.forEach(route => {
app.get(route.path, (_req, res) => {
res.sendFile(path.join(__dirname, "static", route.file));
});
});
app.use((req, res, next) => {
res.status(404).sendFile(path.join(__dirname, "static", "404.html"));
});
app.use((err, req, res, next) => {
console.error(err.stack);
res.status(500).sendFile(path.join(__dirname, "static", "404.html"));
});
server.on("request", (req, res) => {
if (bareServer.shouldRoute(req)) {
bareServer.routeRequest(req, res);
} else {
app(req, res);
}
});
server.on("upgrade", (req, socket, head) => {
if (bareServer.shouldRoute(req)) {
bareServer.routeUpgrade(req, socket, head);
} else {
socket.end();
}
});
server.on("listening", () => {
console.log(chalk.green(`🌍 Server is running on http://localhost:${PORT}`));
});
server.listen({ port: PORT });