bdy 1.12.8-dev → 1.12.8-dev-pipeline-run

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 (156) hide show
  1. package/distTs/package.json +1 -1
  2. package/package.json +1 -1
  3. package/distTs/bin/cli.js +0 -5
  4. package/distTs/src/agent/agent.js +0 -328
  5. package/distTs/src/agent/linux.js +0 -124
  6. package/distTs/src/agent/manager.js +0 -519
  7. package/distTs/src/agent/osx.js +0 -146
  8. package/distTs/src/agent/socket/client.js +0 -178
  9. package/distTs/src/agent/socket/tunnel.js +0 -237
  10. package/distTs/src/agent/socket.js +0 -242
  11. package/distTs/src/agent/system.js +0 -179
  12. package/distTs/src/agent/wait.js +0 -23
  13. package/distTs/src/agent/windows.js +0 -159
  14. package/distTs/src/agent.js +0 -302
  15. package/distTs/src/api/agent.js +0 -99
  16. package/distTs/src/api/buddy.js +0 -139
  17. package/distTs/src/api/socket.js +0 -159
  18. package/distTs/src/cfg.js +0 -234
  19. package/distTs/src/command/agent/disable.js +0 -27
  20. package/distTs/src/command/agent/enable.js +0 -27
  21. package/distTs/src/command/agent/install.js +0 -117
  22. package/distTs/src/command/agent/restart.js +0 -27
  23. package/distTs/src/command/agent/run.js +0 -19
  24. package/distTs/src/command/agent/start.js +0 -28
  25. package/distTs/src/command/agent/status.js +0 -34
  26. package/distTs/src/command/agent/stop.js +0 -28
  27. package/distTs/src/command/agent/target/disable.js +0 -27
  28. package/distTs/src/command/agent/target/enable.js +0 -27
  29. package/distTs/src/command/agent/target/status.js +0 -32
  30. package/distTs/src/command/agent/target.js +0 -15
  31. package/distTs/src/command/agent/tunnel/http.js +0 -43
  32. package/distTs/src/command/agent/tunnel/list.js +0 -27
  33. package/distTs/src/command/agent/tunnel/remove.js +0 -28
  34. package/distTs/src/command/agent/tunnel/start.js +0 -34
  35. package/distTs/src/command/agent/tunnel/status.js +0 -31
  36. package/distTs/src/command/agent/tunnel/tcp.js +0 -43
  37. package/distTs/src/command/agent/tunnel/tls.js +0 -43
  38. package/distTs/src/command/agent/tunnel.js +0 -23
  39. package/distTs/src/command/agent/uninstall.js +0 -44
  40. package/distTs/src/command/agent/update.js +0 -38
  41. package/distTs/src/command/agent/version.js +0 -21
  42. package/distTs/src/command/agent.js +0 -35
  43. package/distTs/src/command/config/add/http.js +0 -25
  44. package/distTs/src/command/config/add/tcp.js +0 -25
  45. package/distTs/src/command/config/add/tls.js +0 -25
  46. package/distTs/src/command/config/add.js +0 -15
  47. package/distTs/src/command/config/get/region.js +0 -15
  48. package/distTs/src/command/config/get/timeout.js +0 -15
  49. package/distTs/src/command/config/get/token.js +0 -15
  50. package/distTs/src/command/config/get/tunnel.js +0 -19
  51. package/distTs/src/command/config/get/tunnels.js +0 -15
  52. package/distTs/src/command/config/get/whitelist.js +0 -15
  53. package/distTs/src/command/config/get.js +0 -21
  54. package/distTs/src/command/config/remove/tunnel.js +0 -19
  55. package/distTs/src/command/config/remove.js +0 -11
  56. package/distTs/src/command/config/set/region.js +0 -17
  57. package/distTs/src/command/config/set/timeout.js +0 -17
  58. package/distTs/src/command/config/set/token.js +0 -16
  59. package/distTs/src/command/config/set/whitelist.js +0 -17
  60. package/distTs/src/command/config/set.js +0 -17
  61. package/distTs/src/command/config.js +0 -17
  62. package/distTs/src/command/http.js +0 -30
  63. package/distTs/src/command/pre.js +0 -49
  64. package/distTs/src/command/start.js +0 -28
  65. package/distTs/src/command/tcp.js +0 -30
  66. package/distTs/src/command/tls.js +0 -30
  67. package/distTs/src/command/tunnel/config.js +0 -17
  68. package/distTs/src/command/tunnel/http.js +0 -31
  69. package/distTs/src/command/tunnel/start.js +0 -28
  70. package/distTs/src/command/tunnel/tcp.js +0 -30
  71. package/distTs/src/command/tunnel/tls.js +0 -30
  72. package/distTs/src/command/tunnel.js +0 -19
  73. package/distTs/src/command/ut/upload.js +0 -108
  74. package/distTs/src/command/ut.js +0 -12
  75. package/distTs/src/command/version.js +0 -13
  76. package/distTs/src/command/vt/close.js +0 -27
  77. package/distTs/src/command/vt/compare/validation.js +0 -173
  78. package/distTs/src/command/vt/compare.js +0 -97
  79. package/distTs/src/command/vt/exec.js +0 -81
  80. package/distTs/src/command/vt/installBrowser.js +0 -20
  81. package/distTs/src/command/vt/scrap.js +0 -193
  82. package/distTs/src/command/vt/storybook.js +0 -92
  83. package/distTs/src/command/vt.js +0 -22
  84. package/distTs/src/format.js +0 -174
  85. package/distTs/src/index.js +0 -34
  86. package/distTs/src/input.js +0 -270
  87. package/distTs/src/logger.js +0 -95
  88. package/distTs/src/output/interactive/tunnel.js +0 -860
  89. package/distTs/src/output/noninteractive/agent/tunnels.js +0 -43
  90. package/distTs/src/output/noninteractive/config/tunnel.js +0 -65
  91. package/distTs/src/output/noninteractive/config/tunnels.js +0 -18
  92. package/distTs/src/output/noninteractive/tunnel.js +0 -59
  93. package/distTs/src/output.js +0 -143
  94. package/distTs/src/server/cert.js +0 -52
  95. package/distTs/src/server/http1.js +0 -75
  96. package/distTs/src/server/http2.js +0 -78
  97. package/distTs/src/server/sftp.js +0 -497
  98. package/distTs/src/server/ssh.js +0 -446
  99. package/distTs/src/server/tls.js +0 -41
  100. package/distTs/src/ssh/client.js +0 -197
  101. package/distTs/src/texts.js +0 -421
  102. package/distTs/src/tunnel/agent.js +0 -103
  103. package/distTs/src/tunnel/api/agent.js +0 -100
  104. package/distTs/src/tunnel/api/buddy.js +0 -141
  105. package/distTs/src/tunnel/cfg.js +0 -243
  106. package/distTs/src/tunnel/compression.js +0 -44
  107. package/distTs/src/tunnel/dns.js +0 -53
  108. package/distTs/src/tunnel/html/503.html +0 -338
  109. package/distTs/src/tunnel/html.js +0 -33
  110. package/distTs/src/tunnel/http/log.js +0 -218
  111. package/distTs/src/tunnel/http/serve.js +0 -133
  112. package/distTs/src/tunnel/http/stream.js +0 -34
  113. package/distTs/src/tunnel/http.js +0 -508
  114. package/distTs/src/tunnel/identification.js +0 -103
  115. package/distTs/src/tunnel/latency.js +0 -83
  116. package/distTs/src/tunnel/output/interactive/tunnel.js +0 -867
  117. package/distTs/src/tunnel/output/noninteractive/agent/tunnels.js +0 -45
  118. package/distTs/src/tunnel/output/noninteractive/config/tunnel.js +0 -67
  119. package/distTs/src/tunnel/output/noninteractive/config/tunnels.js +0 -20
  120. package/distTs/src/tunnel/output/noninteractive/tunnel.js +0 -61
  121. package/distTs/src/tunnel/server/cert.js +0 -54
  122. package/distTs/src/tunnel/server/http1.js +0 -80
  123. package/distTs/src/tunnel/server/http2.js +0 -84
  124. package/distTs/src/tunnel/server/sftp.js +0 -514
  125. package/distTs/src/tunnel/server/ssh.js +0 -464
  126. package/distTs/src/tunnel/server/tls.js +0 -47
  127. package/distTs/src/tunnel/ssh/client.js +0 -211
  128. package/distTs/src/tunnel/tcp.js +0 -113
  129. package/distTs/src/tunnel/tunnel.js +0 -1010
  130. package/distTs/src/tunnel.js +0 -656
  131. package/distTs/src/types/ciInfo.js +0 -10
  132. package/distTs/src/types/options.js +0 -2
  133. package/distTs/src/types/plugin.js +0 -2
  134. package/distTs/src/types/queue.js +0 -2
  135. package/distTs/src/types/requests.js +0 -2
  136. package/distTs/src/types/resources.js +0 -2
  137. package/distTs/src/types/schemas.js +0 -54
  138. package/distTs/src/types/scrape.js +0 -2
  139. package/distTs/src/types/snapshots.js +0 -2
  140. package/distTs/src/types/storybook.js +0 -2
  141. package/distTs/src/types/tunnel.js +0 -141
  142. package/distTs/src/unitTest/ci.js +0 -108
  143. package/distTs/src/unitTest/requests.js +0 -72
  144. package/distTs/src/utils.js +0 -378
  145. package/distTs/src/visualTest/browser.js +0 -39
  146. package/distTs/src/visualTest/ci.js +0 -241
  147. package/distTs/src/visualTest/context.js +0 -52
  148. package/distTs/src/visualTest/exec.js +0 -51
  149. package/distTs/src/visualTest/linkUtils.js +0 -21
  150. package/distTs/src/visualTest/queue.js +0 -43
  151. package/distTs/src/visualTest/requests.js +0 -365
  152. package/distTs/src/visualTest/resources.js +0 -259
  153. package/distTs/src/visualTest/server.js +0 -109
  154. package/distTs/src/visualTest/snapshots.js +0 -129
  155. package/distTs/src/visualTest/utils/parseDom.js +0 -238
  156. package/distTs/src/visualTest/validation.js +0 -18
