@maiyunnet/kebab 9.26.3 → 9.26.4

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
@@ -4,7 +4,7 @@
4
4
  * --- 本文件用来定义每个目录实体地址的常量 ---
5
5
  */
6
6
  /** --- 当前系统版本号 --- */
7
- export declare const VER = "9.26.3";
7
+ export declare const VER = "9.26.4";
8
8
  /** --- 框架根目录,以 / 结尾 --- */
9
9
  export declare const ROOT_PATH: string;
10
10
  /** --- 框架的 LIB,以 / 结尾 --- */
package/index.js CHANGED
@@ -5,7 +5,7 @@
5
5
  * --- 本文件用来定义每个目录实体地址的常量 ---
6
6
  */
7
7
  /** --- 当前系统版本号 --- */
8
- export const VER = '9.26.3';
8
+ export const VER = '9.26.4';
9
9
  // --- 服务端用的路径 ---
10
10
  const imu = decodeURIComponent(import.meta.url).replace('file://', '').replace(/^\/(\w:)/, '$1');
11
11
  /** --- /xxx/xxx --- */
@@ -0,0 +1,36 @@
1
+ import type * as http from 'http';
2
+ import type * as http2 from 'http2';
3
+ import type { THttpHeaders } from '#kebab/lib/undici.js';
4
+ /** --- undici 和 ws 共用请求头模式,mproxy 仅支持 passthrough 和 request --- */
5
+ export interface IProxyHeaderOptions {
6
+ /**
7
+ * --- 默认 passthrough 透传;request 使用目标 Host 并清理代理来源头;reverse 保留外部 Host 并生成来源头 ---
8
+ * passthrough 使用目标 Host,保留通过基础过滤的来路头,不自动生成来源头。
9
+ * request/reverse 均清理传入的 Forwarded、X-Forwarded-*、Via、X-Real-IP 及 CDN 来源头。
10
+ * reverse 的协议和 IP 仅取实际连接,不信任来路代理头;可信代理信息由业务验证后通过 headers 覆盖。
11
+ */
12
+ 'proxyMode'?: 'passthrough' | 'request' | 'reverse';
13
+ }
14
+ /**
15
+ * --- 过滤代理请求或响应的基础头部,不处理来源信息 ---
16
+ * @param headers 待过滤的头部
17
+ * @param res 传入时直接设置响应头并返回空对象,响应头已发送时不再写入
18
+ * @param filter 业务过滤,返回 true 则保留
19
+ * @returns 过滤后的头部,不修改输入
20
+ */
21
+ export declare const filterProxyHeaders: (headers: http.IncomingHttpHeaders | http2.IncomingHttpHeaders | THttpHeaders, res?: http2.Http2ServerResponse | http.ServerResponse, filter?: (h: string) => boolean) => Record<string, string | string[]>;
22
+ /**
23
+ * --- 构造上游请求头,依次执行基础过滤、业务 filter、模式处理及 headers 覆盖 ---
24
+ * 保留 Cookie、Authorization、Origin、Referer 的业务语义,不保证请求无法被识别为代理。
25
+ * 来源头包括 Forwarded、Via、X-Real-IP、X-Forwarded-*、CF-*、EO-*、True-Client-IP、Client-IP、
26
+ * X-Client-IP、Fastly-Client-IP、X-Cluster-Client-IP、X-Envoy-External-Address、X-Original-Forwarded-For、
27
+ * X-Original-Host、X-Original-URL、X-Rewrite-URL;业务可通过 filter 补充过滤,headers 覆盖优先级最高。
28
+ * reverse 输出 Host、X-Forwarded-Host、X-Forwarded-Proto、X-Forwarded-For、X-Real-IP。
29
+ * @param req 原始 HTTP 或 WebSocket 握手请求
30
+ * @param opt 模式、业务过滤及显式覆盖的头部
31
+ * @returns 独立的上游请求头对象,不修改输入
32
+ */
33
+ export declare const getProxyHeaders: (req: http.IncomingMessage | http2.Http2ServerRequest, opt?: IProxyHeaderOptions & {
34
+ 'headers'?: http.IncomingHttpHeaders;
35
+ filter?: (h: string) => boolean;
36
+ }) => http.IncomingHttpHeaders;
@@ -0,0 +1,21 @@
1
+ import { buildProxyHeaders, filterProxyHeaders as filterProxyHeadersImpl } from '#kebab/abox/proxy.js';
2
+ /**
3
+ * --- 过滤代理请求或响应的基础头部,不处理来源信息 ---
4
+ * @param headers 待过滤的头部
5
+ * @param res 传入时直接设置响应头并返回空对象,响应头已发送时不再写入
6
+ * @param filter 业务过滤,返回 true 则保留
7
+ * @returns 过滤后的头部,不修改输入
8
+ */
9
+ export const filterProxyHeaders = filterProxyHeadersImpl;
10
+ /**
11
+ * --- 构造上游请求头,依次执行基础过滤、业务 filter、模式处理及 headers 覆盖 ---
12
+ * 保留 Cookie、Authorization、Origin、Referer 的业务语义,不保证请求无法被识别为代理。
13
+ * 来源头包括 Forwarded、Via、X-Real-IP、X-Forwarded-*、CF-*、EO-*、True-Client-IP、Client-IP、
14
+ * X-Client-IP、Fastly-Client-IP、X-Cluster-Client-IP、X-Envoy-External-Address、X-Original-Forwarded-For、
15
+ * X-Original-Host、X-Original-URL、X-Rewrite-URL;业务可通过 filter 补充过滤,headers 覆盖优先级最高。
16
+ * reverse 输出 Host、X-Forwarded-Host、X-Forwarded-Proto、X-Forwarded-For、X-Real-IP。
17
+ * @param req 原始 HTTP 或 WebSocket 握手请求
18
+ * @param opt 模式、业务过滤及显式覆盖的头部
19
+ * @returns 独立的上游请求头对象,不修改输入
20
+ */
21
+ export const getProxyHeaders = buildProxyHeaders;
package/lib/undici.d.ts CHANGED
@@ -5,6 +5,7 @@ import * as http2 from 'http2';
5
5
  import * as kebab from '#kebab/index.js';
