mirror of
https://github.com/QuiteAFancyEmerald/Holy-Unblocker.git
synced 2025-05-15 04:40:01 -04:00
Change fastify.mjs to server, express is still there for history reasons
This commit is contained in:
parent
234a9a054b
commit
c41ec4142c
4 changed files with 217 additions and 217 deletions
|
@ -1,3 +1,3 @@
|
||||||
(async () => {
|
(async () => {
|
||||||
await import("./src/fastify.mjs");
|
await import("./src/server.mjs");
|
||||||
})();
|
})();
|
||||||
|
|
136
src/express.mjs
Normal file
136
src/express.mjs
Normal file
|
@ -0,0 +1,136 @@
|
||||||
|
import { paintSource, tryReadFile } from "./randomization.mjs";
|
||||||
|
import loadTemplates from "./templates.mjs";
|
||||||
|
import pkg from "./routes.mjs";
|
||||||
|
import { readFile } from "fs/promises";
|
||||||
|
import path from "path";
|
||||||
|
import express from "express";
|
||||||
|
import helmet from "helmet";
|
||||||
|
import http from "http";
|
||||||
|
import createRammerhead from "rammerhead/src/server/index.js";
|
||||||
|
import wisp from "wisp-server-node";
|
||||||
|
import { epoxyPath } from "@mercuryworkshop/epoxy-transport";
|
||||||
|
import { libcurlPath } from "@mercuryworkshop/libcurl-transport";
|
||||||
|
import { bareModulePath } from "@mercuryworkshop/bare-as-module3";
|
||||||
|
import { baremuxPath } from "@mercuryworkshop/bare-mux/node";
|
||||||
|
import { uvPath } from "@titaniumnetwork-dev/ultraviolet";
|
||||||
|
// import { createBareServer } from "@tomphttp/bare-server-node";
|
||||||
|
|
||||||
|
const config = JSON.parse(
|
||||||
|
await readFile(new URL("./config.json", import.meta.url))
|
||||||
|
),
|
||||||
|
{ pages, text404 } = pkg,
|
||||||
|
__dirname = path.resolve(),
|
||||||
|
port = process.env.PORT || config.port,
|
||||||
|
app = express(),
|
||||||
|
router = express.Router(),
|
||||||
|
// bare = createBareServer("/bare/"),
|
||||||
|
rh = createRammerhead();
|
||||||
|
|
||||||
|
const rammerheadScopes = [
|
||||||
|
"/rammerhead.js",
|
||||||
|
"/hammerhead.js",
|
||||||
|
"/transport-worker.js",
|
||||||
|
"/task.js",
|
||||||
|
"/iframe-task.js",
|
||||||
|
"/worker-hammerhead.js",
|
||||||
|
"/messaging",
|
||||||
|
"/sessionexists",
|
||||||
|
"/deletesession",
|
||||||
|
"/newsession",
|
||||||
|
"/editsession",
|
||||||
|
"/needpassword",
|
||||||
|
"/syncLocalStorage",
|
||||||
|
"/api/shuffleDict",
|
||||||
|
"/mainport",
|
||||||
|
];
|
||||||
|
|
||||||
|
const rammerheadSession = /^\/[a-z0-9]{32}/,
|
||||||
|
|
||||||
|
shouldRouteRh = req => {
|
||||||
|
const url = new URL(req.url, "http://0.0.0.0");
|
||||||
|
return (
|
||||||
|
rammerheadScopes.includes(url.pathname) ||
|
||||||
|
rammerheadSession.test(url.pathname)
|
||||||
|
);
|
||||||
|
},
|
||||||
|
|
||||||
|
routeRhRequest = (req, res) => {
|
||||||
|
rh.emit("request", req, res);
|
||||||
|
},
|
||||||
|
|
||||||
|
routeRhUpgrade = (req, socket, head) => {
|
||||||
|
rh.emit("upgrade", req, socket, head);
|
||||||
|
},
|
||||||
|
|
||||||
|
server = http.createServer((req, res) => {
|
||||||
|
/*
|
||||||
|
if (bare.shouldRoute(req)) {
|
||||||
|
bare.routeRequest(req, res);
|
||||||
|
} else
|
||||||
|
*/
|
||||||
|
if (shouldRouteRh(req)) {
|
||||||
|
routeRhRequest(req, res);
|
||||||
|
} else {
|
||||||
|
app(req, res);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
server.on("upgrade", (req, socket, head) => {
|
||||||
|
/*
|
||||||
|
if (bare.shouldRoute(req)) {
|
||||||
|
bare.routeUpgrade(req, socket, head);
|
||||||
|
} else
|
||||||
|
*/
|
||||||
|
if (shouldRouteRh(req)) {
|
||||||
|
routeRhUpgrade(req, socket, head);
|
||||||
|
} else if (req.url.endsWith("/wisp/")) {
|
||||||
|
wisp.routeRequest(req, socket, head);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Apply Helmet middleware for security
|
||||||
|
app.use(
|
||||||
|
helmet({
|
||||||
|
contentSecurityPolicy: false, // Disable CSP
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
|
// All website files are stored in the /views directory.
|
||||||
|
// This takes one of those files and displays it for a site visitor.
|
||||||
|
// Query strings like /?j are converted into paths like /views/hidden.html
|
||||||
|
// back here. Which query string converts to what is defined in routes.mjs.
|
||||||
|
router.get("/", async (req, res) =>
|
||||||
|
res.send(
|
||||||
|
paintSource(
|
||||||
|
loadTemplates(
|
||||||
|
tryReadFile(
|
||||||
|
path.join(__dirname,
|
||||||
|
"views",
|
||||||
|
// Return the error page if the query is not found in
|
||||||
|
// routes.mjs. Also set index as the default page.
|
||||||
|
"/?".indexOf(req.url) ? pages[Object.keys(req.query)[0]] || "error.html" : pages.index
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
app.use(router);
|
||||||
|
app.use(express.static(path.join(__dirname, "views")));
|
||||||
|
app.use("/uv/", express.static(uvPath));
|
||||||
|
app.use("/epoxy/", express.static(epoxyPath));
|
||||||
|
app.use("/libcurl/", express.static(libcurlPath));
|
||||||
|
app.use("/bareasmodule/", express.static(bareModulePath));
|
||||||
|
app.use("/baremux/", express.static(baremuxPath));
|
||||||
|
|
||||||
|
app.disable("x-powered-by");
|
||||||
|
|
||||||
|
// Redundant code since 404 is handled elsewhere; left here as insurance.
|
||||||
|
app.use((req, res) => {
|
||||||
|
res.status(404).send(paintSource(loadTemplates(text404)));
|
||||||
|
});
|
||||||
|
|
||||||
|
server.listen(port);
|
||||||
|
console.log("Holy Unblocker is listening on port " + port + ".");
|
114
src/fastify.mjs
114
src/fastify.mjs
|
@ -1,114 +0,0 @@
|
||||||
import Fastify from 'fastify';
|
|
||||||
import { createServer } from 'node:http';
|
|
||||||
import wisp from 'wisp-server-node';
|
|
||||||
import createRammerhead from "rammerhead/src/server/index.js";
|
|
||||||
import { epoxyPath } from "@mercuryworkshop/epoxy-transport";
|
|
||||||
import { libcurlPath } from "@mercuryworkshop/libcurl-transport";
|
|
||||||
import { bareModulePath } from "@mercuryworkshop/bare-as-module3";
|
|
||||||
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 { readFile } from 'node:fs/promises';
|
|
||||||
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';
|
|
||||||
const config = 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 rh = createRammerhead();
|
|
||||||
const rammerheadScopes = [
|
|
||||||
"/rammerhead.js",
|
|
||||||
"/hammerhead.js",
|
|
||||||
"/transport-worker.js",
|
|
||||||
"/task.js",
|
|
||||||
"/iframe-task.js",
|
|
||||||
"/worker-hammerhead.js",
|
|
||||||
"/messaging",
|
|
||||||
"/sessionexists",
|
|
||||||
"/deletesession",
|
|
||||||
"/newsession",
|
|
||||||
"/editsession",
|
|
||||||
"/needpassword",
|
|
||||||
"/syncLocalStorage",
|
|
||||||
"/api/shuffleDict",
|
|
||||||
"/mainport",
|
|
||||||
];
|
|
||||||
const rammerheadSession = /^\/[a-z0-9]{32}/;
|
|
||||||
const shouldRouteRh = req => {
|
|
||||||
const url = new URL(req.url, "http://0.0.0.0");
|
|
||||||
return (
|
|
||||||
rammerheadScopes.includes(url.pathname) ||
|
|
||||||
rammerheadSession.test(url.pathname)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
const routeRhRequest = (req, res) => {
|
|
||||||
rh.emit("request", req, res);
|
|
||||||
}
|
|
||||||
const routeRhUpgrade = (req, socket, head) => {
|
|
||||||
rh.emit("upgrade", req, socket, head);
|
|
||||||
}
|
|
||||||
|
|
||||||
//create a server factory for RH, and wisp (and bare if you please)
|
|
||||||
const serverFactory = (handler) => {
|
|
||||||
return createServer()
|
|
||||||
.on('request', (req, res) => {
|
|
||||||
if (shouldRouteRh(req)) {
|
|
||||||
routeRhRequest(req, res);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
handler(req, res);
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.on('upgrade', (req, socket, head) => {
|
|
||||||
if (shouldRouteRh(req)) {
|
|
||||||
routeRhUpgrade(req, socket, head);
|
|
||||||
}
|
|
||||||
else if (req.url.endsWith('/wisp/')) {
|
|
||||||
wisp.routeRequest(req, socket, head);
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
//set logger to true for logs
|
|
||||||
const app = Fastify({ logger: false, serverFactory: serverFactory });
|
|
||||||
app.register(fastifyStatic, {
|
|
||||||
root: fileURLToPath(new URL('../views', import.meta.url)),
|
|
||||||
});
|
|
||||||
app.register(fastifyStatic, {
|
|
||||||
root: uvPath,
|
|
||||||
//due to how Fastify works, we have to have the uvPath live on a different prefix then the one in /views/
|
|
||||||
prefix: "/uv-static/",
|
|
||||||
decorateReply: false
|
|
||||||
});
|
|
||||||
app.register(fastifyStatic, {
|
|
||||||
root: epoxyPath,
|
|
||||||
prefix: "/epoxy/",
|
|
||||||
decorateReply: false
|
|
||||||
});
|
|
||||||
app.register(fastifyStatic, {
|
|
||||||
root: libcurlPath,
|
|
||||||
prefix: "/libcurl/",
|
|
||||||
decorateReply: false
|
|
||||||
});
|
|
||||||
app.register(fastifyStatic, {
|
|
||||||
root: bareModulePath,
|
|
||||||
prefix: "/bareasmodule/",
|
|
||||||
decorateReply: false
|
|
||||||
});
|
|
||||||
app.register(fastifyStatic, {
|
|
||||||
root: baremuxPath,
|
|
||||||
prefix: "/baremux/",
|
|
||||||
decorateReply: false
|
|
||||||
});
|
|
||||||
app.get("/", function(req, reply) {
|
|
||||||
reply.type('html');
|
|
||||||
reply.send(paintSource(loadTemplates(tryReadFile(path.join(__dirname, "views", "/?".indexOf(req.url) ? pages[Object.keys(req.query)[0]] || "error.html" : pages.index)))))
|
|
||||||
});
|
|
||||||
|
|
||||||
//host is set as to avoid just being on localhost
|
|
||||||
app.listen({ port: port, host: '0.0.0.0' });
|
|
164
src/server.mjs
164
src/server.mjs
|
@ -1,31 +1,26 @@
|
||||||
import { paintSource, tryReadFile } from "./randomization.mjs";
|
import Fastify from 'fastify';
|
||||||
import loadTemplates from "./templates.mjs";
|
import { createServer } from 'node:http';
|
||||||
import pkg from "./routes.mjs";
|
import wisp from 'wisp-server-node';
|
||||||
import { readFile } from "fs/promises";
|
|
||||||
import path from "path";
|
|
||||||
import express from "express";
|
|
||||||
import helmet from "helmet";
|
|
||||||
import http from "http";
|
|
||||||
import createRammerhead from "rammerhead/src/server/index.js";
|
import createRammerhead from "rammerhead/src/server/index.js";
|
||||||
import wisp from "wisp-server-node";
|
|
||||||
import { epoxyPath } from "@mercuryworkshop/epoxy-transport";
|
import { epoxyPath } from "@mercuryworkshop/epoxy-transport";
|
||||||
import { libcurlPath } from "@mercuryworkshop/libcurl-transport";
|
import { libcurlPath } from "@mercuryworkshop/libcurl-transport";
|
||||||
import { bareModulePath } from "@mercuryworkshop/bare-as-module3";
|
import { bareModulePath } from "@mercuryworkshop/bare-as-module3";
|
||||||
import { baremuxPath } from "@mercuryworkshop/bare-mux/node";
|
import { baremuxPath } from "@mercuryworkshop/bare-mux/node";
|
||||||
import { uvPath } from "@titaniumnetwork-dev/ultraviolet";
|
import { uvPath } from "@titaniumnetwork-dev/ultraviolet";
|
||||||
// import { createBareServer } from "@tomphttp/bare-server-node";
|
import fastifyHelmet from '@fastify/helmet';
|
||||||
|
import fastifyStatic from '@fastify/static';
|
||||||
const config = JSON.parse(
|
import pkg from "./routes.mjs";
|
||||||
await readFile(new URL("./config.json", import.meta.url))
|
import { readFile } from 'node:fs/promises';
|
||||||
),
|
import path from 'node:path';
|
||||||
{ pages, text404 } = pkg,
|
import { paintSource, tryReadFile } from './randomization.mjs';
|
||||||
__dirname = path.resolve(),
|
import loadTemplates from './templates.mjs';
|
||||||
port = process.env.PORT || config.port,
|
import { fileURLToPath } from 'node:url';
|
||||||
app = express(),
|
import { existsSync } from 'node:fs';
|
||||||
router = express.Router(),
|
const config = JSON.parse(await readFile(new URL("./config.json", import.meta.url))), { pages, text404 } = pkg;
|
||||||
// bare = createBareServer("/bare/"),
|
const __dirname = path.resolve();
|
||||||
rh = createRammerhead();
|
const port = process.env.PORT || config.port;
|
||||||
|
|
||||||
|
const rh = createRammerhead();
|
||||||
const rammerheadScopes = [
|
const rammerheadScopes = [
|
||||||
"/rammerhead.js",
|
"/rammerhead.js",
|
||||||
"/hammerhead.js",
|
"/hammerhead.js",
|
||||||
|
@ -43,94 +38,77 @@ const rammerheadScopes = [
|
||||||
"/api/shuffleDict",
|
"/api/shuffleDict",
|
||||||
"/mainport",
|
"/mainport",
|
||||||
];
|
];
|
||||||
|
const rammerheadSession = /^\/[a-z0-9]{32}/;
|
||||||
const rammerheadSession = /^\/[a-z0-9]{32}/,
|
const shouldRouteRh = req => {
|
||||||
|
|
||||||
shouldRouteRh = req => {
|
|
||||||
const url = new URL(req.url, "http://0.0.0.0");
|
const url = new URL(req.url, "http://0.0.0.0");
|
||||||
return (
|
return (
|
||||||
rammerheadScopes.includes(url.pathname) ||
|
rammerheadScopes.includes(url.pathname) ||
|
||||||
rammerheadSession.test(url.pathname)
|
rammerheadSession.test(url.pathname)
|
||||||
);
|
);
|
||||||
},
|
}
|
||||||
|
const routeRhRequest = (req, res) => {
|
||||||
routeRhRequest = (req, res) => {
|
|
||||||
rh.emit("request", req, res);
|
rh.emit("request", req, res);
|
||||||
},
|
}
|
||||||
|
const routeRhUpgrade = (req, socket, head) => {
|
||||||
routeRhUpgrade = (req, socket, head) => {
|
|
||||||
rh.emit("upgrade", req, socket, head);
|
rh.emit("upgrade", req, socket, head);
|
||||||
},
|
}
|
||||||
|
|
||||||
server = http.createServer((req, res) => {
|
//create a server factory for RH, and wisp (and bare if you please)
|
||||||
/*
|
const serverFactory = (handler) => {
|
||||||
if (bare.shouldRoute(req)) {
|
return createServer()
|
||||||
bare.routeRequest(req, res);
|
.on('request', (req, res) => {
|
||||||
} else
|
|
||||||
*/
|
|
||||||
if (shouldRouteRh(req)) {
|
if (shouldRouteRh(req)) {
|
||||||
routeRhRequest(req, res);
|
routeRhRequest(req, res);
|
||||||
} else {
|
|
||||||
app(req, res);
|
|
||||||
}
|
}
|
||||||
});
|
else {
|
||||||
|
handler(req, res);
|
||||||
server.on("upgrade", (req, socket, head) => {
|
}
|
||||||
/*
|
})
|
||||||
if (bare.shouldRoute(req)) {
|
.on('upgrade', (req, socket, head) => {
|
||||||
bare.routeUpgrade(req, socket, head);
|
|
||||||
} else
|
|
||||||
*/
|
|
||||||
if (shouldRouteRh(req)) {
|
if (shouldRouteRh(req)) {
|
||||||
routeRhUpgrade(req, socket, head);
|
routeRhUpgrade(req, socket, head);
|
||||||
} else if (req.url.endsWith("/wisp/")) {
|
}
|
||||||
|
else if (req.url.endsWith('/wisp/')) {
|
||||||
wisp.routeRequest(req, socket, head);
|
wisp.routeRequest(req, socket, head);
|
||||||
}
|
}
|
||||||
});
|
|
||||||
|
|
||||||
// Apply Helmet middleware for security
|
|
||||||
app.use(
|
|
||||||
helmet({
|
|
||||||
contentSecurityPolicy: false, // Disable CSP
|
|
||||||
})
|
})
|
||||||
);
|
}
|
||||||
|
|
||||||
// All website files are stored in the /views directory.
|
//set logger to true for logs
|
||||||
// This takes one of those files and displays it for a site visitor.
|
const app = Fastify({ logger: false, serverFactory: serverFactory });
|
||||||
// Query strings like /?j are converted into paths like /views/hidden.html
|
app.register(fastifyStatic, {
|
||||||
// back here. Which query string converts to what is defined in routes.mjs.
|
root: fileURLToPath(new URL('../views', import.meta.url)),
|
||||||
router.get("/", async (req, res) =>
|
});
|
||||||
res.send(
|
app.register(fastifyStatic, {
|
||||||
paintSource(
|
root: uvPath,
|
||||||
loadTemplates(
|
//due to how Fastify works, we have to have the uvPath live on a different prefix then the one in /views/
|
||||||
tryReadFile(
|
prefix: "/uv-static/",
|
||||||
path.join(__dirname,
|
decorateReply: false
|
||||||
"views",
|
});
|
||||||
// Return the error page if the query is not found in
|
app.register(fastifyStatic, {
|
||||||
// routes.mjs. Also set index as the default page.
|
root: epoxyPath,
|
||||||
"/?".indexOf(req.url) ? pages[Object.keys(req.query)[0]] || "error.html" : pages.index
|
prefix: "/epoxy/",
|
||||||
)
|
decorateReply: false
|
||||||
)
|
});
|
||||||
)
|
app.register(fastifyStatic, {
|
||||||
)
|
root: libcurlPath,
|
||||||
)
|
prefix: "/libcurl/",
|
||||||
);
|
decorateReply: false
|
||||||
|
});
|
||||||
|
app.register(fastifyStatic, {
|
||||||
app.use(router);
|
root: bareModulePath,
|
||||||
app.use(express.static(path.join(__dirname, "views")));
|
prefix: "/bareasmodule/",
|
||||||
app.use("/uv/", express.static(uvPath));
|
decorateReply: false
|
||||||
app.use("/epoxy/", express.static(epoxyPath));
|
});
|
||||||
app.use("/libcurl/", express.static(libcurlPath));
|
app.register(fastifyStatic, {
|
||||||
app.use("/bareasmodule/", express.static(bareModulePath));
|
root: baremuxPath,
|
||||||
app.use("/baremux/", express.static(baremuxPath));
|
prefix: "/baremux/",
|
||||||
|
decorateReply: false
|
||||||
app.disable("x-powered-by");
|
});
|
||||||
|
app.get("/", function(req, reply) {
|
||||||
// Redundant code since 404 is handled elsewhere; left here as insurance.
|
reply.type('html');
|
||||||
app.use((req, res) => {
|
reply.send(paintSource(loadTemplates(tryReadFile(path.join(__dirname, "views", "/?".indexOf(req.url) ? pages[Object.keys(req.query)[0]] || "error.html" : pages.index)))))
|
||||||
res.status(404).send(paintSource(loadTemplates(text404)));
|
|
||||||
});
|
});
|
||||||
|
|
||||||
server.listen(port);
|
//host is set as to avoid just being on localhost
|
||||||
console.log("Holy Unblocker is listening on port " + port + ".");
|
app.listen({ port: port, host: '0.0.0.0' });
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue