@maiyunnet/kebab 2.0.0

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 (114) hide show
  1. package/.VSCodeCounter/2025-02-14_14-46-44/details.md +82 -0
  2. package/.VSCodeCounter/2025-02-14_14-46-44/diff-details.md +15 -0
  3. package/.VSCodeCounter/2025-02-14_14-46-44/diff.csv +2 -0
  4. package/.VSCodeCounter/2025-02-14_14-46-44/diff.md +19 -0
  5. package/.VSCodeCounter/2025-02-14_14-46-44/diff.txt +22 -0
  6. package/.VSCodeCounter/2025-02-14_14-46-44/results.csv +69 -0
  7. package/.VSCodeCounter/2025-02-14_14-46-44/results.json +1 -0
  8. package/.VSCodeCounter/2025-02-14_14-46-44/results.md +48 -0
  9. package/.VSCodeCounter/2025-02-14_14-46-44/results.txt +118 -0
  10. package/.vscode/tasks.json +15 -0
  11. package/LICENSE +201 -0
  12. package/README.md +201 -0
  13. package/bin/kebab.js +2 -0
  14. package/eslint.config.js +22 -0
  15. package/index.js +19 -0
  16. package/index.ts +33 -0
  17. package/lib/buffer.js +108 -0
  18. package/lib/buffer.ts +152 -0
  19. package/lib/captcha/zcool-addict-italic.ttf +0 -0
  20. package/lib/captcha.js +71 -0
  21. package/lib/captcha.ts +63 -0
  22. package/lib/consistent.js +171 -0
  23. package/lib/consistent.ts +219 -0
  24. package/lib/core.js +663 -0
  25. package/lib/core.ts +880 -0
  26. package/lib/crypto.js +256 -0
  27. package/lib/crypto.ts +384 -0
  28. package/lib/db.js +521 -0
  29. package/lib/db.ts +719 -0
  30. package/lib/dns.js +321 -0
  31. package/lib/dns.ts +405 -0
  32. package/lib/fs.js +405 -0
  33. package/lib/fs.ts +527 -0
  34. package/lib/jwt.js +223 -0
  35. package/lib/jwt.ts +276 -0
  36. package/lib/kv.js +1004 -0
  37. package/lib/kv.ts +1489 -0
  38. package/lib/lan.js +99 -0
  39. package/lib/lan.ts +87 -0
  40. package/lib/net/cacert.pem +3480 -0
  41. package/lib/net/formdata.js +137 -0
  42. package/lib/net/formdata.ts +166 -0
  43. package/lib/net/request.js +102 -0
  44. package/lib/net/request.ts +150 -0
  45. package/lib/net/response.js +28 -0
  46. package/lib/net/response.ts +59 -0
  47. package/lib/net.js +462 -0
  48. package/lib/net.ts +662 -0
  49. package/lib/s3.js +180 -0
  50. package/lib/s3.ts +235 -0
  51. package/lib/scan.js +276 -0
  52. package/lib/scan.ts +364 -0
  53. package/lib/session.js +177 -0
  54. package/lib/session.ts +230 -0
  55. package/lib/sql.js +818 -0
  56. package/lib/sql.ts +1151 -0
  57. package/lib/ssh/sftp.js +373 -0
  58. package/lib/ssh/sftp.ts +508 -0
  59. package/lib/ssh/shell.js +109 -0
  60. package/lib/ssh/shell.ts +123 -0
  61. package/lib/ssh.js +171 -0
  62. package/lib/ssh.ts +191 -0
  63. package/lib/text/tld.json +1 -0
  64. package/lib/text.js +452 -0
  65. package/lib/text.ts +607 -0
  66. package/lib/time.js +216 -0
  67. package/lib/time.ts +254 -0
  68. package/lib/ws.js +373 -0
  69. package/lib/ws.ts +523 -0
  70. package/lib/zip.js +381 -0
  71. package/lib/zip.ts +447 -0
  72. package/lib/zlib.js +289 -0
  73. package/lib/zlib.ts +350 -0
  74. package/main.js +51 -0
  75. package/main.ts +27 -0
  76. package/package.json +37 -0
  77. package/sys/child.js +585 -0
  78. package/sys/child.ts +678 -0
  79. package/sys/cmd.js +226 -0
  80. package/sys/cmd.ts +225 -0
  81. package/sys/ctr.js +608 -0
  82. package/sys/ctr.ts +904 -0
  83. package/sys/master.js +314 -0
  84. package/sys/master.ts +355 -0
  85. package/sys/mod.js +1273 -0
  86. package/sys/mod.ts +1871 -0
  87. package/sys/route.js +922 -0
  88. package/sys/route.ts +1113 -0
  89. package/types/index.d.ts +283 -0
  90. package/www/example/ctr/main.js +42 -0
  91. package/www/example/ctr/main.ts +9 -0
  92. package/www/example/ctr/middle.js +57 -0
  93. package/www/example/ctr/middle.ts +26 -0
  94. package/www/example/ctr/test.js +2818 -0
  95. package/www/example/ctr/test.ts +3218 -0
  96. package/www/example/data/locale/en.test.json +8 -0
  97. package/www/example/data/locale/index.html +1 -0
  98. package/www/example/data/locale/ja.test.json +8 -0
  99. package/www/example/data/locale/sc.test.json +8 -0
  100. package/www/example/data/locale/tc.test.json +8 -0
  101. package/www/example/data/test.zip +0 -0
  102. package/www/example/kebab.json +24 -0
  103. package/www/example/mod/test.js +49 -0
  104. package/www/example/mod/test.ts +47 -0
  105. package/www/example/mod/testdata.js +11 -0
  106. package/www/example/mod/testdata.ts +30 -0
  107. package/www/example/route.json +6 -0
  108. package/www/example/view/test.ejs +11 -0
  109. package/www/example/ws/mproxy.js +49 -0
  110. package/www/example/ws/mproxy.ts +16 -0
  111. package/www/example/ws/rproxy.js +47 -0
  112. package/www/example/ws/rproxy.ts +14 -0
  113. package/www/example/ws/test.js +68 -0
  114. package/www/example/ws/test.ts +36 -0
