Skip to content

Commit

Permalink
feat(tui): add --tui-privacy-max-ttl flag to set the maximum ttl of…
Browse files Browse the repository at this point in the history
… hops which will be masked for privacy (#766)
  • Loading branch information
fujiapple852 committed Nov 9, 2023
1 parent d5d59a0 commit 7e32969
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 33 deletions.
7 changes: 7 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ pub struct TrippyConfig {
pub tui_max_samples: usize,
pub tui_preserve_screen: bool,
pub tui_refresh_rate: Duration,
pub tui_privacy_ttl: u8,
pub tui_address_mode: AddressMode,
pub tui_as_mode: AsMode,
pub tui_geoip_mode: GeoIpMode,
Expand Down Expand Up @@ -388,6 +389,11 @@ impl TryFrom<(Args, &Platform)> for TrippyConfig {
cfg_file_tui.tui_refresh_rate,
String::from(constants::DEFAULT_TUI_REFRESH_RATE),
);
let tui_privacy_ttl = cfg_layer(
args.tui_privacy_max_ttl,
cfg_file_tui.tui_privacy_max_ttl,
constants::DEFAULT_TUI_PRIVACY_MAX_TTL,
);
let tui_address_mode = cfg_layer(
args.tui_address_mode,
cfg_file_tui.tui_address_mode,
Expand Down Expand Up @@ -550,6 +556,7 @@ impl TryFrom<(Args, &Platform)> for TrippyConfig {
tui_max_samples,
tui_preserve_screen,
tui_refresh_rate,
tui_privacy_ttl,
tui_address_mode,
tui_as_mode,
tui_geoip_mode,
Expand Down
6 changes: 5 additions & 1 deletion src/config/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub struct Args {
#[arg(long, conflicts_with = "protocol", conflicts_with = "udp")]
pub tcp: bool,

/// use IPv4 only
/// Use IPv4 only
#[arg(short = '4', long, conflicts_with = "ipv6")]
pub ipv4: bool,

Expand Down Expand Up @@ -158,6 +158,10 @@ pub struct Args {
#[arg(long)]
pub tui_refresh_rate: Option<String>,

/// The maximum ttl of hops which will be masked for privacy [default: 0]
#[arg(long)]
pub tui_privacy_max_ttl: Option<u8>,

/// The TUI theme colors [item=color,item=color,..]
#[arg(long, value_delimiter(','), value_parser = parse_tui_theme_color_value)]
pub tui_theme_colors: Vec<(TuiThemeItem, TuiColor)>,
Expand Down
3 changes: 3 additions & 0 deletions src/config/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ pub const DEFAULT_TUI_ADDRESS_MODE: AddressMode = AddressMode::Host;
/// The default value for `tui-refresh-rate`.
pub const DEFAULT_TUI_REFRESH_RATE: &str = "100ms";

/// The default value for `tui_privacy_max_ttl`.
pub const DEFAULT_TUI_PRIVACY_MAX_TTL: u8 = 0;

/// The default value for `dns-resolve-method`.
pub const DEFAULT_DNS_RESOLVE_METHOD: DnsResolveMethodConfig = DnsResolveMethodConfig::System;

Expand Down
1 change: 1 addition & 0 deletions src/config/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ pub struct ConfigTui {
pub tui_max_samples: Option<usize>,
pub tui_preserve_screen: Option<bool>,
pub tui_refresh_rate: Option<String>,
pub tui_privacy_max_ttl: Option<u8>,
pub tui_address_mode: Option<AddressMode>,
pub tui_as_mode: Option<AsMode>,
pub tui_geoip_mode: Option<GeoIpMode>,
Expand Down
4 changes: 4 additions & 0 deletions src/frontend/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ use std::time::Duration;
pub struct TuiConfig {
/// Refresh rate.
pub refresh_rate: Duration,
/// The minimum hop for which IPs and hostnames will be displayed.
pub privacy_ttl: u8,
/// Preserve screen on exit.
pub preserve_screen: bool,
/// How to render addresses.
Expand All @@ -33,6 +35,7 @@ impl TuiConfig {
#[allow(clippy::too_many_arguments)]
pub fn new(
refresh_rate: Duration,
privacy_ttl: u8,
preserve_screen: bool,
address_mode: AddressMode,
lookup_as_info: bool,
Expand All @@ -45,6 +48,7 @@ impl TuiConfig {
) -> Self {
Self {
refresh_rate,
privacy_ttl,
preserve_screen,
address_mode,
lookup_as_info,
Expand Down
6 changes: 5 additions & 1 deletion src/frontend/render/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,10 @@ fn format_tui_settings(app: &TuiApp) -> Vec<SettingsItem> {
"tui-refresh-rate",
format!("{}", format_duration(app.tui_config.refresh_rate)),
),
SettingsItem::new(
"tui-privacy-max-ttl",
format!("{}", app.tui_config.privacy_ttl),
),
SettingsItem::new(
"tui-address-mode",
format_address_mode(app.tui_config.address_mode),
Expand Down Expand Up @@ -395,7 +399,7 @@ fn format_theme_settings(app: &TuiApp) -> Vec<SettingsItem> {

/// The name and number of items for each tabs in the setting dialog.
pub const SETTINGS_TABS: [(&str, usize); 6] = [
("Tui", 7),
("Tui", 8),
("Trace", 14),
("Dns", 4),
("GeoIp", 1),
Expand Down
55 changes: 34 additions & 21 deletions src/frontend/render/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,25 +193,33 @@ fn render_hostname(
config: &TuiConfig,
) -> (Cell<'static>, u16) {
let (hostname, count) = if hop.total_recv() > 0 {
match config.max_addrs {
None => {
let hostnames = hop
.addrs_with_counts()
.map(|(addr, &freq)| format_address(addr, freq, hop, dns, geoip_lookup, config))
.join("\n");
let count = hop.addr_count().clamp(1, u8::MAX as usize);
(hostnames, count as u16)
}
Some(max_addr) => {
let hostnames = hop
.addrs_with_counts()
.sorted_unstable_by_key(|(_, &cnt)| cnt)
.rev()
.take(max_addr as usize)
.map(|(addr, &freq)| format_address(addr, freq, hop, dns, geoip_lookup, config))
.join("\n");
let count = hop.addr_count().clamp(1, max_addr as usize);
(hostnames, count as u16)
if config.privacy_ttl >= hop.ttl() {
(String::from("**Hidden**"), 1)
} else {
match config.max_addrs {
None => {
let hostnames = hop
.addrs_with_counts()
.map(|(addr, &freq)| {
format_address(addr, freq, hop, dns, geoip_lookup, config)
})
.join("\n");
let count = hop.addr_count().clamp(1, u8::MAX as usize);
(hostnames, count as u16)
}
Some(max_addr) => {
let hostnames = hop
.addrs_with_counts()
.sorted_unstable_by_key(|(_, &cnt)| cnt)
.rev()
.take(max_addr as usize)
.map(|(addr, &freq)| {
format_address(addr, freq, hop, dns, geoip_lookup, config)
})
.join("\n");
let count = hop.addr_count().clamp(1, max_addr as usize);
(hostnames, count as u16)
}
}
}
} else {
Expand Down Expand Up @@ -334,8 +342,13 @@ fn render_hostname_with_details(
config: &TuiConfig,
) -> (Cell<'static>, u16) {
let (rendered, count) = if hop.total_recv() > 0 {
let index = app.selected_hop_address;
format_details(hop, index, dns, geoip_lookup, config)
if config.privacy_ttl >= hop.ttl() {
let height = if config.lookup_as_info { 6 } else { 3 };
(String::from("**Hidden**"), height)
} else {
let index = app.selected_hop_address;
format_details(hop, index, dns, geoip_lookup, config)
}
} else {
let height = if config.lookup_as_info { 6 } else { 3 };
(String::from("No response"), height)
Expand Down
24 changes: 14 additions & 10 deletions src/frontend/render/world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,16 +138,20 @@ fn render_map_info_panel(f: &mut Frame<'_>, app: &TuiApp, rect: Rect, entries: &
}
})
.collect::<Vec<_>>();
let info = match locations.as_slice() {
_ if app.tracer_config().geoip_mmdb_file.is_none() => "GeoIp not enabled".to_string(),
[] if selected_hop.addr_count() > 0 => format!(
"No GeoIp data for hop {} ({})",
selected_hop.ttl(),
selected_hop.addrs().join(", ")
),
[] => format!("No GeoIp data for hop {}", selected_hop.ttl()),
[loc] => loc.to_string(),
_ => format!("Multiple GeoIp locations for hop {}", selected_hop.ttl()),
let info = if app.tui_config.privacy_ttl >= selected_hop.ttl() {
"**Hidden**".to_string()
} else {
match locations.as_slice() {
_ if app.tracer_config().geoip_mmdb_file.is_none() => "GeoIp not enabled".to_string(),
[] if selected_hop.addr_count() > 0 => format!(
"No GeoIp data for hop {} ({})",
selected_hop.ttl(),
selected_hop.addrs().join(", ")
),
[] => format!("No GeoIp data for hop {}", selected_hop.ttl()),
[loc] => loc.to_string(),
_ => format!("Multiple GeoIp locations for hop {}", selected_hop.ttl()),
}
};
let info_panel = Paragraph::new(info)
.block(
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ fn make_trace_info(
fn make_tui_config(args: &TrippyConfig) -> TuiConfig {
TuiConfig::new(
args.tui_refresh_rate,
args.tui_privacy_ttl,
args.tui_preserve_screen,
args.tui_address_mode,
args.dns_lookup_as_info,
Expand Down
2 changes: 2 additions & 0 deletions trippy-config-sample.toml
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,8 @@ 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]
tuiprivacy-max-ttl = 0

# Tui color theme configure.
#
Expand Down

0 comments on commit 7e32969

Please sign in to comment.