From e9a88bc1cd403ae05aa7fdeefb70f3e8378c597e Mon Sep 17 00:00:00 2001 From: MotorTruck1221 Date: Sat, 23 Mar 2024 01:45:47 -0600 Subject: [PATCH] Add masqr! --- masqr.js | 61 ++++++++++++++++++++++++++++++++++++++++++++++ package.json | 1 + pnpm-lock.yaml | 3 +++ server.ts | 66 ++++++-------------------------------------------- 4 files changed, 73 insertions(+), 58 deletions(-) create mode 100644 masqr.js diff --git a/masqr.js b/masqr.js new file mode 100644 index 0000000..04106e3 --- /dev/null +++ b/masqr.js @@ -0,0 +1,61 @@ +import fp from 'fastify-plugin' +import fs from 'fs'; +const failureFile = fs.readFileSync("Checkfailed.html", "utf8"); +const LICENSE_SERVER_URL = "https://license.mercurywork.shop/validate?license="; +const whiteListedDomain = ["nebulaproxy.io"]; +async function licenseCheck(req, pass) { + try { + const resp = await fetch(`${LICENSE_SERVER_URL}${pass}&host=${req.headers.host}`); + const data = await resp.json(); + if (data.status === "License valid") { + return true; + } + else { + return false; + } + } catch { return false; } +} +const plugin = (fastify, opts, done) => { + fastify.addHook('onRequest', function (req, reply, next) { + if (req.cookies.authcheck === 'true' || whiteListedDomain.includes(req.headers.host)) { + return next(); + } + const authHeader = req.headers.authorization; + if (req.cookies.refreshcheck != "true") { + reply + .setCookie('refreshcheck', 'true', { maxAge: 1000 }) + .type("text/html") + .send(failureFile); + return; + } + if (!authHeader) { + reply + .code(401) + .header('WWW-Authenticate', 'Basic') + .type("text/html") + .send(failureFile); + return; + } + const auth = Buffer.from(authHeader.split(" ")[1], "base64").toString().split(":"); + const user = auth[0]; + const pass = auth[1]; + licenseCheck(req, pass).then((data) => { + if (!data) { + reply.status(401).header('WWW-Authenticate', 'Basic').type("text/html").send(failureFile); + return; + } + else { + reply.setCookie('authcheck', 'true').type("text/html").send('') + return; + } + }); + }); + done(); +}; + +const masqr = fp(plugin, { + fastify: '4.x', + name: 'masqr' +}); + +export default masqr; diff --git a/package.json b/package.json index 946d666..65d61fc 100644 --- a/package.json +++ b/package.json @@ -31,6 +31,7 @@ "crypto-js": "^4.2.0", "express": "^4.19.1", "fastify": "^4.26.2", + "fastify-plugin": "^4.5.1", "framer-motion": "^10.18.0", "i18next": "^23.10.1", "i18next-browser-languagedetector": "^7.2.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 7753eb2..2a31aa7 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -65,6 +65,9 @@ dependencies: fastify: specifier: ^4.26.2 version: 4.26.2 + fastify-plugin: + specifier: ^4.5.1 + version: 4.5.1 framer-motion: specifier: ^10.18.0 version: 10.18.0(react-dom@18.2.0)(react@18.2.0) diff --git a/server.ts b/server.ts index 6180e66..0fb6fa7 100644 --- a/server.ts +++ b/server.ts @@ -5,21 +5,16 @@ import path from "path"; import fs from "fs"; import cookieParser from "@fastify/cookie"; import { createServer } from "http"; - import { createBareServer } from "@tomphttp/bare-server-node"; import createRammerhead from "rammerhead/src/server/index.js"; import wisp from "wisp-server-node"; import { Socket } from "net"; - const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); - const bare = createBareServer("/bare/"); const rh = createRammerhead(); - -const failureFile = fs.readFileSync("Checkfailed.html", "utf8"); - -const LICENSE_SERVER_URL = "https://license.mercurywork.shop/validate?license="; +import chalk from "chalk"; +import masqr from './masqr.js'; const rammerheadScopes = [ "/rammerhead.js", @@ -83,56 +78,7 @@ const app = fastify({ logger: false, serverFactory }); app.register(cookieParser); await app.register(import("@fastify/compress")); - -// Uncomment if you wish to add masqr. -/* -app.addHook("preHandler", async (req, reply) => { - if (req.cookies["authcheck"]) { - return reply; - } - - const authheader = req.headers.authorization; - - if (req.cookies["refreshcheck"] != "true") { - reply - .setCookie("refreshcheck", "true", { maxAge: 10000 }) - .type("text/html") - .send(failureFile); - return reply; - } - - if (!authheader) { - reply - .code(401) - .header("WWW-Authenticate", "Basic") - .type("text/html") - .send(failureFile); - return reply; - } - - const auth = Buffer.from(authheader.split(" ")[1], "base64") - .toString() - .split(":"); - const user = auth[0]; - const pass = auth[1]; - - const licenseCheck = ( - await ( - await fetch(`${LICENSE_SERVER_URL}${pass}&host=${req.headers.host}`) - ).json() - )["status"]; - console.log( - `${LICENSE_SERVER_URL}${pass}&host=${req.headers.host} returned ${licenseCheck}` - ); - - if (licenseCheck === "License valid") { - reply.setCookie("authcheck", "true"); - return reply; - } - - reply.type("text/html").send(failureFile); - return reply; -}); */ +app.register(masqr); app.register(fastifyStatic, { root: path.join(__dirname, "dist"), @@ -155,6 +101,10 @@ app.setNotFoundHandler((req, res) => { res.sendFile("index.html"); // SPA catch-all }); +console.log(chalk.green(`Server listening on ${chalk.bold("http://localhost:8080")}`)); +console.log(chalk.magenta(`Server also listening on ${chalk.bold("http://0.0.0.0:8080")}`)); + app.listen({ - port: 8080 + port: 8080, + host: "0.0.0.0" });