@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/net.js ADDED
@@ -0,0 +1,462 @@
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.getCa = getCa;
37
+ exports.open = open;
38
+ exports.get = get;
39
+ exports.post = post;
40
+ exports.postJson = postJson;
41
+ exports.request = request;
42
+ exports.setCookie = setCookie;
43
+ exports.buildCookieQuery = buildCookieQuery;
44
+ exports.resetCookieSession = resetCookieSession;
45
+ exports.getFormData = getFormData;
46
+ exports.filterProxyHeaders = filterProxyHeaders;
47
+ exports.mproxy = mproxy;
48
+ exports.mproxyData = mproxyData;
49
+ exports.rproxy = rproxy;
50
+ const stream = __importStar(require("stream"));
51
+ const hc = __importStar(require("@litert/http-client"));
52
+ const fs = __importStar(require("~/lib/fs"));
53
+ const text = __importStar(require("~/lib/text"));
54
+ const time = __importStar(require("~/lib/time"));
55
+ const kebab = __importStar(require("~/index"));
56
+ const fd = __importStar(require("./net/formdata"));
57
+ const lRequest = __importStar(require("./net/request"));
58
+ const response = __importStar(require("./net/response"));
59
+ let ca = '';
60
+ async function getCa() {
61
+ if (ca) {
62
+ return ca;
63
+ }
64
+ ca = (await fs.getContent(kebab.LIB_PATH + 'net/cacert.pem', 'utf8')) ?? '';
65
+ return ca;
66
+ }
67
+ const reuses = {
68
+ 'default': hc.createHttpClient()
69
+ };
70
+ function open(u) {
71
+ return new lRequest.Request(u);
72
+ }
73
+ async function get(u, opt = {}) {
74
+ return request(u, undefined, opt);
75
+ }
76
+ async function post(u, data, opt = {}) {
77
+ opt.method = 'POST';
78
+ return request(u, data, opt);
79
+ }
80
+ async function postJson(u, data, opt = {}) {
81
+ opt.method = 'POST';
82
+ opt.type = 'json';
83
+ return request(u, data, opt);
84
+ }
85
+ async function request(u, data, opt = {}) {
86
+ const uri = text.parseUrl(u);
87
+ const puri = opt.mproxy ? text.parseUrl(opt.mproxy.url) : null;
88
+ const method = opt.method ?? 'GET';
89
+ const type = opt.type ?? 'form';
90
+ const timeout = opt.timeout ?? 10;
91
+ const follow = opt.follow ?? 0;
92
+ const hosts = opt.hosts ?? {};
93
+ const save = opt.save;
94
+ const local = opt.local;
95
+ const reuse = opt.reuse ?? 'default';
96
+ const headers = {};
97
+ if (opt.headers) {
98
+ for (const key in opt.headers) {
99
+ headers[key.toLowerCase()] = opt.headers[key];
100
+ }
101
+ }
102
+ headers['user-agent'] ??= 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36';
103
+ if (method === 'GET') {
104
+ if (data && !(data instanceof stream.Readable) && !Buffer.isBuffer(data)) {
105
+ u += (u.includes('?') ? '&' : '?') +
106
+ (typeof data === 'string' ? data : text.queryStringify(data));
107
+ data = undefined;
108
+ }
109
+ }
110
+ else {
111
+ if (data && !(data instanceof stream.Readable) && !Buffer.isBuffer(data) && typeof data !== 'string') {
112
+ if (type === 'form') {
113
+ data = text.queryStringify(data);
114
+ headers['content-type'] = 'application/x-www-form-urlencoded';
115
+ }
116
+ else {
117
+ data = text.stringifyJson(data);
118
+ headers['content-type'] = 'application/json; charset=utf-8';
119
+ }
120
+ }
121
+ else if (data instanceof fd.FormData) {
122
+ headers['content-type'] = 'multipart/form-data; boundary=' + data.getBoundary();
123
+ headers['content-length'] = data.getLength();
124
+ }
125
+ }
126
+ headers['accept-encoding'] = 'gzip, deflate';
127
+ if (uri.protocol === 'https:') {
128
+ await getCa();
129
+ }
130
+ if (opt.cookie) {
131
+ headers['cookie'] = buildCookieQuery(opt.cookie, uri);
132
+ }
133
+ let req;
134
+ try {
135
+ const host = (puri ? puri.hostname : uri.hostname) ?? '';
136
+ if (!host) {
137
+ const res = new response.Response(null);
138
+ res.error = {
139
+ 'name': 'Possible mProxy error',
140
+ 'message': 'host not found'
141
+ };
142
+ return res;
143
+ }
144
+ if (hosts[host] !== undefined && !hosts[host]) {
145
+ const res = new response.Response(null);
146
+ res.error = {
147
+ 'name': 'hosts error',
148
+ 'message': 'hosts param error'
149
+ };
150
+ return res;
151
+ }
152
+ if (!reuses[reuse]) {
153
+ reuses[reuse] = hc.createHttpClient();
154
+ }
155
+ req = await reuses[reuse].request({
156
+ 'url': opt.mproxy ? opt.mproxy.url + (opt.mproxy.url.includes('?') ? '&' : '?') + text.queryStringify({
157
+ 'url': u,
158
+ 'auth': opt.mproxy.auth,
159
+ 'data': opt.mproxy.data ? text.stringifyJson(opt.mproxy.data) : '{}'
160
+ }) : u,
161
+ 'method': method,
162
+ 'data': data,
163
+ 'headers': headers,
164
+ 'timeout': timeout * 1000,
165
+ 'localAddress': local,
166
+ 'ca': ca,
167
+ 'connectionOptions': {
168
+ 'remoteHost': hosts[host]
169
+ },
170
+ });
171
+ }
172
+ catch (err) {
173
+ const res = new response.Response(null);
174
+ res.error = err;
175
+ return res;
176
+ }
177
+ if (opt.cookie) {
178
+ await buildCookieObject(opt.cookie, (req.headers['set-cookie'] ?? []), uri);
179
+ }
180
+ let total = 0;
181
+ if (save && (!req.headers['location'] || follow === 0)) {
182
+ await new Promise(function (resolve) {
183
+ req.getStream().on('end', () => {
184
+ fs.stats(save).then((stat) => {
185
+ total = stat?.size ?? 0;
186
+ resolve();
187
+ }).catch(() => {
188
+ resolve();
189
+ });
190
+ }).on('error', function () {
191
+ resolve();
192
+ }).pipe(fs.createWriteStream(save));
193
+ });
194
+ }
195
+ const res = new response.Response(req);
196
+ if (total) {
197
+ res.setContent(total.toString());
198
+ }
199
+ res.headers = req.headers;
200
+ switch (req.protocol) {
201
+ case hc.EProtocol.HTTPS_2:
202
+ case hc.EProtocol.HTTP_2: {
203
+ req.headers['http-version'] = '2.0';
204
+ break;
205
+ }
206
+ default: {
207
+ res.headers['http-version'] = '1.1';
208
+ }
209
+ }
210
+ res.headers['http-code'] = req.statusCode;
211
+ res.headers['http-url'] = u;
212
+ if (follow === 0) {
213
+ return res;
214
+ }
215
+ if (!res.headers['location']) {
216
+ return res;
217
+ }
218
+ headers['referer'] = u;
219
+ return request(text.urlResolve(u, req.headers['location']), data, {
220
+ 'method': method,
221
+ 'type': type,
222
+ 'timeout': timeout,
223
+ 'follow': follow - 1,
224
+ 'hosts': hosts,
225
+ 'save': save,
226
+ 'local': local,
227
+ 'headers': headers,
228
+ 'mproxy': opt.mproxy,
229
+ 'reuse': reuse
230
+ });
231
+ }
232
+ function setCookie(cookie, name, value, domain, opt = {}) {
233
+ const tim = time.stamp();
234
+ const ttl = opt.ttl ?? 0;
235
+ domain = domain.split(':')[0];
236
+ const domainN = domain.startsWith('.') ? domain.slice(1) : domain;
237
+ let exp = -1992199400;
238
+ if (ttl) {
239
+ exp = tim + ttl;
240
+ }
241
+ cookie[name + '-' + domainN] = {
242
+ 'name': name,
243
+ 'value': value,
244
+ 'exp': exp,
245
+ 'path': opt['path'] ?? '/',
246
+ 'domain': domainN,
247
+ 'secure': opt['ssl'] ? true : false,
248
+ 'httponly': opt['httponly'] ? true : false
249
+ };
250
+ }
251
+ async function buildCookieObject(cookie, setCookies, uri) {
252
+ const tim = time.stamp();
253
+ uri.path ??= '/';
254
+ for (const setCookie of setCookies) {
255
+ const cookieTmp = {};
256
+ const list = setCookie.split(';');
257
+ for (let index = 0; index < list.length; ++index) {
258
+ const item = list[index];
259
+ const arr = item.split('=');
260
+ const key = arr[0].trim();
261
+ const val = arr[1] ?? '';
262
+ if (index === 0) {
263
+ cookieTmp['name'] = key;
264
+ cookieTmp['value'] = decodeURIComponent(val);
265
+ }
266
+ else {
267
+ cookieTmp[key.toLowerCase()] = val;
268
+ }
269
+ }
270
+ let domain = '', domainN = '';
271
+ if (cookieTmp['domain']) {
272
+ cookieTmp['domain'] = cookieTmp['domain'].split(':')[0];
273
+ if (!(cookieTmp['domain'].startsWith('.'))) {
274
+ domain = '.' + cookieTmp['domain'];
275
+ domainN = cookieTmp['domain'];
276
+ }
277
+ else {
278
+ domain = cookieTmp['domain'];
279
+ domainN = cookieTmp['domain'].slice(1);
280
+ }
281
+ }
282
+ else {
283
+ domain = '.' + (uri.hostname ?? '');
284
+ domainN = uri.hostname ?? '';
285
+ }
286
+ if (uri.hostname !== domainN) {
287
+ if (!text.isDomain(uri.hostname ?? '')) {
288
+ continue;
289
+ }
290
+ const parseDomain = await text.parseDomain(domainN);
291
+ if (parseDomain.tld === domainN.toLowerCase()) {
292
+ continue;
293
+ }
294
+ if (!((uri.hostname ?? '').endsWith(domain))) {
295
+ continue;
296
+ }
297
+ }
298
+ const cookieKey = cookieTmp['name'] + '-' + domainN;
299
+ if (cookieTmp['max-age'] && (Number(cookieTmp['max-age']) <= 0)) {
300
+ if (cookie[cookieKey]) {
301
+ delete cookie[cookieKey];
302
+ continue;
303
+ }
304
+ }
305
+ let exp = -1992199400;
306
+ if (cookieTmp['max-age']) {
307
+ exp = tim + Number(cookieTmp['max-age']);
308
+ }
309
+ let path = cookieTmp['path'] ?? '';
310
+ if (path === '') {
311
+ const srp = (uri.pathname ?? '').lastIndexOf('/');
312
+ path = (uri.pathname ?? '').slice(0, srp + 1);
313
+ }
314
+ else if (!path.startsWith('/')) {
315
+ path = '/' + path;
316
+ }
317
+ cookie[cookieKey] = {
318
+ 'name': cookieTmp['name'],
319
+ 'value': cookieTmp['value'],
320
+ 'exp': exp,
321
+ 'path': path,
322
+ 'domain': domainN,
323
+ 'secure': cookieTmp['secure'] !== undefined ? true : false,
324
+ 'httponly': cookieTmp['httponly'] !== undefined ? true : false
325
+ };
326
+ }
327
+ }
328
+ function buildCookieQuery(cookie, uri) {
329
+ const tim = time.stamp();
330
+ let cookieStr = '';
331
+ for (const key in cookie) {
332
+ const item = cookie[key];
333
+ if ((item.exp < tim) && (item.exp !== -1992199400)) {
334
+ delete cookie[key];
335
+ continue;
336
+ }
337
+ uri.path ??= '/';
338
+ if (item.secure && (uri.protocol === 'https')) {
339
+ continue;
340
+ }
341
+ if (!uri.path.startsWith(item.path)) {
342
+ continue;
343
+ }
344
+ const domain = '.' + item.domain;
345
+ if ('.' + (uri.hostname ?? '') !== domain) {
346
+ if (!(uri.hostname.endsWith(domain))) {
347
+ continue;
348
+ }
349
+ }
350
+ cookieStr += item.name + '=' + encodeURIComponent(item.value) + '; ';
351
+ }
352
+ if (cookieStr !== '') {
353
+ return cookieStr.slice(0, -2);
354
+ }
355
+ else {
356
+ return '';
357
+ }
358
+ }
359
+ function resetCookieSession(cookie) {
360
+ for (const key in cookie) {
361
+ const item = cookie[key];
362
+ if (item.exp === -1992199400000) {
363
+ delete cookie[key];
364
+ }
365
+ }
366
+ }
367
+ function getFormData() {
368
+ return new fd.FormData();
369
+ }
370
+ const proxyContinueHeaders = ['host', 'connection', 'http-version', 'http-code', 'http-url'];
371
+ function filterProxyHeaders(headers, res) {
372
+ const heads = {};
373
+ for (const h in headers) {
374
+ if (proxyContinueHeaders.includes(h)) {
375
+ continue;
376
+ }
377
+ if (h.includes(':') || h.includes('(')) {
378
+ continue;
379
+ }
380
+ const v = headers[h];
381
+ if (v === undefined) {
382
+ continue;
383
+ }
384
+ if (res) {
385
+ res.setHeader(h, v);
386
+ continue;
387
+ }
388
+ heads[h] = v;
389
+ }
390
+ return heads;
391
+ }
392
+ async function mproxy(ctr, auth, opt = {}) {
393
+ const req = ctr.getPrototype('_req');
394
+ const res = ctr.getPrototype('_res');
395
+ const input = ctr.getPrototype('_input');
396
+ const get = ctr.getPrototype('_get');
397
+ if (get['auth'] !== auth) {
398
+ return 0;
399
+ }
400
+ if (!get['url']) {
401
+ return -1;
402
+ }
403
+ opt.method = req.method ?? 'GET';
404
+ opt.headers ??= {};
405
+ Object.assign(opt.headers, filterProxyHeaders(req.headers));
406
+ const rres = await request(get['url'], req.headers['content-type']?.includes('form-data') ? req : input, opt);
407
+ if (rres.error) {
408
+ return -2;
409
+ }
410
+ if (rres.headers) {
411
+ filterProxyHeaders(rres.headers, res);
412
+ }
413
+ res.writeHead(rres.headers?.['http-code'] ?? 200);
414
+ await new Promise((resolve) => {
415
+ rres.getRawStream().pipe(res).on('finish', () => {
416
+ resolve();
417
+ });
418
+ });
419
+ return 1;
420
+ }
421
+ function mproxyData(ctr) {
422
+ const get = ctr.getPrototype('_get');
423
+ if (!get['data']) {
424
+ return {};
425
+ }
426
+ const data = text.parseJson(get['data']);
427
+ if (!data) {
428
+ return {};
429
+ }
430
+ return data;
431
+ }
432
+ async function rproxy(ctr, route, opt = {}) {
433
+ const req = ctr.getPrototype('_req');
434
+ const res = ctr.getPrototype('_res');
435
+ const config = ctr.getPrototype('_config');
436
+ const input = ctr.getPrototype('_input');
437
+ const path = config.const.path + (config.const.qs ? '?' + config.const.qs : '');
438
+ for (const key in route) {
439
+ if (!path.startsWith(key)) {
440
+ continue;
441
+ }
442
+ const lpath = path.slice(key.length);
443
+ opt.method = req.method ?? 'GET';
444
+ opt.headers ??= {};
445
+ Object.assign(opt.headers, filterProxyHeaders(req.headers));
446
+ const rres = await request(route[key] + lpath, req.headers['content-type']?.includes('form-data') ? req : input, opt);
447
+ if (rres.error) {
448
+ return false;
449
+ }
450
+ if (rres.headers) {
451
+ filterProxyHeaders(rres.headers, res);
452
+ }
453
+ res.writeHead(rres.headers?.['http-code'] ?? 200);
454
+ await new Promise((resolve) => {
455
+ rres.getRawStream().pipe(res).on('finish', () => {
456
+ resolve();
457
+ });
458
+ });
459
+ return true;
460
+ }
461
+ return false;
462
+ }