(WIP) basic client operations

This commit is contained in:
Solomon W. 2025-03-12 16:50:09 -04:00
parent ec63debe4f
commit aa415c3599

View File

@ -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<u32>),
Del(Vec<usize>),
Reload,
}
@ -121,6 +120,58 @@ impl<S: AsRef<str> + Serialize> ClientResp<S> {
}
}
/*
#[instrument]
async fn delete_mapping(config: &Arc<RwLock<Config<'static>>>) -> 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<PortMapping> {
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<AppState>) {
let config = &app.config;
@ -137,7 +188,9 @@ async fn handle_ipc_client(mut conn: UnixStream, app: Arc<AppState>) {
let resp: ClientResp<_> = match serde_json::from_str::<IPCRequest>(&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<String> = ni
// .iter()
@ -202,15 +255,13 @@ async fn ipc_server(app: Arc<AppState>, 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<AppState>, 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;