dsh-connect-workbuddy-gateway 1.4.2 → 1.4.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/README.md +1 -1
- package/client.js +139 -3
- package/package.json +1 -1
- package/src/config.js +27 -1
- package/src/pairing.js +37 -5
- package/src/peers.js +18 -5
- package/src/routes.js +144 -4
package/README.md
CHANGED
|
@@ -558,7 +558,7 @@ hy4flash-free@#9 ← 昵称含中文/emoji,退化成序号
|
|
|
558
558
|
node tests/harness.mjs
|
|
559
559
|
```
|
|
560
560
|
|
|
561
|
-
覆盖
|
|
561
|
+
覆盖 308 项:首次建 key 与惰性补选、鉴权、模型勾选与清单投影、重名后缀(区域 / 昵称 /
|
|
562
562
|
非 ASCII 序号)、跨区同名、按名字路由、推理档透传、**固定监听端口与占用提示**、**局域网分享与接入**、**两种角色模式**、
|
|
563
563
|
并行工具调用按 index 分组、
|
|
564
564
|
Responses 流的规范校验(`sequence_number`、part 一致性、terminal 的 output 顺序)、
|
package/client.js
CHANGED
|
@@ -124,6 +124,19 @@ window.__ModuleLoader__.load({
|
|
|
124
124
|
pairingShareName: '本机名称',
|
|
125
125
|
pairingShareNameHint: '对方在列表里会看到这个名字,留空则只显示 IP。',
|
|
126
126
|
pairingSaveName: '保存名称',
|
|
127
|
+
/* 已分享的凭据 */
|
|
128
|
+
shares: '已分享出去的凭据',
|
|
129
|
+
sharesEmpty: '还没有分享给任何设备',
|
|
130
|
+
shareDevice: '设备',
|
|
131
|
+
shareAddress: '地址',
|
|
132
|
+
shareWhen: '分享于',
|
|
133
|
+
shareRevoke: '撤销分享',
|
|
134
|
+
shareRevoking: '撤销中…',
|
|
135
|
+
shareKeyNote: '撤销会**同时删除**那把专用 key(它唯一的用途就是给这台设备)。',
|
|
136
|
+
shareAndKey: '同时删除 key',
|
|
137
|
+
unknownDevice: '未知设备',
|
|
138
|
+
pairingRetry: '这个申请已失效,请重新申请',
|
|
139
|
+
peersRetry: '重新申请',
|
|
127
140
|
/* 运行模式 */
|
|
128
141
|
mode: '运行模式',
|
|
129
142
|
modeHint: '两种角色做的事完全不同,配置项也不重叠。切换后插件会重启。',
|
|
@@ -282,6 +295,19 @@ window.__ModuleLoader__.load({
|
|
|
282
295
|
pairingShareName: 'Machine name',
|
|
283
296
|
pairingShareNameHint: 'Other devices see this in their list; leave empty to show only the IP.',
|
|
284
297
|
pairingSaveName: 'Save name',
|
|
298
|
+
/* Issued credentials */
|
|
299
|
+
shares: 'Shared credentials',
|
|
300
|
+
sharesEmpty: 'Nothing has been shared yet',
|
|
301
|
+
shareDevice: 'Device',
|
|
302
|
+
shareAddress: 'Address',
|
|
303
|
+
shareWhen: 'Shared at',
|
|
304
|
+
shareRevoke: 'Revoke',
|
|
305
|
+
shareRevoking: 'Revoking...',
|
|
306
|
+
shareKeyNote: 'Revoking also deletes that dedicated key, whose only purpose was this device.',
|
|
307
|
+
shareAndKey: 'and delete the key',
|
|
308
|
+
unknownDevice: 'unknown device',
|
|
309
|
+
pairingRetry: 'This request expired; start a new one',
|
|
310
|
+
peersRetry: 'Request again',
|
|
285
311
|
/* Run mode */
|
|
286
312
|
mode: 'Run mode',
|
|
287
313
|
modeHint: 'The two roles do different things, and their settings do not overlap. Switching reloads the plugin.',
|
|
@@ -1220,9 +1246,10 @@ window.__ModuleLoader__.load({
|
|
|
1220
1246
|
* 这一屏是「谁可以拿走我的模型额度」的唯一决策点,所以它把三件事摆明:
|
|
1221
1247
|
* 谁在申请、要分享哪些模型、会新建一把专用 key。
|
|
1222
1248
|
*/
|
|
1223
|
-
function PairingCard({ state, t, onApprove, onDeny, onSaveShareName }) {
|
|
1249
|
+
function PairingCard({ state, t, onApprove, onDeny, onSaveShareName, onRevoke }) {
|
|
1224
1250
|
const pairing = state?.pairing ?? {}
|
|
1225
1251
|
const pending = Array.isArray(pairing.pending) ? pairing.pending : []
|
|
1252
|
+
const shares = Array.isArray(state?.shares) ? state.shares : []
|
|
1226
1253
|
const keys = Array.isArray(state?.keys) ? state.keys : []
|
|
1227
1254
|
|
|
1228
1255
|
/* 正在批准哪一个(展开模型勾选)。 */
|
|
@@ -1233,6 +1260,8 @@ window.__ModuleLoader__.load({
|
|
|
1233
1260
|
const [error, setError] = useState(undefined)
|
|
1234
1261
|
const [done, setDone] = useState(undefined)
|
|
1235
1262
|
const [shareName, setShareName] = useState(pairing.shareName ?? '')
|
|
1263
|
+
/* 正在撤销哪一个分享(要二次确认:它会连带删掉那把 key)。 */
|
|
1264
|
+
const [revoking, setRevoking] = useState(undefined)
|
|
1236
1265
|
|
|
1237
1266
|
useEffect(() => {
|
|
1238
1267
|
if (busy) return
|
|
@@ -1271,6 +1300,25 @@ window.__ModuleLoader__.load({
|
|
|
1271
1300
|
}
|
|
1272
1301
|
}
|
|
1273
1302
|
|
|
1303
|
+
/*
|
|
1304
|
+
* 撤销分享。
|
|
1305
|
+
*
|
|
1306
|
+
* 两步:先点「撤销分享」展开确认(因为会连带删掉那把 key),再点确认才真删。
|
|
1307
|
+
* 不做二次确认的话,一次误点就把别人正在用的访问断了。
|
|
1308
|
+
*/
|
|
1309
|
+
const revoke = async (keyId2) => {
|
|
1310
|
+
setBusy(true)
|
|
1311
|
+
setError(undefined)
|
|
1312
|
+
try {
|
|
1313
|
+
await onRevoke(keyId2)
|
|
1314
|
+
setRevoking(undefined)
|
|
1315
|
+
} catch (caught) {
|
|
1316
|
+
setError(String(caught.message ?? caught))
|
|
1317
|
+
} finally {
|
|
1318
|
+
setBusy(false)
|
|
1319
|
+
}
|
|
1320
|
+
}
|
|
1321
|
+
|
|
1274
1322
|
return h('div', { className: 'wbg-card wbg-card-soft' },
|
|
1275
1323
|
h('div', { className: 'wbg-row' },
|
|
1276
1324
|
h('strong', { className: 'wbg-sec-title' }, t.pairing),
|
|
@@ -1299,7 +1347,11 @@ window.__ModuleLoader__.load({
|
|
|
1299
1347
|
style: { borderTop: '1px solid var(--dsw-alias-border-l1, #eee)', paddingTop: 10 },
|
|
1300
1348
|
},
|
|
1301
1349
|
h('div', { className: 'wbg-row' },
|
|
1302
|
-
h('span', null, `${t.pairingFrom}「${entry.deviceName}」`),
|
|
1350
|
+
h('span', null, `${t.pairingFrom}「${entry.deviceName || t.unknownDevice}」`),
|
|
1351
|
+
/* 地址是我们从 socket 看到的,设备名可能是空的 —— 它才是可靠标识。 */
|
|
1352
|
+
entry.address === undefined || entry.address === ''
|
|
1353
|
+
? null
|
|
1354
|
+
: h('span', { className: 'wbg-caption wbg-mono' }, entry.address),
|
|
1303
1355
|
h('span', { className: 'wbg-spacer' }),
|
|
1304
1356
|
h('span', { className: 'wbg-caption' },
|
|
1305
1357
|
`${t.pairingExpires} ${new Date(entry.expiresAt).toLocaleTimeString()}`),
|
|
@@ -1372,6 +1424,55 @@ window.__ModuleLoader__.load({
|
|
|
1372
1424
|
h('span', { className: 'wbg-alert-icon', 'aria-hidden': true }, '!'),
|
|
1373
1425
|
h('span', { className: 'wbg-alert-body' }, error)),
|
|
1374
1426
|
|
|
1427
|
+
/* 已分享出去的凭据:能看到给了谁,并且能撤销。 */
|
|
1428
|
+
h('div', { className: 'wbg-field', style: { marginTop: 12 } },
|
|
1429
|
+
h('span', null, t.shares),
|
|
1430
|
+
shares.length === 0
|
|
1431
|
+
? h('p', { className: 'wbg-caption' }, t.sharesEmpty)
|
|
1432
|
+
: h('div', null, shares.map((share) => h('div', {
|
|
1433
|
+
key: share.keyId,
|
|
1434
|
+
style: { borderTop: '1px solid var(--dsw-alias-border-l1, #eee)', paddingTop: 8, marginTop: 8 },
|
|
1435
|
+
},
|
|
1436
|
+
h('div', { className: 'wbg-row' },
|
|
1437
|
+
h('span', null, share.deviceName === '' ? t.unknownDevice : share.deviceName),
|
|
1438
|
+
h('span', { className: 'wbg-caption wbg-mono' }, share.address),
|
|
1439
|
+
h('span', { className: 'wbg-spacer' }),
|
|
1440
|
+
h('span', { className: 'wbg-caption' },
|
|
1441
|
+
`${t.shareWhen} ${new Date(share.sharedAt).toLocaleString()}`),
|
|
1442
|
+
),
|
|
1443
|
+
h('div', { className: 'wbg-chips', style: { marginTop: 4 } },
|
|
1444
|
+
(share.models ?? []).map((name) => h('span', { key: name, className: 'wbg-chip wbg-mono' }, name)),
|
|
1445
|
+
),
|
|
1446
|
+
|
|
1447
|
+
revoking === share.keyId
|
|
1448
|
+
? h('div', { className: 'wbg-row', style: { marginTop: 6 } },
|
|
1449
|
+
h('span', { className: 'wbg-caption' }, t.shareKeyNote),
|
|
1450
|
+
h('span', { className: 'wbg-spacer' }),
|
|
1451
|
+
h('button', {
|
|
1452
|
+
type: 'button',
|
|
1453
|
+
className: 'wbg-btn wbg-btn-sm',
|
|
1454
|
+
onClick: () => setRevoking(undefined),
|
|
1455
|
+
disabled: busy,
|
|
1456
|
+
}, t.pairingCancel),
|
|
1457
|
+
h('button', {
|
|
1458
|
+
type: 'button',
|
|
1459
|
+
className: 'wbg-btn wbg-btn-sm wbg-btn-danger',
|
|
1460
|
+
onClick: () => revoke(share.keyId),
|
|
1461
|
+
disabled: busy,
|
|
1462
|
+
}, busy ? t.shareRevoking : t.shareRevoke),
|
|
1463
|
+
)
|
|
1464
|
+
: h('div', { className: 'wbg-row', style: { marginTop: 6 } },
|
|
1465
|
+
h('span', { className: 'wbg-spacer' }),
|
|
1466
|
+
h('button', {
|
|
1467
|
+
type: 'button',
|
|
1468
|
+
className: 'wbg-btn wbg-btn-sm',
|
|
1469
|
+
onClick: () => setRevoking(share.keyId),
|
|
1470
|
+
disabled: busy,
|
|
1471
|
+
}, t.shareRevoke),
|
|
1472
|
+
),
|
|
1473
|
+
))),
|
|
1474
|
+
),
|
|
1475
|
+
|
|
1375
1476
|
/* 本机名称:对方在发现列表里靠它认出该连哪台。 */
|
|
1376
1477
|
pairing.available === true
|
|
1377
1478
|
? h('div', { className: 'wbg-field', style: { marginTop: 10 } },
|
|
@@ -1463,7 +1564,7 @@ window.__ModuleLoader__.load({
|
|
|
1463
1564
|
* 这里**没有**任何对外分享的控件 —— client 模式下本机不监听、不发现、不接受
|
|
1464
1565
|
* 配对请求,那些配置项连同界面一起消失(见 src/routes.js 的 upstreamMode)。
|
|
1465
1566
|
*/
|
|
1466
|
-
function PeersCard({ state, t, onScan, onRequest, onClaim, onRemove, onRefresh, onDiagnose }) {
|
|
1567
|
+
function PeersCard({ state, t, onScan, onRequest, onClaim, onRemove, onRefresh, onDiagnose, onReset }) {
|
|
1467
1568
|
const peers = Array.isArray(state?.peers) ? state.peers : []
|
|
1468
1569
|
const pending = state?.pendingPairing
|
|
1469
1570
|
|
|
@@ -1557,6 +1658,17 @@ window.__ModuleLoader__.load({
|
|
|
1557
1658
|
onClick: claim,
|
|
1558
1659
|
disabled: busy,
|
|
1559
1660
|
}, busy ? t.peersClaiming : t.peersClaim),
|
|
1661
|
+
/*
|
|
1662
|
+
* 「重新申请」:申请过期或已被领过时,用户不该只能看着一条报错。
|
|
1663
|
+
* 清掉本机的等待态,回到可以重新申请的界面。
|
|
1664
|
+
*/
|
|
1665
|
+
h('button', {
|
|
1666
|
+
type: 'button',
|
|
1667
|
+
className: 'wbg-btn wbg-btn-sm',
|
|
1668
|
+
onClick: async () => { await onReset(); setNote(undefined); setError(undefined) },
|
|
1669
|
+
disabled: busy,
|
|
1670
|
+
style: { marginLeft: 6 },
|
|
1671
|
+
}, t.peersRetry),
|
|
1560
1672
|
),
|
|
1561
1673
|
),
|
|
1562
1674
|
|
|
@@ -1801,6 +1913,17 @@ window.__ModuleLoader__.load({
|
|
|
1801
1913
|
await reload()
|
|
1802
1914
|
}, [reload])
|
|
1803
1915
|
|
|
1916
|
+
/*
|
|
1917
|
+
* 撤销一次分享。
|
|
1918
|
+
*
|
|
1919
|
+
* 后端会**连带删掉那把专用 key** —— 它存在的唯一目的就是给那台设备用,
|
|
1920
|
+
* 留着等于留一把没人管的凭据。
|
|
1921
|
+
*/
|
|
1922
|
+
const revokeShare = useCallback(async (keyId) => {
|
|
1923
|
+
await adminPost('/share/revoke', { keyId })
|
|
1924
|
+
await reload()
|
|
1925
|
+
}, [reload])
|
|
1926
|
+
|
|
1804
1927
|
/* ── 模式切换与接收方动作 ─────────────────────────────────────── */
|
|
1805
1928
|
|
|
1806
1929
|
const switchMode = useCallback(async (mode) => {
|
|
@@ -1833,6 +1956,17 @@ window.__ModuleLoader__.load({
|
|
|
1833
1956
|
await reload()
|
|
1834
1957
|
}, [reload])
|
|
1835
1958
|
|
|
1959
|
+
/*
|
|
1960
|
+
* 放弃当前这次等待,重新开始。
|
|
1961
|
+
*
|
|
1962
|
+
* 用于「申请已失效」的情形:过了 5 分钟、或被本机取消过。重开之后用户
|
|
1963
|
+
* 会回到「找网关 / 填地址」那一步,而不是卡在一句报错上。
|
|
1964
|
+
*/
|
|
1965
|
+
const resetPairing = useCallback(async () => {
|
|
1966
|
+
await adminPost('/peers/reset', {})
|
|
1967
|
+
await reload()
|
|
1968
|
+
}, [reload])
|
|
1969
|
+
|
|
1836
1970
|
const removePeer = useCallback(async (id) => {
|
|
1837
1971
|
await adminPost('/peers/remove', { id })
|
|
1838
1972
|
await reload()
|
|
@@ -1981,6 +2115,7 @@ window.__ModuleLoader__.load({
|
|
|
1981
2115
|
onRemove: removePeer,
|
|
1982
2116
|
onRefresh: refreshPeer,
|
|
1983
2117
|
onDiagnose: diagnosePeers,
|
|
2118
|
+
onReset: resetPairing,
|
|
1984
2119
|
})
|
|
1985
2120
|
: h('div', { style: { display: 'flex', flexDirection: 'column', gap: 12 } },
|
|
1986
2121
|
h(PortCard, { state, t, onSave: savePort }),
|
|
@@ -1991,6 +2126,7 @@ window.__ModuleLoader__.load({
|
|
|
1991
2126
|
onApprove: approvePairing,
|
|
1992
2127
|
onDeny: denyPairing,
|
|
1993
2128
|
onSaveShareName: saveShareName,
|
|
2129
|
+
onRevoke: revokeShare,
|
|
1994
2130
|
}),
|
|
1995
2131
|
h(OriginCard, { state, t, onSave: saveOriginPolicy }),
|
|
1996
2132
|
),
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-connect-workbuddy-gateway",
|
|
3
|
-
"version": "1.4.
|
|
3
|
+
"version": "1.4.4",
|
|
4
4
|
"description": "把 dsh-connect-workbuddy 的 WorkBuddy 模型开放成 HTTP API:OpenAI Chat / Responses、Anthropic Messages、Gemini 四协议共用一个固定 base URL,模型按 API key 逐个勾选。也可切换成接收方,接入局域网内另一台设备分享的模型。",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"dsh-plugin",
|
package/src/config.js
CHANGED
|
@@ -160,7 +160,15 @@ export function normalizeConfig(value) {
|
|
|
160
160
|
return { version: CONFIG_VERSION, keys }
|
|
161
161
|
}
|
|
162
162
|
|
|
163
|
-
/**
|
|
163
|
+
/**
|
|
164
|
+
* 规整一把 key。`secret` 缺失的条目直接丢弃(不能用的 key 不该留在表里)。
|
|
165
|
+
*
|
|
166
|
+
* `sharedWith` 是**这把 key 是分享给谁用的**。只有通过局域网配对发出去的 key 才有
|
|
167
|
+
* 这个字段;自己手动建的 key 没有。
|
|
168
|
+
*
|
|
169
|
+
* 为什么必须记:不记的话,分享出去之后就看不出「哪把 key 给了哪台设备」,
|
|
170
|
+
* 于是既不能撤销某一次分享(不知道该删哪个),也不能在撤销时连带删掉那把 key。
|
|
171
|
+
*/
|
|
164
172
|
function normalizeKey(entry) {
|
|
165
173
|
if (entry === null || typeof entry !== 'object') return undefined
|
|
166
174
|
const secret = typeof entry.secret === 'string' ? entry.secret.trim() : ''
|
|
@@ -174,10 +182,28 @@ function normalizeKey(entry) {
|
|
|
174
182
|
...(typeof entry.lastUsedAt === 'number' ? { lastUsedAt: entry.lastUsedAt } : {}),
|
|
175
183
|
/* `seeded` 记录「已经自动补过模型勾选」,见 index.js 的 ensureSeeded。 */
|
|
176
184
|
...(entry.seeded === true ? { seeded: true } : {}),
|
|
185
|
+
...(normalizeSharedWith(entry.sharedWith) === undefined ? {} : { sharedWith: normalizeSharedWith(entry.sharedWith) }),
|
|
177
186
|
selections: normalizeSelections(entry.selections),
|
|
178
187
|
}
|
|
179
188
|
}
|
|
180
189
|
|
|
190
|
+
/**
|
|
191
|
+
* 规整「分享给谁」。
|
|
192
|
+
*
|
|
193
|
+
* `deviceName` 可以为空(对方没填),`address` 是服务端从 socket 看到的对端 IP ——
|
|
194
|
+
* 它才是可靠的那个标识,所以至少要有它。
|
|
195
|
+
*/
|
|
196
|
+
function normalizeSharedWith(value) {
|
|
197
|
+
if (value === null || typeof value !== 'object') return undefined
|
|
198
|
+
const address = typeof value.address === 'string' ? value.address.trim() : ''
|
|
199
|
+
if (address === '') return undefined
|
|
200
|
+
return {
|
|
201
|
+
deviceName: typeof value.deviceName === 'string' ? value.deviceName.trim().slice(0, 64) : '',
|
|
202
|
+
address,
|
|
203
|
+
sharedAt: typeof value.sharedAt === 'number' ? value.sharedAt : Date.now(),
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
181
207
|
/**
|
|
182
208
|
* 规整勾选清单。
|
|
183
209
|
*
|
package/src/pairing.js
CHANGED
|
@@ -57,13 +57,22 @@ export function createPairingRegistry({ logger, now = () => Date.now() } = {}) {
|
|
|
57
57
|
/**
|
|
58
58
|
* 接收方发起配对请求。
|
|
59
59
|
*
|
|
60
|
-
*
|
|
60
|
+
* `deviceName` **不是必填**:它只是给对方看的「谁在申请」,缺了给个中性占位
|
|
61
|
+
* 就行,不该让一次配对因为一个显示名而失败。之前这里返回 400,结果手动填地址
|
|
62
|
+
* 那条路径(不带设备名)直接报 `deviceName is required` —— 用户什么都没做错。
|
|
63
|
+
*
|
|
64
|
+
* `address` 是**服务端从 socket 拿到的对端地址**,不用客户端上报(上报的值可以
|
|
65
|
+
* 随便填)。它和设备名一起构成「这是谁」的唯一可靠标识:设备名可能为空、
|
|
66
|
+
* 可能两台机器同名,而 IP 至少能对上网络里的哪一台。
|
|
67
|
+
*
|
|
68
|
+
* @param input `{ deviceName?, address? }`
|
|
61
69
|
* @returns `{ nonce, expiresAt }` 或 `{ error }`
|
|
62
70
|
*/
|
|
63
71
|
function request(input) {
|
|
64
72
|
sweep()
|
|
65
|
-
const
|
|
66
|
-
|
|
73
|
+
const raw = typeof input?.deviceName === 'string' ? input.deviceName.trim() : ''
|
|
74
|
+
const deviceName = raw === '' ? '' : raw.slice(0, 64)
|
|
75
|
+
const address = typeof input?.address === 'string' ? input.address.trim().replace(/^::ffff:/u, '') : ''
|
|
67
76
|
|
|
68
77
|
if (pending.size >= MAX_PENDING) {
|
|
69
78
|
/* 满了就不收新的:拒绝比静默丢更诚实,接收方知道要等人处理。 */
|
|
@@ -78,6 +87,7 @@ export function createPairingRegistry({ logger, now = () => Date.now() } = {}) {
|
|
|
78
87
|
const entry = {
|
|
79
88
|
nonce,
|
|
80
89
|
deviceName,
|
|
90
|
+
address,
|
|
81
91
|
createdAt: now(),
|
|
82
92
|
expiresAt: now() + PAIR_TTL_MS,
|
|
83
93
|
/* 'pending' → 用户批准 → 'approved';批准后带一份要交出去的东西。 */
|
|
@@ -85,15 +95,19 @@ export function createPairingRegistry({ logger, now = () => Date.now() } = {}) {
|
|
|
85
95
|
grant: undefined,
|
|
86
96
|
}
|
|
87
97
|
pending.set(nonce, entry)
|
|
88
|
-
logger?.info?.(`workbuddy-gateway: pairing request from
|
|
98
|
+
logger?.info?.(`workbuddy-gateway: pairing request from ${describeRequester(entry)}`)
|
|
89
99
|
return { nonce, expiresAt: entry.expiresAt }
|
|
90
100
|
}
|
|
91
101
|
|
|
92
102
|
/**
|
|
93
103
|
* 本机批准一个请求,把「要交给对方的东西」挂上去。
|
|
94
104
|
*
|
|
105
|
+
* 返回值带上**申请方是谁**,好让调用方把它记进那把新 key 里 —— 否则分享出去
|
|
106
|
+
* 之后就再也说不清「这把 key 给了谁」,也就无从撤销。
|
|
107
|
+
*
|
|
95
108
|
* @param nonce 请求 id
|
|
96
109
|
* @param grant `{ baseUrl, secret, keyLabel, models }`
|
|
110
|
+
* @returns `{ ok, deviceName, address }` 或 `{ error }`
|
|
97
111
|
*/
|
|
98
112
|
function approve(nonce, grant) {
|
|
99
113
|
const entry = pending.get(nonce)
|
|
@@ -106,7 +120,7 @@ export function createPairingRegistry({ logger, now = () => Date.now() } = {}) {
|
|
|
106
120
|
entry.status = 'approved'
|
|
107
121
|
entry.grant = grant
|
|
108
122
|
entry.approvedAt = now()
|
|
109
|
-
return { ok: true }
|
|
123
|
+
return { ok: true, deviceName: entry.deviceName, address: entry.address }
|
|
110
124
|
}
|
|
111
125
|
|
|
112
126
|
/**
|
|
@@ -138,6 +152,7 @@ export function createPairingRegistry({ logger, now = () => Date.now() } = {}) {
|
|
|
138
152
|
return sweep().map((entry) => ({
|
|
139
153
|
nonce: entry.nonce,
|
|
140
154
|
deviceName: entry.deviceName,
|
|
155
|
+
address: entry.address,
|
|
141
156
|
createdAt: entry.createdAt,
|
|
142
157
|
expiresAt: entry.expiresAt,
|
|
143
158
|
status: entry.status,
|
|
@@ -147,6 +162,23 @@ export function createPairingRegistry({ logger, now = () => Date.now() } = {}) {
|
|
|
147
162
|
return { request, approve, claim, deny, list }
|
|
148
163
|
}
|
|
149
164
|
|
|
165
|
+
/**
|
|
166
|
+
* 「这是谁」的一行可读描述:设备名 + 地址。
|
|
167
|
+
*
|
|
168
|
+
* 设备名可能为空(对方没填、或取不到主机名),地址是我们自己从 socket 看到的,
|
|
169
|
+
* 所以至少有它。两者都缺时才退成 `a device`。
|
|
170
|
+
*
|
|
171
|
+
* @param entry 一条配对记录
|
|
172
|
+
*/
|
|
173
|
+
export function describeRequester(entry) {
|
|
174
|
+
const name = typeof entry?.deviceName === 'string' ? entry.deviceName.trim() : ''
|
|
175
|
+
const address = typeof entry?.address === 'string' ? entry.address.trim() : ''
|
|
176
|
+
if (name !== '' && address !== '') return `"${name}" (${address})`
|
|
177
|
+
if (name !== '') return `"${name}"`
|
|
178
|
+
if (address !== '') return address
|
|
179
|
+
return 'a device'
|
|
180
|
+
}
|
|
181
|
+
|
|
150
182
|
/**
|
|
151
183
|
* 常量时间比较两个字符串,避免按字节泄露时序。
|
|
152
184
|
*
|
package/src/peers.js
CHANGED
|
@@ -196,7 +196,7 @@ export async function requestPairing(baseUrl, deviceName, fetchImpl = fetch) {
|
|
|
196
196
|
*/
|
|
197
197
|
export async function claimPairing(baseUrl, nonce, fetchImpl = fetch) {
|
|
198
198
|
const root = String(baseUrl ?? '').trim().replace(/\/+$/u, '')
|
|
199
|
-
if (root === '' || typeof nonce !== 'string' || nonce === '') return { error: 'baseUrl and nonce are required' }
|
|
199
|
+
if (root === '' || typeof nonce !== 'string' || nonce === '') return { error: 'baseUrl and nonce are required', retryable: true }
|
|
200
200
|
try {
|
|
201
201
|
const response = await fetchImpl(`${root}/pair/claim?nonce=${encodeURIComponent(nonce)}`, {
|
|
202
202
|
signal: AbortSignal.timeout(8000),
|
|
@@ -208,11 +208,24 @@ export async function claimPairing(baseUrl, nonce, fetchImpl = fetch) {
|
|
|
208
208
|
}
|
|
209
209
|
const body = await response.json().catch(() => ({}))
|
|
210
210
|
if (!response.ok) {
|
|
211
|
-
|
|
211
|
+
/*
|
|
212
|
+
* 404 = 这个 nonce 已经没用了:要么过了 5 分钟有效期,要么本机刚取消过、
|
|
213
|
+
* 要么已经被领过。
|
|
214
|
+
*
|
|
215
|
+
* **这必须是可重试的。** 之前它被当成终止错误,界面就只能停在那里,用户
|
|
216
|
+
* 除了重装没有别的办法。现在标记 `retryable`,界面据此把「重新申请」的
|
|
217
|
+
* 入口重新给出来 —— 重新申请会拿一个新 nonce,流程照走。
|
|
218
|
+
*/
|
|
219
|
+
if (response.status === 404) {
|
|
220
|
+
return {
|
|
221
|
+
error: 'this pairing request expired or was already claimed; start a new request',
|
|
222
|
+
retryable: true,
|
|
223
|
+
}
|
|
224
|
+
}
|
|
212
225
|
return { error: body?.error?.message ?? `the gateway answered ${response.status}` }
|
|
213
226
|
}
|
|
214
227
|
if (typeof body?.secret !== 'string' || typeof body?.baseUrl !== 'string') {
|
|
215
|
-
return { error: 'the gateway returned an incomplete grant' }
|
|
228
|
+
return { error: 'the gateway returned an incomplete grant', retryable: true }
|
|
216
229
|
}
|
|
217
230
|
return {
|
|
218
231
|
grant: {
|
|
@@ -223,7 +236,7 @@ export async function claimPairing(baseUrl, nonce, fetchImpl = fetch) {
|
|
|
223
236
|
},
|
|
224
237
|
}
|
|
225
238
|
} catch (error) {
|
|
226
|
-
if (error?.name === 'TimeoutError') return { error: 'the gateway did not answer within 8 seconds' }
|
|
227
|
-
return { error: `cannot reach the gateway: ${error instanceof Error ? error.message : String(error)}
|
|
239
|
+
if (error?.name === 'TimeoutError') return { error: 'the gateway did not answer within 8 seconds', retryable: true }
|
|
240
|
+
return { error: `cannot reach the gateway: ${error instanceof Error ? error.message : String(error)}`, retryable: true }
|
|
228
241
|
}
|
|
229
242
|
}
|
package/src/routes.js
CHANGED
|
@@ -553,7 +553,81 @@ async function dispatchAdmin(req, res, ctx, env) {
|
|
|
553
553
|
return
|
|
554
554
|
}
|
|
555
555
|
|
|
556
|
-
|
|
556
|
+
/*
|
|
557
|
+
* **把这把 key 分享给了谁记下来。**
|
|
558
|
+
*
|
|
559
|
+
* 不记的话,分享出去之后就看不出「哪把 key 在哪台设备上」,于是既没法在
|
|
560
|
+
* 界面上撤销某一次分享(不知道删哪个),也没法在撤销时连带删掉那把 key。
|
|
561
|
+
*
|
|
562
|
+
* 记在批准成功之后:批准失败时上面已经把 key 删了,不该留下一条指向空
|
|
563
|
+
* key 的分享记录。
|
|
564
|
+
*/
|
|
565
|
+
const record = readConfig()
|
|
566
|
+
const target = record.keys.find((entry) => entry.id === created.id)
|
|
567
|
+
if (target !== undefined) {
|
|
568
|
+
target.sharedWith = {
|
|
569
|
+
deviceName: approved.deviceName ?? '',
|
|
570
|
+
address: approved.address ?? '',
|
|
571
|
+
sharedAt: Date.now(),
|
|
572
|
+
}
|
|
573
|
+
writeConfig(record)
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
sendJson(res, 200, {
|
|
577
|
+
ok: true,
|
|
578
|
+
keyId: created.id,
|
|
579
|
+
models: grant.models.length,
|
|
580
|
+
baseUrl,
|
|
581
|
+
sharedWith: target?.sharedWith,
|
|
582
|
+
})
|
|
583
|
+
return
|
|
584
|
+
}
|
|
585
|
+
if (method === 'POST' && path === '/share/revoke') {
|
|
586
|
+
/*
|
|
587
|
+
* 撤销一次分享。
|
|
588
|
+
*
|
|
589
|
+
* **同时删掉那把 key** —— 用户的要求是「撤销了分享,绑定的 key 一起删」。
|
|
590
|
+
* 这不是快捷方式而是语义问题:那把 key 的存在意义就是「给那台设备用」,
|
|
591
|
+
* 留着它等于留一把没人管的凭据。想只停用不删除,用 key 自己的「停用」。
|
|
592
|
+
*/
|
|
593
|
+
const body = await readJsonBody(req)
|
|
594
|
+
const keyId = typeof body?.keyId === 'string' ? body.keyId.trim() : ''
|
|
595
|
+
if (keyId === '') {
|
|
596
|
+
sendJson(res, 400, { error: 'keyId is required' })
|
|
597
|
+
return
|
|
598
|
+
}
|
|
599
|
+
const config = readConfig()
|
|
600
|
+
const target = config.keys.find((entry) => entry.id === keyId)
|
|
601
|
+
if (target === undefined) {
|
|
602
|
+
sendJson(res, 404, { error: `no such key: ${keyId}` })
|
|
603
|
+
return
|
|
604
|
+
}
|
|
605
|
+
config.keys = config.keys.filter((entry) => entry.id !== keyId)
|
|
606
|
+
writeConfig(config)
|
|
607
|
+
sendJson(res, 200, {
|
|
608
|
+
ok: true,
|
|
609
|
+
revoked: { id: target.id, label: target.label, sharedWith: target.sharedWith },
|
|
610
|
+
keys: publicKeys(readConfig(), env),
|
|
611
|
+
})
|
|
612
|
+
return
|
|
613
|
+
}
|
|
614
|
+
if (method === 'POST' && path === '/pair/cancel') {
|
|
615
|
+
/*
|
|
616
|
+
* 本机取消一个**待批准**的请求。
|
|
617
|
+
*
|
|
618
|
+
* 之前只能「拒绝」,而拒绝只对还没批准的请求有意义;批准之后那条记录已经
|
|
619
|
+
* 变成「等对方来领」,界面上还挂着,用户看着像卡住了。这里让两种状态都能
|
|
620
|
+
* 清掉:待批准的丢弃,已批准但还没领的也丢弃(对方再来领会拿到 404,
|
|
621
|
+
* 重新申请即可)。
|
|
622
|
+
*/
|
|
623
|
+
const body = await readJsonBody(req)
|
|
624
|
+
const nonce = typeof body?.nonce === 'string' ? body.nonce.trim() : ''
|
|
625
|
+
const cancelled = env.pairing?.deny(nonce)
|
|
626
|
+
if (cancelled?.error !== undefined) {
|
|
627
|
+
sendJson(res, 404, { error: cancelled.error })
|
|
628
|
+
return
|
|
629
|
+
}
|
|
630
|
+
sendJson(res, 200, { ok: true })
|
|
557
631
|
return
|
|
558
632
|
}
|
|
559
633
|
if (method === 'POST' && path === '/pair/deny') {
|
|
@@ -686,7 +760,17 @@ async function dispatchAdmin(req, res, ctx, env) {
|
|
|
686
760
|
sendJson(res, 400, { error: 'baseUrl must look like http://192.168.31.107:8790' })
|
|
687
761
|
return
|
|
688
762
|
}
|
|
689
|
-
|
|
763
|
+
/*
|
|
764
|
+
* `deviceName` 允许缺省,也允许是空串 —— 那是「用户没填名字」,不是错误。
|
|
765
|
+
*
|
|
766
|
+
* **不能用 `??`**:它只在 null/undefined 时兜底,而手动填地址那条路径传的
|
|
767
|
+
* 是空字符串,`??` 会放过它,于是 '' 一路走到 pairing.request 被拒成
|
|
768
|
+
* 「deviceName is required」。用户什么都没做错,却看到一个字段名报错。
|
|
769
|
+
*/
|
|
770
|
+
const deviceName = typeof body?.deviceName === 'string' && body.deviceName.trim() !== ''
|
|
771
|
+
? body.deviceName.trim()
|
|
772
|
+
: defaultDeviceName()
|
|
773
|
+
const result = await requestPairing(baseUrl, deviceName)
|
|
690
774
|
if (result.error !== undefined) {
|
|
691
775
|
sendJson(res, 400, { error: result.error })
|
|
692
776
|
return
|
|
@@ -700,6 +784,18 @@ async function dispatchAdmin(req, res, ctx, env) {
|
|
|
700
784
|
})
|
|
701
785
|
return
|
|
702
786
|
}
|
|
787
|
+
if (method === 'POST' && path === '/peers/reset') {
|
|
788
|
+
/*
|
|
789
|
+
* 放弃本机当前这次等待,回到「可以重新申请」的状态。
|
|
790
|
+
*
|
|
791
|
+
* 用在申请失效之后:nonce 过期、被对方取消、或已经被领过。没有这个接口
|
|
792
|
+
* 时,界面会一直挂着「等待批准」,而那个 nonce 已经没用了 —— 用户除了重装
|
|
793
|
+
* 没有别的办法。
|
|
794
|
+
*/
|
|
795
|
+
env.pendingPairing = undefined
|
|
796
|
+
sendJson(res, 200, { ok: true })
|
|
797
|
+
return
|
|
798
|
+
}
|
|
703
799
|
if (method === 'POST' && path === '/peers/claim') {
|
|
704
800
|
/*
|
|
705
801
|
* 领取被批准的结果并保存成一个 peer。
|
|
@@ -728,7 +824,15 @@ async function dispatchAdmin(req, res, ctx, env) {
|
|
|
728
824
|
return
|
|
729
825
|
}
|
|
730
826
|
if (result.error !== undefined) {
|
|
731
|
-
|
|
827
|
+
/*
|
|
828
|
+
* **失败时把这次等待清掉。**
|
|
829
|
+
*
|
|
830
|
+
* 否则界面会一直停在「等待批准」,而那个 nonce 已经没用了 —— 用户唯一
|
|
831
|
+
* 能做的就是重装。清掉之后设置页会重新给出「申请接入」的入口,重新申请
|
|
832
|
+
* 会拿到新 nonce,流程照走。
|
|
833
|
+
*/
|
|
834
|
+
env.pendingPairing = undefined
|
|
835
|
+
sendJson(res, 400, { error: result.error, retryable: result.retryable === true })
|
|
732
836
|
return
|
|
733
837
|
}
|
|
734
838
|
|
|
@@ -1002,6 +1106,22 @@ async function adminState(ctx, env) {
|
|
|
1002
1106
|
listening: env.listener?.listening === true,
|
|
1003
1107
|
...(env.listener?.error === undefined ? {} : { error: env.listener.error }),
|
|
1004
1108
|
},
|
|
1109
|
+
/*
|
|
1110
|
+
* 已经分享出去的凭据(每把带 `sharedWith` 的 key 一条)。
|
|
1111
|
+
*
|
|
1112
|
+
* 用户要能看到「我把模型分享给了谁」,并且能撤销。撤销连带删除那把 key。
|
|
1113
|
+
*/
|
|
1114
|
+
shares: readConfig().keys
|
|
1115
|
+
.filter((entry) => entry.sharedWith !== undefined)
|
|
1116
|
+
.map((entry) => ({
|
|
1117
|
+
keyId: entry.id,
|
|
1118
|
+
label: entry.label,
|
|
1119
|
+
enabled: entry.enabled !== false,
|
|
1120
|
+
deviceName: entry.sharedWith.deviceName,
|
|
1121
|
+
address: entry.sharedWith.address,
|
|
1122
|
+
sharedAt: entry.sharedWith.sharedAt,
|
|
1123
|
+
models: buildCatalog(entry.selections ?? [], publicAccounts(env)).map((model) => model.name),
|
|
1124
|
+
})),
|
|
1005
1125
|
/*
|
|
1006
1126
|
* 局域网发现与配对。
|
|
1007
1127
|
*
|
|
@@ -1426,6 +1546,19 @@ function publicPeer(peer) {
|
|
|
1426
1546
|
}
|
|
1427
1547
|
}
|
|
1428
1548
|
|
|
1549
|
+
/**
|
|
1550
|
+
* 对端地址(去掉 IPv6 映射前缀)。
|
|
1551
|
+
*
|
|
1552
|
+
* `::ffff:192.168.31.5` 这种是 IPv4 映射到 IPv6 的形式,剥掉前缀用户才认得。
|
|
1553
|
+
*
|
|
1554
|
+
* @param req Node 请求
|
|
1555
|
+
*/
|
|
1556
|
+
function peerAddressOf(req) {
|
|
1557
|
+
const raw = req?.socket?.remoteAddress
|
|
1558
|
+
if (typeof raw !== 'string') return ''
|
|
1559
|
+
return raw.replace(/^::ffff:/u, '').trim()
|
|
1560
|
+
}
|
|
1561
|
+
|
|
1429
1562
|
/**
|
|
1430
1563
|
* 本机在配对申请里自报的名字。
|
|
1431
1564
|
*
|
|
@@ -1477,7 +1610,14 @@ async function handlePairing(req, res, url, path, env) {
|
|
|
1477
1610
|
return
|
|
1478
1611
|
}
|
|
1479
1612
|
const body = await readJsonBody(req)
|
|
1480
|
-
|
|
1613
|
+
/*
|
|
1614
|
+
* 来源地址由**服务端从 socket 取**,不接受客户端自报 —— 自报的值想填什么都行,
|
|
1615
|
+
* 而 IP 是唯一能对上「网络里哪一台」的标识。
|
|
1616
|
+
*/
|
|
1617
|
+
const created = registry.request({
|
|
1618
|
+
deviceName: body?.deviceName,
|
|
1619
|
+
address: peerAddressOf(req),
|
|
1620
|
+
})
|
|
1481
1621
|
if (created.error !== undefined) {
|
|
1482
1622
|
sendJson(res, 400, { error: { message: created.error, type: 'invalid_request_error', code: 'pairing_rejected' } })
|
|
1483
1623
|
return
|