@maiyunnet/kebab 9.23.0 → 9.25.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.
- package/abox/master-vhost.d.ts +2 -0
- package/abox/master-vhost.js +97 -0
- package/abox/request.d.ts +2 -0
- package/abox/request.js +79 -0
- package/doc/kebab-rag.md +156 -96
- package/index.d.ts +10 -6
- package/index.js +1 -1
- package/lib/core.d.ts +10 -0
- package/lib/core.js +35 -0
- package/lib/undici/response.d.ts +1 -1
- package/package.json +1 -1
- package/sys/child.js +0 -6
- package/sys/master.js +33 -0
- package/sys/route.js +15 -5
- package/www/example/ctr/test.d.ts +3 -0
- package/www/example/ctr/test.js +66 -37
- package/www/example/ws/handler.js +7 -1
- package/www/example/ws/mproxy.js +5 -1
- package/www/example/ws/rproxy.js +7 -1
package/index.d.ts
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* --- 本文件用来定义每个目录实体地址的常量 ---
|
|
6
6
|
*/
|
|
7
7
|
/** --- 当前系统版本号 --- */
|
|
8
|
-
export declare const VER = "9.
|
|
8
|
+
export declare const VER = "9.25.0";
|
|
9
9
|
/** --- 框架根目录,以 / 结尾 --- */
|
|
10
10
|
export declare const ROOT_PATH: string;
|
|
11
11
|
/** --- 框架的 LIB,以 / 结尾 --- */
|
|
@@ -155,16 +155,20 @@ export interface IConfigConst {
|
|
|
155
155
|
'mobile': boolean;
|
|
156
156
|
'wechat': boolean;
|
|
157
157
|
'miniprogram': '' | 'wechat';
|
|
158
|
+
/** --- 请求到达 Kebab 时是否使用 HTTPS;通过 HTTP 回源时为 false --- */
|
|
158
159
|
'https': boolean;
|
|
159
|
-
/** ---
|
|
160
|
+
/** --- 请求头中的主机地址,如 example.com、example.com:8443 或 [::1]:8080;不额外补端口 --- */
|
|
160
161
|
'host': string;
|
|
161
|
-
/** ---
|
|
162
|
+
/** --- 主机名,如 example.com 或 ::1,不含端口或 IPv6 方括号 --- */
|
|
162
163
|
'hostname': string;
|
|
163
|
-
/** ---
|
|
164
|
+
/** --- 请求地址中的端口,如 8443;未指定时 HTTPS 为 443、HTTP 为 80,不取本机监听端口 --- */
|
|
164
165
|
'hostport': number;
|
|
165
|
-
/** ---
|
|
166
|
+
/** --- 用于生成网址的主机地址,仅省略 HTTPS 的 443 和 HTTP 的 80 端口,如 example.com:443 → example.com;保留其他端口和 IPv6 方括号 --- */
|
|
166
167
|
'hostCanonical': string;
|
|
167
|
-
/**
|
|
168
|
+
/**
|
|
169
|
+
* --- 网址的协议与 hostCanonical,如 https://example.com:8443 ---
|
|
170
|
+
* 协议可能取自转发头,不能用它判断请求到达 Kebab 时是否使用 HTTPS。
|
|
171
|
+
*/
|
|
168
172
|
'urlOrigin': string;
|
|
169
173
|
'uri': IUrlParse;
|
|
170
174
|
'rootPath': string;
|
package/index.js
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
* --- 本文件用来定义每个目录实体地址的常量 ---
|
|
7
7
|
*/
|
|
8
8
|
/** --- 当前系统版本号 --- */
|
|
9
|
-
export const VER = '9.
|
|
9
|
+
export const VER = '9.25.0';
|
|
10
10
|
// --- 服务端用的路径 ---
|
|
11
11
|
const imu = decodeURIComponent(import.meta.url).replace('file://', '').replace(/^\/(\w:)/, '$1');
|
|
12
12
|
/** --- /xxx/xxx --- */
|
package/lib/core.d.ts
CHANGED
|
@@ -290,6 +290,16 @@ export declare function sendPackage(content: string, hosts?: string[] | 'config'
|
|
|
290
290
|
'result': boolean;
|
|
291
291
|
'return': string;
|
|
292
292
|
}>>;
|
|
293
|
+
/**
|
|
294
|
+
* --- 全量替换各节点 conf/vhost/ 中的文件,不自动 reload ---
|
|
295
|
+
* @param files 文件名与 JSON 原文的非空集合;仅允许直接 .json 文件名,内容为 IVhost 或 IVhost[]
|
|
296
|
+
* @param hosts 节点列表,config 使用全局 hosts;省略或列表为空时更新本机
|
|
297
|
+
* @returns 各节点的写入结果;成功表示目录替换完成,需自行调用 sendReload 生效
|
|
298
|
+
*/
|
|
299
|
+
export declare function sendVhost(files: Record<string, string>, hosts?: string[] | 'config'): Promise<Record<string, {
|
|
300
|
+
'result': boolean;
|
|
301
|
+
'return': string;
|
|
302
|
+
}>>;
|
|
293
303
|
/** --- log 设置的选项 --- */
|
|
294
304
|
export interface ILogOptions {
|
|
295
305
|
'path'?: string;
|
package/lib/core.js
CHANGED
|
@@ -971,6 +971,41 @@ export async function sendPackage(content, hosts) {
|
|
|
971
971
|
}));
|
|
972
972
|
return Object.fromEntries(results);
|
|
973
973
|
}
|
|
974
|
+
/**
|
|
975
|
+
* --- 全量替换各节点 conf/vhost/ 中的文件,不自动 reload ---
|
|
976
|
+
* @param files 文件名与 JSON 原文的非空集合;仅允许直接 .json 文件名,内容为 IVhost 或 IVhost[]
|
|
977
|
+
* @param hosts 节点列表,config 使用全局 hosts;省略或列表为空时更新本机
|
|
978
|
+
* @returns 各节点的写入结果;成功表示目录替换完成,需自行调用 sendReload 生效
|
|
979
|
+
*/
|
|
980
|
+
export async function sendVhost(files, hosts) {
|
|
981
|
+
if (hosts === 'config') {
|
|
982
|
+
hosts = globalConfig.hosts;
|
|
983
|
+
}
|
|
984
|
+
if (!hosts?.length) {
|
|
985
|
+
hosts = ['127.0.0.1'];
|
|
986
|
+
}
|
|
987
|
+
// --- 每个节点独立发送和返回,失败节点可单独重试 ---
|
|
988
|
+
const results = await Promise.all(hosts.map(async (host) => {
|
|
989
|
+
try {
|
|
990
|
+
const res = await lUndici.postJson(`http://${host}:${globalConfig.rpcPort}/${lCrypto.aesEncrypt(lText.stringifyJson({
|
|
991
|
+
'action': 'vhost',
|
|
992
|
+
'time': lTime.stamp(),
|
|
993
|
+
}), globalConfig.rpcSecret)}`, files, {
|
|
994
|
+
'timeout': 30,
|
|
995
|
+
});
|
|
996
|
+
const content = await res.getContent();
|
|
997
|
+
if (!content) {
|
|
998
|
+
return [host, { 'result': false, 'return': 'Network error' }];
|
|
999
|
+
}
|
|
1000
|
+
const str = content.toString();
|
|
1001
|
+
return [host, { 'result': str === 'Done', 'return': str }];
|
|
1002
|
+
}
|
|
1003
|
+
catch {
|
|
1004
|
+
return [host, { 'result': false, 'return': 'Network error' }];
|
|
1005
|
+
}
|
|
1006
|
+
}));
|
|
1007
|
+
return Object.fromEntries(results);
|
|
1008
|
+
}
|
|
974
1009
|
/**
|
|
975
1010
|
* --- 写入文件日志 ---
|
|
976
1011
|
* @param opt 选项
|
package/lib/undici/response.d.ts
CHANGED
|
@@ -31,7 +31,7 @@ export declare class Response {
|
|
|
31
31
|
/**
|
|
32
32
|
* --- 获取响应读取流对象 ---
|
|
33
33
|
*/
|
|
34
|
-
getStream():
|
|
34
|
+
getStream(): (import("undici/types/readable").default & undici.Dispatcher.BodyMixin) | zlib.Gunzip | zlib.Inflate | zlib.BrotliDecompress | null;
|
|
35
35
|
/**
|
|
36
36
|
* --- 获取原生响应读取流对象 ---
|
|
37
37
|
*/
|
package/package.json
CHANGED
package/sys/child.js
CHANGED
|
@@ -235,12 +235,6 @@ async function requestHandler(req, res, https) {
|
|
|
235
235
|
res.end('403 Forbidden');
|
|
236
236
|
return;
|
|
237
237
|
}
|
|
238
|
-
const hostInfo = lText.parseHost(host);
|
|
239
|
-
host = hostInfo.hostname;
|
|
240
|
-
if (lText.isIPv6(host)) {
|
|
241
|
-
host = `[${host}]`;
|
|
242
|
-
}
|
|
243
|
-
host = host + ':' + (req.socket.localPort ?? (https ? '443' : '80'));
|
|
244
238
|
const uri = lText.parseUrl(`http${https ? 's' : ''}://${host}${req.url ?? ''}`);
|
|
245
239
|
/** --- 当前的 vhost 配置文件 --- */
|
|
246
240
|
const vhost = await getVhostByHostname(uri.hostname ?? '');
|
package/sys/master.js
CHANGED
|
@@ -20,12 +20,28 @@ import * as lCrypto from '#kebab/lib/crypto.js';
|
|
|
20
20
|
import * as lTime from '#kebab/lib/time.js';
|
|
21
21
|
import * as lZip from '#kebab/lib/zip.js';
|
|
22
22
|
import * as masterLog from '#kebab/abox/master-log.js';
|
|
23
|
+
import * as masterVhost from '#kebab/abox/master-vhost.js';
|
|
23
24
|
/**
|
|
24
25
|
* --- 解析一行 CSV,保留空字段,并处理字段里的逗号和引号 ---
|
|
25
26
|
* @param line 不含换行符的 CSV 行
|
|
26
27
|
* @returns 按原顺序排列的字段
|
|
27
28
|
*/
|
|
28
29
|
const parseCsvLine = masterLog.parseCsvLine;
|
|
30
|
+
/**
|
|
31
|
+
* --- 串行替换 vhost 目录;先写临时目录,切换失败时恢复旧目录 ---
|
|
32
|
+
* @param directory 固定的 vhost 目录
|
|
33
|
+
* @param files 已通过校验的文件名与 JSON 原文
|
|
34
|
+
* @param report 记录替换、恢复或临时目录清理失败
|
|
35
|
+
* @returns 成功返回 Done;失败返回错误信息,恢复失败时包含备份目录
|
|
36
|
+
*/
|
|
37
|
+
const replaceVhost = masterVhost.replace;
|
|
38
|
+
/**
|
|
39
|
+
* --- 校验非空 vhost 文件集合:仅直接 .json 文件名,不允许路径或大小写重名 ---
|
|
40
|
+
* --- 每份 JSON 为 IVhost 或其数组:domains 为字符串数组,root 为字符串,name/remark 可选字符串 ---
|
|
41
|
+
* @param files POST 中的文件集合
|
|
42
|
+
* @returns true 表示所有文件可写入;false 表示整批拒绝,不修改旧配置
|
|
43
|
+
*/
|
|
44
|
+
const isVhostFiles = masterVhost.isFiles;
|
|
29
45
|
/** --- 当前运行中的子进程列表 --- */
|
|
30
46
|
const workerList = {};
|
|
31
47
|
/** --- 是否已开始停止整个服务 --- */
|
|
@@ -425,6 +441,23 @@ function createRpcListener() {
|
|
|
425
441
|
}
|
|
426
442
|
break;
|
|
427
443
|
}
|
|
444
|
+
case 'vhost': {
|
|
445
|
+
if (req.method !== 'POST') {
|
|
446
|
+
res.end('Invalid method');
|
|
447
|
+
return;
|
|
448
|
+
}
|
|
449
|
+
const body = await sRoute.getPost(req);
|
|
450
|
+
if (body === false || !isVhostFiles(body.raw)) {
|
|
451
|
+
res.end('Invalid vhost files');
|
|
452
|
+
return;
|
|
453
|
+
}
|
|
454
|
+
// --- 仅更新磁盘配置,是否 reload 由调用方决定 ---
|
|
455
|
+
const result = await replaceVhost(kebab.VHOST_CWD, body.raw, (message, error) => {
|
|
456
|
+
lCore.display(`[master] [vhost] ${message}`, error);
|
|
457
|
+
});
|
|
458
|
+
res.end(result);
|
|
459
|
+
return;
|
|
460
|
+
}
|
|
428
461
|
case 'code': {
|
|
429
462
|
// --- 更新 code 代码包 ---
|
|
430
463
|
const rtn = await sRoute.getFormData(req);
|
package/sys/route.js
CHANGED
|
@@ -16,6 +16,14 @@ import * as lWs from '#kebab/lib/ws.js';
|
|
|
16
16
|
import * as lLang from '#kebab/lib/lang.js';
|
|
17
17
|
import * as sCtr from './ctr.js';
|
|
18
18
|
import * as kebab from '#kebab/index.js';
|
|
19
|
+
import { getPublicProtocol as getPublicProtocolImpl } from '#kebab/abox/request.js';
|
|
20
|
+
/**
|
|
21
|
+
* --- 只用于推导公开 URL;不改变真实 TLS 状态,也不验证转发头来源 ---
|
|
22
|
+
* @param req HTTP 或 HTTP/2 请求
|
|
23
|
+
* @param fallback 原协议;Forwarded 首项优先于 X-Forwarded-Proto 首项
|
|
24
|
+
* @returns 对外 URL 协议;ws/wss 保持原值
|
|
25
|
+
*/
|
|
26
|
+
const getPublicProtocol = getPublicProtocolImpl;
|
|
19
27
|
/** --- 已加载的项目配置,按 kebab.json 的文件路径缓存,已补齐全局默认值 --- */
|
|
20
28
|
let kebabConfigs = {};
|
|
21
29
|
/**
|
|
@@ -133,16 +141,18 @@ export async function run(data) {
|
|
|
133
141
|
}
|
|
134
142
|
// --- 复制一份 set,后面改资源地址时就不会改动原来的配置 ---
|
|
135
143
|
config.set = { ...config.set };
|
|
136
|
-
/** ---
|
|
144
|
+
/** --- 公开 URL 协议仅用于生成地址,不改变原请求协议和 TLS 状态 --- */
|
|
145
|
+
const publicProtocol = getPublicProtocol(data.req, data.uri.protocol ?? '');
|
|
146
|
+
/** --- host 不带公开协议的默认端口(HTTP 的 80、HTTPS 的 443)--- */
|
|
137
147
|
let hostCanonical = data.uri.host ?? '';
|
|
138
|
-
if (
|
|
148
|
+
if (publicProtocol === 'https:' && Number(data.uri.port) === 443) {
|
|
139
149
|
hostCanonical = lText.parseHost(hostCanonical).rawHostname;
|
|
140
150
|
}
|
|
141
|
-
else if (
|
|
151
|
+
else if (publicProtocol === 'http:' && Number(data.uri.port) === 80) {
|
|
142
152
|
hostCanonical = lText.parseHost(hostCanonical).rawHostname;
|
|
143
153
|
}
|
|
144
|
-
/** ---
|
|
145
|
-
const urlOrigin = `${
|
|
154
|
+
/** --- 网址开头的公开协议和 host,如 https://example.com --- */
|
|
155
|
+
const urlOrigin = `${publicProtocol}//${hostCanonical}`;
|
|
146
156
|
config.const = {
|
|
147
157
|
'path': data.path,
|
|
148
158
|
'qs': data.uri.query ?? '',
|
|
@@ -2,7 +2,10 @@ import * as fs from 'fs';
|
|
|
2
2
|
import * as kebab from '#kebab/index.js';
|
|
3
3
|
import * as sCtr from '#kebab/sys/ctr.js';
|
|
4
4
|
export default class extends sCtr.Ctr {
|
|
5
|
+
/** --- 示例自调用的地址,使用当前连接的协议和监听端口 --- */
|
|
5
6
|
private _internalUrl;
|
|
7
|
+
/** --- 将示例域名解析到当前服务,避免绕回公网或容器外部 --- */
|
|
8
|
+
private _internalAddress;
|
|
6
9
|
onLoad(): Array<string | number> | boolean;
|
|
7
10
|
onReady(): boolean;
|
|
8
11
|
onUnload(rtn: string | boolean | kebab.DbValue[]): string | boolean | kebab.DbValue[];
|
package/www/example/ctr/test.js
CHANGED
|
@@ -39,21 +39,24 @@ const valibotInputSchema = v.strictObject({
|
|
|
39
39
|
'count': v.pipe(v.string(), v.toNumber(), v.integer()),
|
|
40
40
|
});
|
|
41
41
|
export default class extends sCtr.Ctr {
|
|
42
|
+
/** --- 示例自调用的地址,使用当前连接的协议和监听端口 --- */
|
|
42
43
|
_internalUrl = '';
|
|
44
|
+
/** --- 将示例域名解析到当前服务,避免绕回公网或容器外部 --- */
|
|
45
|
+
_internalAddress = '';
|
|
43
46
|
onLoad() {
|
|
44
47
|
if (this._config.const.hostname !== '127.0.0.1' && this._config.const.hostname !== '172.17.0.1' &&
|
|
45
48
|
this._config.const.hostname !== 'localhost' && !this._config.const.hostname.endsWith('.local.brc-app.com') &&
|
|
46
49
|
!this._config.const.hostname.startsWith('192.168.')) {
|
|
47
50
|
return [0, 'Please use 127.0.0.1 or local to access the file (' + this._config.const.host + ').'];
|
|
48
51
|
}
|
|
49
|
-
const
|
|
50
|
-
if (
|
|
51
|
-
|
|
52
|
-
this._internalUrl = 'http' + (this._config.const.https ? 's' : '') + '://' + realIp + this._config.const.urlBase;
|
|
53
|
-
}
|
|
54
|
-
else {
|
|
55
|
-
this._internalUrl = this._config.const.urlFull;
|
|
52
|
+
const socket = this._req.socket;
|
|
53
|
+
if (!socket.localAddress || !socket.localPort) {
|
|
54
|
+
return [0, 'Request socket is unavailable.'];
|
|
56
55
|
}
|
|
56
|
+
const url = new URL(this._config.const.urlBase, `http${this._config.const.https ? 's' : ''}://${this._config.const.host}`);
|
|
57
|
+
url.port = socket.localPort.toString();
|
|
58
|
+
this._internalUrl = url.href;
|
|
59
|
+
this._internalAddress = socket.localAddress;
|
|
57
60
|
return true;
|
|
58
61
|
}
|
|
59
62
|
onReady() {
|
|
@@ -2093,8 +2096,8 @@ echo[echo.length - 1] = echo[echo.length - 1].slice(0, -4);</pre>`);
|
|
|
2093
2096
|
headers: <pre>${JSON.stringify(res.headers, null, 4)}</pre>
|
|
2094
2097
|
content: <pre>${(await res.getContent())?.toString() ?? 'null'}</pre>
|
|
2095
2098
|
error: ${JSON.stringify(res.error)}`);
|
|
2096
|
-
const res2 = await lUndici.get(this._internalUrl + 'test?abc');
|
|
2097
|
-
echo.push(`<pre>lUndici.get('${this._internalUrl}test?abc');</pre>
|
|
2099
|
+
const res2 = await lUndici.get(this._internalUrl + 'test?abc', { 'hosts': this._internalAddress });
|
|
2100
|
+
echo.push(`<pre>lUndici.get('${this._internalUrl}test?abc', { 'hosts': this._internalAddress });</pre>
|
|
2098
2101
|
headers: <pre>${JSON.stringify(res2.headers, null, 4)}</pre>
|
|
2099
2102
|
content: <pre>${(await res2.getContent())?.toString().slice(0, 500) ?? 'null'}</pre>
|
|
2100
2103
|
error: ${JSON.stringify(res2.error)}`);
|
|
@@ -2112,8 +2115,8 @@ error: ${JSON.stringify(res.error)}
|
|
|
2112
2115
|
}
|
|
2113
2116
|
async undiciPost() {
|
|
2114
2117
|
const echo = [];
|
|
2115
|
-
const res = await lUndici.post(this._internalUrl + 'test/undiciPost1', { 'a': '1', 'b': '2', 'c': ['1', '2', '3'] });
|
|
2116
|
-
echo.push(`<pre>lUndici.post('${this._internalUrl}test/undiciPost1', { 'a': '1', 'b': '2', 'c': ['1', '2', '3'] });</pre>
|
|
2118
|
+
const res = await lUndici.post(this._internalUrl + 'test/undiciPost1', { 'a': '1', 'b': '2', 'c': ['1', '2', '3'] }, { 'hosts': this._internalAddress });
|
|
2119
|
+
echo.push(`<pre>lUndici.post('${this._internalUrl}test/undiciPost1', { 'a': '1', 'b': '2', 'c': ['1', '2', '3'] }, { 'hosts': this._internalAddress });</pre>
|
|
2117
2120
|
headers: <pre>${JSON.stringify(res.headers, null, 4)}</pre>
|
|
2118
2121
|
content: <pre>${(await res.getContent())?.toString() ?? 'null'}</pre>
|
|
2119
2122
|
error: ${JSON.stringify(res.error)}`);
|
|
@@ -2124,8 +2127,8 @@ error: ${JSON.stringify(res.error)}`);
|
|
|
2124
2127
|
}
|
|
2125
2128
|
async undiciPostString() {
|
|
2126
2129
|
const echo = [];
|
|
2127
|
-
const res = await lUndici.post(this._internalUrl + 'test/undiciPostString1', 'HeiHei');
|
|
2128
|
-
echo.push(`<pre>lUndici.post('${this._internalUrl}test/undiciPostString1', 'HeiHei');</pre>
|
|
2130
|
+
const res = await lUndici.post(this._internalUrl + 'test/undiciPostString1', 'HeiHei', { 'hosts': this._internalAddress });
|
|
2131
|
+
echo.push(`<pre>lUndici.post('${this._internalUrl}test/undiciPostString1', 'HeiHei', { 'hosts': this._internalAddress });</pre>
|
|
2129
2132
|
headers: <pre>${JSON.stringify(res.headers, null, 4)}</pre>
|
|
2130
2133
|
content: <pre>${(await res.getContent())?.toString() ?? 'null'}</pre>
|
|
2131
2134
|
error: ${JSON.stringify(res.error)}`);
|
|
@@ -2136,8 +2139,9 @@ error: ${JSON.stringify(res.error)}`);
|
|
|
2136
2139
|
}
|
|
2137
2140
|
async undiciOpen() {
|
|
2138
2141
|
const echo = [];
|
|
2139
|
-
const res = await lUndici.open(this._internalUrl + 'test/undiciPost1')
|
|
2140
|
-
|
|
2142
|
+
const res = await lUndici.open(this._internalUrl + 'test/undiciPost1')
|
|
2143
|
+
.hosts(this._internalAddress).post().data({ 'a': '2', 'b': '0', 'c': ['0', '1', '3'] }).request();
|
|
2144
|
+
echo.push(`<pre>lUndici.open('${this._internalUrl}test/undiciPost1').hosts(this._internalAddress).post().data({ 'a': '2', 'b': '0', 'c': ['0', '1', '3'] }).request();</pre>
|
|
2141
2145
|
headers: <pre>${JSON.stringify(res.headers, null, 4)}</pre>
|
|
2142
2146
|
content: <pre>${(await res.getContent())?.toString() ?? 'null'}</pre>
|
|
2143
2147
|
error: ${JSON.stringify(res.error)}`);
|
|
@@ -2177,13 +2181,13 @@ error: ${JSON.stringify(res.error)}`);
|
|
|
2177
2181
|
await fd.putFile('file', kebab.LIB_PATH + 'net/cacert.pem');
|
|
2178
2182
|
await fd.putFile('multiple', kebab.LIB_PATH + 'net/cacert.pem');
|
|
2179
2183
|
await fd.putFile('multiple', kebab.LIB_PATH + 'net/cacert.pem');
|
|
2180
|
-
const res = await lUndici.post(this._internalUrl + 'test/undici-upload1', fd);
|
|
2184
|
+
const res = await lUndici.post(this._internalUrl + 'test/undici-upload1', fd, { 'hosts': this._internalAddress });
|
|
2181
2185
|
echo.push(`<pre>const fd = lUndici.getFormData();
|
|
2182
2186
|
fd.putString('a', '1');
|
|
2183
2187
|
await fd.putFile('file', kebab.LIB_PATH + 'net/cacert.pem');
|
|
2184
2188
|
await fd.putFile('multiple', kebab.LIB_PATH + 'net/cacert.pem');
|
|
2185
2189
|
await fd.putFile('multiple', kebab.LIB_PATH + 'net/cacert.pem');
|
|
2186
|
-
lUndici.post('${this._internalUrl}test/undici-upload1', fd);</pre>
|
|
2190
|
+
lUndici.post('${this._internalUrl}test/undici-upload1', fd, { 'hosts': this._internalAddress });</pre>
|
|
2187
2191
|
headers: <pre>${JSON.stringify(res.headers, null, 4)}</pre>
|
|
2188
2192
|
content: <pre>${(await res.getContent())?.toString() ?? 'null'}</pre>
|
|
2189
2193
|
error: ${JSON.stringify(res.error)}`);
|
|
@@ -2199,19 +2203,23 @@ error: ${JSON.stringify(res.error)}`);
|
|
|
2199
2203
|
const echo = [];
|
|
2200
2204
|
const cookie = {};
|
|
2201
2205
|
let res = await lUndici.get(this._internalUrl + 'test/undici-cookie1', {
|
|
2206
|
+
'hosts': this._internalAddress,
|
|
2202
2207
|
'cookie': cookie
|
|
2203
2208
|
});
|
|
2204
2209
|
echo.push(`<pre>const cookie = {};
|
|
2205
2210
|
lUndici.get(this._internalUrl + 'test/undici-cookie1', {
|
|
2211
|
+
'hosts': this._internalAddress,
|
|
2206
2212
|
'cookie': cookie
|
|
2207
2213
|
});</pre>
|
|
2208
2214
|
headers: <pre>${lText.htmlescape(JSON.stringify(res.headers, null, 4))}</pre>
|
|
2209
2215
|
content: <pre>${(await res.getContent())?.toString() ?? 'null'}</pre>
|
|
2210
2216
|
cookie: <pre>${JSON.stringify(cookie, null, 4)}</pre><hr>`);
|
|
2211
2217
|
res = await lUndici.get(this._internalUrl + 'test/undici-cookie2', {
|
|
2218
|
+
'hosts': this._internalAddress,
|
|
2212
2219
|
'cookie': cookie
|
|
2213
2220
|
});
|
|
2214
2221
|
echo.push(`<pre>lUndici.get(this._internalUrl + 'test/undici-cookie2', {
|
|
2222
|
+
'hosts': this._internalAddress,
|
|
2215
2223
|
'cookie': cookie
|
|
2216
2224
|
});</pre>
|
|
2217
2225
|
headers: <pre>${JSON.stringify(res.headers, null, 4)}</pre>
|
|
@@ -2219,11 +2227,13 @@ content: <pre>${(await res.getContent())?.toString() ?? 'null'}</pre>`);
|
|
|
2219
2227
|
lCookie.setCookie(cookie, 'custom1', 'abc1', this._config.const.host);
|
|
2220
2228
|
lCookie.setCookie(cookie, 'custom2', 'abc2', '172.17.0.1');
|
|
2221
2229
|
res = await lUndici.get(this._internalUrl + 'test/undici-cookie2', {
|
|
2230
|
+
'hosts': this._internalAddress,
|
|
2222
2231
|
'cookie': cookie
|
|
2223
2232
|
});
|
|
2224
2233
|
echo.push(`<pre>lCookie.setCookie(cookie, 'custom1', 'abc1', ${this._config.const.host});
|
|
2225
2234
|
lCookie.setCookie(cookie, 'custom2', 'abc2', '172.17.0.1');
|
|
2226
2235
|
lUndici.get(this._internalUrl + 'test/undici-cookie2', {
|
|
2236
|
+
'hosts': this._internalAddress,
|
|
2227
2237
|
'cookie': cookie
|
|
2228
2238
|
});</pre>
|
|
2229
2239
|
headers: <pre>${JSON.stringify(res.headers, null, 4)}</pre>
|
|
@@ -2356,12 +2366,14 @@ error: ${JSON.stringify(res.error)}`);
|
|
|
2356
2366
|
'a': '1',
|
|
2357
2367
|
'b': '2'
|
|
2358
2368
|
}, {
|
|
2369
|
+
'hosts': this._internalAddress,
|
|
2359
2370
|
'follow': 5
|
|
2360
2371
|
});
|
|
2361
2372
|
echo.push(`<pre>lUndici.post(this._internalUrl + 'test/undici-follow1', {
|
|
2362
2373
|
'a': '1',
|
|
2363
2374
|
'b': '2'
|
|
2364
2375
|
}, {
|
|
2376
|
+
'hosts': this._internalAddress,
|
|
2365
2377
|
'follow': 5
|
|
2366
2378
|
});</pre>
|
|
2367
2379
|
headers: <pre>${JSON.stringify(res.headers, null, 4)}</pre>
|
|
@@ -2405,11 +2417,13 @@ error: <pre>${JSON.stringify(res.error, null, 4)}</pre>`);
|
|
|
2405
2417
|
const url = `${this._internalUrl}test/undici-retry-source`;
|
|
2406
2418
|
const time = Date.now();
|
|
2407
2419
|
const res = await lUndici.get(url, {
|
|
2420
|
+
'hosts': this._internalAddress,
|
|
2408
2421
|
'retry': 1,
|
|
2409
2422
|
'log': false,
|
|
2410
2423
|
});
|
|
2411
2424
|
const content = await res.getContent();
|
|
2412
2425
|
return `<pre>const res = await lUndici.get('${url}', {
|
|
2426
|
+
'hosts': this._internalAddress,
|
|
2413
2427
|
'retry': 1,
|
|
2414
2428
|
'log': false,
|
|
2415
2429
|
});
|
|
@@ -2443,15 +2457,16 @@ error: <pre>${lText.htmlescape(res.error ? lText.stringifyError(res.error) : 'nu
|
|
|
2443
2457
|
}
|
|
2444
2458
|
async undiciHosts() {
|
|
2445
2459
|
const echo = [];
|
|
2446
|
-
const
|
|
2447
|
-
|
|
2448
|
-
|
|
2449
|
-
|
|
2460
|
+
const url = new URL('test', this._internalUrl);
|
|
2461
|
+
// --- TLS 保留当前域名以校验证书;HTTP 演示替换域名解析 ---
|
|
2462
|
+
if (!this._config.const.https) {
|
|
2463
|
+
url.hostname = 'nodejs.org';
|
|
2464
|
+
}
|
|
2465
|
+
const res = await lUndici.get(url.href, {
|
|
2466
|
+
'hosts': this._internalAddress,
|
|
2450
2467
|
});
|
|
2451
|
-
echo.push(`<pre>lUndici.get('
|
|
2452
|
-
'hosts': {
|
|
2453
|
-
'nodejs.org': '127.0.0.1'
|
|
2454
|
-
}
|
|
2468
|
+
echo.push(`<pre>lUndici.get('${url.href}', {
|
|
2469
|
+
'hosts': '${this._internalAddress}',
|
|
2455
2470
|
});</pre>
|
|
2456
2471
|
headers: <pre>${JSON.stringify(res.headers, null, 4)}</pre>
|
|
2457
2472
|
content: <pre>${lText.htmlescape((await res.getContent())?.toString() ?? '')}</pre>
|
|
@@ -2463,6 +2478,7 @@ error: <pre>${JSON.stringify(res.error, null, 4)}</pre>`);
|
|
|
2463
2478
|
const res = await lUndici.postJson(this._internalUrl + 'test/undici-mproxy2', {
|
|
2464
2479
|
'abc': 'ok',
|
|
2465
2480
|
}, {
|
|
2481
|
+
'hosts': this._internalAddress,
|
|
2466
2482
|
'mproxy': {
|
|
2467
2483
|
'url': this._internalUrl + 'test/undici-mproxy1',
|
|
2468
2484
|
'auth': '123456',
|
|
@@ -2470,6 +2486,7 @@ error: <pre>${JSON.stringify(res.error, null, 4)}</pre>`);
|
|
|
2470
2486
|
}
|
|
2471
2487
|
});
|
|
2472
2488
|
echo.push(`<pre>lUndici.postJson('${this._internalUrl}test/undici-mproxy2', {
|
|
2489
|
+
'hosts': this._internalAddress,
|
|
2473
2490
|
'mproxy': {
|
|
2474
2491
|
'url': '${this._internalUrl}test/undici-mproxy1',
|
|
2475
2492
|
'auth': '123456',
|
|
@@ -2484,7 +2501,7 @@ error: <pre>${JSON.stringify(res.error, null, 4)}</pre>`);
|
|
|
2484
2501
|
async undiciMproxy1() {
|
|
2485
2502
|
const data = lUndici.mproxyData(this);
|
|
2486
2503
|
lCore.debug('Got data', data);
|
|
2487
|
-
const rtn = await lUndici.mproxy(this, '123456');
|
|
2504
|
+
const rtn = await lUndici.mproxy(this, '123456', { 'hosts': this._internalAddress });
|
|
2488
2505
|
if (rtn > 0) {
|
|
2489
2506
|
return false;
|
|
2490
2507
|
}
|
|
@@ -2533,6 +2550,7 @@ error: <pre>${JSON.stringify(res.error, null, 4)}</pre>`);
|
|
|
2533
2550
|
const echo = [];
|
|
2534
2551
|
try {
|
|
2535
2552
|
const res = await lUndici.fetch(this._internalUrl + 'test/undici-fetch1', {
|
|
2553
|
+
'hosts': this._internalAddress,
|
|
2536
2554
|
'method': 'POST',
|
|
2537
2555
|
'headers': {
|
|
2538
2556
|
'content-type': 'application/json',
|
|
@@ -2540,6 +2558,7 @@ error: <pre>${JSON.stringify(res.error, null, 4)}</pre>`);
|
|
|
2540
2558
|
'body': JSON.stringify({ 'test': '123' }),
|
|
2541
2559
|
});
|
|
2542
2560
|
echo.push(`<pre>lUndici.fetch('${this._internalUrl}test/undici-fetch1', {
|
|
2561
|
+
'hosts': this._internalAddress,
|
|
2543
2562
|
'method': 'POST',
|
|
2544
2563
|
'headers': {
|
|
2545
2564
|
'content-type': 'application/json',
|
|
@@ -2555,6 +2574,7 @@ error: <pre>${JSON.stringify(res.error, null, 4)}</pre>`);
|
|
|
2555
2574
|
echo.push(`mproxy:`);
|
|
2556
2575
|
try {
|
|
2557
2576
|
const res = await lUndici.fetch(this._internalUrl + 'test/undici-fetch1', {
|
|
2577
|
+
'hosts': this._internalAddress,
|
|
2558
2578
|
'method': 'POST',
|
|
2559
2579
|
'headers': {
|
|
2560
2580
|
'content-type': 'application/json',
|
|
@@ -2567,6 +2587,7 @@ error: <pre>${JSON.stringify(res.error, null, 4)}</pre>`);
|
|
|
2567
2587
|
},
|
|
2568
2588
|
});
|
|
2569
2589
|
echo.push(`<pre>lUndici.fetch('${this._internalUrl}test/undici-fetch1', {
|
|
2590
|
+
'hosts': this._internalAddress,
|
|
2570
2591
|
'method': 'POST',
|
|
2571
2592
|
'headers': {
|
|
2572
2593
|
'content-type': 'application/json',
|
|
@@ -2593,15 +2614,16 @@ error: <pre>${JSON.stringify(res.error, null, 4)}</pre>`);
|
|
|
2593
2614
|
}
|
|
2594
2615
|
async undiciGetResponseJson() {
|
|
2595
2616
|
const echo = [];
|
|
2596
|
-
const json = await lUndici.getResponseJson(this._internalUrl + 'test/undici-get-response-json1');
|
|
2597
|
-
echo.push(`<pre>const json = await lUndici.getResponseJson('${this._internalUrl}test/undici-get-response-json1');</pre>
|
|
2617
|
+
const json = await lUndici.getResponseJson(this._internalUrl + 'test/undici-get-response-json1', { 'hosts': this._internalAddress });
|
|
2618
|
+
echo.push(`<pre>const json = await lUndici.getResponseJson('${this._internalUrl}test/undici-get-response-json1', { 'hosts': this._internalAddress });</pre>
|
|
2598
2619
|
result: <pre>${lText.htmlescape(JSON.stringify(json, null, 4))}</pre><hr>`);
|
|
2599
|
-
const json2 = await lUndici.getResponseJson(this._internalUrl + 'test/undici-get-response-json2');
|
|
2600
|
-
echo.push(`<pre>const json2 = await lUndici.getResponseJson('${this._internalUrl}test/undici-get-response-json2');</pre>
|
|
2620
|
+
const json2 = await lUndici.getResponseJson(this._internalUrl + 'test/undici-get-response-json2', { 'hosts': this._internalAddress });
|
|
2621
|
+
echo.push(`<pre>const json2 = await lUndici.getResponseJson('${this._internalUrl}test/undici-get-response-json2', { 'hosts': this._internalAddress });</pre>
|
|
2601
2622
|
result: <pre>${lText.htmlescape(JSON.stringify(json2, null, 4))}</pre><hr>`);
|
|
2602
2623
|
/** --- JSON 重试验证方法的执行次数 --- */
|
|
2603
2624
|
let retryJsonHandlerCount = 0;
|
|
2604
2625
|
const json3 = await lUndici.getResponseJson(this._internalUrl + 'test/undici-get-response-json1', {
|
|
2626
|
+
'hosts': this._internalAddress,
|
|
2605
2627
|
'retryJson': 1,
|
|
2606
2628
|
'retryJsonHandler': (value) => {
|
|
2607
2629
|
++retryJsonHandlerCount;
|
|
@@ -2610,6 +2632,7 @@ result: <pre>${lText.htmlescape(JSON.stringify(json2, null, 4))}</pre><hr>`);
|
|
|
2610
2632
|
});
|
|
2611
2633
|
echo.push(`<pre>let retryJsonHandlerCount = 0;
|
|
2612
2634
|
const json3 = await lUndici.getResponseJson('${this._internalUrl}test/undici-get-response-json1', {
|
|
2635
|
+
'hosts': this._internalAddress,
|
|
2613
2636
|
'retryJson': 1,
|
|
2614
2637
|
'retryJsonHandler': (value) => {
|
|
2615
2638
|
++retryJsonHandlerCount;
|
|
@@ -3718,18 +3741,24 @@ setInterval(() => {
|
|
|
3718
3741
|
return echo + '<br>' + this._getEnd();
|
|
3719
3742
|
}
|
|
3720
3743
|
async wsClient() {
|
|
3721
|
-
const
|
|
3722
|
-
|
|
3744
|
+
const url = new URL('test', this._internalUrl);
|
|
3745
|
+
url.protocol = this._config.const.https ? 'wss:' : 'ws:';
|
|
3746
|
+
if (this._get['ac'] === 'hosts' && !this._config.const.https) {
|
|
3747
|
+
url.hostname = 'www.maiyun.net';
|
|
3748
|
+
}
|
|
3749
|
+
const proxyUrl = new URL('mproxy', this._internalUrl);
|
|
3750
|
+
proxyUrl.protocol = url.protocol;
|
|
3751
|
+
const ws = await lWs.connect(url.href, {
|
|
3752
|
+
'hosts': this._internalAddress,
|
|
3723
3753
|
'mproxy': this._get['ac'] === 'mproxy' ? {
|
|
3724
|
-
'url':
|
|
3725
|
-
'auth': '123456'
|
|
3754
|
+
'url': proxyUrl.href,
|
|
3755
|
+
'auth': '123456',
|
|
3726
3756
|
} : undefined,
|
|
3727
|
-
'hosts': (this._get['ac'] === 'hosts') ? '127.0.0.1' : undefined
|
|
3728
3757
|
});
|
|
3729
3758
|
if (!ws) {
|
|
3730
|
-
return
|
|
3759
|
+
return `<div>Connect "${url.href}" failed.</div><br>` + this._getEnd();
|
|
3731
3760
|
}
|
|
3732
|
-
const echo = [
|
|
3761
|
+
const echo = [`<div>Connected "${url.href}".</div>`];
|
|
3733
3762
|
// --- 绑定事件 ---
|
|
3734
3763
|
await new Promise((resolve) => {
|
|
3735
3764
|
(async () => {
|
|
@@ -4,8 +4,14 @@ import * as lWs from '#kebab/lib/ws.js';
|
|
|
4
4
|
export default class extends sCtr.Ctr {
|
|
5
5
|
async onLoad() {
|
|
6
6
|
lCore.debug('WebSocket handle test onLoad.');
|
|
7
|
+
const socket = this._req.socket;
|
|
8
|
+
if (!socket.localAddress || !socket.localPort) {
|
|
9
|
+
return false;
|
|
10
|
+
}
|
|
11
|
+
const url = new URL('test', `ws${this._config.const.uri.protocol === 'wss:' ? 's' : ''}://${this._config.const.host}${this._config.const.urlBase}`);
|
|
12
|
+
url.port = socket.localPort.toString();
|
|
7
13
|
if (this._config.const.path === 'rproxy2') {
|
|
8
|
-
await lWs.rproxy(this,
|
|
14
|
+
await lWs.rproxy(this, url.href, { 'hosts': socket.localAddress });
|
|
9
15
|
}
|
|
10
16
|
return false;
|
|
11
17
|
}
|
package/www/example/ws/mproxy.js
CHANGED
|
@@ -4,7 +4,11 @@ import * as lCore from '#kebab/lib/core.js';
|
|
|
4
4
|
export default class extends sCtr.Ctr {
|
|
5
5
|
async onLoad() {
|
|
6
6
|
lCore.debug('WebSocket mproxy test onLoad.');
|
|
7
|
-
const
|
|
7
|
+
const address = this._req.socket.localAddress;
|
|
8
|
+
if (!address) {
|
|
9
|
+
return false;
|
|
10
|
+
}
|
|
11
|
+
const rtn = await lWs.mproxy(this, '123456', { 'hosts': address });
|
|
8
12
|
if (rtn > 0) {
|
|
9
13
|
return false;
|
|
10
14
|
}
|
package/www/example/ws/rproxy.js
CHANGED
|
@@ -4,7 +4,13 @@ import * as lWs from '#kebab/lib/ws.js';
|
|
|
4
4
|
export default class extends sCtr.Ctr {
|
|
5
5
|
async onLoad() {
|
|
6
6
|
lCore.debug('WebSocket rproxy test onLoad.');
|
|
7
|
-
|
|
7
|
+
const socket = this._req.socket;
|
|
8
|
+
if (!socket.localAddress || !socket.localPort) {
|
|
9
|
+
return false;
|
|
10
|
+
}
|
|
11
|
+
const url = new URL('test', `ws${this._config.const.uri.protocol === 'wss:' ? 's' : ''}://${this._config.const.host}${this._config.const.urlBase}`);
|
|
12
|
+
url.port = socket.localPort.toString();
|
|
13
|
+
if (await lWs.rproxy(this, url.href, { 'hosts': socket.localAddress })) {
|
|
8
14
|
return false;
|
|
9
15
|
}
|
|
10
16
|
return true;
|