From 34e04ae7262ca5edc155856d8c6aa45c514ade21 Mon Sep 17 00:00:00 2001 From: shimunn <> Date: Thu, 17 Jan 2019 23:56:19 +0100 Subject: [PATCH] parse nanos as nanos --- wg-event-gen/Cargo.lock | 46 +++++++++++++++++++++++++++++++++++++ wg-event-gen/Cargo.toml | 3 ++- wg-event-gen/src/main.rs | 49 ++++++++++++++++++++++++++++++---------- 3 files changed, 85 insertions(+), 13 deletions(-) diff --git a/wg-event-gen/Cargo.lock b/wg-event-gen/Cargo.lock index c302dfb..084b1f1 100644 --- a/wg-event-gen/Cargo.lock +++ b/wg-event-gen/Cargo.lock @@ -16,15 +16,61 @@ name = "hex" version = "0.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "libc" +version = "0.2.47" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "redox_syscall" +version = "0.1.50" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "time" +version = "0.1.42" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.47 (registry+https://github.com/rust-lang/crates.io-index)", + "redox_syscall 0.1.50 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "wg-event-gen" version = "0.1.0" dependencies = [ "base64 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)", "hex 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)", + "time 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "winapi" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + [metadata] "checksum base64 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "621fc7ecb8008f86d7fb9b95356cd692ce9514b80a86d85b397f32a22da7b9e2" "checksum byteorder 1.2.7 (registry+https://github.com/rust-lang/crates.io-index)" = "94f88df23a25417badc922ab0f5716cc1330e87f71ddd9203b3a3ccd9cedf75d" "checksum hex 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "805026a5d0141ffc30abb3be3173848ad46a1b1664fe632428479619a3644d77" +"checksum libc 0.2.47 (registry+https://github.com/rust-lang/crates.io-index)" = "48450664a984b25d5b479554c29cc04e3150c97aa4c01da5604a2d4ed9151476" +"checksum redox_syscall 0.1.50 (registry+https://github.com/rust-lang/crates.io-index)" = "52ee9a534dc1301776eff45b4fa92d2c39b1d8c3d3357e6eb593e0d795506fc2" +"checksum time 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)" = "db8dcfca086c1143c9270ac42a2bbd8a7ee477b78ac8e45b19abfb0cbede4b6f" +"checksum winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "92c1eb33641e276cfa214a0522acad57be5c56b10cb348b3c5117db75f3ac4b0" +"checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" +"checksum winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" diff --git a/wg-event-gen/Cargo.toml b/wg-event-gen/Cargo.toml index 6200713..9236edc 100644 --- a/wg-event-gen/Cargo.toml +++ b/wg-event-gen/Cargo.toml @@ -7,6 +7,7 @@ edition = "2018" [dependencies] hex = "0.3.2" base64 = "0.10.0" +time = "0.1.42" [profile.release] -lto = true +lto = false diff --git a/wg-event-gen/src/main.rs b/wg-event-gen/src/main.rs index 3f78ebe..2add0c5 100644 --- a/wg-event-gen/src/main.rs +++ b/wg-event-gen/src/main.rs @@ -14,7 +14,9 @@ use std::io::{BufRead, BufReader, Error, ErrorKind, Result}; use std::net::SocketAddr; use std::os::unix::net::UnixStream; use std::path::PathBuf; -use std::{thread, time}; +use time; +use std::thread; +use std::time::Duration; pub type KV = (String, String); @@ -28,8 +30,9 @@ enum State { pub struct Peer { public_key: String, endpoint: Option, - last_handshake: Option, - persistent_keepalive: Option, + last_handshake: Option, + persistent_keepalive: Option, + parsed: time::Timespec, } impl Peer { @@ -52,16 +55,35 @@ impl Peer { .next(), last_handshake: entries .iter() - .filter(|(key, _)| key == &"last_handshake_time_nsec") - .map(|(_, value)| time::Duration::from_millis(value.parse::().unwrap())) - .next(), + .filter_map(|(key, value)| { + let value = || value.parse::().unwrap(); + match key.as_ref() { + "last_handshake_time_sec" if value() != 0 => Some(Duration::new(value(), 0)), + "last_handshake_time_nsec" if value() != 0 => Some(Duration::from_nanos(value())), + _ => None + } + }) + .fold(None, |acc, add| { + if let Some(dur) = acc { + Some(dur + add) + } else { + Some(add) + } + }), persistent_keepalive: entries .iter() .filter(|(key, _)| key == &"persistent_keepalive") - .map(|(_, value)| time::Duration::from_secs(value.parse::().unwrap())) + .map(|(_, value)| Duration::from_secs(value.parse::().unwrap())) .next(), + parsed: time::get_time(), }) } + + pub fn last_handshake_rel(&self) -> Option { + let time = self.parsed; + Some(Duration::new(time.sec as u64, time.nsec as u32) - self.last_handshake?) + } + } impl State { @@ -188,7 +210,8 @@ impl Socket { Peer::from_kv(state.kv()) .ok() .map(|peer| (id.to_owned(), peer)) - }).collect()) + }) + .collect()) } } @@ -204,7 +227,8 @@ fn main() { .map(|i| { i.parse::() .expect("[interval] has to be a positive int") - }).unwrap_or(1000); + }) + .unwrap_or(1000); let mut listeners: Vec> = vec![Box::new(LogListener)]; let events: PathBuf = "/etc/wireguard/events.sh".into(); @@ -213,8 +237,9 @@ fn main() { listeners.push(Box::new(ScriptListener::new(events))) } - let timeout = time::Duration::from_secs(3); - + let timeout = env::vars().collect::>().get("WG_EVENT_GEN_TIMEOUT").map(|timeout| Duration::from_secs(timeout.parse::().expect(&format!("Can't parse {} as timeout", timeout)))).unwrap_or(Duration::from_secs(30)); + + if let Some(path) = path { let sock = Socket { path }; let mut prev_state: Option> = None; @@ -230,7 +255,7 @@ fn main() { gen::gen_events(&state, &prev_state, &listeners, timeout); } prev_state = Some(state); - thread::sleep(time::Duration::from_millis(interval)); + thread::sleep(Duration::from_millis(interval)); } } else { println!(" does not exist");