forked from xorynix/dogeub
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmasqr.js
More file actions
68 lines (54 loc) · 2.14 KB
/
masqr.js
File metadata and controls
68 lines (54 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import fs from "fs"
import path from "path"
import fetch from "node-fetch"
export const LICENSE_SERVER_URL = "http://localhost:8004/validate?license="
export const whiteListedDomains = []
export const failure = fs.readFileSync("Checkfailed.html", "utf8")
export const placeholder = fs.readFileSync("placeholder.svg", "utf8")
export async function MasqFail(req, reply) {
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 bruh = await fs.promises.readFile(safeJoin, "utf8")
reply.header("Content-Type", "text/html").send(bruh)
} catch {
reply.header("Content-Type", "text/html").send(failure)
}
}
export async function MasqrMiddleware(req, reply) {
if (req.headers.host && whiteListedDomains.includes(req.headers.host)) return
if (req.url.includes("placeholder.svg")) {
reply.header("Content-Type", "image/svg+xml").send(placeholder)
return
}
const authHeader = req.headers.authorization
if (req.cookies?.auth) return
if (req.cookies?.refreshcheck !== "true") {
reply.setCookie("refreshcheck", "true", { maxAge: 10, path: "/" })
await MasqFail(req, reply)
return
}
if (!authHeader) {
reply.header("WWW-Authenticate", "Basic").status(401)
await MasqFail(req, reply)
return
}
const [user, pass] = Buffer.from(authHeader.split(" ")[1], "base64").toString().split(":")
try {
const licenseRes = await fetch(`${LICENSE_SERVER_URL}${pass}&host=${req.headers.host}`)
const licenseData = await licenseRes.json()
console.log(`${LICENSE_SERVER_URL}${pass}&host=${req.headers.host} returned`, licenseData)
if (licenseData.status === "License valid") {
reply.setCookie("auth", "true", {
expires: new Date(Date.now() + 365 * 24 * 60 * 60 * 1000),
path: "/",
})
reply.header("Content-Type", "text/html").send("<script>window.location.href = window.location.href</script>")
return
}
} catch {}
await MasqFail(req, reply)
}