bdy 1.7.60-dev → 1.8.4-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 (206) hide show
  1. package/distTs/bin/cli.js +5 -0
  2. package/distTs/src/agent/linux.js +119 -0
  3. package/distTs/src/agent/manager.js +395 -0
  4. package/distTs/src/agent/osx.js +139 -0
  5. package/distTs/src/agent/socket/tunnel.js +212 -0
  6. package/distTs/src/agent/socket.js +220 -0
  7. package/distTs/src/agent/system.js +177 -0
  8. package/distTs/src/agent/wait.js +23 -0
  9. package/distTs/src/agent/windows.js +159 -0
  10. package/distTs/src/agent.js +237 -0
  11. package/distTs/src/api/agent.js +84 -0
  12. package/distTs/src/api/buddy.js +117 -0
  13. package/distTs/src/api/socket.js +133 -0
  14. package/distTs/src/cfg.js +238 -0
  15. package/distTs/src/command/agent/install.js +106 -0
  16. package/distTs/src/command/agent/restart.js +27 -0
  17. package/distTs/src/command/agent/run.js +19 -0
  18. package/distTs/src/command/agent/start.js +28 -0
  19. package/distTs/src/command/agent/status.js +35 -0
  20. package/distTs/src/command/agent/stop.js +28 -0
  21. package/distTs/src/command/agent/tunnel/http.js +44 -0
  22. package/distTs/src/command/agent/tunnel/list.js +27 -0
  23. package/distTs/src/command/agent/tunnel/remove.js +28 -0
  24. package/distTs/src/command/agent/tunnel/start.js +34 -0
  25. package/distTs/src/command/agent/tunnel/status.js +31 -0
  26. package/distTs/src/command/agent/tunnel/tcp.js +43 -0
  27. package/distTs/src/command/agent/tunnel/tls.js +43 -0
  28. package/distTs/src/command/agent/tunnel.js +23 -0
  29. package/distTs/src/command/agent/uninstall.js +38 -0
  30. package/distTs/src/command/agent/update.js +38 -0
  31. package/distTs/src/command/agent/version.js +21 -0
  32. package/distTs/src/command/agent.js +29 -0
  33. package/distTs/src/command/config/add/http.js +25 -0
  34. package/distTs/src/command/config/add/tcp.js +25 -0
  35. package/distTs/src/command/config/add/tls.js +25 -0
  36. package/distTs/src/command/config/add.js +15 -0
  37. package/distTs/src/command/config/get/region.js +15 -0
  38. package/distTs/src/command/config/get/timeout.js +15 -0
  39. package/distTs/src/command/config/get/token.js +15 -0
  40. package/distTs/src/command/config/get/tunnel.js +19 -0
  41. package/distTs/src/command/config/get/tunnels.js +15 -0
  42. package/distTs/src/command/config/get/whitelist.js +15 -0
  43. package/distTs/src/command/config/get.js +21 -0
  44. package/distTs/src/command/config/remove/tunnel.js +19 -0
  45. package/distTs/src/command/config/remove.js +11 -0
  46. package/distTs/src/command/config/set/region.js +17 -0
  47. package/distTs/src/command/config/set/timeout.js +17 -0
  48. package/distTs/src/command/config/set/token.js +16 -0
  49. package/distTs/src/command/config/set/whitelist.js +17 -0
  50. package/distTs/src/command/config/set.js +17 -0
  51. package/distTs/src/command/config.js +17 -0
  52. package/distTs/src/command/http.js +30 -0
  53. package/distTs/src/command/pre.js +49 -0
  54. package/distTs/src/command/start.js +28 -0
  55. package/distTs/src/command/tcp.js +30 -0
  56. package/distTs/src/command/tls.js +30 -0
  57. package/distTs/src/command/version.js +13 -0
  58. package/distTs/src/command/vt/close.js +28 -0
  59. package/distTs/src/command/vt/exec.js +79 -0
  60. package/distTs/src/command/vt/storybook.js +90 -0
  61. package/distTs/src/command/vt.js +15 -0
  62. package/distTs/src/format.js +172 -0
  63. package/distTs/src/index.js +40 -0
  64. package/distTs/src/input.js +286 -0
  65. package/distTs/src/logger.js +93 -0
  66. package/distTs/src/output/interactive/tunnel.js +860 -0
  67. package/distTs/src/output/noninteractive/agent/tunnels.js +43 -0
  68. package/distTs/src/output/noninteractive/config/tunnel.js +67 -0
  69. package/distTs/src/output/noninteractive/config/tunnels.js +18 -0
  70. package/distTs/src/output/noninteractive/tunnel.js +59 -0
  71. package/distTs/src/output.js +138 -0
  72. package/distTs/src/server/cert.js +52 -0
  73. package/distTs/src/server/http1.js +74 -0
  74. package/distTs/src/server/http2.js +74 -0
  75. package/distTs/src/server/sftp.js +487 -0
  76. package/distTs/src/server/ssh.js +112 -0
  77. package/distTs/src/server/tls.js +41 -0
  78. package/distTs/src/ssh/client.js +191 -0
  79. package/distTs/src/texts.js +345 -0
  80. package/distTs/src/tunnel/agent.js +100 -0
  81. package/distTs/src/tunnel/compression.js +41 -0
  82. package/distTs/src/tunnel/dns.js +54 -0
  83. package/distTs/src/tunnel/html.js +30 -0
  84. package/distTs/src/tunnel/http/log.js +196 -0
  85. package/distTs/src/tunnel/http/serve.js +132 -0
  86. package/distTs/src/tunnel/http/stream.js +45 -0
  87. package/distTs/src/tunnel/http.js +405 -0
  88. package/distTs/src/tunnel/identification.js +96 -0
  89. package/distTs/src/tunnel/latency.js +71 -0
  90. package/distTs/src/tunnel/tcp.js +92 -0
  91. package/distTs/src/tunnel.js +647 -0
  92. package/distTs/src/types/ciInfo.js +10 -0
  93. package/distTs/src/types/options.js +2 -0
  94. package/distTs/src/types/plugin.js +2 -0
  95. package/distTs/src/types/queue.js +2 -0
  96. package/distTs/src/types/requests.js +2 -0
  97. package/distTs/src/types/resources.js +2 -0
  98. package/distTs/src/types/snapshots.js +2 -0
  99. package/distTs/src/types/storybook.js +2 -0
  100. package/distTs/src/utils.js +437 -0
  101. package/distTs/src/visualTest/browser.js +32 -0
  102. package/distTs/src/visualTest/ci.js +206 -0
  103. package/distTs/src/visualTest/context.js +44 -0
  104. package/distTs/src/visualTest/exec.js +51 -0
  105. package/distTs/src/visualTest/queue.js +43 -0
  106. package/distTs/src/visualTest/requests.js +197 -0
  107. package/distTs/src/visualTest/resources.js +195 -0
  108. package/distTs/src/visualTest/server.js +33 -0
  109. package/distTs/src/visualTest/snapshots.js +109 -0
  110. package/distTs/src/visualTest/utils/parseDom.js +238 -0
  111. package/distTs/src/visualTest/validation.js +18 -0
  112. package/package.json +42 -11
  113. package/.eslintrc.yml +0 -23
  114. package/bin/cli.js +0 -5
  115. package/dockerfile +0 -15
  116. package/link.sh +0 -3
  117. package/src/agent/linux.js +0 -127
  118. package/src/agent/manager.js +0 -404
  119. package/src/agent/osx.js +0 -150
  120. package/src/agent/socket/tunnel.js +0 -232
  121. package/src/agent/socket.js +0 -260
  122. package/src/agent/system.js +0 -205
  123. package/src/agent/wait.js +0 -20
  124. package/src/agent/windows.js +0 -168
  125. package/src/agent.js +0 -256
  126. package/src/api/agent.js +0 -95
  127. package/src/api/buddy.js +0 -131
  128. package/src/api/socket.js +0 -148
  129. package/src/cfg.js +0 -239
  130. package/src/command/agent/install.js +0 -116
  131. package/src/command/agent/restart.js +0 -27
  132. package/src/command/agent/run.js +0 -16
  133. package/src/command/agent/start.js +0 -27
  134. package/src/command/agent/status.js +0 -44
  135. package/src/command/agent/stop.js +0 -27
  136. package/src/command/agent/tunnel/http.js +0 -45
  137. package/src/command/agent/tunnel/list.js +0 -26
  138. package/src/command/agent/tunnel/remove.js +0 -29
  139. package/src/command/agent/tunnel/start.js +0 -37
  140. package/src/command/agent/tunnel/status.js +0 -31
  141. package/src/command/agent/tunnel/tcp.js +0 -45
  142. package/src/command/agent/tunnel/tls.js +0 -45
  143. package/src/command/agent/tunnel.js +0 -20
  144. package/src/command/agent/uninstall.js +0 -36
  145. package/src/command/agent/update.js +0 -42
  146. package/src/command/agent/version.js +0 -22
  147. package/src/command/agent.js +0 -26
  148. package/src/command/config/add/http.js +0 -31
  149. package/src/command/config/add/tcp.js +0 -31
  150. package/src/command/config/add/tls.js +0 -31
  151. package/src/command/config/add.js +0 -12
  152. package/src/command/config/get/region.js +0 -12
  153. package/src/command/config/get/timeout.js +0 -12
  154. package/src/command/config/get/token.js +0 -12
  155. package/src/command/config/get/tunnel.js +0 -20
  156. package/src/command/config/get/tunnels.js +0 -12
  157. package/src/command/config/get/whitelist.js +0 -12
  158. package/src/command/config/get.js +0 -18
  159. package/src/command/config/remove/tunnel.js +0 -21
  160. package/src/command/config/remove.js +0 -8
  161. package/src/command/config/set/region.js +0 -18
  162. package/src/command/config/set/timeout.js +0 -21
  163. package/src/command/config/set/token.js +0 -18
  164. package/src/command/config/set/whitelist.js +0 -18
  165. package/src/command/config/set.js +0 -14
  166. package/src/command/config.js +0 -14
  167. package/src/command/http.js +0 -33
  168. package/src/command/pre.js +0 -45
  169. package/src/command/start.js +0 -30
  170. package/src/command/tcp.js +0 -31
  171. package/src/command/tls.js +0 -31
  172. package/src/command/version.js +0 -10
  173. package/src/format.js +0 -171
  174. package/src/index.js +0 -38
  175. package/src/input.js +0 -283
  176. package/src/logger.js +0 -92
  177. package/src/output/interactive/tunnel.js +0 -871
  178. package/src/output/noninteractive/agent/tunnels.js +0 -32
  179. package/src/output/noninteractive/config/tunnel.js +0 -51
  180. package/src/output/noninteractive/config/tunnels.js +0 -19
  181. package/src/output/noninteractive/tunnel.js +0 -79
  182. package/src/output.js +0 -131
  183. package/src/server/cert.js +0 -51
  184. package/src/server/http1.js +0 -79
  185. package/src/server/http2.js +0 -79
  186. package/src/server/sftp.js +0 -474
  187. package/src/server/ssh.js +0 -107
  188. package/src/server/tls.js +0 -41
  189. package/src/ssh/client.js +0 -204
  190. package/src/texts.js +0 -445
  191. package/src/tunnel/agent.js +0 -100
  192. package/src/tunnel/compression.js +0 -32
  193. package/src/tunnel/dns.js +0 -55
  194. package/src/tunnel/html/404.html +0 -129
  195. package/src/tunnel/html/503.html +0 -136
  196. package/src/tunnel/html.js +0 -32
  197. package/src/tunnel/http/log.js +0 -204
  198. package/src/tunnel/http/serve.js +0 -127
  199. package/src/tunnel/http/stream.js +0 -47
  200. package/src/tunnel/http.js +0 -406
  201. package/src/tunnel/identification.js +0 -95
  202. package/src/tunnel/latency.js +0 -69
  203. package/src/tunnel/tcp.js +0 -85
  204. package/src/tunnel.js +0 -713
  205. package/src/utils.js +0 -577
  206. package/unlink.sh +0 -3
