Created
September 22, 2021 20:56
-
-
Save joaosouz4dev/449410da80447dc6733dabae8db9cec0 to your computer and use it in GitHub Desktop.
proxy server node js - change ip request
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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