Skip to content

Feature, hide dns queries by default #161

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

Merged
merged 3 commits into from
Apr 5, 2020
Merged
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
17 changes: 13 additions & 4 deletions src/display/components/help_text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,31 @@ use ::tui::widgets::{Paragraph, Text, Widget};

pub struct HelpText {
pub paused: bool,
pub show_dns: bool,
}

const TEXT_WHEN_PAUSED: &str = " Press <SPACE> to resume.";
const TEXT_WHEN_NOT_PAUSED: &str = " Press <SPACE> to pause.";
const TEXT_WHEN_PAUSED: &str = " Press <SPACE> to resume";
const TEXT_WHEN_NOT_PAUSED: &str = " Press <SPACE> to pause";
const TEXT_WHEN_DNS_NOT_SHOWN: &str = " (DNS queries hidden).";
const TEXT_WHEN_DNS_SHOWN: &str = " (DNS queries shown).";

impl HelpText {
pub fn render(&self, frame: &mut Frame<impl Backend>, rect: Rect) {
let text = {
let content = if self.paused {
let pause_content = if self.paused {
TEXT_WHEN_PAUSED
} else {
TEXT_WHEN_NOT_PAUSED
};

let dns_content = if self.show_dns {
TEXT_WHEN_DNS_SHOWN
} else {
TEXT_WHEN_DNS_NOT_SHOWN
};

[Text::styled(
content,
format!("{}{}", pause_content, dns_content),
Style::default().modifier(Modifier::BOLD),
)]
};
Expand Down
4 changes: 2 additions & 2 deletions src/display/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ where
));
}
}
pub fn draw(&mut self, paused: bool) {
pub fn draw(&mut self, paused: bool, show_dns: bool) {
let state = &self.state;
let children = self.get_tables_to_display();
self.terminal
Expand All @@ -87,7 +87,7 @@ where
state: &state,
paused,
};
let help_text = HelpText { paused };
let help_text = HelpText { paused, show_dns };
let layout = Layout {
header: total_bandwidth,
children,
Expand Down
13 changes: 9 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,12 @@ pub struct Opt {
no_resolve: bool,
#[structopt(flatten)]
render_opts: RenderOpts,
#[structopt(short, long)]
/// Show DNS queries
show_dns: bool,
}

#[derive(StructOpt, Debug)]
#[derive(StructOpt, Debug, Copy, Clone)]
pub struct RenderOpts {
#[structopt(short, long)]
/// Show processes table only
Expand Down Expand Up @@ -115,6 +118,7 @@ where
{
let running = Arc::new(AtomicBool::new(true));
let paused = Arc::new(AtomicBool::new(false));
let dns_shown = opts.show_dns;

let mut active_threads = vec![];

Expand All @@ -141,7 +145,7 @@ where
on_winch({
Box::new(move || {
let mut ui = ui.lock().unwrap();
ui.draw(paused.load(Ordering::SeqCst));
ui.draw(paused.load(Ordering::SeqCst), dns_shown);
})
});
}
Expand Down Expand Up @@ -184,7 +188,7 @@ where
if raw_mode {
ui.output_text(&mut write_to_stdout);
} else {
ui.draw(paused);
ui.draw(paused, dns_shown);
}
}
let render_duration = render_start_time.elapsed();
Expand Down Expand Up @@ -235,12 +239,13 @@ where
.map(|(iface, frames)| {
let name = format!("sniffing_handler_{}", iface.name);
let running = running.clone();
let show_dns = opts.show_dns;
let network_utilization = network_utilization.clone();

thread::Builder::new()
.name(name)
.spawn(move || {
let mut sniffer = Sniffer::new(iface, frames);
let mut sniffer = Sniffer::new(iface, frames, show_dns);

while running.load(Ordering::Acquire) {
if let Some(segment) = sniffer.next() {
Expand Down
23 changes: 18 additions & 5 deletions src/network/sniffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,19 @@ macro_rules! extract_transport_protocol {
pub struct Sniffer {
network_interface: NetworkInterface,
network_frames: Box<dyn DataLinkReceiver>,
dns_shown: bool,
}

impl Sniffer {
pub fn new(
network_interface: NetworkInterface,
network_frames: Box<dyn DataLinkReceiver>,
dns_shown: bool,
) -> Self {
Sniffer {
network_interface,
network_frames,
dns_shown,
}
}
pub fn next(&mut self) -> Option<Segment> {
Expand All @@ -109,17 +112,19 @@ impl Sniffer {
let version = ip_packet.get_version();

match version {
4 => Self::handle_v4(ip_packet, &self.network_interface),
4 => Self::handle_v4(ip_packet, &self.network_interface, self.dns_shown),
6 => Self::handle_v6(
Ipv6Packet::new(&bytes[payload_offset..])?,
&self.network_interface,
),
_ => {
let pkg = EthernetPacket::new(bytes)?;
match pkg.get_ethertype() {
EtherTypes::Ipv4 => {
Self::handle_v4(Ipv4Packet::new(pkg.payload())?, &self.network_interface)
}
EtherTypes::Ipv4 => Self::handle_v4(
Ipv4Packet::new(pkg.payload())?,
&self.network_interface,
self.dns_shown,
),
EtherTypes::Ipv6 => {
Self::handle_v6(Ipv6Packet::new(pkg.payload())?, &self.network_interface)
}
Expand Down Expand Up @@ -148,7 +153,11 @@ impl Sniffer {
direction,
})
}
fn handle_v4(ip_packet: Ipv4Packet, network_interface: &NetworkInterface) -> Option<Segment> {
fn handle_v4(
ip_packet: Ipv4Packet,
network_interface: &NetworkInterface,
show_dns: bool,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm... can't we use self.dns_shown inside this function instead of passing show_dns to it?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right. I'm not sure why we implemented it like that shrugs nevermind. :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@imsnif ok, so, we keep at as it is, or should i try to modify it and passing self parameter ?

) -> Option<Segment> {
let (protocol, source_port, destination_port, data_length) =
extract_transport_protocol!(ip_packet);

Expand All @@ -161,6 +170,10 @@ impl Sniffer {
Direction::Download => Connection::new(from, to.ip(), destination_port, protocol),
Direction::Upload => Connection::new(to, from.ip(), source_port, protocol),
};

if !show_dns && connection.remote_socket.port == 53 {
return None;
}
Some(Segment {
interface_name,
connection,
Expand Down
1 change: 1 addition & 0 deletions src/tests/cases/raw_mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,7 @@ fn no_resolve_mode() {
interface: Some(String::from("interface_name")),
raw: true,
no_resolve: true,
show_dns: false,
render_opts: RenderOpts {
addresses: false,
connections: false,
Expand Down
2 changes: 1 addition & 1 deletion src/tests/cases/snapshots/ui__basic_only_addresses.snap
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,5 @@ expression: "&terminal_draw_events_mirror[0]"
│ │
│ │
└────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
Press <SPACE> to pause.
Press <SPACE> to pause (DNS queries hidden).

2 changes: 1 addition & 1 deletion src/tests/cases/snapshots/ui__basic_only_connections.snap
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,5 @@ expression: "&terminal_draw_events_mirror[0]"
│ │
│ │
└────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
Press <SPACE> to pause.
Press <SPACE> to pause (DNS queries hidden).

2 changes: 1 addition & 1 deletion src/tests/cases/snapshots/ui__basic_only_processes.snap
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,5 @@ expression: "&terminal_draw_events_mirror[0]"
│ │
│ │
└────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
Press <SPACE> to pause.
Press <SPACE> to pause (DNS queries hidden).

Loading