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

Privacy toggle #824

Merged
merged 8 commits into from
Nov 25, 2023
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
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
only) ([#101](https://github.com/fujiapple852/trippy/issues/101))
- Added `--tui-privacy-max-ttl` flag to hide host and IP details for low ttl
hops ([#766](https://github.com/fujiapple852/trippy/issues/766))
- Added `toggle-privacy` (default: `p`) key binding to show or hide private
hops ([#823](https://github.com/fujiapple852/trippy/issues/823))
- Added `toggle-flows` (default: `f`) key binding to show or hide tracing
flows ([#777](https://github.com/fujiapple852/trippy/issues/777))
- Added `--dns-resolve-all` (`-y`) flag to allow tracing to all IPs resolved from DNS lookup
entry ([#743](https://github.com/fujiapple852/trippy/issues/743))
- Added `dot` report mode (`-m dot`) to output hop graph in Graphviz `DOT`
Expand All @@ -30,7 +34,8 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
- Added `--print-config-template` flag to output a template config
file ([#792](https://github.com/fujiapple852/trippy/issues/792))
- Added `--icmp` flag as a shortcut for `--protocol icmp` ([#649](https://github.com/fujiapple852/trippy/issues/649))
- Added support for `ToggleHelpAlt` key binding ([#694](https://github.com/fujiapple852/trippy/issues/694))
- Added `toggle-help-alt` (default: `?`) key binding to show or hide
help ([#694](https://github.com/fujiapple852/trippy/issues/694))
- Added panic handing to Tui ([#784](https://github.com/fujiapple852/trippy/issues/784))
- Added official Windows `scoop` package ([#462](https://github.com/fujiapple852/trippy/issues/462))
- Added official Windows `winget` package ([#460](https://github.com/fujiapple852/trippy/issues/460))
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -716,6 +716,7 @@ command line option.
| `toggle-chart` | Toggle the chart | `c` |
| `toggle-map` | Toggle the GeoIp map | `m` |
| `toggle-flows` | Toggle the flows | `f` |
| `toggle-privacy` | Toggle the hop privacy | `p` |
| `expand-hosts` | Expand the hosts shown per hop | `]` |
| `expand-hosts-max` | Expand the hosts shown per hop to the maximum | `}` |
| `contract-hosts` | Contract the hosts shown per hop | `[` |
Expand Down
11 changes: 10 additions & 1 deletion src/config/binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pub struct TuiBindings {
pub toggle_chart: TuiKeyBinding,
pub toggle_map: TuiKeyBinding,
pub toggle_flows: TuiKeyBinding,
pub toggle_privacy: TuiKeyBinding,
pub expand_hosts: TuiKeyBinding,
pub contract_hosts: TuiKeyBinding,
pub expand_hosts_max: TuiKeyBinding,
Expand Down Expand Up @@ -62,6 +63,7 @@ impl Default for TuiBindings {
toggle_chart: TuiKeyBinding::new(KeyCode::Char('c')),
toggle_map: TuiKeyBinding::new(KeyCode::Char('m')),
toggle_flows: TuiKeyBinding::new(KeyCode::Char('f')),
toggle_privacy: TuiKeyBinding::new(KeyCode::Char('p')),
expand_hosts: TuiKeyBinding::new(KeyCode::Char(']')),
contract_hosts: TuiKeyBinding::new(KeyCode::Char('[')),
expand_hosts_max: TuiKeyBinding::new(KeyCode::Char('}')),
Expand Down Expand Up @@ -109,6 +111,7 @@ impl TuiBindings {
(self.toggle_chart, TuiCommandItem::ToggleChart),
(self.toggle_map, TuiCommandItem::ToggleMap),
(self.toggle_flows, TuiCommandItem::ToggleFlows),
(self.toggle_privacy, TuiCommandItem::TogglePrivacy),
(self.expand_hosts, TuiCommandItem::ExpandHosts),
(self.expand_hosts_max, TuiCommandItem::ExpandHostsMax),
(self.contract_hosts, TuiCommandItem::ContractHosts),
Expand Down Expand Up @@ -207,7 +210,11 @@ impl From<(HashMap<TuiCommandItem, TuiKeyBinding>, ConfigBindings)> for TuiBindi
toggle_flows: *cmd_items
.get(&TuiCommandItem::ToggleFlows)
.or(cfg.toggle_flows.as_ref())
.unwrap_or(&TuiKeyBinding::new(KeyCode::Char('f'))),
.unwrap_or(&Self::default().toggle_flows),
toggle_privacy: *cmd_items
.get(&TuiCommandItem::ToggleFlows)
.or(cfg.toggle_privacy.as_ref())
.unwrap_or(&Self::default().toggle_privacy),
toggle_map: *cmd_items
.get(&TuiCommandItem::ToggleMap)
.or(cfg.toggle_map.as_ref())
Expand Down Expand Up @@ -515,6 +522,8 @@ pub enum TuiCommandItem {
ToggleMap,
/// Toggle the flows panel.
ToggleFlows,
/// Toggle hop privacy mode.
TogglePrivacy,
/// Expand hosts.
ExpandHosts,
/// Expand hosts to max.
Expand Down
2 changes: 2 additions & 0 deletions src/config/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,7 @@ pub struct ConfigBindings {
pub toggle_freeze: Option<TuiKeyBinding>,
pub toggle_chart: Option<TuiKeyBinding>,
pub toggle_flows: Option<TuiKeyBinding>,
pub toggle_privacy: Option<TuiKeyBinding>,
pub toggle_map: Option<TuiKeyBinding>,
pub expand_hosts: Option<TuiKeyBinding>,
pub contract_hosts: Option<TuiKeyBinding>,
Expand Down Expand Up @@ -366,6 +367,7 @@ impl Default for ConfigBindings {
toggle_freeze: Some(bindings.toggle_freeze),
toggle_chart: Some(bindings.toggle_chart),
toggle_flows: Some(bindings.toggle_flows),
toggle_privacy: Some(bindings.toggle_privacy),
toggle_map: Some(bindings.toggle_map),
expand_hosts: Some(bindings.expand_hosts),
contract_hosts: Some(bindings.contract_hosts),
Expand Down
2 changes: 2 additions & 0 deletions src/frontend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ fn run_app<B: Backend>(
app.toggle_map();
} else if bindings.toggle_flows.check(key) {
app.toggle_flows();
} else if bindings.toggle_privacy.check(key) {
app.toggle_privacy();
} else if bindings.contract_hosts_min.check(key) {
app.contract_hosts_min();
} else if bindings.expand_hosts_max.check(key) {
Expand Down
2 changes: 2 additions & 0 deletions src/frontend/binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub struct Bindings {
pub toggle_chart: KeyBinding,
pub toggle_map: KeyBinding,
pub toggle_flows: KeyBinding,
pub toggle_privacy: KeyBinding,
pub expand_hosts: KeyBinding,
pub contract_hosts: KeyBinding,
pub expand_hosts_max: KeyBinding,
Expand Down Expand Up @@ -55,6 +56,7 @@ impl From<TuiBindings> for Bindings {
toggle_chart: KeyBinding::from(value.toggle_chart),
toggle_map: KeyBinding::from(value.toggle_map),
toggle_flows: KeyBinding::from(value.toggle_flows),
toggle_privacy: KeyBinding::from(value.toggle_privacy),
expand_hosts: KeyBinding::from(value.expand_hosts),
contract_hosts: KeyBinding::from(value.contract_hosts),
expand_hosts_max: KeyBinding::from(value.expand_hosts_max),
Expand Down
4 changes: 2 additions & 2 deletions src/frontend/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ impl TuiConfig {
#[allow(clippy::too_many_arguments)]
pub fn new(
refresh_rate: Duration,
privacy_ttl: u8,
privacy_max_ttl: u8,
preserve_screen: bool,
address_mode: AddressMode,
lookup_as_info: bool,
Expand All @@ -48,7 +48,7 @@ impl TuiConfig {
) -> Self {
Self {
refresh_rate,
privacy_max_ttl: privacy_ttl,
privacy_max_ttl,
preserve_screen,
address_mode,
lookup_as_info,
Expand Down
7 changes: 6 additions & 1 deletion src/frontend/render/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ pub fn render(f: &mut Frame<'_>, app: &TuiApp, rect: Rect) {
.tui_config
.max_addrs
.map_or_else(|| String::from("auto"), |m| m.to_string());
let privacy = if app.hide_private_hops && app.tui_config.privacy_max_ttl > 0 {
"on"
} else {
"off"
};
let source = render_source(app);
let dest = render_destination(app);
let target = format!("{source} -> {dest}");
Expand All @@ -86,7 +91,7 @@ pub fn render(f: &mut Frame<'_>, app: &TuiApp, rect: Rect) {
Line::from(vec![
Span::styled("Config: ", Style::default().add_modifier(Modifier::BOLD)),
Span::raw(format!(
"protocol={protocol} as-info={as_info} details={details} max-hosts={max_hosts}"
"protocol={protocol} as-info={as_info} details={details} max-hosts={max_hosts}, privacy={privacy}"
)),
]),
Line::from(vec![
Expand Down
5 changes: 3 additions & 2 deletions src/frontend/render/help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub fn render(f: &mut Frame<'_>, app: &TuiApp) {
f.render_widget(control, area);
}

const HELP_LINES: [&str; 21] = [
const HELP_LINES: [&str; 22] = [
"[up] & [down] - select hop",
"[left] & [right] - select trace or flow",
", & . - select hop address",
Expand All @@ -34,7 +34,8 @@ const HELP_LINES: [&str; 21] = [
"f - toggle flows",
"c - toggle chart",
"m - toggle map",
"Ctrl-f - toggle freeze display",
"p - toggle privacy",
"Ctrl+f - toggle freeze display",
"Ctrl+r - reset statistics",
"Ctrl+k - flush DNS cache",
"i - show IP only",
Expand Down
3 changes: 2 additions & 1 deletion src/frontend/render/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ fn format_binding_settings(app: &TuiApp) -> Vec<SettingsItem> {
SettingsItem::new("toggle-chart", format!("{}", binds.toggle_chart)),
SettingsItem::new("toggle-map", format!("{}", binds.toggle_map)),
SettingsItem::new("toggle-flows", format!("{}", binds.toggle_flows)),
SettingsItem::new("toggle-privacy", format!("{}", binds.toggle_privacy)),
SettingsItem::new("expand-hosts", format!("{}", binds.expand_hosts)),
SettingsItem::new("expand-hosts-max", format!("{}", binds.expand_hosts_max)),
SettingsItem::new("contract-hosts", format!("{}", binds.contract_hosts)),
Expand Down Expand Up @@ -421,7 +422,7 @@ pub const SETTINGS_TABS: [(&str, usize); 6] = [
("Trace", 14),
("Dns", 4),
("GeoIp", 1),
("Bindings", 27),
("Bindings", 29),
("Theme", 31),
];

Expand Down
14 changes: 7 additions & 7 deletions src/frontend/render/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ fn render_table_row(
let (hostname_cell, row_height) = if is_selected_hop && app.show_hop_details {
render_hostname_with_details(app, hop, dns, geoip_lookup, config)
} else {
render_hostname(hop, dns, geoip_lookup, config)
render_hostname(app, hop, dns, geoip_lookup)
};
let loss_pct_cell = render_loss_pct_cell(hop);
let total_sent_cell = render_total_sent_cell(hop);
Expand Down Expand Up @@ -187,21 +187,21 @@ fn render_status_cell(hop: &Hop, is_target: bool) -> Cell<'static> {

/// Render hostname table cell (normal mode).
fn render_hostname(
app: &TuiApp,
hop: &Hop,
dns: &DnsResolver,
geoip_lookup: &GeoIpLookup,
config: &TuiConfig,
) -> (Cell<'static>, u16) {
let (hostname, count) = if hop.total_recv() > 0 {
if config.privacy_max_ttl >= hop.ttl() {
if app.hide_private_hops && app.tui_config.privacy_max_ttl >= hop.ttl() {
(String::from("**Hidden**"), 1)
} else {
match config.max_addrs {
match app.tui_config.max_addrs {
None => {
let hostnames = hop
.addrs_with_counts()
.map(|(addr, &freq)| {
format_address(addr, freq, hop, dns, geoip_lookup, config)
format_address(addr, freq, hop, dns, geoip_lookup, &app.tui_config)
})
.join("\n");
let count = hop.addr_count().clamp(1, u8::MAX as usize);
Expand All @@ -214,7 +214,7 @@ fn render_hostname(
.rev()
.take(max_addr as usize)
.map(|(addr, &freq)| {
format_address(addr, freq, hop, dns, geoip_lookup, config)
format_address(addr, freq, hop, dns, geoip_lookup, &app.tui_config)
})
.join("\n");
let count = hop.addr_count().clamp(1, max_addr as usize);
Expand Down Expand Up @@ -342,7 +342,7 @@ fn render_hostname_with_details(
config: &TuiConfig,
) -> (Cell<'static>, u16) {
let (rendered, count) = if hop.total_recv() > 0 {
if config.privacy_max_ttl >= hop.ttl() {
if app.hide_private_hops && config.privacy_max_ttl >= hop.ttl() {
let height = if config.lookup_as_info { 6 } else { 3 };
(String::from("**Hidden**"), height)
} else {
Expand Down
24 changes: 15 additions & 9 deletions src/frontend/render/world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,20 @@ fn render_map_canvas(f: &mut Frame<'_>, app: &TuiApp, rect: Rect, entries: &[Map
render_map_canvas_world(ctx, theme.map_world_color);
ctx.layer();
for entry in entries {
render_map_canvas_pin(ctx, entry);
render_map_canvas_radius(ctx, entry, theme.map_radius_color);
render_map_canvas_selected(
ctx,
entry,
app.selected_hop_or_target(),
theme.map_selected_color,
);
let any_show = entry
.hops
.iter()
.any(|hop| *hop > app.tui_config.privacy_max_ttl);
if !app.hide_private_hops || any_show {
render_map_canvas_pin(ctx, entry);
render_map_canvas_radius(ctx, entry, theme.map_radius_color);
render_map_canvas_selected(
ctx,
entry,
app.selected_hop_or_target(),
theme.map_selected_color,
);
}
}
})
.marker(Marker::Braille)
Expand Down Expand Up @@ -138,7 +144,7 @@ fn render_map_info_panel(f: &mut Frame<'_>, app: &TuiApp, rect: Rect, entries: &
}
})
.collect::<Vec<_>>();
let info = if app.tui_config.privacy_max_ttl >= selected_hop.ttl() {
let info = if app.hide_private_hops && app.tui_config.privacy_max_ttl >= selected_hop.ttl() {
"**Hidden**".to_string()
} else {
match locations.as_slice() {
Expand Down
7 changes: 7 additions & 0 deletions src/frontend/tui_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ pub struct TuiApp {
pub show_settings: bool,
pub show_hop_details: bool,
pub show_flows: bool,
/// Whether private hops should be shown or not.
pub hide_private_hops: bool,
pub show_chart: bool,
pub show_map: bool,
pub frozen_start: Option<SystemTime>,
Expand Down Expand Up @@ -68,6 +70,7 @@ impl TuiApp {
show_settings: false,
show_hop_details: false,
show_flows: false,
hide_private_hops: true,
show_chart: false,
show_map: false,
frozen_start: None,
Expand Down Expand Up @@ -332,6 +335,10 @@ impl TuiApp {
}
}

pub fn toggle_privacy(&mut self) {
self.hide_private_hops = !self.hide_private_hops;
}

pub fn toggle_asinfo(&mut self) {
match self.resolver.config().resolve_method {
ResolveMethod::Resolv | ResolveMethod::Google | ResolveMethod::Cloudflare => {
Expand Down
1 change: 1 addition & 0 deletions trippy-config-sample.toml
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,7 @@ toggle-freeze = "ctrl+f"
toggle-chart = "c"
toggle-map = "m"
toggle-flows = "f"
toggle-privacy = "p"
expand-hosts = "]"
expand-hosts-max = "}"
contract-hosts = "["
Expand Down
Loading