From c41ec4142c18a27da9457bde248dce950f8df917 Mon Sep 17 00:00:00 2001 From: MotorTruck1221 Date: Fri, 2 Aug 2024 00:58:45 -0600 Subject: [PATCH] Change fastify.mjs to server, express is still there for history reasons --- backend.js | 2 +- src/express.mjs | 136 ++++++++++++++++++++++++++++++++++++ src/fastify.mjs | 114 ------------------------------ src/server.mjs | 182 +++++++++++++++++++++--------------------------- 4 files changed, 217 insertions(+), 217 deletions(-) create mode 100644 src/express.mjs delete mode 100644 src/fastify.mjs diff --git a/backend.js b/backend.js index 1973b5ce..0062b594 100644 --- a/backend.js +++ b/backend.js @@ -1,3 +1,3 @@ (async () => { - await import("./src/fastify.mjs"); + await import("./src/server.mjs"); })(); diff --git a/src/express.mjs b/src/express.mjs new file mode 100644 index 00000000..20f8908e --- /dev/null +++ b/src/express.mjs @@ -0,0 +1,136 @@ +import { paintSource, tryReadFile } from "./randomization.mjs"; +import loadTemplates from "./templates.mjs"; +import pkg from "./routes.mjs"; +import { readFile } from "fs/promises"; +import path from "path"; +import express from "express"; +import helmet from "helmet"; +import http from "http"; +import createRammerhead from "rammerhead/src/server/index.js"; +import wisp from "wisp-server-node"; +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 { createBareServer } from "@tomphttp/bare-server-node"; + +const config = JSON.parse( + await readFile(new URL("./config.json", import.meta.url)) +), +{ pages, text404 } = pkg, +__dirname = path.resolve(), +port = process.env.PORT || config.port, +app = express(), +router = express.Router(), +// bare = createBareServer("/bare/"), +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}/, + +shouldRouteRh = req => { + const url = new URL(req.url, "http://0.0.0.0"); + return ( + rammerheadScopes.includes(url.pathname) || + rammerheadSession.test(url.pathname) + ); +}, + +routeRhRequest = (req, res) => { + rh.emit("request", req, res); +}, + +routeRhUpgrade = (req, socket, head) => { + rh.emit("upgrade", req, socket, head); +}, + +server = http.createServer((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); + } else if (req.url.endsWith("/wisp/")) { + wisp.routeRequest(req, socket, head); + } +}); + +// Apply Helmet middleware for security +app.use( + helmet({ + contentSecurityPolicy: false, // Disable CSP + }) +); + +// All website files are stored in the /views directory. +// This takes one of those files and displays it for a site visitor. +// Query strings like /?j are converted into paths like /views/hidden.html +// back here. Which query string converts to what is defined in routes.mjs. +router.get("/", async (req, res) => + res.send( + paintSource( + loadTemplates( + tryReadFile( + path.join(__dirname, + "views", +// Return the error page if the query is not found in +// routes.mjs. Also set index as the default page. + "/?".indexOf(req.url) ? pages[Object.keys(req.query)[0]] || "error.html" : pages.index + ) + ) + ) + ) + ) +); + + +app.use(router); +app.use(express.static(path.join(__dirname, "views"))); +app.use("/uv/", express.static(uvPath)); +app.use("/epoxy/", express.static(epoxyPath)); +app.use("/libcurl/", express.static(libcurlPath)); +app.use("/bareasmodule/", express.static(bareModulePath)); +app.use("/baremux/", express.static(baremuxPath)); + +app.disable("x-powered-by"); + +// Redundant code since 404 is handled elsewhere; left here as insurance. +app.use((req, res) => { + res.status(404).send(paintSource(loadTemplates(text404))); +}); + +server.listen(port); +console.log("Holy Unblocker is listening on port " + port + "."); diff --git a/src/fastify.mjs b/src/fastify.mjs deleted file mode 100644 index 9b9fad37..00000000 --- a/src/fastify.mjs +++ /dev/null @@ -1,114 +0,0 @@ -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' }); diff --git a/src/server.mjs b/src/server.mjs index 20f8908e..9b9fad37 100644 --- a/src/server.mjs +++ b/src/server.mjs @@ -1,31 +1,26 @@ -import { paintSource, tryReadFile } from "./randomization.mjs"; -import loadTemplates from "./templates.mjs"; -import pkg from "./routes.mjs"; -import { readFile } from "fs/promises"; -import path from "path"; -import express from "express"; -import helmet from "helmet"; -import http from "http"; +import Fastify from 'fastify'; +import { createServer } from 'node:http'; +import wisp from 'wisp-server-node'; import createRammerhead from "rammerhead/src/server/index.js"; -import wisp from "wisp-server-node"; 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 { createBareServer } from "@tomphttp/bare-server-node"; - -const config = JSON.parse( - await readFile(new URL("./config.json", import.meta.url)) -), -{ pages, text404 } = pkg, -__dirname = path.resolve(), -port = process.env.PORT || config.port, -app = express(), -router = express.Router(), -// bare = createBareServer("/bare/"), -rh = createRammerhead(); +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", @@ -43,94 +38,77 @@ const rammerheadScopes = [ "/api/shuffleDict", "/mainport", ]; - -const rammerheadSession = /^\/[a-z0-9]{32}/, - -shouldRouteRh = req => { +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) ); -}, - -routeRhRequest = (req, res) => { +} +const routeRhRequest = (req, res) => { rh.emit("request", req, res); -}, - -routeRhUpgrade = (req, socket, head) => { +} +const routeRhUpgrade = (req, socket, head) => { rh.emit("upgrade", req, socket, head); -}, +} -server = http.createServer((req, res) => { -/* - if (bare.shouldRoute(req)) { - bare.routeRequest(req, res); - } else -*/ - if (shouldRouteRh(req)) { - routeRhRequest(req, res); - } else { - app(req, res); - } +//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))))) }); -server.on("upgrade", (req, socket, head) => { -/* - if (bare.shouldRoute(req)) { - bare.routeUpgrade(req, socket, head); - } else -*/ - if (shouldRouteRh(req)) { - routeRhUpgrade(req, socket, head); - } else if (req.url.endsWith("/wisp/")) { - wisp.routeRequest(req, socket, head); - } -}); - -// Apply Helmet middleware for security -app.use( - helmet({ - contentSecurityPolicy: false, // Disable CSP - }) -); - -// All website files are stored in the /views directory. -// This takes one of those files and displays it for a site visitor. -// Query strings like /?j are converted into paths like /views/hidden.html -// back here. Which query string converts to what is defined in routes.mjs. -router.get("/", async (req, res) => - res.send( - paintSource( - loadTemplates( - tryReadFile( - path.join(__dirname, - "views", -// Return the error page if the query is not found in -// routes.mjs. Also set index as the default page. - "/?".indexOf(req.url) ? pages[Object.keys(req.query)[0]] || "error.html" : pages.index - ) - ) - ) - ) - ) -); - - -app.use(router); -app.use(express.static(path.join(__dirname, "views"))); -app.use("/uv/", express.static(uvPath)); -app.use("/epoxy/", express.static(epoxyPath)); -app.use("/libcurl/", express.static(libcurlPath)); -app.use("/bareasmodule/", express.static(bareModulePath)); -app.use("/baremux/", express.static(baremuxPath)); - -app.disable("x-powered-by"); - -// Redundant code since 404 is handled elsewhere; left here as insurance. -app.use((req, res) => { - res.status(404).send(paintSource(loadTemplates(text404))); -}); - -server.listen(port); -console.log("Holy Unblocker is listening on port " + port + "."); +//host is set as to avoid just being on localhost +app.listen({ port: port, host: '0.0.0.0' });