From 558485f5a978913262becd83f53755ea0624666b Mon Sep 17 00:00:00 2001 From: Kevin Yue Date: Sun, 17 Mar 2024 18:41:42 +0800 Subject: [PATCH] Add the --hip option --- Cargo.lock | 1 + apps/gpclient/Cargo.toml | 1 + apps/gpclient/src/connect.rs | 27 ++++++++++++++++++++++----- 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 70c947c..c88d398 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1486,6 +1486,7 @@ version = "2.1.0" dependencies = [ "anyhow", "clap", + "common", "compile-time", "directories", "env_logger", diff --git a/apps/gpclient/Cargo.toml b/apps/gpclient/Cargo.toml index da76af4..b2594bd 100644 --- a/apps/gpclient/Cargo.toml +++ b/apps/gpclient/Cargo.toml @@ -6,6 +6,7 @@ edition.workspace = true license.workspace = true [dependencies] +common = { path = "../../crates/common" } gpapi = { path = "../../crates/gpapi", features = ["clap"] } openconnect = { path = "../../crates/openconnect" } anyhow.workspace = true diff --git a/apps/gpclient/src/connect.rs b/apps/gpclient/src/connect.rs index c07dbe0..b72e620 100644 --- a/apps/gpclient/src/connect.rs +++ b/apps/gpclient/src/connect.rs @@ -1,6 +1,7 @@ use std::{fs, sync::Arc}; use clap::Args; +use common::vpn_utils::find_csd_wrapper; use gpapi::{ clap::args::Os, credential::{Credential, PasswordCredential}, @@ -31,6 +32,12 @@ pub(crate) struct ConnectArgs { #[arg(long, short, help = "The VPNC script to use")] script: Option, + #[arg( + long, + help = "Use the default CSD wrapper to generate the HIP report and send it to the server" + )] + hip: bool, + #[arg(long, help = "Same as the '--csd-user' option in the openconnect command")] csd_user: Option, @@ -112,16 +119,19 @@ impl<'a> ConnectHandler<'a> { let selected_gateway = match &self.args.gateway { Some(gateway) => portal_config .find_gateway(gateway) - .ok_or_else(|| anyhow::anyhow!("Cannot find gateway {}", gateway))?, + .ok_or_else(|| anyhow::anyhow!("Cannot find gateway specified: {}", gateway))?, None => { portal_config.sort_gateways(prelogin.region()); let gateways = portal_config.gateways(); if gateways.len() > 1 { - Select::new("Which gateway do you want to connect to?", gateways) + let gateway = Select::new("Which gateway do you want to connect to?", gateways) .with_vim_mode(true) - .prompt()? + .prompt()?; + info!("Connecting to the selected gateway: {}", gateway); + gateway } else { + info!("Connecting to the only available gateway: {}", gateways[0]); gateways[0] } } @@ -154,14 +164,21 @@ impl<'a> ConnectHandler<'a> { } async fn connect_gateway(&self, gateway: &str, cookie: &str) -> anyhow::Result<()> { - let csd_uid = get_csd_uid(&self.args.csd_user)?; let mtu = self.args.mtu.unwrap_or(0); + let csd_uid = get_csd_uid(&self.args.csd_user)?; + let csd_wrapper = if self.args.csd_user.is_some() { + self.args.csd_wrapper.clone() + } else if self.args.hip { + find_csd_wrapper() + } else { + None + }; let vpn = Vpn::builder(gateway, cookie) .script(self.args.script.clone()) .user_agent(self.args.user_agent.clone()) .csd_uid(csd_uid) - .csd_wrapper(self.args.csd_wrapper.clone()) + .csd_wrapper(csd_wrapper) .mtu(mtu) .build()?;