New shutdown script for cross-platform ease of use; implement host option in config file; minor code cleanup.

This commit is contained in:
00Fjongl 2024-08-07 10:10:19 -05:00
parent c21d775ee8
commit 92909e4ab7
7 changed files with 59 additions and 10 deletions

1
.gitignore vendored
View file

@ -13,3 +13,4 @@ debug.log
/lib/rammerhead/cache-js/*
!/lib/rammerhead/cache-js/.gitignore
/views/dist/*
/src/.shutdown

View file

@ -7,10 +7,10 @@
"scripts": {
"start": "npm install && npm run build && npm run manual-start",
"restart": "npm stop; npm run manual-start",
"stop": "pm2 stop ecosystem.config.js; pkill node || true",
"stop": "( pm2 stop ecosystem.config.js ) ; node shutdown.mjs",
"test": "npm run proxy-validator",
"monit": "pm2 monit",
"manual-start": "node ./read-config.mjs 'production' && ( pm2 start ecosystem.config.js --env production ) || ( node backend.js & pkill -n npm || true )",
"manual-start": "node read-config.mjs 'production' && ( pm2 start ecosystem.config.js --env production ) || ( node backend.js & pkill -n npm || true )",
"build": "sh ./build-assets.sh && cd lib/rammerhead && npm install && npm run build",
"start-test-server": "timeout 5 node backend.js; test $? -eq 124 && ( npm run manual-start & ) || exit 1",
"proxy-validator": "node proxyServiceValidator.js"

View file

@ -1,7 +1,13 @@
import { readFile } from 'node:fs/promises';
// Parse the config file to grab a property value from it.
let property = JSON.parse(await readFile(new URL("./src/config.json", import.meta.url)));
// Combine all descriptors (from command line arguments) to get the property value.
process.argv.slice(2).forEach(descriptor => {property = property[descriptor]});
// Return a boolean if it's true or false.
if (typeof property === "boolean") process.exitCode = +!property;
// Otherwise, return a stringified version to the stream.
else {
console.log(property);
process.exitCode = 0;

21
shutdown.mjs Normal file
View file

@ -0,0 +1,21 @@
import { readFile, writeFile, unlink } from 'node:fs/promises';
const config = Object.freeze(JSON.parse(await readFile(new URL("./src/config.json", import.meta.url))));
const serverUrl = (base => {
try {
base = new URL(config.host);
} catch (e) {
base = new URL("http://a");
base.host = config.host;
}
base.port = process.env.PORT || config.port;
return Object.freeze(base);
})();
const shutdown = new URL("./src/.shutdown", import.meta.url);
await writeFile(shutdown, "");
try {await fetch(new URL("/test-shutdown", serverUrl))}
catch (e) {await unlink(shutdown)}
process.exitCode = 0;

View file

@ -1,5 +1,6 @@
{
"title": "HU LTS",
"host": "0.0.0.0",
"port": "8080",
"minifyScripts": true,
"production": false

View file

@ -15,6 +15,7 @@ const text404 = readFileSync(
const pages = {
index: "index.html",
"manifest.json": "manifest.json",
/* Main */
documentation: "docs.html",
questions: "faq.html",

View file

@ -15,10 +15,22 @@ 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';
import { existsSync, unlinkSync } from 'node:fs';
const config = Object.freeze(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 serverUrl = (base => {
try {
base = new URL(config.host);
} catch (e) {
base = new URL("http://a");
base.host = config.host;
}
base.port = process.env.PORT || config.port;
return Object.freeze(base);
})();
console.log(serverUrl);
const shutdown = fileURLToPath(new URL("./.shutdown", import.meta.url));
const rh = createRammerhead();
const rammerheadScopes = [
@ -40,7 +52,7 @@ const rammerheadScopes = [
];
const rammerheadSession = /^\/[a-z0-9]{32}/;
const shouldRouteRh = req => {
const url = new URL(req.url, "http://0.0.0.0");
const url = new URL(req.url, serverUrl);
return (
rammerheadScopes.includes(url.pathname) ||
rammerheadSession.test(url.pathname)
@ -130,7 +142,7 @@ app.register(fastifyStatic, {
app.register(fastifyStatic, {
root: epoxyPath,
prefix: "/epoxy/",
decorateReply: false
decorateReply: false
});
app.register(fastifyStatic, {
root: libcurlPath,
@ -140,12 +152,12 @@ app.register(fastifyStatic, {
app.register(fastifyStatic, {
root: bareModulePath,
prefix: "/bareasmodule/",
decorateReply: false
decorateReply: false
});
app.register(fastifyStatic, {
root: baremuxPath,
prefix: "/baremux/",
decorateReply: false
decorateReply: false
});
@ -155,6 +167,13 @@ app.register(fastifyStatic, {
// back here. Which path converts to what is defined in routes.mjs.
app.get("/:file", (req, reply) => {
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]);
@ -200,5 +219,5 @@ app.setNotFoundHandler((req, reply) => {
// Configure host to your liking, but remember to tweak the Rammerhead IP
// as well above for any changes.
app.listen({ port: port /*, host: '0.0.0.0' */});
console.log("Holy Unblocker is listening on port " + port + ".");
app.listen({ port: serverUrl.port, host: serverUrl.hostname });
console.log("Holy Unblocker is listening on port " + serverUrl.port + ".");