package/lib/core.js ADDED
@@ -0,0 +1,663 @@
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.global = exports.REAL_IP_CF = exports.REAL_IP_X = exports.CONVERT62_CHAR = exports.RANDOM_LUNS = exports.RANDOM_V = exports.RANDOM_LUN = exports.RANDOM_LU = exports.RANDOM_LN = exports.RANDOM_UN = exports.RANDOM_L = exports.RANDOM_U = exports.RANDOM_N = exports.globalConfig = void 0;
37
+ exports.setCookie = setCookie;
38
+ exports.rand = rand;
39
+ exports.random = random;
40
+ exports.convert62 = convert62;
41
+ exports.unconvert62 = unconvert62;
42
+ exports.purify = purify;
43
+ exports.checkType = checkType;
44
+ exports.muid = muid;
45
+ exports.ip = ip;
46
+ exports.realIP = realIP;
47
+ exports.sleep = sleep;
48
+ exports.objectSort = objectSort;
49
+ exports.emptyObject = emptyObject;
50
+ exports.passThroughAppend = passThroughAppend;
51
+ exports.exec = exec;
52
+ exports.sendReload = sendReload;
53
+ exports.sendRestart = sendRestart;
54
+ exports.setGlobal = setGlobal;
55
+ exports.removeGlobal = removeGlobal;
56
+ exports.updateCode = updateCode;
57
+ exports.log = log;
58
+ exports.getLog = getLog;
59
+ exports.clone = clone;
60
+ exports.debug = debug;
61
+ exports.display = display;
62
+ const cp = __importStar(require("child_process"));
63
+ const stream = __importStar(require("stream"));
64
+ const lTime = __importStar(require("~/lib/time"));
65
+ const lFs = __importStar(require("~/lib/fs"));
66
+ const lText = __importStar(require("~/lib/text"));
67
+ const lNet = __importStar(require("~/lib/net"));
68
+ const lCrypto = __importStar(require("~/lib/crypto"));
69
+ const lResponse = __importStar(require("~/lib/net/response"));
70
+ const sCtr = __importStar(require("~/sys/ctr"));
71
+ const kebab = __importStar(require("~/index"));
72
+ exports.globalConfig = {};
73
+ function setCookie(ctr, name, value, opt = {}) {
74
+ const res = ctr.getPrototype('_res');
75
+ if (!res) {
76
+ return;
77
+ }
78
+ const maxAge = opt.ttl === undefined ? '' : `; Max-Age=${opt.ttl}`;
79
+ const path = `; path=${opt.path ?? '/'}`;
80
+ const domain = opt.domain ? `; domain=${opt.domain}` : '';
81
+ const secure = opt.ssl ? '; secure' : '';
82
+ const httpOnly = opt.httponly ? '; HttpOnly' : '';
83
+ const sameSite = opt.samesite ? '; SameSite=' + opt.samesite : '';
84
+ const cookies = res.getHeader('set-cookie') ?? [];
85
+ cookies.push(`${name}=${encodeURIComponent(value)}${maxAge}${path}${domain}${secure}${httpOnly}${sameSite}`);
86
+ res.setHeader('set-cookie', cookies);
87
+ }
88
+ function rand(min, max, prec = 0) {
89
+ if (prec < 0) {
90
+ prec = 0;
91
+ }
92
+ const p = Math.pow(10, prec);
93
+ min = min * p;
94
+ max = max * p;
95
+ return Math.round(Math.random() * (max - min) + min) / p;
96
+ }
97
+ exports.RANDOM_N = '0123456789';
98
+ exports.RANDOM_U = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
99
+ exports.RANDOM_L = 'abcdefghijklmnopqrstuvwxyz';
100
+ exports.RANDOM_UN = exports.RANDOM_U + exports.RANDOM_N;
101
+ exports.RANDOM_LN = exports.RANDOM_L + exports.RANDOM_N;
102
+ exports.RANDOM_LU = exports.RANDOM_L + exports.RANDOM_U;
103
+ exports.RANDOM_LUN = exports.RANDOM_L + exports.RANDOM_U + exports.RANDOM_N;
104
+ exports.RANDOM_V = 'ACEFGHJKLMNPRSTWXY34567';
105
+ exports.RANDOM_LUNS = exports.RANDOM_LUN + '()`~!@#$%^&*-+=_|{}[]:;"<>,.?/]"';
106
+ function random(length = 8, source = exports.RANDOM_LN, block = '') {
107
+ let len = block.length;
108
+ if (len > 0) {
109
+ for (let i = 0; i < len; ++i) {
110
+ source = source.replace(block[i], '');
111
+ }
112
+ }
113
+ len = source.length;
114
+ if (len === 0) {
115
+ return '';
116
+ }
117
+ let temp = '';
118
+ for (let i = 0; i < length; ++i) {
119
+ temp += source[rand(0, len - 1)];
120
+ }
121
+ return temp;
122
+ }
123
+ exports.CONVERT62_CHAR = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
124
+ function convert62(n) {
125
+ if (typeof n !== 'bigint') {
126
+ n = BigInt(n);
127
+ }
128
+ let res = '';
129
+ while (n > 0) {
130
+ res = exports.CONVERT62_CHAR[Number(n % 62n)] + res;
131
+ n = n / 62n;
132
+ }
133
+ return res;
134
+ }
135
+ function unconvert62(n) {
136
+ let res = 0n;
137
+ const nl = n.length;
138
+ for (let i = 1; i <= nl; ++i) {
139
+ res += BigInt(exports.CONVERT62_CHAR.indexOf(n[i - 1])) * (62n ** BigInt(nl - i));
140
+ }
141
+ return res;
142
+ }
143
+ function purify(text) {
144
+ text = '>' + text + '<';
145
+ const keepScripts = [];
146
+ const keepPres = [];
147
+ let nums = -1;
148
+ let nump = -1;
149
+ text = text.replace(/<!--([\s\S]*?)-->/g, '').replace(/<script[\s\S]+?<\/script>/g, function (t) {
150
+ keepScripts.push(t);
151
+ return '[SCRIPT]';
152
+ }).replace(/<pre[\s\S]+?<\/pre>/g, function (t) {
153
+ keepPres.push(t);
154
+ return '[PRE]';
155
+ }).replace(/>([\s\S]*?)</g, function (t, t1) {
156
+ return '>' + t1.replace(/\t|\r\n| {2}/g, '').replace(/\n|\r/g, '') + '<';
157
+ }).replace(/\[SCRIPT\]/g, function () {
158
+ ++nums;
159
+ return keepScripts[nums];
160
+ }).replace(/\[PRE\]/g, function () {
161
+ ++nump;
162
+ return keepPres[nump];
163
+ });
164
+ return text.slice(1, -1);
165
+ }
166
+ function checkType(val, type, tree = 'root') {
167
+ const vtype = typeof val;
168
+ if (Array.isArray(type)) {
169
+ if (!Array.isArray(val)) {
170
+ return 'array:' + tree + ':' + vtype;
171
+ }
172
+ for (let i = 0; i < val.length; ++i) {
173
+ const res = checkType(val[i], type[0], tree + '.' + i.toString());
174
+ if (res) {
175
+ return res;
176
+ }
177
+ }
178
+ return '';
179
+ }
180
+ const ttype = typeof type;
181
+ if (type instanceof RegExp) {
182
+ if (vtype !== 'string') {
183
+ return 'regexp:' + tree + ':' + vtype;
184
+ }
185
+ return type.test(val) ? '' : 'regexp:' + tree + ':' + vtype;
186
+ }
187
+ if (ttype === 'string') {
188
+ if (vtype !== 'string' && val !== undefined && val !== null) {
189
+ return 'string:' + tree + ':' + vtype;
190
+ }
191
+ if (type) {
192
+ return val ? '' : 'require:' + tree + ':' + vtype;
193
+ }
194
+ return '';
195
+ }
196
+ if (val === undefined || val === null) {
197
+ return ttype + ':' + tree + ':' + (val === undefined ? 'undefined' : 'null');
198
+ }
199
+ if (ttype === 'object') {
200
+ if (vtype !== 'object') {
201
+ return 'object:' + tree + ':' + vtype;
202
+ }
203
+ if (Array.isArray(val)) {
204
+ return 'object:' + tree + ':array';
205
+ }
206
+ if (!Object.keys(type).length) {
207
+ return '';
208
+ }
209
+ for (const key in type) {
210
+ const res = checkType(val[key], type[key], tree + '.' + key);
211
+ if (res) {
212
+ return res;
213
+ }
214
+ }
215
+ for (const key in val) {
216
+ if (type[key] !== undefined) {
217
+ continue;
218
+ }
219
+ return `undefined:${tree}.${key}:${typeof val[key]}`;
220
+ }
221
+ return '';
222
+ }
223
+ return vtype === ttype ? '' : ttype + ':' + tree + ':' + vtype;
224
+ }
225
+ function muid(ctr, opt = {}) {
226
+ const len = opt.len ?? 8;
227
+ const bin = opt.bin ?? true;
228
+ const key = opt.key ?? '';
229
+ const insert = opt.insert ?? '';
230
+ const ilen = insert.length;
231
+ const num = opt.num ?? true;
232
+ const headers = ctr.getPrototype('_headers');
233
+ const req = ctr.getPrototype('_req');
234
+ const char = lCrypto.hashHmac('sha1', (headers['user-agent'] ?? '') +
235
+ (headers['referer'] ?? '') +
236
+ (headers['accept-language'] ?? '') +
237
+ (req.socket.remoteAddress ?? '') +
238
+ (headers['x-forwarded-for'] ?? '') +
239
+ (headers['cf-connecting-ip'] ?? '') + 'muid' + key + rand(0, 1000000000).toString(), 'muid');
240
+ if (!char) {
241
+ return '';
242
+ }
243
+ const over = random(len - 1 - ilen, bin ? (num ? exports.RANDOM_LUN : exports.RANDOM_LU) : (num ? exports.RANDOM_LN : exports.RANDOM_L)) + char[20];
244
+ return over[0] + insert + over.slice(1);
245
+ }
246
+ function ip(ctr, req) {
247
+ const headers = ctr instanceof sCtr.Ctr ? ctr.getPrototype('_headers') : ctr;
248
+ if (typeof headers['cf-connecting-ip'] === 'string') {
249
+ return headers['cf-connecting-ip'];
250
+ }
251
+ else if (typeof headers['x-forwarded-for'] === 'string') {
252
+ return headers['x-forwarded-for'];
253
+ }
254
+ else {
255
+ if (!req) {
256
+ if (ctr instanceof sCtr.Ctr) {
257
+ req = ctr.getPrototype('_req');
258
+ }
259
+ else {
260
+ return '';
261
+ }
262
+ }
263
+ return req.socket.remoteAddress ?? '';
264
+ }
265
+ }
266
+ exports.REAL_IP_X = 'x-forwarded-for';
267
+ exports.REAL_IP_CF = 'cf-connecting-ip';
268
+ function realIP(ctr, name = '') {
269
+ const headers = ctr.getPrototype('_headers');
270
+ if (name !== '') {
271
+ const value = headers[name];
272
+ if (typeof value === 'string') {
273
+ return value;
274
+ }
275
+ }
276
+ const req = ctr.getPrototype('_req');
277
+ return req.socket.remoteAddress ?? '';
278
+ }
279
+ function sleep(ms) {
280
+ return new Promise(function (resolve) {
281
+ setTimeout(function () {
282
+ resolve();
283
+ }, ms);
284
+ });
285
+ }
286
+ function objectSort(o) {
287
+ const ordered = {};
288
+ const list = Object.keys(o).sort();
289
+ for (const key of list) {
290
+ if ((typeof o[key] === 'object') && (!Array.isArray(o[key]))) {
291
+ ordered[key] = objectSort(o[key]);
292
+ }
293
+ else {
294
+ ordered[key] = o[key];
295
+ }
296
+ }
297
+ return ordered;
298
+ }
299
+ function emptyObject(obj, deep = false) {
300
+ const keys = Object.keys(obj);
301
+ for (const key of keys) {
302
+ if (deep) {
303
+ const value = obj[key];
304
+ if (typeof value === 'object') {
305
+ emptyObject(value);
306
+ }
307
+ }
308
+ delete obj[key];
309
+ }
310
+ }
311
+ async function passThroughAppend(passThrough, data, end = true) {
312
+ for (const item of data) {
313
+ if (item instanceof stream.Readable || item instanceof lResponse.Response) {
314
+ const stm = item instanceof stream.Readable ? item : item.getStream();
315
+ stm.pipe(passThrough, {
316
+ 'end': false
317
+ });
318
+ await new Promise((resolve) => {
319
+ stm.on('end', () => {
320
+ resolve();
321
+ });
322
+ });
323
+ }
324
+ else {
325
+ await new Promise((resolve) => {
326
+ passThrough.write(item, () => {
327
+ resolve();
328
+ });
329
+ });
330
+ }
331
+ }
332
+ if (end) {
333
+ passThrough.end();
334
+ }
335
+ }
336
+ function exec(command) {
337
+ return new Promise(function (resolve) {
338
+ cp.exec(command, function (err, stdout) {
339
+ if (err) {
340
+ resolve(false);
341
+ return;
342
+ }
343
+ resolve(stdout);
344
+ });
345
+ });
346
+ }
347
+ async function sendReload(hosts) {
348
+ if (!hosts) {
349
+ console.log('[ Child] Sending reload request...');
350
+ process.send({
351
+ 'action': 'reload'
352
+ });
353
+ return [];
354
+ }
355
+ const time = lTime.stamp();
356
+ const rtn = [];
357
+ for (const host of hosts) {
358
+ const res = await lNet.get('http://' + host + ':' + exports.globalConfig.rpcPort.toString() + '/' + lCrypto.aesEncrypt(lText.stringifyJson({
359
+ 'action': 'reload',
360
+ 'time': time
361
+ }), exports.globalConfig.rpcSecret), {
362
+ 'timeout': 2
363
+ });
364
+ const content = await res.getContent();
365
+ if (!content) {
366
+ continue;
367
+ }
368
+ const str = content.toString();
369
+ if (str === 'Done') {
370
+ rtn.push(host);
371
+ }
372
+ }
373
+ return rtn;
374
+ }
375
+ async function sendRestart(hosts) {
376
+ if (!hosts) {
377
+ console.log('[ Child] Sending restart request...');
378
+ process.send({
379
+ 'action': 'restart'
380
+ });
381
+ return [];
382
+ }
383
+ const time = lTime.stamp();
384
+ const rtn = [];
385
+ for (const host of hosts) {
386
+ const res = await lNet.get('http://' + host + ':' + exports.globalConfig.rpcPort.toString() + '/' + lCrypto.aesEncrypt(lText.stringifyJson({
387
+ 'action': 'restart',
388
+ 'time': time
389
+ }), exports.globalConfig.rpcSecret), {
390
+ 'timeout': 2
391
+ });
392
+ const content = await res.getContent();
393
+ if (!content) {
394
+ continue;
395
+ }
396
+ const str = content.toString();
397
+ if (str === 'Done') {
398
+ rtn.push(host);
399
+ }
400
+ }
401
+ return rtn;
402
+ }
403
+ exports.global = {};
404
+ async function setGlobal(key, data, hosts) {
405
+ if (!hosts) {
406
+ process.send({
407
+ 'action': 'global',
408
+ 'key': key,
409
+ 'data': data
410
+ });
411
+ return [];
412
+ }
413
+ const time = lTime.stamp();
414
+ const rtn = [];
415
+ for (const host of hosts) {
416
+ const res = await lNet.get('http://' + host + ':' + exports.globalConfig.rpcPort.toString() + '/' + lCrypto.aesEncrypt(lText.stringifyJson({
417
+ 'action': 'global',
418
+ 'time': time
419
+ }), exports.globalConfig.rpcSecret), {
420
+ 'timeout': 2
421
+ });
422
+ const content = await res.getContent();
423
+ if (!content) {
424
+ continue;
425
+ }
426
+ const str = content.toString();
427
+ if (str === 'Done') {
428
+ rtn.push(host);
429
+ }
430
+ }
431
+ return rtn;
432
+ }
433
+ async function removeGlobal(key, hosts) {
434
+ return setGlobal(key, null, hosts);
435
+ }
436
+ async function updateCode(sourcePath, path, hosts, config = true) {
437
+ hosts ??= ['127.0.0.1'];
438
+ const rtn = {};
439
+ for (const host of hosts) {
440
+ const fd = lNet.getFormData();
441
+ if (!await fd.putFile('file', sourcePath)) {
442
+ continue;
443
+ }
444
+ fd.putString('path', path);
445
+ fd.putString('config', config ? '1' : '0');
446
+ const res = await lNet.post('http://' + host + ':' + exports.globalConfig.rpcPort.toString() + '/' + lCrypto.aesEncrypt(lText.stringifyJson({
447
+ 'action': 'code',
448
+ 'time': lTime.stamp()
449
+ }), exports.globalConfig.rpcSecret), fd, {
450
+ 'timeout': 4
451
+ });
452
+ const content = await res.getContent();
453
+ if (!content) {
454
+ rtn[host] = {
455
+ 'result': false,
456
+ 'return': 'Network error'
457
+ };
458
+ continue;
459
+ }
460
+ const str = content.toString();
461
+ rtn[host] = {
462
+ 'result': str === 'Done' ? true : false,
463
+ 'return': str
464
+ };
465
+ }
466
+ return rtn;
467
+ }
468
+ async function log(opt, msg, fend = '') {
469
+ let req;
470
+ let headers;
471
+ let get;
472
+ let cookie;
473
+ let wpath;
474
+ let urlFull;
475
+ let hostname;
476
+ if (opt instanceof sCtr.Ctr) {
477
+ req = opt.getPrototype('_req');
478
+ headers = opt.getPrototype('_headers');
479
+ get = opt.getPrototype('_get');
480
+ cookie = opt.getPrototype('_cookie');
481
+ const config = opt.getPrototype('_config');
482
+ wpath = config.const.path;
483
+ urlFull = config.const.urlFull;
484
+ hostname = config.const.hostname;
485
+ }
486
+ else {
487
+ req = opt.req;
488
+ headers = opt.headers;
489
+ get = opt.get;
490
+ cookie = opt.cookie;
491
+ wpath = opt.path;
492
+ urlFull = opt.urlFull;
493
+ hostname = opt.hostname;
494
+ }
495
+ if (hostname === '') {
496
+ hostname = 'system';
497
+ }
498
+ const realIp = req?.socket.remoteAddress ?? '';
499
+ const clientIp = req ? ip(headers, req) : '';
500
+ const [y, m, d, h] = lTime.format(null, 'Y-m-d-H').split('-');
501
+ let path = kebab.LOG_CWD + hostname + fend + '/' + y + '/' + m + '/' + d + '/';
502
+ const rtn = await lFs.mkdir(path, 0o777);
503
+ if (!rtn) {
504
+ return;
505
+ }
506
+ path += h + '.csv';
507
+ if (!await lFs.isFile(path)) {
508
+ if (!await lFs.putContent(path, 'TIME,UNIX,URL,COOKIE,USER_AGENT,REALIP,CLIENTIP,MESSAGE\n', {
509
+ 'encoding': 'utf8',
510
+ 'mode': 0o777
511
+ })) {
512
+ return;
513
+ }
514
+ }
515
+ await lFs.putContent(path, '"' +
516
+ lTime.format(null, 'H:i:s') + '","' +
517
+ lTime.stamp().toString() + '","' +
518
+ urlFull + wpath + (Object.keys(get).length ? '?' + lText.queryStringify(get).replace(/"/g, '""') : '') + '","' +
519
+ lText.queryStringify(cookie).replace(/"/g, '""') + '","' +
520
+ (headers['user-agent']?.replace(/"/g, '""') ?? 'No HTTP_USER_AGENT') + '","' +
521
+ realIp.replace(/"/g, '""') + '","' +
522
+ clientIp.replace(/"/g, '""') + '",' +
523
+ JSON.stringify(msg.replace(/"/g, '""')) + '\n', {
524
+ 'encoding': 'utf8',
525
+ 'mode': 0o777,
526
+ 'flag': 'a'
527
+ });
528
+ }
529
+ async function getLog(opt) {
530
+ const path = kebab.LOG_CWD + opt.host + (opt.fend ?? '') + '/' + opt.path + '.csv';
531
+ if (!await lFs.isFile(path)) {
532
+ return null;
533
+ }
534
+ let limit = opt.limit ?? 100;
535
+ let offset = opt.offset ?? 0;
536
+ return new Promise((resolve) => {
537
+ const list = [];
538
+ let line = 0;
539
+ let packet = '';
540
+ lFs.createReadStream(path, {
541
+ 'encoding': 'utf8'
542
+ }).on('data', (buf) => {
543
+ if (typeof buf !== 'string') {
544
+ return;
545
+ }
546
+ while (true) {
547
+ const index = buf.indexOf('\n');
548
+ if (index === -1) {
549
+ packet += buf;
550
+ break;
551
+ }
552
+ if (limit === 0) {
553
+ break;
554
+ }
555
+ packet += buf.slice(0, index);
556
+ buf = buf.slice(index + 1);
557
+ ++line;
558
+ if (line > 1) {
559
+ if (offset === 0) {
560
+ if (!opt.search || packet.includes(opt.search)) {
561
+ const result = [];
562
+ let currentField = '';
563
+ let inQuotes = false;
564
+ for (let i = 0; i < packet.length; ++i) {
565
+ const char = packet[i];
566
+ if (char === '"') {
567
+ if (inQuotes && packet[i + 1] === '"') {
568
+ currentField += '"';
569
+ ++i;
570
+ }
571
+ else {
572
+ inQuotes = !inQuotes;
573
+ }
574
+ }
575
+ else if (char === ',' && !inQuotes) {
576
+ result.push(currentField);
577
+ currentField = '';
578
+ }
579
+ else {
580
+ currentField += char;
581
+ }
582
+ }
583
+ result.push(currentField);
584
+ list.push(result);
585
+ --limit;
586
+ }
587
+ }
588
+ else {
589
+ --offset;
590
+ }
591
+ }
592
+ packet = '';
593
+ if (!buf.length) {
594
+ break;
595
+ }
596
+ }
597
+ }).on('end', () => {
598
+ resolve(list);
599
+ }).on('error', () => {
600
+ resolve(false);
601
+ });
602
+ });
603
+ }
604
+ function clone(obj) {
605
+ let newObj = {};
606
+ if (obj instanceof Array) {
607
+ newObj = [];
608
+ for (let i = 0; i < obj.length; ++i) {
609
+ if (obj[i] instanceof Date) {
610
+ newObj[i] = new Date(obj[i].getTime());
611
+ }
612
+ else if (obj[i] instanceof FormData) {
613
+ const fd = new FormData();
614
+ for (const item of obj[i]) {
615
+ fd.append(item[0], item[1]);
616
+ }
617
+ newObj[i] = fd;
618
+ }
619
+ else if (obj[i] === null) {
620
+ newObj[i] = null;
621
+ }
622
+ else if (typeof obj[i] === 'object') {
623
+ newObj[i] = clone(obj[i]);
624
+ }
625
+ else {
626
+ newObj[i] = obj[i];
627
+ }
628
+ }
629
+ }
630
+ else {
631
+ for (const key in obj) {
632
+ if (obj[key] instanceof Date) {
633
+ newObj[key] = new Date(obj[key].getTime());
634
+ }
635
+ else if (obj[key] instanceof FormData) {
636
+ const fd = new FormData();
637
+ for (const item of obj[key]) {
638
+ fd.append(item[0], item[1]);
639
+ }
640
+ newObj[key] = fd;
641
+ }
642
+ else if (obj[key] === null) {
643
+ newObj[key] = null;
644
+ }
645
+ else if (typeof obj[key] === 'object') {
646
+ newObj[key] = clone(obj[key]);
647
+ }
648
+ else {
649
+ newObj[key] = obj[key];
650
+ }
651
+ }
652
+ }
653
+ return newObj;
654
+ }
655
+ function debug(message, ...optionalParams) {
656
+ if (!exports.globalConfig.debug) {
657
+ return;
658
+ }
659
+ console.debug(message, ...optionalParams);
660
+ }
661
+ function display(message, ...optionalParams) {
662
+ console.log(message, ...optionalParams);
663
+ }