Skip to content

Commit

Permalink
V5.2.3 - Fixed Local Games, Moved Masqr Setup, Formatted
Browse files Browse the repository at this point in the history
  • Loading branch information
xbubbo committed May 25, 2024
1 parent 0b7cd17 commit bfc5a8c
Show file tree
Hide file tree
Showing 9 changed files with 239 additions and 175 deletions.
72 changes: 72 additions & 0 deletions Masqr.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import path from "node:path"
import fs from "fs"
import fetch from "node-fetch"

const LICENSE_SERVER_URL = "https://masqr.gointerstellar.app/validate?license="
const Fail = fs.readFileSync("Failed.html", "utf8")

export function setupMasqr(app) {
app.use(async (req, res, next) => {
if (req.url.includes("/ov/")) {
next()
return
}

const authheader = req.headers.authorization

if (req.cookies["authcheck"]) {
next()
return
}

if (req.cookies["refreshcheck"] !== "true") {
res.cookie("refreshcheck", "true", { maxAge: 10000 })
MasqFail(req, res)
return
}

if (!authheader) {
res.setHeader("WWW-Authenticate", "Basic")
res.status(401)
MasqFail(req, res)
return
}

const auth = Buffer.from(authheader.split(" ")[1], "base64").toString().split(":")
const pass = auth[1]

try {
const licenseCheckResponse = await fetch(LICENSE_SERVER_URL + pass + "&host=" + req.headers.host)
const licenseCheck = (await licenseCheckResponse.json())["status"]
console.log(LICENSE_SERVER_URL + pass + "&host=" + req.headers.host + " returned " + licenseCheck)
if (licenseCheck === "License valid") {
res.cookie("authcheck", "true", { expires: new Date(Date.now() + 365 * 24 * 60 * 60 * 1000) })
res.send("<script> window.location.href = window.location.href </script>")
return
}

MasqFail(req, res)
} catch (error) {
console.error(error)
MasqFail(req, res)
}
})
}

async function MasqFail(req, res) {
if (!req.headers.host) {
return
}
const unsafeSuffix = req.headers.host + ".html"
const safeSuffix = path.normalize(unsafeSuffix).replace(/^(\.\.(\/|\\|$))+/, "")
const safeJoin = path.join(process.cwd() + "/Masqrd", safeSuffix)
try {
await fs.promises.access(safeJoin)
const FailLocal = await fs.promises.readFile(safeJoin, "utf8")
res.setHeader("Content-Type", "text/html")
res.send(FailLocal)
} catch (e) {
res.setHeader("Content-Type", "text/html")
res.send(Fail)
}
}
127 changes: 38 additions & 89 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import http from "node:http"
import path from "node:path"
import fs from "fs"
import { createBareServer } from "@tomphttp/bare-server-node"
import cors from "cors"
import express from "express"
import basicAuth from "express-basic-auth"
import cookieParser from "cookie-parser"
import mime from "mime"
import config from "./config.js"

const LICENSE_SERVER_URL = "https://masqr.gointerstellar.app/validate?license="
const Fail = fs.readFileSync("Failed.html", "utf8")
import { setupMasqr } from "./Masqr.js"

const __dirname = process.cwd()
const server = http.createServer()
Expand All @@ -23,69 +21,52 @@ if (config.challenge) {
app.use(basicAuth({ users: config.users, challenge: true }))
}

app.use(cookieParser())
app.use(express.json())
app.use(express.urlencoded({ extended: true }))

if (process.env.MASQR_CHECK === "true") {
app.use(async (req, res, next) => {
if (req.url.includes("/ov/")) {
next()
return
}

const authheader = req.headers.authorization
app.get("/e/*", async (req, res, next) => {
const baseUrls = {
"/e/1/": "https://raw.githubusercontent.com/v-5x/x/fixy/",
"/e/2/": "https://raw.githubusercontent.com/ypxa/y/main/",
"/e/3/": "https://raw.githubusercontent.com/ypxa/w/master/",
}

if (req.cookies["authcheck"]) {
next()
return
let reqTarget
for (const [prefix, baseUrl] of Object.entries(baseUrls)) {
if (req.path.startsWith(prefix)) {
reqTarget = baseUrl + req.path.slice(prefix.length)
break
}
}

if (req.cookies["refreshcheck"] != "true") {
res.cookie("refreshcheck", "true", { maxAge: 10000 })
MasqFail(req, res)
return
}
if (!reqTarget) {
return next()
}

if (!authheader) {
res.setHeader("WWW-Authenticate", "Basic")
res.status(401)
MasqFail(req, res)
return
try {
const asset = await fetch(reqTarget)
if (asset.status !== 200) {
return next()
}

const auth = Buffer.from(authheader.split(" ")[1], "base64").toString().split(":")
const pass = auth[1]
const data = Buffer.from(await asset.arrayBuffer())
const ext = path.extname(reqTarget)
const no = [".unityweb"]
const contentType = no.includes(ext) ? "application/octet-stream" : mime.getType(ext)

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") {
res.cookie("authcheck", "true", { expires: new Date(Date.now() + 365 * 24 * 60 * 60 * 1000) })
res.send(`<script> window.location.href = window.location.href </script>`)
return
}
res.writeHead(200, { "Content-Type": contentType })
res.end(data)
} catch (error) {
console.error(error)
res.setHeader("Content-Type", "text/html")
}
})

MasqFail(req, res)
})
app.use(cookieParser())
app.use(express.json())
app.use(express.urlencoded({ extended: true }))

async function MasqFail(req, res) {
if (!req.headers.host) {
return
}
const unsafeSuffix = req.headers.host + ".html"
let safeSuffix = path.normalize(unsafeSuffix).replace(/^(\.\.(\/|\\|$))+/, "")
let safeJoin = path.join(process.cwd() + "/Masqrd", safeSuffix)
try {
await fs.promises.access(safeJoin)
const FailLocal = await fs.promises.readFile(safeJoin, "utf8")
res.setHeader("Content-Type", "text/html")
res.send(FailLocal)
} catch (e) {
res.setHeader("Content-Type", "text/html")
res.send(Fail)
}
}
if (process.env.MASQR === "true") {
setupMasqr(app)
}

app.use(express.static(path.join(__dirname, "static")))
app.use("/ov", cors({ origin: true }))

Expand All @@ -105,38 +86,6 @@ routes.forEach((route) => {
})
})

app.get("/e/*", (req, res, next) => {
const baseUrls = [
"https://raw.githubusercontent.com/v-5x/x/fixy",
"https://raw.githubusercontent.com/ypxa/y/main",
"https://raw.githubusercontent.com/ypxa/w/master",
]
fetchData(req, res, next, baseUrls)
})

const fetchData = async (req, res, next, baseUrls) => {
try {
const reqTarget = baseUrls.map((baseUrl) => `${baseUrl}/${req.params[0]}`)
let data
let asset
for (const target of reqTarget) {
asset = await fetch(target)
if (asset.ok) {
data = await asset.arrayBuffer()
break
}
}
if (data) {
res.end(Buffer.from(data))
} else {
next()
}
} catch (error) {
console.error(`Error fetching ${req.url}:`, error)
next(error)
}
}

app.use((req, res, next) => {
res.status(404).sendFile(path.join(__dirname, "static", "404.html"))
})
Expand Down
Loading

0 comments on commit bfc5a8c

Please sign in to comment.