From a55616b66ad8e58587df79876fc378014ca2e504 Mon Sep 17 00:00:00 2001 From: Kevin Yue Date: Sun, 25 Feb 2024 08:40:32 -0500 Subject: [PATCH] Install gpgui from archive --- Cargo.lock | 22 +++++++++++++ Makefile | 4 +-- apps/gpgui-helper/src-tauri/src/updater.rs | 2 +- apps/gpservice/Cargo.toml | 2 ++ apps/gpservice/src/handlers.rs | 36 ++++++++++++++++++++-- 5 files changed, 60 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c5c2616..6529415 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1527,8 +1527,10 @@ dependencies = [ "openconnect", "serde", "serde_json", + "tar", "tokio", "tokio-util", + "xz2", ] [[package]] @@ -2198,6 +2200,17 @@ dependencies = [ "tracing-subscriber", ] +[[package]] +name = "lzma-sys" +version = "0.1.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5fda04ab3764e6cde78b9974eec4f779acaba7c4e84b36eca3cf77c581b85d27" +dependencies = [ + "cc", + "libc", + "pkg-config", +] + [[package]] name = "mac" version = "0.1.1" @@ -5124,6 +5137,15 @@ version = "0.13.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "66fee0b777b0f5ac1c69bb06d361268faafa61cd4682ae064a171c16c433e9e4" +[[package]] +name = "xz2" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "388c44dc09d76f1536602ead6d325eb532f5c122f17782bd57fb47baeeb767e2" +dependencies = [ + "lzma-sys", +] + [[package]] name = "zeroize" version = "1.7.0" diff --git a/Makefile b/Makefile index 9f69b48..02fdcc9 100644 --- a/Makefile +++ b/Makefile @@ -79,7 +79,7 @@ clean: rm -rf apps/gpgui-helper/node_modules install: - @echo "Installing $(PKG_NAME) $(VERSION)" + @echo "Installing $(PKG_NAME)..." install -Dm755 target/release/gpclient $(DESTDIR)/usr/bin/gpclient install -Dm755 target/release/gpauth $(DESTDIR)/usr/bin/gpauth @@ -94,7 +94,7 @@ install: install -Dm644 packaging/files/usr/share/polkit-1/actions/com.yuezk.gpgui.policy $(DESTDIR)/usr/share/polkit-1/actions/com.yuezk.gpgui.policy uninstall: - @echo "Uninstalling $(PKG_NAME) $(VERSION)" + @echo "Uninstalling $(PKG_NAME)..." rm -f $(DESTDIR)/usr/bin/gpclient rm -f $(DESTDIR)/usr/bin/gpauth diff --git a/apps/gpgui-helper/src-tauri/src/updater.rs b/apps/gpgui-helper/src-tauri/src/updater.rs index 16b98f7..449a048 100644 --- a/apps/gpgui-helper/src-tauri/src/updater.rs +++ b/apps/gpgui-helper/src-tauri/src/updater.rs @@ -86,7 +86,7 @@ impl GuiUpdater { let arch = "aarch64"; let file_url = format!( - "https://github.com/yuezk/GlobalProtect-openconnect/releases/download/v{}/gpgui_${}_${}.bin.tar.xz", + "https://github.com/yuezk/GlobalProtect-openconnect/releases/download/v{}/gpgui_{}_{}.bin.tar.xz", self.version, self.version, arch ); let checksum_url = format!("{}.sha256", file_url); diff --git a/apps/gpservice/Cargo.toml b/apps/gpservice/Cargo.toml index 5a38524..4212f43 100644 --- a/apps/gpservice/Cargo.toml +++ b/apps/gpservice/Cargo.toml @@ -18,3 +18,5 @@ serde_json.workspace = true env_logger.workspace = true log.workspace = true compile-time.workspace = true +xz2 = "0.1" +tar = "0.4" diff --git a/apps/gpservice/src/handlers.rs b/apps/gpservice/src/handlers.rs index 1b2fa72..643eeb0 100644 --- a/apps/gpservice/src/handlers.rs +++ b/apps/gpservice/src/handlers.rs @@ -1,4 +1,12 @@ -use std::{borrow::Cow, fs::Permissions, ops::ControlFlow, os::unix::fs::PermissionsExt, path::PathBuf, sync::Arc}; +use std::{ + borrow::Cow, + fs::{File, Permissions}, + io::BufReader, + ops::ControlFlow, + os::unix::fs::PermissionsExt, + path::PathBuf, + sync::Arc, +}; use anyhow::bail; use axum::{ @@ -17,7 +25,9 @@ use gpapi::{ GP_GUI_BINARY, }; use log::{info, warn}; +use tar::Archive; use tokio::fs; +use xz2::read::XzDecoder; use crate::ws_server::WsServerContext; @@ -60,6 +70,7 @@ pub async fn update_gui(State(ctx): State>, body: Bytes) -> Ok(()) } +// Unpack GPGUI archive, gpgui_2.0.0_{arch}.bin.tar.xz and install it async fn install_gui(src: &str) -> anyhow::Result<()> { let path = PathBuf::from(GP_GUI_BINARY); let Some(dir) = path.parent() else { @@ -68,8 +79,27 @@ async fn install_gui(src: &str) -> anyhow::Result<()> { fs::create_dir_all(dir).await?; - // Copy the file to the final location and make it executable - fs::copy(src, GP_GUI_BINARY).await?; + // Unpack the archive + info!("Unpacking GUI archive"); + let tar = XzDecoder::new(BufReader::new(File::open(src)?)); + let mut ar = Archive::new(tar); + + for entry in ar.entries()? { + let mut entry = entry?; + let path = entry.path()?; + + if let Some(name) = path.file_name() { + let name = name.to_string_lossy(); + + if name == "gpgui" { + let mut file = File::create(GP_GUI_BINARY)?; + std::io::copy(&mut entry, &mut file)?; + break; + } + } + } + + // Make the binary executable fs::set_permissions(GP_GUI_BINARY, Permissions::from_mode(0o755)).await?; Ok(())