verteilen-core 1.2.4 → 1.2.7

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 (142) hide show
  1. package/dist/client/analysis.d.ts +35 -0
  2. package/dist/client/analysis.js +382 -0
  3. package/dist/client/client.d.ts +25 -0
  4. package/dist/client/client.js +227 -0
  5. package/dist/client/cluster.d.ts +1 -0
  6. package/dist/{src/client → client}/cluster.js +1 -2
  7. package/dist/client/database.d.ts +13 -0
  8. package/dist/client/database.js +37 -0
  9. package/dist/client/execute.d.ts +24 -0
  10. package/dist/{src/client → client}/execute.js +61 -51
  11. package/dist/client/http.d.ts +7 -0
  12. package/dist/client/http.js +40 -0
  13. package/dist/client/javascript.d.ts +47 -0
  14. package/dist/{src/client → client}/javascript.js +110 -136
  15. package/dist/client/job_database.d.ts +10 -0
  16. package/dist/client/job_database.js +34 -0
  17. package/dist/client/job_execute.d.ts +20 -0
  18. package/dist/client/job_execute.js +173 -0
  19. package/dist/client/os.d.ts +29 -0
  20. package/dist/client/os.js +197 -0
  21. package/dist/client/resource.d.ts +7 -0
  22. package/dist/client/resource.js +107 -0
  23. package/dist/client/shell.d.ts +16 -0
  24. package/dist/client/shell.js +122 -0
  25. package/dist/computed.d.ts +13 -0
  26. package/dist/index.d.ts +3 -0
  27. package/dist/index.js +3 -3
  28. package/dist/interface/base.d.ts +70 -0
  29. package/dist/interface/bus.d.ts +90 -0
  30. package/dist/interface/enum.d.ts +143 -0
  31. package/dist/interface/execute.d.ts +20 -0
  32. package/dist/interface/record.d.ts +93 -0
  33. package/dist/interface/server.d.ts +76 -0
  34. package/dist/interface/struct.d.ts +182 -0
  35. package/dist/interface/table.d.ts +20 -0
  36. package/dist/interface/ui.d.ts +29 -0
  37. package/dist/interface.d.ts +29 -0
  38. package/dist/plugins/i18n.d.ts +1666 -0
  39. package/dist/script/console_manager.d.ts +18 -0
  40. package/dist/script/console_manager.js +131 -0
  41. package/dist/script/console_server_manager.d.ts +13 -0
  42. package/dist/script/console_server_manager.js +40 -0
  43. package/dist/script/execute/base.d.ts +45 -0
  44. package/dist/script/execute/base.js +200 -0
  45. package/dist/script/execute/feedback.d.ts +12 -0
  46. package/dist/script/execute/feedback.js +179 -0
  47. package/dist/script/execute/interface.d.ts +8 -0
  48. package/dist/script/execute/region_job.d.ts +5 -0
  49. package/dist/{src/script → script}/execute/region_job.js +1 -0
  50. package/dist/script/execute/region_project.d.ts +10 -0
  51. package/dist/{src/script → script}/execute/region_project.js +4 -0
  52. package/dist/script/execute/region_subtask.d.ts +5 -0
  53. package/dist/{src/script → script}/execute/region_subtask.js +1 -0
  54. package/dist/script/execute/region_task.d.ts +10 -0
  55. package/dist/{src/script → script}/execute/region_task.js +4 -0
  56. package/dist/script/execute/runner.d.ts +14 -0
  57. package/dist/script/execute/runner.js +264 -0
  58. package/dist/script/execute/util_parser.d.ts +14 -0
  59. package/dist/script/execute/util_parser.js +135 -0
  60. package/dist/script/execute_manager.d.ts +25 -0
  61. package/dist/script/execute_manager.js +297 -0
  62. package/dist/script/socket_manager.d.ts +31 -0
  63. package/dist/script/socket_manager.js +263 -0
  64. package/dist/script/webhook_manager.d.ts +3 -0
  65. package/dist/script/webhook_server_manager.d.ts +13 -0
  66. package/dist/script/webhook_server_manager.js +133 -0
  67. package/dist/server/detail.d.ts +72 -0
  68. package/dist/server/detail.js +451 -0
  69. package/dist/server/io.d.ts +46 -0
  70. package/dist/{src/server → server}/io.js +42 -51
  71. package/dist/server/plugin.d.ts +18 -0
  72. package/dist/{src/server → server}/plugin.js +45 -54
  73. package/dist/server/server.d.ts +27 -0
  74. package/dist/server/server.js +53 -0
  75. package/dist/server.d.ts +4 -0
  76. package/dist/util/console_handle.d.ts +20 -0
  77. package/dist/util/console_handle.js +244 -0
  78. package/dist/util/log_handle.d.ts +23 -0
  79. package/dist/util/log_handle.js +224 -0
  80. package/index.ts +1 -6
  81. package/package.json +1 -1
  82. package/src/index.ts +6 -0
  83. package/test/TEST.d.ts +1 -0
  84. package/test/TEST.js +88 -0
  85. package/test/client/execute.test.d.ts +1 -0
  86. package/{dist/test → test}/client/execute.test.js +6 -15
  87. package/test/client/javascript.test.d.ts +1 -0
  88. package/{dist/test → test}/client/javascript.test.js +21 -30
  89. package/test/client/server.test.d.ts +1 -0
  90. package/test/client/task.test.d.ts +1 -0
  91. package/{dist/test → test}/client/task.test.js +1 -1
  92. package/test/script/parser.test.d.ts +1 -0
  93. package/test/script/socket.test.d.ts +1 -0
  94. package/tsconfig.json +8 -4
  95. package/dist/jest.config.js +0 -8
  96. package/dist/src/client/analysis.js +0 -383
  97. package/dist/src/client/client.js +0 -233
  98. package/dist/src/client/database.js +0 -36
  99. package/dist/src/client/http.js +0 -37
  100. package/dist/src/client/job_database.js +0 -36
  101. package/dist/src/client/job_execute.js +0 -171
  102. package/dist/src/client/job_parameter.js +0 -36
  103. package/dist/src/client/os.js +0 -202
  104. package/dist/src/client/parameter.js +0 -36
  105. package/dist/src/client/resource.js +0 -118
  106. package/dist/src/client/shell.js +0 -118
  107. package/dist/src/script/console_manager.js +0 -125
  108. package/dist/src/script/console_server_manager.js +0 -37
  109. package/dist/src/script/execute/base.js +0 -197
  110. package/dist/src/script/execute/feedback.js +0 -188
  111. package/dist/src/script/execute/runner.js +0 -274
  112. package/dist/src/script/execute/util_parser.js +0 -137
  113. package/dist/src/script/execute_manager.js +0 -309
  114. package/dist/src/script/socket_manager.js +0 -260
  115. package/dist/src/script/webhook_server_manager.js +0 -140
  116. package/dist/src/server/detail.js +0 -428
  117. package/dist/src/server/server.js +0 -51
  118. package/dist/src/util/console_handle.js +0 -248
  119. package/dist/src/util/log_handle.js +0 -230
  120. package/dist/src/util/server/console_handle.js +0 -248
  121. package/dist/src/util/server/log_handle.js +0 -230
  122. package/dist/test/TEST.js +0 -105
  123. /package/dist/{src/computed.js → computed.js} +0 -0
  124. /package/dist/{src/interface → interface}/base.js +0 -0
  125. /package/dist/{src/interface → interface}/bus.js +0 -0
  126. /package/dist/{src/interface → interface}/enum.js +0 -0
  127. /package/dist/{src/interface → interface}/execute.js +0 -0
  128. /package/dist/{src/interface → interface}/record.js +0 -0
  129. /package/dist/{src/interface → interface}/server.js +0 -0
  130. /package/dist/{src/interface → interface}/struct.js +0 -0
  131. /package/dist/{src/interface → interface}/table.js +0 -0
  132. /package/dist/{src/interface → interface}/ui.js +0 -0
  133. /package/dist/{src/interface.js → interface.js} +0 -0
  134. /package/dist/{src/lan → lan}/en.json +0 -0
  135. /package/dist/{src/lan → lan}/zh_TW.json +0 -0
  136. /package/dist/{src/plugins → plugins}/i18n.js +0 -0
  137. /package/dist/{src/script → script}/execute/interface.js +0 -0
  138. /package/dist/{src/script → script}/webhook_manager.js +0 -0
  139. /package/dist/{src/server.js → server.js} +0 -0
  140. /package/{dist/test → test}/client/server.test.js +0 -0
  141. /package/{dist/test → test}/script/parser.test.js +0 -0
  142. /package/{dist/test → test}/script/socket.test.js +0 -0
