diff --git a/src/main.rs b/src/main.rs index 716ca83..3e90fcf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,10 +1,9 @@ -use anyhow::{Context, Result}; +use anyhow::{bail, Context, Result}; use base64::Engine; use clap::{Parser, Subcommand}; use serde::{Deserialize, Serialize}; use std::collections::BTreeSet; use std::fmt::Display; -use std::path::Path; use std::str::FromStr; use std::sync::Arc; use std::{collections::BTreeMap, path::PathBuf}; @@ -95,7 +94,7 @@ enum ClientOp { Export, Import, Add(PortMapping), - Del(Vec), + Del(Vec), Reload, } @@ -121,6 +120,58 @@ impl + Serialize> ClientResp { } } +/* +#[instrument] +async fn delete_mapping(config: &Arc>>) -> Result<()> { + for mapping in config.read().await.flat_mappings.iter() { + let (rname, tid, pmap) = mapping; + let cfg = &config.read().await; + let mappings = &cfg + .remotes + .get(rname) + .with_context(|| { + format!("Remote was supposed to exist, but couldn't be found: {rname}") + })? + .tunnels + .iter() + .nth(*tid) + .with_context(|| { + format!("Tunnel was supposed to exist, but couldn't be found: {rname}${tid}") + })? + .mappings; + let (mapping_id, _) = mappings + .iter() + .find(|(i, pm)| pm == pmap) + .context("Port mapping was supposed to exist, but wasn't found")?; + Ok(mappings.remove(mapping_id)) + } +} +*/ + +fn delete_mapping_by_flat_id(cfg: Config<'static>, i: usize) -> Result { + let (rname, tid, pm) = cfg + .flat_mappings + .iter() + .nth(i) + .with_context(|| format!("mapping does not exist currently {i}"))?; + let mappings = &mut cfg + .remotes + .get(rname) + .with_context(|| format!("Remote was expected to exist: {rname}"))? + .tunnels + .iter() + .nth(*tid) + .with_context(|| format!("Tunnel was expeted to exist {rname}>{tid}"))? + .mappings; + if let Some((id, _)) = mappings.iter().find(|(_, m)| pm == m) { + Ok(mappings.remove(id).unwrap()) + } else { + bail!(format!( + "Couldn't find associated port mapping! {rname}>{tid}.{pm}" + )) + } +} + #[instrument] async fn handle_ipc_client(mut conn: UnixStream, app: Arc) { let config = &app.config; @@ -137,7 +188,9 @@ async fn handle_ipc_client(mut conn: UnixStream, app: Arc) { let resp: ClientResp<_> = match serde_json::from_str::(&buf) { Ok(req) => match req.op { ClientOp::Del(ni) => { - + let cfg = config.write().await; + for i in ni {} + todo!() // let cfg = config.write().await; // let km: Vec = ni // .iter() @@ -202,15 +255,13 @@ async fn ipc_server(app: Arc, sock: UnixListener) { } fn spawn_listeners(cfg: &Config) { - for (id, mapping) in &cfg.mappings { - info!("Starting port mapping {id} : {mapping}"); - let handle = tokio::spawn(listener_run); + for (_, _, _mapping) in &cfg.flat_mappings { + todo!("Spawn threads for every mapping") } } async fn quic_server(app: Arc, mut notify: mpsc::Receiver<()>) { - let mut handles = Vec::new(); - let (tx, rx) = broadcast::channel::<()>(5); + let (tx, _rx) = broadcast::channel::<()>(5); while notify.recv().await.is_some() { let mut active = app.active_config.write().await;