@maiyunnet/kebab 3.1.19 → 3.1.20

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.
package/index.d.ts CHANGED
@@ -5,7 +5,7 @@
5
5
  * --- 本文件用来定义每个目录实体地址的常量 ---
6
6
  */
7
7
  /** --- 当前系统版本号 --- */
8
- export declare const VER = "3.1.19";
8
+ export declare const VER = "3.1.20";
9
9
  /** --- 框架根目录,以 / 结尾 --- */
10
10
  export declare const ROOT_PATH: string;
11
11
  export declare const LIB_PATH: string;
package/index.js CHANGED
@@ -6,7 +6,7 @@
6
6
  * --- 本文件用来定义每个目录实体地址的常量 ---
7
7
  */
8
8
  /** --- 当前系统版本号 --- */
9
- export const VER = '3.1.19';
9
+ export const VER = '3.1.20';
10
10
  // --- 服务端用的路径 ---
11
11
  const imu = decodeURIComponent(import.meta.url).replace('file://', '').replace(/^\/(\w:)/, '$1');
12
12
  /** --- /xxx/xxx --- */
package/lib/net.d.ts CHANGED
@@ -79,7 +79,7 @@ export declare function getFormData(): fd.FormData;
79
79
  * @param res 直接设置头部而不返回,可置空
80
80
  * @param filter 返回 true 则留下
81
81
  */
82
- export declare function filterProxyHeaders(headers: http.IncomingHttpHeaders | http2.IncomingHttpHeaders | THttpHeaders, res?: http2.Http2ServerResponse | http.ServerResponse, filter?: (h: string) => boolean): Record<string, string | string[]>;
82
+ export declare function filterHeaders(headers: http.IncomingHttpHeaders | http2.IncomingHttpHeaders | THttpHeaders, res?: http2.Http2ServerResponse | http.ServerResponse, filter?: (h: string) => boolean): Record<string, string | string[]>;
83
83
  /**
84
84
  * --- 正向 mproxy 代理,注意提前处理不要自动处理 post 数据,读取 get 的 url 为实际请求地址 ---
85
85
  * --- get: url, auth ---
@@ -107,7 +107,8 @@ export interface IRequestOptions {
107
107
  /** --- 秒数 --- */
108
108
  'timeout'?: number;
109
109
  'follow'?: number;
110
- 'hosts'?: Record<string, string>;
110
+ /** --- 自定义 host 映射,如 {'www.maiyun.net': '127.0.0.1'},或全部映射到一个 host --- */
111
+ 'hosts'?: Record<string, string> | string;
111
112
  'save'?: string;
112
113
  'local'?: string;
113
114
  'headers'?: THttpHeaders;