@@ -1,179 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- const fs_1 = require("fs");
7
- const https_1 = __importDefault(require("https"));
8
- const path_1 = __importDefault(require("path"));
9
- const utils_1 = require("../utils");
10
- const texts_1 = require("../texts");
11
- const logger_1 = __importDefault(require("../logger"));
12
- class AgentSystem {
13
- // extend
14
- isEnabled() {
15
- return Promise.resolve(false);
16
- }
17
- // extend
18
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
19
- enable(id, host, token, port, start, user, pass, debug) {
20
- return Promise.reject();
21
- }
22
- // extend
23
- update() {
24
- return Promise.reject();
25
- }
26
- // extend
27
- disable() {
28
- return Promise.reject();
29
- }
30
- // extend
31
- getSystemConfigPath() {
32
- return '';
33
- }
34
- // extend
35
- getSystemConfigDir() {
36
- return '';
37
- }
38
- getAgentConfigDir() {
39
- return (0, utils_1.getHomeDirectory)();
40
- }
41
- getAgentConfigPath() {
42
- return path_1.default.join(this.getAgentConfigDir(), 'agent.json');
43
- }
44
- // extend
45
- hasAdminRights() {
46
- return Promise.resolve(false);
47
- }
48
- // extend
49
- isSupported() {
50
- return Promise.resolve(false);
51
- }
52
- // extend
53
- getBinaryArchive() {
54
- return '';
55
- }
56
- extractArchive(source, dest) {
57
- return new Promise((resolve, reject) => {
58
- // @ts-ignore
59
- import('@xhmikosr/decompress')
60
- .then(({ default: decompress }) => {
61
- decompress(source, dest)
62
- .then(() => {
63
- (0, fs_1.rmSync)(source, { force: true });
64
- resolve();
65
- })
66
- .catch((err) => {
67
- (0, fs_1.rmSync)(source, { force: true });
68
- reject(err);
69
- });
70
- })
71
- .catch(reject);
72
- });
73
- }
74
- extractBinaryArchive() {
75
- return this.extractArchive(this.getBinaryArchivePath(), this.getSystemConfigDir());
76
- }
77
- loadSystemConfig() {
78
- const str = (0, fs_1.readFileSync)(this.getSystemConfigPath(), 'utf-8');
79
- return JSON.parse(str);
80
- }
81
- loadAgentConfig() {
82
- const str = (0, fs_1.readFileSync)(this.getAgentConfigPath(), 'utf-8');
83
- return JSON.parse(str);
84
- }
85
- clearSystemFiles() {
86
- (0, fs_1.rmSync)(this.getSystemBinaryPath(), { force: true });
87
- (0, fs_1.rmSync)(this.getSystemConfigPath(), { force: true });
88
- }
89
- clearAgentFiles() {
90
- (0, fs_1.rmSync)(this.getAgentConfigPath(), { force: true });
91
- }
92
- ensureSystemConfigDir() {
93
- (0, fs_1.mkdirSync)(this.getSystemConfigDir(), { recursive: true });
94
- }
95
- saveFile(p, c) {
96
- return new Promise((resolve, reject) => {
97
- (0, fs_1.writeFile)(p, c, 'utf-8', (err) => {
98
- if (!err)
99
- resolve();
100
- else
101
- reject(err);
102
- });
103
- });
104
- }
105
- getSystemBinaryPath() {
106
- return path_1.default.join(this.getSystemConfigDir(), 'bdy');
107
- }
108
- getBinaryArchivePath() {
109
- return path_1.default.join(this.getSystemConfigDir(), this.getBinaryArchive());
110
- }
111
- downloadFile(url, dest) {
112
- return new Promise((resolve, reject) => {
113
- const file = (0, fs_1.createWriteStream)(dest);
114
- const request = https_1.default.get(url, (response) => {
115
- if (response.statusCode !== 200) {
116
- file.close();
117
- (0, fs_1.rmSync)(dest);
118
- reject(new Error('File not found'));
119
- return;
120
- }
121
- response.pipe(file);
122
- file.once('finish', () => {
123
- file.close((err) => {
124
- if (err)
125
- reject(err);
126
- else
127
- resolve();
128
- });
129
- });
130
- });
131
- request.once('error', (err) => {
132
- file.close();
133
- (0, fs_1.rmSync)(dest);
134
- reject(err);
135
- });
136
- });
137
- }
138
- downloadBinaryArchive() {
139
- const archivePath = this.getBinaryArchivePath();
140
- const env = (0, utils_1.getVersionEnv)();
141
- const version = (0, utils_1.getVersionWithoutEnv)();
142
- const archive = this.getBinaryArchive();
143
- return this.downloadFile(`https://es.buddy.works/bdy/${env}/${version}/${archive}`, archivePath);
144
- }
145
- saveSystemConfig(id, host, token, port) {
146
- try {
147
- logger_1.default.info(texts_1.LOG_SAVING_AGENT_SYSTEM_CONFIG);
148
- this.ensureSystemConfigDir();
149
- (0, fs_1.writeFileSync)(this.getSystemConfigPath(), JSON.stringify({
150
- id,
151
- host,
152
- token,
153
- port,
154
- }), 'utf-8');
155
- return true;
156
- }
157
- catch (err) {
158
- logger_1.default.error(texts_1.LOG_ERROR_SAVING_AGENT_SYSTEM_CONFIG);
159
- logger_1.default.error(err);
160
- return false;
161
- }
162
- }
163
- saveAgentConfig(shouldStart, sshHostKey) {
164
- try {
165
- logger_1.default.info(texts_1.LOG_SAVING_AGENT_LOCAL_CONFIG);
166
- (0, fs_1.writeFileSync)(this.getAgentConfigPath(), JSON.stringify({
167
- shouldStart,
168
- sshHostKey,
169
- }), 'utf-8');
170
- return true;
171
- }
172
- catch (err) {
173
- logger_1.default.error(texts_1.LOG_ERROR_SAVING_AGENT_LOCAL_CONFIG);
174
- logger_1.default.error(err);
175
- return false;
176
- }
177
- }
178
- }
179
- exports.default = AgentSystem;
@@ -1,23 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const utils_1 = require("../utils");
4
- const waitUntilAgentEnabled = async (api, timeout, onTimeout, onSuccess) => {
5
- let s;
6
- try {
7
- s = await api.fetchStatus();
8
- }
9
- catch {
10
- s = {};
11
- }
12
- if (s.enabled) {
13
- onSuccess();
14
- }
15
- else if (timeout < 1000) {
16
- onTimeout();
17
- }
18
- else {
19
- await (0, utils_1.sleep)(1000);
20
- return waitUntilAgentEnabled(api, timeout - 1000, onTimeout, onSuccess);
21
- }
22
- };
23
- exports.default = waitUntilAgentEnabled;
@@ -1,159 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- const system_1 = __importDefault(require("./system"));
7
- const child_process_1 = require("child_process");
8
- const path_1 = __importDefault(require("path"));
9
- const fs_1 = require("fs");
10
- const logger_1 = __importDefault(require("../logger"));
11
- const texts_1 = require("../texts");
12
- class AgentWindows extends system_1.default {
13
- hasAdminRights() {
14
- return new Promise((resolve) => {
15
- (0, child_process_1.exec)('net session', (err) => {
16
- resolve(!err);
17
- });
18
- });
19
- }
20
- isSupported() {
21
- return Promise.resolve(process.arch === 'x64');
22
- }
23
- isEnabled() {
24
- return new Promise((resolve) => {
25
- this.sc('query bdy')
26
- .then(() => {
27
- resolve(true);
28
- })
29
- .catch(() => {
30
- resolve(false);
31
- });
32
- });
33
- }
34
- getBinaryArchive() {
35
- return 'win-x64.zip';
36
- }
37
- getSystemConfigDir() {
38
- return 'C:\\ProgramData\\bdy';
39
- }
40
- getSystemConfigPath() {
41
- return path_1.default.join(this.getSystemConfigDir(), 'agent.json');
42
- }
43
- getNssmArchivePath() {
44
- return path_1.default.join(this.getSystemConfigDir(), 'nssm.zip');
45
- }
46
- downloadNssm() {
47
- return this.downloadFile('https://es.buddy.works/bdy/nssm.zip', this.getNssmArchivePath());
48
- }
49
- extractNssm() {
50
- return this.extractArchive(this.getNssmArchivePath(), this.getSystemConfigDir());
51
- }
52
- clearNssmExtract() {
53
- (0, fs_1.rmSync)(path_1.default.join(this.getSystemConfigDir(), '__MACOSX'), {
54
- force: true,
55
- recursive: true,
56
- });
57
- }
58
- getNssmBinaryPath() {
59
- return path_1.default.join(this.getSystemConfigDir(), 'nssm', 'win64', 'nssm.exe');
60
- }
61
- getSystemBinaryPath() {
62
- return path_1.default.join(this.getSystemConfigDir(), 'bdy.exe');
63
- }
64
- clearSystemFiles() {
65
- (0, fs_1.rmSync)(this.getSystemBinaryPath(), { force: true });
66
- (0, fs_1.rmSync)(this.getSystemConfigPath(), { force: true });
67
- (0, fs_1.rmSync)(path_1.default.join(this.getSystemConfigDir(), 'nssm'), {
68
- force: true,
69
- recursive: true,
70
- });
71
- }
72
- async update() {
73
- try {
74
- logger_1.default.info(texts_1.LOG_AGENT_STOPPING_SYSTEM);
75
- await this.nssm('stop bdy');
76
- logger_1.default.info(texts_1.LOG_AGENT_SYSTEM_DIR);
77
- await this.ensureSystemConfigDir();
78
- logger_1.default.info(texts_1.LOG_AGENT_DOWNLOADING_ARCHIVE);
79
- await this.downloadBinaryArchive();
80
- logger_1.default.info(texts_1.LOG_AGENT_EXTRACTING_ARCHIVE);
81
- await this.extractBinaryArchive();
82
- try {
83
- logger_1.default.info(texts_1.LOG_AGENT_STARTING_SYSTEM);
84
- await this.nssm('start bdy');
85
- }
86
- catch {
87
- // do nothing, it will throw operation pending
88
- }
89
- }
90
- catch (err) {
91
- logger_1.default.error(err);
92
- throw err;
93
- }
94
- }
95
- async enable(id, host, token, port, start, user, pass, debug) {
96
- try {
97
- logger_1.default.info(texts_1.LOG_AGENT_SYSTEM_DIR);
98
- await this.ensureSystemConfigDir();
99
- logger_1.default.info(texts_1.LOG_AGENT_NSSM_DOWNLOADING);
100
- await this.downloadNssm();
101
- logger_1.default.info(texts_1.LOG_AGENT_NSSM_EXTRACTING);
102
- await this.extractNssm();
103
- logger_1.default.info(texts_1.LOG_AGENT_NSSM_CLEARING);
104
- this.clearNssmExtract();
105
- logger_1.default.info(texts_1.LOG_AGENT_DOWNLOADING_ARCHIVE);
106
- await this.downloadBinaryArchive();
107
- logger_1.default.info(texts_1.LOG_AGENT_EXTRACTING_ARCHIVE);
108
- await this.extractBinaryArchive();
109
- logger_1.default.info(texts_1.LOG_AGENT_ENABLING_SYSTEM);
110
- await this.nssm(`install bdy ${this.getSystemBinaryPath()}`);
111
- await this.nssm(`set bdy AppParameters agent run --id="${id}" --host="${host}" --token="${token}" --port=${port} --start=${!!start}`);
112
- await this.nssm('set bdy DisplayName Buddy Tunnel Agent');
113
- await this.nssm('set bdy AppThrottle 5000');
114
- await this.nssm('set bdy AppRestartDelay 0');
115
- await this.nssm('set bdy AppExit Default Restart');
116
- await this.nssm('set bdy Start SERVICE_AUTO_START');
117
- await this.nssm(`set bdy AppEnvironmentExtra DEBUG=${debug ? 1 : 0}`);
118
- if (user && pass) {
119
- await this.nssm(`set bdy ObjectName ${user} ${pass}`);
120
- }
121
- this.nssm('start bdy').then().catch();
122
- logger_1.default.info(texts_1.LOG_AGENT_ENABLED);
123
- }
124
- catch (err) {
125
- logger_1.default.info(err);
126
- throw err;
127
- }
128
- }
129
- async disable() {
130
- try {
131
- await this.nssm('stop bdy');
132
- }
133
- catch {
134
- // do nothing
135
- }
136
- await this.nssm('remove bdy confirm');
137
- }
138
- nssm(cmd) {
139
- return new Promise((resolve, reject) => {
140
- (0, child_process_1.exec)(`${this.getNssmBinaryPath()} ${cmd}`, (err) => {
141
- if (!err)
142
- resolve();
143
- else
144
- reject(err);
145
- });
146
- });
147
- }
148
- sc(cmd) {
149
- return new Promise((resolve, reject) => {
150
- (0, child_process_1.exec)(`sc ${cmd}`, (err) => {
151
- if (!err)
152
- resolve();
153
- else
154
- reject(err);
155
- });
156
- });
157
- }
158
- }
159
- exports.default = AgentWindows;
@@ -1,302 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- const events_1 = __importDefault(require("events"));
7
- const logger_js_1 = __importDefault(require("./logger.js"));
8
- const texts_js_1 = require("./texts.js");
9
- const utils_js_1 = require("./utils.js");
10
- const tunnel_js_1 = __importDefault(require("./tunnel.js"));
11
- const output_js_1 = __importDefault(require("./output.js"));
12
- const socket_1 = __importDefault(require("./api/socket"));
13
- class Agent extends events_1.default {
14
- constructor(id, host, token, service, target, api, disabled) {
15
- super();
16
- this.id = id;
17
- this.host = host;
18
- this.token = token;
19
- this.service = service;
20
- this.target = target;
21
- this.disabled = disabled;
22
- this.tunnels = [];
23
- this.tunnelsUpdating = false;
24
- this.started = false;
25
- this.api = api;
26
- this.manager = null;
27
- this.onRefresh = null;
28
- this.socket = new socket_1.default(host, id, token);
29
- this.socket.on(utils_js_1.SOCKET_IO_EVENT_FETCH_SUCCESS, (data) => this._onSocketFetch(data));
30
- this.socket.on(utils_js_1.SOCKET_IO_EVENT_FETCH_FAILED, (err) => this._onSocketFetchFailed(err));
31
- this.socket.on(utils_js_1.SOCKET_IO_EVENT_CONNECTED, () => this._onSocketConnected());
32
- this.socket.on(utils_js_1.SOCKET_IO_EVENT_DISCONNECTED, () => this._onSocketDisconnected());
33
- this.socket.on(utils_js_1.SOCKET_IO_EVENT_TUNNEL, (tunnel, action) => this._onSocketTunnel(tunnel, action));
34
- this.socket.on(utils_js_1.SOCKET_IO_EVENT_AGENT, (agent, action) => this._onSocketAgent(agent, action));
35
- }
36
- destroy() {
37
- if (this.socket) {
38
- this.socket.removeAllListeners();
39
- this.socket.destroy();
40
- this.socket = null;
41
- }
42
- this.manager = null;
43
- this.onRefresh = null;
44
- this.api = null;
45
- // musza byc manualnie wczesniej zniszczone to tylko czysci obiekt
46
- this.tunnels = null;
47
- }
48
- _onSocketTunnel(data, action) {
49
- if (action === 'DEL') {
50
- this.destroyTunnel(data.id);
51
- if (!this.service && this.tunnels.length <= 0)
52
- output_js_1.default.exitError(texts_js_1.ERR_TUNNEL_REMOVED);
53
- }
54
- else {
55
- logger_js_1.default.debug('Socket fetch tunnel after sync push');
56
- this.socket.fetch(this.started, null, null, false, []);
57
- }
58
- }
59
- async _onSocketAgent(data, action) {
60
- if (action === 'DEL') {
61
- if (this.manager) {
62
- return this.manager.disableAgentAndExit(texts_js_1.ERR_AGENT_REMOVED);
63
- }
64
- output_js_1.default.exitError(texts_js_1.ERR_AGENT_REMOVED);
65
- }
66
- else {
67
- this.target = !!data.target;
68
- this.disabled = !!data.disabled;
69
- if (data.disabled === this.started) {
70
- await this.makeAction(data.disabled ? utils_js_1.AGENT_ACTION_STOP : utils_js_1.AGENT_ACTION_START);
71
- }
72
- else if (data.action) {
73
- try {
74
- await this.makeAction(data.action);
75
- }
76
- catch (err) {
77
- logger_js_1.default.error('Agent sync error');
78
- logger_js_1.default.error(err);
79
- }
80
- }
81
- }
82
- }
83
- _onSocketConnected() {
84
- if (this.onRefresh)
85
- this.onRefresh(true);
86
- }
87
- _onSocketDisconnected() {
88
- if (this.onRefresh)
89
- this.onRefresh(false);
90
- }
91
- async _onSocketFetch(data) {
92
- try {
93
- if (data.agent.action)
94
- await this.makeAction(data.agent.action);
95
- this.refreshTunnels(data.tunnels || []);
96
- }
97
- catch (err) {
98
- logger_js_1.default.error('Fetch error');
99
- logger_js_1.default.error(err);
100
- }
101
- }
102
- _onSocketFetchFailed(err) {
103
- if (err instanceof utils_js_1.ApiErrorAgentNotFound) {
104
- if (this.manager) {
105
- return this.manager.disableAgentAndExit(texts_js_1.ERR_AGENT_REMOVED);
106
- }
107
- output_js_1.default.exitError(texts_js_1.ERR_AGENT_REMOVED);
108
- }
109
- else {
110
- logger_js_1.default.error(texts_js_1.LOG_ERROR_WHILE_REFRESHING_AGENT);
111
- logger_js_1.default.error(err);
112
- }
113
- }
114
- startRefreshingConfiguration(onRefresh) {
115
- this.onRefresh = onRefresh;
116
- if (this.onRefresh)
117
- this.onRefresh(this.socket.connected);
118
- this.refreshIn15();
119
- }
120
- async start() {
121
- this.started = true;
122
- this.tunnels.forEach((t) => {
123
- t.start();
124
- });
125
- this.socket.fetch(this.started, '', null, true, this.getTunnelsUpdate());
126
- }
127
- async enable() {
128
- this.disabled = false;
129
- this.started = true;
130
- this.tunnels.forEach((t) => {
131
- t.start();
132
- });
133
- this.socket.fetch(this.started, '', false, true, this.getTunnelsUpdate());
134
- }
135
- async stop(clearAction = true) {
136
- this.started = false;
137
- this.tunnels.forEach((t) => {
138
- t.stop();
139
- });
140
- this.socket.fetch(this.started, clearAction ? '' : null, null, false, this.getTunnelsUpdate());
141
- }
142
- async disable() {
143
- this.disabled = true;
144
- this.started = false;
145
- this.tunnels.forEach((t) => {
146
- t.stop();
147
- });
148
- this.socket.fetch(this.started, '', true, false, this.getTunnelsUpdate());
149
- }
150
- async restart() {
151
- await this.stop(false);
152
- await (0, utils_js_1.sleep)(100);
153
- await this.start();
154
- }
155
- async delete() {
156
- try {
157
- await this.api.unregister(this.id, this.host, this.token);
158
- }
159
- catch {
160
- // do nothing
161
- }
162
- process.exit(0);
163
- }
164
- refreshIn15(fetch = true, counter = 1) {
165
- setTimeout(async () => {
166
- try {
167
- if (fetch || counter >= 3) {
168
- counter = 0;
169
- this.socket.fetch(this.started, null, null, false, this.getTunnelsUpdate());
170
- }
171
- else {
172
- this.update();
173
- }
174
- }
175
- catch {
176
- // do nothing
177
- }
178
- this.refreshIn15(false, counter + 1);
179
- }, 15000);
180
- }
181
- httpRequest(tunnelId, logRequest) {
182
- this.socket.request(tunnelId, logRequest);
183
- }
184
- update(force = false) {
185
- this.socket.update(this.started, this.getTunnelsUpdate(), force);
186
- }
187
- async makeAction(action) {
188
- if (action === utils_js_1.AGENT_ACTION_STOP) {
189
- if (this.manager) {
190
- return this.manager.agentStop();
191
- }
192
- return this.stop();
193
- }
194
- if (action === utils_js_1.AGENT_ACTION_START) {
195
- if (this.manager) {
196
- return this.manager.agentStart();
197
- }
198
- return this.start();
199
- }
200
- if (action === utils_js_1.AGENT_ACTION_RESTART) {
201
- if (this.manager) {
202
- return this.manager.agentRestart();
203
- }
204
- return this.restart();
205
- }
206
- if (action === utils_js_1.AGENT_ACTION_DELETE) {
207
- if (this.manager) {
208
- return this.manager.disableAgentAndExit(texts_js_1.ERR_AGENT_REMOVED);
209
- }
210
- return this.delete();
211
- }
212
- }
213
- refreshTunnels(tunnels) {
214
- if (this.tunnelsUpdating)
215
- return;
216
- this.tunnelsUpdating = true;
217
- let hasTunnels = false;
218
- let haveTunnels = false;
219
- try {
220
- const tt = [...this.tunnels];
221
- hasTunnels = tt.length > 0;
222
- haveTunnels = tunnels.length > 0;
223
- if (hasTunnels) {
224
- tt.forEach((tunnel) => {
225
- if (!tunnels.find((t) => t.id === tunnel.id)) {
226
- this.destroyTunnel(tunnel.id);
227
- }
228
- });
229
- }
230
- if (haveTunnels) {
231
- tunnels.forEach((data) => {
232
- let sshHostKey;
233
- if (this.manager)
234
- sshHostKey = this.manager.sshHostKey;
235
- const tunnel = tt.find((tunnel) => data.id === tunnel.id);
236
- if (!tunnel) {
237
- this.addTunnel(new tunnel_js_1.default({
238
- ...data,
239
- sshHostKey,
240
- agent: {
241
- id: this.id,
242
- host: this.host,
243
- token: this.token
244
- },
245
- }));
246
- }
247
- else if (tunnel.hasChanged(data)) {
248
- tunnel.recreate(data);
249
- }
250
- });
251
- }
252
- }
253
- catch {
254
- // do nothing
255
- }
256
- this.tunnelsUpdating = false;
257
- if (!this.service && hasTunnels && !haveTunnels) {
258
- output_js_1.default.exitError(texts_js_1.ERR_TUNNEL_REMOVED);
259
- }
260
- }
261
- getTunnelsUpdate() {
262
- const update = [];
263
- this.tunnels.forEach((t) => {
264
- update.push({
265
- id: t.id,
266
- active: t.status === utils_js_1.TUNNEL_OPEN,
267
- regionLatency: t.regionLatency ? t.regionLatency.latency : -1,
268
- targetLatency: t.targetLatency ? t.targetLatency.latency : -1,
269
- totalConnections: t.totalConnections,
270
- currentConnections: Object.keys(t.connections).length,
271
- httpIdentified: t.identify ? t.identify.type : '',
272
- });
273
- });
274
- return update;
275
- }
276
- addTunnel(tunnel) {
277
- this.tunnels.push(tunnel);
278
- if (this.started)
279
- tunnel.start();
280
- tunnel.on(utils_js_1.TUNNEL_EVENT_OPEN, () => this.update(true));
281
- tunnel.on(utils_js_1.TUNNEL_EVENT_CLOSED, () => this.update());
282
- tunnel.on(utils_js_1.TUNNEL_EVENT_TCP_OPEN, () => this.update());
283
- tunnel.on(utils_js_1.TUNNEL_EVENT_TCP_CLOSED, () => this.update());
284
- tunnel.on(utils_js_1.TUNNEL_EVENT_TLS_OPEN, () => this.update());
285
- tunnel.on(utils_js_1.TUNNEL_EVENT_TLS_CLOSED, () => this.update());
286
- tunnel.on(utils_js_1.TUNNEL_EVENT_HTTP_IDENTIFIED, () => this.update());
287
- tunnel.on(utils_js_1.TUNNEL_EVENT_HTTP_OPEN, () => this.update());
288
- tunnel.on(utils_js_1.TUNNEL_EVENT_HTTP_CLOSED, () => this.update());
289
- tunnel.on(utils_js_1.TUNNEL_EVENT_STOPPED, () => this.update());
290
- tunnel.on(utils_js_1.TUNNEL_EVENT_HTTP_REQUEST, (t, logRequest) => this.httpRequest(t.id, logRequest));
291
- }
292
- destroyTunnel(id) {
293
- let tunnel = this.tunnels.find((t) => t.id === id);
294
- if (!tunnel)
295
- return null;
296
- this.tunnels = this.tunnels.filter((t) => t.id !== tunnel.id);
297
- tunnel.stop();
298
- tunnel.removeAllListeners();
299
- return tunnel;
300
- }
301
- }
302
- exports.default = Agent;