bdy 1.7.60-dev → 1.8.3-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.
Files changed (95) hide show
  1. package/package.json +42 -11
  2. package/.eslintrc.yml +0 -23
  3. package/bin/cli.js +0 -5
  4. package/dockerfile +0 -15
  5. package/link.sh +0 -3
  6. package/src/agent/linux.js +0 -127
  7. package/src/agent/manager.js +0 -404
  8. package/src/agent/osx.js +0 -150
  9. package/src/agent/socket/tunnel.js +0 -232
  10. package/src/agent/socket.js +0 -260
  11. package/src/agent/system.js +0 -205
  12. package/src/agent/wait.js +0 -20
  13. package/src/agent/windows.js +0 -168
  14. package/src/agent.js +0 -256
  15. package/src/api/agent.js +0 -95
  16. package/src/api/buddy.js +0 -131
  17. package/src/api/socket.js +0 -148
  18. package/src/cfg.js +0 -239
  19. package/src/command/agent/install.js +0 -116
  20. package/src/command/agent/restart.js +0 -27
  21. package/src/command/agent/run.js +0 -16
  22. package/src/command/agent/start.js +0 -27
  23. package/src/command/agent/status.js +0 -44
  24. package/src/command/agent/stop.js +0 -27
  25. package/src/command/agent/tunnel/http.js +0 -45
  26. package/src/command/agent/tunnel/list.js +0 -26
  27. package/src/command/agent/tunnel/remove.js +0 -29
  28. package/src/command/agent/tunnel/start.js +0 -37
  29. package/src/command/agent/tunnel/status.js +0 -31
  30. package/src/command/agent/tunnel/tcp.js +0 -45
  31. package/src/command/agent/tunnel/tls.js +0 -45
  32. package/src/command/agent/tunnel.js +0 -20
  33. package/src/command/agent/uninstall.js +0 -36
  34. package/src/command/agent/update.js +0 -42
  35. package/src/command/agent/version.js +0 -22
  36. package/src/command/agent.js +0 -26
  37. package/src/command/config/add/http.js +0 -31
  38. package/src/command/config/add/tcp.js +0 -31
  39. package/src/command/config/add/tls.js +0 -31
  40. package/src/command/config/add.js +0 -12
  41. package/src/command/config/get/region.js +0 -12
  42. package/src/command/config/get/timeout.js +0 -12
  43. package/src/command/config/get/token.js +0 -12
  44. package/src/command/config/get/tunnel.js +0 -20
  45. package/src/command/config/get/tunnels.js +0 -12
  46. package/src/command/config/get/whitelist.js +0 -12
  47. package/src/command/config/get.js +0 -18
  48. package/src/command/config/remove/tunnel.js +0 -21
  49. package/src/command/config/remove.js +0 -8
  50. package/src/command/config/set/region.js +0 -18
  51. package/src/command/config/set/timeout.js +0 -21
  52. package/src/command/config/set/token.js +0 -18
  53. package/src/command/config/set/whitelist.js +0 -18
  54. package/src/command/config/set.js +0 -14
  55. package/src/command/config.js +0 -14
  56. package/src/command/http.js +0 -33
  57. package/src/command/pre.js +0 -45
  58. package/src/command/start.js +0 -30
  59. package/src/command/tcp.js +0 -31
  60. package/src/command/tls.js +0 -31
  61. package/src/command/version.js +0 -10
  62. package/src/format.js +0 -171
  63. package/src/index.js +0 -38
  64. package/src/input.js +0 -283
  65. package/src/logger.js +0 -92
  66. package/src/output/interactive/tunnel.js +0 -871
  67. package/src/output/noninteractive/agent/tunnels.js +0 -32
  68. package/src/output/noninteractive/config/tunnel.js +0 -51
  69. package/src/output/noninteractive/config/tunnels.js +0 -19
  70. package/src/output/noninteractive/tunnel.js +0 -79
  71. package/src/output.js +0 -131
  72. package/src/server/cert.js +0 -51
  73. package/src/server/http1.js +0 -79
  74. package/src/server/http2.js +0 -79
  75. package/src/server/sftp.js +0 -474
  76. package/src/server/ssh.js +0 -107
  77. package/src/server/tls.js +0 -41
  78. package/src/ssh/client.js +0 -204
  79. package/src/texts.js +0 -445
  80. package/src/tunnel/agent.js +0 -100
  81. package/src/tunnel/compression.js +0 -32
  82. package/src/tunnel/dns.js +0 -55
  83. package/src/tunnel/html/404.html +0 -129
  84. package/src/tunnel/html/503.html +0 -136
  85. package/src/tunnel/html.js +0 -32
  86. package/src/tunnel/http/log.js +0 -204
  87. package/src/tunnel/http/serve.js +0 -127
  88. package/src/tunnel/http/stream.js +0 -47
  89. package/src/tunnel/http.js +0 -406
  90. package/src/tunnel/identification.js +0 -95
  91. package/src/tunnel/latency.js +0 -69
  92. package/src/tunnel/tcp.js +0 -85
  93. package/src/tunnel.js +0 -713
  94. package/src/utils.js +0 -577
  95. package/unlink.sh +0 -3
package/src/agent/osx.js DELETED
@@ -1,150 +0,0 @@
1
- const AgentSystem = require('./system');
2
- const { exec } = require('child_process');
3
- const path = require('path');
4
- const logger = require('../logger');
5
- const { LOG_AGENT_SYSTEM_DIR,
6
- LOG_AGENT_DOWNLOADING_ARCHIVE,
7
- LOG_AGENT_EXTRACTING_ARCHIVE,
8
- LOG_AGENT_SYSTEM_SERVICE_CONFIG,
9
- LOG_AGENT_ENABLING_SYSTEM,
10
- LOG_AGENT_ENABLED,
11
- LOG_AGENT_STOPPING_SYSTEM,
12
- LOG_AGENT_STARTING_SYSTEM
13
- } = require('../texts');
14
-
15
- class AgentOsx extends AgentSystem {
16
-
17
- hasAdminRights() {
18
- return new Promise((resolve) => {
19
- resolve(process.getuid() === 0);
20
- });
21
- }
22
-
23
- isSupported() {
24
- // cant check for process.arch because of rosetta (process.arch returns x64 for m1)
25
- return Promise.resolve(true);
26
- }
27
-
28
- isEnabled() {
29
- return new Promise((resolve) => {
30
- this.launchCtl('print system/bdy | grep domain').then((str) => {
31
- resolve(!!str && !!str.match(/domain\s=\ssystem/i));
32
- }).catch(() => {
33
- resolve(false);
34
- });
35
- });
36
- }
37
-
38
- getBinaryArchive() {
39
- return 'darwin-arm64.tar.gz';
40
- }
41
-
42
- getSystemConfigDir() {
43
- return '/Library/Application Support/bdy';
44
- }
45
-
46
- getSystemConfigPath() {
47
- return path.join(this.getSystemConfigDir(), 'agent.json');
48
- }
49
-
50
- getServicePath() {
51
- return '/Library/LaunchDaemons/bdy.plist';
52
- }
53
-
54
- getServiceConfig(id, host, token, port, start, user, debug) {
55
- const cli = this.getSystemBinaryPath();
56
- let cfg = `<?xml version='1.0' encoding='UTF-8'?>
57
- <!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
58
- <plist version='1.0'>
59
- <dict>
60
- <key>Label</key>
61
- <string>bdy</string>
62
- <key>ProgramArguments</key>
63
- <array>
64
- <string>${cli}</string>
65
- <string>agent</string>
66
- <string>run</string>
67
- <string>--id</string>
68
- <string>${id}</string>
69
- <string>--host</string>
70
- <string>${host}</string>
71
- <string>--token</string>
72
- <string>${token}</string>
73
- <string>--port</string>
74
- <string>${port}</string>
75
- <string>--start</string>
76
- <string>${!!start}</string>
77
- </array>
78
- <key>EnvironmentVariables</key>
79
- <dict>
80
- <key>DEBUG</key>
81
- <string>${debug ? 1 : 0}</string>
82
- </dict>
83
- <key>KeepAlive</key>
84
- <true/>
85
- <key>RunAtLoad</key>
86
- <true/>`;
87
- if (user) {
88
- cfg += `
89
- <key>UserName</key>
90
- <string>${user}</string>`;
91
- }
92
- cfg += `
93
- </dict>
94
- </plist>`;
95
- return cfg;
96
- }
97
-
98
- async enable(id, host, token, port, start, user, pass, debug) {
99
- try {
100
- logger.info(LOG_AGENT_SYSTEM_DIR);
101
- await this.ensureSystemConfigDir();
102
- logger.info(LOG_AGENT_DOWNLOADING_ARCHIVE);
103
- await this.downloadBinaryArchive();
104
- logger.info(LOG_AGENT_EXTRACTING_ARCHIVE);
105
- await this.extractBinaryArchive();
106
- logger.info(LOG_AGENT_SYSTEM_SERVICE_CONFIG);
107
- await this.saveFile(this.getServicePath(), this.getServiceConfig(id, host, token, port, start, user, debug));
108
- logger.info(LOG_AGENT_ENABLING_SYSTEM);
109
- await this.launchCtl(`bootstrap system ${this.getServicePath()}`);
110
- logger.info(LOG_AGENT_ENABLED);
111
- } catch (err) {
112
- logger.info(err);
113
- throw err;
114
- }
115
- }
116
-
117
- async update() {
118
- try {
119
- logger.info(LOG_AGENT_STOPPING_SYSTEM);
120
- await this.launchCtl('bootout system/bdy');
121
- logger.info(LOG_AGENT_SYSTEM_DIR);
122
- await this.ensureSystemConfigDir();
123
- logger.info(LOG_AGENT_DOWNLOADING_ARCHIVE);
124
- await this.downloadBinaryArchive();
125
- logger.info(LOG_AGENT_EXTRACTING_ARCHIVE);
126
- await this.extractBinaryArchive();
127
- logger.info(LOG_AGENT_STARTING_SYSTEM);
128
- await this.launchCtl(`bootstrap system ${this.getServicePath()}`);
129
- logger.info(LOG_AGENT_ENABLED);
130
- } catch (err) {
131
- logger.error(err);
132
- throw err;
133
- }
134
- }
135
-
136
- disable() {
137
- return this.launchCtl('bootout system/bdy');
138
- }
139
-
140
- launchCtl(cmd) {
141
- return new Promise((resolve, reject) => {
142
- exec(`launchctl ${cmd}`, (err, stdout, stderr) => {
143
- if (!err) resolve(stdout, stderr);
144
- else reject(err, stderr);
145
- });
146
- });
147
- }
148
- }
149
-
150
- module.exports = AgentOsx;
@@ -1,232 +0,0 @@
1
- const { ERR_TUNNEL_NOT_FOUND } = require('../../texts.js');
2
- const EventEmitter = require('events');
3
- const {
4
- AGENT_SOCKET_MESSAGE_READY,
5
- AGENT_SOCKET_MESSAGE_TUNNEL,
6
- AGENT_SOCKET_MESSAGE_TUNNEL_STOPPED,
7
- AGENT_SOCKET_MESSAGE_TUNNEL_OPEN,
8
- AGENT_SOCKET_MESSAGE_TUNNEL_CLOSED,
9
- AGENT_SOCKET_MESSAGE_TUNNEL_TCP_OPEN,
10
- AGENT_SOCKET_MESSAGE_TUNNEL_TCP_CLOSED,
11
- AGENT_SOCKET_MESSAGE_TUNNEL_TLS_OPEN,
12
- AGENT_SOCKET_MESSAGE_TUNNEL_TLS_CLOSED,
13
- AGENT_SOCKET_MESSAGE_TUNNEL_HTTP_IDENTIFIED,
14
- AGENT_SOCKET_MESSAGE_TUNNEL_HTTP_REQUESTS,
15
- AGENT_SOCKET_MESSAGE_TUNNEL_HTTP_REQUEST,
16
- AGENT_SOCKET_MESSAGE_TUNNEL_HTTP_RESPONSE,
17
- AGENT_SOCKET_MESSAGE_TUNNEL_HTTP_RETRY,
18
- TUNNEL_HTTP_LOG_MAX_REQUESTS,
19
- TUNNEL_EVENT_OPEN,
20
- TUNNEL_EVENT_CLOSED,
21
- TUNNEL_EVENT_TCP_OPEN,
22
- TUNNEL_EVENT_TCP_CLOSED,
23
- TUNNEL_EVENT_TLS_OPEN,
24
- TUNNEL_EVENT_TLS_CLOSED,
25
- TUNNEL_EVENT_HTTP_IDENTIFIED,
26
- TUNNEL_EVENT_HTTP_REQUEST,
27
- TUNNEL_EVENT_HTTP_RESPONSE,
28
- TUNNEL_EVENT_STOPPED
29
- } = require('../../utils');
30
-
31
- class AgentSocketTunnel extends EventEmitter {
32
- constructor(socket, id) {
33
- super();
34
- this.id = id;
35
- this.ready = false;
36
- this.closed = false;
37
- this.socket = socket;
38
- this.socket.on('close', () => this.onClose());
39
- this.socket.on('message', (msg) => this.onMessage(msg));
40
- this.socket.send(JSON.stringify({
41
- message: AGENT_SOCKET_MESSAGE_READY
42
- }));
43
- }
44
-
45
- close() {
46
- if (this.socket) {
47
- this.socket.close();
48
- }
49
- }
50
-
51
- onTunnelRequests(data) {
52
- this.httpLog = {
53
- requests: data.requests.map((r) => {
54
- r.requestBody = {
55
- data: Buffer.from(r.requestBody.data, 'base64'),
56
- tooLarge: r.requestBody.tooLarge,
57
- realLength: r.requestBody.realLength
58
- };
59
- r.responseBody = {
60
- data: Buffer.from(r.responseBody.data, 'base64'),
61
- tooLarge: r.responseBody.tooLarge,
62
- realLength: r.responseBody.realLength
63
- };
64
- return r;
65
- })
66
- };
67
- }
68
-
69
- onTunnelData(data) {
70
- if (data.regionLatency) this.regionLatency = data.regionLatency;
71
- if (data.targetLatency) this.targetLatency = data.targetLatency;
72
- if (data.identify) this.identify = data.identify;
73
- if (data.dns) this.dns = data.dns;
74
- if (data.connections) this.connections = data.connections;
75
- this.totalConnections = data.totalConnections;
76
- this.serve = data.serve;
77
- this.log = data.log;
78
- this.status = data.status;
79
- this.region = data.region;
80
- this.type = data.type;
81
- this.target = data.target;
82
- this.terminate = data.terminate;
83
- this.id = data.id;
84
- this.subdomain = data.subdomain;
85
- this.sshId = data.sshId;
86
- this.domain = data.domain;
87
- this.sshForwardPort = data.sshForwardPort;
88
- }
89
-
90
- upsertHttpLog(data) {
91
- if (!this.httpLog || !this.httpLog.requests || !this.log) return;
92
- /**
93
- * @type {object}
94
- */
95
- const r = this.httpLog.requests.find((r) => r.id === data.id);
96
- if (!r) {
97
- data.requestBody = {
98
- data: Buffer.from(data.requestBody.data, 'base64'),
99
- tooLarge: data.requestBody.tooLarge,
100
- realLength: data.requestBody.realLength
101
- };
102
- data.responseBody = {
103
- data: Buffer.from(data.responseBody.data, 'base64'),
104
- tooLarge: data.responseBody.tooLarge,
105
- realLength: data.responseBody.realLength
106
- };
107
- this.httpLog.requests.unshift(data);
108
- } else {
109
- r.finished = data.finished;
110
- r.time = data.time;
111
- r.httpVersion = data.httpVersion;
112
- r.requestBody = {
113
- data: Buffer.from(data.requestBody.data, 'base64'),
114
- tooLarge: data.requestBody.tooLarge,
115
- realLength: data.requestBody.realLength
116
- };
117
- r.responseBody = {
118
- data: Buffer.from(data.responseBody.data, 'base64'),
119
- tooLarge: data.responseBody.tooLarge,
120
- realLength: data.responseBody.realLength
121
- };
122
- r.headers = data.headers;
123
- r.status = data.status;
124
- r.url = data.url;
125
- r.method = data.method;
126
- }
127
- if (this.httpLog.requests.length > TUNNEL_HTTP_LOG_MAX_REQUESTS) this.httpLog.requests.pop();
128
- }
129
-
130
- onMessage(msg) {
131
- try {
132
- const json = JSON.parse(msg.toString('utf8'));
133
- if (json.message === AGENT_SOCKET_MESSAGE_TUNNEL) {
134
- this.onTunnelData(json);
135
- this.ready = true;
136
- return;
137
- }
138
- if (json.message === AGENT_SOCKET_MESSAGE_TUNNEL_HTTP_REQUESTS) {
139
- this.onTunnelRequests(json);
140
- return;
141
- }
142
- if (json.message === AGENT_SOCKET_MESSAGE_TUNNEL_OPEN) {
143
- this.emit(TUNNEL_EVENT_OPEN, json);
144
- return;
145
- }
146
- if (json.message === AGENT_SOCKET_MESSAGE_TUNNEL_CLOSED) {
147
- this.emit(TUNNEL_EVENT_CLOSED, json);
148
- return;
149
- }
150
- if (json.message === AGENT_SOCKET_MESSAGE_TUNNEL_TCP_OPEN) {
151
- this.emit(TUNNEL_EVENT_TCP_OPEN, json);
152
- return;
153
- }
154
- if (json.message === AGENT_SOCKET_MESSAGE_TUNNEL_TCP_CLOSED) {
155
- this.emit(TUNNEL_EVENT_TCP_CLOSED, json);
156
- return;
157
- }
158
- if (json.message === AGENT_SOCKET_MESSAGE_TUNNEL_TLS_OPEN) {
159
- this.emit(TUNNEL_EVENT_TLS_OPEN, json);
160
- return;
161
- }
162
- if (json.message === AGENT_SOCKET_MESSAGE_TUNNEL_TLS_CLOSED) {
163
- this.emit(TUNNEL_EVENT_TLS_CLOSED, json);
164
- return;
165
- }
166
- if (json.message === AGENT_SOCKET_MESSAGE_TUNNEL_HTTP_IDENTIFIED) {
167
- this.emit(TUNNEL_EVENT_HTTP_IDENTIFIED, json, json.identify);
168
- return;
169
- }
170
- if (json.message === AGENT_SOCKET_MESSAGE_TUNNEL_HTTP_REQUEST) {
171
- this.upsertHttpLog(json.logRequest);
172
- this.emit(TUNNEL_EVENT_HTTP_REQUEST, json, json.logRequest);
173
- return;
174
- }
175
- if (json.message === AGENT_SOCKET_MESSAGE_TUNNEL_HTTP_RESPONSE) {
176
- this.upsertHttpLog(json.logRequest);
177
- this.emit(TUNNEL_EVENT_HTTP_RESPONSE, json, json.logRequest);
178
- return;
179
- }
180
- if (json.message === AGENT_SOCKET_MESSAGE_TUNNEL_STOPPED) {
181
- this.emit(TUNNEL_EVENT_STOPPED, json);
182
- this.close();
183
- }
184
- } catch (err) {
185
- this.close();
186
- }
187
- }
188
-
189
- onClose() {
190
- this.emit(TUNNEL_EVENT_STOPPED);
191
- try {
192
- this.socket.removeAllListeners();
193
- this.socket.close();
194
- } catch (err) {
195
- // do nothing
196
- }
197
- this.closed = true;
198
- this.socket = null;
199
- }
200
-
201
- retryHttpLogRequest(logRequest) {
202
- if (this.socket) {
203
- this.socket.send(JSON.stringify({
204
- message: AGENT_SOCKET_MESSAGE_TUNNEL_HTTP_RETRY,
205
- logRequest
206
- }));
207
- }
208
- }
209
-
210
- waitForReady() {
211
- return new Promise((resolve, reject) => {
212
- const ts1 = setInterval(() => {
213
- if (this.ready) {
214
- clearInterval(ts1);
215
- clearTimeout(ts2);
216
- resolve();
217
- } else if (this.closed) {
218
- clearInterval(ts1);
219
- clearTimeout(ts2);
220
- reject(new Error(ERR_TUNNEL_NOT_FOUND(this.id)));
221
- }
222
- }, 100);
223
- const ts2 = setTimeout(() => {
224
- clearInterval(ts1);
225
- clearTimeout(ts2);
226
- reject(new Error(ERR_TUNNEL_NOT_FOUND(this.id)));
227
- }, 3000);
228
- });
229
- }
230
- }
231
-
232
- module.exports = AgentSocketTunnel;
@@ -1,260 +0,0 @@
1
- const {
2
- AGENT_SOCKET_MESSAGE_READY,
3
- AGENT_SOCKET_MESSAGE_TUNNEL,
4
- AGENT_SOCKET_MESSAGE_TUNNEL_STOPPED,
5
- AGENT_SOCKET_MESSAGE_TUNNEL_OPEN,
6
- AGENT_SOCKET_MESSAGE_TUNNEL_CLOSED,
7
- AGENT_SOCKET_MESSAGE_TUNNEL_TCP_OPEN,
8
- AGENT_SOCKET_MESSAGE_TUNNEL_TCP_CLOSED,
9
- AGENT_SOCKET_MESSAGE_TUNNEL_TLS_OPEN,
10
- AGENT_SOCKET_MESSAGE_TUNNEL_TLS_CLOSED,
11
- AGENT_SOCKET_MESSAGE_TUNNEL_HTTP_IDENTIFIED,
12
- AGENT_SOCKET_MESSAGE_TUNNEL_HTTP_REQUESTS,
13
- AGENT_SOCKET_MESSAGE_TUNNEL_HTTP_REQUEST,
14
- AGENT_SOCKET_MESSAGE_TUNNEL_HTTP_RESPONSE,
15
- AGENT_SOCKET_MESSAGE_TUNNEL_HTTP_RETRY,
16
- TUNNEL_EVENT_OPEN,
17
- TUNNEL_EVENT_CLOSED,
18
- TUNNEL_EVENT_TCP_OPEN,
19
- TUNNEL_EVENT_TCP_CLOSED,
20
- TUNNEL_EVENT_TLS_OPEN,
21
- TUNNEL_EVENT_TLS_CLOSED,
22
- TUNNEL_EVENT_HTTP_IDENTIFIED,
23
- TUNNEL_EVENT_HTTP_REQUEST,
24
- TUNNEL_EVENT_HTTP_RESPONSE,
25
- TUNNEL_EVENT_STOPPED
26
- } = require('../utils');
27
-
28
- class AgentSocket {
29
- constructor(socket, tunnel) {
30
- this.tunnel = tunnel;
31
- this.socket = socket;
32
- this.ready = false;
33
- this.socket.on('close', () => this.close());
34
- this.socket.on('message', (msg) => this.onMessage(msg));
35
- this.boundEventStopped = (t) => this.tunnelStopped(t);
36
- this.boundEventOpen = (t) => this.tunnelOpen(t);
37
- this.boundEventClosed = (t) => this.tunnelClosed(t);
38
- this.boundEventTcpOpen = (t) => this.tunnelTcpOpen(t);
39
- this.boundEventTcpClosed = (t) => this.tunnelTcpClosed(t);
40
- this.boundEventTlsOpen = (t) => this.tunnelTlsOpen(t);
41
- this.boundEventTlsClosed = (t) => this.tunnelTlsClosed(t);
42
- this.boundEventHttpIdentified = (t, type) => this.tunnelHttpIdentified(t, type);
43
- this.boundEventHttpRequest = (t, logRequest) => this.tunnelHttpRequest(t, logRequest);
44
- this.boundEventHttpResponse = (t, logRequest) => this.tunnelHttpResponse(t, logRequest);
45
- this.tunnel.on(TUNNEL_EVENT_STOPPED, this.boundEventStopped);
46
- this.tunnel.on(TUNNEL_EVENT_OPEN, this.boundEventOpen);
47
- this.tunnel.on(TUNNEL_EVENT_CLOSED, this.boundEventClosed);
48
- this.tunnel.on(TUNNEL_EVENT_TCP_OPEN, this.boundEventTcpOpen);
49
- this.tunnel.on(TUNNEL_EVENT_TCP_CLOSED, this.boundEventTcpClosed);
50
- this.tunnel.on(TUNNEL_EVENT_TLS_OPEN, this.boundEventTlsOpen);
51
- this.tunnel.on(TUNNEL_EVENT_TLS_CLOSED, this.boundEventTlsClosed);
52
- this.tunnel.on(TUNNEL_EVENT_HTTP_IDENTIFIED, this.boundEventHttpIdentified);
53
- this.tunnel.on(TUNNEL_EVENT_HTTP_REQUEST, this.boundEventHttpRequest);
54
- this.tunnel.on(TUNNEL_EVENT_HTTP_RESPONSE, this.boundEventHttpResponse);
55
- }
56
-
57
- onMessage(message) {
58
- try {
59
- const json = JSON.parse(message.toString('utf8'));
60
- if (json.message === AGENT_SOCKET_MESSAGE_READY) {
61
- this.ready = true;
62
- this.ts = setInterval(() => {
63
- this.sendTunnel();
64
- }, 100);
65
- this.sendTunnel();
66
- this.sendRequests();
67
- return;
68
- }
69
- if (json.message === AGENT_SOCKET_MESSAGE_TUNNEL_HTTP_RETRY && this.ready) {
70
- this.retryHttpRequest(json.logRequest);
71
- }
72
- } catch (err) {
73
- // do nothing
74
- }
75
- }
76
-
77
- retryHttpRequest(logRequest) {
78
- this.tunnel.retryHttpLogRequest(logRequest);
79
- }
80
-
81
- send(message, data = {}) {
82
- if (this.socket && this.ready) {
83
- this.socket.send(JSON.stringify({
84
- message,
85
- ...data
86
- }));
87
- }
88
- }
89
-
90
- sendEvent(message, t, data = {}) {
91
- this.send(message, {
92
- type: t.type,
93
- serve: t.serve,
94
- target: t.target,
95
- subdomain: t.subdomain,
96
- domain: t.domain,
97
- sshId: t.sshId,
98
- sshForwardPort: t.sshForwardPort,
99
- ...data
100
- });
101
- }
102
-
103
- tunnelOpen(t) {
104
- this.sendEvent(AGENT_SOCKET_MESSAGE_TUNNEL_OPEN, t);
105
- }
106
-
107
- tunnelClosed(t) {
108
- this.sendEvent(AGENT_SOCKET_MESSAGE_TUNNEL_CLOSED, t);
109
- }
110
-
111
- tunnelTcpOpen(t) {
112
- this.sendEvent(AGENT_SOCKET_MESSAGE_TUNNEL_TCP_OPEN, t);
113
- }
114
-
115
- tunnelTcpClosed(t) {
116
- this.sendEvent(AGENT_SOCKET_MESSAGE_TUNNEL_TCP_CLOSED, t);
117
- }
118
-
119
- tunnelTlsOpen(t) {
120
- this.sendEvent(AGENT_SOCKET_MESSAGE_TUNNEL_TLS_OPEN, t);
121
- }
122
-
123
- tunnelTlsClosed(t) {
124
- this.sendEvent(AGENT_SOCKET_MESSAGE_TUNNEL_TLS_CLOSED, t);
125
- }
126
-
127
- tunnelHttpIdentified(t, type) {
128
- this.sendEvent(AGENT_SOCKET_MESSAGE_TUNNEL_HTTP_IDENTIFIED, t, {
129
- identify: type
130
- });
131
- }
132
-
133
- logRequestToSocket(logRequest) {
134
- return {
135
- id: logRequest.id,
136
- finished: logRequest.finished,
137
- time: logRequest.time,
138
- httpVersion: logRequest.httpVersion,
139
- requestBody: {
140
- data: logRequest.requestBody.data.toString('base64'),
141
- tooLarge: logRequest.requestBody.tooLarge,
142
- realLength: logRequest.requestBody.realLength
143
- },
144
- responseBody: {
145
- data: logRequest.responseBody.data.toString('base64'),
146
- tooLarge: logRequest.responseBody.tooLarge,
147
- realLength: logRequest.responseBody.realLength
148
- },
149
- headers: logRequest.headers,
150
- status: logRequest.status,
151
- url: logRequest.url,
152
- method: logRequest.method
153
- };
154
- }
155
-
156
- tunnelHttpRequest(t, logRequest) {
157
- this.sendEvent(AGENT_SOCKET_MESSAGE_TUNNEL_HTTP_REQUEST, t, {
158
- logRequest: this.logRequestToSocket(logRequest)
159
- });
160
- }
161
-
162
- tunnelHttpResponse(t, logRequest) {
163
- this.sendEvent(AGENT_SOCKET_MESSAGE_TUNNEL_HTTP_RESPONSE, t, {
164
- logRequest: this.logRequestToSocket(logRequest)
165
- });
166
- }
167
-
168
- tunnelStopped(t) {
169
- this.sendEvent(AGENT_SOCKET_MESSAGE_TUNNEL_STOPPED, t);
170
- this.close();
171
- }
172
-
173
- sendRequests() {
174
- let requests = [];
175
- if (this.tunnel.httpLog) {
176
- requests = this.tunnel.httpLog.requests.map((lr) => this.logRequestToSocket(lr));
177
- }
178
- this.send(AGENT_SOCKET_MESSAGE_TUNNEL_HTTP_REQUESTS, {
179
- requests
180
- });
181
- }
182
-
183
- sendTunnel() {
184
- const connections = {};
185
- Object.keys(this.tunnel.connections).forEach((id) => {
186
- connections[id] = true;
187
- });
188
- const regionLatency = {
189
- latency: -1
190
- };
191
- if (this.tunnel.regionLatency) {
192
- regionLatency.latency = this.tunnel.regionLatency.latency;
193
- }
194
- const targetLatency = {
195
- latency: -1
196
- };
197
- if (this.tunnel.targetLatency) {
198
- targetLatency.latency = this.tunnel.targetLatency.latency;
199
- }
200
- let identify = null;
201
- if (this.tunnel.identify) {
202
- identify = {
203
- type: this.tunnel.identify.type
204
- };
205
- }
206
- const dns = {
207
- checking: false,
208
- valid: false
209
- };
210
- if (this.tunnel.dns) {
211
- dns.checking = this.tunnel.dns.checking;
212
- dns.valid = this.tunnel.dns.valid;
213
- }
214
- this.send(AGENT_SOCKET_MESSAGE_TUNNEL, {
215
- regionLatency,
216
- targetLatency,
217
- connections,
218
- identify,
219
- dns,
220
- totalConnections: this.tunnel.totalConnections,
221
- serve: this.tunnel.serve,
222
- log: this.tunnel.log,
223
- status: this.tunnel.status,
224
- region: this.tunnel.region,
225
- type: this.tunnel.type,
226
- target: this.tunnel.target,
227
- terminate: this.tunnel.terminate,
228
- id: this.tunnel.id,
229
- subdomain: this.tunnel.subdomain,
230
- domain: this.tunnel.domain,
231
- sshId: this.tunnel.sshId,
232
- sshForwardPort: this.tunnel.sshForwardPort
233
- });
234
- }
235
-
236
- close() {
237
- clearInterval(this.ts);
238
- try {
239
- this.socket.removeAllListeners();
240
- this.socket.close();
241
- } catch (err) {
242
- // do nothing
243
- }
244
- this.ts = null;
245
- this.socket = null;
246
- this.tunnel.removeListener(TUNNEL_EVENT_OPEN, this.boundEventOpen);
247
- this.tunnel.removeListener(TUNNEL_EVENT_CLOSED, this.boundEventClosed);
248
- this.tunnel.removeListener(TUNNEL_EVENT_TCP_OPEN, this.boundEventTcpOpen);
249
- this.tunnel.removeListener(TUNNEL_EVENT_TCP_CLOSED, this.boundEventTcpClosed);
250
- this.tunnel.removeListener(TUNNEL_EVENT_HTTP_IDENTIFIED, this.boundEventHttpIdentified);
251
- this.tunnel.removeListener(TUNNEL_EVENT_HTTP_REQUEST, this.boundEventHttpRequest);
252
- this.tunnel.removeListener(TUNNEL_EVENT_HTTP_RESPONSE, this.boundEventHttpResponse);
253
- this.tunnel.removeListener(TUNNEL_EVENT_TLS_OPEN, this.boundEventTlsOpen);
254
- this.tunnel.removeListener(TUNNEL_EVENT_TLS_CLOSED, this.boundEventTlsClosed);
255
- this.tunnel.removeListener(TUNNEL_EVENT_STOPPED, this.boundEventStopped);
256
- this.tunnel = null;
257
- }
258
- }
259
-
260
- module.exports = AgentSocket;