replaced Duration with SystemTime where applicable, implemented Display

This commit is contained in:
Shimun 2019-03-30 23:44:09 +01:00
parent a6af494cb9
commit b72ca91423

View File

@ -1,10 +1,11 @@
use base64::{decode, encode}; use base64::{decode, encode};
use std::error::Error; use std::error::Error;
use std::fmt;
use std::hash::Hash; use std::hash::Hash;
use std::io; use std::io;
use std::net::{IpAddr, SocketAddr}; use std::net::{IpAddr, SocketAddr};
use std::time::Duration;
use std::time::Instant; use std::time::Instant;
use std::time::{Duration, SystemTime, UNIX_EPOCH};
const KEY_SIZE: usize = 48; //TODO: use VEC instead of array const KEY_SIZE: usize = 48; //TODO: use VEC instead of array
@ -14,6 +15,12 @@ pub enum ECCKey {
PrivateKey(Vec<u8>), PrivateKey(Vec<u8>),
} }
impl fmt::Display for ECCKey {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.as_base64().unwrap())
}
}
pub trait Base64Backed { pub trait Base64Backed {
fn from_bytes(bytes: Vec<u8>) -> Self; fn from_bytes(bytes: Vec<u8>) -> Self;
fn bytes(&self) -> &Vec<u8>; fn bytes(&self) -> &Vec<u8>;
@ -73,6 +80,12 @@ impl ECCKey {
#[derive(Debug, PartialEq, Eq, Hash, Clone)] #[derive(Debug, PartialEq, Eq, Hash, Clone)]
pub struct SharedKey(Vec<u8>); pub struct SharedKey(Vec<u8>);
impl fmt::Display for SharedKey {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.as_base64().unwrap())
}
}
impl Base64Backed for SharedKey { impl Base64Backed for SharedKey {
fn bytes(&self) -> &Vec<u8> { fn bytes(&self) -> &Vec<u8> {
&self.0 &self.0
@ -99,7 +112,7 @@ pub struct Peer {
#[builder(default = "Vec::new()")] #[builder(default = "Vec::new()")]
pub allowed_ips: Vec<(IpAddr, u8)>, pub allowed_ips: Vec<(IpAddr, u8)>,
#[builder(default = "None")] #[builder(default = "None")]
pub last_handshake: Option<Duration>, pub last_handshake: Option<SystemTime>,
#[builder(default = "None")] #[builder(default = "None")]
pub persistent_keepalive: Option<Duration>, pub persistent_keepalive: Option<Duration>,
#[builder(default = "(0u64,0u64)")] #[builder(default = "(0u64,0u64)")]
@ -108,6 +121,24 @@ pub struct Peer {
pub parsed: Instant, 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<T>) -> 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::<Vec<_>>().join(",")
)
}
}
impl PeerBuilder { impl PeerBuilder {
fn validate(&self) -> Result<(), String> { fn validate(&self) -> Result<(), String> {
if let Some(ref key) = self.key { if let Some(ref key) = self.key {
@ -131,7 +162,7 @@ impl PeerBuilder {
pub fn add_last_handshake(&mut self, d: Duration) { pub fn add_last_handshake(&mut self, d: Duration) {
if !self.last_handshake.is_some() { if !self.last_handshake.is_some() {
self.last_handshake = Some(Some(d)); self.last_handshake = Some(Some(UNIX_EPOCH + d));
} else { } else {
self.last_handshake = self self.last_handshake = self
.last_handshake .last_handshake