@@ -1,404 +0,0 @@
1
- const {
2
- ApiErrorAgentNotFound,
3
- AGENT_STATUS_DISABLED,
4
- AGENT_STATUS_ENABLED,
5
- AGENT_STATUS_FETCH_FAILED,
6
- getVersion,
7
- sleep
8
- } = require('../utils.js');
9
- const { WebSocketServer } = require('ws');
10
- const logger = require('../logger.js');
11
- const http = require('http');
12
- const Output = require('../output.js');
13
- const {
14
- AGENT_FETCH_RETRY,
15
- ERR_AGENT_NOT_REGISTERED,
16
- LOG_AGENT_SERVER_STARTED,
17
- LOG_AGENT_STARTED,
18
- LOG_ERROR_STARTING_AGENT_SERVER,
19
- LOG_REQUEST
20
- } = require('../texts.js');
21
- const AgentSocket = require('./socket.js');
22
- const ApiBuddy = require('../api/buddy.js');
23
- const { isOsx,
24
- AGENT_STATUS_INITIALIZING,
25
- isLinux,
26
- isWindows,
27
- TUNNEL_SSH,
28
- createSshHostKey
29
- } = require('../utils');
30
- const AgentOsx = require('./osx');
31
- const { ERR_AGENT_NOT_FOUND,
32
- ERR_SAVING_AGENT_CONFIG
33
- } = require('../texts');
34
- const AgentSystem = require('./system');
35
- const AgentLinux = require('./linux');
36
- const AgentWindows = require('./windows');
37
-
38
- class AgentManagerClass {
39
- constructor() {
40
- this.status = AGENT_STATUS_DISABLED;
41
- this.id = null;
42
- this.host = null;
43
- this.token = null;
44
- this.port = null;
45
- this.shouldStart = null;
46
- this.sshHostKey = null;
47
- this.agent = null;
48
- this.server = null;
49
- this.ws = null;
50
- if (isOsx()) {
51
- this.system = new AgentOsx();
52
- } else if (isLinux()) {
53
- this.system = new AgentLinux();
54
- } else if (isWindows()) {
55
- this.system = new AgentWindows();
56
- } else {
57
- this.system = new AgentSystem();
58
- }
59
- }
60
-
61
- start(id, host, token, port, start) {
62
- this.load(id, host, token, parseInt(port, 10), !!start).then();
63
- }
64
-
65
- async tryFetch() {
66
- try {
67
- this.agent = await ApiBuddy.fetchAgent(this.id, this.host, this.token);
68
- this.agent.manager = this;
69
- this.agent.startRefreshingConfiguration((success) => {
70
- if (success) this.status = AGENT_STATUS_ENABLED;
71
- else this.status = AGENT_STATUS_FETCH_FAILED;
72
- });
73
- if (this.shouldStart) await this.agent.start();
74
- this.status = AGENT_STATUS_ENABLED;
75
- } catch (err) {
76
- if (err instanceof ApiErrorAgentNotFound) {
77
- await this.disableAgentAndExit(ERR_AGENT_NOT_FOUND);
78
- } else {
79
- Output.normal(AGENT_FETCH_RETRY);
80
- this.status = AGENT_STATUS_FETCH_FAILED;
81
- await sleep(3000);
82
- return this.tryFetch();
83
- }
84
- }
85
- }
86
-
87
- serverOutput(res, data) {
88
- res.statusCode = 200;
89
- res.setHeader('content-type', 'application/json');
90
- res.end(JSON.stringify(data));
91
- }
92
-
93
- server404(res) {
94
- res.statusCode = 404;
95
- res.end('Not found');
96
- }
97
-
98
- serverError(res, message) {
99
- res.statusCode = 400;
100
- res.end(JSON.stringify({
101
- message
102
- }));
103
- }
104
-
105
- async processTunnels(res) {
106
- const tunnels = [];
107
- if (this.agent) {
108
- this.agent.tunnels.forEach((tunnel) => {
109
- if (tunnel.type === TUNNEL_SSH) return;
110
- tunnels.push({
111
- id: tunnel.id,
112
- type: tunnel.type,
113
- target: tunnel.target,
114
- domain: tunnel.domain,
115
- subdomain: tunnel.subdomain,
116
- serve: tunnel.serve,
117
- status: tunnel.status,
118
- region: tunnel.region,
119
- sshId: tunnel.sshId,
120
- sshForwardPort: tunnel.sshForwardPort,
121
- regionLatency: tunnel.regionLatency ? tunnel.regionLatency.latency : -1,
122
- targetLatency: tunnel.targetLatency ? tunnel.targetLatency.latency : -1
123
- });
124
- });
125
- }
126
- this.serverOutput(res, {
127
- tunnels
128
- });
129
- }
130
-
131
- async processStatus(res) {
132
- this.serverOutput(res, {
133
- id: this.id,
134
- status: this.status,
135
- version: getVersion(),
136
- started: this.shouldStart,
137
- enabled: this.status === AGENT_STATUS_ENABLED
138
- });
139
- }
140
-
141
- processBody(req) {
142
- return new Promise((resolve, reject) => {
143
- let data = '';
144
- req.setEncoding('utf-8');
145
- req.on('data', (d) => {
146
- data += d;
147
- });
148
- req.on('end', () => {
149
- try {
150
- resolve(JSON.parse(data));
151
- } catch (err) {
152
- reject(err);
153
- }
154
- });
155
- });
156
- }
157
-
158
- async processTunnelStop(req, res) {
159
- if (!this.agent) {
160
- this.serverError(res, 'Agent not enabled');
161
- return;
162
- }
163
- let data;
164
- try {
165
- data = await this.processBody(req);
166
- } catch (err) {
167
- this.serverError(res, 'Wrong input data');
168
- return;
169
- }
170
- const tunnel = this.agent.destroyTunnel(data.id);
171
- if (!tunnel) {
172
- this.serverError(res, 'Tunnel not found');
173
- return;
174
- }
175
- try {
176
- await ApiBuddy.removeTunnel(this.id, tunnel.id, this.host, this.token);
177
- } catch (err) {
178
- this.serverError(res, err.message);
179
- return;
180
- }
181
- this.serverOutput(res, {
182
- success: true
183
- });
184
- }
185
-
186
- async agentRestart() {
187
- let saved = await this.agentStop();
188
- if (!saved) return false;
189
- return await this.agentStart();
190
- }
191
-
192
- async disableAgentAndExit(txt) {
193
- logger.error(txt);
194
- try {
195
- await this.system.disable();
196
- } catch {
197
- // do nothing
198
- }
199
- try {
200
- await ApiBuddy.unregister(this.id, this.host, this.token);
201
- } catch {
202
- // do nothing
203
- }
204
- try {
205
- this.system.clearSystemFiles();
206
- } catch {
207
- // do nothing
208
- }
209
- try {
210
- this.system.clearAgentFiles();
211
- } catch {
212
- // do nothing
213
- }
214
- Output.exitError(txt);
215
- }
216
-
217
- async agentStop() {
218
- const saved = this.resaveAgentConfig(false, true);
219
- if (!saved) return false;
220
- await this.agent.stop();
221
- return true;
222
- }
223
-
224
- async processAgentRestart(req, res) {
225
- if (!this.agent) {
226
- this.serverError(res, 'Agent not enabled');
227
- return;
228
- }
229
- const saved = await this.agentRestart();
230
- if (!saved) {
231
- this.serverError(res, 'Something went wrong');
232
- return;
233
- }
234
- this.serverOutput(res, {
235
- success: true
236
- });
237
- }
238
-
239
- async processAgentStop(req, res) {
240
- if (!this.agent) {
241
- this.serverError(res, 'Agent not enabled');
242
- return;
243
- }
244
- const saved = await this.agentStop();
245
- if (!saved) {
246
- this.serverError(res, 'Something went wrong');
247
- return;
248
- }
249
- this.serverOutput(res, {
250
- success: true
251
- });
252
- }
253
-
254
- async agentStart() {
255
- const saved = this.resaveAgentConfig(true, true);
256
- if (!saved) {
257
- return false;
258
- }
259
- await this.agent.start();
260
- return true;
261
- }
262
-
263
- async processAgentStart(req, res) {
264
- if (!this.agent) {
265
- this.serverError(res, 'Agent not enabled');
266
- return;
267
- }
268
- const saved = await this.agentStart();
269
- if (!saved) {
270
- this.serverError(res, 'Something went wrong');
271
- return;
272
- }
273
- this.serverOutput(res, {
274
- success: true
275
- });
276
- }
277
-
278
- async processTunnelAdd(req, res) {
279
- if (!this.agent) {
280
- this.serverError(res, 'Agent not enabled');
281
- return;
282
- }
283
- let data;
284
- try {
285
- data = await this.processBody(req);
286
- } catch (err) {
287
- this.serverError(res, 'Wrong input data');
288
- return;
289
- }
290
- let tunnel;
291
- try {
292
- tunnel = await ApiBuddy.addTunnel(this.id, this.host, this.token, data, this.sshHostKey);
293
- } catch (err) {
294
- this.serverError(res, err.message);
295
- return;
296
- }
297
- this.agent.addTunnel(tunnel);
298
- this.serverOutput(res, {
299
- id: tunnel.id,
300
- type: tunnel.type,
301
- });
302
- }
303
-
304
- async processRequest(req, res) {
305
- logger.info(LOG_REQUEST(req.url));
306
- if (req.url === '/status') return this.processStatus(res);
307
- if (req.url === '/tunnels') return this.processTunnels(res);
308
- if (req.url === '/tunnel/add') return this.processTunnelAdd(req, res);
309
- if (req.url === '/tunnel/stop') return this.processTunnelStop(req, res);
310
- if (req.url === '/agent/start') return this.processAgentStart(req, res);
311
- if (req.url === '/agent/stop') return this.processAgentStop(req, res);
312
- if (req.url === '/agent/restart') return this.processAgentRestart(req, res);
313
- this.server404(res);
314
- }
315
-
316
- processUpgrade(req, socket, head) {
317
- logger.info(LOG_REQUEST(req.url));
318
- const url = new URL(req.url, 'http://localhost');
319
- const id = url.searchParams.get('id');
320
- if (url.pathname === '/tunnel' && id) {
321
- this.ws.handleUpgrade(req, socket, head, (con) => {
322
- this.ws.emit('connection', con, id);
323
- });
324
- return;
325
- }
326
- socket.destroy();
327
- }
328
-
329
- processWebsocketConnection(con, id) {
330
- if (!this.agent) {
331
- con.close();
332
- return;
333
- }
334
- const tunnel = this.agent.tunnels.find((t) => t.id === id);
335
- if (!tunnel || tunnel.type === TUNNEL_SSH) {
336
- con.close();
337
- return;
338
- }
339
- new AgentSocket(con, tunnel);
340
- }
341
-
342
- createServer() {
343
- this.server = http.createServer((req, res) => this.processRequest(req, res));
344
- this.ws = new WebSocketServer({ noServer: true });
345
- this.ws.on('connection', (con, id) => this.processWebsocketConnection(con, id));
346
- this.server.on('upgrade', (req, socket, head) => this.processUpgrade(req, socket, head));
347
- this.server.listen(this.port, async (err) => {
348
- if (err) {
349
- await this.disableAgentAndExit(LOG_ERROR_STARTING_AGENT_SERVER);
350
- } else {
351
- logger.info(LOG_AGENT_SERVER_STARTED);
352
- }
353
- });
354
- }
355
-
356
- resaveAgentConfig(shouldStart, forceParam = false) {
357
- if (!forceParam) {
358
- try {
359
- // load from config
360
- const json = this.system.loadAgentConfig();
361
- this.shouldStart = !!json.shouldStart;
362
- this.sshHostKey = json.sshHostKey;
363
- } catch {
364
- // save from param
365
- this.shouldStart = shouldStart;
366
- }
367
- if (!this.sshHostKey) this.sshHostKey = createSshHostKey();
368
- } else {
369
- // save from param
370
- this.shouldStart = shouldStart;
371
- }
372
- try {
373
- // resave config
374
- this.system.saveAgentConfig(this.shouldStart, this.sshHostKey);
375
- return true;
376
- } catch {
377
- return false;
378
- }
379
- }
380
-
381
- async load(id, host, token, port, start) {
382
- this.status = AGENT_STATUS_INITIALIZING;
383
- this.id = id;
384
- this.host = host;
385
- this.token = token;
386
- this.port = parseInt(port, 10);
387
- if (!this.id || !this.token || !this.host || !this.port) {
388
- await this.disableAgentAndExit(ERR_AGENT_NOT_REGISTERED);
389
- return;
390
- }
391
- const ok = this.resaveAgentConfig(start);
392
- if (!ok) {
393
- await this.disableAgentAndExit(ERR_SAVING_AGENT_CONFIG);
394
- return;
395
- }
396
- this.createServer();
397
- await this.tryFetch();
398
- logger.info(LOG_AGENT_STARTED);
399
- }
400
- }
401
-
402
- const AgentManager = new AgentManagerClass();
403
-
404
- module.exports = AgentManager;
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;