Skip to content

Instantly share code, notes, and snippets.

@joaosouz4dev
Created September 22, 2021 20:56
Show Gist options
  • Save joaosouz4dev/449410da80447dc6733dabae8db9cec0 to your computer and use it in GitHub Desktop.
Save joaosouz4dev/449410da80447dc6733dabae8db9cec0 to your computer and use it in GitHub Desktop.
proxy server node js - change ip request
import express from "express";
import httpProxy from "http-proxy";
const proxy = httpProxy.createProxyServer({
changeOrigin: true,
});
const app = express();
function buildAuthHeader(user, pass) {
return "Basic " + Buffer.from(user + ":" + pass).toString("base64");
}
app.use("", (req, res, next) => {
// console.log('req '+req.headers['proxy-authorization']);
// console.log(buildAuthHeader('admin', 'admin'))
let userpass = buildAuthHeader('admin', 'admin');
if (req.headers["proxy-authorization"] == userpass) {
next();
} else {
res.sendStatus(403);
}
});
app.get("*", function (req, res) {
console.log("Request", req.method, req.url);
proxy.web(req, res, { target: `${req.protocol}://${req.hostname}` });
});
app.listen(8500, function () {
console.log("Listening on port 8500");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment