From 4b4949bbf3e3e6c0b008bfe72b12222d17de42fd Mon Sep 17 00:00:00 2001 From: Nick Cao Date: Tue, 23 Jan 2024 18:25:21 -0500 Subject: [PATCH] only initiate sa if necessary --- Cargo.lock | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + src/vici.rs | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 90 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 27d49c3..5c0ddcc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -227,12 +227,54 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "00b0228411908ca8685dba7fc2cdd70ec9990a6e753e89b6ac91a84c40fbaf4b" +[[package]] +name = "futures" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "645c6916888f6cb6350d2550b80fb63e734897a8498abe35cfb732b6487804b0" +dependencies = [ + "futures-channel", + "futures-core", + "futures-executor", + "futures-io", + "futures-sink", + "futures-task", + "futures-util", +] + +[[package]] +name = "futures-channel" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eac8f7d7865dcb88bd4373ab671c8cf4508703796caa2b1985a9ca867b3fcb78" +dependencies = [ + "futures-core", + "futures-sink", +] + [[package]] name = "futures-core" version = "0.3.30" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dfc6580bb841c5a68e9ef15c77ccc837b40a7504914d52e47b8b0e9bbda25a1d" +[[package]] +name = "futures-executor" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a576fc72ae164fca6b9db127eaa9a9dda0d61316034f33a0a0d4eda41f02b01d" +dependencies = [ + "futures-core", + "futures-task", + "futures-util", +] + +[[package]] +name = "futures-io" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a44623e20b9681a318efdd71c299b6b222ed6f231972bfe2f224ebad6311f0c1" + [[package]] name = "futures-macro" version = "0.3.30" @@ -244,6 +286,12 @@ dependencies = [ "syn 2.0.48", ] +[[package]] +name = "futures-sink" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9fb8e00e87438d937621c1c6269e53f536c14d3fbd6a042bb24879e57d474fb5" + [[package]] name = "futures-task" version = "0.3.30" @@ -256,9 +304,13 @@ version = "0.3.30" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3d6401deb83407ab3da39eba7e33987a73c3df0c82b4bb5813ee871c19c41d48" dependencies = [ + "futures-channel", "futures-core", + "futures-io", "futures-macro", + "futures-sink", "futures-task", + "memchr", "pin-project-lite", "pin-utils", "slab", @@ -542,6 +594,7 @@ name = "ranet" version = "0.9.3" dependencies = [ "clap", + "futures", "hex", "indoc", "ipnet", diff --git a/Cargo.toml b/Cargo.toml index d06bad5..8144b15 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,6 +17,7 @@ thiserror = "*" indoc = "*" tracing = { version = "0.1.40" } tracing-subscriber = { version = "0.3.18", features = [ "env-filter" ] } +futures = "0.3.30" [features] vendored = ["openssl/vendored"] diff --git a/src/vici.rs b/src/vici.rs index 8ba3762..f8722c1 100644 --- a/src/vici.rs +++ b/src/vici.rs @@ -1,6 +1,8 @@ use crate::error::Error; +use futures::TryStreamExt; use serde::{Deserialize, Serialize}; use std::{collections::HashMap, path::Path}; +use tracing::debug; pub struct Client { client: rsvici::Client, @@ -40,6 +42,16 @@ impl Client { resp.parse() } pub async fn initiate(&mut self, name: &str) -> Result<(), Error> { + let sas = self.list_sas(name).await?; + + for sa in sas.iter().flat_map(|v| v.values()) { + if sa.child_sas.len() > 0 || sa.tasks_active.contains(&"CHILD_CREATE".to_string()) { + return Ok(()); + } + } + + debug!("initiating sa {}", name); + let _res: Status = self .client .request( @@ -75,6 +87,15 @@ impl Client { let res: Status = self.client.request("unload-conn", Unload { name }).await?; res.parse() } + async fn list_sas(&mut self, name: &str) -> Result>, Error> { + let sas = self.client.stream_request::( + "list-sas", + "list-sa", + ListSAs { ike: name }, + ); + + Ok(sas.try_collect::>().await?) + } } #[derive(Debug, Deserialize)] @@ -124,6 +145,21 @@ struct Initiate<'a, 'b> { init_limits: bool, } +#[derive(Debug, Serialize)] +struct ListSAs<'a> { + ike: &'a str, +} + +#[derive(Debug, Deserialize)] +#[serde(rename_all = "kebab-case")] +struct SA { + #[serde(default)] + tasks_active: Vec, + child_sas: HashMap, +} + +type SAs = HashMap; + #[derive(Debug, Serialize)] struct Terminate<'a> { ike: &'a str,