diff options
author | cyqsimon <28627918+cyqsimon@users.noreply.github.com> | 2023-08-25 14:54:24 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-08-25 14:54:24 +0800 |
commit | 94df6e07db856cfffcc2e1dcf13942acdb2fcbd9 (patch) | |
tree | 764bd5f8988991983579e69b0b996b16840e4fc7 /src | |
parent | 45503a01a687208cdc61be3fda25b1603d008653 (diff) | |
parent | 04e3a2aac6c717fb1844a9f2bb9588c9e91c50ac (diff) |
Merge dependency bumps and fixes
Tests still have to be fixed.
Diffstat (limited to 'src')
-rw-r--r-- | src/display/components/table.rs | 12 | ||||
-rw-r--r-- | src/display/ui.rs | 6 | ||||
-rw-r--r-- | src/display/ui_state.rs | 2 | ||||
-rw-r--r-- | src/main.rs | 6 | ||||
-rw-r--r-- | src/network/dns/client.rs | 2 | ||||
-rw-r--r-- | src/network/dns/resolver.rs | 12 | ||||
-rw-r--r-- | src/os/linux.rs | 14 | ||||
-rw-r--r-- | src/os/lsof.rs | 9 | ||||
-rw-r--r-- | src/os/lsof_utils.rs | 8 | ||||
-rw-r--r-- | src/os/mod.rs | 6 | ||||
-rw-r--r-- | src/os/shared.rs | 23 | ||||
-rw-r--r-- | src/os/windows.rs | 7 | ||||
-rw-r--r-- | src/tests/cases/raw_mode.rs | 4 | ||||
-rw-r--r-- | src/tests/cases/test_utils.rs | 17 | ||||
-rw-r--r-- | src/tests/cases/ui.rs | 40 | ||||
-rw-r--r-- | src/tests/fakes/fake_input.rs | 1 |
16 files changed, 78 insertions, 91 deletions
diff --git a/src/display/components/table.rs b/src/display/components/table.rs index 65de8be..d161451 100644 --- a/src/display/components/table.rs +++ b/src/display/components/table.rs @@ -96,8 +96,8 @@ impl<'a> Table<'a> { .map(|(connection, connection_data)| { vec![ display_connection_string( - &connection, - &ip_to_host, + connection, + ip_to_host, &connection_data.interface_name, ), connection_data.process_name.to_string(), @@ -201,7 +201,7 @@ impl<'a> Table<'a> { .remote_addresses .iter() .map(|(remote_address, data_for_remote_address)| { - let remote_address = display_ip_or_host(*remote_address, &ip_to_host); + let remote_address = display_ip_or_host(*remote_address, ip_to_host); vec![ remote_address, data_for_remote_address.connection_count.to_string(), @@ -289,12 +289,12 @@ impl<'a> Table<'a> { ], }); - let table_rows = rows.map(|row| Row::StyledData(row.into_iter(), Style::default())); + let table_rows = rows.map(|row| Row::new(row).style(Style::default())); let width_constraints: Vec<Constraint> = widths.iter().map(|w| Constraint::Length(*w)).collect(); - let table = ::tui::widgets::Table::new(column_names.into_iter(), table_rows) + let table = ::tui::widgets::Table::new(table_rows) .block(Block::default().title(self.title).borders(Borders::ALL)) - .header_style(Style::default().fg(Color::Yellow)) + .header(Row::new(column_names).style(Style::default().fg(Color::Yellow))) .widths(&width_constraints) .style(Style::default()) .column_spacing(column_spacing); diff --git a/src/display/ui.rs b/src/display/ui.rs index 24de083..9a1bf80 100644 --- a/src/display/ui.rs +++ b/src/display/ui.rs @@ -130,10 +130,10 @@ where let state = &self.state; let children = self.get_tables_to_display(); self.terminal - .draw(|mut frame| { + .draw(|frame| { let size = frame.size(); let total_bandwidth = HeaderDetails { - state: &state, + state, elapsed_time, paused, }; @@ -143,7 +143,7 @@ where children, footer: help_text, }; - layout.render(&mut frame, size, ui_offset); + layout.render(frame, size, ui_offset); }) .unwrap(); } diff --git a/src/display/ui_state.rs b/src/display/ui_state.rs index 8601a5c..c992808 100644 --- a/src/display/ui_state.rs +++ b/src/display/ui_state.rs @@ -149,7 +149,7 @@ impl UIState { total_bytes_uploaded += connection_info.total_bytes_uploaded; let data_for_process = if let Some(process_name) = - UIState::get_proc_name(&connections_to_procs, &connection.local_socket) + UIState::get_proc_name(connections_to_procs, &connection.local_socket) { connection_data.process_name = process_name.clone(); processes.entry(process_name.clone()).or_default() diff --git a/src/main.rs b/src/main.rs index 39319f7..445e4fb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -226,10 +226,12 @@ where Event::Key(KeyEvent { modifiers: KeyModifiers::CONTROL, code: KeyCode::Char('c'), + .. }) | Event::Key(KeyEvent { modifiers: KeyModifiers::NONE, code: KeyCode::Char('q'), + .. }) => { running.store(false, Ordering::Release); display_handler.unpark(); @@ -242,6 +244,7 @@ where Event::Key(KeyEvent { modifiers: KeyModifiers::NONE, code: KeyCode::Char(' '), + .. }) => { let restarting = paused.fetch_xor(true, Ordering::SeqCst); if restarting { @@ -260,6 +263,7 @@ where Event::Key(KeyEvent { modifiers: KeyModifiers::NONE, code: KeyCode::Tab, + .. }) => { let paused = paused.load(Ordering::SeqCst); let elapsed_time = elapsed_time( @@ -284,7 +288,7 @@ where let sniffer_threads = os_input .network_interfaces .into_iter() - .zip(os_input.network_frames.into_iter()) + .zip(os_input.network_frames) .map(|(iface, frames)| { let name = format!("sniffing_handler_{}", iface.name); let running = running.clone(); diff --git a/src/network/dns/client.rs b/src/network/dns/client.rs index c3ed96c..ef32180 100644 --- a/src/network/dns/client.rs +++ b/src/network/dns/client.rs @@ -22,7 +22,7 @@ pub struct Client { } impl Client { - pub fn new<R>(resolver: R, mut runtime: Runtime) -> Result<Self, failure::Error> + pub fn new<R>(resolver: R, runtime: Runtime) -> Result<Self, failure::Error> where R: Lookup + Send + Sync + 'static, { diff --git a/src/network/dns/resolver.rs b/src/network/dns/resolver.rs index 1bdaf78..7327654 100644 --- a/src/network/dns/resolver.rs +++ b/src/network/dns/resolver.rs @@ -2,7 +2,6 @@ use async_trait::async_trait; use std::net::IpAddr; use std::net::SocketAddr; use std::net::{Ipv4Addr, SocketAddrV4}; -use tokio::runtime::Handle; use trust_dns_resolver::config::{NameServerConfig, Protocol, ResolverConfig, ResolverOpts}; use trust_dns_resolver::{error::ResolveErrorKind, TokioAsyncResolver}; @@ -14,10 +13,7 @@ pub trait Lookup { pub struct Resolver(TokioAsyncResolver); impl Resolver { - pub async fn new( - runtime: Handle, - dns_server: &Option<Ipv4Addr>, - ) -> Result<Self, failure::Error> { + pub async fn new(dns_server: &Option<Ipv4Addr>) -> Result<Self, failure::Error> { let resolver = match dns_server { Some(dns_server_address) => { let mut config = ResolverConfig::new(); @@ -27,11 +23,13 @@ impl Resolver { socket_addr: socket, protocol: Protocol::Udp, tls_dns_name: None, + trust_negative_responses: false, + bind_addr: None, }; config.add_name_server(nameserver_config); - TokioAsyncResolver::new(config, options, runtime).await? + TokioAsyncResolver::tokio(config, options) } - None => TokioAsyncResolver::from_system_conf(runtime).await?, + None => TokioAsyncResolver::tokio_from_system_conf()?, }; Ok(Self(resolver)) } diff --git a/src/os/linux.rs b/src/os/linux.rs index a28c5ac..5dd5c2b 100644 --- a/src/os/linux.rs +++ b/src/os/linux.rs @@ -10,13 +10,13 @@ pub(crate) fn get_open_sockets() -> OpenSockets { let mut inode_to_procname = HashMap::new(); if let Ok(all_procs) = procfs::process::all_processes() { - for process in all_procs { - if let Ok(fds) = process.fd() { - let procname = process.stat.comm; - for fd in fds { - if let FDTarget::Socket(inode) = fd.target { - inode_to_procname.insert(inode, procname.clone()); - } + for process in all_procs.filter_map(|res| res.ok()) { + let Ok(fds) = process.fd() else { continue }; + let Ok(stat) = process.stat() else { continue }; + let procname = stat.comm; + for fd in fds.filter_map(|res| res.ok()) { + if let FDTarget::Socket(inode) = fd.target { + inode_to_procname.insert(inode, procname.clone()); } } } diff --git a/src/os/lsof.rs b/src/os/lsof.rs index bb17948..77331ed 100644 --- a/src/os/lsof.rs +++ b/src/os/lsof.rs @@ -5,15 +5,6 @@ use crate::OpenSockets; use super::lsof_utils; -#[derive(Debug)] -struct RawConnection { - ip: String, - local_port: String, - remote_port: String, - protocol: String, - process_name: String, -} - pub(crate) fn get_open_sockets() -> OpenSockets { let mut open_sockets = HashMap::new(); diff --git a/src/os/lsof_utils.rs b/src/os/lsof_utils.rs index 3d715b8..46fe8fc 100644 --- a/src/os/lsof_utils.rs +++ b/src/os/lsof_utils.rs @@ -5,6 +5,7 @@ use std::ffi::OsStr; use std::net::IpAddr; use std::process::Command; +#[allow(dead_code)] #[derive(Debug, Clone)] pub struct RawConnection { remote_ip: String, @@ -116,7 +117,7 @@ impl RawConnection { } pub fn get_connections() -> RawConnections { - let content = run(&["-n", "-P", "-i4", "-i6", "+c", "0"]); + let content = run(["-n", "-P", "-i4", "-i6", "+c", "0"]); RawConnections::new(content) } @@ -139,10 +140,7 @@ pub struct RawConnections { impl RawConnections { pub fn new(content: String) -> RawConnections { - let lines: Vec<RawConnection> = content - .lines() - .flat_map(|string| RawConnection::new(string)) - .collect(); + let lines: Vec<RawConnection> = content.lines().flat_map(RawConnection::new).collect(); RawConnections { content: lines } } diff --git a/src/os/mod.rs b/src/os/mod.rs index 4db5b0a..7770de9 100644 --- a/src/os/mod.rs +++ b/src/os/mod.rs @@ -1,14 +1,14 @@ #[cfg(target_os = "linux")] -pub(self) mod linux; +mod linux; #[cfg(any(target_os = "macos", target_os = "freebsd"))] -pub(self) mod lsof; +mod lsof; #[cfg(any(target_os = "macos", target_os = "freebsd"))] mod lsof_utils; #[cfg(target_os = "windows")] -pub(self) mod windows; +mod windows; mod errors; pub(crate) mod shared; diff --git a/src/os/shared.rs b/src/os/shared.rs index ae42022..f28e49e 100644 --- a/src/os/shared.rs +++ b/src/os/shared.rs @@ -153,7 +153,7 @@ pub fn get_input( dns_server: &Option<Ipv4Addr>, ) -> Result<OsInputOutput, failure::Error> { let network_interfaces = if let Some(name) = interface_name { - match get_interface(&name) { + match get_interface(name) { Some(interface) => vec![interface], None => { failure::bail!("Cannot find interface {}", name); @@ -164,7 +164,7 @@ pub fn get_input( datalink::interfaces() }; - #[cfg(any(target_os = "windows"))] + #[cfg(target_os = "windows")] let network_frames = network_interfaces .iter() .filter(|iface| !iface.ips.is_empty()) @@ -204,15 +204,14 @@ pub fn get_input( let keyboard_events = Box::new(TerminalEvents); let write_to_stdout = create_write_to_stdout(); let dns_client = if resolve { - let mut runtime = Runtime::new()?; - let resolver = - match runtime.block_on(dns::Resolver::new(runtime.handle().clone(), dns_server)) { - Ok(resolver) => resolver, - Err(err) => failure::bail!( - "Could not initialize the DNS resolver. Are you offline?\n\nReason: {:?}", - err - ), - }; + let runtime = Runtime::new()?; + let resolver = match runtime.block_on(dns::Resolver::new(dns_server)) { + Ok(resolver) => resolver, + Err(err) => failure::bail!( + "Could not initialize the DNS resolver. Are you offline?\n\nReason: {:?}", + err + ), + }; let dns_client = dns::Client::new(resolver, runtime)?; Some(dns_client) } else { @@ -250,7 +249,7 @@ fn eperm_message() -> &'static str { } #[inline] -#[cfg(any(target_os = "windows"))] +#[cfg(target_os = "windows")] fn eperm_message() -> &'static str { "Insufficient permissions to listen on network interface(s). Try running with administrator rights." } diff --git a/src/os/windows.rs b/src/os/windows.rs index 69adfde..5bb6118 100644 --- a/src/os/windows.rs +++ b/src/os/windows.rs @@ -1,11 +1,10 @@ -use ::std::collections::HashMap; +use std::collections::HashMap; -use ::sysinfo::ProcessExt; use netstat2::*; +use sysinfo::{Pid, PidExt, ProcessExt, System, SystemExt}; use crate::network::{LocalSocket, Protocol}; use crate::OpenSockets; -use sysinfo::{Pid, System, SystemExt}; pub(crate) fn get_open_sockets() -> OpenSockets { let mut open_sockets = HashMap::new(); @@ -21,7 +20,7 @@ pub(crate) fn get_open_sockets() -> OpenSockets { for si in sockets_info { let mut procname = String::new(); for pid in si.associated_pids { - if let Some(process) = sysinfo.get_process(pid as Pid) { + if let Some(process) = sysinfo.process(Pid::from_u32(pid)) { procname = String::from(process.name()); break; } diff --git a/src/tests/cases/raw_mode.rs b/src/tests/cases/raw_mode.rs index bee763d..6da54ab 100644 --- a/src/tests/cases/raw_mode.rs +++ b/src/tests/cases/raw_mode.rs @@ -6,10 +6,8 @@ use ::std::sync::{Arc, Mutex}; use ::std::collections::HashMap; use ::std::net::IpAddr; -use packet_builder::payload::PayloadData; use packet_builder::*; -use pnet::datalink::DataLinkReceiver; -use pnet::packet::Packet; +use pnet::{datalink::DataLinkReceiver, packet::Packet}; use crate::tests::cases::test_utils::{ build_tcp_packet, opts_raw, os_input_output_dns, os_input_output_stdout, test_backend_factory, diff --git a/src/tests/cases/test_utils.rs b/src/tests/cases/test_utils.rs index ab8e502..1137157 100644 --- a/src/tests/cases/test_utils.rs +++ b/src/tests/cases/test_utils.rs @@ -13,26 +13,25 @@ use std::collections::HashMap; use std::io::Write; use std::sync::{Arc, Mutex}; -use packet_builder::payload::PayloadData; use pnet::packet::Packet; use pnet_base::MacAddr; pub fn sleep_and_quit_events(sleep_num: usize) -> Box<TerminalEvents> { let mut events: Vec<Option<Event>> = iter::repeat(None).take(sleep_num).collect(); - events.push(Some(Event::Key(KeyEvent { - modifiers: KeyModifiers::CONTROL, - code: KeyCode::Char('c'), - }))); + events.push(Some(Event::Key(KeyEvent::new( + KeyCode::Char('c'), + KeyModifiers::CONTROL, + )))); Box::new(TerminalEvents::new(events)) } pub fn sleep_resize_and_quit_events(sleep_num: usize) -> Box<TerminalEvents> { let mut events: Vec<Option<Event>> = iter::repeat(None).take(sleep_num).collect(); events.push(Some(Event::Resize(100, 100))); - events.push(Some(Event::Key(KeyEvent { - modifiers: KeyModifiers::CONTROL, - code: KeyCode::Char('c'), - }))); + events.push(Some(Event::Key(KeyEvent::new( + KeyCode::Char('c'), + KeyModifiers::CONTROL, + )))); Box::new(TerminalEvents::new(events)) } diff --git a/src/tests/cases/ui.rs b/src/tests/cases/ui.rs index 14bf861..ce12319 100644 --- a/src/tests/cases/ui.rs +++ b/src/tests/cases/ui.rs @@ -68,20 +68,20 @@ fn pause_by_space() { // sleep for 1s, then press space, sleep for 2s, then quit let mut events: Vec<Option<Event>> = iter::repeat(None).take(1).collect(); - events.push(Some(Event::Key(KeyEvent { - modifiers: KeyModifiers::NONE, - code: KeyCode::Char(' '), - }))); + events.push(Some(Event::Key(KeyEvent::new( + KeyCode::Char(' '), + KeyModifiers::NONE, + )))); events.push(None); events.push(None); - events.push(Some(Event::Key(KeyEvent { - modifiers: KeyModifiers::NONE, - code: KeyCode::Char(' '), - }))); - events.push(Some(Event::Key(KeyEvent { - modifiers: KeyModifiers::CONTROL, - code: KeyCode::Char('c'), - }))); + events.push(Some(Event::Key(KeyEvent::new( + KeyCode::Char(' '), + KeyModifiers::NONE, + )))); + events.push(Some(Event::Key(KeyEvent::new( + KeyCode::Char('c'), + KeyModifiers::CONTROL, + )))); let events = Box::new(TerminalEvents::new(events)); let os_input = os_input_output_factory(network_frames, None, None, events); @@ -128,16 +128,16 @@ fn rearranged_by_tab() { // sleep for 1s, then press tab, sleep for 2s, then quit let mut events: Vec<Option<Event>> = iter::repeat(None).take(1).collect(); events.push(None); - events.push(Some(Event::Key(KeyEvent { - modifiers: KeyModifiers::NONE, - code: KeyCode::Tab, - }))); + events.push(Some(Event::Key(KeyEvent::new( + KeyCode::Tab, + KeyModifiers::NONE, + )))); events.push(None); events.push(None); - events.push(Some(Event::Key(KeyEvent { - modifiers: KeyModifiers::CONTROL, - code: KeyCode::Char('c'), - }))); + events.push(Some(Event::Key(KeyEvent::new( + KeyCode::Char('c'), + KeyModifiers::CONTROL, + )))); let events = Box::new(TerminalEvents::new(events)); let os_input = os_input_output_factory(network_frames, None, None, events); diff --git a/src/tests/fakes/fake_input.rs b/src/tests/fakes/fake_input.rs index 0fdf63a..462ceb6 100644 --- a/src/tests/fakes/fake_input.rs +++ b/src/tests/fakes/fake_input.rs @@ -146,6 +146,7 @@ pub fn get_open_sockets() -> OpenSockets { pub fn get_interfaces() -> Vec<NetworkInterface> { vec![NetworkInterface { name: String::from("interface_name"), + description: String::from("Fake interface"), index: 42, mac: None, ips: vec![IpNetwork::V4("10.0.0.2".parse().unwrap())], |