From 65e979de801daa5f6ef77ed875e6505aa902fd9c Mon Sep 17 00:00:00 2001 From: Alex Auvolat Date: Thu, 9 Mar 2023 16:58:16 +0100 Subject: reannounce persisted data --- src/main.rs | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/src/main.rs b/src/main.rs index 04e655c..60881a2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -192,13 +192,6 @@ impl Daemon { let socket = UdpSocket::bind(SocketAddr::new("0.0.0.0".parse()?, config.gossip_port))?; socket.set_broadcast(true)?; - let gossip = config - .persist_file - .as_ref() - .and_then(|x| std::fs::read(x).ok()) - .and_then(|x| bincode::deserialize(&x).ok()) - .unwrap_or_default(); - Ok(Daemon { config, gossip_key, @@ -207,7 +200,7 @@ impl Daemon { socket, state: Mutex::new(State { peers: HashMap::new(), - gossip, + gossip: HashMap::new(), }), }) } @@ -217,6 +210,10 @@ impl Daemon { error!("Error while initializing Wireguard configuration: {}", e); } + if let Err(e) = self.load_persisted_data() { + error!("Error while loading/reannouncing persisted data: {}", e); + } + let request = self.make_packet(&Gossip::Request)?; for peer in self.config.peers.iter() { let addr = SocketAddr::new(peer.address, self.config.gossip_port); @@ -242,6 +239,17 @@ impl Daemon { Ok(()) } + fn load_persisted_data(&self) -> Result<()> { + if let Some(file) = &self.config.persist_file { + let mut state = self.state.lock().unwrap(); + let persisted: HashMap<_, _> = bincode::deserialize(&std::fs::read(file)?)?; + for (pubkey, addrs) in persisted.into_iter() { + state.handle_announce(self, pubkey, addrs)?; + } + } + Ok(()) + } + fn wg_loop(&self) -> ! { let mut i = 0; loop { -- cgit v1.2.3