Last active
May 7, 2020 06:44
-
-
Save kirel/8229162611c44ca629850abb7db3353c to your computer and use it in GitHub Desktop.
Proxy for basic auth.
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
// Usage: USER=user PASSWORD=pw TARGET=http://localhost:3838 PORT=8080 node proxy.js | |
var http = require('http'), | |
auth = require('basic-auth'), | |
httpProxy = require('http-proxy'); | |
var proxy = httpProxy.createProxyServer({}); | |
var server = http.createServer(function(req, res) { | |
var credentials = auth(req) | |
if (!credentials || credentials.name !== process.env.USER || credentials.pass !== process.env.PASSWORD) { | |
res.statusCode = 401 | |
res.setHeader('WWW-Authenticate', 'Basic realm="example"') | |
res.end('Access denied') | |
} else { | |
proxy.web(req, res, { target: process.env.TARGET }); | |
} | |
}); | |
server.listen(process.env.PORT); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment