From 5603157679803a277fc8fff5b29cd6e5e997a210 Mon Sep 17 00:00:00 2001 From: Kevin Yue Date: Mon, 26 Jun 2023 09:23:26 +0800 Subject: [PATCH] refactor: find the vpnc_script from predefined locations --- .vscode/settings.json | 1 + Cargo.lock | 10 ++++++++++ gpcommon/Cargo.toml | 1 + gpcommon/src/vpn/mod.rs | 14 +++++++++++++- gpcommon/src/vpn/vpnc_script.rs | 21 +++++++++++++++++++++ 5 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 gpcommon/src/vpn/vpnc_script.rs diff --git a/.vscode/settings.json b/.vscode/settings.json index 0270beb..9052608 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -18,6 +18,7 @@ "tauri", "unlisten", "userauthcookie", + "vpnc", "vpninfo" ], "files.associations": { diff --git a/Cargo.lock b/Cargo.lock index c013539..25f7ead 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1122,6 +1122,7 @@ dependencies = [ "bytes", "cc", "data-encoding", + "is_executable", "log", "ring", "serde", @@ -1416,6 +1417,15 @@ dependencies = [ "windows-sys 0.48.0", ] +[[package]] +name = "is_executable" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fa9acdc6d67b75e626ad644734e8bc6df893d9cd2a834129065d3dd6158ea9c8" +dependencies = [ + "winapi", +] + [[package]] name = "itoa" version = "0.4.8" diff --git a/gpcommon/Cargo.toml b/gpcommon/Cargo.toml index e2fbe34..665fef9 100644 --- a/gpcommon/Cargo.toml +++ b/gpcommon/Cargo.toml @@ -16,6 +16,7 @@ async-trait = "0.1" ring = "0.16" data-encoding = "2.3" log = "0.4" +is_executable = "1.0" [build-dependencies] cc = "1.0" diff --git a/gpcommon/src/vpn/mod.rs b/gpcommon/src/vpn/mod.rs index f71723d..243797f 100644 --- a/gpcommon/src/vpn/mod.rs +++ b/gpcommon/src/vpn/mod.rs @@ -1,3 +1,4 @@ +use crate::vpn::vpnc_script::find_default_vpnc_script; use log::{debug, info, warn}; use serde::{Deserialize, Serialize}; use std::ffi::{c_void, CString}; @@ -7,6 +8,7 @@ use tokio::sync::watch; use tokio::sync::{mpsc, Mutex}; mod ffi; +mod vpnc_script; #[derive(Debug, PartialEq, Clone, Copy, Serialize, Deserialize)] #[serde(rename_all = "lowercase")] @@ -87,11 +89,21 @@ impl Vpn { server: &str, cookie: &str, ) -> Result<(), Box> { + let script = match find_default_vpnc_script() { + Some(script) => { + debug!("Using default vpnc-script: {}", script); + script + } + None => { + return Err("Failed to find default vpnc-script".into()); + } + }; + // Save the VPN options so we can use them later, e.g. reconnect *self.vpn_options.lock().await = Some(VpnOptions { server: VpnOptions::to_cstr(server), cookie: VpnOptions::to_cstr(cookie), - script: VpnOptions::to_cstr("/usr/share/vpnc-scripts/vpnc-script"), + script: VpnOptions::to_cstr(script), }); let vpn_options = self.vpn_options.clone(); diff --git a/gpcommon/src/vpn/vpnc_script.rs b/gpcommon/src/vpn/vpnc_script.rs new file mode 100644 index 0000000..f812f7f --- /dev/null +++ b/gpcommon/src/vpn/vpnc_script.rs @@ -0,0 +1,21 @@ +use std::path::Path; + +use is_executable::IsExecutable; + +const VPNC_SCRIPT_LOCATIONS: [&str; 4] = [ + "/usr/local/share/vpnc-scripts/vpnc-script", + "/usr/local/sbin/vpnc-script", + "/usr/share/vpnc-scripts/vpnc-script", + "/usr/sbin/vpnc-script /etc/vpnc/vpnc-script", +]; + +pub(crate) fn find_default_vpnc_script() -> Option<&'static str> { + for location in VPNC_SCRIPT_LOCATIONS.iter() { + let path = Path::new(location); + if path.is_executable() { + return Some(location); + } + } + + None +}