From aedf04cf90ba77784a6b985e44caf509c83068bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20L=C3=B6nnhager?= Date: Fri, 9 Aug 2024 20:56:04 +0200 Subject: [PATCH] Remove recreateTunIfOpen --- .../net/mullvad/talpid/TalpidVpnService.kt | 9 -- talpid-tunnel/src/tun_provider/android/mod.rs | 109 +++++++----------- 2 files changed, 41 insertions(+), 77 deletions(-) diff --git a/android/lib/talpid/src/main/kotlin/net/mullvad/talpid/TalpidVpnService.kt b/android/lib/talpid/src/main/kotlin/net/mullvad/talpid/TalpidVpnService.kt index b5bbcba79320..cc458e0b46f4 100644 --- a/android/lib/talpid/src/main/kotlin/net/mullvad/talpid/TalpidVpnService.kt +++ b/android/lib/talpid/src/main/kotlin/net/mullvad/talpid/TalpidVpnService.kt @@ -72,15 +72,6 @@ open class TalpidVpnService : LifecycleVpnService() { } } - fun recreateTunIfOpen(config: TunConfig) { - synchronized(this) { - if (tunIsOpen) { - currentTunConfig = config - activeTunStatus = createTun(config) - } - } - } - fun closeTun() { synchronized(this) { activeTunStatus = null } } diff --git a/talpid-tunnel/src/tun_provider/android/mod.rs b/talpid-tunnel/src/tun_provider/android/mod.rs index 6b141565349f..58f8e6856487 100644 --- a/talpid-tunnel/src/tun_provider/android/mod.rs +++ b/talpid-tunnel/src/tun_provider/android/mod.rs @@ -54,7 +54,7 @@ pub struct AndroidTunProvider { jvm: Arc, class: GlobalRef, object: GlobalRef, - last_tun_config: (TunConfig, bool), + last_tun_config: Option<(TunConfig, bool)>, allow_lan: bool, custom_dns_servers: Option>, allowed_lan_networks: Vec, @@ -82,7 +82,7 @@ impl AndroidTunProvider { jvm: context.jvm, class: talpid_vpn_service_class, object: context.vpn_service, - last_tun_config: (TunConfig::default(), false), + last_tun_config: None, allow_lan, custom_dns_servers, allowed_lan_networks, @@ -122,22 +122,27 @@ impl AndroidTunProvider { /// Retrieve a tunnel device with the provided configuration. Custom DNS and LAN routes are /// appended to the provided config. pub fn get_tun(&mut self, config: TunConfig) -> Result { - let original_config = config; - let config = VpnServiceConfig::new( - original_config.clone(), + self.get_tun_inner(config, false) + } + + /// Retrieve a tunnel device with the provided configuration. + fn get_tun_inner(&mut self, config: TunConfig, blocking: bool) -> Result { + let service_config = VpnServiceConfig::new( + config.clone(), &self.allowed_lan_networks, self.allow_lan, - self.custom_dns_servers.clone(), + if !blocking { + self.custom_dns_servers.clone() + } else { + // Disable DNS + Some(vec![]) + }, self.excluded_packages.clone(), ); - let tun = self.get_tun_inner(config)?; - self.last_tun_config = (original_config, false); - Ok(tun) - } - /// Retrieve a tunnel device with the provided configuration. - fn get_tun_inner(&self, config: VpnServiceConfig) -> Result { - let tun_fd = self.get_tun_fd(config.clone())?; + let tun_fd = self.get_tun_fd(service_config)?; + + self.last_tun_config = Some((config, blocking)); let jvm = unsafe { JavaVM::from_raw(self.jvm.get_java_vm_pointer()) } .map_err(Error::CloneJavaVm)?; @@ -150,23 +155,30 @@ impl AndroidTunProvider { }) } + fn get_tun_fd(&self, config: VpnServiceConfig) -> Result { + let env = self.env()?; + let java_config = config.into_java(&env); + + let result = self.call_method( + "getTun", + "(Lnet/mullvad/talpid/model/TunConfig;)Lnet/mullvad/talpid/model/CreateTunResult;", + JavaType::Object("net/mullvad/talpid/model/CreateTunResult".to_owned()), + &[JValue::Object(java_config.as_obj())], + )?; + + match result { + JValue::Object(result) => CreateTunResult::from_java(&env, result).into(), + value => Err(Error::InvalidMethodResult("getTun", format!("{:?}", value))), + } + } + /// Open a tunnel device that routes everything but (potentially) LAN routes via the tunnel /// device. Excluded apps will also be kept. /// /// Will open a new tunnel if there is already an active tunnel. The previous tunnel will be /// closed. pub fn create_blocking_tun(&mut self) -> Result<(), Error> { - let original_config = TunConfig::default(); - let config = VpnServiceConfig::new( - original_config.clone(), - &self.allowed_lan_networks, - self.allow_lan, - // Disable DNS - Some(vec![]), - self.excluded_packages.clone(), - ); - let _ = self.get_tun_inner(config)?; - self.last_tun_config = (original_config, true); + let _ = self.get_tun_inner(TunConfig::default(), true)?; Ok(()) } @@ -183,6 +195,8 @@ impl AndroidTunProvider { Err(error) => Some(error), }; + self.last_tun_config = None; + if let Some(error) = error { log::error!( "{}", @@ -191,52 +205,11 @@ impl AndroidTunProvider { } } - fn get_tun_fd(&self, config: VpnServiceConfig) -> Result { - let env = self.env()?; - let java_config = config.into_java(&env); - - let result = self.call_method( - "getTun", - "(Lnet/mullvad/talpid/model/TunConfig;)Lnet/mullvad/talpid/model/CreateTunResult;", - JavaType::Object("net/mullvad/talpid/model/CreateTunResult".to_owned()), - &[JValue::Object(java_config.as_obj())], - )?; - - match result { - JValue::Object(result) => CreateTunResult::from_java(&env, result).into(), - value => Err(Error::InvalidMethodResult("getTun", format!("{:?}", value))), - } - } - fn recreate_tun_if_open(&mut self) -> Result<(), Error> { - let (last_tun_config, blocking) = self.last_tun_config.clone(); - - let config = VpnServiceConfig::new( - last_tun_config, - &self.allowed_lan_networks, - self.allow_lan, - if !blocking { - self.custom_dns_servers.clone() - } else { - Some(vec![]) - }, - self.excluded_packages.clone(), - ); - - let env = self.env()?; - let java_config = config.into_java(&env); - - let result = self.call_method( - "recreateTunIfOpen", - "(Lnet/mullvad/talpid/model/TunConfig;)V", - JavaType::Primitive(Primitive::Void), - &[JValue::Object(java_config.as_obj())], - )?; - - match result { - JValue::Void => Ok(()), - value => Err(Error::InvalidMethodResult("getTun", format!("{:?}", value))), + if let Some((config, blocking)) = self.last_tun_config.clone() { + let _ = self.get_tun_inner(config, blocking)?; } + Ok(()) } /// Allow a socket to bypass the tunnel.