Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Misc dependency upgrades #265

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2,315 changes: 1,196 additions & 1,119 deletions Cargo.lock

Large diffs are not rendered by default.

42 changes: 22 additions & 20 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,38 +19,40 @@ license = "MIT"
exclude = ["src/tests/*", "demo.gif"]

[dependencies]
pnet = "0.26.0"
ipnetwork = "0.16.0"
tui = { version = "0.12", default-features = false, features = ["crossterm"]}
crossterm = "0.17.7"
pnet = "0.33.0"
ipnetwork = "0.20.0"
tui = "0.19"
crossterm = "0.26.1"
structopt = "0.3"
failure = "0.1.6"
chrono = "0.4"
regex = "1.3.1"
regex = "1.7.3"
lazy_static = "1.4.0"
tokio = { version = "0.2", features = ["rt-core", "sync"] }
trust-dns-resolver = "0.18.1"
async-trait = "0.1.21"
unicode-width = "0.1.8"
tokio = { version = "1.27", features = ["rt-multi-thread", "sync"] }
trust-dns-resolver = "0.22.0"
async-trait = "0.1.68"
unicode-width = "0.1.10"
resolv-conf = "0.7.0"
anyhow = "1.0.70"
thiserror = "1.0.40"

[target.'cfg(target_os="windows")'.dependencies]
netstat2 = "0.9.0"
sysinfo = "0.15.1"
netstat2 = "0.9.1"
sysinfo = "0.28.4"

[target.'cfg(target_os="linux")'.dependencies]
procfs = "0.7.4"
procfs = "0.7.9"

[dev-dependencies]
insta = "0.12.0"
pnet_base = "0.26.0"
cargo-insta = "0.11.0"
packet-builder = "0.5.0"
insta = "1.29.0"
pnet_base = "0.33.0"
cargo-insta = "1.29.0"
packet-builder = "0.7.0"
regex = "1"

[target.'cfg(target_os="windows")'.build-dependencies]
http_req = "0.7.0"
zip = "0.5.6"
http_req = "0.9.1"
zip = "0.6.4"

[patch.crates-io]
resolv-conf = { git = 'https://github.com/tailhook/resolv-conf', rev = '83c0f25ebcb0615550488692c5213ca1ae4acd8f' }
# https://github.com/hughesac/packet_builder/pull/5
packet-builder = { git = "https://github.com/jcgruenhage/packet_builder", branch = "upgrades-2023-04-10" }
6 changes: 3 additions & 3 deletions src/display/components/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
.header(Row::new(column_names).style(Style::default().fg(Color::Yellow)))
.block(Block::default().title(self.title).borders(Borders::ALL))
.header_style(Style::default().fg(Color::Yellow))
.widths(&width_constraints)
.style(Style::default())
.column_spacing(column_spacing);
Expand Down
21 changes: 10 additions & 11 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ mod os;
#[cfg(test)]
mod tests;

use anyhow::Context;
use display::{elapsed_time, RawTerminalBackend, Ui};
use network::{
dns::{self, IpTable},
Expand Down Expand Up @@ -77,7 +78,7 @@ fn main() {
}
}

