Fix RustDesk remote access sync after agent install
- Reload config/token directly from store before syncing - Avoid race condition when register() calls ensureRustdesk before React state updates - Ensures machineId and token are always fresh from disk 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
parent
bc41f6ae34
commit
376e81c9c7
1 changed files with 39 additions and 3 deletions
|
|
@ -850,14 +850,50 @@ const resolvedAppUrl = useMemo(() => {
|
|||
throw error
|
||||
}
|
||||
setRustdeskInfo(normalized)
|
||||
|
||||
// Recarrega o config diretamente do store para garantir que temos o machineId mais recente
|
||||
// (evita race condition quando register() chama ensureRustdesk antes do state React atualizar)
|
||||
const freshStore = await loadStore()
|
||||
const freshConfig = await readConfig(freshStore)
|
||||
const freshToken = await readToken(freshStore)
|
||||
if (!freshConfig?.machineId) {
|
||||
logDesktop("rustdesk:provision:sync-skipped", { reason: "no-machineId-in-store" })
|
||||
return
|
||||
}
|
||||
if (!freshToken) {
|
||||
logDesktop("rustdesk:provision:sync-skipped", { reason: "no-token-in-store" })
|
||||
return
|
||||
}
|
||||
|
||||
// Faz o sync diretamente com os dados frescos do store
|
||||
try {
|
||||
await syncRemoteAccessNow(normalized)
|
||||
logDesktop("rustdesk:provision:synced", { id: normalized.id })
|
||||
const syncPayload = buildRemoteAccessPayload(normalized)
|
||||
if (!syncPayload) {
|
||||
logDesktop("rustdesk:provision:sync-skipped", { reason: "invalid-payload" })
|
||||
return
|
||||
}
|
||||
const response = await fetch(`${apiBaseUrl}/api/machines/remote-access`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
"Idempotency-Key": `${freshConfig.machineId}:RustDesk:${normalized.id}`,
|
||||
},
|
||||
body: JSON.stringify({ machineToken: freshToken, ...syncPayload }),
|
||||
})
|
||||
if (response.ok) {
|
||||
const nextInfo: RustdeskInfo = { ...normalized, lastSyncedAt: Date.now(), lastError: null }
|
||||
await writeRustdeskInfo(freshStore, nextInfo)
|
||||
setRustdeskInfo(nextInfo)
|
||||
logDesktop("rustdesk:provision:synced", { id: normalized.id })
|
||||
} else {
|
||||
const errorText = await response.text().catch(() => "")
|
||||
logDesktop("rustdesk:provision:sync-error", { status: response.status, error: errorText.slice(0, 200) })
|
||||
}
|
||||
} catch (error) {
|
||||
logDesktop("rustdesk:provision:sync-error", { error: String(error) })
|
||||
}
|
||||
},
|
||||
[store, syncRemoteAccessNow]
|
||||
[store]
|
||||
)
|
||||
|
||||
const ensureRustdesk = useCallback(async () => {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue