Skip to content

Commit

Permalink
Merge pull request #19 from NordSecurity/fix_handshake_time_values
Browse files Browse the repository at this point in the history
Fix last_handshake_time values
  • Loading branch information
stalowyjez authored Jul 17, 2024
2 parents 79b5790 + 61b5561 commit 90a45a6
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
14 changes: 11 additions & 3 deletions boringtun/src/device/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,9 +199,17 @@ fn api_get<W: Write>(writer: &mut BufWriter<W>, d: &Device) -> i32 {
writeln!(writer, "allowed_ip={}/{}", addr, cidr);
}

if let Some(time) = p.time_since_last_handshake() {
writeln!(writer, "last_handshake_time_sec={}", time.as_secs());
writeln!(writer, "last_handshake_time_nsec={}", time.subsec_nanos());
if let Some(last_handshake_time) = p.last_handshake_time() {
writeln!(
writer,
"last_handshake_time_sec={}",
last_handshake_time.as_secs()
);
writeln!(
writer,
"last_handshake_time_nsec={}",
last_handshake_time.subsec_nanos()
);
}

let (_, tx_bytes, rx_bytes, ..) = p.tunnel.stats();
Expand Down
4 changes: 4 additions & 0 deletions boringtun/src/device/peer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,10 @@ impl Peer {
self.tunnel.time_since_last_handshake()
}

pub fn last_handshake_time(&self) -> Option<std::time::Duration> {
self.tunnel.last_handshake_time()
}

pub fn persistent_keepalive(&self) -> Option<u16> {
self.tunnel.persistent_keepalive()
}
Expand Down
10 changes: 10 additions & 0 deletions boringtun/src/noise/timers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use super::errors::WireGuardError;
use crate::noise::{safe_duration::SafeDuration as Duration, Tunn, TunnResult};
use std::mem;
use std::ops::{Index, IndexMut};
use std::time::SystemTime;

#[cfg(feature = "mock-instant")]
use mock_instant::Instant;
Expand Down Expand Up @@ -355,6 +356,15 @@ impl Tunn {
}
}

pub fn last_handshake_time(&self) -> Option<std::time::Duration> {
self.time_since_last_handshake().and_then(|d| {
SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
.ok()
.map(|t| t - d)
})
}

pub fn persistent_keepalive(&self) -> Option<u16> {
let keepalive = self.timers.persistent_keepalive;

Expand Down

0 comments on commit 90a45a6

Please sign in to comment.