use std::{ffi::CString, io, marker::PhantomData, rc::Rc, sync::Arc}; use crate::{ffi, vpn_utils::check_executable}; pub type HipGenerator = dyn Fn(&HipRequest, &HipControl<'_>) -> io::Result + Send + Sync; type HipValidator = dyn Fn(&HipControl<'_>) -> io::Result<()> + Send + Sync; #[derive(Default)] pub enum HipSource { #[default] Disabled, Generator(Arc), Script(HipScript), } impl HipSource { /// Collect through the same bounded executor used by tunnel submissions. /// Disabled posture is a caller policy; this method never invents a report. pub fn collect(&self, request: &HipRequest, check: &dyn Fn() -> io::Result<()>) -> io::Result { ffi::collect_hip_source(self, request, check) } } /// Current gateway and negotiated identity inputs for one report invocation. #[derive(Clone, Default)] pub struct HipRequest { pub cookie: String, pub client_ip: Option, pub client_ipv6: Option, pub md5: String, pub client_version: String, pub client_os: String, pub os_version: String, pub host_id: Option, pub local_hostname: Option, } /// Borrowed cancellation and deadline control. It must stay on the invocation thread. pub struct HipControl<'a> { pub(crate) raw: &'a ffi::HipControlRaw, pub(crate) _thread: PhantomData>, } impl HipControl<'_> { pub fn check(&self) -> io::Result<()> { let result = unsafe { (self.raw.check)(self.raw.data) }; if result < 0 { Err(io::Error::from_raw_os_error(-result)) } else { Ok(()) } } } pub struct HipScriptEnvironment { pub variables: Vec<(String, String)>, pub cwd: String, } pub struct HipScript { pub(crate) path: CString, pub(crate) user: Option, pub(crate) validator: Option>, pub(crate) environment: Option>, pub(crate) cwd: Option, } impl HipScript { pub fn new(path: String, user: Option) -> io::Result { if !std::fs::metadata(&path)?.is_file() { return Err(io::Error::new( io::ErrorKind::InvalidInput, "HIP executable is not a regular file", )); } check_executable(&path)?; let path = c_string(path)?; Ok(Self { path, user, validator: None, environment: None, cwd: None, }) } pub fn with_validator( mut self, validator: impl Fn(&HipControl<'_>) -> io::Result<()> + Send + Sync + 'static, ) -> Self { self.validator = Some(Arc::new(validator)); self } /// A child-local replacement environment and working directory, never process-global changes. pub fn with_environment(mut self, environment: HipScriptEnvironment) -> io::Result { let variables = environment .variables .into_iter() .map(|(key, value)| { if key.is_empty() || key.contains('=') { return Err(io::Error::new( io::ErrorKind::InvalidInput, "Invalid HIP environment key", )); } c_string(format!("{key}={value}")) }) .collect::>>()?; self.environment = Some(variables); self.cwd = Some(c_string(environment.cwd)?); Ok(self) } /// Collect using the same OpenConnect subprocess machinery as connected submissions. pub fn collect(&self, request: &HipRequest, check: &dyn Fn() -> io::Result<()>) -> io::Result { ffi::collect_hip_script(self, request, check) } } fn c_string(value: String) -> io::Result { CString::new(value).map_err(|_| io::Error::new(io::ErrorKind::InvalidInput, "HIP value contains NUL")) }