@maiyunnet/kebab 9.29.6 → 10.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 (52) hide show
  1. package/abox/proxy.js +24 -2
  2. package/abox/response.js +36 -17
  3. package/abox/session-login.js +1 -13
  4. package/doc/kebab-rag.md +1522 -1034
  5. package/index.d.ts +12 -1
  6. package/index.js +1 -1
  7. package/lib/ai.d.ts +4 -0
  8. package/lib/ai.js +32 -16
  9. package/lib/cookie.d.ts +2 -0
  10. package/lib/cookie.js +47 -45
  11. package/lib/core.d.ts +26 -0
  12. package/lib/core.js +36 -13
  13. package/lib/cron.js +1 -8
  14. package/lib/crypto.js +20 -6
  15. package/lib/db/conn.js +1 -0
  16. package/lib/fs.js +78 -31
  17. package/lib/kv.d.ts +182 -79
  18. package/lib/kv.js +518 -214
  19. package/lib/s3.d.ts +2 -0
  20. package/lib/s3.js +14 -27
  21. package/lib/session.d.ts +7 -0
  22. package/lib/session.js +23 -2
  23. package/lib/socket.d.ts +4 -1
  24. package/lib/socket.js +1 -0
  25. package/lib/sql.d.ts +0 -5
  26. package/lib/sql.js +48 -12
  27. package/lib/text.d.ts +10 -5
  28. package/lib/text.js +22 -12
  29. package/lib/turnstile.d.ts +6 -0
  30. package/lib/turnstile.js +16 -9
  31. package/lib/undici.d.ts +7 -1
  32. package/lib/undici.js +72 -36
  33. package/lib/ws.d.ts +1 -1
  34. package/lib/ws.js +85 -128
  35. package/lib/zip.js +46 -45
  36. package/package.json +2 -2
  37. package/sys/child.js +178 -146
  38. package/sys/cmd.js +196 -197
  39. package/sys/ctr.d.ts +14 -14
  40. package/sys/ctr.js +145 -59
  41. package/sys/master.js +111 -80
  42. package/sys/mod.d.ts +8 -2
  43. package/sys/mod.js +23 -11
  44. package/sys/monitor/files.d.ts +2 -1
  45. package/sys/monitor/files.js +77 -21
  46. package/sys/monitor/watchdog.d.ts +7 -0
  47. package/sys/monitor/watchdog.js +311 -203
  48. package/sys/monitor.d.ts +59 -54
  49. package/sys/monitor.js +742 -681
  50. package/sys/route.d.ts +4 -4
  51. package/sys/route.js +93 -52
  52. package/www/example/ctr/test.js +8 -8
