diff --git a/src/main.rs b/src/main.rs index 698bea5..5c3eb58 100644 --- a/src/main.rs +++ b/src/main.rs @@ -33,6 +33,7 @@ use bubble_flexrouter::admin::{AdminRegistration, start_admin}; use bubble_flexrouter::pass::init_password; use bubble_flexrouter::proxy::start_proxy; use bubble_flexrouter::ssh::ssh_command; +use bubble_flexrouter::net::flush_static_routes; use bubble_flexrouter::util::read_required_env_var_argument; use bubble_flexrouter::util::read_required_env_var_argument_as_file; use bubble_flexrouter::util::read_path_to_string; @@ -209,6 +210,8 @@ async fn main() { let admin_reg: Arc>> = Arc::new(Mutex::new(None)); + flush_static_routes(); // start fresh + let admin = start_admin( admin_reg.clone(), admin_port, diff --git a/src/net.rs b/src/net.rs index 864e71e..9ffeca6 100644 --- a/src/net.rs +++ b/src/net.rs @@ -5,6 +5,7 @@ */ use std::process::{exit, Command, Stdio}; +use std::sync::Arc; use log::{trace, info, error}; @@ -199,3 +200,63 @@ pub fn remove_static_route(ip_string: &String) -> bool { } ok } + +// rust complains about "unused mut" in "mut parts = data.split_ascii_whitespace()" +// but removing the "mut" causes a compilation failure +#[allow(unused_mut)] +pub fn flush_static_routes() -> bool { + info!("flush_static_routes: flushing static routes..."); + let gateway = ip_gateway(); + let platform: Platform = platform(); + let output = match platform { + Platform::Windows => { + Command::new("C:\\Windows\\System32\\cmd.exe") + .stdin(Stdio::null()) + .arg("/c") + .arg("route").arg("print") + .arg("|") + .arg("findstr").arg("/L").arg(format!("/C:\" {} \"", gateway)) + .output().unwrap() + } + Platform::MacOS => { + Command::new("/bin/sh") + .stdin(Stdio::null()) + .arg("-c") + .arg(format!("netstat -rn | grep UGHS | grep {}", gateway)) + .output().unwrap() + } Platform::Linux => { + Command::new("/bin/sh") + .stdin(Stdio::null()) + .arg("-c") + .arg(format!("sudo ip route show | egrep \"[[:digit:]]{{1,3}}\\.[[:digit:]]{{1,3}}\\.[[:digit:]]{{1,3}}\\.[[:digit:]]{{1,3}} via {}\"", gateway)) + .output().unwrap() + } + _ => { + error!("flush_static_routes: unsupported platform: {:?}", platform); + exit(2); + } + }; + if !output.stderr.is_empty() { + error!("flush_static_routes: error flushing: {:?}", String::from_utf8(output.stderr)); + return false; + } + let data = String::from_utf8(output.stdout).unwrap(); + let mut lines = data.lines(); + let mut all_ok = true; + for line in lines { + let mut parts = line.split_ascii_whitespace(); + let first_part = parts.next().unwrap(); + let ip_string = Arc::new(String::from(first_part)); + trace!("flush_static_routes: flushing route: {:?}", ip_string); + if !remove_static_route(&ip_string) { + error!("flush_static_routes: error flushing route: {:?}", ip_string); + all_ok = false; + } + } + if all_ok { + trace!("flush_static_routes: all static routes flushed"); + } else { + error!("flush_static_routes: error flushing static routes"); + } + return all_ok; +}