trippy-tui 0.13.0

A network diagnostic tool
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
use crate::app::TraceInfo;
use crate::frontend::config::TuiConfig;
use crate::frontend::render::settings::{settings_tabs, SETTINGS_TAB_COLUMNS};
use crate::geoip::GeoIpLookup;
use itertools::Itertools;
use ratatui::widgets::TableState;
use std::time::SystemTime;
use trippy_core::FlowId;
use trippy_core::Hop;
use trippy_core::State;
use trippy_dns::{DnsResolver, ResolveMethod};

pub struct TuiApp {
    pub selected_tracer_data: State,
    pub trace_info: Vec<TraceInfo>,
    pub tui_config: TuiConfig,
    /// The state of the hop table.
    pub table_state: TableState,
    /// The state of the settings table.
    pub setting_table_state: TableState,
    /// The selected trace.
    pub trace_selected: usize,
    /// The selected tab in the settings dialog.
    pub settings_tab_selected: usize,
    /// The index of the current address to show for the selected hop.
    ///
    /// Only used in detail mode.
    pub selected_hop_address: usize,
    /// The `FlowId` of the selected flow.
    ///
    /// FlowId(0) represents the unified flow for the trace.
    pub selected_flow: FlowId,
    /// Ordered flow ids with counts.
    pub flow_counts: Vec<(FlowId, usize)>,
    pub resolver: DnsResolver,
    pub geoip_lookup: GeoIpLookup,
    pub show_help: bool,
    pub show_settings: bool,
    pub show_hop_details: bool,
    pub show_flows: bool,
    pub show_chart: bool,
    pub show_map: bool,
    pub frozen_start: Option<SystemTime>,
    pub zoom_factor: usize,
}

impl TuiApp {
    pub fn new(
        tui_config: TuiConfig,
        resolver: DnsResolver,
        geoip_lookup: GeoIpLookup,
        trace_info: Vec<TraceInfo>,
    ) -> Self {
        Self {
            selected_tracer_data: State::default(),
            trace_info,
            tui_config,
            table_state: TableState::default(),
            setting_table_state: TableState::default(),
            trace_selected: 0,
            settings_tab_selected: 0,
            selected_hop_address: 0,
            selected_flow: State::default_flow_id(),
            flow_counts: vec![],
            resolver,
            geoip_lookup,
            show_help: false,
            show_settings: false,
            show_hop_details: false,
            show_flows: false,
            show_chart: false,
            show_map: false,
            frozen_start: None,
            zoom_factor: 1,
        }
    }

    pub const fn tracer_data(&self) -> &State {
        &self.selected_tracer_data
    }

    pub fn snapshot_trace_data(&mut self) {
        self.selected_tracer_data = self.trace_info[self.trace_selected].data.snapshot();
    }

    pub fn clear_trace_data(&self) {
        self.trace_info[self.trace_selected].data.clear();
    }

    pub fn selected_hop_or_target(&self) -> &Hop {
        self.table_state.selected().map_or_else(
            || self.tracer_data().target_hop(self.selected_flow),
            |s| &self.tracer_data().hops_for_flow(self.selected_flow)[s],
        )
    }

    pub fn selected_hop(&self) -> Option<&Hop> {
        self.table_state
            .selected()
            .map(|s| &self.tracer_data().hops_for_flow(self.selected_flow)[s])
    }

    pub fn tracer_config(&self) -> &TraceInfo {
        &self.trace_info[self.trace_selected]
    }

    pub fn clamp_selected_hop(&mut self) {
        let hop_count = self.tracer_data().hops_for_flow(self.selected_flow).len();
        if let Some(selected) = self.table_state.selected() {
            if selected > hop_count - 1 {
                self.table_state.select(Some(hop_count - 1));
            }
        }
    }

    pub fn update_order_flow_counts(&mut self) {
        pub fn order_flows(
            &(flow_id1, count1): &(FlowId, usize),
            &(flow_id2, count2): &(FlowId, usize),
        ) -> std::cmp::Ordering {
            match count1.cmp(&count2) {
                std::cmp::Ordering::Equal => flow_id2.cmp(&flow_id1),
                ord => ord,
            }
        }
        self.flow_counts = self
            .tracer_data()
            .flows()
            .iter()
            .map(|&(_, flow_id)| {
                let count = self.tracer_data().round_count(flow_id);
                (flow_id, count)
            })
            .sorted_by(order_flows)
            .rev()
            .take(self.selected_tracer_data.max_flows())
            .collect::<Vec<_>>();
    }