@@ -127,7 +128,8 @@ export interface IMproxyOptions {
127
128
  /** --- 秒数 --- */
128
129
  'timeout'?: number;
129
130
  'follow'?: number;
130
- 'hosts'?: Record<string, string>;
131
+ /** --- 自定义 host 映射,如 {'www.maiyun.net': '127.0.0.1'},或全部映射到一个 host --- */
132
+ 'hosts'?: Record<string, string> | string;
131
133
  'local'?: string;
132
134
  'headers'?: THttpHeaders;
133
135
  /** --- 过滤 header,返回 true 则留下 --- */
@@ -140,7 +142,8 @@ export interface IRproxyOptions {
140
142
  /** --- 秒数 --- */
141
143
  'timeout'?: number;
142
144
  'follow'?: number;
143
- 'hosts'?: Record<string, string>;
145
+ /** --- 自定义 host 映射,如 {'www.maiyun.net': '127.0.0.1'},或全部映射到一个 host --- */
146
+ 'hosts'?: Record<string, string> | string;
144
147
  'local'?: string;
145
148
  'headers'?: THttpHeaders;
146
149
  /** --- 过滤 header,返回 true 则留下 --- */
package/lib/net.js CHANGED
@@ -136,7 +136,9 @@ export async function request(u, data, opt = {}) {
136
136
  };
137
137
  return res;
138
138
  }
139
- if (hosts[host] !== undefined && !hosts[host]) {
139
+ if (typeof hosts === 'string' ?
140
+ !hosts :
141
+ (hosts[host] !== undefined && !hosts[host])) {
140
142
  const res = new lResponse.Response(null);
141
143
  res.error = {
142
144
  'name': 'hosts error',
@@ -160,7 +162,7 @@ export async function request(u, data, opt = {}) {
160
162
  'localAddress': local,
161
163
  'ca': ca,
162
164
  'connectionOptions': {
163
- 'remoteHost': hosts[host],
165
+ 'remoteHost': typeof hosts === 'string' ? hosts : hosts[host],
164
166
  },
165
167
  });
166
168
  }
@@ -439,7 +441,7 @@ const proxyContinueHeaders = [
439
441
  * @param res 直接设置头部而不返回,可置空
440
442
  * @param filter 返回 true 则留下
441
443
  */
442
- export function filterProxyHeaders(headers, res, filter) {
444
+ export function filterHeaders(headers, res, filter) {
443
445
  const heads = {};
444
446
  for (const h in headers) {
445
447
  if (proxyContinueHeaders.includes(h)) {
@@ -486,14 +488,17 @@ export async function mproxy(ctr, auth, opt = {}) {
486
488
  }
487
489
  opt.method = req.method ?? 'GET';
488
490
  opt.headers ??= {};
489
- Object.assign(filterProxyHeaders(req.headers, undefined, opt.filter), opt.headers);
491
+ const headers = Object.assign(filterHeaders(req.headers, undefined, opt.filter), opt.headers);
490
492
  // --- 发起请求 ---
491
- const rres = await request(get['url'], req, opt);
493
+ const rres = await request(get['url'], req, {
494
+ ...opt,
495
+ headers
496
+ });
492
497
  if (rres.error) {
493
498
  return -2;
494
499
  }
495
500
  if (rres.headers) {
496
- filterProxyHeaders(rres.headers, res, opt.filter);
501
+ filterHeaders(rres.headers, res, opt.filter);
497
502
  }
498
503
  res.writeHead(rres.headers?.['http-code'] ?? 200);
499
504
  await new Promise((resolve) => {
@@ -543,14 +548,17 @@ export async function rproxy(ctr, route, opt = {}) {
543
548
  const lpath = path.slice(key.length);
544
549
  opt.method = req.method ?? 'GET';
545
550
  opt.headers ??= {};
546
- Object.assign(filterProxyHeaders(req.headers, undefined, opt.filter), opt.headers);
551
+ const headers = Object.assign(filterHeaders(req.headers, undefined, opt.filter), opt.headers);
547
552
  // --- 发起请求 ---
548
- const rres = await request(route[key] + lpath, req, opt);
553
+ const rres = await request(route[key] + lpath, req, {
554
+ ...opt,
555
+ headers
556
+ });
549
557
  if (rres.error) {
550
558
  return false;
551
559
  }
552
560
  if (rres.headers) {
553
- filterProxyHeaders(rres.headers, res, opt.filter);
561
+ filterHeaders(rres.headers, res, opt.filter);
554
562
  }
555
563
  res.writeHead(rres.headers?.['http-code'] ?? 200);
556
564
  await new Promise((resolve) => {
package/lib/ws.d.ts CHANGED
@@ -27,7 +27,8 @@ export declare enum EOpcode {
27
27
  export interface IConnectOptions {
28
28
  /** --- 秒数 --- */
29
29
  'timeout'?: number;
30
- 'hosts'?: Record<string, string>;
30
+ /** --- 自定义 host 映射,如 {'www.maiyun.net': '127.0.0.1'},或全部映射到一个 host --- */
31
+ 'hosts'?: Record<string, string> | string;
31
32
  'local'?: string;
32
33
  'headers'?: lNet.THttpHeaders;
33
34
  /** --- cookie 托管对象 --- */
@@ -46,7 +47,8 @@ export interface IConnectOptions {
46
47
  export interface IMproxyOptions {
47
48
  /** --- 秒数 --- */
48
49
  'timeout'?: number;
49
- 'hosts'?: Record<string, string>;
50
+ /** --- 自定义 host 映射,如 {'www.maiyun.net': '127.0.0.1'},或全部映射到一个 host --- */
51
+ 'hosts'?: Record<string, string> | string;
50
52
  'local'?: string;
51
53
  'headers'?: lNet.THttpHeaders;
52
54
  /** --- 过滤 header,返回 true 则留下 --- */
@@ -60,7 +62,8 @@ export interface IMproxyOptions {
60
62
  export interface IRproxyOptions {
61
63
  /** --- 秒数 --- */
62
64
  'timeout'?: number;
63
- 'hosts'?: Record<string, string>;
65
+ /** --- 自定义 host 映射,如 {'www.maiyun.net': '127.0.0.1'},或全部映射到一个 host --- */
66
+ 'hosts'?: Record<string, string> | string;
64
67
  'local'?: string;
65
68
  'headers'?: lNet.THttpHeaders;
66
69
  /** --- 过滤 header,返回 true 则留下 --- */
package/lib/ws.js CHANGED
@@ -92,7 +92,7 @@ export class Socket {
92
92
  const ca = puri ?
93
93
  puri.protocol === 'wss:' ? await lNet.getCa() : null :
94
94
  uri.protocol === 'wss:' ? await lNet.getCa() : null;
95
- if (!ca && hosts[uri.hostname]) {
95
+ if (!ca && (typeof hosts === 'string' ? hosts : hosts[uri.hostname])) {
96
96
  // --- 没有 ca,但是要设置 额外的 host ---
97
97
  headers['host'] = uri.hostname + (uri.port ? ':' + uri.port : '');
98
98
  }
@@ -106,7 +106,7 @@ export class Socket {
106
106
  }) : uri.path;
107
107
  const cli = ca ?
108
108
  await liws.wssConnect({
109
- 'hostname': hosts[host] ?? host,
109
+ 'hostname': (typeof hosts === 'string' ? hosts : hosts[host]) || host,
110
110
  'port': port,
111
111
  'path': path,
112
112
  'servername': host,
@@ -117,7 +117,7 @@ export class Socket {
117
117
  'ca': ca
118
118
  }) :
119
119
  await liws.wsConnect({
120
- 'hostname': hosts[host] ?? host,
120
+ 'hostname': (typeof hosts === 'string' ? hosts : hosts[host]) || host,
121
121
  'port': port,
122
122
  'path': path,
123
123
  'headers': headers,
@@ -366,10 +366,13 @@ export async function mproxy(ctr, auth, opt = {}) {
366
366
  return -1;
367
367
  }
368
368
  opt.headers ??= {};
369
- Object.assign(lNet.filterProxyHeaders(req.headers, undefined, opt.filter), opt.headers);
369
+ const headers = Object.assign(lNet.filterHeaders(req.headers, undefined, opt.filter), opt.headers);
370
370
  // --- 发起请求 ---
371
371
  /** --- 远程端的双向 socket --- */
372
- const rsocket = await connect(get['url'], opt);
372
+ const rsocket = await connect(get['url'], {
373
+ ...opt,
374
+ headers
375
+ });
373
376
  if (!rsocket) {
374
377
  return -2;
375
378
  }
@@ -387,10 +390,13 @@ export async function rproxy(ctr, url, opt = {}) {
387
390
  /** --- 请求端产生的双向 socket --- */
388
391
  const socket = ctr.getPrototype('_socket');
389
392
  opt.headers ??= {};
390
- Object.assign(lNet.filterProxyHeaders(req.headers, undefined, opt.filter), opt.headers);
393
+ const headers = Object.assign(lNet.filterHeaders(req.headers, undefined, opt.filter), opt.headers);
391
394
  // --- 发起请求 ---
392
395
  /** --- 远程端的双向 socket --- */
393
- const rsocket = await connect(url, opt);
396
+ const rsocket = await connect(url, {
397
+ ...opt,
398
+ headers
399
+ });
394
400
  if (!rsocket) {
395
401
  return false;
396
402
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@maiyunnet/kebab",
3
- "version": "3.1.19",
3
+ "version": "3.1.20",
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": [
@@ -71,6 +71,7 @@ export default class extends sCtr.Ctr {
71
71
  netRproxy(): Promise<string | boolean>;
72
72
  netMproxy(): Promise<string | boolean>;
73
73
  netMproxy1(): Promise<string | boolean>;
74
+ netFilterheaders(): string;
74
75
  scan(): Promise<kebab.Json>;
75
76
  scan1(): Promise<kebab.Json>;
76
77
  scan2(): Promise<kebab.Json>;
@@ -160,6 +160,7 @@ export default class extends sCtr.Ctr {
160
160
  `<br><a href="${this._config.const.urlBase}test/net-hosts">View "test/net-hosts"</a>`,
161
161
  `<br><a href="${this._config.const.urlBase}test/net-rproxy/dist/core.js">View "test/net-rproxy/dist/core.js"</a> <a href="${this._config.const.urlBase}test/net-rproxy/package.json">View "package.json"</a>`,
162
162
  `<br><a href="${this._config.const.urlBase}test/net-mproxy">View "test/net-mproxy"</a>`,
163
+ `<br><a href="${this._config.const.urlBase}test/net-filterheaders">View "test/net-filterheaders"</a>`,
163
164
  '<br><br><b>Scan:</b>',
164
165
  `<br><br><a href="${this._config.const.urlBase}test/scan?s=db">View "test/scan?s=db"</a>`,
165
166
  `<br><a href="${this._config.const.urlBase}test/scan?s=kv">View "test/scan?s=kv"</a>`,
@@ -1813,6 +1814,40 @@ error: <pre>${JSON.stringify(res.error, null, 4)}</pre>`);
1813
1814
  }
1814
1815
  return 'Nothing(' + rtn + ')';
1815
1816
  }
1817
+ netFilterheaders() {
1818
+ const echo = [];
1819
+ const headers = {
1820
+ 'host': 'www.maiyun.net',
1821
+ 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/180.0.0.0 Safari/537.36',
1822
+ 'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
1823
+ 'accept-encoding': 'gzip, deflate',
1824
+ 'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8',
1825
+ 'cache-control': 'no-cache',
1826
+ 'cdn-loop': 'TencentEdgeOne; loops=2',
1827
+ 'eo-connecting-ip': 'xx:xx:xx:xx:xx',
1828
+ 'eo-log-uuid': '102780998859203995',
1829
+ 'pragma': 'no-cache',
1830
+ 'priority': 'u=0, i',
1831
+ 'sec-ch-ua': '"Chromium";v="140", "Not=A?Brand";v="24", "Google Chrome";v="180"',
1832
+ 'sec-ch-ua-mobile': '?0',
1833
+ 'sec-ch-ua-platform': '"Windows"',
1834
+ 'sec-fetch-dest': 'document',
1835
+ 'sec-fetch-mode': 'navigate',
1836
+ 'sec-fetch-site': 'none',
1837
+ 'sec-fetch-user': '?1',
1838
+ 'upgrade-insecure-requests': '1',
1839
+ 'x-forwarded-for': '127.1.2.3',
1840
+ 'x-forwarded-host': 'www.maiyun.net',
1841
+ 'x-forwarded-proto': 'http',
1842
+ 'authorization': ''
1843
+ };
1844
+ echo.push(`origin:<pre>${JSON.stringify(headers, null, 4)}</pre>`);
1845
+ const filtered = lNet.filterHeaders(headers, undefined, h => {
1846
+ return !['x-', 'eo-'].some(i => h.startsWith(i));
1847
+ });
1848
+ echo.push(`const filtered = lNet.filterHeaders(headers);<pre>${JSON.stringify(filtered, null, 4)}</pre>`);
1849
+ return echo.join('') + this._getEnd();
1850
+ }
1816
1851
  async scan() {
1817
1852
  const link = await this._scanLink();
1818
1853
  if (!link) {