@@ -0,0 +1,35 @@
1
+ import { WebSocket } from 'ws';
2
+ import { Header, Messager, Messager_log } from "../interface";
3
+ import { Client } from './client';
4
+ export declare class ClientAnalysis {
5
+ private messager;
6
+ private messager_log;
7
+ private client;
8
+ private exec;
9
+ private shell;
10
+ private resource_wanter;
11
+ private resource_thread;
12
+ private resource_cache;
13
+ constructor(_messager: Messager, _messager_log: Messager_log, _client: Client);
14
+ analysis: (h: Header | undefined, source: WebSocket) => 0 | 1 | 2;
15
+ private execute_job;
16
+ private release;
17
+ private set_database;
18
+ private set_libs;
19
+ private exec_checker;
20
+ private pong;
21
+ private plugin_info;
22
+ private get_releases;
23
+ private filterout;
24
+ private write_plugin;
25
+ private finish_plugin;
26
+ private plugin_download;
27
+ private plugin_remove;
28
+ private resource_start;
29
+ private resource_end;
30
+ update: (client: Client) => void;
31
+ disconnect: (source: WebSocket) => void;
32
+ stop_all: () => void;
33
+ destroy: () => void;
34
+ private resource_require;
35
+ }
@@ -0,0 +1,382 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.ClientAnalysis = void 0;
37
+ const child_process_1 = require("child_process");
38
+ const interface_1 = require("../interface");
39
+ const client_1 = require("./client");
40
+ const execute_1 = require("./execute");
41
+ const shell_1 = require("./shell");
42
+ const fs_1 = require("fs");
43
+ const path = __importStar(require("path"));
44
+ const os = __importStar(require("os"));
45
+ class ClientAnalysis {
46
+ messager;
47
+ messager_log;
48
+ client;
49
+ exec;
50
+ shell;
51
+ resource_wanter = [];
52
+ resource_thread = undefined;
53
+ resource_cache = undefined;
54
+ constructor(_messager, _messager_log, _client) {
55
+ this.client = _client;
56
+ this.messager = _messager;
57
+ this.messager_log = _messager_log;
58
+ this.shell = new shell_1.ClientShell(_messager, _messager_log, this.client);
59
+ this.exec = [];
60
+ }
61
+ analysis = (h, source) => {
62
+ const typeMap = {
63
+ 'execute_job': this.execute_job,
64
+ 'release': this.release,
65
+ 'stop_job': this.stop_all,
66
+ 'set_database': this.set_database,
67
+ 'set_libs': this.set_libs,
68
+ 'shell_folder': this.shell.shell_folder,
69
+ 'open_shell': this.shell.open_shell,
70
+ 'close_shell': this.shell.close_shell,
71
+ 'enter_shell': this.shell.enter_shell,
72
+ 'resource_start': this.resource_start,
73
+ 'resource_end': this.resource_end,
74
+ 'ping': this.pong,
75
+ 'plugin_info': this.plugin_info,
76
+ 'plugin_download': this.plugin_download,
77
+ 'plugin_remove': this.plugin_remove,
78
+ };
79
+ if (h == undefined) {
80
+ this.messager_log('[Client Analysis] Analysis Failed, Value is undefined');
81
+ return 1;
82
+ }
83
+ if (h.message != undefined && h.message.length > 0) {
84
+ this.messager_log(`[Client Analysis] ${h.message}`);
85
+ }
86
+ if (h.data == undefined) {
87
+ this.messager_log('[Client Analysis] Analysis Warn, Data is undefined');
88
+ h.data = 0;
89
+ }
90
+ if (typeMap.hasOwnProperty(h.name)) {
91
+ const castingFunc = typeMap[h.name];
92
+ castingFunc(h.data, source, h.channel);
93
+ return 0;
94
+ }
95
+ else {
96
+ this.messager_log(`[Client Analysis] Analysis Failed, Unknowed header, name: ${h.name}, meta: ${h.meta}`);
97
+ return 2;
98
+ }
99
+ };
100
+ execute_job = (job, source, channel) => {
101
+ if (channel == undefined)
102
+ return;
103
+ const target = this.exec_checker(channel);
104
+ target.execute_job(job, source);
105
+ };
106
+ release = (dummy, source, channel) => {
107
+ if (channel == undefined)
108
+ return;
109
+ const index = this.exec.findIndex(x => x.uuid == channel);
110
+ if (index == -1)
111
+ return;
112
+ this.exec.splice(index, 1);
113
+ };
114
+ set_database = (data, source, channel) => {
115
+ if (channel == undefined)
116
+ return;
117
+ const target = this.exec_checker(channel);
118
+ target.set_database(data);
119
+ };
120
+ set_libs = (data, source, channel) => {
121
+ if (channel == undefined)
122
+ return;
123
+ const target = this.exec_checker(channel);
124
+ target.set_libs(data);
125
+ };
126
+ exec_checker = (uuid) => {
127
+ let r = undefined;
128
+ const index = this.exec.findIndex(x => x.uuid == uuid);
129
+ if (index == -1) {
130
+ r = new execute_1.ClientExecute(uuid, this.messager, this.messager_log, this.client);
131
+ this.exec.push(r);
132
+ }
133
+ else {
134
+ r = this.exec[index];
135
+ }
136
+ return r;
137
+ };
138
+ pong = (data, source) => {
139
+ const h = { name: 'pong', data: data };
140
+ source.send(JSON.stringify(h));
141
+ };
142
+ plugin_info = (data, source) => {
143
+ const pat = path.join(os.homedir(), interface_1.DATA_FOLDER, "plugin.json");
144
+ if ((0, fs_1.existsSync)(pat)) {
145
+ const p = JSON.parse((0, fs_1.readFileSync)(pat).toString());
146
+ const h = { name: 'plugin_info_reply', data: p.plugins };
147
+ source.send(JSON.stringify(h));
148
+ }
149
+ else {
150
+ const p = { plugins: [] };
151
+ const h = { name: 'plugin_info_reply', data: p.plugins };
152
+ (0, fs_1.writeFileSync)(pat, JSON.stringify(p));
153
+ source.send(JSON.stringify(h));
154
+ }
155
+ };
156
+ get_releases = async (repo, token) => {
157
+ const qu = await fetch(`https://api.github.com/repos/${repo}/releases`, {
158
+ headers: {
159
+ Authorization: token ? `token ${token}` : '',
160
+ Accept: "application/vnd.github.v3.raw",
161
+ }
162
+ });
163
+ return qu.text();
164
+ };
165
+ filterout = async (repo, token, version, filename) => {
166
+ const text = await this.get_releases(repo, token);
167
+ const json = JSON.parse(text);
168
+ const v = json.find(x => x.tag_name == version);
169
+ if (!v)
170
+ return;
171
+ const f = v.assets.find(x => x.name == filename);
172
+ if (!f)
173
+ return;
174
+ return f.id;
175
+ };
176
+ write_plugin = (t, plugin, source) => {
177
+ const list = this.client.plugins.plugins;
178
+ const index = list.findIndex(x => x.name == plugin.name);
179
+ plugin.token = t ? [t] : [];
180
+ plugin.progress = 0;
181
+ if (index == -1) {
182
+ list.push(plugin);
183
+ }
184
+ else {
185
+ list[index] = plugin;
186
+ }
187
+ this.client.savePlugin();
188
+ this.plugin_info(0, source);
189
+ };
190
+ finish_plugin = (plugin, source) => {
191
+ const list = this.client.plugins.plugins;
192
+ const index = list.findIndex(x => x.name == plugin.name);
193
+ plugin.progress = 1;
194
+ if (index == -1) {
195
+ list.push(plugin);
196
+ }
197
+ else {
198
+ list[index] = plugin;
199
+ }
200
+ this.client.savePlugin();
201
+ this.plugin_info(0, source);
202
+ };
203
+ plugin_download = async (plugin, source) => {
204
+ const target = plugin.contents.find(x => x.arch == process.arch && x.platform == process.platform);
205
+ if (target == undefined) {
206
+ this.messager_log(`[Plugin] Cannot find target plugin for ${plugin.name} on ${process.platform} ${process.arch}`);
207
+ return;
208
+ }
209
+ const links = target.url.split('/');
210
+ const filename = links[links.length - 1];
211
+ const version = links[links.length - 2];
212
+ const REPO = `${links[3]}/${links[4]}`;
213
+ const dir = path.join(os.homedir(), interface_1.DATA_FOLDER, "exe");
214
+ if (!(0, fs_1.existsSync)(dir))
215
+ (0, fs_1.mkdirSync)(dir, { recursive: true });
216
+ let req = {};
217
+ const tokens = [undefined, ...plugin.token];
218
+ const fileStream = (0, fs_1.createWriteStream)(path.join(dir, target.filename), { flags: 'a' });
219
+ let pass = false;
220
+ for (let t of tokens) {
221
+ if (pass)
222
+ break;
223
+ try {
224
+ const id = await this.filterout(REPO, t, version, filename);
225
+ req = {
226
+ method: 'GET',
227
+ credentials: 'include',
228
+ headers: {
229
+ Authorization: t ? `token ${t}` : '',
230
+ Accept: "application/octet-stream"
231
+ }
232
+ };
233
+ const url = `https://api.github.com/repos/${REPO}/releases/assets/${id}`;
234
+ fetch(url, req).then(async (res) => {
235
+ if (!res.ok) {
236
+ throw new Error(`Failed to download file: ${res.status} ${res.statusText}`);
237
+ }
238
+ this.write_plugin(t, plugin, source);
239
+ return res.blob();
240
+ }).then(blob => {
241
+ return blob.stream().getReader().read();
242
+ })
243
+ .then(reader => {
244
+ if (!reader.done) {
245
+ fileStream.write(Buffer.from(reader.value));
246
+ }
247
+ }).finally(() => {
248
+ this.messager_log(`[Plugin] Downloaded ${plugin.name} successfully`);
249
+ fileStream.end();
250
+ if (process.platform == 'linux') {
251
+ (0, child_process_1.exec)(`chmod +x ${path.join(dir, target.filename)}`, (err) => {
252
+ this.messager_log(`[Plugin] Permission failed ${err?.message}`);
253
+ });
254
+ }
255
+ this.finish_plugin(plugin, source);
256
+ pass = true;
257
+ });
258
+ }
259
+ catch (err) {
260
+ this.messager_log(`[Plugin] Download failed for ${plugin.name}: ${err.message}`);
261
+ }
262
+ }
263
+ };
264
+ plugin_remove = (plugin, source) => {
265
+ this.client.plugins.plugins = this.client.plugins.plugins.filter(x => x.name != plugin.name);
266
+ this.client.savePlugin();
267
+ const dir = path.join(os.homedir(), interface_1.DATA_FOLDER, "exe");
268
+ if (!(0, fs_1.existsSync)(dir))
269
+ (0, fs_1.mkdirSync)(dir, { recursive: true });
270
+ plugin.contents.forEach(x => {
271
+ if ((0, fs_1.existsSync)(path.join(dir, x.filename))) {
272
+ (0, fs_1.rmSync)(path.join(dir, x.filename));
273
+ }
274
+ });
275
+ this.plugin_info(0, source);
276
+ };
277
+ resource_start = (data, source) => {
278
+ this.resource_wanter.push(source);
279
+ this.messager_log(`Register resource_wanter!, count: ${this.resource_wanter.length}`);
280
+ if (this.resource_cache != undefined)
281
+ source.send(JSON.stringify(this.resource_cache));
282
+ };
283
+ resource_end = (data, source) => {
284
+ const index = this.resource_wanter.findIndex(x => x == source);
285
+ if (index != -1) {
286
+ this.resource_wanter.splice(index, 1);
287
+ this.messager_log(`UnRegister resource_wanter!, count: ${this.resource_wanter.length}`);
288
+ }
289
+ };
290
+ update = (client) => {
291
+ this.resource_require();
292
+ if (this.resource_cache != undefined) {
293
+ this.resource_wanter.forEach(x => x.send(JSON.stringify(this.resource_cache)));
294
+ }
295
+ };
296
+ disconnect = (source) => {
297
+ this.shell.disconnect(source);
298
+ this.exec.forEach(x => x.stop_job());
299
+ };
300
+ stop_all = () => {
301
+ this.exec.forEach(x => x.stop_job());
302
+ };
303
+ destroy = () => {
304
+ if (this.resource_thread != undefined)
305
+ this.resource_thread.kill();
306
+ };
307
+ resource_require = () => {
308
+ if (this.resource_thread != undefined)
309
+ return;
310
+ const shouldRun = this.resource_thread == undefined && (this.resource_cache == undefined || this.resource_wanter.length > 0);
311
+ if (!shouldRun)
312
+ return;
313
+ this.resource_thread = (0, child_process_1.spawn)(client_1.Client.workerPath(), [], {
314
+ stdio: ['inherit', 'pipe', 'pipe'],
315
+ shell: true,
316
+ windowsHide: true,
317
+ env: {
318
+ ...process.env,
319
+ type: "RESOURCE",
320
+ cache: this.resource_cache == undefined ? undefined : JSON.stringify(this.resource_cache.data)
321
+ }
322
+ });
323
+ let k = "";
324
+ const workerFeedbackExec = (str) => {
325
+ try {
326
+ const msg = JSON.parse(str);
327
+ if (msg.name == 'messager') {
328
+ this.messager(msg.data, "RESOURCE");
329
+ }
330
+ else if (msg.name == 'messager_log') {
331
+ this.messager_log(msg.data, "RESOURCE");
332
+ }
333
+ else if (msg.name == 'resource') {
334
+ const h = {
335
+ name: 'system_info',
336
+ data: msg.data
337
+ };
338
+ this.resource_cache = h;
339
+ this.resource_wanter.forEach(x => x.send(JSON.stringify(h)));
340
+ }
341
+ else if (msg.name == 'error') {
342
+ if (msg.data instanceof String)
343
+ this.messager_log(msg.data.toString(), "RESOURCE");
344
+ else
345
+ this.messager_log(JSON.stringify(msg.data), "RESOURCE");
346
+ }
347
+ }
348
+ catch (err) {
349
+ console.log("str: " + str);
350
+ console.log(err.name + "\n" + err.message);
351
+ }
352
+ };
353
+ const workerFeedback = (str) => {
354
+ for (let i = 0; i < str.length; i++) {
355
+ if (str[i] != '\n')
356
+ k += str[i];
357
+ else {
358
+ workerFeedbackExec(k);
359
+ k = '';
360
+ }
361
+ }
362
+ };
363
+ this.resource_thread.on('error', (err) => {
364
+ this.messager_log(`[Worker Error] ${err}`);
365
+ });
366
+ this.resource_thread.on('exit', (code, signal) => {
367
+ this.resource_thread = undefined;
368
+ });
369
+ this.resource_thread.on('message', (message, sendHandle) => {
370
+ workerFeedback(message.toString());
371
+ });
372
+ this.resource_thread.stdout?.setEncoding('utf8');
373
+ this.resource_thread.stdout?.on('data', (chunk) => {
374
+ workerFeedback(chunk.toString());
375
+ });
376
+ this.resource_thread.stderr?.setEncoding('utf8');
377
+ this.resource_thread.stderr?.on('data', (chunk) => {
378
+ workerFeedback(chunk.toString());
379
+ });
380
+ };
381
+ }
382
+ exports.ClientAnalysis = ClientAnalysis;
@@ -0,0 +1,25 @@
1
+ import { WebSocket } from 'ws';
2
+ import { Messager, Messager_log, PluginList } from '../interface';
3
+ export declare class Client {
4
+ plugins: PluginList;
5
+ private httpss;
6
+ private client;
7
+ private sources;
8
+ private messager;
9
+ private messager_log;
10
+ private analysis;
11
+ private updatehandle;
12
+ get count(): number;
13
+ get clients(): Array<WebSocket>;
14
+ constructor(_messager: Messager, _messager_log: Messager_log);
15
+ Dispose(): void;
16
+ Init: () => Promise<void>;
17
+ Destroy: () => void;
18
+ Release: () => void;
19
+ savePlugin: () => void;
20
+ private update;
21
+ private loadPlugins;
22
+ private get_pem;
23
+ static workerPath: (filename?: string, extension?: string) => string;
24
+ static isTypescript: () => boolean;
25
+ }
@@ -0,0 +1,227 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.Client = void 0;
37
+ const path = __importStar(require("path"));
38
+ const tcp_port_used_1 = require("tcp-port-used");
39
+ const ws = __importStar(require("ws"));
40
+ const interface_1 = require("../interface");
41
+ const analysis_1 = require("./analysis");
42
+ const fs_1 = require("fs");
43
+ const os = __importStar(require("os"));
44
+ const pem = __importStar(require("pem"));
45
+ const https = __importStar(require("https"));
46
+ class Client {
47
+ plugins = { plugins: [] };
48
+ httpss = undefined;
49
+ client = undefined;
50
+ sources = [];
51
+ messager;
52
+ messager_log;
53
+ analysis;
54
+ updatehandle;
55
+ get count() {
56
+ return this.sources.length;
57
+ }
58
+ get clients() {
59
+ return this.sources;
60
+ }
61
+ constructor(_messager, _messager_log) {
62
+ this.messager = _messager;
63
+ this.messager_log = _messager_log;
64
+ this.analysis = [];
65
+ this.updatehandle = setInterval(this.update, interface_1.CLIENT_UPDATETICK);
66
+ this.loadPlugins();
67
+ }
68
+ Dispose() {
69
+ clearInterval(this.updatehandle);
70
+ }
71
+ Init = async () => {
72
+ let port_result = interface_1.PORT;
73
+ let canbeuse = false;
74
+ while (!canbeuse) {
75
+ await (0, tcp_port_used_1.check)(port_result).then(x => {
76
+ canbeuse = !x;
77
+ }).catch(err => {
78
+ canbeuse = true;
79
+ });
80
+ if (!canbeuse)
81
+ port_result += 1;
82
+ }
83
+ const pems = await this.get_pem();
84
+ this.httpss = https.createServer({ key: pems[0], cert: pems[1], minVersion: 'TLSv1.2', maxVersion: 'TLSv1.3' }, (req, res) => {
85
+ res.writeHead(200);
86
+ res.end('HTTPS server is running');
87
+ });
88
+ this.httpss.addListener('upgrade', (req, res, head) => console.log('UPGRADE:', req.url));
89
+ this.client = new ws.Server({ server: this.httpss });
90
+ this.client.on('listening', () => {
91
+ this.messager_log('[Server] Listen PORT: ' + port_result.toString());
92
+ });
93
+ this.client.on('error', (err) => {
94
+ this.messager_log(`[Server] Error ${err.name}\n\t${err.message}\n\t${err.stack}`);
95
+ });
96
+ this.client.on('close', () => {
97
+ this.messager_log('[Server] Close !');
98
+ this.Release();
99
+ });
100
+ this.client.on('connection', (ws, request) => {
101
+ const a = new analysis_1.ClientAnalysis(this.messager, this.messager_log, this);
102
+ this.analysis.push(a);
103
+ this.sources.push(ws);
104
+ this.messager_log(`[Server] New Connection detected, ${ws.url}`);
105
+ ws.on('close', (code, reason) => {
106
+ const index = this.sources.findIndex(x => x == ws);
107
+ if (index != -1)
108
+ this.sources.splice(index, 1);
109
+ this.messager_log(`[Source] Close ${code} ${reason}`);
110
+ a.disconnect(ws);
111
+ });
112
+ ws.on('error', (err) => {
113
+ this.messager_log(`[Source] Error ${err.name}\n\t${err.message}\n\t${err.stack}`);
114
+ });
115
+ ws.on('open', () => {
116
+ this.messager_log(`[Source] New source is connected, URL: ${ws?.url}`);
117
+ });
118
+ ws.on('message', (data, isBinery) => {
119
+ const h = JSON.parse(data.toString());
120
+ a.analysis(h, ws);
121
+ });
122
+ });
123
+ this.httpss.listen(port_result, () => {
124
+ this.messager_log('[Server] Select Port: ' + port_result.toString());
125
+ });
126
+ };
127
+ Destroy = () => {
128
+ if (this.client == undefined)
129
+ return;
130
+ this.client.close((err) => {
131
+ this.messager_log(`[Client] Close error ${err}`);
132
+ });
133
+ this.Release();
134
+ };
135
+ Release = () => {
136
+ this.analysis.forEach(x => x.stop_all());
137
+ this.analysis.forEach(x => x.destroy());
138
+ this.analysis = [];
139
+ };
140
+ savePlugin = () => {
141
+ const f = path.join(os.homedir(), interface_1.DATA_FOLDER);
142
+ const pluginPath = path.join(f, 'plugin.json');
143
+ if (!(0, fs_1.existsSync)(f))
144
+ (0, fs_1.mkdirSync)(f, { recursive: true });
145
+ (0, fs_1.writeFileSync)(pluginPath, JSON.stringify(this.plugins, null, 4));
146
+ };
147
+ update = () => {
148
+ this.analysis.forEach(x => x.update(this));
149
+ };
150
+ loadPlugins = () => {
151
+ const f = path.join(os.homedir(), interface_1.DATA_FOLDER);
152
+ const pluginPath = path.join(f, 'plugin.json');
153
+ if (!(0, fs_1.existsSync)(f))
154
+ (0, fs_1.mkdirSync)(f, { recursive: true });
155
+ if (!(0, fs_1.existsSync)(pluginPath)) {
156
+ (0, fs_1.writeFileSync)(pluginPath, JSON.stringify(this.plugins, null, 4));
157
+ }
158
+ else {
159
+ this.plugins = JSON.parse((0, fs_1.readFileSync)(pluginPath).toString());
160
+ }
161
+ };
162
+ get_pem = () => {
163
+ return new Promise((resolve) => {
164
+ const pemFolder = path.join(os.homedir(), interface_1.DATA_FOLDER, 'pem');
165
+ if (!(0, fs_1.existsSync)(pemFolder))
166
+ (0, fs_1.mkdirSync)(pemFolder);
167
+ const clientKey = path.join(pemFolder, "client_clientkey.pem");
168
+ const certificate = path.join(pemFolder, "client_certificate.pem");
169
+ if (!(0, fs_1.existsSync)(clientKey) || !(0, fs_1.existsSync)(certificate)) {
170
+ pem.createCertificate({ selfSigned: true }, (err, keys) => {
171
+ (0, fs_1.writeFileSync)(clientKey, keys.clientKey, { encoding: 'utf8' });
172
+ (0, fs_1.writeFileSync)(certificate, keys.certificate, { encoding: 'utf8' });
173
+ resolve([keys.clientKey, keys.certificate]);
174
+ });
175
+ }
176
+ else {
177
+ resolve([(0, fs_1.readFileSync)(clientKey, 'utf8').toString(), (0, fs_1.readFileSync)(certificate, 'utf8').toString()]);
178
+ }
179
+ });
180
+ };
181
+ static workerPath = (filename = "worker", extension = ".exe") => {
182
+ const isExe = process.pkg?.entrypoint != undefined;
183
+ const exe = process.platform == 'win32' ? filename + extension : filename;
184
+ let workerExe = "";
185
+ let p = 0;
186
+ if (isExe && path.basename(process.execPath) == (process.platform ? "app.exe" : 'app')) {
187
+ workerExe = path.join(process.execPath, "..", "bin", exe);
188
+ p = 1;
189
+ }
190
+ else if ((process.mainModule && process.mainModule.filename.indexOf('app.asar') !== -1) ||
191
+ process.argv.filter(a => a.indexOf('app.asar') !== -1).length > 0) {
192
+ workerExe = path.join("bin", exe);
193
+ p = 2;
194
+ }
195
+ else if (process.env.NODE_ENV === 'development') {
196
+ workerExe = path.join(process.cwd(), "bin", exe);
197
+ p = 3;
198
+ }
199
+ else {
200
+ workerExe = Client.isTypescript() ? path.join(__dirname, "bin", exe) : path.join(__dirname, "..", "bin", exe);
201
+ p = 4;
202
+ }
203
+ return workerExe;
204
+ };
205
+ static isTypescript = () => {
206
+ const extension = path.extname(__filename);
207
+ if (extension === ".ts") {
208
+ return true;
209
+ }
210
+ const lastArg = process.execArgv[process.execArgv.length - 1];
211
+ if (lastArg && path.parse(lastArg).name.indexOf("ts-node") > 0) {
212
+ return true;
213
+ }
214
+ try {
215
+ return process[Symbol.for("ts-node.register.instance")] ||
216
+ (process.env.NODE_ENV === "test" &&
217
+ process.env.ACTIONHERO_TEST_FILE_EXTENSION !== "js")
218
+ ? true
219
+ : false;
220
+ }
221
+ catch (error) {
222
+ console.error(error);
223
+ return false;
224
+ }
225
+ };
226
+ }
227
+ exports.Client = Client;
@@ -0,0 +1 @@
1
+ export declare function RUN(): void;
@@ -33,11 +33,10 @@ const messager_log = (msg, tag, meta) => {
33
33
  console.log(JSON.stringify(d));
34
34
  };
35
35
  const ERROR = (err) => {
36
- var _a;
37
36
  const d = {
38
37
  name: "error",
39
38
  meta: "Execute job failed",
40
- data: `(${(_a = err.code) !== null && _a !== void 0 ? _a : 'unknown'}) ${err.message}`,
39
+ data: `(${err.code ?? 'unknown'}) ${err.message}`,
41
40
  };
42
41
  console.log(JSON.stringify(d));
43
42
  process.exit(1);