2026-10-02 15:37:08 +01:00
|
|
|
//! The per-network devices window: full detail about one network — its
|
2026-10-02 16:09:33 +01:00
|
|
|
//! settings and counters, and one table of every device, online first, with
|
|
|
|
|
//! inline traffic plots for the connected ones.
|
2026-10-02 15:37:08 +01:00
|
|
|
//!
|
|
|
|
|
//! Rendered in a separate OS window (an egui viewport) opened from a tile in
|
|
|
|
|
//! the main window.
|
|
|
|
|
|
|
|
|
|
use std::collections::HashMap;
|
|
|
|
|
|
|
|
|
|
use eframe::egui;
|
|
|
|
|
|
2026-10-02 16:09:33 +01:00
|
|
|
use tsunagi::ipc::{NetworkReport, OverlayPeerReport};
|
2026-10-02 15:37:08 +01:00
|
|
|
|
|
|
|
|
use crate::agent::AgentClient;
|
|
|
|
|
use crate::format;
|
|
|
|
|
use crate::stats::{Traffic, Unit};
|
|
|
|
|
|
|
|
|
|
/// A readable title for a network's window.
|
|
|
|
|
pub(crate) fn title(network: &NetworkReport) -> String {
|
|
|
|
|
format!("tsunagi · {}", network.name)
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-02 16:09:33 +01:00
|
|
|
/// One row of the device table, built from a tunnel and/or a signed member.
|
|
|
|
|
struct Device<'a> {
|
|
|
|
|
online: bool,
|
2026-10-02 19:23:06 +01:00
|
|
|
/// The last hostname seen for it, kept while it is offline.
|
|
|
|
|
hostname: Option<&'a str>,
|
|
|
|
|
/// What clicking the hostname copies: `hostname.network`, so it resolves.
|
|
|
|
|
copy: Option<String>,
|
|
|
|
|
/// The full endpoint id, which clicking the short form copies.
|
|
|
|
|
id: &'a str,
|
2026-10-02 16:09:33 +01:00
|
|
|
proto: &'a str,
|
|
|
|
|
address: Option<String>,
|
|
|
|
|
handshake_secs: Option<u64>,
|
|
|
|
|
overlay: Option<&'a OverlayPeerReport>,
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-02 19:23:06 +01:00
|
|
|
impl Device<'_> {
|
|
|
|
|
fn sort_key(&self) -> String {
|
|
|
|
|
self.hostname.unwrap_or(self.id).to_lowercase()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// The first characters of an endpoint id, enough to tell devices apart.
|
|
|
|
|
fn short_id(id: &str) -> String {
|
|
|
|
|
id.chars().take(5).collect()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// The device cell: the hostname and, in brackets, the start of the id.
|
|
|
|
|
/// Clicking either copies its own full value.
|
|
|
|
|
fn name_cell(ui: &mut egui::Ui, device: &Device<'_>) {
|
|
|
|
|
ui.horizontal(|ui| {
|
|
|
|
|
ui.spacing_mut().item_spacing.x = 4.0;
|
|
|
|
|
match (device.hostname, &device.copy) {
|
|
|
|
|
(Some(hostname), Some(copy)) => {
|
|
|
|
|
format::copy_field(ui, hostname, copy);
|
|
|
|
|
format::copy_field(ui, &format!("({})", short_id(device.id)), device.id);
|
|
|
|
|
}
|
|
|
|
|
_ => format::copy_field(ui, &short_id(device.id), device.id),
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-02 15:37:08 +01:00
|
|
|
/// Draws the window body into its viewport.
|
|
|
|
|
pub(crate) fn show(
|
|
|
|
|
ctx: &egui::Context,
|
|
|
|
|
_agent: &AgentClient,
|
2026-10-02 16:04:39 +01:00
|
|
|
unit: &mut Unit,
|
2026-10-02 15:37:08 +01:00
|
|
|
network: &NetworkReport,
|
2026-10-02 19:23:06 +01:00
|
|
|
own_id: &str,
|
2026-10-02 15:37:08 +01:00
|
|
|
traffic: &Traffic,
|
|
|
|
|
) {
|
|
|
|
|
egui::CentralPanel::default().show(ctx, |ui| {
|
2026-10-02 16:04:39 +01:00
|
|
|
ui.horizontal(|ui| {
|
|
|
|
|
ui.heading(&network.name);
|
|
|
|
|
ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
|
|
|
|
|
ui.selectable_value(unit, Unit::Bytes, "bytes");
|
|
|
|
|
ui.selectable_value(unit, Unit::Packets, "pkts");
|
|
|
|
|
ui.label("show:");
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
// A copy of the chosen unit for the read-only rendering below.
|
|
|
|
|
let unit = *unit;
|
2026-10-02 16:09:33 +01:00
|
|
|
|
2026-10-02 15:37:08 +01:00
|
|
|
summary(ui, network);
|
|
|
|
|
ui.add_space(6.0);
|
|
|
|
|
|
|
|
|
|
ui.label(egui::RichText::new("traffic").strong());
|
|
|
|
|
format::sparkline(ui, traffic.network(&network.network_id), unit, 48.0);
|
|
|
|
|
ui.add_space(8.0);
|
|
|
|
|
|
2026-10-02 19:23:06 +01:00
|
|
|
let devices = devices(network, own_id);
|
2026-10-02 16:09:33 +01:00
|
|
|
let online = devices.iter().filter(|d| d.online).count();
|
|
|
|
|
ui.label(
|
|
|
|
|
egui::RichText::new(format!(
|
|
|
|
|
"devices ({} online / {} known)",
|
|
|
|
|
online,
|
|
|
|
|
devices.len()
|
|
|
|
|
))
|
|
|
|
|
.strong(),
|
|
|
|
|
);
|
2026-10-02 15:37:08 +01:00
|
|
|
egui::ScrollArea::vertical()
|
|
|
|
|
.auto_shrink([false, false])
|
|
|
|
|
.show(ui, |ui| {
|
2026-10-02 16:09:33 +01:00
|
|
|
device_table(ui, unit, &network.network_id, &devices, traffic);
|
2026-10-02 15:37:08 +01:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// The network's own settings and counters.
|
|
|
|
|
fn summary(ui: &mut egui::Ui, network: &NetworkReport) {
|
|
|
|
|
egui::Grid::new("summary")
|
|
|
|
|
.num_columns(2)
|
|
|
|
|
.spacing([12.0, 3.0])
|
|
|
|
|
.show(ui, |ui| {
|
|
|
|
|
ui.weak("id");
|
2026-10-02 16:00:23 +01:00
|
|
|
format::copy_field(ui, &network.network_id, &network.network_id);
|
2026-10-02 15:37:08 +01:00
|
|
|
ui.end_row();
|
|
|
|
|
|
|
|
|
|
ui.weak("active");
|
|
|
|
|
ui.label(yes_no(network.active));
|
|
|
|
|
ui.end_row();
|
|
|
|
|
ui.weak("broadcast");
|
|
|
|
|
ui.label(yes_no(network.broadcast));
|
|
|
|
|
ui.end_row();
|
|
|
|
|
|
|
|
|
|
if let Some(range) = &network.range {
|
|
|
|
|
ui.weak("range");
|
2026-10-02 16:00:23 +01:00
|
|
|
format::copy_field(ui, range, range);
|
2026-10-02 15:37:08 +01:00
|
|
|
ui.end_row();
|
|
|
|
|
}
|
|
|
|
|
if let Some(address) = network.overlay.as_ref().and_then(|o| o.address.as_ref()) {
|
|
|
|
|
ui.weak("this device");
|
2026-10-02 16:00:23 +01:00
|
|
|
format::copy_field(ui, address, address);
|
2026-10-02 15:37:08 +01:00
|
|
|
ui.end_row();
|
|
|
|
|
}
|
|
|
|
|
ui.weak("candidates");
|
|
|
|
|
ui.label(network.candidates.to_string());
|
|
|
|
|
ui.end_row();
|
|
|
|
|
ui.weak("relay fwd/via/recv");
|
|
|
|
|
ui.label(format!(
|
|
|
|
|
"{} / {} / {}",
|
|
|
|
|
network.relay_forwarded, network.relay_sent_via, network.relay_received_via
|
|
|
|
|
));
|
|
|
|
|
ui.end_row();
|
|
|
|
|
ui.weak("dial/handshake fails");
|
|
|
|
|
ui.label(format!(
|
|
|
|
|
"{} / {}",
|
|
|
|
|
network.dial_failures, network.handshake_failures
|
|
|
|
|
));
|
|
|
|
|
ui.end_row();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-02 19:23:06 +01:00
|
|
|
/// Every other device this network knows of, from a live tunnel or the signed
|
|
|
|
|
/// membership. This device is in the roster too but is not one of the others.
|
|
|
|
|
fn known_ids<'a>(network: &'a NetworkReport, own_id: &str) -> Vec<&'a str> {
|
|
|
|
|
let overlay_peers = network
|
|
|
|
|
.overlay
|
|
|
|
|
.as_ref()
|
|
|
|
|
.map(|o| o.peers.as_slice())
|
|
|
|
|
.unwrap_or(&[]);
|
|
|
|
|
let mut ids: Vec<&str> = Vec::new();
|
|
|
|
|
let candidates = overlay_peers
|
|
|
|
|
.iter()
|
|
|
|
|
.map(|p| p.endpoint_id.as_str())
|
|
|
|
|
.chain(network.peers.iter().map(|p| p.endpoint_id.as_str()))
|
|
|
|
|
.chain(network.members.iter().map(|m| m.endpoint_id.as_str()));
|
|
|
|
|
for id in candidates {
|
|
|
|
|
if id != own_id && !ids.contains(&id) {
|
|
|
|
|
ids.push(id);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
ids
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// How many other devices this network knows of, connected or not.
|
|
|
|
|
pub(crate) fn known_count(network: &NetworkReport, own_id: &str) -> usize {
|
|
|
|
|
known_ids(network, own_id).len()
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-02 16:09:33 +01:00
|
|
|
/// Builds the device list: every endpoint known from a live tunnel or the
|
|
|
|
|
/// signed membership, online ones first.
|
2026-10-02 19:23:06 +01:00
|
|
|
fn devices<'a>(network: &'a NetworkReport, own_id: &str) -> Vec<Device<'a>> {
|
|
|
|
|
// The signed members carry the last name seen, so a device that is away
|
|
|
|
|
// is still called by it; a live announcement is fresher and wins.
|
|
|
|
|
let mut hostnames: HashMap<&str, &str> = network
|
|
|
|
|
.members
|
2026-10-02 16:09:33 +01:00
|
|
|
.iter()
|
2026-10-02 19:23:06 +01:00
|
|
|
.filter_map(|m| m.hostname.as_deref().map(|h| (m.endpoint_id.as_str(), h)))
|
2026-10-02 16:09:33 +01:00
|
|
|
.collect();
|
2026-10-02 19:23:06 +01:00
|
|
|
hostnames.extend(
|
|
|
|
|
network
|
|
|
|
|
.peers
|
|
|
|
|
.iter()
|
|
|
|
|
.filter_map(|p| p.hostname.as_deref().map(|h| (p.endpoint_id.as_str(), h))),
|
|
|
|
|
);
|
2026-10-02 16:09:33 +01:00
|
|
|
let overlay_peers = network
|
|
|
|
|
.overlay
|
|
|
|
|
.as_ref()
|
|
|
|
|
.map(|o| o.peers.as_slice())
|
|
|
|
|
.unwrap_or(&[]);
|
|
|
|
|
|
2026-10-02 19:23:06 +01:00
|
|
|
let ids = known_ids(network, own_id);
|
2026-10-02 16:09:33 +01:00
|
|
|
|
|
|
|
|
let mut devices: Vec<Device> = ids
|
|
|
|
|
.into_iter()
|
|
|
|
|
.map(|id| {
|
|
|
|
|
let overlay = overlay_peers.iter().find(|p| p.endpoint_id == id);
|
|
|
|
|
let member = network.members.iter().find(|m| m.endpoint_id == id);
|
|
|
|
|
let online = overlay.is_some_and(|p| p.handshake_secs_ago.is_some());
|
|
|
|
|
let hostname = hostnames.get(id).copied();
|
|
|
|
|
Device {
|
|
|
|
|
online,
|
2026-10-02 19:23:06 +01:00
|
|
|
hostname,
|
|
|
|
|
copy: hostname.map(|h| format!("{h}.{}", network.name)),
|
|
|
|
|
id,
|
2026-10-02 16:09:33 +01:00
|
|
|
proto: overlay.map_or("—", |p| p.protocol.as_str()),
|
|
|
|
|
address: overlay
|
|
|
|
|
.and_then(|p| p.address.clone())
|
|
|
|
|
.or_else(|| member.and_then(|m| m.overlay_address_v4.clone())),
|
|
|
|
|
handshake_secs: overlay.and_then(|p| p.handshake_secs_ago),
|
|
|
|
|
overlay,
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
|
|
devices.sort_by(|a, b| {
|
|
|
|
|
b.online
|
|
|
|
|
.cmp(&a.online)
|
2026-10-02 19:23:06 +01:00
|
|
|
.then_with(|| a.sort_key().cmp(&b.sort_key()))
|
2026-10-02 16:09:33 +01:00
|
|
|
});
|
|
|
|
|
devices
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// One table of all devices: online rows carry live counters and a plot,
|
|
|
|
|
/// offline rows show what is known (name and address) and dashes.
|
|
|
|
|
fn device_table(
|
2026-10-02 15:37:08 +01:00
|
|
|
ui: &mut egui::Ui,
|
|
|
|
|
unit: Unit,
|
|
|
|
|
network_id: &str,
|
2026-10-02 16:09:33 +01:00
|
|
|
devices: &[Device<'_>],
|
2026-10-02 15:37:08 +01:00
|
|
|
traffic: &Traffic,
|
|
|
|
|
) {
|
2026-10-02 16:09:33 +01:00
|
|
|
if devices.is_empty() {
|
2026-10-02 15:37:08 +01:00
|
|
|
ui.weak("none");
|
|
|
|
|
return;
|
|
|
|
|
}
|
2026-10-02 16:09:33 +01:00
|
|
|
egui::Grid::new("devices")
|
2026-10-02 16:04:39 +01:00
|
|
|
.num_columns(8)
|
2026-10-02 15:37:08 +01:00
|
|
|
.striped(true)
|
|
|
|
|
.spacing([10.0, 4.0])
|
|
|
|
|
.show(ui, |ui| {
|
|
|
|
|
for header in [
|
2026-10-02 16:09:33 +01:00
|
|
|
"device",
|
2026-10-02 15:37:08 +01:00
|
|
|
"proto",
|
|
|
|
|
"address",
|
|
|
|
|
"handshake",
|
|
|
|
|
"pkts",
|
|
|
|
|
"bytes",
|
2026-10-02 16:04:39 +01:00
|
|
|
"rate",
|
|
|
|
|
"plot",
|
2026-10-02 15:37:08 +01:00
|
|
|
] {
|
|
|
|
|
ui.weak(header);
|
|
|
|
|
}
|
|
|
|
|
ui.end_row();
|
|
|
|
|
|
2026-10-02 16:09:33 +01:00
|
|
|
for device in devices {
|
2026-10-02 19:23:06 +01:00
|
|
|
name_cell(ui, device);
|
2026-10-02 16:09:33 +01:00
|
|
|
ui.label(device.proto);
|
|
|
|
|
match &device.address {
|
2026-10-02 16:00:23 +01:00
|
|
|
Some(address) => format::copy_field(ui, address, address),
|
2026-10-02 15:37:08 +01:00
|
|
|
None => {
|
|
|
|
|
ui.weak("—");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
ui.label(
|
2026-10-02 16:09:33 +01:00
|
|
|
device
|
|
|
|
|
.handshake_secs
|
|
|
|
|
.map_or_else(|| "offline".to_string(), format::age),
|
2026-10-02 15:37:08 +01:00
|
|
|
);
|
|
|
|
|
|
2026-10-02 16:09:33 +01:00
|
|
|
match device.overlay.filter(|_| device.online) {
|
|
|
|
|
Some(peer) => {
|
|
|
|
|
ui.label(format!("{}/{}", peer.tx_packets, peer.rx_packets));
|
|
|
|
|
ui.label(format!(
|
|
|
|
|
"{} / {}",
|
|
|
|
|
format::bytes(peer.tx_bytes as f64),
|
|
|
|
|
format::bytes(peer.rx_bytes as f64)
|
|
|
|
|
));
|
|
|
|
|
let series = traffic.peer(network_id, &peer.public_key);
|
|
|
|
|
ui.add(egui::Label::new(format::series_rate(series, unit)).truncate());
|
|
|
|
|
ui.allocate_ui(egui::vec2(100.0, 20.0), |ui| {
|
|
|
|
|
format::sparkline(ui, series, unit, 18.0);
|
|
|
|
|
});
|
|
|
|
|
}
|
2026-10-02 16:00:23 +01:00
|
|
|
None => {
|
2026-10-02 16:09:33 +01:00
|
|
|
for _ in 0..4 {
|
|
|
|
|
ui.weak("—");
|
|
|
|
|
}
|
2026-10-02 16:00:23 +01:00
|
|
|
}
|
|
|
|
|
}
|
2026-10-02 15:37:08 +01:00
|
|
|
ui.end_row();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn yes_no(value: bool) -> &'static str {
|
|
|
|
|
if value { "yes" } else { "no" }
|
|
|
|
|
}
|