Skip to content

Commit

Permalink
only initiate sa if necessary
Browse files Browse the repository at this point in the history
  • Loading branch information
NickCao committed Jan 23, 2024
1 parent 9b79634 commit 4b4949b
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 0 deletions.
53 changes: 53 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
36 changes: 36 additions & 0 deletions src/vici.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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<Vec<HashMap<String, SA>>, Error> {
let sas = self.client.stream_request::<ListSAs, SAs>(
"list-sas",
"list-sa",
ListSAs { ike: name },
);

Ok(sas.try_collect::<Vec<_>>().await?)
}
}

#[derive(Debug, Deserialize)]
Expand Down Expand Up @@ -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<String>,
child_sas: HashMap<String, serde::de::IgnoredAny>,
}

type SAs = HashMap<String, SA>;

#[derive(Debug, Serialize)]
struct Terminate<'a> {
ike: &'a str,
Expand Down

0 comments on commit 4b4949b

Please sign in to comment.