-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
67 lines (56 loc) · 1.83 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
66
67
#!/usr/bin/env node
var os = require('os'),
proxy = require('http-proxy'),
pkg = require('./package');
var exit = function() {
var bin = Object.keys(pkg.bin)[0];
console.log('Usage examples:');
console.log('\t%s 51123 to 3000', bin);
console.log('\t%s [http(s)://]192.168.0.100:51123 to 3000', bin);
console.log('\t%s [http://]domain.com:80 to 3000', bin);
console.log('\t%s [https://]ssl-domain.com:443 to 3000', bin);
console.log();
process.exit();
};
console.log('Port Proxy %s', pkg.version);
if (process.argv.length != 5 || process.argv[3].toLowerCase() !== 'to') {
exit();
}
var source = process.argv[2].match(/^(https?:\/\/)?(.+?)(?:\:(\d+))$/);
var protocolPrefix = 'http://',
host = 'localhost',
port, proxyPort;
if (source === null) {
port = parseInt(process.argv[2], 10);
} else {
protocolPrefix = source[1] || 'http://';
host = source[2];
port = parseInt(source[3], 10);
}
proxyPort = parseInt(process.argv[4], 10);
if (isNaN(port) || isNaN(proxyPort)) {
exit();
}
console.log('Proxying %s%s:%d to network interfaces:', protocolPrefix, host, port);
var interfaces = os.networkInterfaces();
Object.keys(interfaces).forEach(function(name) {
interfaces[name].filter(function(item) {
return item.family == 'IPv4' && !item.internal;
}).forEach(function(item) {
console.log("\t%s: %s:%s", name, item.address, proxyPort);
});
});
proxy.createProxyServer({
target: protocolPrefix + host + ':' + port,
changeOrigin: true,
secure: false
}).listen(proxyPort, function() {
console.log('Listening... [press Control-C to exit]');
}).on('error', function (err, req, res) {
console.log(err.stack);
console.log('Listening... [press Control-C to exit]');
res.writeHead(500, {
'Content-Type': 'text/plain'
});
res.end('Something went wrong. Check your console to see the error.');
});