more error tolerance
This commit is contained in:
parent
4f5ecdd55f
commit
c2940724de
1 changed files with 49 additions and 38 deletions
87
src/main.rs
87
src/main.rs
|
@ -160,10 +160,16 @@ impl Daemon {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn run(&self) -> Result<()> {
|
fn run(&self) -> Result<()> {
|
||||||
|
if let Err(e) = self.state.lock().unwrap().setup_wg_peers(self, 0) {
|
||||||
|
error!("Error initializing wireguard peers: {}", e);
|
||||||
|
}
|
||||||
|
|
||||||
let request = bincode::serialize(&Gossip::Request)?;
|
let request = bincode::serialize(&Gossip::Request)?;
|
||||||
for peer in self.config.peers.iter() {
|
for peer in self.config.peers.iter() {
|
||||||
let addr = SocketAddr::new(peer.address, self.config.gossip_port);
|
let addr = SocketAddr::new(peer.address, self.config.gossip_port);
|
||||||
self.socket.send_to(&request, addr)?;
|
if let Err(e) = self.socket.send_to(&request, addr) {
|
||||||
|
error!("Error sending initial request to {}: {}", addr, e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
thread::scope(|s| {
|
thread::scope(|s| {
|
||||||
|
@ -235,43 +241,7 @@ impl Daemon {
|
||||||
}
|
}
|
||||||
|
|
||||||
// 3. Try new address for disconnected peers
|
// 3. Try new address for disconnected peers
|
||||||
let now = time();
|
state.setup_wg_peers(&self, i)?;
|
||||||
for peer in self.config.peers.iter() {
|
|
||||||
// Skip peer if it is in connected state
|
|
||||||
if state
|
|
||||||
.peers
|
|
||||||
.get(&peer.pubkey)
|
|
||||||
.map(|x| now < x.last_seen + TIMEOUT.as_secs())
|
|
||||||
.unwrap_or(false)
|
|
||||||
{
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
let mut endpoints = state.gossip.get(&peer.pubkey).cloned().unwrap_or_default();
|
|
||||||
if endpoints.is_empty() {
|
|
||||||
if let Some(endpoint) = peer.endpoint {
|
|
||||||
endpoints.push((endpoint, 0));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
endpoints.sort();
|
|
||||||
if !endpoints.is_empty() {
|
|
||||||
let endpoint = endpoints[i % endpoints.len()];
|
|
||||||
info!("Configure {} with endpoint {}", peer.pubkey, endpoint.0);
|
|
||||||
Command::new("wg")
|
|
||||||
.args([
|
|
||||||
"set",
|
|
||||||
&self.config.interface,
|
|
||||||
"peer",
|
|
||||||
&peer.pubkey,
|
|
||||||
"endpoint",
|
|
||||||
&endpoint.0.to_string(),
|
|
||||||
"persistent-keepalive",
|
|
||||||
"20",
|
|
||||||
"allowed-ips",
|
|
||||||
&format!("{}/32", peer.address),
|
|
||||||
])
|
|
||||||
.output()?;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -389,4 +359,45 @@ impl State {
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn setup_wg_peers(&self, daemon: &Daemon, i: usize) -> Result<()> {
|
||||||
|
let now = time();
|
||||||
|
for peer in daemon.config.peers.iter() {
|
||||||
|
// Skip peer if it is in connected state
|
||||||
|
if self
|
||||||
|
.peers
|
||||||
|
.get(&peer.pubkey)
|
||||||
|
.map(|x| now < x.last_seen + TIMEOUT.as_secs())
|
||||||
|
.unwrap_or(false)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
let mut endpoints = self.gossip.get(&peer.pubkey).cloned().unwrap_or_default();
|
||||||
|
if endpoints.is_empty() {
|
||||||
|
if let Some(endpoint) = peer.endpoint {
|
||||||
|
endpoints.push((endpoint, 0));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
endpoints.sort();
|
||||||
|
if !endpoints.is_empty() {
|
||||||
|
let endpoint = endpoints[i % endpoints.len()];
|
||||||
|
info!("Configure {} with endpoint {}", peer.pubkey, endpoint.0);
|
||||||
|
Command::new("wg")
|
||||||
|
.args([
|
||||||
|
"set",
|
||||||
|
&daemon.config.interface,
|
||||||
|
"peer",
|
||||||
|
&peer.pubkey,
|
||||||
|
"endpoint",
|
||||||
|
&endpoint.0.to_string(),
|
||||||
|
"persistent-keepalive",
|
||||||
|
"20",
|
||||||
|
"allowed-ips",
|
||||||
|
&format!("{}/32", peer.address),
|
||||||
|
])
|
||||||
|
.output()?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue