parse nanos as nanos
This commit is contained in:
@@ -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,8 +237,9 @@ 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 };
|
||||
let mut prev_state: Option<HashMap<String, Peer>> = 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!("<path> does not exist");
|
||||
|
Reference in New Issue
Block a user