Skip to content

Commit

Permalink
Don't build wg deps where it's not supported (#4305)
Browse files Browse the repository at this point in the history
* Don't build wg deps where it's not supported

* Fix compilation

* Another fix

* More fixes

* another fix
  • Loading branch information
octol committed Jan 11, 2024
1 parent a834bb1 commit d3d5cc3
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 29 deletions.
4 changes: 3 additions & 1 deletion common/wireguard/Cargo.toml
Expand Up @@ -16,10 +16,12 @@ base64 = "0.21.3"
# version mismatch with x25519-dalek/curve25519-dalek that is resolved in the
# latest commit. So pick that for now.
x25519-dalek = "2.0.0"
defguard_wireguard_rs = { git = "https://github.com/neacsu/wireguard-rs.git", rev = "c2cd0c1119f699f4bc43f5e6ffd6fc242caa42ed" }
ip_network = "0.4.1"
log.workspace = true
nym-network-defaults = { path = "../network-defaults" }
nym-task = { path = "../task" }
nym-wireguard-types = { path = "../wireguard-types" }
tokio = { workspace = true, features = ["rt-multi-thread", "net", "io-util"] }

[target."cfg(target_os = \"linux\")".dependencies]
defguard_wireguard_rs = { git = "https://github.com/neacsu/wireguard-rs.git", rev = "c2cd0c1119f699f4bc43f5e6ffd6fc242caa42ed" }
34 changes: 13 additions & 21 deletions common/wireguard/src/lib.rs
Expand Up @@ -5,26 +5,20 @@

pub mod setup;

use nym_wireguard_types::registration::GatewayClientRegistry;
use std::sync::Arc;

// Currently the module related to setting up the virtual network device is platform specific.
#[cfg(target_os = "linux")]
use crate::setup::{peer_allowed_ips, peer_static_public_key, PRIVATE_KEY};
use defguard_wireguard_rs::WGApi;
#[cfg(target_os = "linux")]
use defguard_wireguard_rs::{
host::Peer, key::Key, net::IpAddrMask, InterfaceConfiguration, WireguardInterfaceApi,
};
#[cfg(target_os = "linux")]
use nym_network_defaults::{WG_PORT, WG_TUN_DEVICE_ADDRESS};

/// Start wireguard device
#[cfg(target_os = "linux")]
pub async fn start_wireguard(
mut task_client: nym_task::TaskClient,
_gateway_client_registry: Arc<GatewayClientRegistry>,
) -> Result<WGApi, Box<dyn std::error::Error + Send + Sync + 'static>> {
_gateway_client_registry: std::sync::Arc<
nym_wireguard_types::registration::GatewayClientRegistry,
>,
) -> Result<defguard_wireguard_rs::WGApi, Box<dyn std::error::Error + Send + Sync + 'static>> {
use crate::setup::{peer_allowed_ips, peer_static_public_key, PRIVATE_KEY};
use defguard_wireguard_rs::{
host::Peer, key::Key, net::IpAddrMask, InterfaceConfiguration, WGApi, WireguardInterfaceApi,
};
use nym_network_defaults::{WG_PORT, WG_TUN_DEVICE_ADDRESS};

let ifname = String::from("wg0");
let wgapi = WGApi::new(ifname.clone(), false)?;
wgapi.create_interface()?;
Expand All @@ -48,10 +42,8 @@ pub async fn start_wireguard(

Ok(wgapi)
}

#[cfg(not(target_os = "linux"))]
pub async fn start_wireguard(
_task_client: nym_task::TaskClient,
_gateway_client_registry: Arc<GatewayClientRegistry>,
) -> Result<WGApi, Box<dyn std::error::Error + Send + Sync + 'static>> {
todo!("WireGuard is currently only supported on Linux")
pub async fn start_wireguard() {
todo!("WireGuard is currently only supported on Linux");
}
21 changes: 14 additions & 7 deletions gateway/src/node/mod.rs
Expand Up @@ -22,8 +22,6 @@ use crate::node::statistics::collector::GatewayStatisticsCollector;
use crate::node::storage::Storage;
use anyhow::bail;
use dashmap::DashMap;
#[cfg(feature = "wireguard")]
use defguard_wireguard_rs::{WGApi, WireguardInterfaceApi};
use futures::channel::{mpsc, oneshot};
use log::*;
use nym_crypto::asymmetric::{encryption, identity};
Expand Down Expand Up @@ -202,14 +200,19 @@ impl<St> Gateway<St> {
mixnet_handling::Listener::new(listening_address, shutdown).start(connection_handler);
}

#[cfg(feature = "wireguard")]
#[cfg(all(feature = "wireguard", target_os = "linux"))]
async fn start_wireguard(
&self,
shutdown: TaskClient,
) -> Result<WGApi, Box<dyn Error + Send + Sync>> {
) -> Result<defguard_wireguard_rs::WGApi, Box<dyn Error + Send + Sync>> {
nym_wireguard::start_wireguard(shutdown, Arc::clone(&self.client_registry)).await
}

#[cfg(all(feature = "wireguard", not(target_os = "linux")))]
async fn start_wireguard(&self, _shutdown: TaskClient) {
nym_wireguard::start_wireguard().await
}

fn start_client_websocket_listener(
&self,
forwarding_channel: MixForwardingSender,
Expand Down Expand Up @@ -524,21 +527,25 @@ impl<St> Gateway<St> {

// Once this is a bit more mature, make this a commandline flag instead of a compile time
// flag
#[cfg(feature = "wireguard")]
#[cfg(all(feature = "wireguard", target_os = "linux"))]
let wg_api = self
.start_wireguard(shutdown.subscribe().named("wireguard"))
.await
.ok();

#[cfg(all(feature = "wireguard", not(target_os = "linux")))]
self.start_wireguard(shutdown.subscribe().named("wireguard"))
.await;

info!("Finished nym gateway startup procedure - it should now be able to receive mix and client traffic!");

if let Err(err) = Self::wait_for_interrupt(shutdown).await {
// that's a nasty workaround, but anyhow errors are generally nicer, especially on exit
bail!("{err}")
}
#[cfg(feature = "wireguard")]
#[cfg(all(feature = "wireguard", target_os = "linux"))]
if let Some(wg_api) = wg_api {
wg_api.remove_interface()?;
defguard_wireguard_rs::WireguardInterfaceApi::remove_interface(&wg_api)?;
}
Ok(())
}
Expand Down

0 comments on commit d3d5cc3

Please sign in to comment.