From 3165317a302fe6e6ea264eec0cf8c5c3895a6885 Mon Sep 17 00:00:00 2001 From: Jonathan Cobb Date: Sat, 19 Sep 2020 10:55:25 -0400 Subject: [PATCH] check if static route exists before trying to remove --- src/net.rs | 8 ++++++-- src/proxy.rs | 2 +- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/net.rs b/src/net.rs index 50dcad0..a0c6244 100644 --- a/src/net.rs +++ b/src/net.rs @@ -89,7 +89,7 @@ pub fn ip_gateway() -> String { gateway } -pub fn needs_static_route(ip_string: &String) -> bool { +pub fn static_route_exists(ip_string: &String) -> bool { trace!("needs_static_route: checking ip={:?}", ip_string); let platform: Platform = platform(); let output = match platform { @@ -124,7 +124,7 @@ pub fn needs_static_route(ip_string: &String) -> bool { let data = String::from_utf8(output).unwrap(); let mut parts = data.split_ascii_whitespace(); let first_part = parts.next(); - first_part.is_none() || first_part.unwrap().len() == 0 + first_part.is_some() && first_part.unwrap().len() > 0 } pub fn create_static_route(gateway: &String, ip_string: &String) -> bool { @@ -168,6 +168,10 @@ pub fn create_static_route(gateway: &String, ip_string: &String) -> bool { } pub fn remove_static_route(ip_string: &String) -> bool { + if !static_route_exists(ip_string) { + info!("remove_static_route: route does not exist for ip={}", ip_string); + return true; + } info!("remove_static_route: removing ip={}", ip_string); let platform: Platform = platform(); let output = match platform { diff --git a/src/proxy.rs b/src/proxy.rs index 219906e..e749273 100644 --- a/src/proxy.rs +++ b/src/proxy.rs @@ -147,7 +147,7 @@ async fn proxy(client: Client>>, info!("proxy: host {} resolved to: {}", host, ip_string); trace!("proxy: request is {:?}", req); - if needs_static_route(&ip_string) { + if !static_route_exists(&ip_string) { if !create_static_route(&gateway, &ip_string) { // we MUST fail here, without a valid static route, the request would go back out // through the VPN interface, creating an infinite loop