forked from 55gms/55GMS
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
128 lines (113 loc) · 3.37 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
const express = require("express");
const http = require("http");
const { createBareServer } = require("@tomphttp/bare-server-node");
const path = require("path");
const cors = require("cors");
const server = http.createServer();
const app = express(server);
const bareServer = createBareServer("/t/");
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(cors());
app.use((req, res, next) => {
if (path.extname(req.url) === ".js") {
res.setHeader("Content-Type", "application/javascript");
}
next();
});
app.use(express.static(path.join(__dirname, "static")));
const routes = [
{ path: "/a", file: "apps.html" },
{ path: "/g", file: "art.html" },
{ path: "/s", file: "settings.html" },
{ path: "/p", file: "science.html" },
{ path: "/!", file: "!.html" },
{ path: "/", file: "index.html" },
{ path: "/d", file: "dashboard.html" },
{ path: "/-", file: "math.html" },
];
routes.forEach(route => {
app.get(route.path, (req, res) => {
res.sendFile(path.join(__dirname, "static", route.file));
});
});
app.get("/misc/*", async (req, res, next) => {
const baseUrl = "https://raw.githubusercontent.com/kfm5/a/main";
const secondaryUrl = "https://raw.githubusercontent.com/22yeets22/a/main";
await fetchDataFromGithub(req, res, next, baseUrl, secondaryUrl);
});
async function fetchDataFromGithub(req, res, next, baseUrl, secondaryUrl = null) {
function isAFile(urlString) {
return urlString.trim().split("/").pop().length !== 0;
}
async function fetchDataOneSource(req, res, next, url) {
if (isAFile(req.params[0])) {
const reqTarget = `${url}/${req.params[0]}`;
const asset = await fetch(reqTarget);
if (asset.ok) {
const data = await asset.arrayBuffer();
res.end(Buffer.from(data));
return true;
}
} else {
const indexReqTarget = `${url}/${req.params[0]}/index.html`;
const indexAsset = await fetch(indexReqTarget);
if (indexAsset.ok) {
const indexData = await indexAsset.arrayBuffer();
res.end(Buffer.from(indexData));
return true;
}
}
return false;
}
try {
if (await fetchDataOneSource(req, res, next, baseUrl)) return;
if (secondaryUrl) {
if (await fetchDataOneSource(req, res, next, secondaryUrl)) return;
}
res.status(404).send("Resource not found");
} catch (error) {
console.error("Error fetching data, internal server error:", error);
res.status(500).send("Internal Server Error");
}
}
server.on("request", (req, res) => {
try {
if (bareServer.shouldRoute(req)) {
bareServer.routeRequest(req, res);
} else {
app(req, res);
}
} catch (error) {
console.error("Request error:", error);
res.status(500).send("Internal Server Error");
}
});
server.on("upgrade", (req, socket, head) => {
try {
if (bareServer.shouldRoute(req)) {
bareServer.routeUpgrade(req, socket, head);
} else {
socket.end();
}
} catch (error) {
console.error("Upgrade error:", error);
socket.end();
}
});
/* app.get("/*", (req, res) => {
res.sendFile(path.join(__dirname, "static/404.html"), err => {
if (err) {
res.status(404).send(err);
}
});
}); */
server.on("listening", () => {
console.log(`Running at http://localhost:8080`);
});
server.listen({
port: 8080,
});
server.on("error", error => {
console.error("Server error:", error);
});