diff --git a/wg-event-gen/src/model.rs b/wg-event-gen/src/model.rs index d7a0c63..4b40b8a 100644 --- a/wg-event-gen/src/model.rs +++ b/wg-event-gen/src/model.rs @@ -1,10 +1,11 @@ use base64::{decode, encode}; use std::error::Error; +use std::fmt; use std::hash::Hash; use std::io; use std::net::{IpAddr, SocketAddr}; -use std::time::Duration; use std::time::Instant; +use std::time::{Duration, SystemTime, UNIX_EPOCH}; const KEY_SIZE: usize = 48; //TODO: use VEC instead of array @@ -14,6 +15,12 @@ pub enum ECCKey { PrivateKey(Vec), } +impl fmt::Display for ECCKey { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{}", self.as_base64().unwrap()) + } +} + pub trait Base64Backed { fn from_bytes(bytes: Vec) -> Self; fn bytes(&self) -> &Vec; @@ -73,6 +80,12 @@ impl ECCKey { #[derive(Debug, PartialEq, Eq, Hash, Clone)] pub struct SharedKey(Vec); +impl fmt::Display for SharedKey { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{}", self.as_base64().unwrap()) + } +} + impl Base64Backed for SharedKey { fn bytes(&self) -> &Vec { &self.0 @@ -99,7 +112,7 @@ pub struct Peer { #[builder(default = "Vec::new()")] pub allowed_ips: Vec<(IpAddr, u8)>, #[builder(default = "None")] - pub last_handshake: Option, + pub last_handshake: Option, #[builder(default = "None")] pub persistent_keepalive: Option, #[builder(default = "(0u64,0u64)")] @@ -108,6 +121,24 @@ pub struct Peer { pub parsed: Instant, } +impl fmt::Display for Peer { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + fn dis_opt<'a, T: fmt::Display + 'a>(opt: &Option) -> String { + opt.as_ref().map(|s| s.to_string()).unwrap_or(" ".to_string()) + } + write!( + f, + "peer {} {}{}{}", + self.key, + dis_opt(&self.shared_key), + dis_opt(&self.endpoint), + self.allowed_ips.iter() + .map(|(ip, sub)| format!(" {}/{}", ip, sub)) + .collect::>().join(",") + ) + } +} + impl PeerBuilder { fn validate(&self) -> Result<(), String> { if let Some(ref key) = self.key { @@ -131,7 +162,7 @@ impl PeerBuilder { pub fn add_last_handshake(&mut self, d: Duration) { if !self.last_handshake.is_some() { - self.last_handshake = Some(Some(d)); + self.last_handshake = Some(Some(UNIX_EPOCH + d)); } else { self.last_handshake = self .last_handshake