-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #249 from jamesmcm/port_forwarding_refactoring
Refactor shared code in port forwarding into traits
- Loading branch information
Showing
6 changed files
with
132 additions
and
92 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
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
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,73 @@ | ||
use std::sync::mpsc::Receiver; | ||
|
||
use super::netns::NetworkNamespace; | ||
|
||
pub mod natpmpc; | ||
pub mod piapf; | ||
|
||
pub trait Forwarder { | ||
fn forwarded_port(&self) -> u16; | ||
} | ||
|
||
/// ThreadParams must implement these methods | ||
pub trait ThreadParameters { | ||
fn get_callback_command(&self) -> Option<String>; | ||
fn get_loop_delay(&self) -> u64; | ||
fn get_netns_name(&self) -> String; | ||
} | ||
|
||
pub trait ThreadLoopForwarder: Forwarder { | ||
/// Implementation defines parameter struct passed to loop on thread | ||
type ThreadParams: ThreadParameters; | ||
|
||
/// Implementation defines how to refresh port | ||
fn refresh_port(params: &Self::ThreadParams) -> anyhow::Result<u16>; | ||
|
||
/// Provided common implementation for thread loop | ||
fn thread_loop(params: Self::ThreadParams, recv: Receiver<bool>) { | ||
loop { | ||
let resp = recv.recv_timeout(std::time::Duration::from_secs(params.get_loop_delay())); | ||
if resp.is_ok() { | ||
log::debug!("Thread exiting..."); | ||
return; | ||
} else { | ||
let port = Self::refresh_port(¶ms); | ||
match port { | ||
Err(e) => { | ||
log::error!("Thread failed to refresh port: {e:?}"); | ||
return; | ||
} | ||
Ok(p) => { | ||
log::debug!("Thread refreshed port: {p}"); | ||
Self::callback_command(¶ms, p); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
fn callback_command(params: &Self::ThreadParams, port: u16) -> Option<anyhow::Result<String>> { | ||
params.get_callback_command().map(|callback_command| | ||
{ | ||
let refresh_response = NetworkNamespace::exec_with_output( | ||
¶ms.get_netns_name(), | ||
&[&callback_command, &port.to_string()], | ||
)?; | ||
if !refresh_response.status.success() { | ||
log::error!( | ||
"Port forwarding callback script was unsuccessful!: stdout: {:?}, stderr: {:?}, exit code: {}", | ||
String::from_utf8(refresh_response.stdout), | ||
String::from_utf8(refresh_response.stderr), | ||
refresh_response.status | ||
); | ||
Err(anyhow::anyhow!("Port forwarding callback script failed")) | ||
} else if let Ok(out) = String::from_utf8(refresh_response.stdout) { | ||
println!("{}", out); | ||
Ok(out) | ||
} else { | ||
Ok("Callback script succeeded but stdout was not valid UTF8".to_string()) | ||
} | ||
} | ||
) | ||
} | ||
} |
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