(WIP) basic client operations
This commit is contained in:
parent
ec63debe4f
commit
aa415c3599
69
src/main.rs
69
src/main.rs
@ -1,10 +1,9 @@
|
|||||||
use anyhow::{Context, Result};
|
use anyhow::{bail, Context, Result};
|
||||||
use base64::Engine;
|
use base64::Engine;
|
||||||
use clap::{Parser, Subcommand};
|
use clap::{Parser, Subcommand};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::collections::BTreeSet;
|
use std::collections::BTreeSet;
|
||||||
use std::fmt::Display;
|
use std::fmt::Display;
|
||||||
use std::path::Path;
|
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use std::{collections::BTreeMap, path::PathBuf};
|
use std::{collections::BTreeMap, path::PathBuf};
|
||||||
@ -95,7 +94,7 @@ enum ClientOp {
|
|||||||
Export,
|
Export,
|
||||||
Import,
|
Import,
|
||||||
Add(PortMapping),
|
Add(PortMapping),
|
||||||
Del(Vec<u32>),
|
Del(Vec<usize>),
|
||||||
Reload,
|
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]
|
#[instrument]
|
||||||
async fn handle_ipc_client(mut conn: UnixStream, app: Arc<AppState>) {
|
async fn handle_ipc_client(mut conn: UnixStream, app: Arc<AppState>) {
|
||||||
let config = &app.config;
|
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) {
|
let resp: ClientResp<_> = match serde_json::from_str::<IPCRequest>(&buf) {
|
||||||
Ok(req) => match req.op {
|
Ok(req) => match req.op {
|
||||||
ClientOp::Del(ni) => {
|
ClientOp::Del(ni) => {
|
||||||
|
let cfg = config.write().await;
|
||||||
|
for i in ni {}
|
||||||
|
todo!()
|
||||||
// let cfg = config.write().await;
|
// let cfg = config.write().await;
|
||||||
// let km: Vec<String> = ni
|
// let km: Vec<String> = ni
|
||||||
// .iter()
|
// .iter()
|
||||||
@ -202,15 +255,13 @@ async fn ipc_server(app: Arc<AppState>, sock: UnixListener) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn spawn_listeners(cfg: &Config) {
|
fn spawn_listeners(cfg: &Config) {
|
||||||
for (id, mapping) in &cfg.mappings {
|
for (_, _, _mapping) in &cfg.flat_mappings {
|
||||||
info!("Starting port mapping {id} : {mapping}");
|
todo!("Spawn threads for every mapping")
|
||||||
let handle = tokio::spawn(listener_run);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn quic_server(app: Arc<AppState>, mut notify: mpsc::Receiver<()>) {
|
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() {
|
while notify.recv().await.is_some() {
|
||||||
let mut active = app.active_config.write().await;
|
let mut active = app.active_config.write().await;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user