Remove dead code.
This commit is contained in:
parent
1a4add2caa
commit
21b55e483d
4 changed files with 2 additions and 197 deletions
|
@ -1,12 +1,9 @@
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
use std::sync::Arc;
|
|
||||||
|
|
||||||
use cidr::IpInet;
|
use cidr::IpInet;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use tokio::sync::Mutex;
|
|
||||||
|
|
||||||
type WgLink = String;
|
type WgLink = String;
|
||||||
type WgPeer = String;
|
|
||||||
|
|
||||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||||
pub struct EventInfo {
|
pub struct EventInfo {
|
||||||
|
@ -55,5 +52,3 @@ impl Event {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type EventStore = Arc<Mutex<Vec<Event>>>;
|
|
||||||
|
|
|
@ -1,7 +1,5 @@
|
||||||
use std::process::Output;
|
use std::process::Output;
|
||||||
use tokio::process::Command;
|
use tokio::process::Command;
|
||||||
use warp::{Filter, Rejection};
|
|
||||||
use std::str::FromStr;
|
|
||||||
|
|
||||||
pub async fn command_output(command: &mut Command) -> std::io::Result<Output> {
|
pub async fn command_output(command: &mut Command) -> std::io::Result<Output> {
|
||||||
log::debug!("Shell command {:?}", command);
|
log::debug!("Shell command {:?}", command);
|
||||||
|
@ -9,24 +7,3 @@ pub async fn command_output(command: &mut Command) -> std::io::Result<Output> {
|
||||||
log::debug!("Output: {:?}", output);
|
log::debug!("Output: {:?}", output);
|
||||||
output
|
output
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn param_handler<T: FromStr>(s: String) -> Result<T, Rejection>
|
|
||||||
where
|
|
||||||
<T as FromStr>::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<T: FromStr + Send + 'static>() -> impl Filter<Extract = (T,), Error = Rejection> + Copy
|
|
||||||
where
|
|
||||||
<T as FromStr>::Err: std::fmt::Debug,
|
|
||||||
{
|
|
||||||
warp::path::param::<String>().and_then(param_handler)
|
|
||||||
}
|
|
||||||
|
|
135
src/main.rs
135
src/main.rs
|
@ -30,35 +30,8 @@ struct RejectBadRequest;
|
||||||
impl warp::reject::Reject for RejectBadRequest {}
|
impl warp::reject::Reject for RejectBadRequest {}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
struct CommandError {
|
struct CommandError {}
|
||||||
stdout: String,
|
|
||||||
stderr: String,
|
|
||||||
}
|
|
||||||
impl warp::reject::Reject for CommandError {}
|
impl warp::reject::Reject for CommandError {}
|
||||||
impl From<std::process::Output> 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<cidr::IpInet>) {
|
|
||||||
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<cidr::IpInet>) {
|
|
||||||
text.split('\n')
|
|
||||||
.map(|p| split_allowed_ips_text_line(p))
|
|
||||||
.filter(|l| l.0 == peer)
|
|
||||||
.next()
|
|
||||||
.unwrap()
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn wg_modify_address(
|
async fn wg_modify_address(
|
||||||
config: wg::Wg,
|
config: wg::Wg,
|
||||||
|
@ -114,111 +87,7 @@ async fn wg_modify_address(
|
||||||
if output.status.success() {
|
if output.status.success() {
|
||||||
Ok("")
|
Ok("")
|
||||||
} else {
|
} else {
|
||||||
Err(warp::reject::custom(CommandError::from(output)))
|
Err(warp::reject::custom(CommandError {}))
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn wg_add_address(
|
|
||||||
link: WgLink,
|
|
||||||
peer: WgPeer,
|
|
||||||
cidr: cidr::IpInet,
|
|
||||||
) -> Result<impl warp::Reply, warp::Rejection> {
|
|
||||||
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<impl warp::Reply, warp::Rejection> {
|
|
||||||
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)))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
36
src/wg.rs
36
src/wg.rs
|
@ -7,42 +7,6 @@ use serde_with::{serde_as};
|
||||||
|
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
|
|
||||||
fn de_one_many<'de, T, D>(deserializer: D) -> Result<Vec<T>, D::Error>
|
|
||||||
where
|
|
||||||
D: Deserializer<'de>,
|
|
||||||
T: serde::Deserialize<'de>,
|
|
||||||
{
|
|
||||||
#[derive(Deserialize)]
|
|
||||||
#[serde(untagged)]
|
|
||||||
enum OneMany<T> {
|
|
||||||
One(T),
|
|
||||||
Vec(Vec<T>),
|
|
||||||
}
|
|
||||||
match OneMany::<T>::deserialize(deserializer)? {
|
|
||||||
OneMany::One(t) => Ok(vec![t]),
|
|
||||||
OneMany::Vec(v) => Ok(v),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn de_one_many_none<'de, T, D>(deserializer: D) -> Result<Option<Vec<T>>, D::Error>
|
|
||||||
where
|
|
||||||
D: Deserializer<'de>,
|
|
||||||
T: serde::Deserialize<'de>,
|
|
||||||
{
|
|
||||||
#[derive(Deserialize)]
|
|
||||||
#[serde(untagged)]
|
|
||||||
enum OneManyNone<T> {
|
|
||||||
None,
|
|
||||||
One(T),
|
|
||||||
Vec(Vec<T>),
|
|
||||||
}
|
|
||||||
match OneManyNone::<T>::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<String>, u16), D::Error>
|
fn de_host<'de, D>(deserializer: D) -> Result<(url::Host<String>, u16), D::Error>
|
||||||
where
|
where
|
||||||
D: Deserializer<'de>,
|
D: Deserializer<'de>,
|
||||||
|
|
Loading…
Reference in a new issue