Skip to content

Commit

Permalink
Add customizable external redirects in /src/routes.mjs.
Browse files Browse the repository at this point in the history
  • Loading branch information
00Fjongl committed Aug 8, 2024
1 parent b6ced80 commit 5ba6105
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 51 deletions.
19 changes: 19 additions & 0 deletions src/routes.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,24 @@ const pages = {
vos: "archive/vibeOS/index.html",
};

const externalPages = {
github: {
default: "https://github.com/QuiteAFancyEmerald/Holy-Unblocker",
aos: "https://github.com/michalsnik/aos",
"bare-module": "https://github.com/motortruck1221/bare-as-module3",
"bare-mux": "https://github.com/MercuryWorkshop/bare-mux",
epoxy: "https://github.com/MercuryWorkshop/epoxy-tls",
fastify: "https://github.com/fastify/fastify",
"font-awesome": "https://github.com/FortAwesome/Font-Awesome",
"libcurl-js": "https://github.com/ading2210/libcurl.js",
"nord-theme": "https://github.com/nordtheme",
ultraviolet: "https://github.com/titaniumnetwork-dev/Ultraviolet",
wisp: "https://github.com/MercuryWorkshop/wisp-protocol"
},
"titaniumnetwork-documentation": "https://docs.titaniumnetwork.org",
"rammerhead-discord": "https://discord.gg/VNT4E7gN5Y"
};

const cookingInserts = insert.content,
vegetables = insert.keywords,
charRandom = insert.chars,
Expand All @@ -54,6 +72,7 @@ cacheBustList = {

export default {
pages,
externalPages,
text404,
cookingInserts,
vegetables,
Expand Down
82 changes: 47 additions & 35 deletions src/server.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const config = Object.freeze(
ecosystemConfig = Object.freeze(
ecosystem.apps.find(app => app.name === "HolyUB") || ecosystem.apps[0]
),
{ pages, text404 } = pkg,
{ pages, externalPages, text404 } = pkg,
__dirname = path.resolve();

// Record the server's location as a URL object, including its host and port.
Expand Down Expand Up @@ -67,11 +67,13 @@ const rammerheadScopes = [

const rammerheadSession = /^\/[a-z0-9]{32}/,
shouldRouteRh = req => {
const url = new URL(req.url, serverUrl);
return (
rammerheadScopes.includes(url.pathname) ||
rammerheadSession.test(url.pathname)
);
try {
const url = new URL(req.url, serverUrl);
return (
rammerheadScopes.includes(url.pathname) ||
rammerheadSession.test(url.pathname)
);
} catch (e) {return false}
},
routeRhRequest = (req, res) => {
rh.emit("request", req, res);
Expand All @@ -97,7 +99,12 @@ const serverFactory = (handler) => {
};

// Set logger to true for logs
const app = Fastify({ logger: false, serverFactory: serverFactory });
const app = Fastify({
ignoreDuplicateSlashes: true,
ignoreTrailingSlash: true,
logger: false,
serverFactory: serverFactory
});

// Apply Helmet middleware for security
app.register(fastifyHelmet, {
Expand Down Expand Up @@ -190,44 +197,49 @@ app.register(fastifyStatic, {
// back here. Which path converts to what is defined in routes.mjs.
app.get("/:file", (req, reply) => {

// 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)) {
console.log("Holy Unblocker is shutting down.");
app.close();
unlinkSync(shutdown);
process.exitCode = 0;
}

// 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 ]);

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
if (req.params.file in externalPages) {
let externalRoute = externalPages[req.params.file];
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)) {
console.log("Holy Unblocker is shutting down.");
app.close();
unlinkSync(shutdown);
process.exitCode = 0;
}

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
)
)
)
)
)
);
);
});

