mirror of
https://github.com/QuiteAFancyEmerald/Holy-Unblocker.git
synced 2025-05-12 19:40:02 -04:00
New shutdown script for cross-platform ease of use; implement host option in config file; minor code cleanup.
This commit is contained in:
parent
c21d775ee8
commit
92909e4ab7
7 changed files with 59 additions and 10 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -13,3 +13,4 @@ debug.log
|
||||||
/lib/rammerhead/cache-js/*
|
/lib/rammerhead/cache-js/*
|
||||||
!/lib/rammerhead/cache-js/.gitignore
|
!/lib/rammerhead/cache-js/.gitignore
|
||||||
/views/dist/*
|
/views/dist/*
|
||||||
|
/src/.shutdown
|
||||||
|
|
|
@ -7,10 +7,10 @@
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"start": "npm install && npm run build && npm run manual-start",
|
"start": "npm install && npm run build && npm run manual-start",
|
||||||
"restart": "npm stop; 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",
|
"test": "npm run proxy-validator",
|
||||||
"monit": "pm2 monit",
|
"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",
|
"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",
|
"start-test-server": "timeout 5 node backend.js; test $? -eq 124 && ( npm run manual-start & ) || exit 1",
|
||||||
"proxy-validator": "node proxyServiceValidator.js"
|
"proxy-validator": "node proxyServiceValidator.js"
|
||||||
|
|
|
@ -1,7 +1,13 @@
|
||||||
import { readFile } from 'node:fs/promises';
|
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)));
|
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]});
|
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;
|
if (typeof property === "boolean") process.exitCode = +!property;
|
||||||
|
// Otherwise, return a stringified version to the stream.
|
||||||
else {
|
else {
|
||||||
console.log(property);
|
console.log(property);
|
||||||
process.exitCode = 0;
|
process.exitCode = 0;
|
||||||
|
|
21
shutdown.mjs
Normal file
21
shutdown.mjs
Normal 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;
|
|
@ -1,5 +1,6 @@
|
||||||
{
|
{
|
||||||
"title": "HU LTS",
|
"title": "HU LTS",
|
||||||
|
"host": "0.0.0.0",
|
||||||
"port": "8080",
|
"port": "8080",
|
||||||
"minifyScripts": true,
|
"minifyScripts": true,
|
||||||
"production": false
|
"production": false
|
||||||
|
|
|
@ -15,6 +15,7 @@ const text404 = readFileSync(
|
||||||
|
|
||||||
const pages = {
|
const pages = {
|
||||||
index: "index.html",
|
index: "index.html",
|
||||||
|
"manifest.json": "manifest.json",
|
||||||
/* Main */
|
/* Main */
|
||||||
documentation: "docs.html",
|
documentation: "docs.html",
|
||||||
questions: "faq.html",
|
questions: "faq.html",
|
||||||
|
|
|
@ -15,10 +15,22 @@ import path from 'node:path';
|
||||||
import { paintSource, tryReadFile } from './randomization.mjs';
|
import { paintSource, 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 } 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 config = Object.freeze(JSON.parse(await readFile(new URL("./config.json", import.meta.url)))), { pages, text404 } = pkg;
|
||||||
const __dirname = path.resolve();
|
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 rh = createRammerhead();
|
||||||
const rammerheadScopes = [
|
const rammerheadScopes = [
|
||||||
|
@ -40,7 +52,7 @@ const rammerheadScopes = [
|
||||||
];
|
];
|
||||||
const rammerheadSession = /^\/[a-z0-9]{32}/;
|
const rammerheadSession = /^\/[a-z0-9]{32}/;
|
||||||
const shouldRouteRh = req => {
|
const shouldRouteRh = req => {
|
||||||
const url = new URL(req.url, "http://0.0.0.0");
|
const url = new URL(req.url, serverUrl);
|
||||||
return (
|
return (
|
||||||
rammerheadScopes.includes(url.pathname) ||
|
rammerheadScopes.includes(url.pathname) ||
|
||||||
rammerheadSession.test(url.pathname)
|
rammerheadSession.test(url.pathname)
|
||||||
|
@ -130,7 +142,7 @@ app.register(fastifyStatic, {
|
||||||
app.register(fastifyStatic, {
|
app.register(fastifyStatic, {
|
||||||
root: epoxyPath,
|
root: epoxyPath,
|
||||||
prefix: "/epoxy/",
|
prefix: "/epoxy/",
|
||||||
decorateReply: false
|
decorateReply: false
|
||||||
});
|
});
|
||||||
app.register(fastifyStatic, {
|
app.register(fastifyStatic, {
|
||||||
root: libcurlPath,
|
root: libcurlPath,
|
||||||
|
@ -140,12 +152,12 @@ app.register(fastifyStatic, {
|
||||||
app.register(fastifyStatic, {
|
app.register(fastifyStatic, {
|
||||||
root: bareModulePath,
|
root: bareModulePath,
|
||||||
prefix: "/bareasmodule/",
|
prefix: "/bareasmodule/",
|
||||||
decorateReply: false
|
decorateReply: false
|
||||||
});
|
});
|
||||||
app.register(fastifyStatic, {
|
app.register(fastifyStatic, {
|
||||||
root: baremuxPath,
|
root: baremuxPath,
|
||||||
prefix: "/baremux/",
|
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.
|
// back here. Which path converts to what is defined in routes.mjs.
|
||||||
app.get("/:file", (req, reply) => {
|
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.
|
// 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]);
|
||||||
|
@ -200,5 +219,5 @@ app.setNotFoundHandler((req, reply) => {
|
||||||
|
|
||||||
// Configure host to your liking, but remember to tweak the Rammerhead IP
|
// Configure host to your liking, but remember to tweak the Rammerhead IP
|
||||||
// as well above for any changes.
|
// as well above for any changes.
|
||||||
app.listen({ port: port /*, host: '0.0.0.0' */});
|
app.listen({ port: serverUrl.port, host: serverUrl.hostname });
|
||||||
console.log("Holy Unblocker is listening on port " + port + ".");
|
console.log("Holy Unblocker is listening on port " + serverUrl.port + ".");
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue