bdy 1.11.0-stage → 1.12.1-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 (60) hide show
  1. package/distTs/package.json +16 -5
  2. package/distTs/src/agent/agent.js +62 -36
  3. package/distTs/src/agent/linux.js +9 -4
  4. package/distTs/src/agent/manager.js +61 -42
  5. package/distTs/src/agent/osx.js +12 -5
  6. package/distTs/src/agent/socket/client.js +26 -7
  7. package/distTs/src/agent/socket/tunnel.js +56 -32
  8. package/distTs/src/agent/socket.js +83 -63
  9. package/distTs/src/agent/system.js +3 -1
  10. package/distTs/src/agent/windows.js +6 -6
  11. package/distTs/src/command/agent/install.js +4 -4
  12. package/distTs/src/command/agent/run.js +2 -2
  13. package/distTs/src/command/agent/status.js +6 -7
  14. package/distTs/src/command/agent/tunnel/http.js +4 -5
  15. package/distTs/src/command/agent/tunnel/tcp.js +3 -3
  16. package/distTs/src/command/agent/tunnel/tls.js +3 -3
  17. package/distTs/src/command/config/add/http.js +3 -3
  18. package/distTs/src/command/config/add/tcp.js +3 -3
  19. package/distTs/src/command/config/add/tls.js +3 -3
  20. package/distTs/src/command/config/get/region.js +1 -1
  21. package/distTs/src/command/pre.js +1 -1
  22. package/distTs/src/command/tunnel/http.js +2 -1
  23. package/distTs/src/command/tunnel/tcp.js +3 -3
  24. package/distTs/src/command/tunnel/tls.js +4 -4
  25. package/distTs/src/format.js +14 -15
  26. package/distTs/src/index.js +1 -1
  27. package/distTs/src/input.js +13 -12
  28. package/distTs/src/logger.js +12 -13
  29. package/distTs/src/tunnel/agent.js +3 -1
  30. package/distTs/src/tunnel/api/agent.js +1 -0
  31. package/distTs/src/tunnel/api/buddy.js +2 -0
  32. package/distTs/src/tunnel/cfg.js +6 -2
  33. package/distTs/src/tunnel/compression.js +17 -14
  34. package/distTs/src/tunnel/dns.js +20 -29
  35. package/distTs/src/tunnel/html.js +2 -0
  36. package/distTs/src/tunnel/http/log.js +30 -8
  37. package/distTs/src/tunnel/http/serve.js +3 -2
  38. package/distTs/src/tunnel/http/stream.js +5 -1
  39. package/distTs/src/tunnel/http.js +67 -26
  40. package/distTs/src/tunnel/identification.js +21 -14
  41. package/distTs/src/tunnel/latency.js +19 -11
  42. package/distTs/src/tunnel/output/interactive/tunnel.js +16 -9
  43. package/distTs/src/tunnel/output/noninteractive/agent/tunnels.js +4 -2
  44. package/distTs/src/tunnel/output/noninteractive/config/tunnel.js +6 -4
  45. package/distTs/src/tunnel/output/noninteractive/config/tunnels.js +2 -0
  46. package/distTs/src/tunnel/output/noninteractive/tunnel.js +13 -11
  47. package/distTs/src/tunnel/server/cert.js +2 -0
  48. package/distTs/src/tunnel/server/http1.js +15 -10
  49. package/distTs/src/tunnel/server/http2.js +18 -12
  50. package/distTs/src/tunnel/server/sftp.js +32 -15
  51. package/distTs/src/tunnel/server/ssh.js +49 -31
  52. package/distTs/src/tunnel/server/tls.js +11 -5
  53. package/distTs/src/tunnel/ssh/client.js +45 -31
  54. package/distTs/src/tunnel/tcp.js +61 -42
  55. package/distTs/src/tunnel/tunnel.js +196 -112
  56. package/distTs/src/types/tunnel.js +135 -0
  57. package/distTs/src/unitTest/requests.js +4 -4
  58. package/distTs/src/utils.js +2 -87
  59. package/distTs/src/visualTest/requests.js +12 -11
  60. package/package.json +16 -5
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "bdy",
3
3
  "preferGlobal": false,
4
- "version": "1.11.0-stage",
4
+ "version": "1.12.1-dev",
5
5
  "type": "commonjs",
6
6
  "license": "MIT",
7
7
  "scripts": {
@@ -28,6 +28,7 @@
28
28
  "cross-spawn": "7.0.6",
29
29
  "eventsource": "4.0.0",
30
30
  "fastify": "4.28.1",
31
+ "fdir": "6.5.0",
31
32
  "isbinaryfile": "5.0.2",
32
33
  "jsonwebtoken": "9.0.2",
33
34
  "mime-db": "1.52.0",
@@ -36,10 +37,9 @@
36
37
  "node-fetch": "3.3.2",
37
38
  "node-forge": "1.3.1",
38
39
  "path-is-inside": "1.0.2",
39
- "fdir": "6.5.0",
40
40
  "picomatch": "4.0.3",
41
41
  "pino": "8.20.0",
42
- "punycode.js": "2.3.1",
42
+ "punycode": "2.3.1",
43
43
  "puppeteer-core": "23.6.0",
44
44
  "range-parser": "1.2.1",
45
45
  "socket.io-client": "4.7.5",
@@ -60,13 +60,24 @@
60
60
  "@rollup/plugin-replace": "6.0.1",
61
61
  "@stylistic/eslint-plugin-js": "2.9.0",
62
62
  "@types/cross-spawn": "6.0.6",
63
- "@types/picomatch": "4.0.2",
64
63
  "@types/eslint__js": "8.42.3",
65
- "@types/ssh2": "1.15.1",
64
+ "@types/picomatch": "4.0.2",
65
+ "@types/punycode": "2.1.4",
66
+ "@types/netmask": "2.0.5",
67
+ "@types/ssh2": "1.15.5",
66
68
  "@types/tar-stream": "3.1.3",
67
69
  "@types/terminal-kit": "2.5.6",
68
70
  "@types/uuid": "10.0.0",
69
71
  "@types/which": "3.0.4",
72
+ "@types/ws": "8.18.0",
73
+ "@types/jsonwebtoken": "9.0.2",
74
+ "@types/mime-db": "1.43.6",
75
+ "@types/basic-auth": "1.1.8",
76
+ "@types/path-is-inside": "1.0.2",
77
+ "@types/range-parser": "1.2.1",
78
+ "@types/content-disposition": "0.5.4",
79
+ "@types/mime-types": "2.1.4",
80
+ "@types/node-forge": "1.3.1",
70
81
  "eslint": "9.13.0",
71
82
  "eslint-config-prettier": "9.1.0",
72
83
  "globals": "15.11.0",
@@ -10,7 +10,21 @@ const utils_1 = require("../utils");
10
10
  const tunnel_1 = __importDefault(require("../tunnel/tunnel"));
11
11
  const output_1 = __importDefault(require("../output"));
12
12
  const client_1 = __importDefault(require("./socket/client"));
13
+ const tunnel_2 = require("../types/tunnel");
13
14
  class Agent extends events_1.default {
15
+ id;
16
+ host;
17
+ token;
18
+ service;
19
+ target;
20
+ disabled;
21
+ tunnels;
22
+ tunnelsUpdating;
23
+ started;
24
+ api;
25
+ socket;
26
+ manager;
27
+ onRefresh;
14
28
  constructor(id, host, token, service, target, api, disabled) {
15
29
  super();
16
30
  this.id = id;
@@ -26,12 +40,12 @@ class Agent extends events_1.default {
26
40
  this.manager = null;
27
41
  this.onRefresh = null;
28
42
  this.socket = new client_1.default(host, id, token);
29
- this.socket.on(utils_1.SOCKET_IO_EVENT_FETCH_SUCCESS, (data) => this._onSocketFetch(data));
30
- this.socket.on(utils_1.SOCKET_IO_EVENT_FETCH_FAILED, (err) => this._onSocketFetchFailed(err));
31
- this.socket.on(utils_1.SOCKET_IO_EVENT_CONNECTED, () => this._onSocketConnected());
32
- this.socket.on(utils_1.SOCKET_IO_EVENT_DISCONNECTED, () => this._onSocketDisconnected());
33
- this.socket.on(utils_1.SOCKET_IO_EVENT_TUNNEL, (tunnel, action) => this._onSocketTunnel(tunnel, action));
34
- this.socket.on(utils_1.SOCKET_IO_EVENT_AGENT, (agent, action) => this._onSocketAgent(agent, action));
43
+ this.socket.on(tunnel_2.TUNNEL_SOCKET_EVENT.FETCH_SUCCESS, (data) => this._onSocketFetch(data));
44
+ this.socket.on(tunnel_2.TUNNEL_SOCKET_EVENT.FETCH_FAILED, (err) => this._onSocketFetchFailed(err));
45
+ this.socket.on(tunnel_2.TUNNEL_SOCKET_EVENT.CONNECTED, () => this._onSocketConnected());
46
+ this.socket.on(tunnel_2.TUNNEL_SOCKET_EVENT.DISCONNECTED, () => this._onSocketDisconnected());
47
+ this.socket.on(tunnel_2.TUNNEL_SOCKET_EVENT.TUNNEL, (tunnel, action) => this._onSocketTunnel(tunnel, action));
48
+ this.socket.on(tunnel_2.TUNNEL_SOCKET_EVENT.AGENT, (agent, action) => this._onSocketAgent(agent, action));
35
49
  }
36
50
  destroy() {
37
51
  if (this.socket) {
@@ -42,8 +56,8 @@ class Agent extends events_1.default {
42
56
  this.manager = null;
43
57
  this.onRefresh = null;
44
58
  this.api = null;
45
- // musza byc manualnie wczesniej zniszczone to tylko czysci obiekt
46
- this.tunnels = null;
59
+ // muszą byc manualnie wcześniej zniszczone to tylko czyści obiekt
60
+ this.tunnels = [];
47
61
  }
48
62
  _onSocketTunnel(data, action) {
49
63
  if (action === 'DEL') {
@@ -53,7 +67,8 @@ class Agent extends events_1.default {
53
67
  }
54
68
  else {
55
69
  logger_1.default.debug('Socket fetch tunnel after sync push');
56
- this.socket.fetch(this.started, null, null, false, []);
70
+ if (this.socket)
71
+ this.socket.fetch(this.started, null, null, false, []);
57
72
  }
58
73
  }
59
74
  async _onSocketAgent(data, action) {
@@ -67,7 +82,7 @@ class Agent extends events_1.default {
67
82
  this.target = !!data.target;
68
83
  this.disabled = !!data.disabled;
69
84
  if (data.disabled === this.started) {
70
- await this.makeAction(data.disabled ? utils_1.AGENT_ACTION_STOP : utils_1.AGENT_ACTION_START);
85
+ await this.makeAction(data.disabled ? tunnel_2.TUNNEL_AGENT_ACTION.STOP : tunnel_2.TUNNEL_AGENT_ACTION.START);
71
86
  }
72
87
  else if (data.action) {
73
88
  try {
@@ -113,7 +128,7 @@ class Agent extends events_1.default {
113
128
  }
114
129
  startRefreshingConfiguration(onRefresh) {
115
130
  this.onRefresh = onRefresh;
116
- if (this.onRefresh)
131
+ if (this.onRefresh && this.socket)
117
132
  this.onRefresh(this.socket.connected);
118
133
  this.refreshIn15();
119
134
  }
@@ -122,7 +137,8 @@ class Agent extends events_1.default {
122
137
  this.tunnels.forEach((t) => {
123
138
  t.start();
124
139
  });
125
- this.socket.fetch(this.started, '', null, true, this.getTunnelsUpdate());
140
+ if (this.socket)
141
+ this.socket.fetch(this.started, '', null, true, this.getTunnelsUpdate());
126
142
  }
127
143
  async enable() {
128
144
  this.disabled = false;
@@ -130,13 +146,16 @@ class Agent extends events_1.default {
130
146
  this.tunnels.forEach((t) => {
131
147
  t.start();
132
148
  });
133
- this.socket.fetch(this.started, '', false, true, this.getTunnelsUpdate());
149
+ if (this.socket)
150
+ this.socket.fetch(this.started, '', false, true, this.getTunnelsUpdate());
134
151
  }
135
152
  async stop(clearAction = true) {
136
153
  this.started = false;
137
154
  this.tunnels.forEach((t) => {
138
155
  t.stop();
139
156
  });
157
+ if (!this.socket)
158
+ return;
140
159
  this.socket.fetch(this.started, clearAction ? '' : null, null, false, this.getTunnelsUpdate());
141
160
  }
142
161
  async disable() {
@@ -145,7 +164,8 @@ class Agent extends events_1.default {
145
164
  this.tunnels.forEach((t) => {
146
165
  t.stop();
147
166
  });
148
- this.socket.fetch(this.started, '', true, false, this.getTunnelsUpdate());
167
+ if (this.socket)
168
+ this.socket.fetch(this.started, '', true, false, this.getTunnelsUpdate());
149
169
  }
150
170
  async restart() {
151
171
  await this.stop(false);
@@ -154,7 +174,9 @@ class Agent extends events_1.default {
154
174
  }
155
175
  async delete() {
156
176
  try {
157
- await this.api.unregister(this.id, this.host, this.token);
177
+ if (this.api) {
178
+ await this.api.unregister(this.id, this.host, this.token);
179
+ }
158
180
  }
159
181
  catch {
160
182
  // do nothing
@@ -166,7 +188,9 @@ class Agent extends events_1.default {
166
188
  try {
167
189
  if (fetch || counter >= 3) {
168
190
  counter = 0;
169
- this.socket.fetch(this.started, null, null, false, this.getTunnelsUpdate());
191
+ if (this.socket) {
192
+ this.socket.fetch(this.started, null, null, false, this.getTunnelsUpdate());
193
+ }
170
194
  }
171
195
  else {
172
196
  this.update();
@@ -179,31 +203,33 @@ class Agent extends events_1.default {
179
203
  }, 15000);
180
204
  }
181
205
  httpRequest(tunnelId, logRequest) {
182
- this.socket.request(tunnelId, logRequest);
206
+ if (this.socket)
207
+ this.socket.request(tunnelId, logRequest);
183
208
  }
184
209
  update(force = false) {
185
- this.socket.update(this.started, this.getTunnelsUpdate(), force);
210
+ if (this.socket)
211
+ this.socket.update(this.started, this.getTunnelsUpdate(), force);
186
212
  }
187
213
  async makeAction(action) {
188
- if (action === utils_1.AGENT_ACTION_STOP) {
214
+ if (action === tunnel_2.TUNNEL_AGENT_ACTION.STOP) {
189
215
  if (this.manager) {
190
216
  return this.manager.agentStop();
191
217
  }
192
218
  return this.stop();
193
219
  }
194
- if (action === utils_1.AGENT_ACTION_START) {
220
+ if (action === tunnel_2.TUNNEL_AGENT_ACTION.START) {
195
221
  if (this.manager) {
196
222
  return this.manager.agentStart();
197
223
  }
198
224
  return this.start();
199
225
  }
200
- if (action === utils_1.AGENT_ACTION_RESTART) {
226
+ if (action === tunnel_2.TUNNEL_AGENT_ACTION.RESTART) {
201
227
  if (this.manager) {
202
228
  return this.manager.agentRestart();
203
229
  }
204
230
  return this.restart();
205
231
  }
206
- if (action === utils_1.AGENT_ACTION_DELETE) {
232
+ if (action === tunnel_2.TUNNEL_AGENT_ACTION.DELETE) {
207
233
  if (this.manager) {
208
234
  return this.manager.disableAgentAndExit(texts_1.ERR_AGENT_REMOVED);
209
235
  }
@@ -263,11 +289,11 @@ class Agent extends events_1.default {
263
289
  this.tunnels.forEach((t) => {
264
290
  update.push({
265
291
  id: t.id,
266
- active: t.status === utils_1.TUNNEL_OPEN,
292
+ active: t.status === tunnel_2.TUNNEL_STATUS.OPEN,
267
293
  regionLatency: t.regionLatency ? t.regionLatency.latency : -1,
268
294
  targetLatency: t.targetLatency ? t.targetLatency.latency : -1,
269
295
  totalConnections: t.totalConnections,
270
- currentConnections: Object.keys(t.connections).length,
296
+ currentConnections: Object.keys(t.connections || {}).length,
271
297
  httpIdentified: t.identify ? t.identify.type : '',
272
298
  });
273
299
  });
@@ -277,20 +303,20 @@ class Agent extends events_1.default {
277
303
  this.tunnels.push(tunnel);
278
304
  if (this.started)
279
305
  tunnel.start();
280
- tunnel.on(utils_1.TUNNEL_EVENT_OPEN, () => this.update(true));
281
- tunnel.on(utils_1.TUNNEL_EVENT_CLOSED, () => this.update());
282
- tunnel.on(utils_1.TUNNEL_EVENT_TCP_OPEN, () => this.update());
283
- tunnel.on(utils_1.TUNNEL_EVENT_TCP_CLOSED, () => this.update());
284
- tunnel.on(utils_1.TUNNEL_EVENT_TLS_OPEN, () => this.update());
285
- tunnel.on(utils_1.TUNNEL_EVENT_TLS_CLOSED, () => this.update());
286
- tunnel.on(utils_1.TUNNEL_EVENT_HTTP_IDENTIFIED, () => this.update());
287
- tunnel.on(utils_1.TUNNEL_EVENT_HTTP_OPEN, () => this.update());
288
- tunnel.on(utils_1.TUNNEL_EVENT_HTTP_CLOSED, () => this.update());
289
- tunnel.on(utils_1.TUNNEL_EVENT_STOPPED, () => this.update());
290
- tunnel.on(utils_1.TUNNEL_EVENT_HTTP_REQUEST, (t, logRequest) => this.httpRequest(t.id, logRequest));
306
+ tunnel.on(tunnel_2.TUNNEL_EVENT.OPEN, () => this.update(true));
307
+ tunnel.on(tunnel_2.TUNNEL_EVENT.CLOSED, () => this.update());
308
+ tunnel.on(tunnel_2.TUNNEL_EVENT.TCP_OPEN, () => this.update());
309
+ tunnel.on(tunnel_2.TUNNEL_EVENT.TCP_CLOSED, () => this.update());
310
+ tunnel.on(tunnel_2.TUNNEL_EVENT.TLS_OPEN, () => this.update());
311
+ tunnel.on(tunnel_2.TUNNEL_EVENT.TLS_CLOSED, () => this.update());
312
+ tunnel.on(tunnel_2.TUNNEL_EVENT.HTTP_IDENTIFIED, () => this.update());
313
+ tunnel.on(tunnel_2.TUNNEL_EVENT.HTTP_OPEN, () => this.update());
314
+ tunnel.on(tunnel_2.TUNNEL_EVENT.HTTP_CLOSED, () => this.update());
315
+ tunnel.on(tunnel_2.TUNNEL_EVENT.STOPPED, () => this.update());
316
+ tunnel.on(tunnel_2.TUNNEL_EVENT.HTTP_REQUEST, (t, logRequest) => this.httpRequest(t.id, logRequest));
291
317
  }
292
318
  destroyTunnel(id) {
293
- let tunnel = this.tunnels.find((t) => t.id === id);
319
+ const tunnel = this.tunnels.find((t) => t.id === id);
294
320
  if (!tunnel)
295
321
  return null;
296
322
  this.tunnels = this.tunnels.filter((t) => t.id !== tunnel.id);
@@ -11,7 +11,12 @@ const texts_1 = require("../texts");
11
11
  class AgentLinux extends system_1.default {
12
12
  hasAdminRights() {
13
13
  return new Promise((resolve) => {
14
- resolve(process.getuid() === 0);
14
+ if (process.getuid) {
15
+ resolve(process.getuid() === 0);
16
+ }
17
+ else {
18
+ resolve(false);
19
+ }
15
20
  });
16
21
  }
17
22
  isSupported() {
@@ -107,11 +112,11 @@ WantedBy=multi-user.target`;
107
112
  }
108
113
  systemctl(cmd) {
109
114
  return new Promise((resolve, reject) => {
110
- (0, child_process_1.exec)(`systemctl ${cmd}`, (err, stdout, stderr) => {
115
+ (0, child_process_1.exec)(`systemctl ${cmd}`, (err) => {
111
116
  if (!err)
112
- resolve(stdout, stderr);
117
+ resolve();
113
118
  else
114
- reject(err, stderr);
119
+ reject(err);
115
120
  });
116
121
  });
117
122
  }
@@ -3,6 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.AgentManagerClass = void 0;
6
7
  const utils_1 = require("../utils");
7
8
  const ws_1 = require("ws");
8
9
  const logger_1 = __importDefault(require("../logger"));
@@ -15,9 +16,21 @@ const system_1 = __importDefault(require("./system"));
15
16
  const osx_1 = __importDefault(require("./osx"));
16
17
  const buddy_1 = __importDefault(require("../tunnel/api/buddy"));
17
18
  const socket_1 = __importDefault(require("./socket"));
19
+ const tunnel_1 = require("../types/tunnel");
18
20
  class AgentManagerClass {
21
+ status;
22
+ id;
23
+ host;
24
+ token;
25
+ port;
26
+ shouldStart;
27
+ sshHostKey;
28
+ agent;
29
+ ws;
30
+ server;
31
+ system;
19
32
  constructor() {
20
- this.status = utils_1.AGENT_STATUS_DISABLED;
33
+ this.status = tunnel_1.TUNNEL_AGENT_STATUS.DISABLED;
21
34
  this.id = null;
22
35
  this.host = null;
23
36
  this.token = null;
@@ -41,21 +54,21 @@ class AgentManagerClass {
41
54
  }
42
55
  }
43
56
  start(id, host, token, port, start) {
44
- this.load(id, host, token, parseInt(port, 10), !!start).then();
57
+ this.load(id, host, token, port, start).then();
45
58
  }
46
59
  async tryFetch() {
47
60
  try {
48
- this.agent = await buddy_1.default.fetchAgent(this.id, this.host, this.token);
61
+ this.agent = await buddy_1.default.fetchAgent(this.id || '', this.host || '', this.token || '');
49
62
  this.agent.manager = this;
50
63
  this.agent.startRefreshingConfiguration((success) => {
51
64
  if (success)
52
- this.status = utils_1.AGENT_STATUS_ENABLED;
65
+ this.status = tunnel_1.TUNNEL_AGENT_STATUS.ENABLED;
53
66
  else
54
- this.status = utils_1.AGENT_STATUS_FETCH_FAILED;
67
+ this.status = tunnel_1.TUNNEL_AGENT_STATUS.FETCH_FAILED;
55
68
  });
56
69
  if (this.shouldStart)
57
70
  await this.agent.start();
58
- this.status = utils_1.AGENT_STATUS_ENABLED;
71
+ this.status = tunnel_1.TUNNEL_AGENT_STATUS.ENABLED;
59
72
  }
60
73
  catch (err) {
61
74
  if (err instanceof utils_1.ApiErrorAgentNotFound) {
@@ -63,7 +76,7 @@ class AgentManagerClass {
63
76
  }
64
77
  else {
65
78
  output_1.default.normal(texts_1.AGENT_FETCH_RETRY);
66
- this.status = utils_1.AGENT_STATUS_FETCH_FAILED;
79
+ this.status = tunnel_1.TUNNEL_AGENT_STATUS.FETCH_FAILED;
67
80
  await (0, utils_1.sleep)(3000);
68
81
  return this.tryFetch();
69
82
  }
@@ -88,14 +101,13 @@ class AgentManagerClass {
88
101
  const tunnels = [];
89
102
  if (this.agent) {
90
103
  this.agent.tunnels.forEach((tunnel) => {
91
- if (tunnel.type === utils_1.TUNNEL_SSH)
104
+ if (tunnel.type === tunnel_1.TUNNEL_TYPE.SSH)
92
105
  return;
93
106
  tunnels.push({
94
107
  id: tunnel.id,
95
108
  type: tunnel.type,
96
109
  target: tunnel.target,
97
110
  domain: tunnel.domain,
98
- customDomain: tunnel.customDomain,
99
111
  subdomain: tunnel.subdomain,
100
112
  serve: tunnel.serve,
101
113
  status: tunnel.status,
@@ -121,7 +133,7 @@ class AgentManagerClass {
121
133
  return;
122
134
  }
123
135
  this.serverOutput(res, {
124
- target: this.agent.target
136
+ target: this.agent.target,
125
137
  });
126
138
  }
127
139
  async processAgentTargetEnable(res) {
@@ -130,7 +142,7 @@ class AgentManagerClass {
130
142
  return;
131
143
  }
132
144
  try {
133
- await buddy_1.default.enableTarget(this.id, this.host, this.token);
145
+ await buddy_1.default.enableTarget(this.id || '', this.host || '', this.token || '');
134
146
  this.agent.target = true;
135
147
  }
136
148
  catch (err) {
@@ -145,7 +157,7 @@ class AgentManagerClass {
145
157
  return;
146
158
  }
147
159
  try {
148
- await buddy_1.default.disableTarget(this.id, this.host, this.token);
160
+ await buddy_1.default.disableTarget(this.id || '', this.host || '', this.token || '');
149
161
  this.agent.target = false;
150
162
  }
151
163
  catch (err) {
@@ -160,7 +172,7 @@ class AgentManagerClass {
160
172
  status: this.status,
161
173
  version: (0, utils_1.getVersion)(),
162
174
  started: this.shouldStart,
163
- enabled: this.status === utils_1.AGENT_STATUS_ENABLED,
175
+ enabled: this.status === tunnel_1.TUNNEL_AGENT_STATUS.ENABLED,
164
176
  });
165
177
  }
166
178
  processBody(req) {
@@ -199,7 +211,7 @@ class AgentManagerClass {
199
211
  return;
200
212
  }
201
213
  try {
202
- await buddy_1.default.removeTunnel(this.id, tunnel.id, this.host, this.token);
214
+ await buddy_1.default.removeTunnel(this.id || '', tunnel.id, this.host || '', this.token || '');
203
215
  }
204
216
  catch (err) {
205
217
  this.serverError(res, err.message);
@@ -210,7 +222,7 @@ class AgentManagerClass {
210
222
  });
211
223
  }
212
224
  async agentRestart() {
213
- let saved = await this.agentStop();
225
+ const saved = await this.agentStop();
214
226
  if (!saved)
215
227
  return false;
216
228
  await (0, utils_1.sleep)(100);
@@ -225,7 +237,7 @@ class AgentManagerClass {
225
237
  // do nothing
226
238
  }
227
239
  try {
228
- await buddy_1.default.unregister(this.id, this.host, this.token);
240
+ await buddy_1.default.unregister(this.id || '', this.host || '', this.token || '');
229
241
  }
230
242
  catch {
231
243
  // do nothing
@@ -246,26 +258,26 @@ class AgentManagerClass {
246
258
  }
247
259
  async agentStop() {
248
260
  const saved = this.resaveAgentConfig(false, true);
249
- if (!saved)
261
+ if (!saved || !this.agent)
250
262
  return false;
251
263
  await this.agent.stop();
252
264
  return true;
253
265
  }
254
266
  async agentDisable() {
255
267
  const saved = this.resaveAgentConfig(false, true);
256
- if (!saved)
268
+ if (!saved || !this.agent)
257
269
  return false;
258
270
  await this.agent.disable();
259
271
  return true;
260
272
  }
261
273
  async agentEnable() {
262
274
  const saved = this.resaveAgentConfig(true, true);
263
- if (!saved)
275
+ if (!saved || !this.agent)
264
276
  return false;
265
277
  await this.agent.enable();
266
278
  return true;
267
279
  }
268
- async processAgentRestart(req, res) {
280
+ async processAgentRestart(res) {
269
281
  if (!this.agent) {
270
282
  this.serverError(res, 'Agent not enabled');
271
283
  return;
@@ -283,7 +295,7 @@ class AgentManagerClass {
283
295
  success: true,
284
296
  });
285
297
  }
286
- async processAgentEnable(req, res) {
298
+ async processAgentEnable(res) {
287
299
  if (!this.agent) {
288
300
  this.serverError(res, 'Agent not enabled');
289
301
  return;
@@ -297,7 +309,7 @@ class AgentManagerClass {
297
309
  success: true,
298
310
  });
299
311
  }
300
- async processAgentDisable(req, res) {
312
+ async processAgentDisable(res) {
301
313
  if (!this.agent) {
302
314
  this.serverError(res, 'Agent not enabled');
303
315
  return;
@@ -311,7 +323,7 @@ class AgentManagerClass {
311
323
  success: true,
312
324
  });
313
325
  }
314
- async processAgentStop(req, res) {
326
+ async processAgentStop(res) {
315
327
  if (!this.agent) {
316
328
  this.serverError(res, 'Agent not enabled');
317
329
  return;
@@ -330,6 +342,8 @@ class AgentManagerClass {
330
342
  });
331
343
  }
332
344
  async agentStart() {
345
+ if (!this.agent)
346
+ return false;
333
347
  const saved = this.resaveAgentConfig(true, true);
334
348
  if (!saved) {
335
349
  return false;
@@ -337,7 +351,7 @@ class AgentManagerClass {
337
351
  await this.agent.start();
338
352
  return true;
339
353
  }
340
- async processAgentStart(req, res) {
354
+ async processAgentStart(res) {
341
355
  if (!this.agent) {
342
356
  this.serverError(res, 'Agent not enabled');
343
357
  return;
@@ -370,7 +384,7 @@ class AgentManagerClass {
370
384
  }
371
385
  let tunnel;
372
386
  try {
373
- tunnel = await buddy_1.default.addTunnel(this.id, this.host, this.token, data, this.sshHostKey);
387
+ tunnel = await buddy_1.default.addTunnel(this.id || '', this.host || '', this.token || '', data, this.sshHostKey || '');
374
388
  }
375
389
  catch (err) {
376
390
  this.serverError(res, err.message);
@@ -383,7 +397,7 @@ class AgentManagerClass {
383
397
  });
384
398
  }
385
399
  async processRequest(req, res) {
386
- logger_1.default.info((0, texts_1.LOG_REQUEST)(req.url));
400
+ logger_1.default.info((0, texts_1.LOG_REQUEST)(req.url || ''));
387
401
  if (req.url === '/status')
388
402
  return this.processStatus(res);
389
403
  if (req.url === '/target')
@@ -399,24 +413,25 @@ class AgentManagerClass {
399
413
  if (req.url === '/tunnel/stop')
400
414
  return this.processTunnelStop(req, res);
401
415
  if (req.url === '/agent/start')
402
- return this.processAgentStart(req, res);
416
+ return this.processAgentStart(res);
403
417
  if (req.url === '/agent/stop')
404
- return this.processAgentStop(req, res);
418
+ return this.processAgentStop(res);
405
419
  if (req.url === '/agent/restart')
406
- return this.processAgentRestart(req, res);
420
+ return this.processAgentRestart(res);
407
421
  if (req.url === '/agent/disable')
408
- return this.processAgentDisable(req, res);
422
+ return this.processAgentDisable(res);
409
423
  if (req.url === '/agent/enable')
410
- return this.processAgentEnable(req, res);
424
+ return this.processAgentEnable(res);
411
425
  this.server404(res);
412
426
  }
413
427
  processUpgrade(req, socket, head) {
414
- logger_1.default.info((0, texts_1.LOG_REQUEST)(req.url));
415
- const url = new URL(req.url, 'http://localhost');
428
+ logger_1.default.info((0, texts_1.LOG_REQUEST)(req.url || ''));
429
+ const url = new URL(req.url || '', 'http://localhost');
416
430
  const id = url.searchParams.get('id');
417
- if (url.pathname === '/tunnel' && id) {
431
+ if (url.pathname === '/tunnel' && id && this.ws) {
418
432
  this.ws.handleUpgrade(req, socket, head, (con) => {
419
- this.ws.emit('connection', con, id);
433
+ if (this.ws)
434
+ this.ws.emit('connection', con, id);
420
435
  });
421
436
  return;
422
437
  }
@@ -428,7 +443,7 @@ class AgentManagerClass {
428
443
  return;
429
444
  }
430
445
  const tunnel = this.agent.tunnels.find((t) => t.id === id);
431
- if (!tunnel || tunnel.type === utils_1.TUNNEL_SSH) {
446
+ if (!tunnel || tunnel.type === tunnel_1.TUNNEL_TYPE.SSH) {
432
447
  con.close();
433
448
  return;
434
449
  }
@@ -439,14 +454,17 @@ class AgentManagerClass {
439
454
  this.ws = new ws_1.WebSocketServer({ noServer: true });
440
455
  this.ws.on('connection', (con, id) => this.processWebsocketConnection(con, id));
441
456
  this.server.on('upgrade', (req, socket, head) => this.processUpgrade(req, socket, head));
442
- this.server.listen(this.port, async (err) => {
443
- if (err) {
457
+ this.server.on('error', async (err) => {
458
+ if (err.code === 'EADDRINUSE') {
444
459
  await this.disableAgentAndExit(texts_1.LOG_ERROR_STARTING_AGENT_SERVER);
445
460
  }
446
461
  else {
447
- logger_1.default.info(texts_1.LOG_AGENT_SERVER_STARTED);
462
+ logger_1.default.error(err);
448
463
  }
449
464
  });
465
+ this.server.listen(this.port, () => {
466
+ logger_1.default.info(texts_1.LOG_AGENT_SERVER_STARTED);
467
+ });
450
468
  }
451
469
  resaveAgentConfig(shouldStart, forceParam = false) {
452
470
  if (!forceParam) {
@@ -469,7 +487,7 @@ class AgentManagerClass {
469
487
  }
470
488
  try {
471
489
  // resave config
472
- this.system.saveAgentConfig(this.shouldStart, this.sshHostKey);
490
+ this.system.saveAgentConfig(this.shouldStart, this.sshHostKey || '');
473
491
  return true;
474
492
  }
475
493
  catch {
@@ -477,11 +495,11 @@ class AgentManagerClass {
477
495
  }
478
496
  }
479
497
  async load(id, host, token, port, start) {
480
- this.status = utils_1.AGENT_STATUS_INITIALIZING;
498
+ this.status = tunnel_1.TUNNEL_AGENT_STATUS.INITIALIZING;
481
499
  this.id = id;
482
500
  this.host = host;
483
501
  this.token = token;
484
- this.port = parseInt(port, 10);
502
+ this.port = port;
485
503
  if (!this.id || !this.token || !this.host || !this.port) {
486
504
  await this.disableAgentAndExit(texts_1.ERR_AGENT_NOT_REGISTERED);
487
505
  return;
@@ -496,5 +514,6 @@ class AgentManagerClass {
496
514
  logger_1.default.info(texts_1.LOG_AGENT_STARTED);
497
515
  }
498
516
  }
517
+ exports.AgentManagerClass = AgentManagerClass;
499
518
  const AgentManager = new AgentManagerClass();
500
519
  exports.default = AgentManager;
@@ -11,7 +11,12 @@ const system_1 = __importDefault(require("./system"));
11
11
  class AgentOsx extends system_1.default {
12
12
  hasAdminRights() {
13
13
  return new Promise((resolve) => {
14
- resolve(process.getuid() === 0);
14
+ if (process.getuid) {
15
+ resolve(process.getuid() === 0);
16
+ }
17
+ else {
18
+ resolve(false);
19
+ }
15
20
  });
16
21
  }
17
22
  isSupported() {
@@ -123,15 +128,17 @@ class AgentOsx extends system_1.default {
123
128
  }
124
129
  }
125
130
  disable() {
126
- return this.launchCtl('bootout system/bdy');
131
+ return new Promise((resolve, reject) => {
132
+ this.launchCtl('bootout system/bdy').then(() => resolve()).catch(reject);
133
+ });
127
134
  }
128
135
  launchCtl(cmd) {
129
136
  return new Promise((resolve, reject) => {
130
- (0, child_process_1.exec)(`launchctl ${cmd}`, (err, stdout, stderr) => {
137
+ (0, child_process_1.exec)(`launchctl ${cmd}`, (err, stdout) => {
131
138
  if (!err)
132
- resolve(stdout, stderr);
139
+ resolve(stdout);
133
140
  else
134
- reject(err, stderr);
141
+ reject(err);
135
142
  });
136
143
  });
137
144
  }