net-helper/components/http-reverse.js
2025-01-23 16:22:26 +03:00

42 lines
1011 B
JavaScript

const HTTPServer = require("./_http");
const { fetchProxy } = HTTPServer;
function logger (...p) {
//return;
console.log("[DEBUG.http.reverse]:", ...p);
}
module.exports = class HTTPReverse extends HTTPServer {
constructor (adr, syntaxTree) {
super(adr, syntaxTree);
}
run () {
this.preinit();
const reverseTo = this.syntax.target ?? "https://example.org";
this.app.use("*", async (req, res) => {
logger("REQ HEADERS:>", req.headers);
logger("originalUrl:>", req.originalUrl);
const currentDomain = req.hostname;
logger("hostname:", currentDomain);
const isNotDomained = !this.syntax.domains[currentDomain];
if (!isNotDomained) {
return this
.callDomain(currentDomain, this.syntax.domains[currentDomain])
.argv("proxy", req, res);
}
return fetchProxy(req, res, reverseTo);
});
this.postinit((err) => {
if (err) {
throw err;
}
console.log(`HTTP reverse proxy server listened at ${
this.adr.address
}:${this.adr.port}`);
});
}
};