// Ignore trailing slashes for the above path handling.
app.get("/:file/", (req, reply) => {
reply.redirect("/" + req.params.file);
app.get("/github/:redirect", (req, reply) => {
if (req.params.redirect in externalPages.github)
reply.redirect(externalPages.github[req.params.redirect]);
});


/*
Testing for future restructuring of this config file.
Expand All @@ -240,7 +252,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(paintSource(loadTemplates(tryReadFile(path.join(__dirname, "views/error.html")))));
reply.code(404).type("text/html").send(text404);
});

app.listen({ port: serverUrl.port, host: serverUrl.hostname });
Expand Down
4 changes: 2 additions & 2 deletions views/assets/js/common-16451543478.js
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ addEventListener("DOMContentLoaded", async () => {
"h5-nav": "h5-nav"
};

for (let [listId, filename] of Object.entries(navLists)) {
for (const [listId, filename] of Object.entries(navLists)) {

let navList = document.getElementById(listId);

Expand Down Expand Up @@ -510,7 +510,7 @@ addEventListener("DOMContentLoaded", async () => {
for (let i = 0; i < data.length; i++) {
// Load each item as an anchor tag with an image, heading,
// description, and click event listener.
let item = data[i],
const item = data[i],
a = document.createElement("a"),
img = document.createElement("img"),
title = document.createElement("h3"),
Expand Down
28 changes: 14 additions & 14 deletions views/pages/misc/deobf/footer.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,28 @@ <h3>Dependencies</h3>
<li><a
target="_blank"
rel="noopener noreferrer"
href="https://github.com/fastify/fastify"
href="/github/fastify"
title="Fastify - Fast and low overhead web framework, for Node.js "
>Fastify</a
></li>
<li><a
target="_blank"
rel="noopener noreferrer"
href="https://github.com/nordtheme"
href="/github/nord-theme"
title="Nord Theme - An arctic, north-bluish color palette."
>Nord Theme</a
></li>
<li><a
target="_blank"
rel="noopener noreferrer"
href="https://github.com/michalsnik/aos"
href="/github/aos"
title="AOS - Animate on scroll library "
>AOS</a
></li>
<li><a
target="_blank"
rel="noopener noreferrer"
href="https://github.com/FortAwesome/Font-Awesome"
href="/github/font-awesome"
title="Font Awesome - The iconic SVG, font, and CSS toolkit"
>Font Awesome </a
></li>
Expand All @@ -43,21 +43,21 @@ <h3>Transports</h3>
<li><a
target="_blank"
rel="noopener noreferrer"
href="https://github.com/MercuryWorkshop/epoxy-tls"
href="/github/epoxy"
title="Epoxy - Epoxy is an encrypted proxy for browser javascript. It allows you to make requests that bypass CORS without compromising security, by running SSL/TLS inside webassembly."
>Epoxy</a
></li>
<li><a
target="_blank"
rel="noopener noreferrer"
href="https://github.com/ading2210/libcurl.js"
href="/github/libcurl-js"
title="A port of libcurl to WebAssembly, for proxying HTTPS requests from the browser with full TLS encryption"
>Libcurl</a
></li>
<li><a
target="_blank"
rel="noopener noreferrer"
href="https://github.com/motortruck1221/bare-as-module3"
href="/github/bare-module"
title="Bare Module - Tomp Bare Client as a bare-mux module"
>Bare Module</a
></li>
Expand All @@ -69,28 +69,28 @@ <h3>Services</h3>
<li><a
target="_blank"
rel="noopener noreferrer"
href="https://github.com/titaniumnetwork-dev/Ultraviolet"
href="/github/ultraviolet"
title="Ultraviolet - Explore Ultraviolet"
>Ultraviolet</a
></li>
<li><a
target="_blank"
rel="noopener noreferrer"
href="https://github.com/MercuryWorkshop/wisp-protocol"
href="/github/wisp"
title="Wisp - Wisp is a low-overhead, easy to implement protocol for proxying multiple TCP/UDP sockets over a single websocket. "
>Wisp Protocol</a
></li>
<li><a
target="_blank"
rel="noopener noreferrer"
href="https://discord.gg/VNT4E7gN5Y"
href="/rammerhead-discord"
title="Rammerhead - Join Rammerhead on Discord"
>Rammerhead</a
></li>
<li><a
target="_blank"
rel="noopener noreferrer"
href="https://github.com/MercuryWorkshop/bare-mux"
href="/github/bare-mux"
title="BareMux - A system for managing http transports in a project such as Ultraviolet"
>Bare-Mux</a
></li>
Expand All @@ -102,7 +102,7 @@ <h3>About</h3>
<li><a
target="_blank"
rel="noopener noreferrer"
href="https://github.com/QuiteAFancyEmerald/Holy-Unblocker"
href="/github"
title="Holy Unblocker GitHub Repository"
>GitHub</a
></li>
Expand All @@ -119,15 +119,15 @@ <h3>About</h3>
target="_blank"
rel="noopener noreferrer"
class="soc-github"
href="https://github.com/QuiteAFancyEmerald/Holy-Unblocker"
href="/github"
title="Follow Holy Unblocker on GitHub"
><i class="fab fa-github" aria-hidden="true"></i
></a>
<a
target="_blank"
rel="noopener noreferrer"
class="soc-patreon"
href="https://docs.titaniumnetwork.org"
href="/titaniumnetwork-documentation"
title="View Documentation on Titanium Network"
><i class="fas fa-file-code" aria-hidden="true"></i
></a>
Expand Down

0 comments on commit 5ba6105

Please sign in to comment.