package/lib/zip.js CHANGED
@@ -62,7 +62,7 @@ export class Zip {
62
62
  stats(path) {
63
63
  path = lText.urlResolve(this._path, path);
64
64
  let dirpath = path.endsWith('/') ? path : path + '/';
65
- if (!this._list[dirpath]) {
65
+ if (!Object.hasOwn(this._list, dirpath)) {
66
66
  // --- 可能是文件 ---
67
67
  if (path.endsWith('/')) {
68
68
  // --- 不可能是文件 ---
@@ -71,11 +71,11 @@ export class Zip {
71
71
  const lio = path.lastIndexOf('/') + 1;
72
72
  const dpath = path.slice(0, lio);
73
73
  const fname = path.slice(lio);
74
- if (!this._list[dpath]) {
74
+ if (!Object.hasOwn(this._list, dpath)) {
75
75
  // --- 上层目录不存在 ---
76
76
  return null;
77
77
  }
78
- const file = this._list[dpath][fname];
78
+ const file = Object.hasOwn(this._list[dpath], fname) ? this._list[dpath][fname] : undefined;
79
79
  if (!file) {
80
80
  return null;
81
81
  }
@@ -102,12 +102,12 @@ export class Zip {
102
102
  const lio = dirpath.lastIndexOf('/') + 1;
103
103
  const dpath = dirpath.slice(0, lio);
104
104
  const fname = dirpath.slice(lio);
105
- const pfolder = this._list[dpath];
106
- const folder = pfolder[fname];
105
+ const pfolder = Object.hasOwn(this._list, dpath) ? this._list[dpath] : undefined;
106
+ const folder = pfolder && Object.hasOwn(pfolder, fname) ? pfolder[fname] : undefined;
107
107
  return {
108
108
  'compressedSize': 0,
109
109
  'uncompressedSize': 0,
110
- 'date': folder.date,
110
+ 'date': folder?.date ?? new Date(0),
111
111
  'isFile': false,
112
112
  'isDirectory': true
113
113
  };
@@ -157,16 +157,26 @@ export class Zip {
157
157
  if (!folder) {
158
158
  return opt.pathAsKey ? {} : [];
159
159
  }
160
- if (!this._list[path]) {
160
+ if (!Object.hasOwn(this._list, path)) {
161
161
  return opt.pathAsKey ? {} : [];
162
162
  }
163
163
  if (!opt.hasChildren) {
164
164
  if (opt.pathAsKey) {
165
- return this._list[path];
165
+ const res = {};
166
+ for (const k in this._list[path]) {
167
+ const item = this._list[path][k];
168
+ if (item.isFile || opt.hasDir) {
169
+ res[k] = item;
170
+ }
171
+ }
172
+ return res;
166
173
  }
167
174
  const list = [];
168
175
  for (const k in this._list[path]) {
169
- list.push(this._list[path][k]);
176
+ const item = this._list[path][k];
177
+ if (item.isFile || opt.hasDir) {
178
+ list.push(item);
179
+ }
170
180
  }
171
181
  return list;
172
182
  }
@@ -256,7 +266,7 @@ export class Zip {
256
266
  * --- 重建目录列表缓存 ---
257
267
  */
258
268
  _refreshList() {
259
- const list = {};
269
+ const list = Object.create(null);
260
270
  // eslint-disable-next-line @litert/disable-for-each-method
261
271
  this._zip.forEach(function (relativePath, item) {
262
272
  if (relativePath.startsWith('/')) {
@@ -281,8 +291,8 @@ export class Zip {
281
291
  if (item.dir) {
282
292
  name = name.slice(0, -1);
283
293
  }
284
- if (!list[parentPath]) {
285
- list[parentPath] = {};
294
+ if (!Object.hasOwn(list, parentPath)) {
295
+ list[parentPath] = Object.create(null);
286
296
  }
287
297
  list[parentPath][name] = {
288
298
  'name': name,
@@ -350,43 +360,34 @@ export class Zip {
350
360
  'hasChildren': true,
351
361
  'hasDir': false
352
362
  });
363
+ if (list.length === 0) {
364
+ resolve(files);
365
+ return;
366
+ }
353
367
  let loaded = 0;
354
368
  for (const file of list) {
369
+ const fullPath = file.path + file.name;
355
370
  const mim = mime.getData(file.name);
356
- if (['txt', 'json', 'js', 'css', 'xml', 'html'].includes(mim.extension)) {
357
- this.getContent(file.path + file.name, 'string').then(function (fb) {
358
- if (fb) {
359
- files[file.path + file.name] = fb;
360
- }
361
- ++loaded;
362
- if (loaded === list.length) {
363
- resolve(files);
364
- }
365
- }).catch(function () {
366
- ++loaded;
367
- if (loaded === list.length) {
368
- resolve(files);
369
- }
370
- });
371
- }
372
- else {
373
- this.getContent(file.path + file.name, 'nodebuffer').then(function (fb) {
374
- if (fb) {
375
- files[file.path + file.name] = fb;
376
- }
377
- ++loaded;
378
- if (loaded === list.length) {
379
- resolve(files);
380
- }
381
- }).catch(function () {
382
- ++loaded;
383
- if (loaded === list.length) {
384
- resolve(files);
385
- }
386
- });
387
- }
371
+ const isText = ['txt', 'json', 'js', 'css', 'xml', 'html'].includes(mim.extension);
372
+ const zipObj = this._zip.file(fullPath.slice(1)) ?? this._zip.file(fullPath);
373
+ const task = zipObj
374
+ ? zipObj.async(isText ? 'string' : 'nodebuffer')
375
+ : this.getContent(fullPath, isText ? 'string' : 'nodebuffer');
376
+ task.then(function (fb) {
377
+ if (fb !== null) {
378
+ files[fullPath] = fb;
379
+ }
380
+ ++loaded;
381
+ if (loaded === list.length) {
382
+ resolve(files);
383
+ }
384
+ }).catch(function () {
385
+ ++loaded;
386
+ if (loaded === list.length) {
387
+ resolve(files);
388
+ }
389
+ });
388
390
  }
389
- return files;
390
391
  });
391
392
  }
392
393
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@maiyunnet/kebab",
3
- "version": "9.29.6",
3
+ "version": "10.0.0",
4
4
  "description": "Simple, easy-to-use, and fully-featured Node.js framework that is ready-to-use out of the box.",
5
5
  "type": "module",
6
6
  "keywords": [
@@ -30,7 +30,6 @@
30
30
  "@aws-sdk/lib-storage": "^3.1161.0",
31
31
  "@litert/http-client": "^1.2.0",
32
32
  "@litert/mime": "^2.0.0",
33
- "@litert/redis": "^3.2.1",
34
33
  "@radix-ui/react-checkbox": "^1.4.1",
35
34
  "@radix-ui/react-label": "^2.1.17",
36
35
  "@radix-ui/react-slot": "^1.5.0",
@@ -51,6 +50,7 @@
51
50
  "react": "^19.3.1",
52
51
  "react-dom": "^19.3.1",
53
52
  "react-router-dom": "^7.18.5",
53
+ "redis": "^6.4.0",
54
54
  "ssh2": "^1.17.0",
55
55
  "svg-captcha": "^1.4.0",
56
56
  "tailwind-merge": "^3.7.0",
package/sys/child.js CHANGED
@@ -3,11 +3,12 @@
3
3
  * Date: 2019-5-3 23:54
4
4
  * Last: 2020-3-31 15:01:07, 2020-4-9 22:28:50, 2022-07-22 14:19:46, 2022-9-29 22:11:07, 2023-5-1 18:26:57, 2024-1-12 13:32:00, 2024-3-4 16:49:19, 2026-3-8 16:21:40, 2026-8-22
5
5
  */
6
+ // --- 系统 ---
6
7
  import * as http2 from 'http2';
7
8
  import * as tls from 'tls';
8
9
  import * as http from 'http';
9
10
  import * as crypto from 'crypto';
10
- // --- 库 ---
11
+ // --- 库和定义 ---
11
12
  import * as lFs from '#kebab/lib/fs.js';
12
13
  import * as lCore from '#kebab/lib/core.js';
13
14
  import * as lText from '#kebab/lib/text.js';
@@ -38,8 +39,8 @@ const hbTimer = setInterval(function () {
38
39
  return;
39
40
  }
40
41
  process.send({
41
- action: 'hbtime',
42
- pid: process.pid
42
+ 'action': 'hbtime',
43
+ 'pid': process.pid,
43
44
  });
44
45
  }, 10_000);
45
46
  /**
@@ -228,79 +229,142 @@ async function requestHandler(req, res, https) {
228
229
  }
229
230
  };
230
231
  timer.timer = setTimeout(timer.callback, timer.timeout);
231
- res.setHeader('Server', 'Kebab');
232
- res.setHeader('expires', 'Mon, 26 Jul 1994 05:00:00 GMT');
233
- res.setHeader('cache-control', 'no-store');
234
- // --- 停止中:通知 HTTP/1.1 客户端不要复用此连接(HTTP/2 由 GOAWAY 帧处理) ---
235
- if (stopping && res instanceof http.ServerResponse) {
236
- res.setHeader('connection', 'close');
237
- }
238
- // --- 当前 uri ---
239
- let host = req.headers[':authority'];
240
- if (host === undefined || typeof host !== 'string') {
241
- host = req.headers['host'];
242
- }
243
- if (host === undefined) {
244
- res.end('403 Forbidden');
245
- return;
246
- }
247
- const uri = lText.parseUrl(`http${https ? 's' : ''}://${host}${req.url ?? ''}`);
248
- /** --- 当前的 vhost 配置文件 --- */
249
- const vhost = await getVhostByHostname(uri.hostname ?? '');
250
- if (!vhost) {
251
- res.setHeader('x-kebab-error', '1');
252
- lCore.writeHead(res, 403);
253
- // --- 不能用 req.socket.destroy() 可能会导致底层复用(如 CDN) 的连接被直接断开 ---
254
- res.end('403 Forbidden');
255
- return;
256
- }
257
- // --- 匹配虚拟主机后即记录,反代和提前响应也属于访问 ---
258
- lCore.log({
259
- 'req': req,
260
- 'headers': req.headers,
261
- 'hostname': uri.hostname ?? '',
262
- 'urlFull': `${uri.protocol ?? ''}//${uri.host ?? ''}`,
263
- 'path': uri.path ?? '/',
264
- }, '', '-visit');
265
- // --- 配置来自 JSON,仅布尔值 true 开放内置示例 ---
266
- const debug = lCore.globalConfig.debug;
267
- if (debug !== true && await isExamplePath(vhost.real)) {
268
- rejectExample(res);
269
- return;
270
- }
271
- /** --- 请求的路径部分,前导带 / 末尾不一定,用户怎么请求就是什么 --- */
272
- let path = uri.pathname ?? '/';
273
- if (path !== '/') {
274
- // --- 去除 ../ ./ 之类的 ---
275
- path = lText.urlResolve('/', path);
276
- }
277
- /** --- [''] / ['', 'abc', 'def'] --- */
278
- const pathList = path.split('/');
279
- /** --- 当前处理的路径,前导不带 / 末尾带 /,例如 abc/ --- */
280
- let now = '';
281
- for (let i = 0; i < pathList.length; ++i) {
282
- const item = pathList[i];
283
- if (item !== '') {
284
- // --- 判断 item 是文件还是文件夹 ---
285
- /** --- 'abc' / 'def.json' --- */
286
- let stat = await lFs.stats(vhost.real + now + item);
287
- if (!stat) {
288
- const content = '<h1>404 Not found</h1>';
289
- res.setHeader('content-type', 'text/html; charset=utf-8');
290
- res.setHeader('content-length', Buffer.byteLength(content));
291
- lCore.writeHead(res, 404);
292
- res.end(content);
293
- return;
294
- }
295
- if (debug !== true && (stat.isDirectory() || stat.isSymbolicLink()) &&
296
- await isExamplePath(vhost.real + now + item)) {
297
- rejectExample(res);
298
- return;
232
+ try {
233
+ res.setHeader('Server', 'Kebab');
234
+ res.setHeader('expires', 'Mon, 26 Jul 1994 05:00:00 GMT');
235
+ res.setHeader('cache-control', 'no-store');
236
+ // --- 停止中:通知 HTTP/1.1 客户端不要复用此连接(HTTP/2 由 GOAWAY 帧处理) ---
237
+ if (stopping && res instanceof http.ServerResponse) {
238
+ res.setHeader('connection', 'close');
239
+ }
240
+ // --- 当前 uri ---
241
+ let host = req.headers[':authority'];
242
+ if (host === undefined || typeof host !== 'string') {
243
+ host = req.headers['host'];
244
+ }
245
+ if (!host || host.length > 255 || /[\\/?#@\s\u0000-\u001f\u007f]/.test(host)) {
246
+ lCore.writeHead(res, 403);
247
+ res.end('403 Forbidden');
248
+ return;
249
+ }
250
+ const rawUrl = req.url ?? '/';
251
+ const safeUrl = rawUrl.startsWith('/') ? rawUrl : '/' + rawUrl;
252
+ const uri = lText.parseUrl(`http${https ? 's' : ''}://${host}${safeUrl}`);
253
+ /** --- 当前的 vhost 配置文件 --- */
254
+ const vhost = await getVhostByHostname(uri.hostname ?? '');
255
+ if (!vhost) {
256
+ res.setHeader('x-kebab-error', '1');
257
+ lCore.writeHead(res, 403);
258
+ // --- 不能用 req.socket.destroy() 可能会导致底层复用(如 CDN) 的连接被直接断开 ---
259
+ res.end('403 Forbidden');
260
+ return;
261
+ }
262
+ // --- 匹配虚拟主机后即记录,反代和提前响应也属于访问 ---
263
+ lCore.log({
264
+ 'req': req,
265
+ 'headers': req.headers,
266
+ 'hostname': uri.hostname ?? '',
267
+ 'urlFull': `${uri.protocol ?? ''}//${uri.host ?? ''}`,
268
+ 'path': uri.path ?? '/',
269
+ }, '', '-visit');
270
+ // --- 配置来自 JSON,仅布尔值 true 开放内置示例 ---
271
+ const debug = lCore.globalConfig.debug;
272
+ if (debug !== true && await isExamplePath(vhost.real)) {
273
+ rejectExample(res);
274
+ return;
275
+ }
276
+ /** --- 请求的路径部分,前导带 / 末尾不一定,用户怎么请求就是什么 --- */
277
+ let path = uri.pathname ?? '/';
278
+ if (path !== '/') {
279
+ // --- 去除 ../ ./ 之类的 ---
280
+ path = lText.urlResolve('/', path);
281
+ }
282
+ /** --- [''] / ['', 'abc', 'def'] --- */
283
+ const pathList = path.split('/');
284
+ /** --- 当前处理的路径,前导不带 / 末尾带 /,例如 abc/ --- */
285
+ let now = '';
286
+ for (let i = 0; i < pathList.length; ++i) {
287
+ const item = pathList[i];
288
+ if (item !== '') {
289
+ // --- 判断 item 是文件还是文件夹 ---
290
+ /** --- 'abc' / 'def.json' --- */
291
+ let stat = await lFs.stats(vhost.real + now + item);
292
+ if (!stat) {
293
+ const content = '<h1>404 Not found</h1>';
294
+ res.setHeader('content-type', 'text/html; charset=utf-8');
295
+ res.setHeader('content-length', Buffer.byteLength(content));
296
+ lCore.writeHead(res, 404);
297
+ res.end(content);
298
+ return;
299
+ }
300
+ if (debug !== true && (stat.isDirectory() || stat.isSymbolicLink()) &&
301
+ await isExamplePath(vhost.real + now + item)) {
302
+ rejectExample(res);
303
+ return;
304
+ }
305
+ if (stat.isDirectory()) {
306
+ now += item + '/';
307
+ // --- 判断是不是动态层 ---
308
+ stat = await lFs.stats(vhost.real + now + 'kebab.json');
309
+ if (stat) {
310
+ const subPath = pathList.slice(i + 1).join('/');
311
+ // --- 动态层,交给 Route 处理器 ---
312
+ try {
313
+ if (await sRoute.run({
314
+ 'req': req,
315
+ 'res': res,
316
+ 'uri': uri,
317
+ 'rootPath': vhost.real + now,
318
+ 'urlBase': '/' + now,
319
+ 'path': subPath,
320
+ 'timer': timer
321
+ })) {
322
+ return;
323
+ }
324
+ }
325
+ catch (e) {
326
+ lCore.log({
327
+ 'path': subPath,
328
+ 'urlFull': (uri.protocol ?? '') + '//' + (uri.host ?? '') + '/' + now,
329
+ 'hostname': uri.hostname ?? '',
330
+ 'req': req,
331
+ 'get': uri.query ? lText.queryParse(uri.query) : {},
332
+ 'headers': {},
333
+ }, '[CHILD][requestHandler][E0]' + lText.stringifyError(e), '-error');
334
+ const content = '<h1>500 Server Error</h1>';
335
+ if (!res.headersSent) {
336
+ res.setHeader('content-type', 'text/html; charset=utf-8');
337
+ res.setHeader('content-length', Buffer.byteLength(content));
338
+ lCore.writeHead(res, 500);
339
+ }
340
+ if (!res.writableEnded) {
341
+ res.end(content);
342
+ }
343
+ return;
344
+ }
345
+ }
346
+ }
347
+ else if (stat.isFile()) {
348
+ // --- 普通文件,直接输出并结束(拒绝符号链接与特殊文件) ---
349
+ await lFs.readToResponse(vhost.real + now + item, req, res, stat);
350
+ return;
351
+ }
352
+ else {
353
+ const content = '<h1>403 Forbidden</h1>';
354
+ res.setHeader('content-type', 'text/html; charset=utf-8');
355
+ res.setHeader('content-length', Buffer.byteLength(content));
356
+ lCore.writeHead(res, 403);
357
+ res.end(content);
358
+ return;
359
+ }
299
360
  }
300
- if (stat.isDirectory()) {
301
- now += item + '/';
302
- // --- 判断是不是动态层 ---
303
- stat = await lFs.stats(vhost.real + now + 'kebab.json');
361
+ else {
362
+ // --- item 为空,但是 i 不是 0,则是最后一个空,直接 break ---
363
+ if (i > 0) {
364
+ break;
365
+ }
366
+ // --- 本层是根,判断根是不是动态层 ---
367
+ const stat = await lFs.stats(vhost.real + now + 'kebab.json');
304
368
  if (stat) {
305
369
  // --- 动态层,交给 Route 处理器 ---
306
370
  try {
@@ -310,21 +374,14 @@ async function requestHandler(req, res, https) {
310
374
  'uri': uri,
311
375
  'rootPath': vhost.real + now,
312
376
  'urlBase': '/' + now,
313
- 'path': path.slice(('/' + pathList.slice(0, i).join('/')).length + 1),
314
- 'timer': timer
377
+ 'path': path.slice(1),
378
+ 'timer': timer,
315
379
  })) {
316
380
  return;
317
381
  }
318
382
  }
319
383
  catch (e) {
320
- lCore.log({
321
- 'path': path.slice(('/' + pathList.slice(0, i).join('/')).length + 1),
322
- 'urlFull': (uri.protocol ?? '') + '//' + (uri.host ?? '') + '/' + now,
323
- 'hostname': uri.hostname ?? '',
324
- 'req': req,
325
- 'get': uri.query ? lText.queryParse(uri.query) : {},
326
- 'headers': {},
327
- }, '[CHILD][requestHandler][E0]' + lText.stringifyError(e), '-error');
384
+ lCore.log({}, '[CHILD][requestHandler][E1]' + lText.stringifyError(e), '-error');
328
385
  const content = '<h1>500 Server Error</h1>';
329
386
  if (!res.headersSent) {
330
387
  res.setHeader('content-type', 'text/html; charset=utf-8');
@@ -338,64 +395,25 @@ async function requestHandler(req, res, https) {
338
395
  }
339
396
  }
340
397
  }
341
- else {
342
- // --- 文件,直接输出并结束 ---
343
- await lFs.readToResponse(vhost.real + now + item, req, res, stat);
344
- return;
345
- }
346
398
  }
347
- else {
348
- // --- item 为空,但是 i 不是 0,则是最后一个空,直接 break ---
349
- if (i > 0) {
350
- break;
351
- }
352
- // --- 本层是根,判断根是不是动态层 ---
353
- const stat = await lFs.stats(vhost.real + now + 'kebab.json');
399
+ // --- 最后一层是目录,且不是动态层,判断有没有主页,有则输出,没有则 403 出错 ---
400
+ const indexFiles = ['index.html', 'index.htm'];
401
+ for (const indexFile of indexFiles) {
402
+ const stat = await lFs.isFile(vhost.real + now + indexFile);
354
403
  if (stat) {
355
- // --- 动态层,交给 Route 处理器 ---
356
- try {
357
- if (await sRoute.run({
358
- 'req': req,
359
- 'res': res,
360
- 'uri': uri,
361
- 'rootPath': vhost.real + now,
362
- 'urlBase': '/' + now,
363
- 'path': path.slice(1),
364
- 'timer': timer,
365
- })) {
366
- return;
367
- }
368
- }
369
- catch (e) {
370
- lCore.log({}, '[CHILD][requestHandler][E1]' + lText.stringifyError(e), '-error');
371
- const content = '<h1>500 Server Error</h1>';
372
- if (!res.headersSent) {
373
- res.setHeader('content-type', 'text/html; charset=utf-8');
374
- res.setHeader('content-length', Buffer.byteLength(content));
375
- lCore.writeHead(res, 500);
376
- }
377
- if (!res.writableEnded) {
378
- res.end(content);
379
- }
380
- return;
381
- }
404
+ await lFs.readToResponse(vhost.real + now + indexFile, req, res, stat);
405
+ return;
382
406
  }
383
407
  }
408
+ const content = '<h1>403 Forbidden</h1>';
409
+ res.setHeader('content-type', 'text/html; charset=utf-8');
410
+ res.setHeader('content-length', Buffer.byteLength(content));
411
+ lCore.writeHead(res, 403);
412
+ res.end(content);
384
413
  }
385
- // --- 最后一层是目录,且不是动态层,判断有没有主页,有则输出,没有则 403 出错 ---
386
- const indexFiles = ['index.html', 'index.htm'];
387
- for (const indexFile of indexFiles) {
388
- const stat = await lFs.isFile(vhost.real + now + indexFile);
389
- if (stat) {
390
- await lFs.readToResponse(vhost.real + now + indexFile, req, res, stat);
391
- return;
392
- }
414
+ finally {
415
+ clearTimeout(timer.timer);
393
416
  }
394
- const content = '<h1>403 Forbidden</h1>';
395
- res.setHeader('content-type', 'text/html; charset=utf-8');
396
- res.setHeader('content-length', Buffer.byteLength(content));
397
- lCore.writeHead(res, 403);
398
- res.end(content);
399
417
  }
400
418
  /**
401
419
  * --- WebSocket 响应 handler ---
@@ -407,7 +425,14 @@ async function requestHandler(req, res, https) {
407
425
  async function upgradeHandler(req, socket, https, head) {
408
426
  socket.removeAllListeners('error');
409
427
  // --- 当前 uri ---
410
- const uri = lText.parseUrl(`ws${https ? 's' : ''}://${req.headers['host'] ?? ''}${req.url ?? ''}`);
428
+ const host = req.headers['host'] ?? '';
429
+ if (!host || host.length > 255 || /[\\/?#@\s\u0000-\u001f\u007f]/.test(host)) {
430
+ socket.end();
431
+ return;
432
+ }
433
+ const rawUrl = req.url ?? '/';
434
+ const safeUrl = rawUrl.startsWith('/') ? rawUrl : '/' + rawUrl;
435
+ const uri = lText.parseUrl(`ws${https ? 's' : ''}://${host}${safeUrl}`);
411
436
  /** --- 当前的 vhost 配置文件 --- */
412
437
  const vhost = await getVhostByHostname(uri.hostname ?? '');
413
438
  if (!vhost) {
@@ -464,7 +489,7 @@ async function upgradeHandler(req, socket, https, head) {
464
489
  'uri': uri,
465
490
  'rootPath': vhost.real + now,
466
491
  'urlBase': '/' + now,
467
- 'path': path.slice(('/' + pathList.slice(0, i).join('/')).length + 1),
492
+ 'path': pathList.slice(i + 1).join('/'),
468
493
  })) {
469
494
  return;
470
495
  }
@@ -612,7 +637,9 @@ async function gracefulStop(waitForever = false) {
612
637
  try {
613
638
  session.goaway(http2.constants.NGHTTP2_NO_ERROR);
614
639
  }
615
- catch { }
640
+ catch {
641
+ // --- 忽略已关闭会话的异常 ---
642
+ }
616
643
  }
617
644
  httpServer?.close();
618
645
  http2Server?.close();
@@ -656,14 +683,21 @@ process.on('message', function (msg) {
656
683
  break;
657
684
  }
658
685
  case 'global': {
686
+ if (typeof msg.key !== 'string' || msg.key === '__proto__' || msg.key === 'constructor' || msg.key === 'prototype') {
687
+ break;
688
+ }
659
689
  if (msg.data === undefined || msg.data === null) {
660
690
  delete lCore.global[msg.key];
661
691
  break;
662
692
  }
663
693
  if (msg.key === '__init__') {
664
694
  // --- 初始化 ---
665
- for (const key in msg.data) {
666
- lCore.global[key] = msg.data[key];
695
+ if (typeof msg.data === 'object') {
696
+ for (const key of Object.keys(msg.data)) {
697
+ if (key !== '__proto__' && key !== 'constructor' && key !== 'prototype') {
698
+ lCore.global[key] = msg.data[key];
699
+ }
700
+ }
667
701
  }
668
702
  break;
669
703
  }
@@ -707,17 +741,16 @@ async function getVhostByHostname(hostname) {
707
741
  // --- 完全匹配 ---
708
742
  const real = getVhostReal(vhost.root);
709
743
  if (await lFs.isDir(real)) {
710
- return { ...vhost, real };
744
+ return { ...vhost, 'real': real };
711
745
  }
712
746
  }
713
747
  if (vSub && vGlobal) {
714
- // --- 意味着已经找到了更精准的子域名匹配和全局匹配,后续所有 vhost 和 domain 无需再通过正则或磁盘 IO 进行匹配判断 ---
748
+ // --- 已同时找到通配符与全局匹配,跳过剩余遍历 ---
715
749
  break vhostLoop;
716
750
  }
717
751
  if (domain === '*') {
718
752
  // --- 全局泛匹配 ---
719
753
  if (vGlobal) {
720
- // --- 已经有了全局匹配结果,无需重复赋值,跳过后续逻辑以节省性能 ---
721
754
  continue;
722
755
  }
723
756
  const real = getVhostReal(vhost.root);
@@ -729,7 +762,6 @@ async function getVhostByHostname(hostname) {
729
762
  else if (domain.includes('*')) {
730
763
  // --- 通配符匹配 ---
731
764
  if (vSub) {
732
- // --- 已经存在子域名通配符匹配结果,按照优先级不再寻找其他通配符匹配,跳过正则计算 ---
733
765
  continue;
734
766
  }
735
767
  const pattern = domain.replace(/\./g, '\\.').replace(/\*/g, '[\\w-]+?');