Skip to content

Instantly share code, notes, and snippets.

@kirel
Last active May 7, 2020 06:44
Show Gist options
  • Save kirel/8229162611c44ca629850abb7db3353c to your computer and use it in GitHub Desktop.
Save kirel/8229162611c44ca629850abb7db3353c to your computer and use it in GitHub Desktop.
Proxy for basic auth.
// 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