diff options
| author | diogo464 <[email protected]> | 2026-02-17 14:47:15 +0000 |
|---|---|---|
| committer | diogo464 <[email protected]> | 2026-02-17 14:47:15 +0000 |
| commit | a0a5cb22ecf4c1760a3eadaea5ed2c0a55aa62c7 (patch) | |
| tree | 880a2ca8992d4e621e5531b37f9a79afed9c93fc /src/view.rs | |
| parent | 0c6a34024786d6117c238a0164218a4e718178f0 (diff) | |
add wireguard-show binary and optional handshake timestamps
Diffstat (limited to 'src/view.rs')
| -rw-r--r-- | src/view.rs | 33 |
1 files changed, 23 insertions, 10 deletions
diff --git a/src/view.rs b/src/view.rs index c0bd807..666f5d4 100644 --- a/src/view.rs +++ b/src/view.rs | |||
| @@ -28,7 +28,7 @@ pub struct PeerView { | |||
| 28 | pub preshared_key: Option<Key>, | 28 | pub preshared_key: Option<Key>, |
| 29 | pub endpoint: Option<SocketAddr>, | 29 | pub endpoint: Option<SocketAddr>, |
| 30 | pub persistent_keepalive: Option<u16>, | 30 | pub persistent_keepalive: Option<u16>, |
| 31 | pub last_handshake: SystemTime, | 31 | pub last_handshake: Option<SystemTime>, |
| 32 | pub rx_bytes: u64, | 32 | pub rx_bytes: u64, |
| 33 | pub tx_bytes: u64, | 33 | pub tx_bytes: u64, |
| 34 | pub allowed_ips: Vec<IpNet>, | 34 | pub allowed_ips: Vec<IpNet>, |
| @@ -103,20 +103,33 @@ fn peer_from_wg_peer(wg_peer: WireguardPeer) -> Result<PeerView> { | |||
| 103 | } | 103 | } |
| 104 | } | 104 | } |
| 105 | 105 | ||
| 106 | let last_handshake = last_handshake.ok_or_else(|| Error::message("missing last_handshake"))?; | ||
| 107 | let rx_bytes = rx_bytes.ok_or_else(|| Error::message("missing rx_bytes"))?; | ||
| 108 | let tx_bytes = tx_bytes.ok_or_else(|| Error::message("missing tx_bytes"))?; | ||
| 109 | |||
| 110 | let handshake_is_zero = last_handshake.seconds == 0 && last_handshake.nano_seconds == 0; | ||
| 111 | let last_handshake = if handshake_is_zero && (rx_bytes == 0 || tx_bytes == 0) { | ||
| 112 | None | ||
| 113 | } else { | ||
| 114 | let duration = Duration::new( | ||
| 115 | last_handshake.seconds as u64, | ||
| 116 | last_handshake.nano_seconds as u32, | ||
| 117 | ); | ||
| 118 | Some( | ||
| 119 | SystemTime::UNIX_EPOCH | ||
| 120 | .checked_add(duration) | ||
| 121 | .ok_or_else(|| Error::message("invalid last_handshake"))?, | ||
| 122 | ) | ||
| 123 | }; | ||
| 124 | |||
| 106 | Ok(PeerView { | 125 | Ok(PeerView { |
| 107 | public_key: public_key.ok_or_else(|| Error::message("missing public_key"))?, | 126 | public_key: public_key.ok_or_else(|| Error::message("missing public_key"))?, |
| 108 | preshared_key, | 127 | preshared_key, |
| 109 | endpoint, | 128 | endpoint, |
| 110 | persistent_keepalive, | 129 | persistent_keepalive, |
| 111 | last_handshake: last_handshake | 130 | last_handshake, |
| 112 | .map(|ts| { | 131 | rx_bytes, |
| 113 | SystemTime::UNIX_EPOCH | 132 | tx_bytes, |
| 114 | .checked_add(Duration::new(ts.seconds as u64, ts.nano_seconds as u32)) | ||
| 115 | .unwrap() | ||
| 116 | }) | ||
| 117 | .ok_or_else(|| Error::message("missing last_handshake"))?, | ||
| 118 | rx_bytes: rx_bytes.ok_or_else(|| Error::message("missing rx_bytes"))?, | ||
| 119 | tx_bytes: tx_bytes.ok_or_else(|| Error::message("missing tx_bytes"))?, | ||
| 120 | allowed_ips, | 133 | allowed_ips, |
| 121 | }) | 134 | }) |
| 122 | } | 135 | } |
