/
index.js
166 lines (148 loc) · 4.76 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
import { uvPath } from "@titaniumnetwork-dev/ultraviolet";
import { epoxyPath } from "@mercuryworkshop/epoxy-transport";
import { libcurlPath } from "@mercuryworkshop/libcurl-transport";
import { createBareServer } from "@tomphttp/bare-server-node";
import express from "express";
import { createServer } from "http";
import path from "node:path";
import createRammerhead from "rammerhead/src/server/index.js";
import compression from "compression";
import { build } from "astro";
import chalk from "chalk";
import { existsSync } from "fs";
import dotenv from "dotenv";
import cookieParser from "cookie-parser";
import wisp from "wisp-server-node";
import fetch from "node-fetch";
import { masqrCheck } from "./masqr.js";
dotenv.config();
const whiteListedDomains = ["aluu.xyz", "localhost:3000"];
const LICENSE_SERVER_URL = "https://license.mercurywork.shop/validate?license=";
const WISP_ENABLED = process.env.USE_WISP;
const MASQR_ENABLED = process.env.MASQR_ENABLED;
if (!existsSync("./dist")) build({});
function log(message) {
console.log(chalk.gray("[Alu] " + message));
}
const bare = createBareServer("/bare/");
const PORT = process.env.PORT || 3000;
log("Starting Rammerhead...");
const rh = createRammerhead();
const rammerheadScopes = [
"/rammerhead.js",
"/hammerhead.js",
"/transport-worker.js",
"/task.js",
"/iframe-task.js",
"/worker-hammerhead.js",
"/messaging",
"/sessionexists",
"/deletesession",
"/newsession",
"/editsession",
"/needpassword",
"/syncLocalStorage",
"/api/shuffleDict",
];
const rammerheadSession = /^\/[a-z0-9]{32}/;
const app = express();
app.use(compression({ threshold: 0, filter: () => true }));
app.use(cookieParser());
// Set process.env.MASQR_ENABLED to "true" to enable masqr protection.
if (MASQR_ENABLED == "true") {
log("Starting Masqr...");
app.use(await masqrCheck({ whitelist: whiteListedDomains, licenseServer: LICENSE_SERVER_URL }));
}
app.use(express.static(path.join(process.cwd(), "static")));
app.use(express.static(path.join(process.cwd(), "build")));
app.use("/uv/", express.static(uvPath));
app.use("/epoxy/", express.static(epoxyPath));
app.use("/libcurl/", express.static(libcurlPath));
app.use(express.json());
app.use(
express.urlencoded({
extended: true,
})
);
app.use((req, res, next) => {
if (req.url.includes("/games/")) {
res.header("Cross-Origin-Opener-Policy", "same-origin");
res.header("Cross-Origin-Embedder-Policy", "require-corp");
}
next();
});
app.use("/custom-favicon", async (req, res) => {
try {
const { url } = req.query;
const response = await fetch(`https://www.google.com/s2/favicons?domain=${url}&sz=128`);
const buffer = new Buffer.from(await response.arrayBuffer());
res.set("Content-Type", "image/png");
res.send(buffer);
} catch {
}
});
app.use("/", express.static("dist/client/"));
app.get("/favicon.ico", (req, res) => {
res.sendFile(path.join(process.cwd(), "dist/client/favicon.svg"));
});
app.get("/robots.txt", (req, res) => {
if (req.headers.host && whiteListedDomains.includes(req.headers.host)) {
res.sendFile(path.join(process.cwd(), "dist/client/robots-allow.txt"));
} else {
res.sendFile(path.join(process.cwd(), "dist/client/robots-deny.txt"));
}
});
app.get("/search", async (req, res) => {
try {
const { query } = req.query;
const response = await fetch(`http://api.duckduckgo.com/ac?q=${query}&format=json`).then(
(apiRes) => apiRes.json()
);
res.send(response);
} catch (err) {
res.redirect(302, "/404.html");
}
});
app.get("*", (req, res) => {
res.sendFile(path.join(process.cwd(), "dist/client/404.html"));
});
let server = createServer();
server.on("request", (req, res) => {
if (bare.shouldRoute(req)) {
bare.routeRequest(req, res);
} else if (shouldRouteRh(req)) {
routeRhRequest(req, res);
} else {
app(req, res);
}
});
server.on("upgrade", (req, socket, head) => {
if (bare.shouldRoute(req)) {
bare.routeUpgrade(req, socket, head);
} else if (shouldRouteRh(req)) {
routeRhUpgrade(req, socket, head);
/* Kinda hacky, I need to do a proper dynamic import. */
} else if (req.url.endsWith("/wisp/") && WISP_ENABLED == "true") {
wisp.routeRequest(req, socket, head);
} else {
socket.end();
}
});
function shouldRouteRh(req) {
const url = new URL(req.url, "http://0.0.0.0");
return rammerheadScopes.includes(url.pathname) || rammerheadSession.test(url.pathname);
}
function routeRhRequest(req, res) {
rh.emit("request", req, res);
}
function routeRhUpgrade(req, socket, head) {
rh.emit("upgrade", req, socket, head);
}
log("Starting Alu...");
console.log(chalk.green("[Alu] Alu started successfully!"));
server.on("listening", () => {
console.log(chalk.green(`[Alu] Server running at http://localhost:${PORT}/.`));
});
server.listen({
port: PORT,
});