basic strcuture [CI SKIP]

This commit is contained in:
Drone CI
2019-03-19 12:05:31 +01:00
parent fc494fe4c0
commit 7491902b34
3 changed files with 78 additions and 44 deletions

View File

@@ -1,47 +1,56 @@
#[macro_use]
extern crate structopt;
use base64::decode;
use std::error::Error;
use std::io;
use std::net::{IpAddr, SocketAddr};
use std::time::Duration;
use std::time::Instant;
#[macro_use]
extern crate derive_builder;
use std::io::Result;
use base64::{decode};
pub enum ECCKey{
PublicKey([u8; 32]),
PrivateKey([u8; 32])
#[derive(Debug, PartialEq, Eq, Hash, Copy, Clone)]
pub enum ECCKey {
PublicKey([u8; 32]),
PrivateKey([u8; 32]),
}
impl ECCKey {
fn from_base64<I: AsRef<str>>(key: I) -> Result<ECCKey> {
let key = decode(key.as_ref::<str>())?;
let bytes = [0; 32];
bytes.copy_from_slice(key);
ECCKey::PublicKey(bytes)
}
pub fn from_base64<I: AsRef<str>>(key: I) -> io::Result<ECCKey> {
let key = decode(key.as_ref()).map_err(|err| {
io::Error::new(io::ErrorKind::InvalidData, "Failed to decode base64".into())
})?;
let bytes = [0; 32];
if key.len() != 32 {
return Err(io::Error::new(
io::ErrorKind::Other,
"Mismatched key size".into(),
));
}
bytes.copy_from_slice(&key);
Ok(ECCKey::PublicKey(bytes))
}
}
#[derive(Debug, PartialEq, Eq, Hash, Copy, Clone)]
struct SharedKey([u8; 32]);
#[derive(Debug,Builder, PartialEq, Eq, Hash, Clone)]
#[derive(Debug, Builder, PartialEq, Eq, Hash, Clone)]
#[builder(public)]
pub struct Peer {
key: ECCKey,
#[builder(default = "None")]
shared_key: Option<SharedKey>,
#[builder(default = "None")]
endpoint: Option<SocketAddr>,
allowed_ips: Vec<(IpAddr, u8)>,
last_handshake: Option<Duration>,
#[builder(default = "None")]
persistent_keepalive: Option<Duration>,
#[builder(default = "(0u64,0u64)")]
traffic: (u64, u64),
parsed: time::Timespec,
#[builder(default = "Instant::now()")]
parsed: Instant,
}
pub trait WireguardController {
fn peers<'a>(&'a mut self) -> io::Result<Box<Iterator<Item = io::Result<Peer>> + 'a>>;
trait WireguardController {
fn peers<'a>(&'a mut self) -> Box<Iterator<Item=Result<Peer>> + 'a>;
fn update_peer(&mut self, peer: &Peer) -> Result<()>;
fn update_peer(&mut self, peer: &Peer) -> io::Result<()>;
}