Switch to fastify!

This commit is contained in:
MotorTruck1221 2024-08-02 00:56:09 -06:00
parent 5d609adc52
commit 3649044eb8
No known key found for this signature in database
GPG key ID: 08F417E2B8B61EA4
11 changed files with 135 additions and 18 deletions

View file

@ -1,3 +1,3 @@
(async () => {
await import("./src/server.mjs");
await import("./src/fastify.mjs");
})();

View file

@ -19,6 +19,8 @@
"author": "Titanium Network",
"license": "GNU AFFERO",
"dependencies": {
"@fastify/helmet": "^11.1.1",
"@fastify/static": "^7.0.4",
"@mercuryworkshop/bare-as-module3": "^2.2.2",
"@mercuryworkshop/bare-mux": "^2.0.1",
"@mercuryworkshop/epoxy-transport": "^2.1.3",
@ -27,6 +29,7 @@
"@tomphttp/bare-server-node": "^2.0.3",
"axios": "^1.7.2",
"express": "^4.19.2",
"fastify": "^4.28.1",
"helmet": "^7.1.0",
"mime-types": "^2.1.35",
"puppeteer": "^22.12.1",

114
src/fastify.mjs Normal file
View file

@ -0,0 +1,114 @@
import Fastify from 'fastify';
import { createServer } from 'node:http';
import wisp from 'wisp-server-node';
import createRammerhead from "rammerhead/src/server/index.js";
import { epoxyPath } from "@mercuryworkshop/epoxy-transport";
import { libcurlPath } from "@mercuryworkshop/libcurl-transport";
import { bareModulePath } from "@mercuryworkshop/bare-as-module3";
import { baremuxPath } from "@mercuryworkshop/bare-mux/node";
import { uvPath } from "@titaniumnetwork-dev/ultraviolet";
import fastifyHelmet from '@fastify/helmet';
import fastifyStatic from '@fastify/static';
import pkg from "./routes.mjs";
import { readFile } from 'node:fs/promises';
import path from 'node:path';
import { paintSource, tryReadFile } from './randomization.mjs';
import loadTemplates from './templates.mjs';
import { fileURLToPath } from 'node:url';
import { existsSync } from 'node:fs';
const config = JSON.parse(await readFile(new URL("./config.json", import.meta.url))), { pages, text404 } = pkg;
const __dirname = path.resolve();
const port = process.env.PORT || config.port;
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",
"/mainport",
];
const rammerheadSession = /^\/[a-z0-9]{32}/;
const shouldRouteRh = req => {
const url = new URL(req.url, "http://0.0.0.0");
return (
rammerheadScopes.includes(url.pathname) ||
rammerheadSession.test(url.pathname)
);
}
const routeRhRequest = (req, res) => {
rh.emit("request", req, res);
}
const routeRhUpgrade = (req, socket, head) => {
rh.emit("upgrade", req, socket, head);
}
//create a server factory for RH, and wisp (and bare if you please)
const serverFactory = (handler) => {
return createServer()
.on('request', (req, res) => {
if (shouldRouteRh(req)) {
routeRhRequest(req, res);
}
else {
handler(req, res);
}
})
.on('upgrade', (req, socket, head) => {
if (shouldRouteRh(req)) {
routeRhUpgrade(req, socket, head);
}
else if (req.url.endsWith('/wisp/')) {
wisp.routeRequest(req, socket, head);
}
})
}
//set logger to true for logs
const app = Fastify({ logger: false, serverFactory: serverFactory });
app.register(fastifyStatic, {
root: fileURLToPath(new URL('../views', import.meta.url)),
});
app.register(fastifyStatic, {
root: uvPath,
//due to how Fastify works, we have to have the uvPath live on a different prefix then the one in /views/
prefix: "/uv-static/",
decorateReply: false
});
app.register(fastifyStatic, {
root: epoxyPath,
prefix: "/epoxy/",
decorateReply: false
});
app.register(fastifyStatic, {
root: libcurlPath,
prefix: "/libcurl/",
decorateReply: false
});
app.register(fastifyStatic, {
root: bareModulePath,
prefix: "/bareasmodule/",
decorateReply: false
});
app.register(fastifyStatic, {
root: baremuxPath,
prefix: "/baremux/",
decorateReply: false
});
app.get("/", function(req, reply) {
reply.type('html');
reply.send(paintSource(loadTemplates(tryReadFile(path.join(__dirname, "views", "/?".indexOf(req.url) ? pages[Object.keys(req.query)[0]] || "error.html" : pages.index)))))
});
//host is set as to avoid just being on localhost
app.listen({ port: port, host: '0.0.0.0' });

