dsh-plugin-reverse-proxy 0.0.0-stage → 0.3.5
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/CHANGELOG.md +161 -0
- package/LICENSE +21 -0
- package/README.en.md +234 -0
- package/README.md +274 -2
- package/assets/lan-access-cli.jpg +0 -0
- package/assets/logo.jpg +0 -0
- package/cordis.patch.yml +75 -0
- package/index.js +568 -0
- package/lan-access.js +164 -0
- package/package.json +59 -4
- package/vendor/skip/LICENSE +21 -0
- package/vendor/skip/NOTICE +41 -0
- package/vendor/skip/adapters/carrier-neutral-017.js +57 -0
- package/vendor/skip/adapters/carrier-neutral-021.js +85 -0
- package/vendor/skip/adapters/carrier-neutral.js +23 -0
- package/vendor/skip/adapters/legacy-web.js +6 -0
- package/vendor/skip/adapters/types.js +20 -0
- package/vendor/skip/bridge.js +122 -0
- package/vendor/skip/client.js +14489 -0
- package/vendor/skip/compatibility.js +223 -0
- package/vendor/skip/connection-runtime.js +63 -0
- package/vendor/skip/gate.js +176 -0
- package/vendor/skip/host.js +212 -0
- package/vendor/skip/trust-fence.js +135 -0
- package/vendor/skip/trusted-auth.js +95 -0
package/lan-access.js
ADDED
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 局域网访问开关(0.3.2)。
|
|
3
|
+
*
|
|
4
|
+
* 默认关闭:反代只监听 127.0.0.1,同一台机器上的浏览器才能打开。
|
|
5
|
+
* 用户必须在设置页点开关(弹安全提示、点「确定」)或者在命令行里确认,
|
|
6
|
+
* 才把监听地址切到 0.0.0.0 / ::,对同一网络里的所有设备开放。
|
|
7
|
+
*
|
|
8
|
+
* 状态落在 `<DSH_HOME>/plugin-reverse-proxy/lan-access.json`:
|
|
9
|
+
* 开关切换后马上写盘,重启 dsh 不需要重新打开。
|
|
10
|
+
*/
|
|
11
|
+
import fs from 'node:fs'
|
|
12
|
+
import os from 'node:os'
|
|
13
|
+
import path from 'node:path'
|
|
14
|
+
|
|
15
|
+
const DSH_HOME = (() => {
|
|
16
|
+
const fromEnv = process.env.DSH_HOME
|
|
17
|
+
if (typeof fromEnv === 'string' && fromEnv.trim() !== '') return fromEnv
|
|
18
|
+
return path.join(os.homedir(), '.dsh')
|
|
19
|
+
})()
|
|
20
|
+
|
|
21
|
+
export const STATE_FILE = path.join(DSH_HOME, 'plugin-reverse-proxy', 'lan-access.json')
|
|
22
|
+
|
|
23
|
+
/** 默认监听端口:0.3.5 起端口也进状态文件,可在设置页改,重启保留。 */
|
|
24
|
+
export const DEFAULT_PORT = 8080
|
|
25
|
+
|
|
26
|
+
/** 安全提示文案:设置页弹窗与命令行共用同一份,避免两处说法不一致。 */
|
|
27
|
+
export const LAN_WARNING_TITLE = '对局域网开放访问?'
|
|
28
|
+
export const LAN_WARNING_LINES = [
|
|
29
|
+
'开启后,同一网络里的任何设备打开这台机器的 8080 端口,都能直接进入 DSH 后台,',
|
|
30
|
+
'不需要 Token、不需要登录,等于把整台机器的控制权交给同一网段的所有人。',
|
|
31
|
+
'只在你信任当前 Wi-Fi / 局域网(比如家里、自己手机的热点)时才开启。',
|
|
32
|
+
]
|
|
33
|
+
export const LAN_WARNING_TEXT = [
|
|
34
|
+
LAN_WARNING_TITLE,
|
|
35
|
+
...LAN_WARNING_LINES,
|
|
36
|
+
'恢复为只允许本机访问后,其他设备会立刻断开。',
|
|
37
|
+
].join('\n')
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* 端口合法性:1024–65535,且不能是上游的 3080(自己转发给自己会成环)。
|
|
41
|
+
* 返回 { ok, port } 或 { ok:false, error }——HTTP 接口、命令行、前端共用同一套规则。
|
|
42
|
+
*/
|
|
43
|
+
export const validatePort = (value) => {
|
|
44
|
+
const n = typeof value === 'string' ? Number(value.trim()) : Number(value)
|
|
45
|
+
if (!Number.isInteger(n)) return { ok: false, error: '端口必须是整数' }
|
|
46
|
+
if (n < 1024) return { ok: false, error: '端口要在 1024–65535 之间(1024 以下需要 root,且更容易撞系统服务)' }
|
|
47
|
+
if (n > 65535) return { ok: false, error: '端口不能超过 65535' }
|
|
48
|
+
if (n === 3080) return { ok: false, error: '3080 是 DSH 自己的端口,反代不能占它' }
|
|
49
|
+
return { ok: true, port: n }
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const readState = () => {
|
|
53
|
+
try {
|
|
54
|
+
const raw = JSON.parse(fs.readFileSync(STATE_FILE, 'utf8'))
|
|
55
|
+
const parsed = validatePort(raw?.port)
|
|
56
|
+
return {
|
|
57
|
+
lanAccess: raw?.lanAccess === true,
|
|
58
|
+
port: parsed.ok ? parsed.port : DEFAULT_PORT,
|
|
59
|
+
updatedAt: typeof raw?.updatedAt === 'string' ? raw.updatedAt : null,
|
|
60
|
+
updatedBy: typeof raw?.updatedBy === 'string' ? raw.updatedBy : null,
|
|
61
|
+
}
|
|
62
|
+
} catch {
|
|
63
|
+
// 文件不存在 / 读坏了都当「关闭」——安全默认值
|
|
64
|
+
return { lanAccess: false, port: DEFAULT_PORT, updatedAt: null, updatedBy: null }
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
let state = readState()
|
|
69
|
+
const listeners = new Set()
|
|
70
|
+
|
|
71
|
+
const writeState = (next) => {
|
|
72
|
+
try {
|
|
73
|
+
fs.mkdirSync(path.dirname(STATE_FILE), { recursive: true })
|
|
74
|
+
const tmp = STATE_FILE + '.tmp'
|
|
75
|
+
fs.writeFileSync(tmp, JSON.stringify(next, null, 2) + '\n', 'utf8')
|
|
76
|
+
fs.renameSync(tmp, STATE_FILE)
|
|
77
|
+
} catch (error) {
|
|
78
|
+
// 写盘失败不能连坐宿主,但必须让调用方看见
|
|
79
|
+
throw new Error('无法保存局域网开关状态: ' + (error && error.message ? error.message : String(error)))
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export const getLanState = () => ({ ...state })
|
|
84
|
+
|
|
85
|
+
/** 默认监听地址:没开就是本机回环。 */
|
|
86
|
+
export const effectiveListenHost = () => (state.lanAccess ? '0.0.0.0' : '127.0.0.1')
|
|
87
|
+
|
|
88
|
+
/** 当前生效端口(状态文件优先,没有就用默认 8080)。 */
|
|
89
|
+
export const effectivePort = () => state.port || DEFAULT_PORT
|
|
90
|
+
|
|
91
|
+
/** 订阅开关变化(反代用它重建监听)。返回取消订阅函数。 */
|
|
92
|
+
export const onLanChange = (listener) => {
|
|
93
|
+
listeners.add(listener)
|
|
94
|
+
return () => { listeners.delete(listener) }
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** 改开关:写盘 → 通知监听者。`by` 只用于记录是谁动的(settings / command)。 */
|
|
98
|
+
export const setLanAccess = (enabled, by = 'unknown') => {
|
|
99
|
+
const next = {
|
|
100
|
+
lanAccess: enabled === true,
|
|
101
|
+
port: effectivePort(),
|
|
102
|
+
updatedAt: new Date().toISOString(),
|
|
103
|
+
updatedBy: by,
|
|
104
|
+
}
|
|
105
|
+
writeState(next)
|
|
106
|
+
state = next
|
|
107
|
+
for (const listener of listeners) {
|
|
108
|
+
try { listener(getLanState()) } catch { /* 单个监听者失败不连坐 */ }
|
|
109
|
+
}
|
|
110
|
+
return getLanState()
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** 改端口:校验 → 写盘 → 通知监听者(调用方负责确认端口没被占用)。 */
|
|
114
|
+
export const setPort = (value, by = 'unknown') => {
|
|
115
|
+
const parsed = validatePort(value)
|
|
116
|
+
if (!parsed.ok) throw new Error(parsed.error)
|
|
117
|
+
const next = {
|
|
118
|
+
lanAccess: state.lanAccess === true,
|
|
119
|
+
port: parsed.port,
|
|
120
|
+
updatedAt: new Date().toISOString(),
|
|
121
|
+
updatedBy: by,
|
|
122
|
+
}
|
|
123
|
+
writeState(next)
|
|
124
|
+
state = next
|
|
125
|
+
for (const listener of listeners) {
|
|
126
|
+
try { listener(getLanState()) } catch { /* 单个监听者失败不连坐 */ }
|
|
127
|
+
}
|
|
128
|
+
return getLanState()
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** 给 UI / 命令用的统一状态描述。 */
|
|
132
|
+
/**
|
|
133
|
+
* 盯住状态文件:命令行脚本 / 手动改 json 也能触发重建监听。
|
|
134
|
+
* 目录不存在时先建出来,否则 fs.watch 会直接抛。
|
|
135
|
+
*/
|
|
136
|
+
const startWatch = () => {
|
|
137
|
+
try {
|
|
138
|
+
fs.mkdirSync(path.dirname(STATE_FILE), { recursive: true })
|
|
139
|
+
fs.watch(path.dirname(STATE_FILE), { persistent: false }, (_event, filename) => {
|
|
140
|
+
if (filename && filename !== path.basename(STATE_FILE)) return
|
|
141
|
+
const next = readState()
|
|
142
|
+
if (next.lanAccess === state.lanAccess && next.port === state.port) return
|
|
143
|
+
state = next
|
|
144
|
+
for (const listener of listeners) {
|
|
145
|
+
try { listener(getLanState()) } catch { /* 单个监听者失败不连坐 */ }
|
|
146
|
+
}
|
|
147
|
+
})
|
|
148
|
+
} catch { /* 盯不住就算了,开关本身还能用 */ }
|
|
149
|
+
}
|
|
150
|
+
startWatch()
|
|
151
|
+
|
|
152
|
+
export const describeLanState = (port) => {
|
|
153
|
+
const current = getLanState()
|
|
154
|
+
return {
|
|
155
|
+
ok: true,
|
|
156
|
+
lanAccess: current.lanAccess,
|
|
157
|
+
updatedAt: current.updatedAt,
|
|
158
|
+
updatedBy: current.updatedBy,
|
|
159
|
+
listenHost: effectiveListenHost(),
|
|
160
|
+
port: typeof port === 'number' ? port : effectivePort(),
|
|
161
|
+
defaultPort: DEFAULT_PORT,
|
|
162
|
+
stateFile: STATE_FILE,
|
|
163
|
+
}
|
|
164
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,61 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-plugin-reverse-proxy",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.3.5",
|
|
4
|
+
"description": "DSH Web 反向代理 + 免 Token 登录(Web UI 专用):监听局域网端口,改写 Host/Origin/Sec-Fetch-Site 后转发到 127.0.0.1:3080,并替换官方 Connection 提供方跳过 BrowserAuth(版本门控,不匹配自动休眠)。仅适用于 Web UI 版,桌面版用不上。",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "index.js",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": "./index.js",
|
|
9
|
+
"./client": "./vendor/skip/client.js",
|
|
10
|
+
"./cordis.patch.yml": "./cordis.patch.yml",
|
|
11
|
+
"./package.json": "./package.json"
|
|
12
|
+
},
|
|
13
|
+
"files": [
|
|
14
|
+
"index.js",
|
|
15
|
+
"cordis.patch.yml",
|
|
16
|
+
"lan-access.js",
|
|
17
|
+
"README.md",
|
|
18
|
+
"README.en.md",
|
|
19
|
+
"assets",
|
|
20
|
+
"vendor",
|
|
21
|
+
"LICENSE",
|
|
22
|
+
"CHANGELOG.md"
|
|
23
|
+
],
|
|
24
|
+
"engines": {
|
|
25
|
+
"node": ">=22"
|
|
26
|
+
},
|
|
27
|
+
"license": "MIT",
|
|
28
|
+
"keywords": [
|
|
29
|
+
"deepseek",
|
|
30
|
+
"harness",
|
|
31
|
+
"dsh",
|
|
32
|
+
"dsh-plugin",
|
|
33
|
+
"reverse-proxy"
|
|
34
|
+
],
|
|
35
|
+
"dsh": {
|
|
36
|
+
"bundle": {
|
|
37
|
+
"patch": "./cordis.patch.yml"
|
|
38
|
+
},
|
|
39
|
+
"client": {
|
|
40
|
+
"platform": "web",
|
|
41
|
+
"inject": [
|
|
42
|
+
"@deepseek-ai/dsh-client-runtime",
|
|
43
|
+
"@deepseek-ai/dsh-client-ui-settings"
|
|
44
|
+
],
|
|
45
|
+
"immediately": true
|
|
46
|
+
}
|
|
47
|
+
},
|
|
48
|
+
"author": {
|
|
49
|
+
"name": "云风",
|
|
50
|
+
"email": "3538638569@qq.com",
|
|
51
|
+
"url": "https://github.com/llifco"
|
|
52
|
+
},
|
|
53
|
+
"repository": {
|
|
54
|
+
"type": "git",
|
|
55
|
+
"url": "git+https://github.com/llifco/dsh-plugin-reverse-proxy.git"
|
|
56
|
+
},
|
|
57
|
+
"bugs": {
|
|
58
|
+
"url": "https://github.com/llifco/dsh-plugin-reverse-proxy/issues"
|
|
59
|
+
},
|
|
60
|
+
"homepage": "https://github.com/llifco/dsh-plugin-reverse-proxy#readme"
|
|
61
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 iasi
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
Third-party notices
|
|
2
|
+
===================
|
|
3
|
+
|
|
4
|
+
This package includes or adapts MIT-licensed code from DeepSeek Harness
|
|
5
|
+
(@deepseek-ai/dsh-client-connection and related packages, the "Upstream
|
|
6
|
+
Code"):
|
|
7
|
+
|
|
8
|
+
- lib/client.js embeds the factory bodies of the official published client
|
|
9
|
+
bundles (one per supported DSH generation), unmodified and re-identified
|
|
10
|
+
only by its registration id;
|
|
11
|
+
- src/trust-fence.ts and src/bridge.ts are ported from the official
|
|
12
|
+
source;
|
|
13
|
+
- src/index.ts and src/trusted-auth.ts follow the semantics of the
|
|
14
|
+
official Connection implementation.
|
|
15
|
+
|
|
16
|
+
The Upstream Code is Copyright (c) 2026 DeepSeek. It is distributed in
|
|
17
|
+
this package under the MIT License, whose full text follows and applies
|
|
18
|
+
to the Upstream Code in its entirety.
|
|
19
|
+
|
|
20
|
+
---
|
|
21
|
+
MIT License
|
|
22
|
+
|
|
23
|
+
Copyright (c) 2026 DeepSeek
|
|
24
|
+
|
|
25
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
26
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
27
|
+
in the Software without restriction, including without limitation the rights
|
|
28
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
29
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
30
|
+
furnished to do so, subject to the following conditions:
|
|
31
|
+
|
|
32
|
+
The above copyright notice and this permission notice shall be included in all
|
|
33
|
+
copies or substantial portions of the Software.
|
|
34
|
+
|
|
35
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
36
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
37
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
38
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
39
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
40
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
41
|
+
SOFTWARE.
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { bridge } from '../bridge.js';
|
|
2
|
+
/**
|
|
3
|
+
* Host implementation for the 0.1.7 carrier-neutral connection contract.
|
|
4
|
+
*
|
|
5
|
+
* 与 carrier-neutral(0.1.5)的三处代际差异,均来自官方 0.1.7 apply 的逐行
|
|
6
|
+
* 对照(docs/COMPATIBILITY.md §5.4):
|
|
7
|
+
* 1. `/api` 准入走新的 `admit()`(OperatorPeer 语义),不再直调
|
|
8
|
+
* `requestRejection()`;
|
|
9
|
+
* 2. bridge 由 `webCtx.waterfall('connection/request', req, res, next)` 包裹,
|
|
10
|
+
* 监听该瀑布的插件才能继续看到每个连接请求;
|
|
11
|
+
* 3. BrowserAuth 消费是 mount-preserving(见 trusted-auth 的
|
|
12
|
+
* `mount-preserving` 语义,由 profile 选择,本 adapter 不重复实现)。
|
|
13
|
+
*/
|
|
14
|
+
export const HOST_ADAPTER_ID = 'carrier-neutral-017';
|
|
15
|
+
export function createCarrierNeutral017Runtime(input) {
|
|
16
|
+
const connection = new input.module.HostConnectionService(input.ctx, input.trustedHosts, input.auth);
|
|
17
|
+
const fetchHandler = connection.createSharedFetchHandler(input.module.API_PATH);
|
|
18
|
+
// admit() 是 0.1.7 代际的定义性契约,先于 requestBodyMode 检查:更早代际的
|
|
19
|
+
// 模块(如 pinned 0.1.2)在这里以确定性的报错信息被拒绝。
|
|
20
|
+
if (typeof connection.admit !== 'function') {
|
|
21
|
+
throw new Error('dsh-plugin-reverse-proxy: carrier-neutral-017 profile requires the 0.1.7 admit() connection seam');
|
|
22
|
+
}
|
|
23
|
+
if (typeof fetchHandler.requestBodyMode !== 'function') {
|
|
24
|
+
throw new Error('dsh-plugin-reverse-proxy: carrier-neutral-017 profile requires a requestBodyMode-aware connection handler');
|
|
25
|
+
}
|
|
26
|
+
const waterfall = input.ctx.waterfall;
|
|
27
|
+
if (typeof waterfall !== 'function') {
|
|
28
|
+
throw new Error('dsh-plugin-reverse-proxy: carrier-neutral-017 profile requires a waterfall-capable carrier context');
|
|
29
|
+
}
|
|
30
|
+
// 0.1.7 keeps the 0.1.5 register() shape: the physical route mounts on
|
|
31
|
+
// `owner.webServer`, while rpc.handle() readings often come from sibling
|
|
32
|
+
// plugin contexts without webServer. Bind only this replacement instance's
|
|
33
|
+
// registrations to the adapter's webServer-owning context (the 405 fix).
|
|
34
|
+
const service = connection;
|
|
35
|
+
const originalRegister = service.register;
|
|
36
|
+
if (typeof originalRegister !== 'function') {
|
|
37
|
+
throw new Error('dsh-plugin-reverse-proxy: carrier-neutral-017 connection lacks its RPC register seam');
|
|
38
|
+
}
|
|
39
|
+
service.register = function registerWithCarrier(_owner, channel, handler) {
|
|
40
|
+
return originalRegister.call(this, input.ctx, channel, handler);
|
|
41
|
+
};
|
|
42
|
+
const admitted = connection;
|
|
43
|
+
const route = {
|
|
44
|
+
kind: 'prefix',
|
|
45
|
+
path: input.module.API_PATH,
|
|
46
|
+
handler: async (req, res) => {
|
|
47
|
+
const admission = admitted.admit(req);
|
|
48
|
+
if ('rejection' in admission) {
|
|
49
|
+
res.writeHead(admission.rejection);
|
|
50
|
+
res.end(admission.rejection === 401 ? 'unauthorized' : 'forbidden');
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
await waterfall.call(input.ctx, 'connection/request', req, res, () => bridge(req, res, fetchHandler, input.maxRequestBodyBytes));
|
|
54
|
+
},
|
|
55
|
+
};
|
|
56
|
+
return { connection, route, fetchHandler, adapterId: HOST_ADAPTER_ID };
|
|
57
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { bridgeWithPeer } from '../bridge.js';
|
|
2
|
+
/**
|
|
3
|
+
* Host implementation for the 0.2.1 carrier-neutral connection contract.
|
|
4
|
+
*
|
|
5
|
+
* 相对 carrier-neutral-017(0.1.7–0.2.0 代际)的官方差异,均来自 0.2.1-rc.1
|
|
6
|
+
* 官方 apply 的逐行对照(docs/COMPATIBILITY.md §5.8 审计 + §5.9 收编):
|
|
7
|
+
* 1. 共享 `/api` 路由的 bridge 调用把 admit() 产生的 Peer 透传给 fetch
|
|
8
|
+
* (`bridge(req, res, fetchHandler, admission.peer, maxRequestBodyBytes)`)
|
|
9
|
+
* ——Peer 最终成为 RPC handler 第 4 参,并把 exact Fetch route 门在
|
|
10
|
+
* owner-principal 上(非 owner ⇒ 403,官方 fetch handler 自带);
|
|
11
|
+
* 2. 新增 exact `/connect` 路由(`module.CONNECT_PATH`),handler 直接委托
|
|
12
|
+
* `connection.authorizeConnect(req, res)`(socket 连接协议通道);
|
|
13
|
+
* 3. `admit()` 语义升级(isTrusted → disposed → browserAuth.isAuthenticated ⇒
|
|
14
|
+
* browserPeer / token 台账 peer),但形状仍是 `{peer} | {rejection}`,
|
|
15
|
+
* Replacement 的消费方式不变——认证由 trusted-auth stub 承担(恒
|
|
16
|
+
* authenticated ⇒ 恒 browserPeer,principal 为 owner,与官方浏览器半区
|
|
17
|
+
* 同一对象),fence 语义由 stub 与 service 共同保持;
|
|
18
|
+
* 4. `register()` 仍是 owner-based 形状(sibling 无 webServer 的 405 语义
|
|
19
|
+
* 不变),沿用 017 的 carrier 绑定包装。
|
|
20
|
+
*
|
|
21
|
+
* 代际 seam 守卫:`CONNECT_PATH` 导出 + `authorizeConnect` 方法是 0.2.1 特有
|
|
22
|
+
* 面——更早代际的模块(含 0.1.7/0.2.0)在这里以确定性报错被拒绝(0.1.7 的
|
|
23
|
+
* `admit()` 存在性已不足以区分代际)。
|
|
24
|
+
*/
|
|
25
|
+
export const HOST_ADAPTER_ID = 'carrier-neutral-021';
|
|
26
|
+
export function createCarrierNeutral021Runtime(input) {
|
|
27
|
+
const connection = new input.module.HostConnectionService(input.ctx, input.trustedHosts, input.auth);
|
|
28
|
+
const fetchHandler = connection.createSharedFetchHandler(input.module.API_PATH);
|
|
29
|
+
// 代际守卫(fail loud 于 apply 前,绝不静默降级到错误代际的语义):
|
|
30
|
+
// - admit():0.1.7 起的准入 seam(0.2.1 延续);
|
|
31
|
+
// - CONNECT_PATH + authorizeConnect:0.2.1 特有的 /connect 通道 seam;
|
|
32
|
+
// - requestBodyMode:0.1.5 起的 body-mode 感知 handler。
|
|
33
|
+
if (typeof connection.admit !== 'function') {
|
|
34
|
+
throw new Error('dsh-plugin-reverse-proxy: carrier-neutral-021 profile requires the 0.2.1 admit() connection seam');
|
|
35
|
+
}
|
|
36
|
+
if (typeof connection.authorizeConnect !== 'function' || input.module.CONNECT_PATH === undefined) {
|
|
37
|
+
throw new Error('dsh-plugin-reverse-proxy: carrier-neutral-021 profile requires the 0.2.1 /connect connection seam (CONNECT_PATH + authorizeConnect)');
|
|
38
|
+
}
|
|
39
|
+
if (typeof fetchHandler.requestBodyMode !== 'function') {
|
|
40
|
+
throw new Error('dsh-plugin-reverse-proxy: carrier-neutral-021 profile requires a requestBodyMode-aware connection handler');
|
|
41
|
+
}
|
|
42
|
+
const waterfall = input.ctx.waterfall;
|
|
43
|
+
if (typeof waterfall !== 'function') {
|
|
44
|
+
throw new Error('dsh-plugin-reverse-proxy: carrier-neutral-021 profile requires a waterfall-capable carrier context');
|
|
45
|
+
}
|
|
46
|
+
// 0.2.1 keeps the owner-based register() shape: the physical route mounts on
|
|
47
|
+
// `owner.webServer`, while rpc.handle() readings often come from sibling
|
|
48
|
+
// plugin contexts without webServer. Bind only this replacement instance's
|
|
49
|
+
// registrations to the adapter's webServer-owning context (the 405 fix,
|
|
50
|
+
// unchanged from the 017 adapter).
|
|
51
|
+
const service = connection;
|
|
52
|
+
const originalRegister = service.register;
|
|
53
|
+
if (typeof originalRegister !== 'function') {
|
|
54
|
+
throw new Error('dsh-plugin-reverse-proxy: carrier-neutral-021 connection lacks its RPC register seam');
|
|
55
|
+
}
|
|
56
|
+
service.register = function registerWithCarrier(_owner, channel, handler) {
|
|
57
|
+
return originalRegister.call(this, input.ctx, channel, handler);
|
|
58
|
+
};
|
|
59
|
+
const admitted = connection;
|
|
60
|
+
const connectPath = input.module.CONNECT_PATH;
|
|
61
|
+
const route = {
|
|
62
|
+
kind: 'prefix',
|
|
63
|
+
path: input.module.API_PATH,
|
|
64
|
+
handler: async (req, res) => {
|
|
65
|
+
const admission = admitted.admit(req);
|
|
66
|
+
if ('rejection' in admission) {
|
|
67
|
+
res.writeHead(admission.rejection);
|
|
68
|
+
res.end(admission.rejection === 401 ? 'unauthorized' : 'forbidden');
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
await waterfall.call(input.ctx, 'connection/request', req, res, () => bridgeWithPeer(req, res, fetchHandler, admission.peer, input.maxRequestBodyBytes));
|
|
72
|
+
},
|
|
73
|
+
};
|
|
74
|
+
// 官方 0.2.1 apply 的第二个注册(顺序照抄:/api 在前,/connect 在后):
|
|
75
|
+
// exact CONNECT_PATH 路由整体委托 authorizeConnect( admission/栅栏语义在
|
|
76
|
+
// service 内部,Replacement 不复刻其实现)。
|
|
77
|
+
const connectRoute = {
|
|
78
|
+
kind: 'exact',
|
|
79
|
+
path: connectPath,
|
|
80
|
+
handler: (req, res) => {
|
|
81
|
+
admitted.authorizeConnect(req, res);
|
|
82
|
+
},
|
|
83
|
+
};
|
|
84
|
+
return { connection, route, additionalRoutes: [connectRoute], fetchHandler, adapterId: HOST_ADAPTER_ID };
|
|
85
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { createHostRuntime } from './types.js';
|
|
2
|
+
/** Host implementation for the 0.1.5 carrier-neutral connection contract. */
|
|
3
|
+
export const HOST_ADAPTER_ID = 'carrier-neutral';
|
|
4
|
+
export function createCarrierNeutralRuntime(input) {
|
|
5
|
+
const runtime = createHostRuntime(input, HOST_ADAPTER_ID);
|
|
6
|
+
if (typeof runtime.fetchHandler.requestBodyMode !== 'function') {
|
|
7
|
+
throw new Error('dsh-plugin-reverse-proxy: carrier-neutral profile requires a requestBodyMode-aware connection handler');
|
|
8
|
+
}
|
|
9
|
+
// 0.1.5's carrier-neutral service keeps generic rpc.handle() registrations
|
|
10
|
+
// on the reading context, which is often a sibling plugin without webServer.
|
|
11
|
+
// Bind only this replacement instance to the adapter's carrier context. This
|
|
12
|
+
// is the narrow equivalent of the upstream workaround and leaves fetch
|
|
13
|
+
// registry ownership untouched.
|
|
14
|
+
const service = runtime.connection;
|
|
15
|
+
const originalRegister = service.register;
|
|
16
|
+
if (typeof originalRegister !== 'function') {
|
|
17
|
+
throw new Error('dsh-plugin-reverse-proxy: carrier-neutral connection lacks its RPC register seam');
|
|
18
|
+
}
|
|
19
|
+
service.register = function registerWithCarrier(_owner, channel, handler) {
|
|
20
|
+
return originalRegister.call(this, input.ctx, channel, handler);
|
|
21
|
+
};
|
|
22
|
+
return runtime;
|
|
23
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { createHostRuntime } from './types.js';
|
|
2
|
+
/** Host implementation for the 0.1.2-rc.1 web-carrier contract. */
|
|
3
|
+
export const HOST_ADAPTER_ID = 'legacy-web';
|
|
4
|
+
export function createLegacyWebRuntime(input) {
|
|
5
|
+
return createHostRuntime(input, HOST_ADAPTER_ID);
|
|
6
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { bridge } from '../bridge.js';
|
|
2
|
+
/** Construct the common trusted /api carrier route for one compatibility adapter. */
|
|
3
|
+
export function createHostRuntime(input, adapterId) {
|
|
4
|
+
const connection = new input.module.HostConnectionService(input.ctx, input.trustedHosts, input.auth);
|
|
5
|
+
const fetchHandler = connection.createSharedFetchHandler(input.module.API_PATH);
|
|
6
|
+
const route = {
|
|
7
|
+
kind: 'prefix',
|
|
8
|
+
path: input.module.API_PATH,
|
|
9
|
+
handler: async (req, res) => {
|
|
10
|
+
const rejection = connection.requestRejection(req);
|
|
11
|
+
if (rejection !== undefined) {
|
|
12
|
+
res.writeHead(rejection);
|
|
13
|
+
res.end(rejection === 401 ? 'unauthorized' : 'forbidden');
|
|
14
|
+
return;
|
|
15
|
+
}
|
|
16
|
+
await bridge(req, res, fetchHandler, input.maxRequestBodyBytes);
|
|
17
|
+
},
|
|
18
|
+
};
|
|
19
|
+
return { connection, route, fetchHandler, adapterId };
|
|
20
|
+
}
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* node:http ↔ WHATWG fetch bridge for the /api transport (host side of the
|
|
3
|
+
* web carrier; the fetch-shaped handler itself is transport-agnostic).
|
|
4
|
+
*
|
|
5
|
+
* 移植自官方 packages/client/connection/src/http-bridge.ts(rc.1),语义照抄。
|
|
6
|
+
*/
|
|
7
|
+
import { Readable } from 'node:stream';
|
|
8
|
+
/** Default carrier cap for all HTTP RPC bodies: sized for the default
|
|
9
|
+
* aggregate image limit (200 MiB) after base64 expansion plus envelope
|
|
10
|
+
* headroom (~267.7 MiB required), rounded up for slack. The bridge buffers
|
|
11
|
+
* each body in memory, so this cap is also the per-request resident bound. */
|
|
12
|
+
export const DEFAULT_MAX_REQUEST_BODY_BYTES = 300 * 1024 * 1024;
|
|
13
|
+
/**
|
|
14
|
+
* Bridge one node:http request to the fetch-shaped handler (client close
|
|
15
|
+
* aborts; response bodies stream out chunk by chunk).
|
|
16
|
+
* @param req - incoming node:http request (fully read before dispatch).
|
|
17
|
+
* @param res - node:http response the bridge writes and owns to completion.
|
|
18
|
+
* @param apiHandler - fetch-shaped API carrier the request is dispatched to.
|
|
19
|
+
* @param maxRequestBodyBytes - maximum body bytes buffered before dispatch.
|
|
20
|
+
*/
|
|
21
|
+
export async function bridge(req, res, apiHandler, maxRequestBodyBytes = DEFAULT_MAX_REQUEST_BODY_BYTES) {
|
|
22
|
+
return bridgeRequest(req, res, apiHandler, undefined, maxRequestBodyBytes);
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* 0.2.1 (carrier-neutral-021) bridge: identical to {@link bridge} except the
|
|
26
|
+
* admission Peer is threaded into the handler call, replicating the official
|
|
27
|
+
* `bridge(req, res, fetchHandler, admission.peer, maxRequestBodyBytes)` wiring
|
|
28
|
+
* (the peer lands in RPC handler slot 4 and gates exact Fetch routes to
|
|
29
|
+
* owner-principal peers). Generations never cross: the v3 adapter keeps
|
|
30
|
+
* calling {@link bridge}.
|
|
31
|
+
*/
|
|
32
|
+
export async function bridgeWithPeer(req, res, apiHandler, peer, maxRequestBodyBytes = DEFAULT_MAX_REQUEST_BODY_BYTES) {
|
|
33
|
+
return bridgeRequest(req, res, apiHandler, peer, maxRequestBodyBytes);
|
|
34
|
+
}
|
|
35
|
+
async function bridgeRequest(req, res, apiHandler, peer, maxRequestBodyBytes) {
|
|
36
|
+
const abort = new AbortController();
|
|
37
|
+
// Client-disconnect detection MUST hang off the response, not the request:
|
|
38
|
+
// since Node 16, IncomingMessage 'close' fires as soon as the request body is
|
|
39
|
+
// fully consumed (immediately for a bodyless GET), which would abort a
|
|
40
|
+
// streaming response right after open. ServerResponse 'close' fires on connection teardown;
|
|
41
|
+
// writableEnded distinguishes a normal end() from the client going away.
|
|
42
|
+
res.on('close', () => {
|
|
43
|
+
if (!res.writableEnded)
|
|
44
|
+
abort.abort();
|
|
45
|
+
});
|
|
46
|
+
/* v8 ignore next 3 -- `??` arms: node:http always sets url/method on server
|
|
47
|
+
requests; the fields are only optional on the client-side IncomingMessage type */
|
|
48
|
+
const url = new URL(req.url ?? '/', 'http://dsh.internal');
|
|
49
|
+
const method = req.method ?? 'GET';
|
|
50
|
+
const headers = Object.fromEntries(Object.entries(req.headers).filter(([, value]) => typeof value === 'string'));
|
|
51
|
+
const bodyMode = apiHandler.requestBodyMode?.({ method, url }) ?? 'buffered';
|
|
52
|
+
let request;
|
|
53
|
+
if (bodyMode === 'buffered') {
|
|
54
|
+
const declaredLength = req.headers['content-length'];
|
|
55
|
+
if (declaredLength !== undefined && Number(declaredLength) > maxRequestBodyBytes) {
|
|
56
|
+
res.writeHead(413, { connection: 'close' });
|
|
57
|
+
res.end();
|
|
58
|
+
req.destroy();
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
const chunks = [];
|
|
62
|
+
let received = 0;
|
|
63
|
+
for await (const chunk of req) {
|
|
64
|
+
const buffer = chunk;
|
|
65
|
+
received += buffer.byteLength;
|
|
66
|
+
if (received > maxRequestBodyBytes) {
|
|
67
|
+
res.writeHead(413, { connection: 'close' });
|
|
68
|
+
res.end();
|
|
69
|
+
req.destroy();
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
chunks.push(buffer);
|
|
73
|
+
}
|
|
74
|
+
request = new Request(url, {
|
|
75
|
+
method,
|
|
76
|
+
headers,
|
|
77
|
+
...chunks.length > 0 ? { body: Buffer.concat(chunks) } : {},
|
|
78
|
+
signal: abort.signal,
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
else {
|
|
82
|
+
request = new Request(url, {
|
|
83
|
+
method,
|
|
84
|
+
headers,
|
|
85
|
+
body: Readable.toWeb(req),
|
|
86
|
+
signal: abort.signal,
|
|
87
|
+
duplex: 'half',
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
const response = await apiHandler.fetch(request, peer);
|
|
91
|
+
const requestUnread = bodyMode === 'streaming' && !req.readableEnded;
|
|
92
|
+
const responseHeaders = Object.fromEntries(response.headers.entries());
|
|
93
|
+
res.writeHead(response.status, requestUnread
|
|
94
|
+
? { ...responseHeaders, connection: 'close' }
|
|
95
|
+
: responseHeaders);
|
|
96
|
+
if (response.body === null) {
|
|
97
|
+
res.end();
|
|
98
|
+
if (requestUnread)
|
|
99
|
+
req.destroy();
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
for await (const chunk of response.body) {
|
|
103
|
+
// Backpressure: a false return means the socket buffer is full — wait for drain
|
|
104
|
+
// instead of buffering unboundedly (slow or suspended consumers). 'close' also
|
|
105
|
+
// resolves so a mid-wait disconnect can't park this loop forever; the close
|
|
106
|
+
// handler above aborts the handler stream, which then ends the iteration.
|
|
107
|
+
if (!res.write(chunk)) {
|
|
108
|
+
await new Promise((resolve) => {
|
|
109
|
+
const done = () => {
|
|
110
|
+
res.off('drain', done);
|
|
111
|
+
res.off('close', done);
|
|
112
|
+
resolve();
|
|
113
|
+
};
|
|
114
|
+
res.once('drain', done);
|
|
115
|
+
res.once('close', done);
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
res.end();
|
|
120
|
+
if (requestUnread)
|
|
121
|
+
req.destroy();
|
|
122
|
+
}
|