Compare commits

..

2 Commits

Author SHA1 Message Date
Kevin Yue
cff2ff9dbe Update dependencies 2024-03-16 21:24:41 +08:00
Kevin Yue
d5d92cfbee Ensure vpnc_script and csd_wrapper executable 2024-03-16 21:06:49 +08:00
12 changed files with 175 additions and 93 deletions

17
Cargo.lock generated
View File

@ -562,6 +562,13 @@ dependencies = [
"memchr",
]
[[package]]
name = "common"
version = "2.1.0"
dependencies = [
"is_executable",
]
[[package]]
name = "compile-time"
version = "0.2.0"
@ -2300,9 +2307,9 @@ dependencies = [
[[package]]
name = "mio"
version = "0.8.10"
version = "0.8.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8f3d0b296e374a4e6f3c7b0a1f5a51d748a0d34c85e7dc48fc3fa9a87657fe09"
checksum = "a4a650543ca06a924e8b371db273b2756685faae30f8487da1b56505a8f78b0c"
dependencies = [
"libc",
"log",
@ -2522,7 +2529,7 @@ name = "openconnect"
version = "2.1.0"
dependencies = [
"cc",
"is_executable",
"common",
"log",
]
@ -4147,9 +4154,9 @@ checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20"
[[package]]
name = "tokio"
version = "1.35.1"
version = "1.36.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c89b4efa943be685f629b149f53829423f8f5531ea21249408e8e2f8671ec104"
checksum = "61285f6515fa018fb2d1e46eb21223fff441ee8db5d0f1435e8ab4f5cdb80931"
dependencies = [
"backtrace",
"bytes",

View File

@ -158,12 +158,12 @@ impl<'a> ConnectHandler<'a> {
let mtu = self.args.mtu.unwrap_or(0);
let vpn = Vpn::builder(gateway, cookie)
.user_agent(self.args.user_agent.clone())
.script(self.args.script.clone())
.user_agent(self.args.user_agent.clone())
.csd_uid(csd_uid)
.csd_wrapper(self.args.csd_wrapper.clone())
.mtu(mtu)
.build();
.build()?;
let vpn = Arc::new(vpn);
let vpn_clone = vpn.clone();

View File

@ -18,7 +18,7 @@
"react-dom": "^18.2.0"
},
"devDependencies": {
"@tauri-apps/cli": "^1.5.0",
"@tauri-apps/cli": "^1.5.6",
"@types/node": "^20.8.10",
"@types/react": "^18.2.15",
"@types/react-dom": "^18.2.7",
@ -31,6 +31,6 @@
"eslint-plugin-react-hooks": "^4.6.0",
"prettier": "3.1.0",
"typescript": "^5.0.2",
"vite": "^4.4.4"
"vite": "^4.5.2"
}
}

View File

@ -29,8 +29,8 @@ dependencies:
devDependencies:
'@tauri-apps/cli':
specifier: ^1.5.0
version: 1.5.0
specifier: ^1.5.6
version: 1.5.6
'@types/node':
specifier: ^20.8.10
version: 20.8.10
@ -48,7 +48,7 @@ devDependencies:
version: 6.12.0(eslint@8.54.0)(typescript@5.0.2)
'@vitejs/plugin-react':
specifier: ^4.0.3
version: 4.0.3(vite@4.4.4)
version: 4.0.3(vite@4.5.2)
eslint:
specifier: ^8.54.0
version: 8.54.0
@ -68,8 +68,8 @@ devDependencies:
specifier: ^5.0.2
version: 5.0.2
vite:
specifier: ^4.4.4
version: 4.4.4(@types/node@20.8.10)
specifier: ^4.5.2
version: 4.5.2(@types/node@20.8.10)
packages:
@ -940,8 +940,8 @@ packages:
engines: {node: '>= 14.6.0', npm: '>= 6.6.0', yarn: '>= 1.19.1'}
dev: false
/@tauri-apps/cli-darwin-arm64@1.5.0:
resolution: {integrity: sha512-wvEfcSBjlh1G8uBiylMNFgBtAyk4mXfvDFcGyigf/2ui7Wve6fcAFDJdTVwiHOZ4Wxnw6BD3lIkVMQdDpE+nFg==}
/@tauri-apps/cli-darwin-arm64@1.5.6:
resolution: {integrity: sha512-NNvG3XLtciCMsBahbDNUEvq184VZmOveTGOuy0So2R33b/6FDkuWaSgWZsR1mISpOuP034htQYW0VITCLelfqg==}
engines: {node: '>= 10'}
cpu: [arm64]
os: [darwin]
@ -949,8 +949,8 @@ packages:
dev: true
optional: true
/@tauri-apps/cli-darwin-x64@1.5.0:
resolution: {integrity: sha512-pWzLMAMslx3dkLyq1f4PpuEhgUs8mPISM5nQoHfgYYchJk6Ip1YtWupo56h5QjqyRNPUsSYT8DVGIw0Oi8auXQ==}
/@tauri-apps/cli-darwin-x64@1.5.6:
resolution: {integrity: sha512-nkiqmtUQw3N1j4WoVjv81q6zWuZFhBLya/RNGUL94oafORloOZoSY0uTZJAoeieb3Y1YK0rCHSDl02MyV2Fi4A==}
engines: {node: '>= 10'}
cpu: [x64]
os: [darwin]
@ -958,8 +958,8 @@ packages:
dev: true
optional: true
/@tauri-apps/cli-linux-arm-gnueabihf@1.5.0:
resolution: {integrity: sha512-hP3nAd0TjxblIAgriXhdX33sKXwbkY3CKsWBVB3O+5DOGy7XzKosIt0KaqiV8BHI2pbHMVOwTZuvjdK8pPLULQ==}
/@tauri-apps/cli-linux-arm-gnueabihf@1.5.6:
resolution: {integrity: sha512-z6SPx+axZexmWXTIVPNs4Tg7FtvdJl9EKxYN6JPjOmDZcqA13iyqWBQal2DA/GMZ1Xqo3vyJf6EoEaKaliymPQ==}
engines: {node: '>= 10'}
cpu: [arm]
os: [linux]
@ -967,8 +967,8 @@ packages:
dev: true
optional: true
/@tauri-apps/cli-linux-arm64-gnu@1.5.0:
resolution: {integrity: sha512-/AwGSjUplzeiGWbKP8rAW4gQx5umWiaQJ0ifx6NakA/sIrhRXPYTobwzg4ixw31ALQNXaEosJCkmvXyHUvoUYw==}
/@tauri-apps/cli-linux-arm64-gnu@1.5.6:
resolution: {integrity: sha512-QuQjMQmpsCbzBrmtQiG4uhnfAbdFx3nzm+9LtqjuZlurc12+Mj5MTgqQ3AOwQedH3f7C+KlvbqD2AdXpwTg7VA==}
engines: {node: '>= 10'}
cpu: [arm64]
os: [linux]
@ -976,8 +976,8 @@ packages:
dev: true
optional: true
/@tauri-apps/cli-linux-arm64-musl@1.5.0:
resolution: {integrity: sha512-OFzKMkg3bmBjr/BYQ1kx4QOHL+JSkzX+Cw8RcG7CKnq8QoJyg8N0K0UTskgsVwlCN4l7bxeuSLvEveg4SBA2AQ==}
/@tauri-apps/cli-linux-arm64-musl@1.5.6:
resolution: {integrity: sha512-8j5dH3odweFeom7bRGlfzDApWVOT4jIq8/214Wl+JeiNVehouIBo9lZGeghZBH3XKFRwEvU23i7sRVjuh2s8mg==}
engines: {node: '>= 10'}
cpu: [arm64]
os: [linux]
@ -985,8 +985,8 @@ packages:
dev: true
optional: true
/@tauri-apps/cli-linux-x64-gnu@1.5.0:
resolution: {integrity: sha512-V2stfUH3Qrc3cIXAd+cKbJruS1oJqqGd40GTVcKOKlRk9Ef9H3WNUQ5PyWKj1t1rk8AxjcBO/vK+Unkuy1WSCw==}
/@tauri-apps/cli-linux-x64-gnu@1.5.6:
resolution: {integrity: sha512-gbFHYHfdEGW0ffk8SigDsoXks6USpilF6wR0nqB/JbWzbzFR/sBuLVNQlJl1RKNakyJHu+lsFxGy0fcTdoX8xA==}
engines: {node: '>= 10'}
cpu: [x64]
os: [linux]
@ -994,8 +994,8 @@ packages:
dev: true
optional: true
/@tauri-apps/cli-linux-x64-musl@1.5.0:
resolution: {integrity: sha512-qpcGeesuksxzE7lC3RCnikTY9DCRMnAYwhWa9i8MA7pKDX1IXaEvAaXrse44XCZUohxLLgn2k2o6Pb+65dDijQ==}
/@tauri-apps/cli-linux-x64-musl@1.5.6:
resolution: {integrity: sha512-9v688ogoLkeFYQNgqiSErfhTreLUd8B3prIBSYUt+x4+5Kcw91zWvIh+VSxL1n3KCGGsM7cuXhkGPaxwlEh1ug==}
engines: {node: '>= 10'}
cpu: [x64]
os: [linux]
@ -1003,8 +1003,8 @@ packages:
dev: true
optional: true
/@tauri-apps/cli-win32-arm64-msvc@1.5.0:
resolution: {integrity: sha512-Glt/AEWwbFmFnQuoPRbB6vMzCIT9jYSpD59zRP8ljhRSzwDHE59q5nXOrheLKICwMmaXqtAuCIq9ndDBKghwoQ==}
/@tauri-apps/cli-win32-arm64-msvc@1.5.6:
resolution: {integrity: sha512-DRNDXFNZb6y5IZrw+lhTTA9l4wbzO4TNRBAlHAiXUrH+pRFZ/ZJtv5WEuAj9ocVSahVw2NaK5Yaold4NPAxHog==}
engines: {node: '>= 10'}
cpu: [arm64]
os: [win32]
@ -1012,8 +1012,8 @@ packages:
dev: true
optional: true
/@tauri-apps/cli-win32-ia32-msvc@1.5.0:
resolution: {integrity: sha512-k+R2VI8eZJfRjaRS8LwbkjMBKFaKcWtA/byaFnGDDUnb3VM/WFW++3KjC5Ne2wXpxFW9RVaFiBNIpmRcExI0Qw==}
/@tauri-apps/cli-win32-ia32-msvc@1.5.6:
resolution: {integrity: sha512-oUYKNR/IZjF4fsOzRpw0xesl2lOjhsQEyWlgbpT25T83EU113Xgck9UjtI7xemNI/OPCv1tPiaM1e7/ABdg5iA==}
engines: {node: '>= 10'}
cpu: [ia32]
os: [win32]
@ -1021,8 +1021,8 @@ packages:
dev: true
optional: true
/@tauri-apps/cli-win32-x64-msvc@1.5.0:
resolution: {integrity: sha512-jEwq9UuUldVyDJ/dsYoHFuZfNZIkxbeDYSMELfZXsRvWWEA8xRYeTkH38S++KU1eBl5dTK0LbxhztEB2HjRT1g==}
/@tauri-apps/cli-win32-x64-msvc@1.5.6:
resolution: {integrity: sha512-RmEf1os9C8//uq2hbjXi7Vgz9ne7798ZxqemAZdUwo1pv3oLVZSz1/IvZmUHPdy2e6zSeySqWu1D0Y3QRNN+dg==}
engines: {node: '>= 10'}
cpu: [x64]
os: [win32]
@ -1030,21 +1030,21 @@ packages:
dev: true
optional: true
/@tauri-apps/cli@1.5.0:
resolution: {integrity: sha512-usY7Ncfkxl2f/ufjWDtp+eJsodlj8ipMKExIt160KR+tx0GtQgLtxRnrKxe1o7wu18Pkqd5JIuWMaOmT3YZeYA==}
/@tauri-apps/cli@1.5.6:
resolution: {integrity: sha512-k4Y19oVCnt7WZb2TnDzLqfs7o98Jq0tUoVMv+JQSzuRDJqaVu2xMBZ8dYplEn+EccdR5SOMyzaLBJWu38TVK1A==}
engines: {node: '>= 10'}
hasBin: true
optionalDependencies:
'@tauri-apps/cli-darwin-arm64': 1.5.0
'@tauri-apps/cli-darwin-x64': 1.5.0
'@tauri-apps/cli-linux-arm-gnueabihf': 1.5.0
'@tauri-apps/cli-linux-arm64-gnu': 1.5.0
'@tauri-apps/cli-linux-arm64-musl': 1.5.0
'@tauri-apps/cli-linux-x64-gnu': 1.5.0
'@tauri-apps/cli-linux-x64-musl': 1.5.0
'@tauri-apps/cli-win32-arm64-msvc': 1.5.0
'@tauri-apps/cli-win32-ia32-msvc': 1.5.0
'@tauri-apps/cli-win32-x64-msvc': 1.5.0
'@tauri-apps/cli-darwin-arm64': 1.5.6
'@tauri-apps/cli-darwin-x64': 1.5.6
'@tauri-apps/cli-linux-arm-gnueabihf': 1.5.6
'@tauri-apps/cli-linux-arm64-gnu': 1.5.6
'@tauri-apps/cli-linux-arm64-musl': 1.5.6
'@tauri-apps/cli-linux-x64-gnu': 1.5.6
'@tauri-apps/cli-linux-x64-musl': 1.5.6
'@tauri-apps/cli-win32-arm64-msvc': 1.5.6
'@tauri-apps/cli-win32-ia32-msvc': 1.5.6
'@tauri-apps/cli-win32-x64-msvc': 1.5.6
dev: true
/@types/json-schema@7.0.14:
@ -1229,7 +1229,7 @@ packages:
resolution: {integrity: sha512-zuVdFrMJiuCDQUMCzQaD6KL28MjnqqN8XnAqiEq9PNm/hCPTSGfrXCOfwj1ow4LFb/tNymJPwsNbVePc1xFqrQ==}
dev: true
/@vitejs/plugin-react@4.0.3(vite@4.4.4):
/@vitejs/plugin-react@4.0.3(vite@4.5.2):
resolution: {integrity: sha512-pwXDog5nwwvSIzwrvYYmA2Ljcd/ZNlcsSG2Q9CNDBwnsd55UGAyr2doXtB5j+2uymRCnCfExlznzzSFbBRcoCg==}
engines: {node: ^14.18.0 || >=16.0.0}
peerDependencies:
@ -1239,7 +1239,7 @@ packages:
'@babel/plugin-transform-react-jsx-self': 7.22.5(@babel/core@7.23.2)
'@babel/plugin-transform-react-jsx-source': 7.22.5(@babel/core@7.23.2)
react-refresh: 0.14.0
vite: 4.4.4(@types/node@20.8.10)
vite: 4.5.2(@types/node@20.8.10)
transitivePeerDependencies:
- supports-color
dev: true
@ -2979,8 +2979,8 @@ packages:
punycode: 2.3.1
dev: true
/vite@4.4.4(@types/node@20.8.10):
resolution: {integrity: sha512-4mvsTxjkveWrKDJI70QmelfVqTm+ihFAb6+xf4sjEU2TmUCTlVX87tmg/QooPEMQb/lM9qGHT99ebqPziEd3wg==}
/vite@4.5.2(@types/node@20.8.10):
resolution: {integrity: sha512-tBCZBNSBbHQkaGyhGCDUGqeo2ph8Fstyp6FMSvTtsXeZSPpSMGlviAOav2hxVTqFcx8Hj/twtWKsMJXNY0xI8w==}
engines: {node: ^14.18.0 || >=16.0.0}
hasBin: true
peerDependencies:

View File

@ -4,7 +4,7 @@ use gpapi::service::{
request::{ConnectRequest, WsRequest},
vpn_state::VpnState,
};
use log::info;
use log::{info, warn};
use openconnect::Vpn;
use tokio::sync::{mpsc, oneshot, watch, RwLock};
use tokio_util::sync::CancellationToken;
@ -31,22 +31,29 @@ impl VpnTaskContext {
return;
}
let vpn_state_tx = self.vpn_state_tx.clone();
let info = req.info().clone();
let vpn_handle = Arc::clone(&self.vpn_handle);
let args = req.args();
let vpn = Vpn::builder(req.gateway().server(), args.cookie())
.user_agent(args.user_agent())
let vpn = match Vpn::builder(req.gateway().server(), args.cookie())
.script(args.vpnc_script())
.user_agent(args.user_agent())
.csd_uid(args.csd_uid())
.csd_wrapper(args.csd_wrapper())
.mtu(args.mtu())
.os(args.openconnect_os())
.build();
.build()
{
Ok(vpn) => vpn,
Err(err) => {
warn!("Failed to create VPN: {}", err);
vpn_state_tx.send(VpnState::Disconnected).ok();
return;
}
};
// Save the VPN handle
vpn_handle.write().await.replace(vpn);
let vpn_state_tx = self.vpn_state_tx.clone();
let connect_info = Box::new(info.clone());
vpn_state_tx.send(VpnState::Connecting(connect_info)).ok();

11
crates/common/Cargo.toml Normal file
View File

@ -0,0 +1,11 @@
[package]
name = "common"
rust-version.workspace = true
version.workspace = true
authors.workspace = true
homepage.workspace = true
edition.workspace = true
license.workspace = true
[dependencies]
is_executable.workspace = true

1
crates/common/src/lib.rs Normal file
View File

@ -0,0 +1 @@
pub mod vpn_utils;

View File

@ -0,0 +1,41 @@
use is_executable::IsExecutable;
use std::path::Path;
pub use is_executable::is_executable;
const VPNC_SCRIPT_LOCATIONS: [&str; 6] = [
"/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",
"/etc/openconnect/vpnc-script",
];
const CSD_WRAPPER_LOCATIONS: [&str; 3] = [
#[cfg(target_arch = "x86_64")]
"/usr/lib/x86_64-linux-gnu/openconnect/hipreport.sh",
#[cfg(target_arch = "aarch64")]
"/usr/lib/aarch64-linux-gnu/openconnect/hipreport.sh",
"/usr/lib/openconnect/hipreport.sh",
"/usr/libexec/openconnect/hipreport.sh",
];
fn find_executable(locations: &[&str]) -> Option<String> {
for location in locations.iter() {
let path = Path::new(location);
if path.is_executable() {
return Some(location.to_string());
}
}
None
}
pub fn find_vpnc_script() -> Option<String> {
find_executable(&VPNC_SCRIPT_LOCATIONS)
}
pub fn find_csd_wrapper() -> Option<String> {
find_executable(&CSD_WRAPPER_LOCATIONS)
}

View File

@ -6,8 +6,8 @@ license.workspace = true
links = "openconnect"
[dependencies]
common = { path = "../common" }
log.workspace = true
is_executable.workspace = true
[build-dependencies]
cc = "1"

View File

@ -1,5 +1,4 @@
mod ffi;
mod vpn;
mod vpnc_script;
pub use vpn::*;

View File

@ -1,11 +1,13 @@
use std::{
ffi::{c_char, CString},
fmt,
sync::{Arc, RwLock},
};
use common::vpn_utils::{find_vpnc_script, is_executable};
use log::info;
use crate::{ffi, vpnc_script::find_default_vpnc_script};
use crate::ffi;
type OnConnectedCallback = Arc<RwLock<Option<Box<dyn FnOnce() + 'static + Send + Sync>>>>;
@ -77,11 +79,31 @@ impl Vpn {
}
}
#[derive(Debug)]
pub struct VpnError<'a> {
message: &'a str,
}
impl<'a> VpnError<'a> {
fn new(message: &'a str) -> Self {
Self { message }
}
}
impl fmt::Display for VpnError<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.message)
}
}
impl std::error::Error for VpnError<'_> {}
pub struct VpnBuilder {
server: String,
cookie: String,
user_agent: Option<String>,
script: Option<String>,
user_agent: Option<String>,
os: Option<String>,
csd_uid: u32,
@ -95,22 +117,25 @@ impl VpnBuilder {
Self {
server: server.to_string(),
cookie: cookie.to_string(),
user_agent: None,
script: None,
user_agent: None,
os: None,
csd_uid: 0,
csd_wrapper: None,
mtu: 0,
}
}
pub fn user_agent<T: Into<Option<String>>>(mut self, user_agent: T) -> Self {
self.user_agent = user_agent.into();
pub fn script<T: Into<Option<String>>>(mut self, script: T) -> Self {
self.script = script.into();
self
}
pub fn script<T: Into<Option<String>>>(mut self, script: T) -> Self {
self.script = script.into();
pub fn user_agent<T: Into<Option<String>>>(mut self, user_agent: T) -> Self {
self.user_agent = user_agent.into();
self
}
@ -134,12 +159,27 @@ impl VpnBuilder {
self
}
pub fn build(self) -> Vpn {
pub fn build(self) -> Result<Vpn, VpnError<'static>> {
let script = match self.script {
Some(script) => {
if !is_executable(&script) {
return Err(VpnError::new("vpnc script is not executable"));
}
script
}
None => find_vpnc_script().ok_or_else(|| VpnError::new("Failed to find vpnc-script"))?,
};
if let Some(csd_wrapper) = &self.csd_wrapper {
if !is_executable(csd_wrapper) {
return Err(VpnError::new("CSD wrapper is not executable"));
}
}
let user_agent = self.user_agent.unwrap_or_default();
let script = self.script.or_else(find_default_vpnc_script).unwrap_or_default();
let os = self.os.unwrap_or("linux".to_string());
Vpn {
Ok(Vpn {
server: Self::to_cstring(&self.server),
cookie: Self::to_cstring(&self.cookie),
user_agent: Self::to_cstring(&user_agent),
@ -154,7 +194,7 @@ impl VpnBuilder {
mtu: self.mtu,
callback: Default::default(),
}
})
}
fn to_cstring(value: &str) -> CString {

View File

@ -1,24 +0,0 @@
use is_executable::IsExecutable;
use std::path::Path;
const VPNC_SCRIPT_LOCATIONS: [&str; 6] = [
"/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",
"/etc/openconnect/vpnc-script"
];
pub(crate) fn find_default_vpnc_script() -> Option<String> {
for location in VPNC_SCRIPT_LOCATIONS.iter() {
let path = Path::new(location);
if path.is_executable() {
return Some(location.to_string());
}
}
log::warn!("vpnc-script not found");
None
}