From 0a9355bc9d97680364e6742a7310bb82a401d1dc Mon Sep 17 00:00:00 2001 From: FullGreaM Date: Sat, 11 Jan 2025 20:36:08 +0300 Subject: [PATCH] Add support of password --- components/http-reverse.js | 42 +++++++++++++++++++++++++------------- 1 file changed, 28 insertions(+), 14 deletions(-) diff --git a/components/http-reverse.js b/components/http-reverse.js index 1505931..bfbee4d 100644 --- a/components/http-reverse.js +++ b/components/http-reverse.js @@ -48,18 +48,38 @@ function fetchProxy (req, res, reverseTo) { }); } +function authPage (self, req, res, next) { + if (self.syntax.auth) { + const ACCESS = "Basic " + Buffer.from(`${self.syntax.auth.login}:${self.syntax.auth.password}`).toString('base64'); + if (req.headers['authorization'] === ACCESS) { + return next(); + } + else { + if (self.syntax.auth.mode === "strict" && req.headers['authorization'] !== undefined) { + return res.status(403).send("

Permission denied


Permission denied.

"); + } + res.set("WWW-Authenticate", "Basic"); + return res.status(401).send(); + } + } + next(); +} + module.exports = class HTTPReverse extends Component { constructor (adr, syntaxTree) { super(adr, syntaxTree); } - domainAction (context, domain, callCommand, req, res) { + domainAction (context, domain, callCommand, req, res, next) { logger("DOMAIN ACT:", this.syntax); if (!(context instanceof HTTPReverse)) throw new SyntaxError("You can't call revHTTP domain from unsupported type. Supported types: revHTTP, simpleHTTP, redirectHTTP"); switch (callCommand) { case "proxy": return fetchProxy(req, res, this.syntax.target); + case "auth": + const authContext = context.syntax.auth === undefined ? this : context; + return authPage(authContext, req, res); } } @@ -85,20 +105,14 @@ module.exports = class HTTPReverse extends Component { }); this.app.use((req, res, next) => { - if (this.syntax.auth) { - const ACCESS = "Basic " + Buffer.from(`${this.syntax.auth.login}:${this.syntax.auth.password}`).toString('base64'); - if (req.headers['authorization'] === ACCESS) { - return next(); - } - else { - if (this.syntax.auth.mode === "strict" && req.headers['authorization'] !== undefined) { - return res.status(403).send("

Permission denied


Permission denied.

"); - } - res.set("WWW-Authenticate", "Basic"); - return res.status(401).send(); - } + const currentDomain = req.hostname; + const isNotDomained = !this.syntax.domains[currentDomain]; + if (!isNotDomained) { + return this + .callDomain(currentDomain, this.syntax.domains[currentDomain]) + .argv("auth", req, res, next); } - next(); + authPage(this, req, res, next) }); this.app.use("*", async (req, res) => {