mirror of
				https://github.com/yuezk/GlobalProtect-openconnect.git
				synced 2025-05-20 07:26:58 -04:00 
			
		
		
		
	refactor: upgrade tauri 2.0
This commit is contained in:
		@@ -9,29 +9,29 @@
 | 
			
		||||
    "tauri": "tauri"
 | 
			
		||||
  },
 | 
			
		||||
  "dependencies": {
 | 
			
		||||
    "@emotion/react": "^11.13.0",
 | 
			
		||||
    "@emotion/styled": "^11.13.0",
 | 
			
		||||
    "@mui/icons-material": "^5.16.7",
 | 
			
		||||
    "@mui/material": "^5.16.7",
 | 
			
		||||
    "@tauri-apps/api": "^1.6.0",
 | 
			
		||||
    "@emotion/react": "^11.14.0",
 | 
			
		||||
    "@emotion/styled": "^11.14.0",
 | 
			
		||||
    "@mui/icons-material": "^6.2.0",
 | 
			
		||||
    "@mui/material": "^6.2.0",
 | 
			
		||||
    "@tauri-apps/api": "^2.1.1",
 | 
			
		||||
    "react": "^18.3.1",
 | 
			
		||||
    "react-dom": "^18.3.1"
 | 
			
		||||
  },
 | 
			
		||||
  "devDependencies": {
 | 
			
		||||
    "@tauri-apps/cli": "^1.6.0",
 | 
			
		||||
    "@types/node": "^20.14.15",
 | 
			
		||||
    "@types/react": "^18.3.3",
 | 
			
		||||
    "@types/react-dom": "^18.3.0",
 | 
			
		||||
    "@typescript-eslint/eslint-plugin": "^6.21.0",
 | 
			
		||||
    "@typescript-eslint/parser": "^6.21.0",
 | 
			
		||||
    "@vitejs/plugin-react": "^4.3.1",
 | 
			
		||||
    "eslint": "^8.57.0",
 | 
			
		||||
    "@tauri-apps/cli": "^2.1.0",
 | 
			
		||||
    "@types/node": "^22.10.2",
 | 
			
		||||
    "@types/react": "^18.3.12",
 | 
			
		||||
    "@types/react-dom": "^18.3.1",
 | 
			
		||||
    "@typescript-eslint/eslint-plugin": "^8.18.0",
 | 
			
		||||
    "@typescript-eslint/parser": "^8.18.0",
 | 
			
		||||
    "@vitejs/plugin-react": "^4.3.4",
 | 
			
		||||
    "eslint": "^9.16.0",
 | 
			
		||||
    "eslint-config-prettier": "^9.1.0",
 | 
			
		||||
    "eslint-plugin-react": "^7.35.0",
 | 
			
		||||
    "eslint-plugin-react-hooks": "^4.6.2",
 | 
			
		||||
    "prettier": "3.1.0",
 | 
			
		||||
    "typescript": "^5.5.4",
 | 
			
		||||
    "vite": "^4.5.3"
 | 
			
		||||
    "eslint-plugin-react": "^7.37.2",
 | 
			
		||||
    "eslint-plugin-react-hooks": "^5.1.0",
 | 
			
		||||
    "prettier": "3.4.2",
 | 
			
		||||
    "typescript": "^5.7.2",
 | 
			
		||||
    "vite": "^6.0.3"
 | 
			
		||||
  },
 | 
			
		||||
  "packageManager": "pnpm@8.15.7"
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										2093
									
								
								apps/gpgui-helper/pnpm-lock.yaml
									
									
									
										generated
									
									
									
								
							
							
						
						
									
										2093
									
								
								apps/gpgui-helper/pnpm-lock.yaml
									
									
									
										generated
									
									
									
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							@@ -6,11 +6,12 @@ edition.workspace = true
 | 
			
		||||
license.workspace = true
 | 
			
		||||
 | 
			
		||||
[build-dependencies]
 | 
			
		||||
tauri-build = { version = "1.5", features = [] }
 | 
			
		||||
tauri-build = { version = "2", features = [] }
 | 
			
		||||
 | 
			
		||||
