Provisiona RustDesk automaticamente
This commit is contained in:
parent
967d4bf1c6
commit
ef1db284fa
6 changed files with 565 additions and 9 deletions
|
|
@ -76,6 +76,20 @@ type AgentConfig = {
|
|||
heartbeatIntervalSec?: number | null
|
||||
}
|
||||
|
||||
type RustdeskProvisioningResult = {
|
||||
id: string
|
||||
password: string
|
||||
installedVersion?: string | null
|
||||
updated: boolean
|
||||
}
|
||||
|
||||
type RustdeskInfo = RustdeskProvisioningResult & {
|
||||
lastProvisionedAt: number
|
||||
lastSyncedAt?: number | null
|
||||
}
|
||||
|
||||
const RUSTDESK_STORE_KEY = "rustdesk"
|
||||
|
||||
declare global {
|
||||
interface ImportMetaEnv {
|
||||
readonly VITE_APP_URL?: string
|
||||
|
|
@ -128,6 +142,15 @@ async function writeConfig(store: Store, cfg: AgentConfig): Promise<void> {
|
|||
await store.save()
|
||||
}
|
||||
|
||||
async function readRustdeskInfo(store: Store): Promise<RustdeskInfo | null> {
|
||||
return (await store.get<RustdeskInfo>(RUSTDESK_STORE_KEY)) ?? null
|
||||
}
|
||||
|
||||
async function writeRustdeskInfo(store: Store, info: RustdeskInfo): Promise<void> {
|
||||
await store.set(RUSTDESK_STORE_KEY, info)
|
||||
await store.save()
|
||||
}
|
||||
|
||||
function bytes(n?: number) {
|
||||
if (!n || !Number.isFinite(n)) return "—"
|
||||
const u = ["B","KB","MB","GB","TB"]
|
||||
|
|
@ -191,6 +214,9 @@ function App() {
|
|||
const [tokenValidationTick, setTokenValidationTick] = useState(0)
|
||||
const [, setIsValidatingToken] = useState(false)
|
||||
const tokenVerifiedRef = useRef(false)
|
||||
const [rustdeskInfo, setRustdeskInfo] = useState<RustdeskInfo | null>(null)
|
||||
const [isRustdeskProvisioning, setIsRustdeskProvisioning] = useState(false)
|
||||
const rustdeskBootstrapRef = useRef(false)
|
||||
|
||||
const [provisioningCode, setProvisioningCode] = useState("")
|
||||
const [validatedCompany, setValidatedCompany] = useState<{ id: string; name: string; slug: string; tenantId: string } | null>(null)
|
||||
|
|
@ -219,6 +245,8 @@ function App() {
|
|||
setToken(t)
|
||||
const cfg = await readConfig(s)
|
||||
setConfig(cfg)
|
||||
const existingRustdesk = await readRustdeskInfo(s)
|
||||
setRustdeskInfo(existingRustdesk)
|
||||
if (cfg?.collaboratorEmail) setCollabEmail(cfg.collaboratorEmail)
|
||||
if (cfg?.collaboratorName) setCollabName(cfg.collaboratorName)
|
||||
if (cfg?.companyName) setCompanyName(cfg.companyName)
|
||||
|
|
@ -426,14 +454,94 @@ function App() {
|
|||
}
|
||||
}, [provisioningCode])
|
||||
|
||||
const resolvedAppUrl = useMemo(() => {
|
||||
if (!config?.appUrl) return appUrl
|
||||
const normalized = normalizeUrl(config.appUrl, appUrl)
|
||||
if (import.meta.env.MODE === "production" && normalized.includes("localhost")) {
|
||||
return appUrl
|
||||
const resolvedAppUrl = useMemo(() => {
|
||||
if (!config?.appUrl) return appUrl
|
||||
const normalized = normalizeUrl(config.appUrl, appUrl)
|
||||
if (import.meta.env.MODE === "production" && normalized.includes("localhost")) {
|
||||
return appUrl
|
||||
}
|
||||
return normalized
|
||||
}, [config?.appUrl])
|
||||
|
||||
const syncRustdeskAccess = useCallback(
|
||||
async (machineToken: string, info: RustdeskInfo) => {
|
||||
if (!store || !machineToken) return
|
||||
try {
|
||||
const response = await fetch(`${apiBaseUrl}/api/machines/remote-access`, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({
|
||||
machineToken,
|
||||
provider: "RustDesk",
|
||||
identifier: info.id,
|
||||
url: `rustdesk://${info.id}`,
|
||||
password: info.password,
|
||||
notes: info.installedVersion ? `RustDesk ${info.installedVersion}` : undefined,
|
||||
}),
|
||||
})
|
||||
if (!response.ok) {
|
||||
const text = await response.text()
|
||||
throw new Error(text.slice(0, 300) || "Falha ao registrar acesso remoto")
|
||||
}
|
||||
const nextInfo: RustdeskInfo = { ...info, lastSyncedAt: Date.now() }
|
||||
await writeRustdeskInfo(store, nextInfo)
|
||||
setRustdeskInfo(nextInfo)
|
||||
} catch (error) {
|
||||
console.error("Falha ao sincronizar acesso remoto com a plataforma", error)
|
||||
}
|
||||
return normalized
|
||||
}, [config?.appUrl])
|
||||
},
|
||||
[store]
|
||||
)
|
||||
|
||||
const provisionRustdesk = useCallback(
|
||||
async (machineId: string, machineToken: string): Promise<RustdeskInfo | null> => {
|
||||
if (!store || !machineId) return null
|
||||
setIsRustdeskProvisioning(true)
|
||||
try {
|
||||
const result = await invoke<RustdeskProvisioningResult>("provision_rustdesk", { machineId })
|
||||
const info: RustdeskInfo = {
|
||||
...result,
|
||||
lastProvisionedAt: Date.now(),
|
||||
lastSyncedAt: null,
|
||||
}
|
||||
await writeRustdeskInfo(store, info)
|
||||
setRustdeskInfo(info)
|
||||
if (machineToken) {
|
||||
await syncRustdeskAccess(machineToken, info)
|
||||
}
|
||||
return info
|
||||
} catch (error) {
|
||||
const message = error instanceof Error ? error.message : String(error)
|
||||
if (message.toLowerCase().includes("apenas no windows")) {
|
||||
console.info("Provisionamento do RustDesk ignorado (plataforma não suportada)")
|
||||
} else {
|
||||
console.error("Falha ao provisionar RustDesk", error)
|
||||
}
|
||||
return null
|
||||
} finally {
|
||||
setIsRustdeskProvisioning(false)
|
||||
}
|
||||
},
|
||||
[store, syncRustdeskAccess]
|
||||
)
|
||||
|
||||
useEffect(() => {
|
||||
if (!store || !config?.machineId || !token) return
|
||||
if (!rustdeskInfo && !isRustdeskProvisioning && !rustdeskBootstrapRef.current) {
|
||||
rustdeskBootstrapRef.current = true
|
||||
provisionRustdesk(config.machineId, token).finally(() => {
|
||||
rustdeskBootstrapRef.current = false
|
||||
})
|
||||
return
|
||||
}
|
||||
if (rustdeskInfo && !isRustdeskProvisioning) {
|
||||
const lastSync = rustdeskInfo.lastSyncedAt ?? 0
|
||||
const needsSync = Date.now() - lastSync > 7 * 24 * 60 * 60 * 1000
|
||||
if (needsSync) {
|
||||
syncRustdeskAccess(token, rustdeskInfo)
|
||||
}
|
||||
}
|
||||
}, [store, config?.machineId, token, rustdeskInfo, provisionRustdesk, syncRustdeskAccess, isRustdeskProvisioning])
|
||||
|
||||
async function register() {
|
||||
if (!profile) return
|
||||
|
|
@ -524,6 +632,8 @@ function App() {
|
|||
setToken(data.machineToken)
|
||||
setCompanyName(validatedCompany.name)
|
||||
|
||||
await provisionRustdesk(data.machineId, data.machineToken)
|
||||
|
||||
await invoke("start_machine_agent", {
|
||||
baseUrl: apiBaseUrl,
|
||||
token: data.machineToken,
|
||||
|
|
@ -885,6 +995,9 @@ function App() {
|
|||
) : null}
|
||||
<div className="mt-2 flex gap-2">
|
||||
<button disabled={busy || !validatedCompany || !isEmailValid || !collabName.trim() || provisioningCode.trim().length < 32} onClick={register} className="rounded-lg border border-black bg-black px-3 py-2 text-sm font-semibold text-white hover:bg-black/90 disabled:opacity-60">Registrar dispositivo</button>
|
||||
{isRustdeskProvisioning ? (
|
||||
<p className="text-xs text-neutral-500">Preparando cliente de acesso remoto (RustDesk)...</p>
|
||||
) : null}
|
||||
</div>
|
||||
</div>
|
||||
) : (
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue