https-proxy-server-express
Version:
72 lines (71 loc) • 2.25 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ProxyServer = void 0;
const express_1 = __importDefault(require("express"));
const https_1 = __importDefault(require("https"));
const http_proxy_middleware_1 = require("http-proxy-middleware");
class ProxyServer {
log;
app;
server;
port;
options;
target;
constructor(port, options, target, log = false) {
this.log = log;
this.app = (0, express_1.default)();
this.server = null;
this.port = port;
this.options = options;
this.target = target;
this.setupMiddleware();
this.setupProxy();
}
setupMiddleware() {
const clientCertAuth = (req, res, next) => {
const clientCert = req.socket.getPeerCertificate();
if (!clientCert) {
return res.status(401).send("No client certificate provided.");
}
if (!req.socket.authorized) {
return res
.status(401)
.send("Invalid client certificate authentication.");
}
next();
return null;
};
this.app.use(clientCertAuth);
}
setupProxy() {
const proxyOptions = {
target: this.target,
changeOrigin: true,
secure: false,
};
this.app.use("/", (0, http_proxy_middleware_1.createProxyMiddleware)(proxyOptions));
}
async start() {
this.server = https_1.default
.createServer(this.options, this.app)
.listen(this.port, () => {
if (this.log)
console.log(`Server is running on port ${this.port}`);
});
}
async stop() {
if (this.server) {
await new Promise((resolve) => {
this.server?.close(() => {
if (this.log)
console.log(`Server on port ${this.port} has been stopped`);
resolve(null);
});
});
}
}
}
exports.ProxyServer = ProxyServer;