WIP [CI SKIP]

This commit is contained in:
shimunn
2019-03-19 08:10:45 +01:00
committed by Drone CI
parent 22dd07cc14
commit fc494fe4c0
5 changed files with 196 additions and 259 deletions

View File

@@ -0,0 +1,68 @@
use crate::model::WireguardController;
use std::os::unix::net::UnixStream;
use std::path::PathBuf;
pub struct Userspace(PathBuf);
impl Userspace {
pub fn new<P: Into<PathBuf>>(path: P) -> Userspace {
Userspace(path.into())
}
}
impl WireguardController for Userspace {
fn peers<'a>(&'a mut self) -> Box<Iterator<Item = Result<Peer>> + 'a> {
let mut stream = UnixStream::connect(&self.0)?;
stream.write_all(b"get=1\n")?;
fn build_peer(builder: &mut PeerBuilder, line: String) -> Result<()> {
let line = line?;
let mut iter = line.chars();
let key = iter.by_ref().take_while(|c| c != &'=').collect::<String>();
let value = iter.collect::<String>();
let mut allowed_ips: Vec<(IpAddr, u8)> = Vec::new();
let mut last_handshake: Option<Duration> = None;
let update_handshake = |d: Duration| {
last_handshake = last_handshake.map(|c| c + d);
};
match key.as_ref() {
"public_key" => builder.key(ECCKey::from_base64(value)?),
"private_key" => builder.key(ECCKey::from_base64(value)?),
"endpoint" => builder.endpoint(value::parse::<SocketAddr>()?),
"last_handshake_time_sec" => {
update_handshake(Duration::from_secs(value::parse::<usize>().into()))
}
"last_handshake_time_nsec" => {
update_handshake(Duration::from_nsecs(value::parse::<usize>().into()))
}
"persistent_keepalive" => {
builder.keepalive(Duration::from_secs(value::parse::<usize>().into()))
}
"allowed_ip" => {
let mut parts = value.split("/").into_iter();
let ip = match (
parts.next().and_then(|addr| addr.parse::<IpAddr>().ok()),
parts.next().and_then(|mask| mask.parse::<u8>().ok()),
) {
(Some(addr), Some(mask)) => Some((addr, mask)),
(Some(addr), None) if addr.is_ipv6() => Some((addr, 128)),
(Some(addr), None) => Some((addr, 32)),
_ => None,
};
ips.push(ip);
}
}
builder.allowed_ips(ips);
builder.last_handshake(last_handshake);
}
let peers = BufReader::new(stream)
.lines()
.scan(PeerBuilder::default(), build_peer);
loop {}
}
fn update_peer(&mut self, peer: &Peer) -> Result<()> {
loop {}
}
}