-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp-proxy.js
36 lines (29 loc) · 999 Bytes
/
http-proxy.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/**
* HTTP Proxy with SSL
* Author: Martin E. Cocaro
* Email: [email protected]
*/
var fs = require('fs');
var https = require('https');
var httpProxy = require('http-proxy');
var url = require('url');
var options = {
key: fs.readFileSync('./ssl/cert.key'),
cert: fs.readFileSync('./ssl/cert.crt')
};
var proxy = httpProxy.createProxyServer();
proxy.on('proxyReq', function(proxyReq, req, res, options) {
proxyReq.removeHeader('x-target-endpoint');
var targetUrl = url.parse(req.headers['x-target-endpoint'],true,true);
var hostname = targetUrl.hostname + (targetUrl.port? ':' + targetUrl.port:'');
proxyReq.setHeader('host',hostname);
});
var server = https.createServer(options,function (req, res) {
var target = req.headers['x-target-endpoint'];
if (target) {
return proxy.web(req, res, { 'target': target });
}
res.send('Forbidden', 403);
});
server.listen(8000);
console.info('Proxy listening port 8000');