Skip to content

Commit

Permalink
feat(tui): add support for expand-privacy and contract-privacy - WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
fujiapple852 committed Oct 15, 2024
1 parent 977ad2e commit b602578
Show file tree
Hide file tree
Showing 11 changed files with 56 additions and 34 deletions.
30 changes: 21 additions & 9 deletions crates/trippy-tui/src/config/binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ pub struct TuiBindings {
pub toggle_chart: TuiKeyBinding,
pub toggle_map: TuiKeyBinding,
pub toggle_flows: TuiKeyBinding,
pub toggle_privacy: TuiKeyBinding,
pub expand_privacy: TuiKeyBinding,
pub contract_privacy: TuiKeyBinding,
pub expand_hosts: TuiKeyBinding,
pub contract_hosts: TuiKeyBinding,
pub expand_hosts_max: TuiKeyBinding,
Expand Down Expand Up @@ -77,7 +78,11 @@ 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_privacy: TuiKeyBinding::new(KeyCode::Char('p')),
contract_privacy: TuiKeyBinding::new_with_modifier(
KeyCode::Char('p'),
KeyModifiers::SHIFT,
),
expand_hosts: TuiKeyBinding::new(KeyCode::Char(']')),
contract_hosts: TuiKeyBinding::new(KeyCode::Char('[')),
expand_hosts_max: TuiKeyBinding::new(KeyCode::Char('}')),
Expand Down Expand Up @@ -135,7 +140,8 @@ impl TuiBindings {
(self.toggle_chart, TuiCommandItem::ToggleChart),
(self.toggle_map, TuiCommandItem::ToggleMap),
(self.toggle_flows, TuiCommandItem::ToggleFlows),
(self.toggle_privacy, TuiCommandItem::TogglePrivacy),
(self.expand_privacy, TuiCommandItem::ExpandPrivacy),
(self.contract_privacy, TuiCommandItem::ContractPrivacy),
(self.expand_hosts, TuiCommandItem::ExpandHosts),
(self.expand_hosts_max, TuiCommandItem::ExpandHostsMax),
(self.contract_hosts, TuiCommandItem::ContractHosts),
Expand Down Expand Up @@ -263,10 +269,14 @@ impl From<(HashMap<TuiCommandItem, TuiKeyBinding>, ConfigBindings)> for TuiBindi
.get(&TuiCommandItem::ToggleFlows)
.or(cfg.toggle_flows.as_ref())
.unwrap_or(&Self::default().toggle_flows),
toggle_privacy: *cmd_items
.get(&TuiCommandItem::TogglePrivacy)
.or(cfg.toggle_privacy.as_ref())
.unwrap_or(&Self::default().toggle_privacy),
expand_privacy: *cmd_items
.get(&TuiCommandItem::ExpandPrivacy)
.or(cfg.expand_privacy.as_ref())
.unwrap_or(&Self::default().expand_privacy),
contract_privacy: *cmd_items
.get(&TuiCommandItem::ContractPrivacy)
.or(cfg.contract_privacy.as_ref())
.unwrap_or(&Self::default().contract_privacy),
toggle_map: *cmd_items
.get(&TuiCommandItem::ToggleMap)
.or(cfg.toggle_map.as_ref())
Expand Down Expand Up @@ -588,8 +598,10 @@ pub enum TuiCommandItem {
ToggleMap,
/// Toggle the flows panel.
ToggleFlows,
/// Toggle hop privacy mode.
TogglePrivacy,
/// Expand hop privacy.
ExpandPrivacy,
/// Contract hop privacy.
ContractPrivacy,
/// Expand hosts.
ExpandHosts,
/// Expand hosts to max.
Expand Down
6 changes: 4 additions & 2 deletions crates/trippy-tui/src/config/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,8 @@ pub struct ConfigBindings {
pub toggle_freeze: Option<TuiKeyBinding>,
pub toggle_chart: Option<TuiKeyBinding>,
pub toggle_flows: Option<TuiKeyBinding>,
pub toggle_privacy: Option<TuiKeyBinding>,
pub expand_privacy: Option<TuiKeyBinding>,
pub contract_privacy: Option<TuiKeyBinding>,
pub toggle_map: Option<TuiKeyBinding>,
pub expand_hosts: Option<TuiKeyBinding>,
pub contract_hosts: Option<TuiKeyBinding>,
Expand Down Expand Up @@ -419,7 +420,8 @@ 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),
expand_privacy: Some(bindings.expand_privacy),
contract_privacy: Some(bindings.contract_privacy),
toggle_map: Some(bindings.toggle_map),
expand_hosts: Some(bindings.expand_hosts),
contract_hosts: Some(bindings.contract_hosts),
Expand Down
6 changes: 4 additions & 2 deletions crates/trippy-tui/src/frontend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,10 @@ 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.expand_privacy.check(key) {
app.expand_privacy();
} else if bindings.contract_privacy.check(key) {
app.contract_privacy();
} else if bindings.contract_hosts_min.check(key) {
app.contract_hosts_min();
} else if bindings.expand_hosts_max.check(key) {
Expand Down
6 changes: 4 additions & 2 deletions crates/trippy-tui/src/frontend/binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ pub struct Bindings {
pub toggle_chart: KeyBinding,
pub toggle_map: KeyBinding,
pub toggle_flows: KeyBinding,
pub toggle_privacy: KeyBinding,
pub expand_privacy: KeyBinding,
pub contract_privacy: KeyBinding,
pub expand_hosts: KeyBinding,
pub contract_hosts: KeyBinding,
pub expand_hosts_max: KeyBinding,
Expand Down Expand Up @@ -70,7 +71,8 @@ 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_privacy: KeyBinding::from(value.expand_privacy),
contract_privacy: KeyBinding::from(value.contract_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
6 changes: 1 addition & 5 deletions crates/trippy-tui/src/frontend/render/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,7 @@ pub fn render(f: &mut Frame<'_>, app: &TuiApp, rect: Rect) {
.tui_config
.max_addrs
.map_or_else(|| String::from(t!("auto")), |m| m.to_string());
let privacy = if app.hide_private_hops && app.tui_config.privacy_max_ttl > 0 {
t!("on")
} else {
t!("off")
};
let privacy = app.tui_config.privacy_max_ttl;
let source = render_source(app);
let dest = render_destination(app);
let target = format!("{source} -> {dest}");
Expand Down
5 changes: 3 additions & 2 deletions crates/trippy-tui/src/frontend/render/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,8 @@ 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-privacy", format!("{}", binds.expand_privacy)),
SettingsItem::new("contract-privacy", format!("{}", binds.contract_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 @@ -523,7 +524,7 @@ pub fn settings_tabs() -> [(String, usize); 7] {
(t!("settings_tab_trace_title").to_string(), 17),
(t!("settings_tab_dns_title").to_string(), 5),
(t!("settings_tab_geoip_title").to_string(), 1),
(t!("settings_tab_bindings_title").to_string(), 36),
(t!("settings_tab_bindings_title").to_string(), 37),
(t!("settings_tab_theme_title").to_string(), 31),
(t!("settings_tab_columns_title").to_string(), 0),
]
Expand Down
4 changes: 2 additions & 2 deletions crates/trippy-tui/src/frontend/render/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ fn render_hostname(
geoip_lookup: &GeoIpLookup,
) -> (Cell<'static>, u16) {
let (hostname, count) = if hop.total_recv() > 0 {
if app.hide_private_hops && app.tui_config.privacy_max_ttl >= hop.ttl() {
if app.tui_config.privacy_max_ttl >= hop.ttl() {
(format!("**{}**", t!("hidden")), 1)
} else {
match app.tui_config.max_addrs {
Expand Down Expand Up @@ -512,7 +512,7 @@ fn render_hostname_with_details(
config: &TuiConfig,
) -> (Cell<'static>, u16) {
let rendered = if hop.total_recv() > 0 {
if app.hide_private_hops && config.privacy_max_ttl >= hop.ttl() {
if config.privacy_max_ttl >= hop.ttl() {
format!("**{}**", t!("hidden"))
} else {
let index = app.selected_hop_address;
Expand Down
4 changes: 2 additions & 2 deletions crates/trippy-tui/src/frontend/render/world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ fn render_map_canvas(f: &mut Frame<'_>, app: &TuiApp, rect: Rect, entries: &[Map
.hops
.iter()
.any(|hop| *hop > app.tui_config.privacy_max_ttl);
if !app.hide_private_hops || any_show {
if any_show {
render_map_canvas_pin(ctx, entry);
render_map_canvas_radius(ctx, entry, theme.map_radius);
render_map_canvas_selected(
Expand Down Expand Up @@ -146,7 +146,7 @@ fn render_map_info_panel(f: &mut Frame<'_>, app: &TuiApp, rect: Rect, entries: &
}
})
.collect::<Vec<_>>();
let info = if app.hide_private_hops && app.tui_config.privacy_max_ttl >= selected_hop.ttl() {
let info = if app.tui_config.privacy_max_ttl >= selected_hop.ttl() {
format!("**{}**", t!("hidden"))
} else {
match locations.as_slice() {
Expand Down
16 changes: 11 additions & 5 deletions crates/trippy-tui/src/frontend/tui_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ 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 @@ -70,7 +68,6 @@ 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 @@ -382,8 +379,17 @@ impl TuiApp {
}
}

pub fn toggle_privacy(&mut self) {
self.hide_private_hops = !self.hide_private_hops;
pub fn expand_privacy(&mut self) {
let hop_count = self.tracer_data().hops_for_flow(self.selected_flow).len();
if usize::from(self.tui_config.privacy_max_ttl) < hop_count {
self.tui_config.privacy_max_ttl += 1;
}
}

pub fn contract_privacy(&mut self) {
if self.tui_config.privacy_max_ttl > 0 {
self.tui_config.privacy_max_ttl -= 1;
}
}

pub fn toggle_asinfo(&mut self) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
---
source: crates/trippy-tui/src/print.rs
---
TUIbindingcommands:toggle-help,toggle-help-alt,toggle-settings,toggle-settings-tui,toggle-settings-trace,toggle-settings-dns,toggle-settings-geoip,toggle-settings-bindings,toggle-settings-theme,toggle-settings-columns,next-hop,previous-hop,next-trace,previous-trace,next-hop-address,previous-hop-address,address-mode-ip,address-mode-host,address-mode-both,toggle-freeze,toggle-chart,toggle-map,toggle-flows,toggle-privacy,expand-hosts,expand-hosts-max,contract-hosts,contract-hosts-min,chart-zoom-in,chart-zoom-out,clear-trace-data,clear-dns-cache,clear-selection,toggle-as-info,toggle-hop-details,quit
TUIbindingcommands:toggle-help,toggle-help-alt,toggle-settings,toggle-settings-tui,toggle-settings-trace,toggle-settings-dns,toggle-settings-geoip,toggle-settings-bindings,toggle-settings-theme,toggle-settings-columns,next-hop,previous-hop,next-trace,previous-trace,next-hop-address,previous-hop-address,address-mode-ip,address-mode-host,address-mode-both,toggle-freeze,toggle-chart,toggle-map,toggle-flows,expand-privacy,contract-privacy,expand-hosts,expand-hosts-max,contract-hosts,contract-hosts-min,chart-zoom-in,chart-zoom-out,clear-trace-data,clear-dns-cache,clear-selection,toggle-as-info,toggle-hop-details,quit
5 changes: 3 additions & 2 deletions trippy-config-sample.toml
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ tui-preserve-screen = false
# The Tui refresh rate [default: 100ms]
tui-refresh-rate = "100ms"

# The maximum ttl of hops which will be masked for privacy [default: 1]
# The maximum ttl of hops which will be masked for privacy [default: 0]
tui-privacy-max-ttl = 0

# The locale to use for Tui [default: auto]
Expand Down Expand Up @@ -408,7 +408,8 @@ toggle-freeze = "ctrl+f"
toggle-chart = "c"
toggle-map = "m"
toggle-flows = "f"
toggle-privacy = "p"
expand-privacy = "p"
contract-privacy = "shift+p"
expand-hosts = "]"
expand-hosts-max = "}"
contract-hosts = "["
Expand Down

0 comments on commit b602578

Please sign in to comment.