Chleba / netscanner

Terminal Network scanner & diagnostic tool with modern TUI
MIT License
865 stars 20 forks source link

Prevent crash on launch when running without `sudo` #3

Closed kdheepak closed 9 months ago

kdheepak commented 9 months ago

I added some log::info! statements and was able to trace the crash down to a panic! instead of a thread spawned inside start_loop:

    fn start_loop(&mut self) {
        if self.loop_thread.is_none() {
            let tx = self.action_tx.clone().unwrap();
            let interface = self.active_interface.clone().unwrap();
            let t_handle = thread::spawn(move || {
                log::info!("Inside thread");
                Self::t_logic(tx, interface);
            });
            self.loop_thread = Some(t_handle);
        }
    }

Instead of panic!, I think you can use tx.send(Action::Error(msg)) over the channel, e.g.:

diff --git a/src/components/packetdump.rs b/src/components/packetdump.rs
index 2c2f3dd..451410a 100644
--- a/src/components/packetdump.rs
+++ b/src/components/packetdump.rs
@@ -90,13 +91,23 @@ impl PacketDump {
     }

     fn t_logic(tx: UnboundedSender<Action>, interface: NetworkInterface) {
         let (_, mut receiver) = match pnet::datalink::channel(&interface, Default::default()) {
             Ok(Channel::Ethernet(tx, rx)) => (tx, rx),
-            Ok(_) => panic!("Unknown channel type"),
-            Err(e) => panic!("Error happened {}", e),
+            Ok(_) => {
+                let _ = tx.send(Action::Error("Unable to create pnet datalink".into()));
+                return;
+            }
+            Err(e) => {
+                let _ = tx.send(Action::Error(format!(
+                    "Unable to create pnet datalink: {e}"
+                )));
+                return;
+            }
         };
         loop {
             let mut buf: [u8; 1600] = [0u8; 1600];
             let mut fake_ethernet_frame = MutableEthernetPacket::new(&mut buf[..]).unwrap();

             match receiver.next() {

And then you can handle that action with a popup or a notification or exit the app gracefully in ./src/app.rs.

kdheepak commented 9 months ago
image
Chleba commented 9 months ago

Yeah, I should cover somehow better those panics for a graceful exit. Thank You for Your suggestion @kdheepak. That's a nice solution. I will address that in the next version.

Chleba commented 9 months ago

Added fix here: https://github.com/Chleba/netscanner/commit/28caa78ae3a8a499bf95128cc11f3bea393e3e78