Compare commits

...

4 Commits

Author SHA1 Message Date
Kevin Yue
a884c41813 Rename PreloginCredential 2024-04-06 19:40:08 +08:00
Kevin Yue
879b977321 Add message for the '--as-gateway' option 2024-04-06 19:26:42 +08:00
Kevin Yue
e9cb253be1 Update dependencies 2024-04-06 19:14:31 +08:00
Kevin Yue
07eacae385 Add '--as-gateway' option (#318) 2024-04-06 19:07:09 +08:00
3 changed files with 41 additions and 21 deletions

27
Cargo.lock generated
View File

@ -1599,9 +1599,9 @@ dependencies = [
[[package]] [[package]]
name = "h2" name = "h2"
version = "0.3.24" version = "0.3.26"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bb2c4422095b67ee78da96fbb51a4cc413b3b25883c7717ff7ca1ab31022c9c9" checksum = "81fe527a889e1532da5c525686d96d4c2e74cdd345badf8dfef9f6b39dd5f5e8"
dependencies = [ dependencies = [
"bytes", "bytes",
"fnv", "fnv",
@ -1618,9 +1618,9 @@ dependencies = [
[[package]] [[package]]
name = "h2" name = "h2"
version = "0.4.2" version = "0.4.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "31d030e59af851932b72ceebadf4a2b5986dba4c3b99dd2493f8273a0f151943" checksum = "816ec7294445779408f36fe57bc5b7fc1cf59664059096c65f905c1c61f58069"
dependencies = [ dependencies = [
"bytes", "bytes",
"fnv", "fnv",
@ -1787,7 +1787,7 @@ dependencies = [
"futures-channel", "futures-channel",
"futures-core", "futures-core",
"futures-util", "futures-util",
"h2 0.3.24", "h2 0.3.26",
"http 0.2.11", "http 0.2.11",
"http-body 0.4.6", "http-body 0.4.6",
"httparse", "httparse",
@ -1810,7 +1810,7 @@ dependencies = [
"bytes", "bytes",
"futures-channel", "futures-channel",
"futures-util", "futures-util",
"h2 0.4.2", "h2 0.4.4",
"http 1.0.0", "http 1.0.0",
"http-body 1.0.0", "http-body 1.0.0",
"httparse", "httparse",
@ -3167,7 +3167,7 @@ dependencies = [
"encoding_rs", "encoding_rs",
"futures-core", "futures-core",
"futures-util", "futures-util",
"h2 0.3.24", "h2 0.3.26",
"http 0.2.11", "http 0.2.11",
"http-body 0.4.6", "http-body 0.4.6",
"hyper 0.14.28", "hyper 0.14.28",
@ -4606,6 +4606,12 @@ version = "0.11.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
[[package]]
name = "wasite"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b8dad83b4f25e74f184f64c43b150b91efe7647395b42289f38e50566d82855b"
[[package]] [[package]]
name = "wasm-bindgen" name = "wasm-bindgen"
version = "0.2.89" version = "0.2.89"
@ -4782,11 +4788,12 @@ dependencies = [
[[package]] [[package]]
name = "whoami" name = "whoami"
version = "1.4.1" version = "1.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "22fc3756b8a9133049b26c7f61ab35416c130e8c09b660f5b3958b446f52cc50" checksum = "a44ab49fad634e88f55bf8f9bb3abd2f27d7204172a112c7c9987e01c1c94ea9"
dependencies = [ dependencies = [
"wasm-bindgen", "redox_syscall",
"wasite",
"web-sys", "web-sys",
] ]

View File

@ -32,6 +32,8 @@ pub(crate) struct ConnectArgs {
user: Option<String>, user: Option<String>,
#[arg(long, short, help = "The VPNC script to use")] #[arg(long, short, help = "The VPNC script to use")]
script: Option<String>, script: Option<String>,
#[arg(long, help = "Treat the server as a gateway, instead of a portal")]
as_gateway: bool,
#[arg( #[arg(
long, long,
@ -95,6 +97,12 @@ impl<'a> ConnectHandler<'a> {
pub(crate) async fn handle(&self) -> anyhow::Result<()> { pub(crate) async fn handle(&self) -> anyhow::Result<()> {
let server = self.args.server.as_str(); let server = self.args.server.as_str();
let as_gateway = self.args.as_gateway;
if as_gateway {
info!("Treating the server as a gateway");
return self.connect_gateway_with_prelogin(server).await;
}
let Err(err) = self.connect_portal_with_prelogin(server).await else { let Err(err) = self.connect_portal_with_prelogin(server).await else {
return Ok(()); return Ok(());
@ -103,10 +111,15 @@ impl<'a> ConnectHandler<'a> {
info!("Failed to connect portal with prelogin: {}", err); info!("Failed to connect portal with prelogin: {}", err);
if err.root_cause().downcast_ref::<PortalError>().is_some() { if err.root_cause().downcast_ref::<PortalError>().is_some() {
info!("Trying the gateway authentication workflow..."); info!("Trying the gateway authentication workflow...");
return self.connect_gateway_with_prelogin(server).await; self.connect_gateway_with_prelogin(server).await?;
}
Err(err) eprintln!("\nNOTE: the server may be a gateway, not a portal.");
eprintln!("NOTE: try to use the `--as-gateway` option if you were authenticated twice.");
Ok(())
} else {
Err(err)
}
} }
async fn connect_portal_with_prelogin(&self, portal: &str) -> anyhow::Result<()> { async fn connect_portal_with_prelogin(&self, portal: &str) -> anyhow::Result<()> {

View File

@ -37,13 +37,13 @@ impl From<&CachedCredential> for PasswordCredential {
#[derive(Debug, Serialize, Deserialize, Type, Clone)] #[derive(Debug, Serialize, Deserialize, Type, Clone)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct PreloginCookieCredential { pub struct PreloginCredential {
username: String, username: String,
prelogin_cookie: Option<String>, prelogin_cookie: Option<String>,
token: Option<String>, token: Option<String>,
} }
impl PreloginCookieCredential { impl PreloginCredential {
pub fn new(username: &str, prelogin_cookie: Option<&str>, token: Option<&str>) -> Self { pub fn new(username: &str, prelogin_cookie: Option<&str>, token: Option<&str>) -> Self {
Self { Self {
username: username.to_string(), username: username.to_string(),
@ -65,7 +65,7 @@ impl PreloginCookieCredential {
} }
} }
impl From<SamlAuthData> for PreloginCookieCredential { impl From<SamlAuthData> for PreloginCredential {
fn from(value: SamlAuthData) -> Self { fn from(value: SamlAuthData) -> Self {
let username = value.username().to_string(); let username = value.username().to_string();
let prelogin_cookie = value.prelogin_cookie(); let prelogin_cookie = value.prelogin_cookie();
@ -160,7 +160,7 @@ impl From<PasswordCredential> for CachedCredential {
#[serde(tag = "type", rename_all = "camelCase")] #[serde(tag = "type", rename_all = "camelCase")]
pub enum Credential { pub enum Credential {
Password(PasswordCredential), Password(PasswordCredential),
PreloginCookie(PreloginCookieCredential), Prelogin(PreloginCredential),
AuthCookie(AuthCookieCredential), AuthCookie(AuthCookieCredential),
CachedCredential(CachedCredential), CachedCredential(CachedCredential),
} }
@ -177,7 +177,7 @@ impl Credential {
pub fn username(&self) -> &str { pub fn username(&self) -> &str {
match self { match self {
Credential::Password(cred) => cred.username(), Credential::Password(cred) => cred.username(),
Credential::PreloginCookie(cred) => cred.username(), Credential::Prelogin(cred) => cred.username(),
Credential::AuthCookie(cred) => cred.username(), Credential::AuthCookie(cred) => cred.username(),
Credential::CachedCredential(cred) => cred.username(), Credential::CachedCredential(cred) => cred.username(),
} }
@ -189,7 +189,7 @@ impl Credential {
let (passwd, prelogin_cookie, portal_userauthcookie, portal_prelogonuserauthcookie, token) = match self { let (passwd, prelogin_cookie, portal_userauthcookie, portal_prelogonuserauthcookie, token) = match self {
Credential::Password(cred) => (Some(cred.password()), None, None, None, None), Credential::Password(cred) => (Some(cred.password()), None, None, None, None),
Credential::PreloginCookie(cred) => (None, cred.prelogin_cookie(), None, None, cred.token()), Credential::Prelogin(cred) => (None, cred.prelogin_cookie(), None, None, cred.token()),
Credential::AuthCookie(cred) => ( Credential::AuthCookie(cred) => (
None, None,
None, None,
@ -224,9 +224,9 @@ impl Credential {
impl From<SamlAuthData> for Credential { impl From<SamlAuthData> for Credential {
fn from(value: SamlAuthData) -> Self { fn from(value: SamlAuthData) -> Self {
let cred = PreloginCookieCredential::from(value); let cred = PreloginCredential::from(value);
Self::PreloginCookie(cred) Self::Prelogin(cred)
} }
} }