fn try_main() -> Result<(), failure::Error> {
fn try_main() -> Result<(), anyhow::Error> {
use os::get_input;
let opts = Opt::from_args();
let os_input = get_input(&opts.interface, !opts.no_resolve, &opts.dns_server)?;
Expand All @@ -86,16 +87,10 @@ fn try_main() -> Result<(), failure::Error> {
let terminal_backend = RawTerminalBackend {};
start(terminal_backend, os_input, opts);
} else {
match terminal::enable_raw_mode() {
Ok(()) => {
let stdout = std::io::stdout();
let terminal_backend = CrosstermBackend::new(stdout);
start(terminal_backend, os_input, opts);
}
Err(_) => failure::bail!(
"Failed to get stdout: if you are trying to pipe 'bandwhich' you should use the --raw flag"
),
}
terminal::enable_raw_mode().context("Failed to get stdout: if you are trying to pipe 'bandwhich' you should use the --raw flag")?;
let stdout = std::io::stdout();
let terminal_backend = CrosstermBackend::new(stdout);
start(terminal_backend, os_input, opts);
}
Ok(())
}
Expand Down Expand Up @@ -226,10 +221,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();
Expand All @@ -242,6 +239,7 @@ where
Event::Key(KeyEvent {
modifiers: KeyModifiers::NONE,
code: KeyCode::Char(' '),
..
}) => {
let restarting = paused.fetch_xor(true, Ordering::SeqCst);
if restarting {
Expand All @@ -260,6 +258,7 @@ where
Event::Key(KeyEvent {
modifiers: KeyModifiers::NONE,
code: KeyCode::Tab,
..
}) => {
let paused = paused.load(Ordering::SeqCst);
let elapsed_time = elapsed_time(
Expand Down
2 changes: 1 addition & 1 deletion src/network/dns/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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, anyhow::Error>
where
R: Lookup + Send + Sync + 'static,
{
Expand Down
10 changes: 5 additions & 5 deletions src/network/dns/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand All @@ -15,9 +14,8 @@ pub struct Resolver(TokioAsyncResolver);

impl Resolver {
pub async fn new(
runtime: Handle,
dns_server: &Option<Ipv4Addr>,
) -> Result<Self, failure::Error> {
) -> Result<Self, anyhow::Error> {
let resolver = match dns_server {
Some(dns_server_address) => {
let mut config = ResolverConfig::new();
Expand All @@ -26,12 +24,14 @@ impl Resolver {
let nameserver_config = NameServerConfig {
socket_addr: socket,
protocol: Protocol::Udp,
trust_nx_responses: true,
tls_dns_name: None,
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))
}
Expand Down
45 changes: 5 additions & 40 deletions src/os/errors.rs
Original file line number Diff line number Diff line change
@@ -1,44 +1,9 @@
use std::fmt;
use thiserror::Error;

use failure::{Backtrace, Context, Fail};

#[derive(Debug)]
pub struct GetInterfaceError {
inner: Context<GetInterfaceErrorKind>,
}

impl From<GetInterfaceErrorKind> for GetInterfaceError {
fn from(kind: GetInterfaceErrorKind) -> GetInterfaceError {
GetInterfaceError {
inner: Context::new(kind),
}
}
}

impl From<Context<GetInterfaceErrorKind>> for GetInterfaceError {
fn from(inner: Context<GetInterfaceErrorKind>) -> GetInterfaceError {
GetInterfaceError { inner }
}
}
#[derive(Clone, Eq, PartialEq, Debug, Fail)]
pub enum GetInterfaceErrorKind {
#[fail(display = "{}", _0)]
#[derive(Clone, Eq, PartialEq, Debug, Error)]
pub enum GetInterfaceError {
#[error("{0}")]
PermissionError(String),
#[fail(display = "{}", _0)]
#[error("{0}")]
OtherError(String),
}
impl Fail for GetInterfaceError {
fn cause(&self) -> Option<&dyn Fail> {
self.inner.cause()
}

fn backtrace(&self) -> Option<&Backtrace> {
self.inner.backtrace()
}
}

impl fmt::Display for GetInterfaceError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Display::fmt(&self.inner, f)
}
}
35 changes: 15 additions & 20 deletions src/os/shared.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use anyhow::Context;
use ::crossterm::event::read;
use ::crossterm::event::Event;
use ::pnet::datalink::Channel::Ethernet;
Expand All @@ -9,7 +10,7 @@ use ::tokio::runtime::Runtime;
use ::std::net::Ipv4Addr;
use ::std::time;

use crate::os::errors::GetInterfaceErrorKind;
use crate::os::errors::GetInterfaceError;

#[cfg(target_os = "linux")]
use crate::os::linux::get_open_sockets;
Expand All @@ -34,7 +35,7 @@ impl Iterator for TerminalEvents {

pub(crate) fn get_datalink_channel(
interface: &NetworkInterface,
) -> Result<Box<dyn DataLinkReceiver>, GetInterfaceErrorKind> {
) -> Result<Box<dyn DataLinkReceiver>, GetInterfaceError> {
let config = Config {
read_timeout: Some(time::Duration::new(1, 0)),
read_buffer_size: 65536,
Expand All @@ -43,15 +44,15 @@ pub(crate) fn get_datalink_channel(

match datalink::channel(interface, config) {
Ok(Ethernet(_tx, rx)) => Ok(rx),
Ok(_) => Err(GetInterfaceErrorKind::OtherError(format!(
Ok(_) => Err(GetInterfaceError::OtherError(format!(
"{}: Unsupported interface type",
interface.name
))),
Err(e) => match e.kind() {
ErrorKind::PermissionDenied => Err(GetInterfaceErrorKind::PermissionError(
ErrorKind::PermissionDenied => Err(GetInterfaceError::PermissionError(
interface.name.to_owned(),
)),
_ => Err(GetInterfaceErrorKind::OtherError(format!(
_ => Err(GetInterfaceError::OtherError(format!(
"{}: {}",
&interface.name, e
))),
Expand Down Expand Up @@ -85,7 +86,7 @@ where
I: Iterator<
Item = (
&'a NetworkInterface,
Result<Box<dyn DataLinkReceiver>, GetInterfaceErrorKind>,
Result<Box<dyn DataLinkReceiver>, GetInterfaceError>,
),
>,
{
Expand All @@ -97,7 +98,7 @@ where
|acc, (_, elem)| {
if let Some(iface_error) = elem.err() {
match iface_error {
GetInterfaceErrorKind::PermissionError(interface_name) => {
GetInterfaceError::PermissionError(interface_name) => {
if let Some(prev_interface) = acc.permission {
return UserErrors {
permission: Some(format!("{}, {}", prev_interface, interface_name)),
Expand Down Expand Up @@ -151,15 +152,9 @@ pub fn get_input(
interface_name: &Option<String>,
resolve: bool,
dns_server: &Option<Ipv4Addr>,
) -> Result<OsInputOutput, failure::Error> {
) -> Result<OsInputOutput, anyhow::Error> {
let network_interfaces = if let Some(name) = interface_name {
match get_interface(&name) {
Some(interface) => vec![interface],
None => {
failure::bail!("Cannot find interface {}", name);
// the homebrew formula relies on this wording, please be careful when changing
}
}
vec![get_interface(&name).context(format!("Cannot find interface {}", name))?]
} else {
datalink::interfaces()
};
Expand Down Expand Up @@ -195,20 +190,20 @@ pub fn get_input(
if available_network_frames.is_empty() {
let all_errors = collect_errors(network_frames.clone());
if !all_errors.is_empty() {
failure::bail!(all_errors);
anyhow::bail!(all_errors);
}

failure::bail!("Failed to find any network interface to listen on.");
anyhow::bail!("Failed to find any network interface to listen on.");
}

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 runtime = Runtime::new()?;
let resolver =
match runtime.block_on(dns::Resolver::new(runtime.handle().clone(), dns_server)) {
match runtime.block_on(dns::Resolver::new(dns_server)) {
Ok(resolver) => resolver,
Err(err) => failure::bail!(
Err(err) => anyhow::bail!(
"Could not initialize the DNS resolver. Are you offline?\n\nReason: {:?}",
err
),
Expand Down
5 changes: 5 additions & 0 deletions src/tests/cases/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::iter;

use crate::network::dns::Client;
use crate::{Opt, OsInputOutput, RenderOpts};
use crossterm::event::{KeyEventKind, KeyEventState};
use ::crossterm::event::{Event, KeyCode, KeyEvent, KeyModifiers};
use packet_builder::*;
use pnet::datalink::DataLinkReceiver;
Expand All @@ -22,6 +23,8 @@ pub fn sleep_and_quit_events(sleep_num: usize) -> Box<TerminalEvents> {
events.push(Some(Event::Key(KeyEvent {
modifiers: KeyModifiers::CONTROL,
code: KeyCode::Char('c'),
kind: KeyEventKind::Press,
state: KeyEventState::NONE,
})));
Box::new(TerminalEvents::new(events))
}
Expand All @@ -32,6 +35,8 @@ pub fn sleep_resize_and_quit_events(sleep_num: usize) -> Box<TerminalEvents> {
events.push(Some(Event::Key(KeyEvent {
modifiers: KeyModifiers::CONTROL,
code: KeyCode::Char('c'),
kind: KeyEventKind::Press,
state: KeyEventState::NONE,
})));
Box::new(TerminalEvents::new(events))
}
Expand Down
Loading