Add support of password

This commit is contained in:
FullGreaM 2025-01-11 20:36:08 +03:00
parent c0755fe55c
commit 0a9355bc9d

View File

@ -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("<h1>Permission denied</h1><hr/><p>Permission denied.</p>");
}
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();
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);
}
else {
if (this.syntax.auth.mode === "strict" && req.headers['authorization'] !== undefined) {
return res.status(403).send("<h1>Permission denied</h1><hr/><p>Permission denied.</p>");
}
res.set("WWW-Authenticate", "Basic");
return res.status(401).send();
}
}
next();
authPage(this, req, res, next)
});
this.app.use("*", async (req, res) => {