-
Notifications
You must be signed in to change notification settings - Fork 5
/
testproxy.js
executable file
·149 lines (119 loc) · 2.71 KB
/
testproxy.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#! /usr/bin/env node
/**
testProxy 1.3.4
Test your local websites on other devices
@copyright 2023 Edwin Martin
@license MIT
*/
const http = require('http');
const httpProxy = require('http-proxy');
const args = getArguments();
if (args.error) {
printUsage(args.error);
} else {
const url = startProxy(args.host, args.port, args.listenPort);
if (args.qr) {
showQR(url);
}
}
//=== Function declarations ============
/**
* Parse command line arguments
*/
function getArguments() {
let opt = {};
if (process.argv.length < 3) {
return {error: "testProxy does not see enough arguments."};
}
process.argv.forEach(arg => {
parseUrl(opt, arg);
parseListenPort(opt, arg);
parseQR(opt, arg);
});
return opt;
//== functions
function parseListenPort(opt, arg) {
const match = arg.match(/-[lp] ?([0-9]+)/);
if (match) {
opt.listenPort = parseInt(match[1], 10);
} else if (!opt.listenPort) {
opt.listenPort = 0;
}
}
function parseUrl(opt, url) {
const match = url.match(/http(s?):\/\/([-a-z0-9.]+):?([0-9]+)?(\/.*)?/);
if (match) {
opt.host = match[2];
opt.port = match[3] || 80;
opt.path = match[4];
}
}
function parseQR(opt, arg) {
if (arg == '-noqr') {
opt.qr = false;
} else if (opt.qr == null) {
opt.qr = true;
}
}
}
/**
* Find ip addresses of local computer
* @returns {Array} ip-addresses
*/
function getIpAddresses() {
const os = require('os');
const ifaces = os.networkInterfaces();
return Object.keys(ifaces).reduce((prev, iface) =>
prev.concat(ifaces[iface].filter(address =>
!address.internal && address.family == 'IPv4'
))
, []);
}
/**
* Start the actual proxy
* @returns {String} (First) url
*/
function startProxy(hostname, port, listenPort) {
const proxy = httpProxy.createProxyServer({});
let returnUrl;
proxy.on('proxyReq', proxyReq =>
proxyReq.setHeader('Host', hostname)
);
const server = http.createServer((req, res) =>
proxy.web(req, res, {
target: `http://${hostname}:${port}`
})
);
server.listen(listenPort);
if (listenPort === 0) {
listenPort = server.address().port;
}
getIpAddresses().forEach(ip => {
const url = getUrl(ip.address, listenPort);
returnUrl = returnUrl || url;
console.log(`Listening on ${url}`);
}
);
return returnUrl;
}
function printUsage(error) {
console.error(`Oops. ${error}`);
console.error("Usage: node testproxy <url> [-l<listen-port>] [-noqr]");
}
/**
* Show QR code on the terminal
* @param url
*/
function showQR(url) {
const qrcode = require('qrcode-terminal');
qrcode.generate(url);
}
/**
* Build url from host, port and path
* @param host
* @param port
* @param path
*/
function getUrl(host, port = 80, path = '/') {
return `http://${host}:${port}${path}`;
}