parse nanos as nanos
This commit is contained in:
parent
e2dbf0112e
commit
34e04ae726
46
wg-event-gen/Cargo.lock
generated
46
wg-event-gen/Cargo.lock
generated
@ -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"
|
||||
|
@ -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
|
||||
|
@ -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<SocketAddr>,
|
||||
last_handshake: Option<time::Duration>,
|
||||
persistent_keepalive: Option<time::Duration>,
|
||||
last_handshake: Option<Duration>,
|
||||
persistent_keepalive: Option<Duration>,
|
||||
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::<u64>().unwrap()))
|
||||
.next(),
|
||||
.filter_map(|(key, value)| {
|
||||
let value = || value.parse::<u64>().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::<u64>().unwrap()))
|
||||
.map(|(_, value)| Duration::from_secs(value.parse::<u64>().unwrap()))
|
||||
.next(),
|
||||
parsed: time::get_time(),
|
||||
})
|
||||
}
|
||||
|
||||
pub fn last_handshake_rel(&self) -> Option<Duration> {
|
||||
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::<u64>()
|
||||
.expect("[interval] has to be a positive int")
|
||||
}).unwrap_or(1000);
|
||||
})
|
||||
.unwrap_or(1000);
|
||||
let mut listeners: Vec<Box<EventListener>> = vec![Box::new(LogListener)];
|
||||
|
||||
let events: PathBuf = "/etc/wireguard/events.sh".into();
|
||||
@ -213,7 +237,8 @@ fn main() {
|
||||
listeners.push(Box::new(ScriptListener::new(events)))
|
||||
}
|
||||
|
||||
let timeout = time::Duration::from_secs(3);
|
||||
let timeout = env::vars().collect::<HashMap<String,String>>().get("WG_EVENT_GEN_TIMEOUT").map(|timeout| Duration::from_secs(timeout.parse::<u64>().expect(&format!("Can't parse {} as timeout", timeout)))).unwrap_or(Duration::from_secs(30));
|
||||
|
||||
|
||||
if let Some(path) = path {
|
||||
let sock = Socket { path };
|
||||
@ -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!("<path> does not exist");
|
||||
|
Loading…
x
Reference in New Issue
Block a user