6
6
  import * as lCookie from '#kebab/lib/cookie.js';
7
7
  import * as sCtr from '#kebab/sys/ctr.js';
8
+ import * as lProxy from '#kebab/lib/undici/proxy.js';
8
9
  import * as lFd from './undici/formdata.js';
9
10
  import * as lRequest from './undici/request.js';
10
11
  import * as lResponse from './undici/response.js';
@@ -89,12 +90,13 @@ export declare function request(u: string, data?: Record<string, kebab.Json> | B
89
90
  */
90
91
  export declare function getFormData(): lFd.FormData;
91
92
  /**
92
- * --- 剔除不代理的 header,返回新的 header ---
93
- * @param headers 剔除前的 header
94
- * @param res 直接设置头部而不返回,可置空
95
- * @param filter 返回 true 则留下
93
+ * --- 过滤代理请求或响应的基础头部,不生成或清理来源信息 ---
94
+ * @param headers 待过滤的头部
95
+ * @param res 传入时直接设置响应头并返回空对象,响应头已发送时不再写入
96
+ * @param filter 业务过滤,返回 true 则保留
97
+ * @returns 过滤后的头部,不修改输入
96
98
  */
97
- export declare function filterHeaders(headers: http.IncomingHttpHeaders | http2.IncomingHttpHeaders | THttpHeaders, res?: http2.Http2ServerResponse | http.ServerResponse, filter?: (h: string) => boolean): Record<string, string | string[]>;
99
+ export declare const filterProxyHeaders: (headers: http.IncomingHttpHeaders | http2.IncomingHttpHeaders | THttpHeaders, res?: http2.Http2ServerResponse | http.ServerResponse, filter?: (h: string) => boolean) => Record<string, string | string[]>;
98
100
  /**
99
101
  * --- 正向 mproxy 代理,注意提前处理不要自动处理 post 数据,读取 get 的 url 为实际请求地址 ---
100
102
  * --- get: url, auth ---
@@ -171,6 +173,8 @@ export interface IRequestOptions {
171
173
  }
172
174
  /** --- 正向代理请求的传入参数选项 --- */
173
175
  export interface IMproxyOptions {
176
+ /** --- 默认 passthrough 保留已有来源头;request 清理来源头;两者均使用目标 Host --- */
177
+ 'proxyMode'?: 'passthrough' | 'request';
174
178
  /** --- 秒数,默认 300 秒 --- */
175
179
  'timeout'?: number;
176
180
  'follow'?: number;
@@ -188,7 +192,7 @@ export interface IMproxyOptions {
188
192
  'reuse'?: string | undici.ProxyAgent | undici.Agent;
189
193
  }
190
194
  /** --- 反向代理请求的传入参数选项 --- */
191
- export interface IRproxyOptions {
195
+ export interface IRproxyOptions extends lProxy.IProxyHeaderOptions {
192
196
  /** --- 秒数,默认 300 秒 --- */
193
197
  'timeout'?: number;
194
198
  'follow'?: number;
package/lib/undici.js CHANGED
@@ -7,6 +7,7 @@ import * as lCrypto from '#kebab/lib/crypto.js';
7
7
  import * as lCore from '#kebab/lib/core.js';
8
8
  import * as lCookie from '#kebab/lib/cookie.js';
9
9
  import * as lZlib from '#kebab/lib/zlib.js';
10
+ import * as lProxy from '#kebab/lib/undici/proxy.js';
10
11
  // --- 自己 ---
11
12
  import * as lFd from './undici/formdata.js';
12
13
  import * as lRequest from './undici/request.js';
@@ -576,45 +577,14 @@ export async function request(u, data, opt = {}) {
576
577
  export function getFormData() {
577
578
  return new lFd.FormData();
578
579
  }
579
- /** --- proxy 要剔除的基础头部 --- */
580
- const proxyContinueHeaders = [
581
- 'host', 'connection', 'keep-alive', 'http-code', 'http-url',
582
- 'transfer-encoding', 'upgrade', 'proxy-connection'
583
- ];
584
580
  /**
585
- * --- 剔除不代理的 header,返回新的 header ---
586
- * @param headers 剔除前的 header
587
- * @param res 直接设置头部而不返回,可置空
588
- * @param filter 返回 true 则留下
581
+ * --- 过滤代理请求或响应的基础头部,不生成或清理来源信息 ---
582
+ * @param headers 待过滤的头部
583
+ * @param res 传入时直接设置响应头并返回空对象,响应头已发送时不再写入
584
+ * @param filter 业务过滤,返回 true 则保留
585
+ * @returns 过滤后的头部,不修改输入
589
586
  */
590
- export function filterHeaders(headers, res, filter) {
591
- const heads = {};
592
- // --- HTTP/2 下响应已发送则跳过 setHeader,避免 ERR_HTTP2_HEADERS_SENT ---
593
- const sent = res ? res.headersSent : false;
594
- for (const h in headers) {
595
- if (proxyContinueHeaders.includes(h)) {
596
- continue;
597
- }
598
- if (h.includes(':') || h.includes('(')) {
599
- continue;
600
- }
601
- const v = headers[h];
602
- if (v === undefined) {
603
- continue;
604
- }
605
- if (filter && !filter(h)) {
606
- continue;
607
- }
608
- if (res) {
609
- if (!sent) {
610
- res.setHeader(h, v);
611
- }
612
- continue;
613
- }
614
- heads[h] = v;
615
- }
616
- return heads;
617
- }
587
+ export const filterProxyHeaders = lProxy.filterProxyHeaders;
618
588
  /**
619
589
  * --- 正向 mproxy 代理,注意提前处理不要自动处理 post 数据,读取 get 的 url 为实际请求地址 ---
620
590
  * --- get: url, auth ---
@@ -640,7 +610,7 @@ export async function mproxy(ctr, auth, opt = {}) {
640
610
  opt.headers ??= {};
641
611
  /** --- 传来的 hosts --- */
642
612
  const hosts = lText.parseJson(get['hosts']);
643
- const headers = Object.assign(filterHeaders(req.headers, undefined, opt.filter), opt.headers);
613
+ const headers = lProxy.getProxyHeaders(req, opt);
644
614
  // --- 发起请求 ---
645
615
  const rres = await request(get['url'], req, {
646
616
  ...opt,
@@ -658,7 +628,7 @@ export async function mproxy(ctr, auth, opt = {}) {
658
628
  // --- HTTP/2 下响应可能已被客户端取消,跳过头部设置避免崩溃 ---
659
629
  if (!res.headersSent) {
660
630
  if (rres.headers) {
661
- filterHeaders(rres.headers, res, opt.filter);
631
+ filterProxyHeaders(rres.headers, res, opt.filter);
662
632
  }
663
633
  lCore.writeHead(res, rres.headers?.['http-code'] ?? 200);
664
634
  await new Promise(resolve => {
@@ -728,7 +698,7 @@ export async function rproxy(ctr, route, opt = {}) {
728
698
  const lpath = path.slice(key.length);
729
699
  opt.method = req.method ?? 'GET';
730
700
  opt.headers ??= {};
731
- const headers = Object.assign(filterHeaders(req.headers, undefined, opt.filter), opt.headers);
701
+ const headers = lProxy.getProxyHeaders(req, opt);
732
702
  // --- 拼接代理目标 URL ---
733
703
  let proxyUrl = route[key] + lpath;
734
704
  // --- 自定义 GET 查询参数(直接替换整个 query string) ---
@@ -755,7 +725,7 @@ export async function rproxy(ctr, route, opt = {}) {
755
725
  // --- HTTP/2 下响应可能已被客户端取消,跳过头部设置避免崩溃 ---
756
726
  if (!res.headersSent) {
757
727
  if (rres.headers) {
758
- filterHeaders(rres.headers, res, opt.filter);
728
+ filterProxyHeaders(rres.headers, res, opt.filter);
759
729
  }
760
730
  /** --- 上游已压缩则不再压缩,避免双重压缩导致乱码 --- */
761
731
  const upstreamEncoded = !!(rres.headers?.['content-encoding']);
package/lib/ws.d.ts CHANGED
@@ -9,6 +9,7 @@ import * as kebab from '#kebab/index.js';
9
9
  import * as lUndici from '#kebab/lib/undici.js';
10
10
  import * as lCookie from '#kebab/lib/cookie.js';
11
11
  import * as sCtr from '#kebab/sys/ctr.js';
12
+ import * as lProxy from '#kebab/lib/undici/proxy.js';
12
13
  /** --- 一般用 SIMPLE --- */
13
14
  export declare enum EFrameReceiveMode {
14
15
  STANDARD = 0,
@@ -65,6 +66,8 @@ export interface ISocketCloseInfo {
65
66
  }
66
67
  /** --- 正向代理请求的传入参数选项 --- */
67
68
  export interface IMproxyOptions {
69
+ /** --- 默认 passthrough 保留已有来源头;request 清理来源头;两者均使用目标 Host --- */
70
+ 'proxyMode'?: 'passthrough' | 'request';
68
71
  /** --- 秒数 --- */
69
72
  'timeout'?: number;
70
73
  /** --- 自定义 host 映射,如 {'www.maiyun.net': '127.0.0.1'},或全部映射到一个 host --- */
@@ -79,7 +82,7 @@ export interface IMproxyOptions {
79
82
  'masking'?: boolean;
80
83
  }
81
84
  /** --- 反向代理请求的传入参数选项 --- */
82
- export interface IRproxyOptions {
85
+ export interface IRproxyOptions extends lProxy.IProxyHeaderOptions {
83
86
  /** --- 秒数 --- */
84
87
  'timeout'?: number;
85
88
  /** --- 自定义 host 映射,如 {'www.maiyun.net': '127.0.0.1'},或全部映射到一个 host --- */
package/lib/ws.js CHANGED
@@ -3,8 +3,8 @@ import * as net from 'net';
3
3
  import WebSocket, { WebSocketServer } from 'ws';
4
4
  import * as lCore from '#kebab/lib/core.js';
5
5
  import * as lText from '#kebab/lib/text.js';
6
- import * as lUndici from '#kebab/lib/undici.js';
7
6
  import * as lCookie from '#kebab/lib/cookie.js';
7
+ import * as lProxy from '#kebab/lib/undici/proxy.js';
8
8
  /** --- 一般用 SIMPLE --- */
9
9
  export var EFrameReceiveMode;
10
10
  (function (EFrameReceiveMode) {
@@ -778,7 +778,7 @@ export async function mproxy(ctr, auth, opt = {}) {
778
778
  return -1;
779
779
  }
780
780
  opt.headers ??= {};
781
- const headers = Object.assign(lUndici.filterHeaders(req.headers, undefined, opt.filter), opt.headers);
781
+ const headers = lProxy.getProxyHeaders(req, opt);
782
782
  // --- 发起请求 ---
783
783
  /** --- 远程端的双向 socket --- */
784
784
  const rsocket = await connect(get['url'], {
@@ -802,7 +802,7 @@ export async function rproxy(ctr, url, opt = {}) {
802
802
  /** --- 请求端产生的双向 socket --- */
803
803
  const socket = ctr.getPrototype('_socket');
804
804
  opt.headers ??= {};
805
- const headers = Object.assign(lUndici.filterHeaders(req.headers, undefined, opt.filter), opt.headers);
805
+ const headers = lProxy.getProxyHeaders(req, opt);
806
806
  // --- 发起请求 ---
807
807
  /** --- 远程端的双向 socket --- */
808
808
  /** --- 目标连接的握手错误 --- */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@maiyunnet/kebab",
3
- "version": "9.26.3",
3
+ "version": "9.26.4",
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": [
@@ -2540,10 +2540,10 @@ error: <pre>${JSON.stringify(res.error, null, 4)}</pre>`);
2540
2540
  'authorization': ''
2541
2541
  };
2542
2542
  echo.push(`origin:<pre>${JSON.stringify(headers, null, 4)}</pre>`);
2543
- const filtered = lUndici.filterHeaders(headers, undefined, h => {
2543
+ const filtered = lUndici.filterProxyHeaders(headers, undefined, h => {
2544
2544
  return !['x-', 'eo-'].some(i => h.startsWith(i));
2545
2545
  });
2546
- echo.push(`const filtered = lUndici.filterHeaders(headers);<pre>${JSON.stringify(filtered, null, 4)}</pre>`);
2546
+ echo.push(`const filtered = lUndici.filterProxyHeaders(headers);<pre>${JSON.stringify(filtered, null, 4)}</pre>`);
2547
2547
  return echo.join('') + this._getEnd();
2548
2548
  }
2549
2549
  async undiciFetch() {