[dependencies]
 | 
			
		||||
gpapi = { path = "../../../crates/gpapi", features = ["tauri"] }
 | 
			
		||||
tauri = { workspace = true, features = ["window-start-dragging"] }
 | 
			
		||||
tauri.workspace = true
 | 
			
		||||
 | 
			
		||||
tokio.workspace = true
 | 
			
		||||
anyhow.workspace = true
 | 
			
		||||
log.workspace = true
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										12
									
								
								apps/gpgui-helper/src-tauri/capabilities/default.json
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								apps/gpgui-helper/src-tauri/capabilities/default.json
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,12 @@
 | 
			
		||||
{
 | 
			
		||||
  "$schema": "../gen/schemas/desktop-schema.json",
 | 
			
		||||
  "identifier": "default",
 | 
			
		||||
  "description": "Capability for the main window",
 | 
			
		||||
  "windows": ["main"],
 | 
			
		||||
  "permissions": [
 | 
			
		||||
    "core:window:allow-start-dragging",
 | 
			
		||||
    "core:event:allow-listen",
 | 
			
		||||
    "core:event:allow-emit",
 | 
			
		||||
    "core:event:allow-unlisten"
 | 
			
		||||
  ]
 | 
			
		||||
}
 | 
			
		||||
										
											
												File diff suppressed because one or more lines are too long
											
										
									
								
							@@ -0,0 +1 @@
 | 
			
		||||
{"default":{"identifier":"default","description":"Capability for the main window","local":true,"windows":["main"],"permissions":["core:window:allow-start-dragging","core:event:allow-listen","core:event:allow-emit","core:event:allow-unlisten"]}}
 | 
			
		||||
							
								
								
									
										1756
									
								
								apps/gpgui-helper/src-tauri/gen/schemas/desktop-schema.json
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1756
									
								
								apps/gpgui-helper/src-tauri/gen/schemas/desktop-schema.json
									
									
									
									
									
										Normal file
									
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							
							
								
								
									
										1756
									
								
								apps/gpgui-helper/src-tauri/gen/schemas/linux-schema.json
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1756
									
								
								apps/gpgui-helper/src-tauri/gen/schemas/linux-schema.json
									
									
									
									
									
										Normal file
									
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							@@ -1,8 +1,7 @@
 | 
			
		||||
use std::sync::Arc;
 | 
			
		||||
 | 
			
		||||
use gpapi::utils::window::WindowExt;
 | 
			
		||||
use log::info;
 | 
			
		||||
use tauri::Manager;
 | 
			
		||||
use tauri::{Listener, Manager};
 | 
			
		||||
 | 
			
		||||
use crate::updater::{GuiUpdater, Installer, ProgressNotifier};
 | 
			
		||||
 | 
			
		||||
