From dad81d124b73798c767c88d6c9b652a0f9f7c0f4 Mon Sep 17 00:00:00 2001 From: Aram Drevekenin Date: Fri, 3 Jan 2020 23:02:33 +0100 Subject: [PATCH 1/4] fix(error): error when 0 interfaces to listen to are found --- src/os/shared.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/os/shared.rs b/src/os/shared.rs index 8fd130026..c184a481f 100644 --- a/src/os/shared.rs +++ b/src/os/shared.rs @@ -106,6 +106,9 @@ pub fn get_input( .map(|iface| get_datalink_channel(iface)) .filter_map(Result::ok) .collect::>(); + if network_frames.len() == 0 { + failure::bail!("Could not find any network interface to listen to. Try running with sudo"); + } let keyboard_events = Box::new(KeyboardEvents); let write_to_stdout = create_write_to_stdout(); From aae1d4ad086d98988e726907c8131990ae59faf0 Mon Sep 17 00:00:00 2001 From: Aram Drevekenin Date: Fri, 3 Jan 2020 23:15:40 +0100 Subject: [PATCH 2/4] style(clippy): clippy --- src/os/shared.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/os/shared.rs b/src/os/shared.rs index c184a481f..66808bc03 100644 --- a/src/os/shared.rs +++ b/src/os/shared.rs @@ -106,7 +106,7 @@ pub fn get_input( .map(|iface| get_datalink_channel(iface)) .filter_map(Result::ok) .collect::>(); - if network_frames.len() == 0 { + if network_frames.is_empty() { failure::bail!("Could not find any network interface to listen to. Try running with sudo"); } From a9420d219acad40063d7a1c1c1756d217d69c509 Mon Sep 17 00:00:00 2001 From: Aram Drevekenin Date: Sat, 4 Jan 2020 14:32:29 +0100 Subject: [PATCH 3/4] fix(error): differentiate between privileges errors and other errors --- src/os/shared.rs | 41 +++++++++++++++++++++++++---------------- 1 file changed, 25 insertions(+), 16 deletions(-) diff --git a/src/os/shared.rs b/src/os/shared.rs index 66808bc03..88b330f5a 100644 --- a/src/os/shared.rs +++ b/src/os/shared.rs @@ -5,6 +5,7 @@ use ::std::io::{self, stdin, Write}; use ::termion::event::Event; use ::termion::input::TermRead; +use ::std::io::ErrorKind; use ::std::time; use signal_hook::iterator::Signals; @@ -32,23 +33,17 @@ impl Iterator for KeyboardEvents { fn get_datalink_channel( interface: &NetworkInterface, -) -> Result, failure::Error> { +) -> Result, std::io::Error> { let mut config = Config::default(); config.read_timeout = Some(time::Duration::new(1, 0)); match datalink::channel(interface, config) { Ok(Ethernet(_tx, rx)) => Ok(rx), - Ok(_) => failure::bail!("Unknown interface type"), - Err(e) => { - match e.kind() { - std::io::ErrorKind::PermissionDenied => failure::bail!("Failed to listen on network interface due to permission error. Try running with sudo"), - _ => failure::bail!( - "Failed to listen on network interface {}: {}", - interface.name, - e - ), - } - } + Ok(_) => Err(std::io::Error::new( + ErrorKind::Other, + "Unsupported interface type", + )), + Err(e) => Err(e), } } @@ -103,11 +98,25 @@ pub fn get_input( let network_frames = network_interfaces .iter() - .map(|iface| get_datalink_channel(iface)) + .map(|iface| get_datalink_channel(iface)); + + let available_network_frames = network_frames + .clone() .filter_map(Result::ok) .collect::>(); - if network_frames.is_empty() { - failure::bail!("Could not find any network interface to listen to. Try running with sudo"); + + if available_network_frames.is_empty() { + for iface in network_frames { + if let Some(iface_error) = iface.err() { + match iface_error.kind() { + ErrorKind::PermissionDenied => failure::bail!( + "Insufficient permissions to listen on network interface(s). Try running with sudo.", + ), + _ => () + } + } + } + failure::bail!("Failed to find any network interface to listen on."); } let keyboard_events = Box::new(KeyboardEvents); @@ -123,7 +132,7 @@ pub fn get_input( Ok(OsInputOutput { network_interfaces, - network_frames, + network_frames: available_network_frames, get_open_sockets, keyboard_events, dns_client, From 97ed18cbfa447e1bd5a4cd3d6793b07c18e5372b Mon Sep 17 00:00:00 2001 From: Aram Drevekenin Date: Sat, 4 Jan 2020 14:39:00 +0100 Subject: [PATCH 4/4] style(clippy): clippy --- src/os/shared.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/os/shared.rs b/src/os/shared.rs index 88b330f5a..1f71c61da 100644 --- a/src/os/shared.rs +++ b/src/os/shared.rs @@ -108,11 +108,10 @@ pub fn get_input( if available_network_frames.is_empty() { for iface in network_frames { if let Some(iface_error) = iface.err() { - match iface_error.kind() { - ErrorKind::PermissionDenied => failure::bail!( + if let ErrorKind::PermissionDenied = iface_error.kind() { + failure::bail!( "Insufficient permissions to listen on network interface(s). Try running with sudo.", - ), - _ => () + ) } } }