Skip to content

Commit

Permalink
Improve 404 error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
00Fjongl committed Aug 8, 2024
1 parent 5ba6105 commit 995183d
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 17 deletions.
2 changes: 1 addition & 1 deletion proxyServiceValidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const testGeneratedUrl = async (url, headers) => {
const testServerResponse = async () => {
const endpoints = [
"http://localhost:8080/",
"http://localhost:8080/pathtonowhere",
"http://localhost:8080/test-404",
"http://localhost:8080/browsing",
"http://localhost:8080/rammerhead",
"http://localhost:8080/ultraviolet",
Expand Down
6 changes: 4 additions & 2 deletions src/randomization.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pkg from "./routes.mjs";
import { existsSync, readFileSync } from "fs";
export { paintSource, tryReadFile };
export { paintSource, preloaded404, tryReadFile };
const {
cookingInserts,
vegetables,
Expand Down Expand Up @@ -37,9 +37,11 @@ const randomListItem = (lis) => () => lis[(Math.random() * lis.length) | 0],
// Apply the final obfuscation changes to an entire file.
paintSource = (str) =>
insertCharset(hutaoInsert(versionInsert(insertCooking(cacheBusting(str))))),
// Use this instead of text404 for a preloaded error page.
preloaded404 = paintSource(text404),
// Grab the text content of a file. Ensure the file is a string.
tryReadFile = (file) =>
existsSync(file + "") ? readFileSync(file + "", "utf8") : text404;
existsSync(file + "") ? readFileSync(file + "", "utf8") : preloaded404;

/*
// All of this is now old code.
Expand Down
3 changes: 2 additions & 1 deletion src/routes.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const text404 = readFileSync(
const pages = {
index: "index.html",
"manifest.json": "manifest.json",
"test-404": "error.html",
/* Main */
documentation: "docs.html",
questions: "faq.html",
Expand All @@ -39,7 +40,7 @@ const pages = {
/* Misc */
flash: "archive/gfiles/flash/index.html",
webretro: "archive/gfiles/rarch/index.html",
vos: "archive/vibeOS/index.html",
"vibe-os": "archive/vibeOS/index.html",
};

const externalPages = {
Expand Down
30 changes: 17 additions & 13 deletions src/server.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ 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 pageRoutes from "./routes.mjs";
import { readFile } from 'node:fs/promises';
import path from 'node:path';
import { paintSource, tryReadFile } from './randomization.mjs';
import { paintSource, preloaded404, tryReadFile } from './randomization.mjs';
import loadTemplates from './templates.mjs';
import { fileURLToPath } from 'node:url';
import { existsSync, unlinkSync } from 'node:fs';
Expand All @@ -24,7 +24,7 @@ const config = Object.freeze(
ecosystemConfig = Object.freeze(
ecosystem.apps.find(app => app.name === "HolyUB") || ecosystem.apps[0]
),
{ pages, externalPages, text404 } = pkg,
{ pages, externalPages } = pageRoutes,
__dirname = path.resolve();

// Record the server's location as a URL object, including its host and port.
Expand Down Expand Up @@ -195,39 +195,42 @@ app.register(fastifyStatic, {
// This takes one of those files and displays it for a site visitor.
// Paths like /browsing are converted into paths like /views/pages/surf.html
// back here. Which path converts to what is defined in routes.mjs.
app.get("/:file", (req, reply) => {
app.get("/:path", (req, reply) => {

// Testing for future features that need cookies to deliver alternate source files.
if (req.raw.rawHeaders.includes("Cookie"))
console.log(req.raw.rawHeaders[ req.raw.rawHeaders.indexOf("Cookie") + 1 ]);

if (req.params.file in externalPages) {
let externalRoute = externalPages[req.params.file];
const reqPath = req.params.path;

if (reqPath in externalPages) {
let externalRoute = externalPages[reqPath];
if (typeof externalRoute !== "string") externalRoute = externalRoute.default;
return reply.redirect(externalRoute);
}

// If a GET request is sent to /test-shutdown and a script-generated shutdown file
// is present, gracefully shut the server down.
if (req.params.file === "test-shutdown" && existsSync(shutdown)) {
if (reqPath === "test-shutdown" && existsSync(shutdown)) {
console.log("Holy Unblocker is shutting down.");
app.close();
unlinkSync(shutdown);
process.exitCode = 0;
}

// Return the error page if the query is not found in routes.mjs.
if (reqPath && !(reqPath in pages))
return reply.code(404).type("text/html").send(preloaded404);

reply.type("text/html").send(
paintSource(
loadTemplates(
tryReadFile(
path.join(
__dirname,
"views",
// Return the error page if the query is not found in routes.mjs.
// Also set the index the as the default page.
req.params.file
? pages[req.params.file] || "error.html"
: pages.index
// Set the index the as the default page.
reqPath ? pages[reqPath] : pages.index
)
)
)
Expand All @@ -238,6 +241,7 @@ app.get("/:file", (req, reply) => {
app.get("/github/:redirect", (req, reply) => {
if (req.params.redirect in externalPages.github)
reply.redirect(externalPages.github[req.params.redirect]);
else reply.code(404).type("text/html").send(preloaded404);
});

/*
Expand All @@ -252,7 +256,7 @@ app.get("/assets/js/uv/uv.config.js", (req, reply) => {

// Set an error page for invalid paths outside the query string system.
app.setNotFoundHandler((req, reply) => {
reply.code(404).type("text/html").send(text404);
reply.code(404).type("text/html").send(preloaded404);
});

app.listen({ port: serverUrl.port, host: serverUrl.hostname });
Expand Down

0 comments on commit 995183d

Please sign in to comment.