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 2 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
18 changes: 14 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ pub struct Opt {
render_opts: RenderOpts,
}

#[derive(StructOpt, Debug)]
#[derive(StructOpt, Debug, Copy, Clone)]
pub struct RenderOpts {
#[structopt(short, long)]
/// Show processes table only
Expand All @@ -59,6 +59,9 @@ pub struct RenderOpts {
#[structopt(short, long)]
/// Show remote addresses table only
addresses: bool,
#[structopt(short, long)]
/// Show DNS queries
show_dns: bool,
}

fn main() {
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 = Arc::new(AtomicBool::new(opts.render_opts.show_dns));
Copy link
Owner

Choose a reason for hiding this comment

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

Why does this need to be an Arc<AtomicBool> - do you think we can make it a normal boolean and then pass it around without cloning or anything? Seems easier to me.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I just thought that we could switch between show_dns modes via some key. But, yeah, i will replace it, or maybe should I add switching functionality to it?


let mut active_threads = vec![];

Expand All @@ -137,11 +141,15 @@ where
.spawn({
let ui = ui.clone();
let paused = paused.clone();
let show_dns = dns_shown.clone();
move || {
on_winch({
Box::new(move || {
let mut ui = ui.lock().unwrap();
ui.draw(paused.load(Ordering::SeqCst));
ui.draw(
paused.load(Ordering::SeqCst),
show_dns.load(Ordering::SeqCst),
);
})
});
}
Expand All @@ -155,6 +163,7 @@ where
.spawn({
let running = running.clone();
let paused = paused.clone();
let show_dns = dns_shown.clone();
let network_utilization = network_utilization.clone();
move || {
while running.load(Ordering::Acquire) {
Expand Down Expand Up @@ -184,7 +193,7 @@ where
if raw_mode {
ui.output_text(&mut write_to_stdout);
} else {
ui.draw(paused);
ui.draw(paused, show_dns.load(Ordering::SeqCst));
}
}
let render_duration = render_start_time.elapsed();
Expand Down Expand Up @@ -235,12 +244,13 @@ where
.map(|(iface, frames)| {
let name = format!("sniffing_handler_{}", iface.name);
let running = running.clone();
let show_dns = opts.render_opts.show_dns.clone();
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
25 changes: 20 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 @@ -157,6 +166,12 @@ impl Sniffer {
let from = SocketAddr::new(ip_packet.get_source().into(), source_port);
let to = SocketAddr::new(ip_packet.get_destination().into(), destination_port);

if !show_dns {
if from.port() == 53 {
return None;
}
}

Copy link
Owner

Choose a reason for hiding this comment

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

I think it would be more accurate to do this after creating the connection... something like:

         if !show_dns {                                                                                                                                                                                                                     
             if connection.remote_socket.port == 53 {
                 return None;
             }
         }

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi @imsnif! Thank you, yeah, sure, i just was going to add a tests for this. This PR should be work in progress yet.

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 Good afternoon, could you please check my pr?

let connection = match direction {
Direction::Download => Connection::new(from, to.ip(), destination_port, protocol),
Direction::Upload => Connection::new(to, from.ip(), source_port, protocol),
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 @@ -580,6 +580,7 @@ fn no_resolve_mode() {
addresses: false,
connections: false,
processes: false,
show_dns: false,
},
};
start(backend, os_input, opts);
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).

2 changes: 1 addition & 1 deletion src/tests/cases/snapshots/ui__basic_startup.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__bi_directional_traffic.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).

Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@ expression: "&terminal_draw_events_mirror[0]"
│ ││ │
│ ││ │
└─────────────────────────────────────────────────────────────────────────────────────────────┘└─────────────────────────────────────────────────────────────────────────────────────────────┘
Press <SPACE> to pause.
Press <SPACE> to pause (DNS queries hidden).

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).

Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@ expression: "&terminal_draw_events_mirror[0]"
│ │
│ │
└─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
Press <SPACE> to pause.
Press <SPACE> to pause (DNS queries hidden).

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