/
Muvz82
/
openbox-linux
Обзор
Документация
Войти
/
Muvz82
/
openbox-linux
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
apps/desktop/src/api/client.js
252 строки
7 KB
Mikhail Sviridov
init
18 май 2026, 17:54
18 май 2026, 17:54
482974b
Код
Авторство
О чём код?
const BACKEND_URL = window.openwinbox?.backendUrl || 'http://127.0.0.1:8787'; // Auth token — obtained from backend via Electron IPC or /api/auth-token let _authToken = ''; async function getAuthToken() { if (_authToken) return _authToken; // Try Electron IPC first if (window.openwinbox?.getAuthToken) { try { _authToken = await window.openwinbox.getAuthToken(); if (_authToken) return _authToken; } catch (e) { // IPC not ready yet, fall through } } // Fallback: fetch token from backend (dev mode) try { const res = await fetch(`${BACKEND_URL}/api/auth-token`); if (res.ok) { const data = await res.json(); _authToken = data.token || ''; } } catch (e) { // Backend not ready yet } return _authToken; } // Helper for authenticated fetch async function apiFetch(url, options = {}) { const token = await getAuthToken(); const headers = { ...options.headers, }; if (token) { headers['X-Auth-Token'] = token; } const res = await fetch(url, { ...options, headers }); if (res.status === 401) { // Token might have changed, clear and retry _authToken = ''; const newToken = await getAuthToken(); if (newToken) { headers['X-Auth-Token'] = newToken; const retryRes = await fetch(url, { ...options, headers }); if (!retryRes.ok) { const err = await retryRes.json().catch(() => ({ error: `HTTP ${retryRes.status}` })); throw new Error(err.error || `HTTP ${retryRes.status}`); } return retryRes.json(); } } if (!res.ok) { const err = await res.json().catch(() => ({ error: `HTTP ${res.status}: ${res.statusText}` })); throw new Error(err.error || `HTTP ${res.status}: ${res.statusText}`); } return res.json(); } export async function fetchHostStatus() { return apiFetch(`${BACKEND_URL}/api/host`); } export async function fetchHealth() { return apiFetch(`${BACKEND_URL}/api/health`); } export async function fetchTools() { return apiFetch(`${BACKEND_URL}/api/tools`); } // VM API export async function fetchVMList() { return apiFetch(`${BACKEND_URL}/api/vm/list`); } export async function fetchVMStatus(name) { return apiFetch(`${BACKEND_URL}/api/vm/status?name=${encodeURIComponent(name)}`); } export async function vmStart(name) { return apiFetch(`${BACKEND_URL}/api/vm/start`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ name }), }); } export async function vmShutdown(name) { return apiFetch(`${BACKEND_URL}/api/vm/shutdown`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ name }), }); } export async function vmDestroy(name) { return apiFetch(`${BACKEND_URL}/api/vm/destroy`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ name }), }); } export async function vmDefine(params) { return apiFetch(`${BACKEND_URL}/api/vm/define`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(params), }); } export async function vmCreateDisk(path, size) { return apiFetch(`${BACKEND_URL}/api/vm/create-disk`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ path, size }), }); } export async function fetchVMLogs(name, lines = 50) { return apiFetch(`${BACKEND_URL}/api/vm/logs?name=${encodeURIComponent(name)}&lines=${lines}`); } // Wizard API export async function checkISO(path) { return apiFetch(`${BACKEND_URL}/api/wizard/check-iso?path=${encodeURIComponent(path)}`); } export async function fetchDiskFree(path) { return apiFetch(`${BACKEND_URL}/api/wizard/disk-free?path=${encodeURIComponent(path || '')}`); } export async function browseISO(dir) { return apiFetch(`${BACKEND_URL}/api/wizard/browse-iso?dir=${encodeURIComponent(dir || '')}`); } export async function browseDir(dir) { return apiFetch(`${BACKEND_URL}/api/wizard/browse-dir?dir=${encodeURIComponent(dir || '')}`); } // App Index API export async function fetchAppList() { return apiFetch(`${BACKEND_URL}/api/apps/list`); } export async function fetchAppGet(id) { return apiFetch(`${BACKEND_URL}/api/apps/get?id=${encodeURIComponent(id)}`); } export async function appAdd(app) { return apiFetch(`${BACKEND_URL}/api/apps/add`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(app), }); } export async function appUpdate(id, app) { return apiFetch(`${BACKEND_URL}/api/apps/update`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ id, ...app }), }); } export async function appDelete(id) { return apiFetch(`${BACKEND_URL}/api/apps/delete`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ id }), }); } export async function importInstaller(sourcePath, appName) { return apiFetch(`${BACKEND_URL}/api/apps/import-installer`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ source_path: sourcePath, app_name: appName }), }); } export async function fetchSharedDir() { return apiFetch(`${BACKEND_URL}/api/apps/shared-dir`); } // RDP API export async function fetchRDPConfig() { return apiFetch(`${BACKEND_URL}/api/rdp/config`); } export async function saveRDPConfig(config) { return apiFetch(`${BACKEND_URL}/api/rdp/config`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(config), }); } export async function rdpConnect(vmName) { return apiFetch(`${BACKEND_URL}/api/rdp/connect`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ vm_name: vmName }), }); } export async function rdpTest() { return apiFetch(`${BACKEND_URL}/api/rdp/test`); } // SSH API export async function fetchSSHConfig() { return apiFetch(`${BACKEND_URL}/api/ssh/config`); } export async function saveSSHConfig(config) { return apiFetch(`${BACKEND_URL}/api/ssh/config`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(config), }); } export async function sshTest() { return apiFetch(`${BACKEND_URL}/api/ssh/test`); } export async function sshExec(vmName, command) { return apiFetch(`${BACKEND_URL}/api/ssh/exec`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ vm_name: vmName, command }), }); } export async function sshLaunchApp(vmName, exePath) { return apiFetch(`${BACKEND_URL}/api/ssh/launch`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ vm_name: vmName, exe_path: exePath }), }); } // Logs API export async function fetchLogs(level = '', limit = 100) { const params = new URLSearchParams(); if (level) params.set('level', level); if (limit) params.set('limit', limit); return apiFetch(`${BACKEND_URL}/api/logs?${params.toString()}`); }