bdy 1.7.46-dev
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintrc.yml +23 -0
- package/LICENSE +21 -0
- package/bin/cli.js +5 -0
- package/dockerfile +15 -0
- package/link.sh +3 -0
- package/package.json +39 -0
- package/src/agent/linux.js +127 -0
- package/src/agent/manager.js +404 -0
- package/src/agent/osx.js +150 -0
- package/src/agent/socket/tunnel.js +232 -0
- package/src/agent/socket.js +260 -0
- package/src/agent/system.js +205 -0
- package/src/agent/wait.js +20 -0
- package/src/agent/windows.js +168 -0
- package/src/agent.js +248 -0
- package/src/api/agent.js +95 -0
- package/src/api/buddy.js +131 -0
- package/src/api/socket.js +142 -0
- package/src/cfg.js +228 -0
- package/src/command/agent/disable.js +37 -0
- package/src/command/agent/enable.js +117 -0
- package/src/command/agent/restart.js +28 -0
- package/src/command/agent/run.js +16 -0
- package/src/command/agent/start.js +28 -0
- package/src/command/agent/status.js +45 -0
- package/src/command/agent/stop.js +28 -0
- package/src/command/agent/tunnel/http.js +47 -0
- package/src/command/agent/tunnel/list.js +27 -0
- package/src/command/agent/tunnel/start.js +38 -0
- package/src/command/agent/tunnel/status.js +32 -0
- package/src/command/agent/tunnel/stop.js +30 -0
- package/src/command/agent/tunnel/tcp.js +47 -0
- package/src/command/agent/tunnel/tls.js +47 -0
- package/src/command/agent/tunnel.js +21 -0
- package/src/command/agent/update.js +43 -0
- package/src/command/agent/version.js +23 -0
- package/src/command/agent.js +27 -0
- package/src/command/config/add/http.js +33 -0
- package/src/command/config/add/tcp.js +33 -0
- package/src/command/config/add/tls.js +33 -0
- package/src/command/config/add.js +13 -0
- package/src/command/config/get/region.js +13 -0
- package/src/command/config/get/timeout.js +13 -0
- package/src/command/config/get/token.js +13 -0
- package/src/command/config/get/tunnel.js +21 -0
- package/src/command/config/get/tunnels.js +13 -0
- package/src/command/config/get/whitelist.js +13 -0
- package/src/command/config/get.js +19 -0
- package/src/command/config/remove/tunnel.js +22 -0
- package/src/command/config/remove.js +9 -0
- package/src/command/config/set/region.js +19 -0
- package/src/command/config/set/timeout.js +22 -0
- package/src/command/config/set/token.js +18 -0
- package/src/command/config/set/whitelist.js +19 -0
- package/src/command/config/set.js +15 -0
- package/src/command/config.js +15 -0
- package/src/command/http.js +34 -0
- package/src/command/pre.js +47 -0
- package/src/command/start.js +31 -0
- package/src/command/tcp.js +32 -0
- package/src/command/tls.js +32 -0
- package/src/command/version.js +10 -0
- package/src/format.js +171 -0
- package/src/index.js +32 -0
- package/src/input.js +283 -0
- package/src/logger.js +87 -0
- package/src/output/interactive/tunnel.js +871 -0
- package/src/output/noninteractive/agent/tunnels.js +32 -0
- package/src/output/noninteractive/config/tunnel.js +52 -0
- package/src/output/noninteractive/config/tunnels.js +19 -0
- package/src/output/noninteractive/tunnel.js +79 -0
- package/src/output.js +136 -0
- package/src/server/cert.js +51 -0
- package/src/server/http1.js +79 -0
- package/src/server/http2.js +79 -0
- package/src/server/sftp.js +474 -0
- package/src/server/ssh.js +107 -0
- package/src/server/tls.js +41 -0
- package/src/ssh/client.js +196 -0
- package/src/texts.js +447 -0
- package/src/tunnel/agent.js +100 -0
- package/src/tunnel/compression.js +32 -0
- package/src/tunnel/dns.js +55 -0
- package/src/tunnel/html/404.html +129 -0
- package/src/tunnel/html/503.html +136 -0
- package/src/tunnel/html.js +32 -0
- package/src/tunnel/http/log.js +204 -0
- package/src/tunnel/http/serve.js +127 -0
- package/src/tunnel/http/stream.js +46 -0
- package/src/tunnel/http.js +406 -0
- package/src/tunnel/identification.js +95 -0
- package/src/tunnel/latency.js +63 -0
- package/src/tunnel/tcp.js +71 -0
- package/src/tunnel.js +696 -0
- package/src/utils.js +496 -0
- package/unlink.sh +3 -0
package/src/format.js
ADDED
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
const {
|
|
2
|
+
TLS_TERMINATE_AT_AGENT,
|
|
3
|
+
TLS_TERMINATE_AT_REGION,
|
|
4
|
+
TUNNEL_HTTP,
|
|
5
|
+
TARGET_ONLY_PORT_REGEX,
|
|
6
|
+
TARGET_HTTP_REGEX,
|
|
7
|
+
TARGET_TCP_TLS_REGEX,
|
|
8
|
+
TUNNEL_TCP,
|
|
9
|
+
TUNNEL_TLS,
|
|
10
|
+
TUNNEL_IDENTIFIED_HTTP1
|
|
11
|
+
} = require('./utils');
|
|
12
|
+
|
|
13
|
+
class Format {
|
|
14
|
+
static date() {
|
|
15
|
+
const dt = new Date();
|
|
16
|
+
return dt.toISOString();
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
static token(token) {
|
|
20
|
+
if (!token || token.length < 6) return 'Not provided';
|
|
21
|
+
return token.substring(0, 3) + '...' + token.substring(token.length - 3);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
static region(region) {
|
|
25
|
+
if (!region) return 'Autodetect';
|
|
26
|
+
return region.toUpperCase();
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
static timeout(timeout) {
|
|
30
|
+
if (timeout === 1) return '1 second';
|
|
31
|
+
return `${timeout} seconds`;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
static tunnelTimeout(timeout) {
|
|
35
|
+
if (!timeout) return 'Default';
|
|
36
|
+
return this.timeout(timeout);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
static whitelist(whitelist) {
|
|
40
|
+
if (!whitelist || !whitelist.length) return 'No one';
|
|
41
|
+
if (whitelist.includes('*')) return 'Everyone';
|
|
42
|
+
return whitelist.join('\n');
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
static tunnelWhitelist(whitelist) {
|
|
46
|
+
if (!whitelist) return 'Default';
|
|
47
|
+
return this.whitelist(whitelist);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
static tunnelHeaders(headers) {
|
|
51
|
+
if (!headers || !headers.length) return 'Not set';
|
|
52
|
+
return headers.map((h) => `${h.name}: ${h.value}`).join('\n');
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
static tunnelCircuitBreaker(cb) {
|
|
56
|
+
if (cb === undefined || cb === null) return 'Not set';
|
|
57
|
+
return String(cb);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
static tunnelUserAgent(userAgents) {
|
|
61
|
+
if (!userAgents || !userAgents.length) return 'Everyone';
|
|
62
|
+
return userAgents.join('\n');
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
static tunnelDomain(domain) {
|
|
66
|
+
if (!domain) return 'Default';
|
|
67
|
+
return domain;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
static type(type) {
|
|
71
|
+
return type.toUpperCase();
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
static tunnelRegion(region) {
|
|
75
|
+
if (!region) return 'Default';
|
|
76
|
+
return this.region(region);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
static identify(identifyType) {
|
|
80
|
+
if (identifyType === TUNNEL_IDENTIFIED_HTTP1) return 'Ver. 1.1';
|
|
81
|
+
return 'Ver. 2';
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
static terminate(terminate) {
|
|
85
|
+
if (terminate === TLS_TERMINATE_AT_AGENT) return 'AGENT';
|
|
86
|
+
if (terminate === TLS_TERMINATE_AT_REGION) return 'REGION';
|
|
87
|
+
return 'TARGET';
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
static serve(serve) {
|
|
91
|
+
return serve;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
static hostHeader(host) {
|
|
95
|
+
if (!host) return 'Not set';
|
|
96
|
+
return host;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
static basicAuth(login, pass) {
|
|
100
|
+
if (!login || !pass) return 'Not set';
|
|
101
|
+
return `${login}:${pass}`;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
static latency(latency) {
|
|
105
|
+
if (latency < 0) return 'Unreachable';
|
|
106
|
+
if (latency < 1000) return `${latency.toFixed(0)}ms`;
|
|
107
|
+
return `${(latency / 1000).toFixed(0)}s`;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
static yesNo(val) {
|
|
111
|
+
if (!val) return 'No';
|
|
112
|
+
return 'Yes';
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
static entryHost(tunnel) {
|
|
116
|
+
let host = '';
|
|
117
|
+
host += tunnel.subdomain;
|
|
118
|
+
host += '.';
|
|
119
|
+
// todo custom domain
|
|
120
|
+
host += tunnel.region.toLowerCase();
|
|
121
|
+
host += '-';
|
|
122
|
+
host += tunnel.sshId;
|
|
123
|
+
host += '.';
|
|
124
|
+
host += tunnel.domain;
|
|
125
|
+
return host;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
static entry(tunnel) {
|
|
129
|
+
let entry = '';
|
|
130
|
+
if (tunnel.type === TUNNEL_HTTP) entry += 'https://';
|
|
131
|
+
entry += this.entryHost(tunnel);
|
|
132
|
+
if (tunnel.type === TUNNEL_TCP) entry += `:${tunnel.sshForwardPort}`;
|
|
133
|
+
else if (tunnel.type === TUNNEL_TLS) entry += ':443';
|
|
134
|
+
return entry;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
static target(type, target) {
|
|
138
|
+
if (type === TUNNEL_HTTP) {
|
|
139
|
+
let port = '80';
|
|
140
|
+
let host = 'localhost';
|
|
141
|
+
let proto = 'http';
|
|
142
|
+
let m = target.match(TARGET_ONLY_PORT_REGEX);
|
|
143
|
+
if (m) {
|
|
144
|
+
port = m[0];
|
|
145
|
+
} else {
|
|
146
|
+
m = target.match(TARGET_HTTP_REGEX);
|
|
147
|
+
if (m) {
|
|
148
|
+
if (m[2]) proto = m[2];
|
|
149
|
+
if (m[4]) host = m[4];
|
|
150
|
+
if (m[6]) port = m[6];
|
|
151
|
+
else if (proto === 'https') port = '443';
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
let t = `${proto}://${host}`;
|
|
155
|
+
if (proto === 'http' && port !== '80') t += `:${port}`;
|
|
156
|
+
else if (proto === 'https' && port !== '443') t += `:${port}`;
|
|
157
|
+
return t;
|
|
158
|
+
} else {
|
|
159
|
+
let host = 'localhost';
|
|
160
|
+
let port = '80';
|
|
161
|
+
const m = target.match(TARGET_TCP_TLS_REGEX);
|
|
162
|
+
if (m) {
|
|
163
|
+
if (m[3]) port = m[3];
|
|
164
|
+
if (m[2]) host = m[2];
|
|
165
|
+
}
|
|
166
|
+
return `${host}:${port}`;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
module.exports = Format;
|
package/src/index.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
const { Command } = require('commander');
|
|
2
|
+
const commandConfig = require('./command/config.js');
|
|
3
|
+
const Output = require('./output.js');
|
|
4
|
+
const commandTcp = require('./command/tcp.js');
|
|
5
|
+
const commandTls = require('./command/tls.js');
|
|
6
|
+
const commandHttp = require('./command/http.js');
|
|
7
|
+
const commandStart = require('./command/start.js');
|
|
8
|
+
const commandAgent = require('./command/agent.js');
|
|
9
|
+
const logger = require('./logger.js');
|
|
10
|
+
const commandVersion = require('./command/version.js');
|
|
11
|
+
const commandPre = require('./command/pre.js');
|
|
12
|
+
const stream = require('stream');
|
|
13
|
+
const { isDocker } = require('./utils');
|
|
14
|
+
|
|
15
|
+
stream.setDefaultHighWaterMark(false, 67108864);
|
|
16
|
+
process.title = 'bdy';
|
|
17
|
+
process.on('uncaughtException', (err) => {
|
|
18
|
+
logger.fatal(err);
|
|
19
|
+
Output.exitError(err);
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
const program = new Command();
|
|
23
|
+
program.hook('preAction', commandPre);
|
|
24
|
+
program.addCommand(commandConfig);
|
|
25
|
+
program.addCommand(commandTcp);
|
|
26
|
+
program.addCommand(commandTls);
|
|
27
|
+
program.addCommand(commandHttp);
|
|
28
|
+
program.addCommand(commandStart);
|
|
29
|
+
if (!isDocker()) program.addCommand(commandAgent);
|
|
30
|
+
program.addCommand(commandVersion);
|
|
31
|
+
|
|
32
|
+
program.parse();
|
package/src/input.js
ADDED
|
@@ -0,0 +1,283 @@
|
|
|
1
|
+
const Output = require('./output.js');
|
|
2
|
+
const netmask = require('netmask');
|
|
3
|
+
const fs = require('fs');
|
|
4
|
+
const { statSync } = require('fs');
|
|
5
|
+
const tls = require('tls');
|
|
6
|
+
const crypto = require('crypto');
|
|
7
|
+
const {
|
|
8
|
+
isStringRegExp,
|
|
9
|
+
REGION_EU,
|
|
10
|
+
REGION_US,
|
|
11
|
+
TARGET_HTTP_REGEX,
|
|
12
|
+
TARGET_ONLY_PORT_REGEX,
|
|
13
|
+
TARGET_TCP_TLS_REGEX,
|
|
14
|
+
TLS_TERMINATE_AT_AGENT,
|
|
15
|
+
TLS_TERMINATE_AT_REGION,
|
|
16
|
+
TLS_TERMINATE_AT_TARGET,
|
|
17
|
+
TUNNEL_HTTP,
|
|
18
|
+
TUNNEL_TCP,
|
|
19
|
+
TUNNEL_TLS
|
|
20
|
+
} = require('./utils.js');
|
|
21
|
+
const {
|
|
22
|
+
ERR_BA_IS_NOT_VALID,
|
|
23
|
+
ERR_BA_LOGIN_NOT_PROVIDED,
|
|
24
|
+
ERR_BA_PASSWORD_NOT_PROVIDED,
|
|
25
|
+
ERR_CA_PATH_IS_NOT_VALID,
|
|
26
|
+
ERR_CB_THRESHOLD_IS_NOT_VALID,
|
|
27
|
+
ERR_CERT_PATH_IS_NOT_VALID,
|
|
28
|
+
ERR_DIRECTORY_DOES_NOT_EXISTS,
|
|
29
|
+
ERR_DOMAIN_IS_NOT_VALID,
|
|
30
|
+
ERR_KEY_PATH_IS_NOT_VALID,
|
|
31
|
+
ERR_NAME_WITHOUT_ASTERISK,
|
|
32
|
+
ERR_PATH_IS_NOT_DIRECTORY,
|
|
33
|
+
ERR_PORT_IS_NOT_VALID,
|
|
34
|
+
ERR_REGION_IS_NOT_VALID,
|
|
35
|
+
ERR_SUBDOMAIN_IS_NOT_VALID,
|
|
36
|
+
ERR_SUBDOMAIN_IS_TOO_LONG,
|
|
37
|
+
ERR_SUBDOMAIN_IS_TOO_SHORT,
|
|
38
|
+
ERR_TARGET_IS_NOT_VALID,
|
|
39
|
+
ERR_TERMINATE_IS_NOT_VALID,
|
|
40
|
+
ERR_TIMEOUT_IS_NOT_VALID,
|
|
41
|
+
ERR_TYPE_IS_NOT_VALID,
|
|
42
|
+
ERR_USER_AGENT_IS_NOT_VALID,
|
|
43
|
+
ERR_WHITELIST_IS_NOT_VALID,
|
|
44
|
+
ERR_WRONG_CA,
|
|
45
|
+
ERR_WRONG_KEY_CERT
|
|
46
|
+
} = require('./texts.js');
|
|
47
|
+
|
|
48
|
+
class Input {
|
|
49
|
+
static timeout(timeout) {
|
|
50
|
+
const t = parseInt(timeout, 10);
|
|
51
|
+
if (isNaN(t) || t <= 0 || t > 999) {
|
|
52
|
+
Output.exitError(ERR_TIMEOUT_IS_NOT_VALID(timeout));
|
|
53
|
+
}
|
|
54
|
+
return t;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
static serve(serve) {
|
|
58
|
+
if (!serve) return serve;
|
|
59
|
+
try {
|
|
60
|
+
const info = statSync(serve);
|
|
61
|
+
if (!info.isDirectory()) {
|
|
62
|
+
Output.exitError(ERR_PATH_IS_NOT_DIRECTORY(serve));
|
|
63
|
+
}
|
|
64
|
+
} catch (err) {
|
|
65
|
+
Output.exitError(ERR_DIRECTORY_DOES_NOT_EXISTS(serve));
|
|
66
|
+
}
|
|
67
|
+
return serve;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
static circuitBreaker(threshold) {
|
|
71
|
+
if (threshold === undefined) return null;
|
|
72
|
+
const n = parseFloat(threshold);
|
|
73
|
+
if (isNaN(n) || n < 0 || n > 1) {
|
|
74
|
+
Output.exitError(ERR_CB_THRESHOLD_IS_NOT_VALID(n));
|
|
75
|
+
}
|
|
76
|
+
return n;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
static headers(headers) {
|
|
80
|
+
const r = [];
|
|
81
|
+
(headers || []).forEach((h) => {
|
|
82
|
+
if (!h) return;
|
|
83
|
+
const s = h.split(':');
|
|
84
|
+
if (s.length !== 2) return;
|
|
85
|
+
r.push({
|
|
86
|
+
name: s[0],
|
|
87
|
+
value: s[1]
|
|
88
|
+
});
|
|
89
|
+
});
|
|
90
|
+
return r;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
static subdomain(subdomain) {
|
|
94
|
+
if (!/^[a-z0-9-]+$/i.test(subdomain)) {
|
|
95
|
+
Output.exitError(ERR_SUBDOMAIN_IS_NOT_VALID(subdomain));
|
|
96
|
+
}
|
|
97
|
+
if (subdomain.length < 5) {
|
|
98
|
+
Output.exitError(ERR_SUBDOMAIN_IS_TOO_SHORT(subdomain));
|
|
99
|
+
}
|
|
100
|
+
if (subdomain.length > 63) {
|
|
101
|
+
Output.exitError(ERR_SUBDOMAIN_IS_TOO_LONG(subdomain));
|
|
102
|
+
}
|
|
103
|
+
return subdomain;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
static domain(domain) {
|
|
107
|
+
if (!/^(?:[A-z0-9](?:[A-z0-9-]{0,61}[A-z0-9])?\.)+[A-z0-9][A-z0-9-]{0,61}[A-z0-9]$/i.test(domain)) {
|
|
108
|
+
Output.exitError(ERR_DOMAIN_IS_NOT_VALID(domain));
|
|
109
|
+
}
|
|
110
|
+
return domain;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
static auth(auth) {
|
|
114
|
+
const s = auth.split(':');
|
|
115
|
+
if (s.length !== 2) {
|
|
116
|
+
Output.exitError(ERR_BA_IS_NOT_VALID(auth));
|
|
117
|
+
}
|
|
118
|
+
if (!s[0]) {
|
|
119
|
+
Output.exitError(ERR_BA_LOGIN_NOT_PROVIDED);
|
|
120
|
+
}
|
|
121
|
+
if (!s[1]) {
|
|
122
|
+
Output.exitError(ERR_BA_PASSWORD_NOT_PROVIDED);
|
|
123
|
+
}
|
|
124
|
+
return {
|
|
125
|
+
login: s[0],
|
|
126
|
+
password: s[1]
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
static useragents(useragents) {
|
|
131
|
+
if (!useragents || !useragents.length) return [];
|
|
132
|
+
useragents.forEach((ua) => {
|
|
133
|
+
if (isStringRegExp(ua)) {
|
|
134
|
+
try {
|
|
135
|
+
new RegExp(ua);
|
|
136
|
+
} catch (err) {
|
|
137
|
+
Output.exitError(ERR_USER_AGENT_IS_NOT_VALID(ua));
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
});
|
|
141
|
+
return useragents;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
static whitelist(whitelist) {
|
|
145
|
+
if (!whitelist || !whitelist.length) return [];
|
|
146
|
+
for (let i = 0; i < whitelist.length; i += 1) {
|
|
147
|
+
const wh = whitelist[i];
|
|
148
|
+
if (wh === '*') return ['*'];
|
|
149
|
+
try {
|
|
150
|
+
new netmask.Netmask(wh);
|
|
151
|
+
} catch (err) {
|
|
152
|
+
Output.exitError(ERR_WHITELIST_IS_NOT_VALID(wh));
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
return whitelist;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
static region(region) {
|
|
159
|
+
if (!region) Output.exitError(ERR_REGION_IS_NOT_VALID(''));
|
|
160
|
+
region = region.toUpperCase();
|
|
161
|
+
if (![REGION_EU, REGION_US].includes(region)) {
|
|
162
|
+
Output.exitError(ERR_REGION_IS_NOT_VALID(region));
|
|
163
|
+
}
|
|
164
|
+
return region;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
static terminate(terminate) {
|
|
168
|
+
if (!terminate) return TLS_TERMINATE_AT_REGION;
|
|
169
|
+
terminate = terminate.toUpperCase();
|
|
170
|
+
if (![TLS_TERMINATE_AT_AGENT, TLS_TERMINATE_AT_REGION, TLS_TERMINATE_AT_TARGET].includes(terminate)) {
|
|
171
|
+
Output.exitError(ERR_TERMINATE_IS_NOT_VALID(terminate));
|
|
172
|
+
}
|
|
173
|
+
return terminate;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
static target(target, type) {
|
|
177
|
+
if (!target) return '80';
|
|
178
|
+
if (target.length > 200) {
|
|
179
|
+
Output.exitError(ERR_TARGET_IS_NOT_VALID(target));
|
|
180
|
+
}
|
|
181
|
+
target = target.toLowerCase();
|
|
182
|
+
// 80
|
|
183
|
+
// localhost
|
|
184
|
+
// localhost:80
|
|
185
|
+
// http://localhost:80
|
|
186
|
+
// http://localhost
|
|
187
|
+
// http://user:pass@localhost
|
|
188
|
+
// http://user:pass@localhost:80
|
|
189
|
+
let port;
|
|
190
|
+
if ([TUNNEL_TCP, TUNNEL_TLS].includes(type)) {
|
|
191
|
+
const m = target.match(TARGET_TCP_TLS_REGEX);
|
|
192
|
+
if (!m) {
|
|
193
|
+
Output.exitError(ERR_TARGET_IS_NOT_VALID(target));
|
|
194
|
+
}
|
|
195
|
+
port = m[3];
|
|
196
|
+
} else {
|
|
197
|
+
let m = target.match(TARGET_ONLY_PORT_REGEX);
|
|
198
|
+
if (m) {
|
|
199
|
+
port = m[0];
|
|
200
|
+
} else {
|
|
201
|
+
m = target.match(TARGET_HTTP_REGEX);
|
|
202
|
+
if (!m) {
|
|
203
|
+
Output.exitError(ERR_TARGET_IS_NOT_VALID(target));
|
|
204
|
+
} else {
|
|
205
|
+
let proto;
|
|
206
|
+
if (m[2]) proto = m[2];
|
|
207
|
+
else proto = 'http';
|
|
208
|
+
if (m[6]) port = m[6];
|
|
209
|
+
else if (proto === 'https') port = '443';
|
|
210
|
+
else port = '80';
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
Input.port(port);
|
|
215
|
+
return target;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
static port(port) {
|
|
219
|
+
const p = parseInt(port, 10);
|
|
220
|
+
if (isNaN(p) || p <= 0 || p > 65535) Output.exitError(ERR_PORT_IS_NOT_VALID(port));
|
|
221
|
+
return p;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
static type(type) {
|
|
225
|
+
if (!type) return TUNNEL_TCP;
|
|
226
|
+
type = type.toUpperCase();
|
|
227
|
+
if (![TUNNEL_TCP, TUNNEL_TLS, TUNNEL_HTTP].includes(type)) {
|
|
228
|
+
Output.exitError(ERR_TYPE_IS_NOT_VALID(type));
|
|
229
|
+
}
|
|
230
|
+
return type;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
static name(name) {
|
|
234
|
+
if (name.includes('*')) {
|
|
235
|
+
Output.exitError(ERR_NAME_WITHOUT_ASTERISK);
|
|
236
|
+
}
|
|
237
|
+
return name;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
static ca(caPath) {
|
|
241
|
+
let cert;
|
|
242
|
+
try {
|
|
243
|
+
cert = fs.readFileSync(caPath, 'utf8');
|
|
244
|
+
} catch (err) {
|
|
245
|
+
Output.exitError(ERR_CA_PATH_IS_NOT_VALID(caPath));
|
|
246
|
+
}
|
|
247
|
+
try {
|
|
248
|
+
new crypto.X509Certificate(cert);
|
|
249
|
+
} catch (err) {
|
|
250
|
+
Output.exitError(ERR_WRONG_CA(caPath));
|
|
251
|
+
}
|
|
252
|
+
return cert;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
static keyCert(keyPath, certPath) {
|
|
256
|
+
let key;
|
|
257
|
+
let cert;
|
|
258
|
+
try {
|
|
259
|
+
key = fs.readFileSync(keyPath, 'utf8');
|
|
260
|
+
} catch (err) {
|
|
261
|
+
Output.exitError(ERR_KEY_PATH_IS_NOT_VALID(keyPath));
|
|
262
|
+
}
|
|
263
|
+
try {
|
|
264
|
+
cert = fs.readFileSync(certPath, 'utf8');
|
|
265
|
+
} catch (err) {
|
|
266
|
+
Output.exitError(ERR_CERT_PATH_IS_NOT_VALID(certPath));
|
|
267
|
+
}
|
|
268
|
+
try {
|
|
269
|
+
tls.createSecureContext({
|
|
270
|
+
cert,
|
|
271
|
+
key
|
|
272
|
+
});
|
|
273
|
+
} catch (err) {
|
|
274
|
+
Output.exitError(ERR_WRONG_KEY_CERT);
|
|
275
|
+
}
|
|
276
|
+
return {
|
|
277
|
+
cert,
|
|
278
|
+
key
|
|
279
|
+
};
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
module.exports = Input;
|
package/src/logger.js
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
const pino = require('pino');
|
|
2
|
+
const { resolve } = require('path');
|
|
3
|
+
const fs = require('fs');
|
|
4
|
+
const {
|
|
5
|
+
getHomeDirectory,
|
|
6
|
+
} = require('./utils.js');
|
|
7
|
+
|
|
8
|
+
class Logger {
|
|
9
|
+
rootPath = null;
|
|
10
|
+
logPath = null;
|
|
11
|
+
log1Path = null;
|
|
12
|
+
logStream = null;
|
|
13
|
+
p = null;
|
|
14
|
+
ts = null;
|
|
15
|
+
constructor() {
|
|
16
|
+
this.rootPath = getHomeDirectory();
|
|
17
|
+
}
|
|
18
|
+
checkLogSize() {
|
|
19
|
+
try {
|
|
20
|
+
const s = fs.statSync(this.logPath);
|
|
21
|
+
if (s.size > 5242880) {
|
|
22
|
+
fs.copyFileSync(this.logPath, this.log1Path);
|
|
23
|
+
fs.truncateSync(this.logPath);
|
|
24
|
+
}
|
|
25
|
+
} catch {
|
|
26
|
+
// do nothing
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
getPino() {
|
|
30
|
+
if (!this.p) {
|
|
31
|
+
this.logPath = resolve(this.rootPath, 'cli.log');
|
|
32
|
+
this.log1Path = resolve(this.rootPath, 'cli.1.log');
|
|
33
|
+
this.logStream = fs.openSync(this.logPath, 'w');
|
|
34
|
+
this.p = pino({
|
|
35
|
+
level: process.env.DEBUG === '1' ? 'debug' : 'info',
|
|
36
|
+
timestamp: () => `, "time": "${new Date().toISOString()}"`,
|
|
37
|
+
formatters: {
|
|
38
|
+
bindings: () => ({}),
|
|
39
|
+
level: (label) => ({
|
|
40
|
+
level: label.toUpperCase()
|
|
41
|
+
})
|
|
42
|
+
}
|
|
43
|
+
}, pino.destination(this.logStream));
|
|
44
|
+
this.checkLogSize();
|
|
45
|
+
this.ts = setInterval(() => {
|
|
46
|
+
this.checkLogSize();
|
|
47
|
+
}, 30000);
|
|
48
|
+
}
|
|
49
|
+
return this.p;
|
|
50
|
+
}
|
|
51
|
+
error(...args) {
|
|
52
|
+
this.getPino().error(...args);
|
|
53
|
+
}
|
|
54
|
+
info(...args) {
|
|
55
|
+
this.getPino().info(...args);
|
|
56
|
+
}
|
|
57
|
+
debug(...args){
|
|
58
|
+
this.getPino().debug(...args);
|
|
59
|
+
}
|
|
60
|
+
fatal(...args){
|
|
61
|
+
this.getPino().fatal(...args);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
stack() {
|
|
65
|
+
this.getPino().info(new Error().stack);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
changeRootPath(path) {
|
|
69
|
+
if (this.ts) {
|
|
70
|
+
clearInterval(this.ts);
|
|
71
|
+
this.ts = null;
|
|
72
|
+
}
|
|
73
|
+
if (this.logStream) {
|
|
74
|
+
try {
|
|
75
|
+
this.logStream.close();
|
|
76
|
+
} catch {
|
|
77
|
+
// do nothing
|
|
78
|
+
}
|
|
79
|
+
this.logStream = null;
|
|
80
|
+
}
|
|
81
|
+
this.p = null;
|
|
82
|
+
this.rootPath = path;
|
|
83
|
+
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
module.exports = new Logger();
|