@tianacloud/dsh-remote 0.0.0-stage → 0.2.2
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/LICENSE +204 -0
- package/README.md +44 -2
- package/cli.js +347 -0
- package/client.js +1501 -0
- package/cordis.patch.yml +3 -0
- package/host-main.js +628 -0
- package/icon.svg +7 -0
- package/locale/en-US.json +9 -0
- package/locale/zh-CN.json +9 -0
- package/package.json +89 -4
- package/relay-go/go.mod +3 -0
- package/relay-go/main.go +1711 -0
- package/scripts/build-web-dist.mjs +46 -0
- package/src/clipboard.js +94 -0
- package/src/connect.js +599 -0
- package/src/dsh-packages.js +46 -0
- package/src/frontend-assets.js +405 -0
- package/src/host-bridge.js +1412 -0
- package/src/local-transport.js +496 -0
- package/src/protocol.js +124 -0
- package/src/remote-api.js +433 -0
- package/src/state.js +409 -0
- package/src/tiana-cli.js +1271 -0
- package/web-src/dsh-remote-entry.mjs +2692 -0
- package/web-src/dsh-remote-sw.js +593 -0
- package/web-src/e2ee.js +460 -0
- package/web-src/frontend-loader.js +450 -0
- package/web-src/multipath.js +375 -0
- package/web-src/peer-path.js +265 -0
- package/web-src/shell-ui.js +280 -0
- package/web-src/ws-fragments.js +46 -0
package/cordis.patch.yml
ADDED
package/host-main.js
ADDED
|
@@ -0,0 +1,628 @@
|
|
|
1
|
+
import { existsSync, mkdirSync, watch } from 'node:fs';
|
|
2
|
+
import { dirname } from 'node:path';
|
|
3
|
+
import { HostBridge } from './src/host-bridge.js';
|
|
4
|
+
import { registerRemoteRoutes, registerWebServerRoutes } from './src/remote-api.js';
|
|
5
|
+
import {
|
|
6
|
+
defaultRuntimePath,
|
|
7
|
+
defaultStatePath,
|
|
8
|
+
DEFAULT_STUN_URLS,
|
|
9
|
+
ensureE2eeConfig,
|
|
10
|
+
loadNormalizedRemoteState,
|
|
11
|
+
loadRemoteState,
|
|
12
|
+
removeRuntimeInfo,
|
|
13
|
+
saveRemoteState,
|
|
14
|
+
withStateLock,
|
|
15
|
+
writeRuntimeInfo,
|
|
16
|
+
} from './src/state.js';
|
|
17
|
+
import {
|
|
18
|
+
bindTianaRemoteWeb,
|
|
19
|
+
buildRemotePolicy,
|
|
20
|
+
ensureHostToken,
|
|
21
|
+
getTianaStatus,
|
|
22
|
+
provisionTianaRemote,
|
|
23
|
+
republishWebShell,
|
|
24
|
+
updateRemotePolicy,
|
|
25
|
+
} from './src/tiana-cli.js';
|
|
26
|
+
|
|
27
|
+
export const name = 'tiana-remote';
|
|
28
|
+
export const inject = ['webServer', 'connection'];
|
|
29
|
+
|
|
30
|
+
const TOKEN_CHECK_INTERVAL_MS = 12 * 60 * 60 * 1000;
|
|
31
|
+
const TOKEN_RENEW_LEAD_SECONDS = 7 * 86400;
|
|
32
|
+
|
|
33
|
+
export function createHostController(options = {}) {
|
|
34
|
+
const statePath = options.statePath || defaultStatePath();
|
|
35
|
+
const runtimePath = options.runtimePath || defaultRuntimePath();
|
|
36
|
+
const log = options.log || console.log;
|
|
37
|
+
const createBridge = options.createBridge || ((cfg) => new HostBridge(cfg));
|
|
38
|
+
const getWebServer = options.getWebServer || (() => null);
|
|
39
|
+
const getConnection = options.getConnection || (() => null);
|
|
40
|
+
const getClientModules = options.getClientModules || (() => null);
|
|
41
|
+
const getWebApp = options.getWebApp || (() => null);
|
|
42
|
+
|
|
43
|
+
let bridge = null;
|
|
44
|
+
let activeStateKey = '';
|
|
45
|
+
let disposed = false;
|
|
46
|
+
let watcher = null;
|
|
47
|
+
let reloadTimer = null;
|
|
48
|
+
let tokenTimer = null;
|
|
49
|
+
let provisionTask = {
|
|
50
|
+
running: false,
|
|
51
|
+
step: 'idle',
|
|
52
|
+
logs: [],
|
|
53
|
+
error: null,
|
|
54
|
+
completedAt: null,
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
function getEnvOverrides() {
|
|
58
|
+
return {
|
|
59
|
+
disabled: process.env.DSH_REMOTE_DISABLE === '1',
|
|
60
|
+
p2p: process.env.DSH_REMOTE_P2P !== undefined,
|
|
61
|
+
stun: process.env.DSH_REMOTE_STUN !== undefined,
|
|
62
|
+
allowedWebPlugins: process.env.DSH_REMOTE_WEB_PLUGINS !== undefined,
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function effectiveConfig(state) {
|
|
67
|
+
const p2p = process.env.DSH_REMOTE_P2P ?? state?.network?.p2pEnabled ?? true;
|
|
68
|
+
const stun = process.env.DSH_REMOTE_STUN ?? state?.network?.stunUrls ?? DEFAULT_STUN_URLS;
|
|
69
|
+
const allowlist = process.env.DSH_REMOTE_WEB_PLUGINS ?? state?.security?.allowedWebPlugins ?? [];
|
|
70
|
+
return { p2p, stun, allowlist };
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function stateSignature(state) {
|
|
74
|
+
if (!state) return '';
|
|
75
|
+
return JSON.stringify({
|
|
76
|
+
enabled: state.enabled,
|
|
77
|
+
hostId: state.host?.hostId,
|
|
78
|
+
hostName: state.host?.hostName,
|
|
79
|
+
reg: state.host?.registrationKey,
|
|
80
|
+
fnUrl: state.function?.url,
|
|
81
|
+
fnToken: state.function?.token,
|
|
82
|
+
webId: state.web?.id,
|
|
83
|
+
owner: state.owner?.userId,
|
|
84
|
+
tenant: state.owner?.tenantId,
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function buildBridgeOptions(state) {
|
|
89
|
+
const webServer = getWebServer();
|
|
90
|
+
const connection = getConnection();
|
|
91
|
+
const clientModules = getClientModules();
|
|
92
|
+
const webApp = getWebApp();
|
|
93
|
+
const eff = effectiveConfig(state);
|
|
94
|
+
return {
|
|
95
|
+
relayUrl: state.function.url,
|
|
96
|
+
relayToken: state.function.token,
|
|
97
|
+
e2eeSecret: state.e2ee.secret,
|
|
98
|
+
hostId: state.host.hostId,
|
|
99
|
+
hostName: state.host.hostName || state.host.hostId,
|
|
100
|
+
registrationKey: state.host.registrationKey,
|
|
101
|
+
localOrigin: options.localOrigin || (() => webServer.getOrigin()),
|
|
102
|
+
inProcessEndpoint: options.inProcessEndpoint || (webServer ? () => ({
|
|
103
|
+
listenOrigin: webServer.getOrigin(),
|
|
104
|
+
boundHost: webServer?.host,
|
|
105
|
+
advertisedOrigin: webApp?.getAdvertisedUrl?.(),
|
|
106
|
+
}) : null),
|
|
107
|
+
getLaunchToken: options.getLaunchToken || (connection ? () => {
|
|
108
|
+
const url = new URL(connection.authenticatedUrl('http://127.0.0.1/'));
|
|
109
|
+
return url.searchParams.get('token') || '';
|
|
110
|
+
} : null),
|
|
111
|
+
activeClientPath: options.activeClientPath || ((id) => clientModules?.artifactBaseline?.(id)?.path),
|
|
112
|
+
collectIndexInjections: options.collectIndexInjections || (webServer && connection
|
|
113
|
+
? () => webServer.collectIndexInjections(connection.browserPeer)
|
|
114
|
+
: undefined),
|
|
115
|
+
ownerUserId: state.owner?.userId || '',
|
|
116
|
+
ownerTenantId: state.owner?.tenantId || '',
|
|
117
|
+
webId: state.web?.id || '',
|
|
118
|
+
p2p: eff.p2p,
|
|
119
|
+
stun: eff.stun,
|
|
120
|
+
frontendOptions: { allowlist: eff.allowlist },
|
|
121
|
+
log,
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
function stopBridge() {
|
|
126
|
+
if (bridge) {
|
|
127
|
+
bridge.stop();
|
|
128
|
+
bridge = null;
|
|
129
|
+
}
|
|
130
|
+
activeStateKey = '';
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
async function syncBridgeWithState() {
|
|
134
|
+
if (disposed) return;
|
|
135
|
+
if (process.env.DSH_REMOTE_DISABLE === '1') {
|
|
136
|
+
stopBridge();
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
const raw = loadRemoteState(statePath);
|
|
140
|
+
if (!raw?.function?.url || !raw?.function?.token || !raw?.e2ee?.secret || !raw?.host?.hostId) {
|
|
141
|
+
stopBridge();
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
if (raw.version !== 2) {
|
|
145
|
+
log('[dsh-remote] Legacy v1 state detected; run `dsh-remote connect` or `dsh-remote setup` to upgrade.');
|
|
146
|
+
stopBridge();
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
const state = loadNormalizedRemoteState(statePath);
|
|
150
|
+
if (!state?.enabled) {
|
|
151
|
+
stopBridge();
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
const sig = stateSignature(state);
|
|
156
|
+
if (bridge && activeStateKey === sig) {
|
|
157
|
+
// Hot-update mutable settings (secret rotation, network, plugins) without dropping Relay WS
|
|
158
|
+
const eff = effectiveConfig(state);
|
|
159
|
+
if (bridge.e2eeSecret !== state.e2ee.secret) {
|
|
160
|
+
await bridge.rotateE2eeSecret(state.e2ee.secret);
|
|
161
|
+
}
|
|
162
|
+
bridge.updateSettings({
|
|
163
|
+
hostName: state.host.hostName,
|
|
164
|
+
p2pEnabled: eff.p2p,
|
|
165
|
+
stunUrls: eff.stun,
|
|
166
|
+
allowedWebPlugins: eff.allowlist,
|
|
167
|
+
relayToken: state.function.token,
|
|
168
|
+
});
|
|
169
|
+
return;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
stopBridge();
|
|
173
|
+
bridge = createBridge(buildBridgeOptions(state));
|
|
174
|
+
activeStateKey = sig;
|
|
175
|
+
await bridge.start();
|
|
176
|
+
if (!disposed) {
|
|
177
|
+
log(`[dsh-remote] E2EE active (${state.e2ee.algorithm}, key ${state.e2ee.fingerprint})`);
|
|
178
|
+
log(`[dsh-remote] Remote access ready: ${state.web?.endpoint}`);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
async function checkAndAutoRenewToken() {
|
|
183
|
+
if (disposed) return;
|
|
184
|
+
const state = loadNormalizedRemoteState(statePath);
|
|
185
|
+
if (!state?.function?.id || !state?.function?.expiresAt) return;
|
|
186
|
+
const now = Math.floor(Date.now() / 1000);
|
|
187
|
+
if (state.function.expiresAt - now > TOKEN_RENEW_LEAD_SECONDS) return;
|
|
188
|
+
try {
|
|
189
|
+
await controller.renewHostToken();
|
|
190
|
+
log('[dsh-remote] Automatically renewed Function Instance Token.');
|
|
191
|
+
} catch (err) {
|
|
192
|
+
log(`[dsh-remote] Auto token renew skipped: ${err instanceof Error ? err.message : String(err)}`);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
function startStateWatcher() {
|
|
197
|
+
try {
|
|
198
|
+
const dir = dirname(statePath);
|
|
199
|
+
mkdirSync(dir, { recursive: true, mode: 0o700 });
|
|
200
|
+
watcher = watch(dir, (_eventType, filename) => {
|
|
201
|
+
if (disposed) return;
|
|
202
|
+
if (filename && !String(filename).includes('state.json')) return;
|
|
203
|
+
clearTimeout(reloadTimer);
|
|
204
|
+
reloadTimer = setTimeout(() => {
|
|
205
|
+
void syncBridgeWithState().catch((err) => {
|
|
206
|
+
log(`[dsh-remote] Hot-reload error: ${err instanceof Error ? err.message : String(err)}`);
|
|
207
|
+
});
|
|
208
|
+
}, 250);
|
|
209
|
+
});
|
|
210
|
+
watcher.on('error', () => {});
|
|
211
|
+
} catch {
|
|
212
|
+
// Non-fatal if filesystem watcher unavailable
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
async function listLocalWebPlugins() {
|
|
217
|
+
try {
|
|
218
|
+
const webServer = getWebServer();
|
|
219
|
+
const connection = getConnection();
|
|
220
|
+
if (!webServer || !connection) return [];
|
|
221
|
+
const rows = await webServer.collectIndexInjections(connection.browserPeer);
|
|
222
|
+
const graph = rows?.find((r) => r.kind === 'global' && r.name === '__DSH_BOOT__')?.value;
|
|
223
|
+
if (!Array.isArray(graph?.entries)) return [];
|
|
224
|
+
return graph.entries
|
|
225
|
+
.map((e) => e.id)
|
|
226
|
+
.filter((id) => typeof id === 'string' && !id.startsWith('@deepseek-ai/dsh-client-') && id !== '@tianacloud/dsh-remote');
|
|
227
|
+
} catch {
|
|
228
|
+
return [];
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
const controller = {
|
|
233
|
+
getBridge: () => bridge,
|
|
234
|
+
|
|
235
|
+
async boot() {
|
|
236
|
+
const webServer = getWebServer();
|
|
237
|
+
try {
|
|
238
|
+
writeRuntimeInfo({ origin: webServer?.getOrigin?.() || '' }, runtimePath);
|
|
239
|
+
} catch { /* non-fatal */ }
|
|
240
|
+
if (process.env.DSH_REMOTE_DISABLE !== '1') {
|
|
241
|
+
const existing = loadRemoteState(statePath);
|
|
242
|
+
if (!existing?.function?.url && process.env.DSH_REMOTE_AUTO_SETUP === '1') {
|
|
243
|
+
log('[dsh-remote] Auto-provisioning Tiana remote resources...');
|
|
244
|
+
await provisionTianaRemote({ statePath, log });
|
|
245
|
+
}
|
|
246
|
+
await syncBridgeWithState();
|
|
247
|
+
}
|
|
248
|
+
startStateWatcher();
|
|
249
|
+
tokenTimer = setInterval(() => void checkAndAutoRenewToken(), TOKEN_CHECK_INTERVAL_MS);
|
|
250
|
+
tokenTimer.unref?.();
|
|
251
|
+
void checkAndAutoRenewToken();
|
|
252
|
+
},
|
|
253
|
+
|
|
254
|
+
dispose() {
|
|
255
|
+
disposed = true;
|
|
256
|
+
clearTimeout(reloadTimer);
|
|
257
|
+
clearInterval(tokenTimer);
|
|
258
|
+
try { watcher?.close(); } catch { /* ignore */ }
|
|
259
|
+
watcher = null;
|
|
260
|
+
stopBridge();
|
|
261
|
+
removeRuntimeInfo(runtimePath);
|
|
262
|
+
},
|
|
263
|
+
|
|
264
|
+
async getPublicStatus() {
|
|
265
|
+
const state = loadNormalizedRemoteState(statePath);
|
|
266
|
+
const snap = bridge?.getStatusSnapshot();
|
|
267
|
+
const configured = Boolean(state?.function?.url && state?.web?.id && state?.host?.hostId);
|
|
268
|
+
return {
|
|
269
|
+
configured,
|
|
270
|
+
enabled: configured && state.enabled !== false && process.env.DSH_REMOTE_DISABLE !== '1',
|
|
271
|
+
hostName: state?.host?.hostName || null,
|
|
272
|
+
hostId: state?.host?.hostId || null,
|
|
273
|
+
webId: state?.web?.id || null,
|
|
274
|
+
webEndpoint: state?.web?.endpoint || null,
|
|
275
|
+
e2eeFingerprint: state?.e2ee?.fingerprint || null,
|
|
276
|
+
p2pEnabled: snap?.p2pEnabled ?? (state?.network?.p2pEnabled !== false),
|
|
277
|
+
};
|
|
278
|
+
},
|
|
279
|
+
|
|
280
|
+
async getAdminOverview() {
|
|
281
|
+
const state = loadNormalizedRemoteState(statePath);
|
|
282
|
+
const snap = bridge?.getStatusSnapshot() || {
|
|
283
|
+
running: false,
|
|
284
|
+
relayConnected: false,
|
|
285
|
+
sessions: [],
|
|
286
|
+
p2pEnabled: state?.network?.p2pEnabled !== false,
|
|
287
|
+
stunUrls: state?.network?.stunUrls || DEFAULT_STUN_URLS,
|
|
288
|
+
};
|
|
289
|
+
let tiana = { loggedIn: false };
|
|
290
|
+
try {
|
|
291
|
+
const status = await getTianaStatus({ timeoutMs: 5000 });
|
|
292
|
+
tiana = {
|
|
293
|
+
loggedIn: true,
|
|
294
|
+
email: status.user?.email || '',
|
|
295
|
+
displayName: status.user?.display_name || '',
|
|
296
|
+
spaceName: status.space?.display_name || '',
|
|
297
|
+
tenantId: status.user?.tenant_id || '',
|
|
298
|
+
};
|
|
299
|
+
} catch {
|
|
300
|
+
// leave loggedIn: false
|
|
301
|
+
}
|
|
302
|
+
const installedWebPlugins = await listLocalWebPlugins();
|
|
303
|
+
const configured = Boolean(state?.function?.url && state?.web?.id && state?.host?.hostId);
|
|
304
|
+
return {
|
|
305
|
+
ok: true,
|
|
306
|
+
configured,
|
|
307
|
+
enabled: configured ? state.enabled !== false : false,
|
|
308
|
+
envOverrides: getEnvOverrides(),
|
|
309
|
+
bridge: snap,
|
|
310
|
+
tiana,
|
|
311
|
+
owner: state?.owner ? {
|
|
312
|
+
userId: state.owner.userId,
|
|
313
|
+
tenantId: state.owner.tenantId,
|
|
314
|
+
email: state.owner.email,
|
|
315
|
+
displayName: state.owner.displayName,
|
|
316
|
+
spaceName: state.owner.spaceName,
|
|
317
|
+
} : null,
|
|
318
|
+
host: state?.host ? {
|
|
319
|
+
hostId: state.host.hostId,
|
|
320
|
+
hostName: state.host.hostName,
|
|
321
|
+
} : null,
|
|
322
|
+
web: state?.web ? {
|
|
323
|
+
id: state.web.id,
|
|
324
|
+
name: state.web.name,
|
|
325
|
+
endpoint: state.web.endpoint,
|
|
326
|
+
} : null,
|
|
327
|
+
function: state?.function ? {
|
|
328
|
+
id: state.function.id,
|
|
329
|
+
name: state.function.name,
|
|
330
|
+
url: state.function.url,
|
|
331
|
+
expiresAt: state.function.expiresAt,
|
|
332
|
+
autoRenew: true,
|
|
333
|
+
} : null,
|
|
334
|
+
e2ee: state?.e2ee?.secret ? {
|
|
335
|
+
algorithm: state.e2ee.algorithm,
|
|
336
|
+
fingerprint: state.e2ee.fingerprint,
|
|
337
|
+
createdAt: state.e2ee.createdAt,
|
|
338
|
+
} : null,
|
|
339
|
+
network: state?.network || { p2pEnabled: true, stunUrls: [...DEFAULT_STUN_URLS] },
|
|
340
|
+
security: state?.security || { allowedWebPlugins: [] },
|
|
341
|
+
allowedWebPlugins: state?.security?.allowedWebPlugins || [],
|
|
342
|
+
installedWebPlugins,
|
|
343
|
+
};
|
|
344
|
+
},
|
|
345
|
+
|
|
346
|
+
async setEnabled(enabled) {
|
|
347
|
+
if (process.env.DSH_REMOTE_DISABLE === '1' && enabled) {
|
|
348
|
+
throw Object.assign(new Error('Remote access is disabled by DSH_REMOTE_DISABLE=1 environment variable'), { status: 409 });
|
|
349
|
+
}
|
|
350
|
+
const updated = await withStateLock(() => {
|
|
351
|
+
const state = loadNormalizedRemoteState(statePath);
|
|
352
|
+
if (!state) throw Object.assign(new Error('Remote access is not configured yet'), { status: 400 });
|
|
353
|
+
state.enabled = Boolean(enabled);
|
|
354
|
+
state.updatedAt = new Date().toISOString();
|
|
355
|
+
saveRemoteState(state, statePath);
|
|
356
|
+
return state;
|
|
357
|
+
}, statePath);
|
|
358
|
+
if (!updated.enabled) {
|
|
359
|
+
stopBridge();
|
|
360
|
+
} else {
|
|
361
|
+
await syncBridgeWithState();
|
|
362
|
+
}
|
|
363
|
+
return { enabled: updated.enabled, relayConnected: Boolean(bridge?.registered) };
|
|
364
|
+
},
|
|
365
|
+
|
|
366
|
+
async revealPairingKey() {
|
|
367
|
+
const state = loadNormalizedRemoteState(statePath);
|
|
368
|
+
if (!state?.e2ee?.secret) {
|
|
369
|
+
throw Object.assign(new Error('Pairing key is not initialized'), { status: 404 });
|
|
370
|
+
}
|
|
371
|
+
return {
|
|
372
|
+
secret: state.e2ee.secret,
|
|
373
|
+
fingerprint: state.e2ee.fingerprint,
|
|
374
|
+
};
|
|
375
|
+
},
|
|
376
|
+
|
|
377
|
+
async rotatePairingKey() {
|
|
378
|
+
const rotated = await withStateLock(() => {
|
|
379
|
+
const state = loadNormalizedRemoteState(statePath);
|
|
380
|
+
if (!state) throw Object.assign(new Error('Remote access is not configured yet'), { status: 400 });
|
|
381
|
+
const { e2ee } = ensureE2eeConfig(state, { rotate: true });
|
|
382
|
+
state.e2ee = e2ee;
|
|
383
|
+
state.updatedAt = new Date().toISOString();
|
|
384
|
+
saveRemoteState(state, statePath);
|
|
385
|
+
return e2ee;
|
|
386
|
+
}, statePath);
|
|
387
|
+
|
|
388
|
+
let kickedSessions = 0;
|
|
389
|
+
if (bridge) {
|
|
390
|
+
const res = await bridge.rotateE2eeSecret(rotated.secret);
|
|
391
|
+
kickedSessions = res.kicked;
|
|
392
|
+
}
|
|
393
|
+
return {
|
|
394
|
+
secret: rotated.secret,
|
|
395
|
+
fingerprint: rotated.fingerprint,
|
|
396
|
+
createdAt: rotated.createdAt,
|
|
397
|
+
kickedSessions,
|
|
398
|
+
};
|
|
399
|
+
},
|
|
400
|
+
|
|
401
|
+
async disconnectSession(sessionId) {
|
|
402
|
+
if (!bridge) return false;
|
|
403
|
+
return await bridge.disconnectSession(sessionId);
|
|
404
|
+
},
|
|
405
|
+
|
|
406
|
+
async disconnectAllSessions() {
|
|
407
|
+
if (!bridge) return 0;
|
|
408
|
+
return await bridge.disconnectAllSessions();
|
|
409
|
+
},
|
|
410
|
+
|
|
411
|
+
async updateSettings(patch) {
|
|
412
|
+
let hostNameChanged = false;
|
|
413
|
+
const saved = await withStateLock(() => {
|
|
414
|
+
const state = loadNormalizedRemoteState(statePath);
|
|
415
|
+
if (!state) throw Object.assign(new Error('Remote access is not configured yet'), { status: 400 });
|
|
416
|
+
if (patch.hostName !== undefined && patch.hostName !== state.host?.hostName) {
|
|
417
|
+
state.host = { ...state.host, hostName: patch.hostName };
|
|
418
|
+
hostNameChanged = true;
|
|
419
|
+
}
|
|
420
|
+
if (patch.p2pEnabled !== undefined || patch.stunUrls !== undefined) {
|
|
421
|
+
state.network = {
|
|
422
|
+
p2pEnabled: patch.p2pEnabled !== undefined ? patch.p2pEnabled : state.network.p2pEnabled,
|
|
423
|
+
stunUrls: patch.stunUrls !== undefined ? patch.stunUrls : state.network.stunUrls,
|
|
424
|
+
};
|
|
425
|
+
}
|
|
426
|
+
if (patch.allowedWebPlugins !== undefined) {
|
|
427
|
+
state.security = {
|
|
428
|
+
...state.security,
|
|
429
|
+
allowedWebPlugins: patch.allowedWebPlugins,
|
|
430
|
+
};
|
|
431
|
+
state.allowedWebPlugins = patch.allowedWebPlugins;
|
|
432
|
+
}
|
|
433
|
+
state.updatedAt = new Date().toISOString();
|
|
434
|
+
saveRemoteState(state, statePath);
|
|
435
|
+
return state;
|
|
436
|
+
}, statePath);
|
|
437
|
+
|
|
438
|
+
if (bridge) {
|
|
439
|
+
const eff = effectiveConfig(saved);
|
|
440
|
+
bridge.updateSettings({
|
|
441
|
+
hostName: saved.host?.hostName,
|
|
442
|
+
p2pEnabled: eff.p2p,
|
|
443
|
+
stunUrls: eff.stun,
|
|
444
|
+
allowedWebPlugins: eff.allowlist,
|
|
445
|
+
});
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
if (hostNameChanged && saved.function?.id && saved.web?.endpoint) {
|
|
449
|
+
try {
|
|
450
|
+
const policy = buildRemotePolicy({
|
|
451
|
+
hostId: saved.host.hostId,
|
|
452
|
+
hostName: saved.host.hostName,
|
|
453
|
+
registrationKey: saved.host.registrationKey,
|
|
454
|
+
ownerUserId: saved.owner.userId,
|
|
455
|
+
ownerTenantId: saved.owner.tenantId,
|
|
456
|
+
webId: saved.web.id,
|
|
457
|
+
origin: new URL(saved.web.endpoint).origin,
|
|
458
|
+
});
|
|
459
|
+
await updateRemotePolicy(saved.function.id, policy, { log });
|
|
460
|
+
} catch (err) {
|
|
461
|
+
log(`[dsh-remote] Failed to sync updated hostName to Relay policy: ${err instanceof Error ? err.message : String(err)}`);
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
return {
|
|
466
|
+
host: { hostId: saved.host?.hostId, hostName: saved.host?.hostName },
|
|
467
|
+
network: saved.network,
|
|
468
|
+
security: saved.security,
|
|
469
|
+
};
|
|
470
|
+
},
|
|
471
|
+
|
|
472
|
+
async repairStaticShell() {
|
|
473
|
+
const state = loadNormalizedRemoteState(statePath);
|
|
474
|
+
if (!state?.web?.id || !state?.function?.id) {
|
|
475
|
+
throw Object.assign(new Error('Remote access is not configured yet'), { status: 400 });
|
|
476
|
+
}
|
|
477
|
+
const policy = buildRemotePolicy({
|
|
478
|
+
hostId: state.host.hostId,
|
|
479
|
+
hostName: state.host.hostName,
|
|
480
|
+
registrationKey: state.host.registrationKey,
|
|
481
|
+
ownerUserId: state.owner.userId,
|
|
482
|
+
ownerTenantId: state.owner.tenantId,
|
|
483
|
+
webId: state.web.id,
|
|
484
|
+
origin: new URL(state.web.endpoint).origin,
|
|
485
|
+
});
|
|
486
|
+
await updateRemotePolicy(state.function.id, policy, { log });
|
|
487
|
+
const publishResult = await republishWebShell(state, { log });
|
|
488
|
+
return { repaired: true, publishResult };
|
|
489
|
+
},
|
|
490
|
+
|
|
491
|
+
async renewHostToken() {
|
|
492
|
+
const initial = await withStateLock(() => loadNormalizedRemoteState(statePath), statePath);
|
|
493
|
+
const expectedFunctionId = initial?.function?.id;
|
|
494
|
+
if (!expectedFunctionId) {
|
|
495
|
+
throw Object.assign(new Error('Relay Function is not configured yet'), { status: 400 });
|
|
496
|
+
}
|
|
497
|
+
const tokenInfo = await ensureHostToken(initial, expectedFunctionId, { rotateToken: true, log });
|
|
498
|
+
const committed = await withStateLock(() => {
|
|
499
|
+
const cur = loadNormalizedRemoteState(statePath);
|
|
500
|
+
if (!cur || cur.function?.id !== expectedFunctionId) {
|
|
501
|
+
return null;
|
|
502
|
+
}
|
|
503
|
+
if (cur.function.token && Number(cur.function.expiresAt) > Number(tokenInfo.expiresAt)) {
|
|
504
|
+
return {
|
|
505
|
+
token: cur.function.token,
|
|
506
|
+
tokenId: cur.function.tokenId,
|
|
507
|
+
expiresAt: cur.function.expiresAt,
|
|
508
|
+
};
|
|
509
|
+
}
|
|
510
|
+
cur.function = {
|
|
511
|
+
...cur.function,
|
|
512
|
+
token: tokenInfo.token,
|
|
513
|
+
tokenId: tokenInfo.tokenId,
|
|
514
|
+
expiresAt: tokenInfo.expiresAt,
|
|
515
|
+
};
|
|
516
|
+
cur.updatedAt = new Date().toISOString();
|
|
517
|
+
saveRemoteState(cur, statePath);
|
|
518
|
+
return tokenInfo;
|
|
519
|
+
}, statePath);
|
|
520
|
+
if (!committed) {
|
|
521
|
+
throw Object.assign(new Error('Relay Function changed during token renewal; stale token discarded'), { status: 409 });
|
|
522
|
+
}
|
|
523
|
+
if (bridge) {
|
|
524
|
+
bridge.updateSettings({ relayToken: committed.token });
|
|
525
|
+
}
|
|
526
|
+
return { expiresAt: committed.expiresAt, tokenId: committed.tokenId };
|
|
527
|
+
},
|
|
528
|
+
|
|
529
|
+
startProvision(body = {}) {
|
|
530
|
+
if (provisionTask.running) return { started: false };
|
|
531
|
+
provisionTask = {
|
|
532
|
+
running: true,
|
|
533
|
+
step: 'starting',
|
|
534
|
+
logs: [],
|
|
535
|
+
error: null,
|
|
536
|
+
completedAt: null,
|
|
537
|
+
};
|
|
538
|
+
const pushLog = (msg) => {
|
|
539
|
+
provisionTask.logs.push(String(msg));
|
|
540
|
+
if (provisionTask.logs.length > 100) provisionTask.logs.shift();
|
|
541
|
+
};
|
|
542
|
+
void (async () => {
|
|
543
|
+
try {
|
|
544
|
+
if (body.mode === 'bind' && body.webId) {
|
|
545
|
+
provisionTask.step = 'binding';
|
|
546
|
+
await bindTianaRemoteWeb({
|
|
547
|
+
webId: body.webId,
|
|
548
|
+
hostName: body.hostName,
|
|
549
|
+
rebind: Boolean(body.rebind),
|
|
550
|
+
statePath,
|
|
551
|
+
log: pushLog,
|
|
552
|
+
});
|
|
553
|
+
} else {
|
|
554
|
+
provisionTask.step = 'provisioning';
|
|
555
|
+
await provisionTianaRemote({
|
|
556
|
+
webName: body.webName,
|
|
557
|
+
hostName: body.hostName,
|
|
558
|
+
statePath,
|
|
559
|
+
log: pushLog,
|
|
560
|
+
});
|
|
561
|
+
}
|
|
562
|
+
provisionTask.step = 'connecting';
|
|
563
|
+
await syncBridgeWithState();
|
|
564
|
+
provisionTask.step = 'done';
|
|
565
|
+
} catch (err) {
|
|
566
|
+
provisionTask.step = 'error';
|
|
567
|
+
provisionTask.error = err instanceof Error ? err.message : String(err);
|
|
568
|
+
} finally {
|
|
569
|
+
provisionTask.running = false;
|
|
570
|
+
provisionTask.completedAt = new Date().toISOString();
|
|
571
|
+
}
|
|
572
|
+
})();
|
|
573
|
+
return { started: true, step: provisionTask.step };
|
|
574
|
+
},
|
|
575
|
+
|
|
576
|
+
getProvisionStatus() {
|
|
577
|
+
return {
|
|
578
|
+
...provisionTask,
|
|
579
|
+
logs: [...provisionTask.logs],
|
|
580
|
+
};
|
|
581
|
+
},
|
|
582
|
+
};
|
|
583
|
+
|
|
584
|
+
return controller;
|
|
585
|
+
}
|
|
586
|
+
|
|
587
|
+
export function apply(ctx) {
|
|
588
|
+
const controller = createHostController({
|
|
589
|
+
getWebServer: () => ctx.get('webServer'),
|
|
590
|
+
getConnection: () => ctx.get('connection'),
|
|
591
|
+
getClientModules: () => ctx.get('clientModules'),
|
|
592
|
+
getWebApp: () => ctx.get('webApp'),
|
|
593
|
+
});
|
|
594
|
+
|
|
595
|
+
ctx.effect(() => () => controller.dispose(), 'tiana-remote: tunnel lifecycle');
|
|
596
|
+
|
|
597
|
+
const mountRoutes = (routeCtx, webServer, connection) => {
|
|
598
|
+
if (typeof webServer?.register === 'function') {
|
|
599
|
+
routeCtx.effect(
|
|
600
|
+
() => registerWebServerRoutes(webServer, connection, controller),
|
|
601
|
+
'tiana-remote: http routes',
|
|
602
|
+
);
|
|
603
|
+
return;
|
|
604
|
+
}
|
|
605
|
+
if (typeof connection?.fetch?.register === 'function') {
|
|
606
|
+
routeCtx.effect(
|
|
607
|
+
() => registerRemoteRoutes(connection, controller),
|
|
608
|
+
'tiana-remote: http routes (fetch fallback)',
|
|
609
|
+
);
|
|
610
|
+
}
|
|
611
|
+
};
|
|
612
|
+
|
|
613
|
+
if (typeof ctx.inject === 'function') {
|
|
614
|
+
ctx.inject(['webServer', 'connection'], (routeCtx) => {
|
|
615
|
+
mountRoutes(routeCtx, routeCtx.webServer, routeCtx.connection);
|
|
616
|
+
});
|
|
617
|
+
} else {
|
|
618
|
+
mountRoutes(ctx, ctx.get('webServer'), ctx.get('connection'));
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
const loader = ctx.get('loader');
|
|
622
|
+
const ready = loader && typeof loader.await === 'function' ? loader.await() : Promise.resolve();
|
|
623
|
+
ready.then(() => controller.boot()).catch((err) => {
|
|
624
|
+
console.error(`[dsh-remote] Startup failed: ${err instanceof Error ? err.message : String(err)}`);
|
|
625
|
+
});
|
|
626
|
+
}
|
|
627
|
+
|
|
628
|
+
export default { name, inject, apply, createHostController };
|
package/icon.svg
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round">
|
|
2
|
+
<rect x="2" y="3" width="20" height="14" rx="2"/>
|
|
3
|
+
<path d="M8 21h8M12 17v4"/>
|
|
4
|
+
<path d="M7.5 10.5c1.3-1.3 3.2-2 5.2-2s3.9.7 5.2 2"/>
|
|
5
|
+
<path d="M9.8 12.8c.8-.8 1.8-1.2 2.9-1.2s2.1.4 2.9 1.2"/>
|
|
6
|
+
<circle cx="12.7" cy="14.5" r="0.6" fill="currentColor"/>
|
|
7
|
+
</svg>
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "Tiana Remote",
|
|
3
|
+
"description": "End-to-end encrypted remote access for DeepSeek Harness (DSH) via Tiana with adaptive sidebar panel, standard QR code, token renewal, and cloud onboarding.",
|
|
4
|
+
"panel": "Tiana Remote",
|
|
5
|
+
"renewToken": "Renew Now",
|
|
6
|
+
"rebindApp": "Switch / Rebind Cloud App",
|
|
7
|
+
"bindExisting": "Bind Existing Tiana App",
|
|
8
|
+
"createApp": "Create New DSH Remote App"
|
|
9
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "Tiana Remote",
|
|
3
|
+
"description": "通过 Tiana 端到端加密隧道与托管网页远程安全访问本机 DeepSeek Harness(DSH),内置侧栏管理面板、标准扫码连接、凭据续期与云端一键引导。",
|
|
4
|
+
"panel": "Tiana Remote",
|
|
5
|
+
"renewToken": "立即续期",
|
|
6
|
+
"rebindApp": "切换/换绑云端应用",
|
|
7
|
+
"bindExisting": "绑定已有 Tiana 应用",
|
|
8
|
+
"createApp": "在当前空间新建 DSH Remote 应用"
|
|
9
|
+
}
|