diff --git a/apps/desktop/src/main.tsx b/apps/desktop/src/main.tsx index fda3b94..6a2f199 100644 --- a/apps/desktop/src/main.tsx +++ b/apps/desktop/src/main.tsx @@ -1042,8 +1042,47 @@ const resolvedAppUrl = useMemo(() => { const openSystem = useCallback(async () => { if (!token) return setIsLaunchingSystem(true) + + // Recarrega store do disco para pegar dados que o Rust salvou diretamente + // e sincroniza RustDesk antes de redirecionar (fire-and-forget com timeout) try { - // Tenta criar a sessão via API (evita dependência de redirecionamento + cookies em 3xx) + if (store && config?.machineId) { + const freshStore = await loadStore() + const freshRustdesk = await readRustdeskInfo(freshStore) + if (freshRustdesk && (!freshRustdesk.lastSyncedAt || Date.now() - freshRustdesk.lastSyncedAt > 60000)) { + logDesktop("openSystem:rustdesk:sync:start", { id: freshRustdesk.id }) + const payload = buildRemoteAccessPayload(freshRustdesk) + if (payload) { + const syncPromise = fetch(`${apiBaseUrl}/api/machines/remote-access`, { + method: "POST", + headers: { + "Content-Type": "application/json", + "Idempotency-Key": `${config.machineId}:RustDesk:${freshRustdesk.id}`, + }, + body: JSON.stringify({ machineToken: token, ...payload }), + }).then(async (syncRes) => { + if (syncRes.ok) { + logDesktop("openSystem:rustdesk:sync:success", { id: freshRustdesk.id }) + const nextInfo: RustdeskInfo = { ...freshRustdesk, lastSyncedAt: Date.now(), lastError: null } + await writeRustdeskInfo(freshStore, nextInfo) + setRustdeskInfo(nextInfo) + } else { + logDesktop("openSystem:rustdesk:sync:error", { status: syncRes.status }) + } + }).catch((err) => { + logDesktop("openSystem:rustdesk:sync:failed", { error: String(err) }) + }) + // Espera no maximo 3s pelo sync, depois continua + await Promise.race([syncPromise, new Promise((r) => setTimeout(r, 3000))]) + } + } + } + } catch (syncErr) { + logDesktop("openSystem:rustdesk:sync:exception", { error: String(syncErr) }) + } + + try { + // Tenta criar a sessao via API (evita dependencia de redirecionamento + cookies em 3xx) const res = await fetch(`${apiBaseUrl}/api/machines/sessions`, { method: "POST", credentials: "include",