From 21b55e483dd0649cd690f370e9fb259ea0d96ea9 Mon Sep 17 00:00:00 2001 From: redxef Date: Mon, 22 Jan 2024 21:10:18 +0100 Subject: [PATCH] Remove dead code. --- src/event.rs | 5 -- src/helpers.rs | 23 --------- src/main.rs | 135 +------------------------------------------------ src/wg.rs | 36 ------------- 4 files changed, 2 insertions(+), 197 deletions(-) diff --git a/src/event.rs b/src/event.rs index 0843755..fc8a00f 100644 --- a/src/event.rs +++ b/src/event.rs @@ -1,12 +1,9 @@ use std::str::FromStr; -use std::sync::Arc; use cidr::IpInet; use serde::{Deserialize, Serialize}; -use tokio::sync::Mutex; type WgLink = String; -type WgPeer = String; #[derive(Clone, Debug, Serialize, Deserialize)] pub struct EventInfo { @@ -55,5 +52,3 @@ impl Event { } } } - -type EventStore = Arc>>; diff --git a/src/helpers.rs b/src/helpers.rs index 40e38cf..c77e684 100644 --- a/src/helpers.rs +++ b/src/helpers.rs @@ -1,7 +1,5 @@ use std::process::Output; use tokio::process::Command; -use warp::{Filter, Rejection}; -use std::str::FromStr; pub async fn command_output(command: &mut Command) -> std::io::Result { log::debug!("Shell command {:?}", command); @@ -9,24 +7,3 @@ pub async fn command_output(command: &mut Command) -> std::io::Result { log::debug!("Output: {:?}", output); output } - -async fn param_handler(s: String) -> Result -where - ::Err: std::fmt::Debug, -{ - let new_s = match urlencoding::decode(&s) { - Ok(v) => v, - Err(_e) => return Err(warp::reject::not_found()), - }; - let t = match T::from_str(&new_s) { - Ok(v) => v, - Err(_e) => return Err(warp::reject::not_found()), - }; - Ok(t) -} -pub fn param() -> impl Filter + Copy -where - ::Err: std::fmt::Debug, -{ - warp::path::param::().and_then(param_handler) -} diff --git a/src/main.rs b/src/main.rs index 41f8fe3..248f277 100644 --- a/src/main.rs +++ b/src/main.rs @@ -30,35 +30,8 @@ struct RejectBadRequest; impl warp::reject::Reject for RejectBadRequest {} #[derive(Debug)] -struct CommandError { - stdout: String, - stderr: String, -} +struct CommandError {} impl warp::reject::Reject for CommandError {} -impl From for CommandError { - fn from(value: std::process::Output) -> Self { - CommandError { - stdout: std::str::from_utf8(value.stdout.as_ref()).unwrap().into(), - stderr: std::str::from_utf8(value.stderr.as_ref()).unwrap().into(), - } - } -} - -fn split_allowed_ips_text_line(text: &str) -> (WgPeer, Vec) { - let mut string_parts = text.split(|c| c == '\t' || c == ' '); - let peer = string_parts.next().unwrap(); - let cidrs = string_parts - .map(|p| cidr::IpInet::from_str(p).unwrap()) - .collect(); - (peer.into(), cidrs) -} -fn split_allowed_ips_text(text: &str, peer: WgPeer) -> (WgPeer, Vec) { - text.split('\n') - .map(|p| split_allowed_ips_text_line(p)) - .filter(|l| l.0 == peer) - .next() - .unwrap() -} async fn wg_modify_address( config: wg::Wg, @@ -114,111 +87,7 @@ async fn wg_modify_address( if output.status.success() { Ok("") } else { - Err(warp::reject::custom(CommandError::from(output))) - } -} - -async fn wg_add_address( - link: WgLink, - peer: WgPeer, - cidr: cidr::IpInet, -) -> Result { - let output = match command_output( - tokio::process::Command::new("wg") - .arg("show") - .arg(&link) - .arg("allowed-ips"), - ) - .await - { - Ok(v) => v, - Err(_) => return Err(warp::reject::custom(RejectCommandFailedToExecute)), - }; - if !output.status.success() { - return Err(warp::reject::custom(CommandError::from(output))); - } - let (peer, ips) = - split_allowed_ips_text(std::str::from_utf8(output.stdout.as_ref()).unwrap(), peer); - let ips_str = - ips.iter() - .chain([cidr].iter()) - .map(|x| x.to_string()) - .fold("".to_string(), |acc, x| { - let mut s = String::from(acc); - s.push_str(&x); - s.push_str(","); - s - }); - let ips_str = &ips_str[0..ips_str.len() - 1]; - let mut command = tokio::process::Command::new("wg"); - command - .arg("set") - .arg(&link) - .arg("peer") - .arg(&peer) - .arg("allowed-ips") - .arg(ips_str); - let output = match command_output(&mut command).await { - Ok(v) => v, - Err(_) => return Err(warp::reject::custom(RejectCommandFailedToExecute)), - }; - - if output.status.success() { - Ok("") - } else { - Err(warp::reject::custom(CommandError::from(output))) - } -} -async fn wg_del_address( - link: WgLink, - peer: WgPeer, - cidr: cidr::IpInet, -) -> Result { - let output = match tokio::process::Command::new("wg") - .arg("show") - .arg(&link) - .arg("allowed-ips") - .output() - .await - { - Ok(v) => v, - Err(_) => return Err(warp::reject::custom(RejectCommandFailedToExecute)), - }; - if !output.status.success() { - return Err(warp::reject::custom(CommandError::from(output))); - } - let (peer, ips) = - split_allowed_ips_text(std::str::from_utf8(output.stdout.as_ref()).unwrap(), peer); - let ips_str = ips - .iter() - .filter(|x| **x != cidr) - .map(|x| x.to_string()) - .fold("".to_string(), |acc, x| { - let mut s = String::from(acc); - s.push_str(&x); - s.push_str(","); - s - }); - let ips_str = &ips_str[0..ips_str.len() - 1]; - let mut command = tokio::process::Command::new("wg"); - command - .arg("set") - .arg(&link) - .arg("peer") - .arg(&peer) - .arg("allowed-ips") - .arg(ips_str); - println!("command = {:?}", command); - let output = match command.output().await { - Ok(v) => v, - Err(_) => return Err(warp::reject::custom(RejectCommandFailedToExecute)), - }; - println!("{:?}", output); - - if output.status.success() { - Ok("") - } else { - Err(warp::reject::custom(CommandError::from(output))) + Err(warp::reject::custom(CommandError {})) } } diff --git a/src/wg.rs b/src/wg.rs index 3d56a35..cbbb92c 100644 --- a/src/wg.rs +++ b/src/wg.rs @@ -7,42 +7,6 @@ use serde_with::{serde_as}; use std::path::{Path, PathBuf}; -fn de_one_many<'de, T, D>(deserializer: D) -> Result, D::Error> -where - D: Deserializer<'de>, - T: serde::Deserialize<'de>, -{ - #[derive(Deserialize)] - #[serde(untagged)] - enum OneMany { - One(T), - Vec(Vec), - } - match OneMany::::deserialize(deserializer)? { - OneMany::One(t) => Ok(vec![t]), - OneMany::Vec(v) => Ok(v), - } -} - -fn de_one_many_none<'de, T, D>(deserializer: D) -> Result>, D::Error> -where - D: Deserializer<'de>, - T: serde::Deserialize<'de>, -{ - #[derive(Deserialize)] - #[serde(untagged)] - enum OneManyNone { - None, - One(T), - Vec(Vec), - } - match OneManyNone::::deserialize(deserializer)? { - OneManyNone::None => Ok(None), - OneManyNone::One(t) => Ok(Some(vec![t])), - OneManyNone::Vec(v) => Ok(Some(v)), - } -} - fn de_host<'de, D>(deserializer: D) -> Result<(url::Host, u16), D::Error> where D: Deserializer<'de>,