-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
65 lines (54 loc) · 1.63 KB
/
index.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/**
* Proxy server for HTTP and HTTPS requests
* @module
*/
var debug = require('debug')('proxy'),
fs = require('fs'),
httpProxy = require('http-proxy'),
proxy = httpProxy.createProxy(),
ranger = require('park-ranger')(),
cert = ranger.cert,
config = ranger.config;
debug('establishing proxy: %O', config);
['http', 'https'].forEach((protocol) => {
if (!config.ports[protocol]) { return; }
var httpRouter = {};
var createServer = (protocol, done) => {
if (protocol === 'https') {
return require(protocol).createServer(cert, done);
} else {
return require(protocol).createServer(done);
}
};
createServer(protocol, function(req, res) {
var target;
config.targets.forEach((configTarget) => {
if (configTarget.host === req.headers.host && configTarget[protocol]) {
target = configTarget[protocol];
}
});
if (!target) {
if (config.defaultTarget && config.defaultTarget[protocol]) {
target = config.defaultTarget[protocol];
} else {
res.writeHead(404);
res.write('Not Found');
res.end();
return;
}
}
debug('proxying %s request for host %s to target %s', protocol, req.headers.host, target);
proxy.web(req, res, {
secure: protocol === 'https' ? true : false,
ssl: cert,
target: protocol + '://' + target
}, function(error) {
res.writeHead(500);
res.write('Internal Server Error');
res.end();
debug(error.message);
});
}).listen(config.ports[protocol], () => {
debug('listening for HTTP requests on port %s', config.ports[protocol]);
});
});