-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Prometheus metrics and tokens for routes
Added first basic prometheus metrics. Now generates a token for newly created routes that can later be used for removing specific routes or otherwise keeping track of them better
- Loading branch information
Lol3rrr
committed
Mar 13, 2024
1 parent
a9a5663
commit a071b46
Showing
6 changed files
with
427 additions
and
136 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
use std::fmt::Debug; | ||
|
||
use crate::ForwardingRoute; | ||
|
||
pub trait Command { | ||
fn execute(self) -> impl core::future::Future<Output = Result<(), ()>>; | ||
} | ||
|
||
pub trait Backend { | ||
type Cmd: Debug + Command; | ||
|
||
fn register_cmds(&self, route: &ForwardingRoute) -> Result<Vec<Self::Cmd>, ()>; | ||
|
||
fn deregister_cmds(&self, route: &ForwardingRoute) -> Result<Vec<Self::Cmd>, ()>; | ||
} | ||
|
||
impl Command for tokio::process::Command { | ||
async fn execute(mut self) -> Result<(), ()> { | ||
self.output().await.map(|o| ()).map_err(|e| ()) | ||
} | ||
} | ||
|
||
pub mod iptables { | ||
use std::borrow::Cow; | ||
|
||
use crate::ForwardingRoute; | ||
|
||
use super::Backend; | ||
|
||
#[derive(Debug)] | ||
pub struct IPTablesBackend {} | ||
|
||
impl IPTablesBackend { | ||
pub fn new() -> Self { | ||
Self {} | ||
} | ||
|
||
fn register_args( | ||
&self, | ||
route: &ForwardingRoute, | ||
) -> impl Iterator<Item = Vec<Cow<'static, str>>> { | ||
[ | ||
vec![ | ||
"-I".into(), | ||
"FORWARD".into(), | ||
"-d".into(), | ||
format!("{}", route.dest_ip).into(), | ||
"-m".into(), | ||
"comment".into(), | ||
"--comment".into(), | ||
"[iptables-proxy] SD - Accept to forward traffic".into(), | ||
"-m".into(), | ||
route.protocol.as_str().into(), | ||
"-p".into(), | ||
route.protocol.as_str().into(), | ||
"--dport".into(), | ||
format!("{}", route.pub_port).into(), | ||
"-j".into(), | ||
"ACCEPT".into(), | ||
], | ||
vec![ | ||
"-I".into(), | ||
"FORWARD".into(), | ||
"-m".into(), | ||
"comment".into(), | ||
"--comment".into(), | ||
"[iptables-proxy] DS - Accept to forward return traffic".into(), | ||
"-s".into(), | ||
format!("{}", route.dest_ip).into(), | ||
"-m".into(), | ||
route.protocol.as_str().into(), | ||
"-p".into(), | ||
route.protocol.as_str().into(), | ||
"--sport".into(), | ||
format!("{}", route.dest_port).into(), | ||
"-j".into(), | ||
"ACCEPT".into(), | ||
], | ||
vec![ | ||
"-t".into(), | ||
"nat".into(), | ||
"-I".into(), | ||
"PREROUTING".into(), | ||
"-m".into(), | ||
route.protocol.as_str().into(), | ||
"-p".into(), | ||
route.protocol.as_str().into(), | ||
"--dport".into(), | ||
format!("{}", route.pub_port).into(), | ||
"-m".into(), | ||
"comment".into(), | ||
"--comment".into(), | ||
"[iptables-proxy] redirect pkts to homeserver".into(), | ||
"-j".into(), | ||
"DNAT".into(), | ||
"--to-destination".into(), | ||
format!("{}:{}", route.dest_ip, route.dest_port).into(), | ||
], | ||
] | ||
.into_iter() | ||
} | ||
fn deregister_args( | ||
&self, | ||
route: &ForwardingRoute, | ||
) -> impl Iterator<Item = Vec<Cow<'static, str>>> { | ||
self.register_args(route).map(|mut args| { | ||
for arg in args.iter_mut() { | ||
if arg == "-I" { | ||
*arg = "-D".into(); | ||
} | ||
} | ||
args | ||
}) | ||
} | ||
} | ||
|
||
impl Backend for IPTablesBackend { | ||
type Cmd = tokio::process::Command; | ||
|
||
fn register_cmds(&self, route: &crate::ForwardingRoute) -> Result<Vec<Self::Cmd>, ()> { | ||
let cmds = self | ||
.register_args(route) | ||
.map(|args| { | ||
let mut cmd = tokio::process::Command::new("iptables"); | ||
cmd.args(args.into_iter().map(|c| c.to_string())); | ||
cmd | ||
}) | ||
.collect(); | ||
|
||
Ok(cmds) | ||
} | ||
|
||
fn deregister_cmds(&self, route: &crate::ForwardingRoute) -> Result<Vec<Self::Cmd>, ()> { | ||
let cmds = self | ||
.deregister_args(route) | ||
.map(|args| { | ||
let mut cmd = tokio::process::Command::new("iptables"); | ||
cmd.args(args.into_iter().map(|c| c.to_string())); | ||
cmd | ||
}) | ||
.collect(); | ||
Ok(cmds) | ||
} | ||
} | ||
} |
Oops, something went wrong.