Skip to content

Commit

Permalink
wait for wg interface to be available
Browse files Browse the repository at this point in the history
  • Loading branch information
elmarx committed Aug 29, 2023
1 parent 1d07ddf commit 878a7c6
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use std::env;
use std::env::args;
use std::time::Duration;

use rtnetlink::new_connection;
use tokio::time::sleep;

use crate::dns::node_repository::DnsNodeRepository;
use crate::mesh::wgmesh::WgMesh;
Expand All @@ -16,6 +18,20 @@ mod routing;
mod traits;
mod wireguard;

pub async fn init_retry_wg(interface_name: &str) -> Result<WireguardImpl, error::Wireguard> {
for _ in 0..12 {
let wireguard = WireguardImpl::new(interface_name);

if wireguard.is_ok() {
return wireguard;
}

sleep(Duration::from_secs(5)).await;
}

WireguardImpl::new(interface_name)
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// TODO: use clap for proper input/argument parsing
Expand All @@ -30,8 +46,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {

let peer_repository = DnsNodeRepository::init(mesh_resolver)?;

// TODO: loop/wait for device to be available
let wireguard_device = WireguardImpl::new(&interface_name)?;
let wireguard_device = init_retry_wg(&interface_name).await?;
let routing_service = RoutingServiceImpl::new(handle, &interface_name);

let wg_mesh = WgMesh::new(peer_repository, routing_service, wireguard_device);
Expand Down

0 comments on commit 878a7c6

Please sign in to comment.