desktop app windows
This commit is contained in:
@@ -6,9 +6,9 @@ use crate::VpnInterface;
|
||||
use netrunner_client::session::SessionManager;
|
||||
use netrunner_client::Session;
|
||||
use serde::de::DeserializeOwned;
|
||||
use tauri::{plugin::PluginApi, AppHandle, Runtime};
|
||||
// ВАЖНО: Добавляем Emitter для возможности вызова .emit()
|
||||
use tauri::{plugin::PluginApi, AppHandle, Emitter, Runtime};
|
||||
|
||||
// Инициализация для ПК
|
||||
pub fn init<R: Runtime, C: DeserializeOwned>(
|
||||
app: &AppHandle<R>,
|
||||
_api: PluginApi<R, C>,
|
||||
@@ -21,16 +21,35 @@ pub fn init<R: Runtime, C: DeserializeOwned>(
|
||||
|
||||
pub struct Vpn<R: Runtime> {
|
||||
app: AppHandle<R>,
|
||||
active_session: Mutex<Option<Arc<Session>>>, // Хранилище сессии
|
||||
active_session: Mutex<Option<Arc<Session>>>,
|
||||
}
|
||||
|
||||
impl<R: Runtime> VpnInterface<R> for Vpn<R> {
|
||||
fn start_vpn(&self, _config: TunnelConfig, ip: String, port: i32) -> crate::Result<()> {
|
||||
// 1. Сразу уведомляем фронт о начале подключения
|
||||
// Имя события совпадает с тем, что мы слушаем в useEffect
|
||||
let _ = self.app.emit(
|
||||
"status-change",
|
||||
StatusResponse {
|
||||
status: "connecting".into(),
|
||||
},
|
||||
);
|
||||
|
||||
// Запуск сессии (здесь может быть паника, если нет прав админа!)
|
||||
let session = SessionManager.start_desktop(format!("{}:{}", ip, port));
|
||||
|
||||
// Сохраняем сессию, чтобы она не выпала при выходе из функции
|
||||
// Сохраняем сессию
|
||||
let mut lock = self.active_session.lock().unwrap();
|
||||
*lock = Some(session);
|
||||
|
||||
// 2. Успешное подключение
|
||||
let _ = self.app.emit(
|
||||
"status-change",
|
||||
StatusResponse {
|
||||
status: "connected".into(),
|
||||
},
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -38,19 +57,24 @@ impl<R: Runtime> VpnInterface<R> for Vpn<R> {
|
||||
let mut lock = self.active_session.lock().unwrap();
|
||||
if let Some(session) = lock.take() {
|
||||
session.stop();
|
||||
|
||||
// 3. Уведомляем об отключении
|
||||
let _ = self.app.emit(
|
||||
"status-change",
|
||||
StatusResponse {
|
||||
status: "idle".into(),
|
||||
},
|
||||
);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn check_actual_vpn_status(&self) -> crate::Result<StatusResponse> {
|
||||
let lock = self.active_session.lock().unwrap();
|
||||
let status = if lock.is_some() { "connected" } else { "idle" };
|
||||
|
||||
let status = if lock.is_some() {
|
||||
"connected".to_string()
|
||||
} else {
|
||||
"idle".to_string()
|
||||
};
|
||||
|
||||
Ok(StatusResponse { status })
|
||||
Ok(StatusResponse {
|
||||
status: status.to_string(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user