mirror of
https://github.com/QuiteAFancyEmerald/Holy-Unblocker.git
synced 2025-05-12 11:30:01 -04:00
Improve 404 error handling
This commit is contained in:
parent
5ba6105943
commit
995183d239
4 changed files with 24 additions and 17 deletions
|
@ -30,7 +30,7 @@ const testGeneratedUrl = async (url, headers) => {
|
||||||
const testServerResponse = async () => {
|
const testServerResponse = async () => {
|
||||||
const endpoints = [
|
const endpoints = [
|
||||||
"http://localhost:8080/",
|
"http://localhost:8080/",
|
||||||
"http://localhost:8080/pathtonowhere",
|
"http://localhost:8080/test-404",
|
||||||
"http://localhost:8080/browsing",
|
"http://localhost:8080/browsing",
|
||||||
"http://localhost:8080/rammerhead",
|
"http://localhost:8080/rammerhead",
|
||||||
"http://localhost:8080/ultraviolet",
|
"http://localhost:8080/ultraviolet",
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
import pkg from "./routes.mjs";
|
import pkg from "./routes.mjs";
|
||||||
import { existsSync, readFileSync } from "fs";
|
import { existsSync, readFileSync } from "fs";
|
||||||
export { paintSource, tryReadFile };
|
export { paintSource, preloaded404, tryReadFile };
|
||||||
const {
|
const {
|
||||||
cookingInserts,
|
cookingInserts,
|
||||||
vegetables,
|
vegetables,
|
||||||
|
@ -37,9 +37,11 @@ const randomListItem = (lis) => () => lis[(Math.random() * lis.length) | 0],
|
||||||
// Apply the final obfuscation changes to an entire file.
|
// Apply the final obfuscation changes to an entire file.
|
||||||
paintSource = (str) =>
|
paintSource = (str) =>
|
||||||
insertCharset(hutaoInsert(versionInsert(insertCooking(cacheBusting(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.
|
// Grab the text content of a file. Ensure the file is a string.
|
||||||
tryReadFile = (file) =>
|
tryReadFile = (file) =>
|
||||||
existsSync(file + "") ? readFileSync(file + "", "utf8") : text404;
|
existsSync(file + "") ? readFileSync(file + "", "utf8") : preloaded404;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
// All of this is now old code.
|
// All of this is now old code.
|
||||||
|
|
|
@ -16,6 +16,7 @@ const text404 = readFileSync(
|
||||||
const pages = {
|
const pages = {
|
||||||
index: "index.html",
|
index: "index.html",
|
||||||
"manifest.json": "manifest.json",
|
"manifest.json": "manifest.json",
|
||||||
|
"test-404": "error.html",
|
||||||
/* Main */
|
/* Main */
|
||||||
documentation: "docs.html",
|
documentation: "docs.html",
|
||||||
questions: "faq.html",
|
questions: "faq.html",
|
||||||
|
@ -39,7 +40,7 @@ const pages = {
|
||||||
/* Misc */
|
/* Misc */
|
||||||
flash: "archive/gfiles/flash/index.html",
|
flash: "archive/gfiles/flash/index.html",
|
||||||
webretro: "archive/gfiles/rarch/index.html",
|
webretro: "archive/gfiles/rarch/index.html",
|
||||||
vos: "archive/vibeOS/index.html",
|
"vibe-os": "archive/vibeOS/index.html",
|
||||||
};
|
};
|
||||||
|
|
||||||
const externalPages = {
|
const externalPages = {
|
||||||
|
|
|
@ -9,10 +9,10 @@ import { baremuxPath } from "@mercuryworkshop/bare-mux/node";
|
||||||
import { uvPath } from "@titaniumnetwork-dev/ultraviolet";
|
import { uvPath } from "@titaniumnetwork-dev/ultraviolet";
|
||||||
import fastifyHelmet from '@fastify/helmet';
|
import fastifyHelmet from '@fastify/helmet';
|
||||||
import fastifyStatic from '@fastify/static';
|
import fastifyStatic from '@fastify/static';
|
||||||
import pkg from "./routes.mjs";
|
import pageRoutes from "./routes.mjs";
|
||||||
import { readFile } from 'node:fs/promises';
|
import { readFile } from 'node:fs/promises';
|
||||||
import path from 'node:path';
|
import path from 'node:path';
|
||||||
import { paintSource, tryReadFile } from './randomization.mjs';
|
import { paintSource, preloaded404, tryReadFile } from './randomization.mjs';
|
||||||
import loadTemplates from './templates.mjs';
|
import loadTemplates from './templates.mjs';
|
||||||
import { fileURLToPath } from 'node:url';
|
import { fileURLToPath } from 'node:url';
|
||||||
import { existsSync, unlinkSync } from 'node:fs';
|
import { existsSync, unlinkSync } from 'node:fs';
|
||||||
|
@ -24,7 +24,7 @@ const config = Object.freeze(
|
||||||
ecosystemConfig = Object.freeze(
|
ecosystemConfig = Object.freeze(
|
||||||
ecosystem.apps.find(app => app.name === "HolyUB") || ecosystem.apps[0]
|
ecosystem.apps.find(app => app.name === "HolyUB") || ecosystem.apps[0]
|
||||||
),
|
),
|
||||||
{ pages, externalPages, text404 } = pkg,
|
{ pages, externalPages } = pageRoutes,
|
||||||
__dirname = path.resolve();
|
__dirname = path.resolve();
|
||||||
|
|
||||||
// Record the server's location as a URL object, including its host and port.
|
// Record the server's location as a URL object, including its host and port.
|
||||||
|
@ -195,27 +195,33 @@ app.register(fastifyStatic, {
|
||||||
// This takes one of those files and displays it for a site visitor.
|
// 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
|
// Paths like /browsing are converted into paths like /views/pages/surf.html
|
||||||
// back here. Which path converts to what is defined in routes.mjs.
|
// 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.
|
// Testing for future features that need cookies to deliver alternate source files.
|
||||||
if (req.raw.rawHeaders.includes("Cookie"))
|
if (req.raw.rawHeaders.includes("Cookie"))
|
||||||
console.log(req.raw.rawHeaders[ req.raw.rawHeaders.indexOf("Cookie") + 1 ]);
|
console.log(req.raw.rawHeaders[ req.raw.rawHeaders.indexOf("Cookie") + 1 ]);
|
||||||
|
|
||||||
if (req.params.file in externalPages) {
|
const reqPath = req.params.path;
|
||||||
let externalRoute = externalPages[req.params.file];
|
|
||||||
|
if (reqPath in externalPages) {
|
||||||
|
let externalRoute = externalPages[reqPath];
|
||||||
if (typeof externalRoute !== "string") externalRoute = externalRoute.default;
|
if (typeof externalRoute !== "string") externalRoute = externalRoute.default;
|
||||||
return reply.redirect(externalRoute);
|
return reply.redirect(externalRoute);
|
||||||
}
|
}
|
||||||
|
|
||||||
// If a GET request is sent to /test-shutdown and a script-generated shutdown file
|
// If a GET request is sent to /test-shutdown and a script-generated shutdown file
|
||||||
// is present, gracefully shut the server down.
|
// 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.");
|
console.log("Holy Unblocker is shutting down.");
|
||||||
app.close();
|
app.close();
|
||||||
unlinkSync(shutdown);
|
unlinkSync(shutdown);
|
||||||
process.exitCode = 0;
|
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(
|
reply.type("text/html").send(
|
||||||
paintSource(
|
paintSource(
|
||||||
loadTemplates(
|
loadTemplates(
|
||||||
|
@ -223,11 +229,8 @@ app.get("/:file", (req, reply) => {
|
||||||
path.join(
|
path.join(
|
||||||
__dirname,
|
__dirname,
|
||||||
"views",
|
"views",
|
||||||
// Return the error page if the query is not found in routes.mjs.
|
// Set the index the as the default page.
|
||||||
// Also set the index the as the default page.
|
reqPath ? pages[reqPath] : pages.index
|
||||||
req.params.file
|
|
||||||
? pages[req.params.file] || "error.html"
|
|
||||||
: pages.index
|
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
@ -238,6 +241,7 @@ app.get("/:file", (req, reply) => {
|
||||||
app.get("/github/:redirect", (req, reply) => {
|
app.get("/github/:redirect", (req, reply) => {
|
||||||
if (req.params.redirect in externalPages.github)
|
if (req.params.redirect in externalPages.github)
|
||||||
reply.redirect(externalPages.github[req.params.redirect]);
|
reply.redirect(externalPages.github[req.params.redirect]);
|
||||||
|
else reply.code(404).type("text/html").send(preloaded404);
|
||||||
});
|
});
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -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.
|
// Set an error page for invalid paths outside the query string system.
|
||||||
app.setNotFoundHandler((req, reply) => {
|
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 });
|
app.listen({ port: serverUrl.port, host: serverUrl.hostname });
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue