Skip to content

Commit

Permalink
Adress review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
kl committed Nov 14, 2024
1 parent b98d8d0 commit 27bb7f6
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 39 deletions.
23 changes: 0 additions & 23 deletions talpid-wireguard/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,29 +194,6 @@ impl Config {
}
}

#[cfg(target_os = "android")]
impl Config {
pub(crate) fn exit_config(&self) -> Option<Config> {
let mut exit_config = self.clone();
exit_config.entry_peer = self.exit_peer.clone()?;
Some(exit_config)
}

pub(crate) fn entry_config(&self) -> Config {
let mut entry_config = self.clone();
entry_config.exit_peer = None;
entry_config
}

pub(crate) fn private_ip(&self) -> CString {
if let Some(ip) = self.tunnel.addresses.iter().find(|addr| addr.is_ipv4()) {
CString::new(ip.to_string()).unwrap()
} else {
CString::default()
}
}
}

enum ConfValue<'a> {
String(&'a str),
Bytes(&'a [u8]),
Expand Down
2 changes: 2 additions & 0 deletions talpid-wireguard/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -775,11 +775,13 @@ impl WireguardMonitor {
// Refer to `docs/architecture.md` for details on how to use multihop + PQ.
#[cfg(target_os = "android")]
let config = Self::patch_allowed_ips(config, gateway_only);

#[cfg(target_os = "android")]
let tunnel = if config.is_multihop() {
WgGoTunnel::start_multihop_tunnel(
#[allow(clippy::needless_borrow)]
&config,
config.exit_peer.clone().unwrap(),
log_path,
tun_provider,
routes,
Expand Down
41 changes: 25 additions & 16 deletions talpid-wireguard/src/wireguard_go/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
use super::{
stats::{Stats, StatsMap},
Config, Tunnel, TunnelError,
};
#[cfg(target_os = "linux")]
use crate::config::MULLVAD_INTERFACE_NAME;
use crate::logging::{clean_up_logging, initialize_logging};
use ipnetwork::IpNetwork;
#[cfg(daita)]
use once_cell::sync::OnceCell;
Expand All @@ -13,16 +20,9 @@ use std::{
#[cfg(target_os = "android")]
use talpid_tunnel::tun_provider::Error as TunProviderError;
use talpid_tunnel::tun_provider::{Tun, TunProvider};
use talpid_types::net::wireguard::PeerConfig;
use talpid_types::BoxedError;

use super::{
stats::{Stats, StatsMap},
Config, Tunnel, TunnelError,
};
#[cfg(target_os = "linux")]
use crate::config::MULLVAD_INTERFACE_NAME;
use crate::logging::{clean_up_logging, initialize_logging};

const MAX_PREPARE_TUN_ATTEMPTS: usize = 4;

/// Maximum number of events that can be stored in the underlying buffer
Expand Down Expand Up @@ -100,8 +100,7 @@ impl WgGoTunnel {

match self {
WgGoTunnel::Multihop(state) if !config.is_multihop() => {
// Important!
state.stop().unwrap();
state.stop()?;
Self::start_tunnel(
config,
log_path.as_deref(),
Expand All @@ -111,9 +110,10 @@ impl WgGoTunnel {
)
}
WgGoTunnel::Singlehop(state) if config.is_multihop() => {
state.stop().unwrap();
state.stop()?;
Self::start_multihop_tunnel(
config,
config.exit_peer.clone().unwrap(),
log_path.as_deref(),
tun_provider,
routes,
Expand Down Expand Up @@ -317,6 +317,7 @@ impl WgGoTunnel {

pub fn start_multihop_tunnel(
config: &Config,
exit_peer: PeerConfig,
log_path: Option<&Path>,
tun_provider: Arc<Mutex<TunProvider>>,
routes: impl Iterator<Item = IpNetwork>,
Expand All @@ -332,14 +333,22 @@ impl WgGoTunnel {
.map(LoggingContext)
.map_err(TunnelError::LoggingError)?;

let entry_config = config.entry_config();
let exit_config = config.exit_config();
let mut entry_config = config.clone();
entry_config.exit_peer = None;

let mut exit_config = config.clone();
exit_config.entry_peer = exit_peer;

// multihop
let exit_config = exit_config.unwrap();
let entry_config_str = entry_config.to_userspace_format();
let exit_config_str = exit_config.to_userspace_format();
let private_ip = config.private_ip();

let private_ip = config
.tunnel
.addresses
.iter()
.find(|addr| addr.is_ipv4())
.map(|addr| CString::new(addr.to_string()).unwrap())
.ok_or(TunnelError::SetConfigError)?;

let handle = wireguard_go_rs::Tunnel::turn_on_multihop(
&exit_config_str,
Expand Down

0 comments on commit 27bb7f6

Please sign in to comment.