View file

@ -26,7 +26,7 @@
<link rel="dns-prefetch" href="https://fonts.googleapis.com" />
<link rel="stylesheet" href="assets/css/styles.css" />
<script src="/baremux/index.js" defer></script>
<script src="/uv/uv.bundle.js" defer></script>
<script src="/uv-static/uv.bundle.js" defer></script>
<script src="/uv/uv.config.js" defer></script>
<script src="/assets/js/register-sw.js" defer></script>
<!-- Arc widget causes lag on some gams, so it is created by js below if &nolag isn't specified in the querystring -->

View file

@ -26,7 +26,7 @@
<link rel="dns-prefetch" href="https://fonts.googleapis.com" />
<link rel="stylesheet" href="assets/css/styles.css" />
<script src="/baremux/index.js" defer></script>
<script src="/uv/uv.bundle.js" defer></script>
<script src="/uv-static/uv.bundle.js" defer></script>
<script src="/uv/uv.config.js" defer></script>
<script src="/assets/js/register-sw.js" defer></script>
<script src="

View file

@ -26,7 +26,7 @@
<link rel="dns-prefetch" href="https://fonts.googleapis.com" />
<link rel="stylesheet" href="assets/css/styles.css" />
<script src="/baremux/index.js" defer></script>
<script src="/uv/uv.bundle.js" defer></script>
<script src="/uv-static/uv.bundle.js" defer></script>
<script src="/uv/uv.config.js" defer></script>
<script src="/assets/js/register-sw.js" defer></script>
<script src="

View file

@ -30,7 +30,7 @@
"></script>
<script src="/baremux/index.js" defer></script>
<script src="/epoxy/index.js" defer></script>
<script src="/uv/uv.bundle.js" defer></script>
<script src="/uv-static/uv.bundle.js" defer></script>
<script src="/uv/uv.config.js" defer></script>
<script src="/assets/js/register-sw.js" defer></script>
</head>

View file

@ -26,7 +26,7 @@
<link rel="dns-prefetch" href="https://fonts.googleapis.com" />
<link rel="stylesheet" href="assets/css/styles.css" />
<script src="/baremux/index.js" defer></script>
<script src="/uv/uv.bundle.js" defer></script>
<script src="/uv-static/uv.bundle.js" defer></script>
<script src="/uv/uv.config.js" defer></script>
<script src="/assets/js/register-sw.js" defer></script>
<script src="

View file

@ -1,6 +1,6 @@
importScripts("uv.bundle.js");
importScripts("uv.config.js");
importScripts(__uv$config.sw || "uv.sw.js");
importScripts("/uv-static/uv.bundle.js");
importScripts("/uv/uv.config.js");
importScripts(__uv$config.sw || "/uv-static/uv.sw.js");
/*

View file

@ -1,6 +1,6 @@
importScripts("uv.bundle.js");
importScripts("uv.config.js");
importScripts(__uv$config.sw || "uv.sw.js");
importScripts("/uv-static/uv.bundle.js");
importScripts("/uv/uv.config.js");
importScripts(__uv$config.sw || "/uv-static/uv.sw.js");
const uv = new UVServiceWorker();

View file

@ -4,9 +4,9 @@ self.__uv$config = {
prefix: "/uv/service/",
encodeUrl: Ultraviolet.codec.xor.encode,
decodeUrl: Ultraviolet.codec.xor.decode,
handler: "/uv/uv.handler.js",
client: "/uv/uv.client.js",
bundle: "/uv/uv.bundle.js",
handler: "/uv-static/uv.handler.js",
client: "/uv-static/uv.client.js",
bundle: "/uv-static/uv.bundle.js",
config: "/uv/uv.config.js",
sw: "/uv/uv.sw.js",
sw: "/uv-static/uv.sw.js",
};