@tianacloud/dsh-remote 0.2.5 → 0.2.7
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/client.js +306 -32
- package/host-main.js +196 -15
- package/package.json +1 -1
- package/src/connect.js +55 -5
- package/src/dsh-packages.js +96 -7
- package/src/frontend-assets.js +90 -32
- package/src/host-bridge.js +76 -12
- package/src/remote-api.js +60 -1
- package/src/state.js +90 -2
- package/src/tiana-cli.js +126 -86
package/src/frontend-assets.js
CHANGED
|
@@ -140,34 +140,80 @@ function cleanBundle(file) {
|
|
|
140
140
|
const BUILTIN_CLIENT_PREFIX = '@deepseek-ai/dsh-';
|
|
141
141
|
const SELF_CLIENT_MODULE = '@tianacloud/dsh-remote';
|
|
142
142
|
|
|
143
|
+
export function isProtectedWebPlugin(id) {
|
|
144
|
+
return typeof id === 'string' && (id === BOOTSTRAP || id.startsWith(BUILTIN_CLIENT_PREFIX) || id === SELF_CLIENT_MODULE);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
function uniquePluginList(list) {
|
|
148
|
+
if (!Array.isArray(list)) return [];
|
|
149
|
+
return [...new Set(list.map((id) => String(id).trim()).filter(Boolean))];
|
|
150
|
+
}
|
|
151
|
+
|
|
143
152
|
/**
|
|
144
|
-
*
|
|
145
|
-
*
|
|
146
|
-
*
|
|
147
|
-
*
|
|
148
|
-
* -
|
|
153
|
+
* Normalize a plugin access policy or legacy allowlist into:
|
|
154
|
+
* `{ mode: 'allow_all' | 'deny_all', blockedWebPlugins: string[], allowedWebPlugins: string[] }`.
|
|
155
|
+
*
|
|
156
|
+
* - Policy object: respects `mode` (`'deny_all'` or default `'allow_all'`), `blockedWebPlugins`, `allowedWebPlugins`.
|
|
157
|
+
* - Legacy array/string allowlist: `'*'` -> `allow_all`; explicit list / `[]` -> `deny_all` with that allowlist.
|
|
158
|
+
* - Omitted (`undefined` / `null`): defaults to `allow_all` with empty blacklist.
|
|
149
159
|
*/
|
|
150
|
-
export function
|
|
151
|
-
|
|
152
|
-
?
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
if (
|
|
160
|
-
|
|
160
|
+
export function normalizePluginPolicy(policyOrAllowlist) {
|
|
161
|
+
if (policyOrAllowlist && typeof policyOrAllowlist === 'object' && !Array.isArray(policyOrAllowlist)) {
|
|
162
|
+
const mode = policyOrAllowlist.mode === 'deny_all' ? 'deny_all' : 'allow_all';
|
|
163
|
+
const blockedWebPlugins = uniquePluginList(policyOrAllowlist.blockedWebPlugins)
|
|
164
|
+
.filter((id) => !isProtectedWebPlugin(id));
|
|
165
|
+
const allowedWebPlugins = uniquePluginList(policyOrAllowlist.allowedWebPlugins)
|
|
166
|
+
.filter((id) => !isProtectedWebPlugin(id));
|
|
167
|
+
return { mode, blockedWebPlugins, allowedWebPlugins };
|
|
168
|
+
}
|
|
169
|
+
if (Array.isArray(policyOrAllowlist) || typeof policyOrAllowlist === 'string') {
|
|
170
|
+
const rawLimit = Array.isArray(policyOrAllowlist)
|
|
171
|
+
? policyOrAllowlist
|
|
172
|
+
: policyOrAllowlist.split(',');
|
|
173
|
+
const limit = uniquePluginList(rawLimit);
|
|
174
|
+
if (limit.includes('*')) {
|
|
175
|
+
return { mode: 'allow_all', blockedWebPlugins: [], allowedWebPlugins: [] };
|
|
176
|
+
}
|
|
177
|
+
return {
|
|
178
|
+
mode: 'deny_all',
|
|
179
|
+
blockedWebPlugins: [],
|
|
180
|
+
allowedWebPlugins: limit.filter((id) => !isProtectedWebPlugin(id)),
|
|
181
|
+
};
|
|
161
182
|
}
|
|
183
|
+
return { mode: 'allow_all', blockedWebPlugins: [], allowedWebPlugins: [] };
|
|
184
|
+
}
|
|
162
185
|
|
|
186
|
+
/**
|
|
187
|
+
* Select which active DSH client modules are exposed over the remote tunnel.
|
|
188
|
+
* Builtin DSH core (`@deepseek-ai/dsh-*`, `@deepseek-ai/dsh-client-modules`) and
|
|
189
|
+
* `@tianacloud/dsh-remote` are ALWAYS allowed and cannot be blacklisted.
|
|
190
|
+
*
|
|
191
|
+
* - `mode: 'allow_all'` (default): allow all active modules except `blockedWebPlugins`.
|
|
192
|
+
* - `mode: 'deny_all'`: allow only protected modules plus `allowedWebPlugins`.
|
|
193
|
+
* - Legacy array/string allowlists (`[]`, `['pkg']`, `['*']`) remain fully supported.
|
|
194
|
+
*/
|
|
195
|
+
export function selectAllowedWebPlugins(modules, policyOrAllowlist) {
|
|
196
|
+
const policy = normalizePluginPolicy(policyOrAllowlist);
|
|
163
197
|
const selected = new Set();
|
|
198
|
+
|
|
164
199
|
if (modules.has(BOOTSTRAP)) selected.add(BOOTSTRAP);
|
|
165
200
|
for (const id of modules.keys()) {
|
|
166
|
-
if (
|
|
201
|
+
if (isProtectedWebPlugin(id)) {
|
|
167
202
|
selected.add(id);
|
|
168
203
|
}
|
|
169
204
|
}
|
|
170
|
-
|
|
205
|
+
|
|
206
|
+
if (policy.mode === 'allow_all') {
|
|
207
|
+
const blocked = new Set(policy.blockedWebPlugins);
|
|
208
|
+
for (const id of modules.keys()) {
|
|
209
|
+
if (!blocked.has(id)) {
|
|
210
|
+
selected.add(id);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
return selected;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
for (const id of policy.allowedWebPlugins) {
|
|
171
217
|
if (modules.has(id)) {
|
|
172
218
|
selected.add(id);
|
|
173
219
|
}
|
|
@@ -187,13 +233,12 @@ function compilePlugins(graph, options, add) {
|
|
|
187
233
|
}
|
|
188
234
|
modules.set(row.id, row);
|
|
189
235
|
}
|
|
190
|
-
const
|
|
191
|
-
? options.allowlist
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
const
|
|
196
|
-
const selected = selectAllowedWebPlugins(modules, options.allowlist);
|
|
236
|
+
const policy = normalizePluginPolicy(
|
|
237
|
+
options.pluginPolicy !== undefined ? options.pluginPolicy : options.allowlist,
|
|
238
|
+
);
|
|
239
|
+
const allowAllUnblocked = policy.mode === 'allow_all' && policy.blockedWebPlugins.length === 0;
|
|
240
|
+
const blockedSet = new Set(policy.blockedWebPlugins);
|
|
241
|
+
const selected = selectAllowedWebPlugins(modules, policy);
|
|
197
242
|
if (modules.size > 0 && !modules.has(BOOTSTRAP)) {
|
|
198
243
|
throw new Error('A required DSH client module is absent: ' + BOOTSTRAP);
|
|
199
244
|
}
|
|
@@ -208,7 +253,7 @@ function compilePlugins(graph, options, add) {
|
|
|
208
253
|
const name = dependency.replace(/\/client$/u, '');
|
|
209
254
|
if (selected.has(name)) continue;
|
|
210
255
|
if (modules.has(name)) {
|
|
211
|
-
if (
|
|
256
|
+
if ((allowAllUnblocked && !blockedSet.has(name)) || isBuiltinOrBootstrap(name) || name === SELF_CLIENT_MODULE) {
|
|
212
257
|
selected.add(name);
|
|
213
258
|
} else if (isBuiltinOrBootstrap(id)) {
|
|
214
259
|
continue;
|
|
@@ -285,19 +330,32 @@ export class FrontendAssets {
|
|
|
285
330
|
this.waitingCompression = [];
|
|
286
331
|
}
|
|
287
332
|
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
333
|
+
getPluginPolicy() {
|
|
334
|
+
return normalizePluginPolicy(
|
|
335
|
+
this.options.pluginPolicy !== undefined ? this.options.pluginPolicy : this.options.allowlist,
|
|
336
|
+
);
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
setPluginPolicy(policyOrAllowlist) {
|
|
340
|
+
const next = normalizePluginPolicy(policyOrAllowlist);
|
|
341
|
+
const prev = this.getPluginPolicy();
|
|
342
|
+
if (JSON.stringify(next) === JSON.stringify(prev) && this.options.pluginPolicy !== undefined) {
|
|
343
|
+
return false;
|
|
344
|
+
}
|
|
345
|
+
this.options.pluginPolicy = next;
|
|
346
|
+
delete this.options.allowlist;
|
|
293
347
|
this.snapshot = null;
|
|
294
348
|
this.building = null;
|
|
295
349
|
return true;
|
|
296
350
|
}
|
|
297
351
|
|
|
352
|
+
setAllowlist(allowlist) {
|
|
353
|
+
return this.setPluginPolicy(allowlist);
|
|
354
|
+
}
|
|
355
|
+
|
|
298
356
|
async prepare(rows) {
|
|
299
357
|
const graph = rows.find((row) => row.kind === 'global' && row.name === '__DSH_BOOT__')?.value;
|
|
300
|
-
const signature = JSON.stringify({ graph: graph ?? null,
|
|
358
|
+
const signature = JSON.stringify({ graph: graph ?? null, policy: this.getPluginPolicy() });
|
|
301
359
|
if (!this.snapshot || this.snapshot.signature !== signature) {
|
|
302
360
|
const existing = this.building;
|
|
303
361
|
if (existing?.signature === signature) return existing.promise;
|
package/src/host-bridge.js
CHANGED
|
@@ -87,18 +87,38 @@ const RESERVED_HTTP_PREFIX_SEGMENTS = new Set([
|
|
|
87
87
|
'__dsh_remote__',
|
|
88
88
|
'_tiana',
|
|
89
89
|
'connect',
|
|
90
|
-
'plugins',
|
|
91
90
|
'oauth',
|
|
92
91
|
'inspector',
|
|
93
92
|
'open-in-app',
|
|
94
93
|
]);
|
|
95
94
|
|
|
95
|
+
function isBlockedPluginsRoute(segments) {
|
|
96
|
+
// segments[0] is 'plugins' (case-insensitive)
|
|
97
|
+
const trimmed = segments.length > 1 && segments[segments.length - 1] === ''
|
|
98
|
+
? segments.slice(0, -1)
|
|
99
|
+
: segments;
|
|
100
|
+
if (trimmed.length < 2) return true; // /plugins or /plugins/
|
|
101
|
+
const first = trimmed[1].toLowerCase();
|
|
102
|
+
if (!first || first === 'events' || first.startsWith('??')) return true;
|
|
103
|
+
const subIndex = first.startsWith('@') ? 3 : 2;
|
|
104
|
+
if (trimmed.length <= subIndex) return true; // /plugins/<pkg> or /plugins/@scope/<pkg>
|
|
105
|
+
const subFirst = trimmed[subIndex].toLowerCase();
|
|
106
|
+
if (!subFirst || subFirst === 'assets') return true;
|
|
107
|
+
// Block client.js, client.<hash>.js, and sourcemap variants
|
|
108
|
+
if (trimmed.length === subIndex + 1 && /^client(?:\.[a-z0-9._-]+)?\.js(?:\.map)?$/iu.test(subFirst)) {
|
|
109
|
+
return true;
|
|
110
|
+
}
|
|
111
|
+
return false;
|
|
112
|
+
}
|
|
113
|
+
|
|
96
114
|
/**
|
|
97
115
|
* Validate and classify a tunneled HTTP target.
|
|
98
116
|
* - Blocks malformed/traversal paths and reserved DSH core or shell routes (returns null -> 404).
|
|
117
|
+
* - Blocks `/plugins` static bundles, chunks, assets, and `/plugins/events` HMR, while forwarding
|
|
118
|
+
* third-party plugin API routes such as `/plugins/<pkg>/<api>` and `/plugins/@scope/<pkg>/<api>`.
|
|
99
119
|
* - Blocks `/api/tiana-remote/admin` and `/api/tiana-remote/admin/*` (returns `{ kind: 'forbidden' }` -> 403).
|
|
100
|
-
* - Allows `/api/*` (including `/api/tiana-remote/status
|
|
101
|
-
* and third-party plugin HTTP routes.
|
|
120
|
+
* - Allows `/api/*` (including `/api/tiana-remote/status`, `/api/tiana-remote/public-status`, and
|
|
121
|
+
* `/api/tiana-remote/plugin-access`) and third-party plugin HTTP routes.
|
|
102
122
|
*/
|
|
103
123
|
export function classifyHttpRoute(pathAndQuery) {
|
|
104
124
|
if (typeof pathAndQuery !== 'string' || !pathAndQuery.startsWith('/') ||
|
|
@@ -141,7 +161,11 @@ export function classifyHttpRoute(pathAndQuery) {
|
|
|
141
161
|
if (RESERVED_HTTP_EXACT_PATHS.has(trimmedLower)) {
|
|
142
162
|
return null;
|
|
143
163
|
}
|
|
144
|
-
|
|
164
|
+
const firstSegment = segments[0].toLowerCase();
|
|
165
|
+
if (RESERVED_HTTP_PREFIX_SEGMENTS.has(firstSegment)) {
|
|
166
|
+
return null;
|
|
167
|
+
}
|
|
168
|
+
if (firstSegment === 'plugins' && isBlockedPluginsRoute(segments)) {
|
|
145
169
|
return null;
|
|
146
170
|
}
|
|
147
171
|
if (/^\/api\/tiana-remote\/admin(?:\/|$)/iu.test(trimmedLower)) {
|
|
@@ -232,6 +256,12 @@ export class HostBridge {
|
|
|
232
256
|
this.stopped = false;
|
|
233
257
|
this.reconnectAttempts = 0;
|
|
234
258
|
this.reconnectTimer = null;
|
|
259
|
+
this.stableTimer = null;
|
|
260
|
+
this.stableConnectionMs = options.stableConnectionMs ?? 15_000;
|
|
261
|
+
this.conflictMinBackoffMs = options.conflictMinBackoffMs ?? 5_000;
|
|
262
|
+
this.hostConflict = false;
|
|
263
|
+
this.lastCloseCode = null;
|
|
264
|
+
this.lastCloseReason = '';
|
|
235
265
|
this.connectedPromise = null;
|
|
236
266
|
this.browserCookie = null;
|
|
237
267
|
this.browserCookieGeneration = 0;
|
|
@@ -301,6 +331,9 @@ export class HostBridge {
|
|
|
301
331
|
return {
|
|
302
332
|
running: !this.stopped,
|
|
303
333
|
relayConnected: this.registered,
|
|
334
|
+
hostConflict: this.hostConflict,
|
|
335
|
+
lastCloseCode: this.lastCloseCode,
|
|
336
|
+
lastCloseReason: this.lastCloseReason,
|
|
304
337
|
hostId: this.hostId,
|
|
305
338
|
hostName: this.hostName,
|
|
306
339
|
p2pEnabled: this.p2pEnabled,
|
|
@@ -337,7 +370,7 @@ export class HostBridge {
|
|
|
337
370
|
return { kicked };
|
|
338
371
|
}
|
|
339
372
|
|
|
340
|
-
updateSettings({ hostName, p2pEnabled, stunUrls, allowedWebPlugins, relayToken } = {}) {
|
|
373
|
+
updateSettings({ hostName, p2pEnabled, stunUrls, pluginPolicy, allowedWebPlugins, relayToken } = {}) {
|
|
341
374
|
if (typeof hostName === 'string' && hostName.trim()) {
|
|
342
375
|
this.hostName = hostName.trim().slice(0, 128);
|
|
343
376
|
}
|
|
@@ -347,7 +380,9 @@ export class HostBridge {
|
|
|
347
380
|
if (stunUrls !== undefined || p2pEnabled !== undefined) {
|
|
348
381
|
this.iceServers = this.p2pEnabled ? normalizeStun(stunUrls ?? this.iceServers) : [];
|
|
349
382
|
}
|
|
350
|
-
if (
|
|
383
|
+
if (pluginPolicy !== undefined) {
|
|
384
|
+
this.frontend.setPluginPolicy(pluginPolicy);
|
|
385
|
+
} else if (allowedWebPlugins !== undefined) {
|
|
351
386
|
this.frontend.setAllowlist(allowedWebPlugins);
|
|
352
387
|
}
|
|
353
388
|
if (typeof relayToken === 'string' && relayToken) {
|
|
@@ -381,6 +416,10 @@ export class HostBridge {
|
|
|
381
416
|
clearTimeout(this.reconnectTimer);
|
|
382
417
|
this.reconnectTimer = null;
|
|
383
418
|
}
|
|
419
|
+
if (this.stableTimer) {
|
|
420
|
+
clearTimeout(this.stableTimer);
|
|
421
|
+
this.stableTimer = null;
|
|
422
|
+
}
|
|
384
423
|
this.cleanupActiveStreams();
|
|
385
424
|
if (this.ws) {
|
|
386
425
|
try {
|
|
@@ -402,11 +441,18 @@ export class HostBridge {
|
|
|
402
441
|
this.relayLegs.clear();
|
|
403
442
|
}
|
|
404
443
|
|
|
405
|
-
scheduleReconnect() {
|
|
444
|
+
scheduleReconnect({ replaced = false, code = 0, reason = '' } = {}) {
|
|
406
445
|
if (this.stopped || this.reconnectTimer) return;
|
|
407
|
-
|
|
446
|
+
let delayMs = Math.min(30_000, 1000 * Math.pow(2, Math.min(this.reconnectAttempts, 5)));
|
|
447
|
+
if (replaced) {
|
|
448
|
+
delayMs = Math.max(this.conflictMinBackoffMs, delayMs);
|
|
449
|
+
}
|
|
408
450
|
this.reconnectAttempts += 1;
|
|
409
|
-
|
|
451
|
+
const detail = reason ? ` (${code}: ${reason})` : code ? ` (${code})` : '';
|
|
452
|
+
this.log(`[dsh-remote] Relay disconnected${detail}; reconnecting in ${Math.round(delayMs / 1000)}s...`);
|
|
453
|
+
if (replaced) {
|
|
454
|
+
this.log(`[dsh-remote] Another DSH host process appears to be connected with host_id "${this.hostId}". Stop duplicate DSH instances to restore a stable connection.`);
|
|
455
|
+
}
|
|
410
456
|
this.reconnectTimer = setTimeout(() => {
|
|
411
457
|
this.reconnectTimer = null;
|
|
412
458
|
if (!this.stopped) {
|
|
@@ -452,7 +498,15 @@ export class HostBridge {
|
|
|
452
498
|
}
|
|
453
499
|
clearTimeout(registrationTimeout);
|
|
454
500
|
this.registered = true;
|
|
455
|
-
this.
|
|
501
|
+
clearTimeout(this.stableTimer);
|
|
502
|
+
this.stableTimer = setTimeout(() => {
|
|
503
|
+
if (this.ws === ws && this.registered) {
|
|
504
|
+
this.reconnectAttempts = 0;
|
|
505
|
+
this.hostConflict = false;
|
|
506
|
+
}
|
|
507
|
+
this.stableTimer = null;
|
|
508
|
+
}, this.stableConnectionMs);
|
|
509
|
+
this.stableTimer.unref?.();
|
|
456
510
|
this.log(`[dsh-remote] Registered host "${this.hostId}"`);
|
|
457
511
|
settled = true;
|
|
458
512
|
resolve();
|
|
@@ -463,14 +517,24 @@ export class HostBridge {
|
|
|
463
517
|
});
|
|
464
518
|
});
|
|
465
519
|
|
|
466
|
-
ws.addEventListener('close', () => {
|
|
520
|
+
ws.addEventListener('close', (event) => {
|
|
467
521
|
clearTimeout(registrationTimeout);
|
|
468
522
|
if (this.ws === ws) {
|
|
523
|
+
clearTimeout(this.stableTimer);
|
|
524
|
+
this.stableTimer = null;
|
|
469
525
|
this.registered = false;
|
|
470
526
|
this.ws = null;
|
|
527
|
+
const code = Number(event?.code || 0);
|
|
528
|
+
const reason = typeof event?.reason === 'string' ? event.reason : '';
|
|
529
|
+
this.lastCloseCode = code || null;
|
|
530
|
+
this.lastCloseReason = reason;
|
|
531
|
+
const replaced = /replaced by newer host connection/iu.test(reason);
|
|
532
|
+
if (replaced) {
|
|
533
|
+
this.hostConflict = true;
|
|
534
|
+
}
|
|
471
535
|
for (const [id] of this.relayLegs) this.detachRelay(id);
|
|
472
536
|
if (!this.stopped) {
|
|
473
|
-
this.scheduleReconnect();
|
|
537
|
+
this.scheduleReconnect({ replaced, code, reason });
|
|
474
538
|
}
|
|
475
539
|
}
|
|
476
540
|
if (!settled) {
|
package/src/remote-api.js
CHANGED
|
@@ -299,6 +299,51 @@ export function createRemoteApiHandler(controller, connection = null) {
|
|
|
299
299
|
: jsonResponse(200, pub);
|
|
300
300
|
}
|
|
301
301
|
|
|
302
|
+
// 1b. Plugin Access Policy (editable from BOTH local owner session AND E2EE-authenticated remote tunnel)
|
|
303
|
+
if (path === '/api/tiana-remote/plugin-access') {
|
|
304
|
+
if (method !== 'GET' && method !== 'HEAD' && method !== 'POST') {
|
|
305
|
+
return jsonResponse(405, { ok: false, error: 'Method not allowed' });
|
|
306
|
+
}
|
|
307
|
+
const pubReject = await guardPublicRequest(request, connection, meta);
|
|
308
|
+
if (pubReject) return pubReject;
|
|
309
|
+
try {
|
|
310
|
+
if (method === 'GET' || method === 'HEAD') {
|
|
311
|
+
const policy = await controller.getPluginAccess();
|
|
312
|
+
return method === 'HEAD'
|
|
313
|
+
? new Response(null, { status: 200, headers: { 'cache-control': 'no-store', 'x-content-type-options': 'nosniff' } })
|
|
314
|
+
: jsonResponse(200, policy);
|
|
315
|
+
}
|
|
316
|
+
const body = await readJsonBody(request);
|
|
317
|
+
const patch = {};
|
|
318
|
+
if (body.mode !== undefined) {
|
|
319
|
+
if (body.mode !== 'allow_all' && body.mode !== 'deny_all') {
|
|
320
|
+
return jsonResponse(400, { ok: false, error: 'mode must be "allow_all" or "deny_all"' });
|
|
321
|
+
}
|
|
322
|
+
patch.mode = body.mode;
|
|
323
|
+
}
|
|
324
|
+
if (body.blockedWebPlugins !== undefined) {
|
|
325
|
+
if (!Array.isArray(body.blockedWebPlugins)) {
|
|
326
|
+
return jsonResponse(400, { ok: false, error: 'blockedWebPlugins must be an array' });
|
|
327
|
+
}
|
|
328
|
+
patch.blockedWebPlugins = body.blockedWebPlugins.map((p) => String(p).trim()).filter(Boolean);
|
|
329
|
+
}
|
|
330
|
+
if (body.allowedWebPlugins !== undefined) {
|
|
331
|
+
if (!Array.isArray(body.allowedWebPlugins)) {
|
|
332
|
+
return jsonResponse(400, { ok: false, error: 'allowedWebPlugins must be an array' });
|
|
333
|
+
}
|
|
334
|
+
patch.allowedWebPlugins = body.allowedWebPlugins.map((p) => String(p).trim()).filter(Boolean);
|
|
335
|
+
}
|
|
336
|
+
const updated = await controller.updatePluginAccess(patch);
|
|
337
|
+
return jsonResponse(200, updated);
|
|
338
|
+
} catch (err) {
|
|
339
|
+
const status = Number.isInteger(err?.status) ? err.status : 500;
|
|
340
|
+
return jsonResponse(status, {
|
|
341
|
+
ok: false,
|
|
342
|
+
error: err instanceof Error ? err.message : String(err),
|
|
343
|
+
});
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
|
|
302
347
|
// 2. Local-only Admin endpoints (triple-guarded)
|
|
303
348
|
if (path === '/api/tiana-remote/admin' || path.startsWith('/api/tiana-remote/admin/')) {
|
|
304
349
|
const adminReject = await guardAdminRequest(request, connection, meta);
|
|
@@ -379,6 +424,18 @@ export function createRemoteApiHandler(controller, connection = null) {
|
|
|
379
424
|
return jsonResponse(400, { ok: false, error: err.message });
|
|
380
425
|
}
|
|
381
426
|
}
|
|
427
|
+
if (body.pluginAccessMode !== undefined) {
|
|
428
|
+
if (body.pluginAccessMode !== 'allow_all' && body.pluginAccessMode !== 'deny_all') {
|
|
429
|
+
return jsonResponse(400, { ok: false, error: 'pluginAccessMode must be "allow_all" or "deny_all"' });
|
|
430
|
+
}
|
|
431
|
+
patch.pluginAccessMode = body.pluginAccessMode;
|
|
432
|
+
}
|
|
433
|
+
if (body.blockedWebPlugins !== undefined) {
|
|
434
|
+
if (!Array.isArray(body.blockedWebPlugins)) {
|
|
435
|
+
return jsonResponse(400, { ok: false, error: 'blockedWebPlugins must be an array' });
|
|
436
|
+
}
|
|
437
|
+
patch.blockedWebPlugins = body.blockedWebPlugins.map((p) => String(p).trim()).filter(Boolean);
|
|
438
|
+
}
|
|
382
439
|
if (body.allowedWebPlugins !== undefined) {
|
|
383
440
|
if (!Array.isArray(body.allowedWebPlugins)) {
|
|
384
441
|
return jsonResponse(400, { ok: false, error: 'allowedWebPlugins must be an array' });
|
|
@@ -522,6 +579,7 @@ export function registerWebServerRoutes(webServer, connection, controller) {
|
|
|
522
579
|
const disposers = [
|
|
523
580
|
webServer.register({ kind: 'exact', path: '/api/tiana-remote/public-status', handler: handleNode }),
|
|
524
581
|
webServer.register({ kind: 'exact', path: '/api/tiana-remote/status', handler: handleNode }),
|
|
582
|
+
webServer.register({ kind: 'exact', path: '/api/tiana-remote/plugin-access', handler: handleNode }),
|
|
525
583
|
webServer.register({ kind: 'prefix', path: '/api/tiana-remote/admin', handler: handleNode }),
|
|
526
584
|
];
|
|
527
585
|
|
|
@@ -535,7 +593,7 @@ export function registerWebServerRoutes(webServer, connection, controller) {
|
|
|
535
593
|
/**
|
|
536
594
|
* Fallback route mounting onto `connection.fetch.register` when `webServer.register` is unavailable.
|
|
537
595
|
* Because Fetch requests lack a verifiable TCP `remoteAddress`, all `/api/tiana-remote/admin/*`
|
|
538
|
-
* routes fail closed with 403 while public status
|
|
596
|
+
* routes fail closed with 403 while public status and plugin-access policy remain available.
|
|
539
597
|
*/
|
|
540
598
|
export function registerRemoteRoutes(connection, controller) {
|
|
541
599
|
const registry = connection?.fetch;
|
|
@@ -546,6 +604,7 @@ export function registerRemoteRoutes(connection, controller) {
|
|
|
546
604
|
const routes = [
|
|
547
605
|
{ path: '/api/tiana-remote/status', methods: ['GET', 'HEAD'] },
|
|
548
606
|
{ path: '/api/tiana-remote/public-status', methods: ['GET', 'HEAD'] },
|
|
607
|
+
{ path: '/api/tiana-remote/plugin-access', methods: ['GET', 'HEAD', 'POST'] },
|
|
549
608
|
{ path: '/api/tiana-remote/admin/overview', methods: ['GET', 'HEAD'] },
|
|
550
609
|
{ path: '/api/tiana-remote/admin/status', methods: ['GET', 'HEAD'] },
|
|
551
610
|
{ path: '/api/tiana-remote/admin/toggle', methods: ['POST'] },
|
package/src/state.js
CHANGED
|
@@ -14,6 +14,9 @@ import {
|
|
|
14
14
|
|
|
15
15
|
export const STATE_CONFIG_VERSION = 2;
|
|
16
16
|
export const DEFAULT_STUN_URLS = ['stun:stun.cloudflare.com:3478'];
|
|
17
|
+
export const PLUGIN_ACCESS_MODE_ALLOW_ALL = 'allow_all';
|
|
18
|
+
export const PLUGIN_ACCESS_MODE_DENY_ALL = 'deny_all';
|
|
19
|
+
export const DEFAULT_PLUGIN_ACCESS_MODE = PLUGIN_ACCESS_MODE_ALLOW_ALL;
|
|
17
20
|
|
|
18
21
|
export { formatPairingKey, normalizePairingKey };
|
|
19
22
|
|
|
@@ -37,6 +40,10 @@ export function defaultRuntimePath() {
|
|
|
37
40
|
return join(defaultStateDir(), 'runtime.json');
|
|
38
41
|
}
|
|
39
42
|
|
|
43
|
+
export function defaultBridgeLockPath(statePath = defaultStatePath()) {
|
|
44
|
+
return join(dirname(resolveStatePath(statePath)), 'bridge-owner.json');
|
|
45
|
+
}
|
|
46
|
+
|
|
40
47
|
function verifyPrivate(stat, type) {
|
|
41
48
|
if ((type === 'directory' ? !stat.isDirectory() : !stat.isFile() || stat.nlink !== 1) ||
|
|
42
49
|
(process.platform !== 'win32' && (stat.mode & 0o077) !== 0) ||
|
|
@@ -130,6 +137,16 @@ export function migrateRemoteState(raw) {
|
|
|
130
137
|
? raw.network.stunUrls.map((u) => String(u).trim()).filter(Boolean)
|
|
131
138
|
: [...DEFAULT_STUN_URLS];
|
|
132
139
|
const p2pEnabled = raw.network?.p2pEnabled !== undefined ? Boolean(raw.network.p2pEnabled) : true;
|
|
140
|
+
const rawMode = raw.security?.pluginAccessMode ?? raw.pluginAccessMode;
|
|
141
|
+
const pluginAccessMode = rawMode === PLUGIN_ACCESS_MODE_DENY_ALL
|
|
142
|
+
? PLUGIN_ACCESS_MODE_DENY_ALL
|
|
143
|
+
: DEFAULT_PLUGIN_ACCESS_MODE;
|
|
144
|
+
const rawBlocked = Array.isArray(raw.security?.blockedWebPlugins)
|
|
145
|
+
? raw.security.blockedWebPlugins
|
|
146
|
+
: Array.isArray(raw.blockedWebPlugins)
|
|
147
|
+
? raw.blockedWebPlugins
|
|
148
|
+
: [];
|
|
149
|
+
const blockedWebPlugins = [...new Set(rawBlocked.map((item) => String(item).trim()).filter(Boolean))];
|
|
133
150
|
const rawAllowed = Array.isArray(raw.security?.allowedWebPlugins)
|
|
134
151
|
? raw.security.allowedWebPlugins
|
|
135
152
|
: Array.isArray(raw.allowedWebPlugins)
|
|
@@ -148,6 +165,8 @@ export function migrateRemoteState(raw) {
|
|
|
148
165
|
},
|
|
149
166
|
security: {
|
|
150
167
|
...(raw.security && typeof raw.security === 'object' ? raw.security : {}),
|
|
168
|
+
pluginAccessMode,
|
|
169
|
+
blockedWebPlugins,
|
|
151
170
|
allowedWebPlugins,
|
|
152
171
|
},
|
|
153
172
|
allowedWebPlugins,
|
|
@@ -181,7 +200,7 @@ export function loadNormalizedRemoteState(filePath = defaultStatePath()) {
|
|
|
181
200
|
return migrateRemoteState(raw);
|
|
182
201
|
}
|
|
183
202
|
|
|
184
|
-
function isPidAlive(pid) {
|
|
203
|
+
export function isPidAlive(pid) {
|
|
185
204
|
if (!Number.isInteger(pid) || pid <= 0) return false;
|
|
186
205
|
try {
|
|
187
206
|
process.kill(pid, 0);
|
|
@@ -316,12 +335,19 @@ export async function withStateLock(fn, filePath = defaultStatePath(), { timeout
|
|
|
316
335
|
}
|
|
317
336
|
}
|
|
318
337
|
|
|
319
|
-
export function writeRuntimeInfo(info, filePath = defaultRuntimePath()) {
|
|
338
|
+
export function writeRuntimeInfo(info, filePath = defaultRuntimePath(), { overwriteLiveOther = false } = {}) {
|
|
339
|
+
if (!overwriteLiveOther) {
|
|
340
|
+
const existing = readRuntimeInfo(filePath);
|
|
341
|
+
if (existing && existing.pid !== process.pid) {
|
|
342
|
+
return false;
|
|
343
|
+
}
|
|
344
|
+
}
|
|
320
345
|
saveRemoteState({
|
|
321
346
|
pid: process.pid,
|
|
322
347
|
startedAt: new Date().toISOString(),
|
|
323
348
|
...info,
|
|
324
349
|
}, filePath);
|
|
350
|
+
return true;
|
|
325
351
|
}
|
|
326
352
|
|
|
327
353
|
export function readRuntimeInfo(filePath = defaultRuntimePath()) {
|
|
@@ -345,6 +371,68 @@ export function removeRuntimeInfo(filePath = defaultRuntimePath()) {
|
|
|
345
371
|
}
|
|
346
372
|
}
|
|
347
373
|
|
|
374
|
+
/**
|
|
375
|
+
* Atomically claim exclusive HostBridge ownership for a state directory across OS processes.
|
|
376
|
+
* Prevents two DSH processes sharing the same state.json from fighting over the same Relay host_id.
|
|
377
|
+
*/
|
|
378
|
+
export async function claimBridgeOwnership(
|
|
379
|
+
ownerInfo = {},
|
|
380
|
+
statePath = defaultStatePath(),
|
|
381
|
+
{ pid = process.pid, isAlive = isPidAlive } = {},
|
|
382
|
+
) {
|
|
383
|
+
const targetStatePath = resolveStatePath(statePath);
|
|
384
|
+
const lockPath = defaultBridgeLockPath(targetStatePath);
|
|
385
|
+
return await withStateLock(() => {
|
|
386
|
+
let existing = null;
|
|
387
|
+
try {
|
|
388
|
+
existing = loadRemoteState(lockPath);
|
|
389
|
+
} catch {
|
|
390
|
+
existing = null;
|
|
391
|
+
}
|
|
392
|
+
const sameOwner = existing
|
|
393
|
+
&& existing.pid === pid
|
|
394
|
+
&& (!existing.instanceId || !ownerInfo.instanceId || existing.instanceId === ownerInfo.instanceId);
|
|
395
|
+
if (existing && Number.isInteger(existing.pid) && !sameOwner && isAlive(existing.pid)) {
|
|
396
|
+
return { acquired: false, owner: existing };
|
|
397
|
+
}
|
|
398
|
+
const record = {
|
|
399
|
+
pid,
|
|
400
|
+
instanceId: ownerInfo.instanceId || '',
|
|
401
|
+
hostId: ownerInfo.hostId || '',
|
|
402
|
+
origin: ownerInfo.origin || '',
|
|
403
|
+
advertisedOrigin: ownerInfo.advertisedOrigin || '',
|
|
404
|
+
acquiredAt: new Date().toISOString(),
|
|
405
|
+
};
|
|
406
|
+
saveRemoteState(record, lockPath);
|
|
407
|
+
return { acquired: true, owner: record };
|
|
408
|
+
}, targetStatePath);
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
export async function releaseBridgeOwnership(
|
|
412
|
+
instanceId = '',
|
|
413
|
+
statePath = defaultStatePath(),
|
|
414
|
+
{ pid = process.pid } = {},
|
|
415
|
+
) {
|
|
416
|
+
const targetStatePath = resolveStatePath(statePath);
|
|
417
|
+
const lockPath = defaultBridgeLockPath(targetStatePath);
|
|
418
|
+
try {
|
|
419
|
+
return await withStateLock(() => {
|
|
420
|
+
let existing = null;
|
|
421
|
+
try {
|
|
422
|
+
existing = loadRemoteState(lockPath);
|
|
423
|
+
} catch {
|
|
424
|
+
existing = null;
|
|
425
|
+
}
|
|
426
|
+
if (!existing) return false;
|
|
427
|
+
if (existing.pid !== pid) return false;
|
|
428
|
+
if (instanceId && existing.instanceId && existing.instanceId !== instanceId) return false;
|
|
429
|
+
try { unlinkSync(lockPath); return true; } catch { return false; }
|
|
430
|
+
}, targetStatePath);
|
|
431
|
+
} catch {
|
|
432
|
+
return false;
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
|
|
348
436
|
export function sanitizeHostId(raw) {
|
|
349
437
|
const cleaned = String(raw || '')
|
|
350
438
|
.toLowerCase()
|