@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.
- package/abox/proxy.js +24 -2
- package/abox/response.js +36 -17
- package/abox/session-login.js +1 -13
- package/doc/kebab-rag.md +1522 -1034
- package/index.d.ts +12 -1
- package/index.js +1 -1
- package/lib/ai.d.ts +4 -0
- package/lib/ai.js +32 -16
- package/lib/cookie.d.ts +2 -0
- package/lib/cookie.js +47 -45
- package/lib/core.d.ts +26 -0
- package/lib/core.js +36 -13
- package/lib/cron.js +1 -8
- package/lib/crypto.js +20 -6
- package/lib/db/conn.js +1 -0
- package/lib/fs.js +78 -31
- package/lib/kv.d.ts +182 -79
- package/lib/kv.js +518 -214
- package/lib/s3.d.ts +2 -0
- package/lib/s3.js +14 -27
- package/lib/session.d.ts +7 -0
- package/lib/session.js +23 -2
- package/lib/socket.d.ts +4 -1
- package/lib/socket.js +1 -0
- package/lib/sql.d.ts +0 -5
- package/lib/sql.js +48 -12
- package/lib/text.d.ts +10 -5
- package/lib/text.js +22 -12
- package/lib/turnstile.d.ts +6 -0
- package/lib/turnstile.js +16 -9
- package/lib/undici.d.ts +7 -1
- package/lib/undici.js +72 -36
- package/lib/ws.d.ts +1 -1
- package/lib/ws.js +85 -128
- package/lib/zip.js +46 -45
- package/package.json +2 -2
- package/sys/child.js +178 -146
- package/sys/cmd.js +196 -197
- package/sys/ctr.d.ts +14 -14
- package/sys/ctr.js +145 -59
- package/sys/master.js +111 -80
- package/sys/mod.d.ts +8 -2
- package/sys/mod.js +23 -11
- package/sys/monitor/files.d.ts +2 -1
- package/sys/monitor/files.js +77 -21
- package/sys/monitor/watchdog.d.ts +7 -0
- package/sys/monitor/watchdog.js +311 -203
- package/sys/monitor.d.ts +59 -54
- package/sys/monitor.js +742 -681
- package/sys/route.d.ts +4 -4
- package/sys/route.js +93 -52
- package/www/example/ctr/test.js +8 -8
package/lib/undici.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import * as undici from 'undici';
|
|
2
1
|
import * as stream from 'stream';
|
|
3
2
|
import * as http from 'http';
|
|
4
3
|
import * as http2 from 'http2';
|
|
4
|
+
import * as undici from 'undici';
|
|
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';
|
|
@@ -203,10 +203,13 @@ export interface IMproxyOptions {
|
|
|
203
203
|
'proxyMode'?: 'passthrough' | 'request';
|
|
204
204
|
/** --- 秒数,默认 300 秒 --- */
|
|
205
205
|
'timeout'?: number;
|
|
206
|
+
/** --- 追踪 location 次数,0 为不追踪,默认为 0 --- */
|
|
206
207
|
'follow'?: number;
|
|
207
208
|
/** --- 自定义 host 映射,如 {'www.maiyun.net': '127.0.0.1'},或全部映射到一个 host --- */
|
|
208
209
|
'hosts'?: Record<string, string> | string;
|
|
210
|
+
/** --- 使用的本地网卡 IP --- */
|
|
209
211
|
'local'?: string;
|
|
212
|
+
/** --- 自定义请求头 --- */
|
|
210
213
|
'headers'?: THttpHeaders;
|
|
211
214
|
/** --- 空闲连接允许复用的最长秒数;非正数或无效值按未设置处理;默认无服务端提示时为 4 秒,有提示时采用提示值减 2 秒且最多 600 秒 --- */
|
|
212
215
|
'keepAliveTimeout'?: number;
|
|
@@ -221,10 +224,13 @@ export interface IMproxyOptions {
|
|
|
221
224
|
export interface IRproxyOptions extends lProxy.IProxyHeaderOptions {
|
|
222
225
|
/** --- 秒数,默认 300 秒 --- */
|
|
223
226
|
'timeout'?: number;
|
|
227
|
+
/** --- 追踪 location 次数,0 为不追踪,默认为 0 --- */
|
|
224
228
|
'follow'?: number;
|
|
225
229
|
/** --- 自定义 host 映射,如 {'www.maiyun.net': '127.0.0.1'},或全部映射到一个 host --- */
|
|
226
230
|
'hosts'?: Record<string, string> | string;
|
|
231
|
+
/** --- 使用的本地网卡 IP --- */
|
|
227
232
|
'local'?: string;
|
|
233
|
+
/** --- 自定义请求头 --- */
|
|
228
234
|
'headers'?: THttpHeaders;
|
|
229
235
|
/** --- 空闲连接允许复用的最长秒数;非正数或无效值按未设置处理;默认无服务端提示时为 4 秒,有提示时采用提示值减 2 秒且最多 600 秒 --- */
|
|
230
236
|
'keepAliveTimeout'?: number;
|
package/lib/undici.js
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
|
-
|
|
1
|
+
// --- 系统 ---
|
|
2
2
|
import * as stream from 'stream';
|
|
3
3
|
import * as dns from 'dns';
|
|
4
|
+
import * as crypto from 'crypto';
|
|
5
|
+
// --- 第三方 ---
|
|
6
|
+
import * as undici from 'undici';
|
|
4
7
|
import * as lFs from '#kebab/lib/fs.js';
|
|
5
8
|
import * as lText from '#kebab/lib/text.js';
|
|
6
9
|
import * as lCrypto from '#kebab/lib/crypto.js';
|
|
@@ -151,8 +154,7 @@ export async function get(u, opt = {}) {
|
|
|
151
154
|
* @param opt 参数
|
|
152
155
|
*/
|
|
153
156
|
export async function post(u, data, opt = {}) {
|
|
154
|
-
opt
|
|
155
|
-
return request(u, data, opt);
|
|
157
|
+
return request(u, data, { ...opt, 'method': 'POST' });
|
|
156
158
|
}
|
|
157
159
|
/**
|
|
158
160
|
* --- 发起 JSON 请求 ---
|
|
@@ -161,9 +163,7 @@ export async function post(u, data, opt = {}) {
|
|
|
161
163
|
* @param opt 选项
|
|
162
164
|
*/
|
|
163
165
|
export async function postJson(u, data, opt = {}) {
|
|
164
|
-
opt
|
|
165
|
-
opt.type = 'json';
|
|
166
|
-
return request(u, data, opt);
|
|
166
|
+
return request(u, data, { ...opt, 'method': 'POST', 'type': 'json' });
|
|
167
167
|
}
|
|
168
168
|
/**
|
|
169
169
|
* --- 发起请求并解析 JSON 响应,解析失败或重试验证未通过时重新请求 ---
|
|
@@ -202,13 +202,10 @@ async function requestResponseJson(u, data, opt, action) {
|
|
|
202
202
|
return false;
|
|
203
203
|
}
|
|
204
204
|
export async function postJsonResponseJson(u, data, opt = {}) {
|
|
205
|
-
opt
|
|
206
|
-
opt.type = 'json';
|
|
207
|
-
return requestResponseJson(u, data, opt, 'POSTJSONRESPONSEJSON');
|
|
205
|
+
return requestResponseJson(u, data, { ...opt, 'method': 'POST', 'type': 'json' }, 'POSTJSONRESPONSEJSON');
|
|
208
206
|
}
|
|
209
207
|
export async function postResponseJson(u, data, opt = {}) {
|
|
210
|
-
opt
|
|
211
|
-
return requestResponseJson(u, data, opt, 'POSTRESPONSEJSON');
|
|
208
|
+
return requestResponseJson(u, data, { ...opt, 'method': 'POST' }, 'POSTRESPONSEJSON');
|
|
212
209
|
}
|
|
213
210
|
export async function getResponseJson(u, opt = {}) {
|
|
214
211
|
return requestResponseJson(u, undefined, opt, 'GETRESPONSEJSON');
|
|
@@ -361,7 +358,7 @@ function buildDnsLookup(hosts) {
|
|
|
361
358
|
return undefined;
|
|
362
359
|
}
|
|
363
360
|
return (hostname, opts, cb) => {
|
|
364
|
-
const override = typeof hosts === 'string' ? hosts : hosts[hostname];
|
|
361
|
+
const override = typeof hosts === 'string' ? hosts : (Object.hasOwn(hosts, hostname) ? hosts[hostname] : undefined);
|
|
365
362
|
if (override) {
|
|
366
363
|
cb(null, [{ 'address': override, 'family': lText.isIPv4(override) ? 4 : 6 }]);
|
|
367
364
|
}
|
|
@@ -439,7 +436,7 @@ export async function request(u, data, opt = {}) {
|
|
|
439
436
|
}
|
|
440
437
|
if (typeof hosts === 'string' ?
|
|
441
438
|
!hosts :
|
|
442
|
-
(hosts
|
|
439
|
+
(Object.hasOwn(hosts, hostname) && !hosts[hostname])) {
|
|
443
440
|
const res = new lResponse.Response(null);
|
|
444
441
|
res.error = {
|
|
445
442
|
'name': 'hosts error',
|
|
@@ -517,22 +514,43 @@ export async function request(u, data, opt = {}) {
|
|
|
517
514
|
}
|
|
518
515
|
// --- 哦,要追踪 ---
|
|
519
516
|
const nextUrl = lText.urlResolve(u, req.headers['location']);
|
|
520
|
-
|
|
517
|
+
// --- 仅允许跟随 http/https 跳转,禁止跳向非 HTTP 协议 ---
|
|
518
|
+
let nextProtocol = '';
|
|
519
|
+
try {
|
|
520
|
+
nextProtocol = new URL(nextUrl).protocol;
|
|
521
|
+
}
|
|
522
|
+
catch {
|
|
523
|
+
return res;
|
|
524
|
+
}
|
|
525
|
+
if (nextProtocol !== 'http:' && nextProtocol !== 'https:') {
|
|
526
|
+
return res;
|
|
527
|
+
}
|
|
528
|
+
const sameOrigin = lText.isSameOrigin(u, nextUrl);
|
|
529
|
+
let nextMethod = method;
|
|
530
|
+
let nextData = data;
|
|
531
|
+
let nextHosts = hosts;
|
|
532
|
+
if (sameOrigin) {
|
|
521
533
|
headers['referer'] = u;
|
|
522
534
|
}
|
|
523
535
|
else {
|
|
524
|
-
// ---
|
|
536
|
+
// --- 跨域跳转不得携带来源站点凭据、hosts 覆盖与敏感请求体 ---
|
|
525
537
|
delete headers['authorization'];
|
|
526
538
|
delete headers['proxy-authorization'];
|
|
527
539
|
delete headers['cookie'];
|
|
528
540
|
delete headers['referer'];
|
|
541
|
+
nextHosts = {};
|
|
542
|
+
if (nextMethod !== 'GET') {
|
|
543
|
+
delete headers['content-type'];
|
|
544
|
+
delete headers['content-length'];
|
|
545
|
+
nextData = undefined;
|
|
546
|
+
}
|
|
529
547
|
}
|
|
530
|
-
let nextMethod = method;
|
|
531
|
-
let nextData = data;
|
|
532
548
|
const status = res.headers['http-code'];
|
|
533
549
|
if (status === 303 || ((status === 301 || status === 302) && method === 'POST')) {
|
|
534
550
|
nextMethod = 'GET';
|
|
535
551
|
nextData = undefined;
|
|
552
|
+
delete headers['content-type'];
|
|
553
|
+
delete headers['content-length'];
|
|
536
554
|
}
|
|
537
555
|
if (nextData instanceof stream.Readable) {
|
|
538
556
|
return res;
|
|
@@ -541,14 +559,12 @@ export async function request(u, data, opt = {}) {
|
|
|
541
559
|
await req.body.dump();
|
|
542
560
|
return request(nextUrl, nextData, {
|
|
543
561
|
...opt,
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
'headers': headers,
|
|
551
|
-
},
|
|
562
|
+
'method': nextMethod,
|
|
563
|
+
'type': type,
|
|
564
|
+
'timeout': timeout,
|
|
565
|
+
'follow': follow - 1,
|
|
566
|
+
'hosts': nextHosts,
|
|
567
|
+
'headers': headers,
|
|
552
568
|
});
|
|
553
569
|
}
|
|
554
570
|
/**
|
|
@@ -580,22 +596,41 @@ export async function mproxy(ctr, auth, opt = {}) {
|
|
|
580
596
|
if (req.readableEnded) {
|
|
581
597
|
return -4;
|
|
582
598
|
}
|
|
583
|
-
|
|
599
|
+
const suppliedAuth = get['auth'];
|
|
600
|
+
if (!auth || typeof suppliedAuth !== 'string') {
|
|
601
|
+
return 0;
|
|
602
|
+
}
|
|
603
|
+
const authBuf = Buffer.from(auth);
|
|
604
|
+
const suppliedBuf = Buffer.from(suppliedAuth);
|
|
605
|
+
if (authBuf.length !== suppliedBuf.length || !crypto.timingSafeEqual(authBuf, suppliedBuf)) {
|
|
584
606
|
return 0;
|
|
585
607
|
}
|
|
586
|
-
|
|
608
|
+
const targetUrl = get['url'];
|
|
609
|
+
if (typeof targetUrl !== 'string' || !targetUrl) {
|
|
587
610
|
return -1;
|
|
588
611
|
}
|
|
589
|
-
|
|
590
|
-
|
|
612
|
+
try {
|
|
613
|
+
const parsedTarget = new URL(targetUrl);
|
|
614
|
+
if (parsedTarget.protocol !== 'http:' && parsedTarget.protocol !== 'https:') {
|
|
615
|
+
return -1;
|
|
616
|
+
}
|
|
617
|
+
}
|
|
618
|
+
catch {
|
|
619
|
+
return -1;
|
|
620
|
+
}
|
|
621
|
+
const proxyOpt = {
|
|
622
|
+
...opt,
|
|
623
|
+
'method': (req.method ?? 'GET'),
|
|
624
|
+
'headers': opt.headers ?? {},
|
|
625
|
+
};
|
|
591
626
|
/** --- 传来的 hosts --- */
|
|
592
|
-
const hosts = lText.parseJson(get['hosts']);
|
|
627
|
+
const hosts = typeof get['hosts'] === 'string' ? lText.parseJson(get['hosts']) : null;
|
|
593
628
|
const headers = lProxy.getProxyHeaders(req, opt);
|
|
594
629
|
// --- 发起请求 ---
|
|
595
|
-
const rres = await request(
|
|
596
|
-
...
|
|
630
|
+
const rres = await request(targetUrl, req, {
|
|
631
|
+
...proxyOpt,
|
|
597
632
|
'hosts': lText.logicalOr(hosts, opt.hosts),
|
|
598
|
-
headers,
|
|
633
|
+
'headers': headers,
|
|
599
634
|
});
|
|
600
635
|
const stream = rres.getRawStream();
|
|
601
636
|
if (!stream) {
|
|
@@ -701,10 +736,12 @@ export async function rproxy(ctr, route, opt = {}) {
|
|
|
701
736
|
catch {
|
|
702
737
|
return false;
|
|
703
738
|
}
|
|
704
|
-
// --- 发起请求(opt.body 优先于原始 req
|
|
739
|
+
// --- 发起请求(opt.body 优先于原始 req 流;反代强制不自动跨源跟随重定向,防止上游 302 SSRF) ---
|
|
705
740
|
const rres = await request(proxyUrl, opt.body ?? req, {
|
|
706
741
|
...opt,
|
|
707
|
-
|
|
742
|
+
'method': (req.method ?? 'GET'),
|
|
743
|
+
'follow': 0,
|
|
744
|
+
'headers': headers,
|
|
708
745
|
});
|
|
709
746
|
const stream = rres.getRawStream();
|
|
710
747
|
if (!stream) {
|
|
@@ -757,4 +794,3 @@ export async function rproxy(ctr, route, opt = {}) {
|
|
|
757
794
|
}
|
|
758
795
|
return false;
|
|
759
796
|
}
|
|
760
|
-
/* eslint-enable */
|
package/lib/ws.d.ts
CHANGED
|
@@ -229,7 +229,7 @@ export declare class Socket {
|
|
|
229
229
|
get isServer(): boolean;
|
|
230
230
|
/** --- 发送 ping --- */
|
|
231
231
|
ping(data?: Buffer | string): boolean;
|
|
232
|
-
/** --- 发送
|
|
232
|
+
/** --- 发送 pong --- */
|
|
233
233
|
pong(data?: Buffer | string): boolean;
|
|
234
234
|
/**
|
|
235
235
|
* --- 发送控制帧 ---
|
package/lib/ws.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import * as net from 'net';
|
|
2
|
+
import * as crypto from 'crypto';
|
|
2
3
|
// --- 第三方 ---
|
|
3
4
|
import WebSocket, { WebSocketServer } from 'ws';
|
|
4
5
|
import * as lCore from '#kebab/lib/core.js';
|
|
@@ -137,7 +138,9 @@ export class Socket {
|
|
|
137
138
|
}
|
|
138
139
|
const host = puri?.hostname ?? uri.hostname ?? '';
|
|
139
140
|
/** --- 真正的连接远程 IP / HOST --- */
|
|
140
|
-
const rhost = typeof hosts === 'string'
|
|
141
|
+
const rhost = typeof hosts === 'string'
|
|
142
|
+
? hosts
|
|
143
|
+
: (Object.hasOwn(hosts, host) ? hosts[host] : undefined);
|
|
141
144
|
if (rhost) {
|
|
142
145
|
headers['host'] ??= connectUrl.host;
|
|
143
146
|
connectUrl.hostname = lText.isIPv6(rhost) ? `[${rhost}]` : rhost;
|
|
@@ -504,7 +507,7 @@ export class Socket {
|
|
|
504
507
|
ping(data) {
|
|
505
508
|
return this._sendControl('ping', data);
|
|
506
509
|
}
|
|
507
|
-
/** --- 发送
|
|
510
|
+
/** --- 发送 pong --- */
|
|
508
511
|
pong(data) {
|
|
509
512
|
return this._sendControl('pong', data);
|
|
510
513
|
}
|
|
@@ -604,36 +607,24 @@ function getPipeCloseReason(info) {
|
|
|
604
607
|
*/
|
|
605
608
|
function bindPipe(s1, s2, closeReason) {
|
|
606
609
|
return new Promise(resolve => {
|
|
607
|
-
/** --- 是否已经完成关闭,防止双向 close 重复处理 --- */
|
|
608
610
|
let closed = false;
|
|
609
|
-
/** ---
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
/** --- 目标侧最后一个错误 --- */
|
|
616
|
-
let targetError;
|
|
617
|
-
/** --- 来源侧 WebSocket 关闭信息 --- */
|
|
618
|
-
let sourceCloseInfo;
|
|
619
|
-
/** --- 目标侧 WebSocket 关闭信息 --- */
|
|
620
|
-
let targetCloseInfo;
|
|
621
|
-
/** --- 销毁已断开侧,并将原因通过关闭帧回传另一侧 --- */
|
|
611
|
+
/** --- 记录来源侧和目标侧关闭前的底层事件与错误 --- */
|
|
612
|
+
const state = {
|
|
613
|
+
'source': { 'event': 'close' },
|
|
614
|
+
'target': { 'event': 'close' },
|
|
615
|
+
};
|
|
616
|
+
// --- 任一侧关闭时,销毁该侧并将原因通过关闭帧通知另一侧 ---
|
|
622
617
|
const close = (side) => {
|
|
623
618
|
if (closed) {
|
|
624
619
|
return;
|
|
625
620
|
}
|
|
626
621
|
closed = true;
|
|
627
|
-
const
|
|
628
|
-
|
|
629
|
-
'event': sourceEvent,
|
|
630
|
-
'error': sourceError,
|
|
631
|
-
...sourceCloseInfo,
|
|
632
|
-
} : {
|
|
622
|
+
const cur = state[side];
|
|
623
|
+
const info = {
|
|
633
624
|
'side': side,
|
|
634
|
-
'event':
|
|
635
|
-
'error':
|
|
636
|
-
...
|
|
625
|
+
'event': cur.event,
|
|
626
|
+
'error': cur.error,
|
|
627
|
+
...cur.close,
|
|
637
628
|
};
|
|
638
629
|
const code = isSendableCloseCode(info.code) ? info.code : 1011;
|
|
639
630
|
let reason;
|
|
@@ -654,110 +645,62 @@ function bindPipe(s1, s2, closeReason) {
|
|
|
654
645
|
}
|
|
655
646
|
resolve(info);
|
|
656
647
|
};
|
|
657
|
-
// ---
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
case EOpcode.BINARY: {
|
|
667
|
-
if (!s2.writeBinary(msg.data)) {
|
|
668
|
-
s1.pause();
|
|
648
|
+
// --- 双向转发帧、背压与关闭事件 ---
|
|
649
|
+
const forward = (from, to, side) => {
|
|
650
|
+
from.on('message', (msg) => {
|
|
651
|
+
switch (msg.opcode) {
|
|
652
|
+
case EOpcode.TEXT: {
|
|
653
|
+
if (!to.writeText(msg.data)) {
|
|
654
|
+
from.pause();
|
|
655
|
+
}
|
|
656
|
+
break;
|
|
669
657
|
}
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
}
|
|
676
|
-
case EOpcode.PING: {
|
|
677
|
-
if (!s2.ping(msg.data)) {
|
|
678
|
-
s1.pause();
|
|
658
|
+
case EOpcode.BINARY: {
|
|
659
|
+
if (!to.writeBinary(msg.data)) {
|
|
660
|
+
from.pause();
|
|
661
|
+
}
|
|
662
|
+
break;
|
|
679
663
|
}
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
if (!s2.pong(msg.data)) {
|
|
684
|
-
s1.pause();
|
|
664
|
+
case EOpcode.CLOSE: {
|
|
665
|
+
close(side);
|
|
666
|
+
break;
|
|
685
667
|
}
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
}
|
|
692
|
-
}).on('error', (error) => {
|
|
693
|
-
if (sourceEvent !== 'timeout') {
|
|
694
|
-
sourceEvent = 'error';
|
|
695
|
-
}
|
|
696
|
-
sourceError = error;
|
|
697
|
-
}).on('end', () => {
|
|
698
|
-
if (sourceEvent === 'close') {
|
|
699
|
-
sourceEvent = 'end';
|
|
700
|
-
}
|
|
701
|
-
}).on('timeout', () => {
|
|
702
|
-
sourceEvent = 'timeout';
|
|
703
|
-
}).on('close', (info) => {
|
|
704
|
-
sourceCloseInfo = info;
|
|
705
|
-
close('source');
|
|
706
|
-
}).on('drain', () => {
|
|
707
|
-
s2.resume();
|
|
708
|
-
});
|
|
709
|
-
// --- 监听远程端的 ---
|
|
710
|
-
s2.on('message', (msg) => {
|
|
711
|
-
switch (msg.opcode) {
|
|
712
|
-
case EOpcode.TEXT: {
|
|
713
|
-
if (!s1.writeText(msg.data)) {
|
|
714
|
-
s2.pause();
|
|
668
|
+
case EOpcode.PING: {
|
|
669
|
+
if (!to.ping(msg.data)) {
|
|
670
|
+
from.pause();
|
|
671
|
+
}
|
|
672
|
+
break;
|
|
715
673
|
}
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
674
|
+
case EOpcode.PONG: {
|
|
675
|
+
if (!to.pong(msg.data)) {
|
|
676
|
+
from.pause();
|
|
677
|
+
}
|
|
678
|
+
break;
|
|
721
679
|
}
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
case EOpcode.CLOSE: {
|
|
725
|
-
close('target');
|
|
726
|
-
break;
|
|
727
|
-
}
|
|
728
|
-
case EOpcode.PING: {
|
|
729
|
-
if (!s1.ping(msg.data)) {
|
|
730
|
-
s2.pause();
|
|
680
|
+
default: {
|
|
681
|
+
// --- EOpcode.CONTINUATION ---
|
|
731
682
|
}
|
|
732
|
-
break;
|
|
733
683
|
}
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
}
|
|
738
|
-
break;
|
|
684
|
+
}).on('error', (error) => {
|
|
685
|
+
if (state[side].event !== 'timeout') {
|
|
686
|
+
state[side].event = 'error';
|
|
739
687
|
}
|
|
740
|
-
|
|
741
|
-
|
|
688
|
+
state[side].error = error;
|
|
689
|
+
}).on('end', () => {
|
|
690
|
+
if (state[side].event === 'close') {
|
|
691
|
+
state[side].event = 'end';
|
|
742
692
|
}
|
|
743
|
-
}
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
targetEvent = 'timeout';
|
|
755
|
-
}).on('close', (info) => {
|
|
756
|
-
targetCloseInfo = info;
|
|
757
|
-
close('target');
|
|
758
|
-
}).on('drain', () => {
|
|
759
|
-
s1.resume();
|
|
760
|
-
});
|
|
693
|
+
}).on('timeout', () => {
|
|
694
|
+
state[side].event = 'timeout';
|
|
695
|
+
}).on('close', (info) => {
|
|
696
|
+
state[side].close = info;
|
|
697
|
+
close(side);
|
|
698
|
+
}).on('drain', () => {
|
|
699
|
+
to.resume();
|
|
700
|
+
});
|
|
701
|
+
};
|
|
702
|
+
forward(s1, s2, 'source');
|
|
703
|
+
forward(s2, s1, 'target');
|
|
761
704
|
});
|
|
762
705
|
}
|
|
763
706
|
/**
|
|
@@ -773,19 +716,34 @@ export async function mproxy(ctr, auth, opt = {}) {
|
|
|
773
716
|
const socket = ctr.getPrototype('_socket');
|
|
774
717
|
/** --- 客户端请求中的 get 的数据 --- */
|
|
775
718
|
const get = ctr.getPrototype('_get');
|
|
776
|
-
|
|
719
|
+
const suppliedAuth = get['auth'];
|
|
720
|
+
if (!auth || typeof suppliedAuth !== 'string') {
|
|
721
|
+
return 0;
|
|
722
|
+
}
|
|
723
|
+
const authBuf = Buffer.from(auth);
|
|
724
|
+
const suppliedBuf = Buffer.from(suppliedAuth);
|
|
725
|
+
if (authBuf.length !== suppliedBuf.length || !crypto.timingSafeEqual(authBuf, suppliedBuf)) {
|
|
777
726
|
return 0;
|
|
778
727
|
}
|
|
779
|
-
|
|
728
|
+
const targetUrl = get['url'];
|
|
729
|
+
if (typeof targetUrl !== 'string' || !targetUrl) {
|
|
780
730
|
return -1;
|
|
781
731
|
}
|
|
782
|
-
|
|
783
|
-
|
|
732
|
+
try {
|
|
733
|
+
const parsedTarget = new URL(targetUrl);
|
|
734
|
+
if (parsedTarget.protocol !== 'ws:' && parsedTarget.protocol !== 'wss:') {
|
|
735
|
+
return -1;
|
|
736
|
+
}
|
|
737
|
+
}
|
|
738
|
+
catch {
|
|
739
|
+
return -1;
|
|
740
|
+
}
|
|
741
|
+
const headers = lProxy.getProxyHeaders(req, { ...opt, 'headers': opt.headers ?? {} });
|
|
784
742
|
// --- 发起请求 ---
|
|
785
743
|
/** --- 远程端的双向 socket --- */
|
|
786
|
-
const rsocket = await connect(
|
|
744
|
+
const rsocket = await connect(targetUrl, {
|
|
787
745
|
...opt,
|
|
788
|
-
headers
|
|
746
|
+
'headers': headers,
|
|
789
747
|
});
|
|
790
748
|
if (!rsocket) {
|
|
791
749
|
return -2;
|
|
@@ -806,12 +764,11 @@ export async function rproxy(ctr, url, opt = {}) {
|
|
|
806
764
|
opt.headers ??= {};
|
|
807
765
|
const headers = lProxy.getProxyHeaders(req, opt);
|
|
808
766
|
// --- 发起请求 ---
|
|
809
|
-
/** --- 远程端的双向 socket --- */
|
|
810
767
|
/** --- 目标连接的握手错误 --- */
|
|
811
768
|
let connectError;
|
|
812
769
|
const rsocket = await connect(url, {
|
|
813
770
|
...opt,
|
|
814
|
-
headers,
|
|
771
|
+
'headers': headers,
|
|
815
772
|
'onConnectError': (error) => {
|
|
816
773
|
connectError = error;
|
|
817
774
|
opt.onConnectError?.(error);
|