    pub fn next_hop(&mut self) {
        let hop_count = self.tracer_data().hops_for_flow(self.selected_flow).len();
        if hop_count == 0 {
            return;
        }
        let max_index = 0.max(hop_count.saturating_sub(1));
        let i = match self.table_state.selected() {
            Some(i) => {
                if i < max_index {
                    i + 1
                } else {
                    i
                }
            }
            None => 0,
        };
        self.table_state.select(Some(i));
        self.selected_hop_address = 0;
    }

    pub fn previous_hop(&mut self) {
        let hop_count = self.tracer_data().hops_for_flow(self.selected_flow).len();
        if hop_count == 0 {
            return;
        }
        let i = match self.table_state.selected() {
            Some(i) => {
                if i > 0 {
                    i - 1
                } else {
                    i
                }
            }
            None => 0.max(hop_count.saturating_sub(1)),
        };
        self.table_state.select(Some(i));
        self.selected_hop_address = 0;
    }

    pub fn next_trace(&mut self) {
        if self.trace_info.len() > 1 && self.trace_selected < self.trace_info.len() - 1 {
            self.trace_selected += 1;
            self.clear();
        }
    }

    pub fn previous_trace(&mut self) {
        if self.trace_info.len() > 1 && self.trace_selected > 0 {
            self.trace_selected -= 1;
            self.clear();
        }
    }

    pub fn next_hop_address(&mut self) {
        if let Some(hop) = self.selected_hop() {
            if self.selected_hop_address < hop.addr_count() - 1 {
                self.selected_hop_address += 1;
            }
        }
    }

    pub fn previous_hop_address(&mut self) {
        if self.selected_hop().is_some() && self.selected_hop_address > 0 {
            self.selected_hop_address -= 1;
        }
    }

    pub fn flow_count(&self) -> usize {
        self.selected_tracer_data.flows().len()
    }

    pub fn next_flow(&mut self) {
        if self.show_flows {
            let (cur_index, _) = self
                .flow_counts
                .iter()
                .find_position(|(flow_id, _)| *flow_id == self.selected_flow)
                .unwrap();
            if cur_index < self.flow_counts.len() - 1 {
                self.selected_flow = self.flow_counts[cur_index + 1].0;
            }
        }
    }

    pub fn previous_flow(&mut self) {
        if self.show_flows {
            let (cur_index, _) = self
                .flow_counts
                .iter()
                .find_position(|(flow_id, _)| *flow_id == self.selected_flow)
                .unwrap();
            if cur_index > 0 {
                self.selected_flow = self.flow_counts[cur_index - 1].0;
            }
        }
    }

    pub fn next_settings_tab(&mut self) {
        if self.settings_tab_selected < settings_tabs().len() - 1 {
            self.settings_tab_selected += 1;
        }
        self.setting_table_state.select(Some(0));
    }

    pub fn previous_settings_tab(&mut self) {
        if self.settings_tab_selected > 0 {
            self.settings_tab_selected -= 1;
        }
        self.setting_table_state.select(Some(0));
    }

    pub fn next_settings_item(&mut self) {
        let count = self.get_settings_items_count();
        let max_index = 0.max(count.saturating_sub(1));
        let i = match self.setting_table_state.selected() {
            Some(i) => {
                if i < max_index {
                    i + 1
                } else {
                    i
                }
            }
            None => 0,
        };
        self.setting_table_state.select(Some(i));
    }

    pub fn previous_settings_item(&mut self) {
        let count = self.get_settings_items_count();
        let i = match self.setting_table_state.selected() {
            Some(i) => {
                if i > 0 {
                    i - 1
                } else {
                    i
                }
            }
            None => 0.max(count.saturating_sub(1)),
        };
        self.setting_table_state.select(Some(i));
    }

    fn get_settings_items_count(&self) -> usize {
        if self.settings_tab_selected == SETTINGS_TAB_COLUMNS {
            self.tui_config.tui_columns.all_columns_count()
        } else {
            settings_tabs()[self.settings_tab_selected].1
        }
    }

    pub fn toggle_column_visibility(&mut self) {
        if self.settings_tab_selected == SETTINGS_TAB_COLUMNS {
            if let Some(selected) = self.setting_table_state.selected() {
                self.tui_config.tui_columns.toggle(selected);
            }
        }
    }

