Last active
March 3, 2016 20:25
-
-
Save trestletech/7160493 to your computer and use it in GitHub Desktop.
Quick test proxy in node that just passes all traffic through with an additional auth header, as if the request had been authenticated.
This file contains hidden or 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
node_modules |
This file contains hidden or 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
var _ = require('underscore'); | |
var http = require('http'); | |
var httpProxy = require('http-proxy'); | |
var https = require('https'); | |
var fs = require('fs'); | |
var opts = require('optimist') | |
.usage('Test auth proxy.\nUsage: $0 [extPort] [destPort] [destHostName] --ssl') | |
.describe('ssl', 'Enable SSL on both the client and expect it on the target.') | |
.demand(0); | |
var argv = opts.argv; | |
var port = argv._[0]?argv._[0] : 8000; | |
var destPort = argv._[1]?argv._[1] : 3838; | |
var destHostname = (argv._[2])?argv._[2] : 'localhost'; | |
var options = { | |
target: { | |
https: argv.ssl, | |
rejectUnauthorized: false | |
} | |
} | |
if (argv.ssl){ | |
options.https = { | |
key: fs.readFileSync('/home/jeff/workspace/test-key.pem'), | |
cert: fs.readFileSync('/home/jeff/workspace/test-cert.pem') | |
} | |
} | |
var proxy = new httpProxy.HttpProxy({ | |
target: { | |
host: destHostname, | |
port: destPort, | |
https: argv.ssl, | |
rejectUnauthorized: false | |
} | |
}); | |
if (argv.ssl){ | |
https.createServer(options.https, function (req, res) { | |
req.headers['X-Auth-Username'] = 'jeff'; | |
req.headers['X-Auth-Groups'] = 'group1, group2'; | |
proxy.proxyRequest(req, res); | |
}).listen(port); | |
} else{ | |
http.createServer(function (req, res) { | |
req.headers['X-Auth-Username'] = 'jeff'; | |
req.headers['X-Auth-Groups'] = 'group1, group2'; | |
proxy.proxyRequest(req, res); | |
}).listen(port); | |
} |
This file contains hidden or 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
{ | |
"name": "test-auth-proxy", | |
"preferGlobal": "false", | |
"version": "0.1.0", | |
"author": "RStudio <[email protected]>", | |
"description": "Simple auth proxy.", | |
"bin": "./main.js", | |
"scripts": { | |
"start": "node ./main.js 8000 3838 localhost" | |
}, | |
"repository": { | |
"type": "git", | |
"url": "https:///gist.github.com/7160493" | |
}, | |
"dependencies" : { | |
"http-proxy" : "0.10.x", | |
"underscore" : "1.4.x", | |
"optimist" : "0.6.x" | |
}, | |
"license": "AGPL-3", | |
"engines": { | |
"node": ">=0.8.16" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment