Last active
July 28, 2017 06:15
-
-
Save nadeesha/6863947 to your computer and use it in GitHub Desktop.
Node-based http/https proxy to forego the pain of configuring iptables for one-off dev tasks. Not recommended to be used in prod environments.
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
var fs = require('fs'), | |
http = require('http'), | |
https = require('https'), | |
httpProxy = require('http-proxy'); | |
var isHttps = true; // do you want a https proxy? | |
var options = { | |
https: { | |
key: fs.readFileSync('key.pem'), | |
cert: fs.readFileSync('key-cert.pem') | |
} | |
}; | |
// this is the target server | |
var proxy = new httpProxy.HttpProxy({ | |
target: { | |
host: '127.0.0.1', | |
port: 8080 | |
} | |
}); | |
if (isHttps) | |
https.createServer(options.https, function(req, res) { | |
console.log('Proxying https request at %s', new Date()); | |
proxy.proxyRequest(req, res); | |
}).listen(443, function(err) { | |
if (err) | |
console.log('Error serving https proxy request: %s', req); | |
console.log('Created https proxy. Forwarding requests from %s to %s:%s', '443', proxy.target.host, proxy.target.port); | |
}); | |
else | |
http.createServer(options.https, function(req, res) { | |
console.log('Proxying http request at %s', new Date()); | |
console.log(req); | |
proxy.proxyRequest(req, res); | |
}).listen(80, function(err) { | |
if (err) | |
console.log('Error serving http proxy request: %s', req); | |
console.log('Created http proxy. Forwarding requests from %s to %s:%s', '80', proxy.target.host, proxy.target.port); | |
}); |
well I'm sorry, it is actually a CONNECT request instead of CONNECTION
httpProxy.HttpProxy is not a function
used this package (wrong npm package?): https://www.npmjs.com/package/http-proxy
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
well I have trouble with https
It turns out that when browser use the server as proxy to reach another host, the 'request' event seems not to be emmited, while the 'connection' event is emmited, leaving the CONNECTION request from the browser untouched.
it is on windows 10 and nodejs 5.