diff options
Diffstat (limited to 'bin')
| -rw-r--r-- | bin/wireguard-show.rs | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/bin/wireguard-show.rs b/bin/wireguard-show.rs new file mode 100644 index 0000000..17476d6 --- /dev/null +++ b/bin/wireguard-show.rs | |||
| @@ -0,0 +1,80 @@ | |||
| 1 | use anyhow::Result; | ||
| 2 | use wireguard::WireGuard; | ||
| 3 | |||
| 4 | #[tokio::main] | ||
| 5 | async fn main() -> Result<()> { | ||
| 6 | let mut wireguard = WireGuard::new().await?; | ||
| 7 | let devices = wireguard.view_devices().await?; | ||
| 8 | |||
| 9 | if devices.is_empty() { | ||
| 10 | println!("no wireguard devices found"); | ||
| 11 | return Ok(()); | ||
| 12 | } | ||
| 13 | |||
| 14 | for device in devices { | ||
| 15 | println!("device: {}", device.name); | ||
| 16 | println!(" ifindex: {}", device.ifindex); | ||
| 17 | println!(" private_key: {}", format_optional_key(device.private_key)); | ||
| 18 | println!(" public_key: {}", format_optional_key(device.public_key)); | ||
| 19 | println!(" listen_port: {}", device.listen_port); | ||
| 20 | println!(" fwmark: {}", device.fwmark); | ||
| 21 | println!(" peers: {}", device.peers.len()); | ||
| 22 | |||
| 23 | for (index, peer) in device.peers.iter().enumerate() { | ||
| 24 | println!(" peer {}:", index + 1); | ||
| 25 | println!(" public_key: {}", peer.public_key); | ||
| 26 | println!( | ||
| 27 | " preshared_key: {}", | ||
| 28 | format_optional_key(peer.preshared_key) | ||
| 29 | ); | ||
| 30 | println!( | ||
| 31 | " endpoint: {}", | ||
| 32 | peer.endpoint | ||
| 33 | .map(|endpoint| endpoint.to_string()) | ||
| 34 | .unwrap_or_else(|| "none".to_string()) | ||
| 35 | ); | ||
| 36 | println!( | ||
| 37 | " persistent_keepalive: {}", | ||
| 38 | peer.persistent_keepalive | ||
| 39 | .map(|keepalive| keepalive.to_string()) | ||
| 40 | .unwrap_or_else(|| "none".to_string()) | ||
| 41 | ); | ||
| 42 | println!( | ||
| 43 | " last_handshake: {}", | ||
| 44 | format_duration(peer.last_handshake) | ||
| 45 | ); | ||
| 46 | println!(" rx_bytes: {}", peer.rx_bytes); | ||
| 47 | println!(" tx_bytes: {}", peer.tx_bytes); | ||
| 48 | if peer.allowed_ips.is_empty() { | ||
| 49 | println!(" allowed_ips: none"); | ||
| 50 | } else { | ||
| 51 | let allowed_ips = peer | ||
| 52 | .allowed_ips | ||
| 53 | .iter() | ||
| 54 | .map(|ip| ip.to_string()) | ||
| 55 | .collect::<Vec<_>>() | ||
| 56 | .join(", "); | ||
| 57 | println!(" allowed_ips: {}", allowed_ips); | ||
| 58 | } | ||
| 59 | } | ||
| 60 | |||
| 61 | println!(); | ||
| 62 | } | ||
| 63 | |||
| 64 | Ok(()) | ||
| 65 | } | ||
| 66 | |||
| 67 | fn format_optional_key(key: Option<wireguard::Key>) -> String { | ||
| 68 | key.map(|value| value.to_string()) | ||
| 69 | .unwrap_or_else(|| "none".to_string()) | ||
| 70 | } | ||
| 71 | |||
| 72 | fn format_duration(time: Option<std::time::SystemTime>) -> String { | ||
| 73 | match time { | ||
| 74 | Some(time) => match time.duration_since(std::time::UNIX_EPOCH) { | ||
| 75 | Ok(duration) => format!("{}.{}", duration.as_secs(), duration.subsec_nanos()), | ||
| 76 | Err(_) => "before-epoch".to_string(), | ||
| 77 | }, | ||
| 78 | None => "none".to_string(), | ||
| 79 | } | ||
| 80 | } | ||