    pub fn move_column_down(&mut self) {
        if self.settings_tab_selected == SETTINGS_TAB_COLUMNS {
            let count = self.tui_config.tui_columns.all_columns_count();
            if let Some(selected) = self.setting_table_state.selected() {
                if selected < count - 1 {
                    self.tui_config.tui_columns.move_down(selected);
                    self.setting_table_state.select(Some(selected + 1));
                }
            }
        }
    }

    pub fn move_column_up(&mut self) {
        if self.settings_tab_selected == SETTINGS_TAB_COLUMNS {
            if let Some(selected) = self.setting_table_state.selected() {
                if selected > 0 {
                    self.tui_config.tui_columns.move_up(selected);
                    self.setting_table_state.select(Some(selected - 1));
                }
            }
        }
    }

    pub fn clear(&mut self) {
        self.table_state.select(None);
        self.selected_hop_address = 0;
    }

    pub fn toggle_help(&mut self) {
        self.show_help = !self.show_help;
    }

    pub fn toggle_settings(&mut self) {
        self.show_settings = !self.show_settings;
    }

    pub fn show_settings_columns(&mut self, column_index: usize) {
        self.show_settings = true;
        if self.settings_tab_selected != column_index {
            self.settings_tab_selected = column_index;
            self.setting_table_state.select(Some(0));
        }
    }

    pub fn toggle_hop_details(&mut self) {
        if self.show_hop_details {
            self.tui_config.max_addrs = None;
        } else {
            self.tui_config.max_addrs = Some(1);
        }
        self.show_hop_details = !self.show_hop_details;
    }

    pub fn toggle_freeze(&mut self) {
        self.frozen_start = match self.frozen_start {
            None => Some(SystemTime::now()),
            Some(_) => None,
        };
    }

    pub fn toggle_chart(&mut self) {
        self.show_chart = !self.show_chart;
        self.show_map = false;
    }

    pub fn toggle_map(&mut self) {
        self.show_map = !self.show_map;
        self.show_chart = false;
    }

    pub fn toggle_flows(&mut self) {
        if self.trace_info.len() == 1 && self.selected_tracer_data.max_flows() > 1 {
            if self.show_flows {
                self.selected_flow = FlowId(0);
                self.show_flows = false;
                self.selected_hop_address = 0;
            } else if self.flow_count() > 0 {
                self.selected_flow = FlowId(1);
                self.show_flows = true;
                self.selected_hop_address = 0;
            }
        }
    }

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

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

    pub fn toggle_asinfo(&mut self) {
        match self.resolver.config().resolve_method {
            ResolveMethod::Resolv | ResolveMethod::Google | ResolveMethod::Cloudflare => {
                self.tui_config.lookup_as_info = !self.tui_config.lookup_as_info;
                self.resolver.flush();
            }
            ResolveMethod::System => {}
        }
    }

    pub fn expand_hosts(&mut self) {
        self.tui_config.max_addrs = match self.tui_config.max_addrs {
            None => Some(1),
            Some(i) if Some(i) < self.max_hosts() => Some(i + 1),
            Some(i) => Some(i),
        }
    }

    pub fn contract_hosts(&mut self) {
        self.tui_config.max_addrs = match self.tui_config.max_addrs {
            Some(i) if i > 1 => Some(i - 1),
            _ => None,
        }
    }

    pub fn zoom_in(&mut self) {
        if self.zoom_factor < MAX_ZOOM_FACTOR {
            self.zoom_factor += 1;
        }
    }

    pub fn zoom_out(&mut self) {
        if self.zoom_factor > 1 {
            self.zoom_factor -= 1;
        }
    }

    pub fn expand_hosts_max(&mut self) {
        self.tui_config.max_addrs = self.max_hosts();
    }

    pub fn contract_hosts_min(&mut self) {
        self.tui_config.max_addrs = Some(1);
    }

    /// The maximum number of hosts per hop for the currently selected trace.
    pub fn max_hosts(&self) -> Option<u8> {
        self.selected_tracer_data
            .hops_for_flow(self.selected_flow)
            .iter()
            .map(|h| h.addrs().count())
            .max()
            .and_then(|i| u8::try_from(i).ok())
    }
}

const MAX_ZOOM_FACTOR: usize = 16;