@@ -25,15 +24,15 @@ impl App {
 | 
			
		||||
 | 
			
		||||
    tauri::Builder::default()
 | 
			
		||||
      .setup(move |app| {
 | 
			
		||||
        let win = app.get_window("main").expect("no main window");
 | 
			
		||||
        win.hide_menu();
 | 
			
		||||
        let win = app.get_webview_window("main").expect("no main window");
 | 
			
		||||
        let _ = win.hide_menu();
 | 
			
		||||
 | 
			
		||||
        let notifier = ProgressNotifier::new(win.clone());
 | 
			
		||||
        let installer = Installer::new(api_key);
 | 
			
		||||
        let updater = Arc::new(GuiUpdater::new(gui_version, notifier, installer));
 | 
			
		||||
 | 
			
		||||
        let win_clone = win.clone();
 | 
			
		||||
        app.listen_global("app://update-done", move |_event| {
 | 
			
		||||
        app.listen_any("app://update-done", move |_event| {
 | 
			
		||||
          info!("Update done");
 | 
			
		||||
          let _ = win_clone.close();
 | 
			
		||||
        });
 | 
			
		||||
@@ -41,12 +40,15 @@ impl App {
 | 
			
		||||
        // Listen for the update event
 | 
			
		||||
        win.listen("app://update", move |_event| {
 | 
			
		||||
          let updater = Arc::clone(&updater);
 | 
			
		||||
          if updater.is_in_progress() {
 | 
			
		||||
            info!("Update already in progress");
 | 
			
		||||
            updater.notify_progress();
 | 
			
		||||
            return;
 | 
			
		||||
          }
 | 
			
		||||
 | 
			
		||||
          tokio::spawn(async move { updater.update().await });
 | 
			
		||||
        });
 | 
			
		||||
 | 
			
		||||
        // Update the GUI on startup
 | 
			
		||||
        win.trigger("app://update", None);
 | 
			
		||||
 | 
			
		||||
        Ok(())
 | 
			
		||||
      })
 | 
			
		||||
      .run(tauri::generate_context!())?;
 | 
			
		||||
 
 | 
			
		||||
@@ -1,5 +1,5 @@
 | 
			
		||||
use clap::Parser;
 | 
			
		||||
use gpapi::utils::base64;
 | 
			
		||||
use gpapi::utils::{base64, env_utils};
 | 
			
		||||
use log::{info, LevelFilter};
 | 
			
		||||
 | 
			
		||||
use crate::app::App;
 | 
			
		||||
@@ -22,6 +22,8 @@ impl Cli {
 | 
			
		||||
    let api_key = self.read_api_key()?;
 | 
			
		||||
    let app = App::new(api_key, &self.gui_version);
 | 
			
		||||
 | 
			
		||||
    env_utils::patch_gui_runtime_env(false);
 | 
			
		||||
 | 
			
		||||
    app.run()
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -1,39 +1,39 @@
 | 
			
		||||
use std::sync::Arc;
 | 
			
		||||
use std::sync::{Arc, RwLock};
 | 
			
		||||
 | 
			
		||||
use gpapi::{
 | 
			
		||||
  service::request::UpdateGuiRequest,
 | 
			
		||||
  utils::{checksum::verify_checksum, crypto::Crypto, endpoint::http_endpoint},
 | 
			
		||||
};
 | 
			
		||||
use log::{info, warn};
 | 
			
		||||
use tauri::{Manager, Window};
 | 
			
		||||
use tauri::{Emitter, WebviewWindow};
 | 
			
		||||
 | 
			
		||||
use crate::downloader::{ChecksumFetcher, FileDownloader};
 | 
			
		||||
 | 
			
		||||
#[cfg(not(debug_assertions))]
 | 
			
		||||
const SNAPSHOT: &str = match option_env!("SNAPSHOT") {
 | 
			
		||||
    Some(val) => val,
 | 
			
		||||
    None => "false"
 | 
			
		||||
  Some(val) => val,
 | 
			
		||||
  None => "false",
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
pub struct ProgressNotifier {
 | 
			
		||||
  win: Window,
 | 
			
		||||
  win: WebviewWindow,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl ProgressNotifier {
 | 
			
		||||
  pub fn new(win: Window) -> Self {
 | 
			
		||||
  pub fn new(win: WebviewWindow) -> Self {
 | 
			
		||||
    Self { win }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  fn notify(&self, progress: Option<f64>) {
 | 
			
		||||
    let _ = self.win.emit_all("app://update-progress", progress);
 | 
			
		||||
    let _ = self.win.emit("app://update-progress", progress);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  fn notify_error(&self) {
 | 
			
		||||
    let _ = self.win.emit_all("app://update-error", ());
 | 
			
		||||
    let _ = self.win.emit("app://update-error", ());
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  fn notify_done(&self) {
 | 
			
		||||
    let _ = self.win.emit_and_trigger("app://update-done", ());
 | 
			
		||||
    let _ = self.win.emit("app://update-done", ());
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -72,6 +72,8 @@ pub struct GuiUpdater {
 | 
			
		||||
  version: String,
 | 
			
		||||
  notifier: Arc<ProgressNotifier>,
 | 
			
		||||
  installer: Installer,
 | 
			
		||||
  in_progress: RwLock<bool>,
 | 
			
		||||
  progress: Arc<RwLock<Option<f64>>>,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl GuiUpdater {
 | 
			
		||||
@@ -80,6 +82,8 @@ impl GuiUpdater {
 | 
			
		||||
      version,
 | 
			
		||||
      notifier: Arc::new(notifier),
 | 
			
		||||
      installer,
 | 
			
		||||
      in_progress: Default::default(),
 | 
			
		||||
      progress: Default::default(),
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
@@ -112,15 +116,23 @@ impl GuiUpdater {
 | 
			
		||||
    let cf = ChecksumFetcher::new(&checksum_url);
 | 
			
		||||
    let notifier = Arc::clone(&self.notifier);
 | 
			
		||||
 | 
			
		||||
    dl.on_progress(move |progress| notifier.notify(progress));
 | 
			
		||||
    let progress_ref = Arc::clone(&self.progress);
 | 
			
		||||
    dl.on_progress(move |progress| {
 | 
			
		||||
      // Save progress to shared state so that it can be notified to the UI when needed
 | 
			
		||||
      if let Ok(mut guard) = progress_ref.try_write() {
 | 
			
		||||
        *guard = progress;
 | 
			
		||||
      }
 | 
			
		||||
      notifier.notify(progress);
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    self.set_in_progress(true);
 | 
			
		||||
    let res = tokio::try_join!(dl.download(), cf.fetch());
 | 
			
		||||
 | 
			
		||||
    let (file, checksum) = match res {
 | 
			
		||||
      Ok((file, checksum)) => (file, checksum),
 | 
			
		||||
      Err(err) => {
 | 
			
		||||
        warn!("Download error: {}", err);
 | 
			
		||||
        self.notifier.notify_error();
 | 
			
		||||
        self.notify_error();
 | 
			
		||||
        return;
 | 
			
		||||
      }
 | 
			
		||||
    };
 | 
			
		||||
@@ -130,7 +142,7 @@ impl GuiUpdater {
 | 
			
		||||
 | 
			
		||||
    if let Err(err) = verify_checksum(&file_path, &checksum) {
 | 
			
		||||
      warn!("Checksum error: {}", err);
 | 
			
		||||
      self.notifier.notify_error();
 | 
			
		||||
      self.notify_error();
 | 
			
		||||
      return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -138,10 +150,48 @@ impl GuiUpdater {
 | 
			
		||||
 | 
			
		||||
    if let Err(err) = self.installer.install(&file_path, &checksum).await {
 | 
			
		||||
      warn!("Install error: {}", err);
 | 
			
		||||
      self.notifier.notify_error();
 | 
			
		||||
      self.notify_error();
 | 
			
		||||
    } else {
 | 
			
		||||
      info!("Install success");
 | 
			
		||||
      self.notifier.notify_done();
 | 
			
		||||
      self.notify_done();
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  pub fn is_in_progress(&self) -> bool {
 | 
			
		||||
    if let Ok(guard) = self.in_progress.try_read() {
 | 
			
		||||
      *guard
 | 
			
		||||
    } else {
 | 
			
		||||
      info!("Failed to acquire in_progress lock");
 | 
			
		||||
      false
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  fn set_in_progress(&self, in_progress: bool) {
 | 
			
		||||
    if let Ok(mut guard) = self.in_progress.try_write() {
 | 
			
		||||
      *guard = in_progress;
 | 
			
		||||
    } else {
 | 
			
		||||
      info!("Failed to acquire in_progress lock");
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  fn notify_error(&self) {
 | 
			
		||||
    self.set_in_progress(false);
 | 
			
		||||
    self.notifier.notify_error();
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  fn notify_done(&self) {
 | 
			
		||||
    self.set_in_progress(false);
 | 
			
		||||
    self.notifier.notify_done();
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  pub fn notify_progress(&self) {
 | 
			
		||||
    let progress = if let Ok(guard) = self.progress.try_read() {
 | 
			
		||||
      *guard
 | 
			
		||||
    } else {
 | 
			
		||||
      info!("Failed to acquire progress lock");
 | 
			
		||||
      None
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
    self.notifier.notify(progress);
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -1,35 +1,15 @@
 | 
			
		||||
{
 | 
			
		||||
  "$schema": "../node_modules/@tauri-apps/cli/schema.json",
 | 
			
		||||
  "$schema": "../node_modules/@tauri-apps/cli/config.schema.json",
 | 
			
		||||
  "build": {
 | 
			
		||||
    "beforeDevCommand": "pnpm dev",
 | 
			
		||||
    "beforeBuildCommand": "pnpm build",
 | 
			
		||||
    "devPath": "http://localhost:1421",
 | 
			
		||||
    "distDir": "../dist",
 | 
			
		||||
    "withGlobalTauri": false
 | 
			
		||||
    "devUrl": "http://localhost:1421",
 | 
			
		||||
    "frontendDist": "../dist"
 | 
			
		||||
  },
 | 
			
		||||
  "package": {
 | 
			
		||||
    "productName": "gpgui-helper"
 | 
			
		||||
  },
 | 
			
		||||
  "tauri": {
 | 
			
		||||
    "allowlist": {
 | 
			
		||||
      "all": false,
 | 
			
		||||
      "window": {
 | 
			
		||||
        "all": false,
 | 
			
		||||
        "startDragging": true
 | 
			
		||||
      }
 | 
			
		||||
    },
 | 
			
		||||
    "bundle": {
 | 
			
		||||
      "active": false,
 | 
			
		||||
      "targets": "deb",
 | 
			
		||||
      "identifier": "com.yuezk.gpgui-helper",
 | 
			
		||||
      "icon": [
 | 
			
		||||
        "icons/32x32.png",
 | 
			
		||||
        "icons/128x128.png",
 | 
			
		||||
        "icons/128x128@2x.png",
 | 
			
		||||
        "icons/icon.icns",
 | 
			
		||||
        "icons/icon.ico"
 | 
			
		||||
      ]
 | 
			
		||||
    },
 | 
			
		||||
  "identifier": "com.yuezk.gpgui-helper",
 | 
			
		||||
  "productName": "gpgui-helper",
 | 
			
		||||
  "app": {
 | 
			
		||||
    "withGlobalTauri": false,
 | 
			
		||||
    "security": {
 | 
			
		||||
      "csp": null
 | 
			
		||||
    },
 | 
			
		||||
@@ -48,5 +28,16 @@
 | 
			
		||||
        "decorations": false
 | 
			
		||||
      }
 | 
			
		||||
    ]
 | 
			
		||||
  },
 | 
			
		||||
  "bundle": {
 | 
			
		||||
    "active": false,
 | 
			
		||||
    "targets": "deb",
 | 
			
		||||
    "icon": [
 | 
			
		||||
      "icons/32x32.png",
 | 
			
		||||
      "icons/128x128.png",
 | 
			
		||||
      "icons/128x128@2x.png",
 | 
			
		||||
      "icons/icon.icns",
 | 
			
		||||
      "icons/icon.ico"
 | 
			
		||||
    ]
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -1,10 +1,12 @@
 | 
			
		||||
import { Box, Button, CssBaseline, LinearProgress, Typography } from "@mui/material";
 | 
			
		||||
import { appWindow } from "@tauri-apps/api/window";
 | 
			
		||||
import { getCurrentWindow } from "@tauri-apps/api/window";
 | 
			
		||||
import logo from "../../assets/icon.svg";
 | 
			
		||||
import { useEffect, useState } from "react";
 | 
			
		||||
 | 
			
		||||
import "./styles.css";
 | 
			
		||||
 | 
			
		||||
const appWindow = getCurrentWindow();
 | 
			
		||||
 | 
			
		||||
function useUpdateProgress() {
 | 
			
		||||
  const [progress, setProgress] = useState<number | null>(null);
 | 
			
		||||
 | 
			
		||||
@@ -25,6 +27,8 @@ export default function App() {
 | 
			
		||||
  const [error, setError] = useState(false);
 | 
			
		||||
 | 
			
		||||
  useEffect(() => {
 | 
			
		||||
    appWindow.emit("app://update");
 | 
			
		||||
 | 
			
		||||
    const unlisten = appWindow.listen("app://update-error", () => {
 | 
			
		||||
      setError(true);
 | 
			
		||||
    });
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user