diff --git a/components/http-reverse.js b/components/http-reverse.js index bddab2e..1505931 100644 --- a/components/http-reverse.js +++ b/components/http-reverse.js @@ -14,23 +14,6 @@ function logger (...p) { console.log("[DEBUG.main]:", ...p); } -function auth (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(); -} - function fetchProxy (req, res, reverseTo) { const params = { method: req.method.toUpperCase(), headers: Object.assign({}, req.headers), redirect: "follow", follow: 20, compress: false }; delete params.headers.host; @@ -70,11 +53,14 @@ module.exports = class HTTPReverse extends Component { super(adr, syntaxTree); } - domainAction (context, domain, req, res) { + domainAction (context, domain, callCommand, req, res) { 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"); - fetchProxy(req, res, this.syntax.target); + switch (callCommand) { + case "proxy": + return fetchProxy(req, res, this.syntax.target); + } } run () { @@ -98,7 +84,22 @@ module.exports = class HTTPReverse extends Component { }); }); - this.app.use((req, res, next) => auth(this, req, res, next)); + 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(); + } + } + next(); + }); this.app.use("*", async (req, res) => { logger("REQ HEADERS:>", req.headers); @@ -109,7 +110,7 @@ module.exports = class HTTPReverse extends Component { if (!isNotDomained) { return this .callDomain(currentDomain, this.syntax.domains[currentDomain]) - .argv(req, res); + .argv("proxy", req, res); } return fetchProxy(req, res, reverseTo); });