diff options
| -rw-r--r-- | mullvad-jni/src/from_java.rs | 26 | ||||
| -rw-r--r-- | mullvad-jni/src/into_java.rs | 82 | ||||
| -rw-r--r-- | mullvad-jni/src/jni_event_listener.rs | 32 | ||||
| -rw-r--r-- | mullvad-jni/src/lib.rs | 44 | ||||
| -rw-r--r-- | mullvad-jni/src/vpn_service_tun_provider.rs | 31 |
5 files changed, 128 insertions, 87 deletions
diff --git a/mullvad-jni/src/from_java.rs b/mullvad-jni/src/from_java.rs index 6483c8299d..e15f132d64 100644 --- a/mullvad-jni/src/from_java.rs +++ b/mullvad-jni/src/from_java.rs @@ -1,7 +1,7 @@ use crate::{get_class, is_null::IsNull}; -use jnix::jni::{ - objects::{JObject, JString}, - JNIEnv, +use jnix::{ + jni::objects::{JObject, JString}, + JnixEnv, }; use mullvad_types::relay_constraints::{ Constraint, LocationConstraint, RelayConstraintsUpdate, RelaySettingsUpdate, @@ -11,7 +11,7 @@ use std::fmt::Debug; pub trait FromJava<'env> { type JavaType: 'env; - fn from_java(env: &JNIEnv<'env>, source: Self::JavaType) -> Self; + fn from_java(env: &JnixEnv<'env>, source: Self::JavaType) -> Self; } impl<'env, T> FromJava<'env> for Option<T> @@ -21,7 +21,7 @@ where { type JavaType = T::JavaType; - fn from_java(env: &JNIEnv<'env>, source: Self::JavaType) -> Self { + fn from_java(env: &JnixEnv<'env>, source: Self::JavaType) -> Self { if source.is_null() { None } else { @@ -33,7 +33,7 @@ where impl<'env> FromJava<'env> for String { type JavaType = JString<'env>; - fn from_java(env: &JNIEnv<'env>, source: Self::JavaType) -> Self { + fn from_java(env: &JnixEnv<'env>, source: Self::JavaType) -> Self { String::from( env.get_string(source) .expect("Failed to convert from Java String"), @@ -48,7 +48,7 @@ where { type JavaType = JObject<'env>; - fn from_java(env: &JNIEnv<'env>, source: Self::JavaType) -> Self { + fn from_java(env: &JnixEnv<'env>, source: Self::JavaType) -> Self { if is_instance_of(env, source, "net/mullvad/mullvadvpn/model/Constraint$Any") { Constraint::Any } else if is_instance_of(env, source, "net/mullvad/mullvadvpn/model/Constraint$Only") { @@ -64,7 +64,7 @@ where impl<'env> FromJava<'env> for LocationConstraint { type JavaType = JObject<'env>; - fn from_java(env: &JNIEnv<'env>, source: Self::JavaType) -> Self { + fn from_java(env: &JnixEnv<'env>, source: Self::JavaType) -> Self { let country_class = "net/mullvad/mullvadvpn/model/LocationConstraint$Country"; let city_class = "net/mullvad/mullvadvpn/model/LocationConstraint$City"; let hostname_class = "net/mullvad/mullvadvpn/model/LocationConstraint$Hostname"; @@ -100,7 +100,7 @@ impl<'env> FromJava<'env> for LocationConstraint { impl<'env> FromJava<'env> for RelayConstraintsUpdate { type JavaType = JObject<'env>; - fn from_java(env: &JNIEnv<'env>, source: Self::JavaType) -> Self { + fn from_java(env: &JnixEnv<'env>, source: Self::JavaType) -> Self { let location = get_object_field( env, source, @@ -120,7 +120,7 @@ impl<'env> FromJava<'env> for RelayConstraintsUpdate { impl<'env> FromJava<'env> for RelaySettingsUpdate { type JavaType = JObject<'env>; - fn from_java(env: &JNIEnv<'env>, source: Self::JavaType) -> Self { + fn from_java(env: &JnixEnv<'env>, source: Self::JavaType) -> Self { let custom_tunnel_endpoint_class = "net/mullvad/mullvadvpn/model/RelaySettingsUpdate$CustomTunnelEndpoint"; let relay_constraints_update_class = @@ -137,7 +137,7 @@ impl<'env> FromJava<'env> for RelaySettingsUpdate { } fn is_instance_of<'env>( - env: &JNIEnv<'env>, + env: &JnixEnv<'env>, object: JObject<'env>, class_name: &'static str, ) -> bool { @@ -148,7 +148,7 @@ fn is_instance_of<'env>( } fn get_string_field<'env>( - env: &JNIEnv<'env>, + env: &JnixEnv<'env>, object: JObject<'env>, field_name: &str, ) -> JString<'env> { @@ -161,7 +161,7 @@ fn get_string_field<'env>( } fn get_object_field<'env>( - env: &JNIEnv<'env>, + env: &JnixEnv<'env>, object: JObject<'env>, field_name: &str, field_type: &str, diff --git a/mullvad-jni/src/into_java.rs b/mullvad-jni/src/into_java.rs index f79b0b7c53..4f90067d9e 100644 --- a/mullvad-jni/src/into_java.rs +++ b/mullvad-jni/src/into_java.rs @@ -1,10 +1,12 @@ use crate::{daemon_interface, get_class}; use ipnetwork::IpNetwork; -use jnix::jni::{ - objects::{JList, JObject, JString, JValue}, - signature::JavaType, - sys::{jboolean, jint, jshort, jsize}, - JNIEnv, +use jnix::{ + jni::{ + objects::{JList, JObject, JString, JValue}, + signature::JavaType, + sys::{jboolean, jint, jshort, jsize}, + }, + JnixEnv, }; use mullvad_types::{ account::AccountData, @@ -30,7 +32,7 @@ use talpid_types::{ pub trait IntoJava<'env> { type JavaType; - fn into_java(self, env: &JNIEnv<'env>) -> Self::JavaType; + fn into_java(self, env: &JnixEnv<'env>) -> Self::JavaType; } impl<'env, T> IntoJava<'env> for Option<T> @@ -40,7 +42,7 @@ where { type JavaType = T::JavaType; - fn into_java(self, env: &JNIEnv<'env>) -> Self::JavaType { + fn into_java(self, env: &JnixEnv<'env>) -> Self::JavaType { match self { Some(data) => data.into_java(env), None => T::JavaType::from(JObject::null()), @@ -51,7 +53,7 @@ where impl<'env> IntoJava<'env> for String { type JavaType = JString<'env>; - fn into_java(self, env: &JNIEnv<'env>) -> Self::JavaType { + fn into_java(self, env: &JnixEnv<'env>) -> Self::JavaType { env.new_string(&self).expect("Failed to create Java String") } } @@ -63,7 +65,7 @@ where { type JavaType = JObject<'env>; - fn into_java(self, env: &JNIEnv<'env>) -> Self::JavaType { + fn into_java(self, env: &JnixEnv<'env>) -> Self::JavaType { let class = get_class("java/util/ArrayList"); let initial_capacity = self.len(); let parameters = [JValue::Int(initial_capacity as jint)]; @@ -89,7 +91,7 @@ where impl<'array, 'env> IntoJava<'env> for &'array [u8] { type JavaType = JObject<'env>; - fn into_java(self, env: &JNIEnv<'env>) -> Self::JavaType { + fn into_java(self, env: &JnixEnv<'env>) -> Self::JavaType { let size = self.len(); let array = env .new_byte_array(size as jsize) @@ -104,7 +106,7 @@ impl<'array, 'env> IntoJava<'env> for &'array [u8] { } } -fn ipvx_addr_into_java<'env>(original_octets: &[u8], env: &JNIEnv<'env>) -> JObject<'env> { +fn ipvx_addr_into_java<'env>(original_octets: &[u8], env: &JnixEnv<'env>) -> JObject<'env> { let class = get_class("java/net/InetAddress"); let constructor = env @@ -147,7 +149,7 @@ fn ipvx_addr_into_java<'env>(original_octets: &[u8], env: &JNIEnv<'env>) -> JObj impl<'env> IntoJava<'env> for Ipv4Addr { type JavaType = JObject<'env>; - fn into_java(self, env: &JNIEnv<'env>) -> Self::JavaType { + fn into_java(self, env: &JnixEnv<'env>) -> Self::JavaType { ipvx_addr_into_java(self.octets().as_ref(), env) } } @@ -155,7 +157,7 @@ impl<'env> IntoJava<'env> for Ipv4Addr { impl<'env> IntoJava<'env> for Ipv6Addr { type JavaType = JObject<'env>; - fn into_java(self, env: &JNIEnv<'env>) -> Self::JavaType { + fn into_java(self, env: &JnixEnv<'env>) -> Self::JavaType { ipvx_addr_into_java(self.octets().as_ref(), env) } } @@ -163,7 +165,7 @@ impl<'env> IntoJava<'env> for Ipv6Addr { impl<'env> IntoJava<'env> for IpAddr { type JavaType = JObject<'env>; - fn into_java(self, env: &JNIEnv<'env>) -> Self::JavaType { + fn into_java(self, env: &JnixEnv<'env>) -> Self::JavaType { match self { IpAddr::V4(address) => address.into_java(env), IpAddr::V6(address) => address.into_java(env), @@ -174,7 +176,7 @@ impl<'env> IntoJava<'env> for IpAddr { impl<'env> IntoJava<'env> for SocketAddr { type JavaType = JObject<'env>; - fn into_java(self, env: &JNIEnv<'env>) -> Self::JavaType { + fn into_java(self, env: &JnixEnv<'env>) -> Self::JavaType { let class = get_class("java/net/InetSocketAddress"); let ip_address = env.auto_local(self.ip().into_java(env)); let port = self.port() as jint; @@ -188,7 +190,7 @@ impl<'env> IntoJava<'env> for SocketAddr { impl<'env> IntoJava<'env> for IpNetwork { type JavaType = JObject<'env>; - fn into_java(self, env: &JNIEnv<'env>) -> Self::JavaType { + fn into_java(self, env: &JnixEnv<'env>) -> Self::JavaType { let class = get_class("net/mullvad/talpid/tun_provider/InetNetwork"); let address = env.auto_local(self.ip().into_java(env)); let prefix_length = self.prefix() as jshort; @@ -205,7 +207,7 @@ impl<'env> IntoJava<'env> for IpNetwork { impl<'env> IntoJava<'env> for PublicKey { type JavaType = JObject<'env>; - fn into_java(self, env: &JNIEnv<'env>) -> Self::JavaType { + fn into_java(self, env: &JnixEnv<'env>) -> Self::JavaType { let class = get_class("net/mullvad/mullvadvpn/model/PublicKey"); let key = env.auto_local(self.key.as_bytes().into_java(env)); let date_created = env.auto_local(*self.created.to_string().into_java(env)); @@ -222,7 +224,7 @@ impl<'env> IntoJava<'env> for PublicKey { impl<'env> IntoJava<'env> for AppVersionInfo { type JavaType = JObject<'env>; - fn into_java(self, env: &JNIEnv<'env>) -> Self::JavaType { + fn into_java(self, env: &JnixEnv<'env>) -> Self::JavaType { let class = get_class("net/mullvad/mullvadvpn/model/AppVersionInfo"); let current_is_supported = self.current_is_supported as jboolean; let current_is_outdated = self.current_is_outdated as jboolean; @@ -247,7 +249,7 @@ impl<'env> IntoJava<'env> for AppVersionInfo { impl<'env> IntoJava<'env> for AccountData { type JavaType = JObject<'env>; - fn into_java(self, env: &JNIEnv<'env>) -> Self::JavaType { + fn into_java(self, env: &JnixEnv<'env>) -> Self::JavaType { let class = get_class("net/mullvad/mullvadvpn/model/AccountData"); let account_expiry = env.auto_local(JObject::from(self.expiry.to_string().into_java(env))); let parameters = [JValue::Object(account_expiry.as_obj())]; @@ -260,7 +262,7 @@ impl<'env> IntoJava<'env> for AccountData { impl<'env> IntoJava<'env> for TunConfig { type JavaType = JObject<'env>; - fn into_java(self, env: &JNIEnv<'env>) -> Self::JavaType { + fn into_java(self, env: &JnixEnv<'env>) -> Self::JavaType { let class = get_class("net/mullvad/talpid/tun_provider/TunConfig"); let addresses = env.auto_local(self.addresses.into_java(env)); let dns_servers = env.auto_local(self.dns_servers.into_java(env)); @@ -285,7 +287,7 @@ impl<'env> IntoJava<'env> for TunConfig { impl<'env> IntoJava<'env> for TransportProtocol { type JavaType = JObject<'env>; - fn into_java(self, env: &JNIEnv<'env>) -> Self::JavaType { + fn into_java(self, env: &JnixEnv<'env>) -> Self::JavaType { let class_name = match self { TransportProtocol::Tcp => "net/mullvad/talpid/net/TransportProtocol$Tcp", TransportProtocol::Udp => "net/mullvad/talpid/net/TransportProtocol$Udp", @@ -300,7 +302,7 @@ impl<'env> IntoJava<'env> for TransportProtocol { impl<'env> IntoJava<'env> for Endpoint { type JavaType = JObject<'env>; - fn into_java(self, env: &JNIEnv<'env>) -> Self::JavaType { + fn into_java(self, env: &JnixEnv<'env>) -> Self::JavaType { let class = get_class("net/mullvad/talpid/net/Endpoint"); let address = env.auto_local(self.address.into_java(env)); let protocol = env.auto_local(self.protocol.into_java(env)); @@ -321,7 +323,7 @@ impl<'env> IntoJava<'env> for Endpoint { impl<'env> IntoJava<'env> for TunnelEndpoint { type JavaType = JObject<'env>; - fn into_java(self, env: &JNIEnv<'env>) -> Self::JavaType { + fn into_java(self, env: &JnixEnv<'env>) -> Self::JavaType { let class = get_class("net/mullvad/talpid/net/TunnelEndpoint"); let endpoint = env.auto_local(self.endpoint.into_java(env)); let parameters = [JValue::Object(endpoint.as_obj())]; @@ -334,7 +336,7 @@ impl<'env> IntoJava<'env> for TunnelEndpoint { impl<'env> IntoJava<'env> for GeoIpLocation { type JavaType = JObject<'env>; - fn into_java(self, env: &JNIEnv<'env>) -> Self::JavaType { + fn into_java(self, env: &JnixEnv<'env>) -> Self::JavaType { let class = get_class("net/mullvad/mullvadvpn/model/GeoIpLocation"); let ipv4 = env.auto_local(self.ipv4.into_java(env)); let ipv6 = env.auto_local(self.ipv6.into_java(env)); @@ -361,7 +363,7 @@ impl<'env> IntoJava<'env> for GeoIpLocation { impl<'env> IntoJava<'env> for RelayList { type JavaType = JObject<'env>; - fn into_java(self, env: &JNIEnv<'env>) -> Self::JavaType { + fn into_java(self, env: &JnixEnv<'env>) -> Self::JavaType { let class = get_class("net/mullvad/mullvadvpn/model/RelayList"); let relay_countries = env.auto_local(self.countries.into_java(env)); let parameters = [JValue::Object(relay_countries.as_obj())]; @@ -374,7 +376,7 @@ impl<'env> IntoJava<'env> for RelayList { impl<'env> IntoJava<'env> for RelayListCountry { type JavaType = JObject<'env>; - fn into_java(self, env: &JNIEnv<'env>) -> Self::JavaType { + fn into_java(self, env: &JnixEnv<'env>) -> Self::JavaType { let class = get_class("net/mullvad/mullvadvpn/model/RelayListCountry"); let name = env.auto_local(JObject::from(self.name.into_java(env))); let code = env.auto_local(JObject::from(self.code.into_java(env))); @@ -397,7 +399,7 @@ impl<'env> IntoJava<'env> for RelayListCountry { impl<'env> IntoJava<'env> for RelayListCity { type JavaType = JObject<'env>; - fn into_java(self, env: &JNIEnv<'env>) -> Self::JavaType { + fn into_java(self, env: &JnixEnv<'env>) -> Self::JavaType { let class = get_class("net/mullvad/mullvadvpn/model/RelayListCity"); let name = env.auto_local(JObject::from(self.name.into_java(env))); let code = env.auto_local(JObject::from(self.code.into_java(env))); @@ -420,7 +422,7 @@ impl<'env> IntoJava<'env> for RelayListCity { impl<'env> IntoJava<'env> for Relay { type JavaType = JObject<'env>; - fn into_java(self, env: &JNIEnv<'env>) -> Self::JavaType { + fn into_java(self, env: &JnixEnv<'env>) -> Self::JavaType { let class = get_class("net/mullvad/mullvadvpn/model/Relay"); let hostname = env.auto_local(JObject::from(self.hostname.into_java(env))); let has_wireguard_tunnels = (!self.tunnels.wireguard.is_empty()) as jboolean; @@ -442,7 +444,7 @@ where { type JavaType = JObject<'env>; - fn into_java(self, env: &JNIEnv<'env>) -> Self::JavaType { + fn into_java(self, env: &JnixEnv<'env>) -> Self::JavaType { match self { Constraint::Any => { let class = get_class("net/mullvad/mullvadvpn/model/Constraint$Any"); @@ -465,7 +467,7 @@ where impl<'env> IntoJava<'env> for LocationConstraint { type JavaType = JObject<'env>; - fn into_java(self, env: &JNIEnv<'env>) -> Self::JavaType { + fn into_java(self, env: &JnixEnv<'env>) -> Self::JavaType { match self { LocationConstraint::Country(country_code) => { let class = get_class("net/mullvad/mullvadvpn/model/LocationConstraint$Country"); @@ -516,7 +518,7 @@ impl<'env> IntoJava<'env> for LocationConstraint { impl<'env> IntoJava<'env> for RelaySettings { type JavaType = JObject<'env>; - fn into_java(self, env: &JNIEnv<'env>) -> Self::JavaType { + fn into_java(self, env: &JnixEnv<'env>) -> Self::JavaType { match self { RelaySettings::CustomTunnelEndpoint(endpoint) => endpoint.into_java(env), RelaySettings::Normal(relay_constraints) => relay_constraints.into_java(env), @@ -527,7 +529,7 @@ impl<'env> IntoJava<'env> for RelaySettings { impl<'env> IntoJava<'env> for CustomTunnelEndpoint { type JavaType = JObject<'env>; - fn into_java(self, env: &JNIEnv<'env>) -> Self::JavaType { + fn into_java(self, env: &JnixEnv<'env>) -> Self::JavaType { let class = get_class("net/mullvad/mullvadvpn/model/RelaySettings$CustomTunnelEndpoint"); env.new_object(&class, "()V", &[]) @@ -538,7 +540,7 @@ impl<'env> IntoJava<'env> for CustomTunnelEndpoint { impl<'env> IntoJava<'env> for KeygenEvent { type JavaType = JObject<'env>; - fn into_java(self, env: &JNIEnv<'env>) -> Self::JavaType { + fn into_java(self, env: &JnixEnv<'env>) -> Self::JavaType { match self { KeygenEvent::NewKey(public_key) => { let class = get_class("net/mullvad/mullvadvpn/model/KeygenEvent$NewKey"); @@ -595,7 +597,7 @@ impl<'env> IntoJava<'env> for KeygenEvent { impl<'env> IntoJava<'env> for RelayConstraints { type JavaType = JObject<'env>; - fn into_java(self, env: &JNIEnv<'env>) -> Self::JavaType { + fn into_java(self, env: &JnixEnv<'env>) -> Self::JavaType { let class = get_class("net/mullvad/mullvadvpn/model/RelaySettings$RelayConstraints"); let location = env.auto_local(self.location.into_java(env)); let parameters = [JValue::Object(location.as_obj())]; @@ -612,7 +614,7 @@ impl<'env> IntoJava<'env> for RelayConstraints { impl<'env> IntoJava<'env> for Settings { type JavaType = JObject<'env>; - fn into_java(self, env: &JNIEnv<'env>) -> Self::JavaType { + fn into_java(self, env: &JnixEnv<'env>) -> Self::JavaType { let class = get_class("net/mullvad/mullvadvpn/model/Settings"); let account_token = env.auto_local(JObject::from(self.get_account_token().into_java(env))); let relay_settings = env.auto_local(self.get_relay_settings().into_java(env)); @@ -633,7 +635,7 @@ impl<'env> IntoJava<'env> for Settings { impl<'env> IntoJava<'env> for ActionAfterDisconnect { type JavaType = JObject<'env>; - fn into_java(self, env: &JNIEnv<'env>) -> Self::JavaType { + fn into_java(self, env: &JnixEnv<'env>) -> Self::JavaType { let variant = match self { ActionAfterDisconnect::Nothing => "Nothing", ActionAfterDisconnect::Block => "Block", @@ -653,7 +655,7 @@ impl<'env> IntoJava<'env> for ActionAfterDisconnect { impl<'env> IntoJava<'env> for BlockReason { type JavaType = JObject<'env>; - fn into_java(self, env: &JNIEnv<'env>) -> Self::JavaType { + fn into_java(self, env: &JnixEnv<'env>) -> Self::JavaType { let variant = match self { BlockReason::AuthFailed(reason) => { let class = get_class("net/mullvad/talpid/tunnel/BlockReason$AuthFailed"); @@ -694,7 +696,7 @@ impl<'env> IntoJava<'env> for BlockReason { impl<'env> IntoJava<'env> for ParameterGenerationError { type JavaType = JObject<'env>; - fn into_java(self, env: &JNIEnv<'env>) -> Self::JavaType { + fn into_java(self, env: &JnixEnv<'env>) -> Self::JavaType { let class_variant = match self { ParameterGenerationError::NoMatchingRelay => "NoMatchingRelay", ParameterGenerationError::NoMatchingBridgeRelay => "NoMatchingBridgeRelay ", @@ -716,7 +718,7 @@ impl<'env> IntoJava<'env> for ParameterGenerationError { impl<'env> IntoJava<'env> for TunnelState { type JavaType = JObject<'env>; - fn into_java(self, env: &JNIEnv<'env>) -> Self::JavaType { + fn into_java(self, env: &JnixEnv<'env>) -> Self::JavaType { match self { TunnelState::Disconnected => { let class = get_class("net/mullvad/mullvadvpn/model/TunnelState$Disconnected"); @@ -773,7 +775,7 @@ impl<'env> IntoJava<'env> for TunnelState { impl<'env> IntoJava<'env> for Result<AccountData, daemon_interface::Error> { type JavaType = JObject<'env>; - fn into_java(self, env: &JNIEnv<'env>) -> Self::JavaType { + fn into_java(self, env: &JnixEnv<'env>) -> Self::JavaType { match self { Ok(data) => { let class = get_class("net/mullvad/mullvadvpn/model/GetAccountDataResult$Ok"); diff --git a/mullvad-jni/src/jni_event_listener.rs b/mullvad-jni/src/jni_event_listener.rs index 1f8c348388..93bf05462d 100644 --- a/mullvad-jni/src/jni_event_listener.rs +++ b/mullvad-jni/src/jni_event_listener.rs @@ -1,8 +1,10 @@ use crate::{get_class, into_java::IntoJava}; -use jnix::jni::{ - objects::{GlobalRef, JMethodID, JObject, JValue}, - signature::{JavaType, Primitive}, - AttachGuard, JNIEnv, +use jnix::{ + jni::{ + objects::{GlobalRef, JMethodID, JObject, JValue}, + signature::{JavaType, Primitive}, + }, + JnixEnv, }; use mullvad_daemon::EventListener; use mullvad_types::{ @@ -37,7 +39,7 @@ enum Event { pub struct JniEventListener(mpsc::Sender<Event>); impl JniEventListener { - pub fn spawn(env: &JNIEnv, mullvad_daemon: &JObject) -> Result<Self, Error> { + pub fn spawn(env: &JnixEnv, mullvad_daemon: &JObject) -> Result<Self, Error> { JniEventHandler::spawn(env, mullvad_daemon) } } @@ -65,7 +67,7 @@ impl EventListener for JniEventListener { } struct JniEventHandler<'env> { - env: AttachGuard<'env>, + env: JnixEnv<'env>, mullvad_ipc_client: JObject<'env>, notify_app_version_info_event: JMethodID<'env>, notify_keygen_event: JMethodID<'env>, @@ -77,7 +79,7 @@ struct JniEventHandler<'env> { impl JniEventHandler<'_> { pub fn spawn( - old_env: &JNIEnv, + old_env: &JnixEnv, old_mullvad_ipc_client: &JObject, ) -> Result<JniEventListener, Error> { let (tx, rx) = mpsc::channel(); @@ -87,10 +89,14 @@ impl JniEventHandler<'_> { .map_err(Error::CreateGlobalReference)?; thread::spawn(move || match jvm.attach_current_thread() { - Ok(env) => match JniEventHandler::new(env, mullvad_ipc_client.as_obj(), rx) { - Ok(mut listener) => listener.run(), - Err(error) => log::error!("{}", error.display_chain()), - }, + Ok(attach_guard) => { + let env = JnixEnv::from(attach_guard.clone()); + + match JniEventHandler::new(env, mullvad_ipc_client.as_obj(), rx) { + Ok(mut listener) => listener.run(), + Err(error) => log::error!("{}", error.display_chain()), + } + } Err(error) => { log::error!( "{}", @@ -107,7 +113,7 @@ impl JniEventHandler<'_> { impl<'env> JniEventHandler<'env> { fn new( - env: AttachGuard<'env>, + env: JnixEnv<'env>, mullvad_ipc_client: JObject<'env>, events: mpsc::Receiver<Event>, ) -> Result<Self, Error> { @@ -156,7 +162,7 @@ impl<'env> JniEventHandler<'env> { } fn get_method_id( - env: &AttachGuard<'env>, + env: &JnixEnv<'env>, class: &GlobalRef, method: &'static str, signature: &str, diff --git a/mullvad-jni/src/lib.rs b/mullvad-jni/src/lib.rs index eec514eb9b..42203a694b 100644 --- a/mullvad-jni/src/lib.rs +++ b/mullvad-jni/src/lib.rs @@ -12,10 +12,13 @@ use crate::{ daemon_interface::DaemonInterface, from_java::FromJava, into_java::IntoJava, jni_event_listener::JniEventListener, vpn_service_tun_provider::VpnServiceTunProvider, }; -use jnix::jni::{ - objects::{GlobalRef, JObject, JString, JValue}, - sys::{jboolean, JNI_FALSE, JNI_TRUE}, - JNIEnv, +use jnix::{ + jni::{ + objects::{GlobalRef, JObject, JString, JValue}, + sys::{jboolean, JNI_FALSE, JNI_TRUE}, + JNIEnv, + }, + JnixEnv, }; use lazy_static::lazy_static; use mullvad_daemon::{logging, version, Daemon, DaemonCommandSender}; @@ -69,6 +72,8 @@ pub extern "system" fn Java_net_mullvad_mullvadvpn_MullvadDaemon_initialize( this: JObject, vpnService: JObject, ) { + let env = JnixEnv::from(env); + match *LOG_INIT_RESULT { Ok(ref log_dir) => { LOAD_CLASSES.call_once(|| load_classes(&env)); @@ -95,7 +100,7 @@ fn start_logging() -> Result<PathBuf, Error> { Ok(log_dir) } -fn load_classes(env: &JNIEnv) { +fn load_classes(env: &JnixEnv) { let mut classes = CLASSES.write(); for class in classes::CLASSES { @@ -103,7 +108,7 @@ fn load_classes(env: &JNIEnv) { } } -fn load_class_reference(env: &JNIEnv, name: &str) -> GlobalRef { +fn load_class_reference(env: &JnixEnv, name: &str) -> GlobalRef { let class = match env.find_class(name) { Ok(class) => class, Err(_) => panic!("Failed to find {} Java class", name), @@ -114,7 +119,7 @@ fn load_class_reference(env: &JNIEnv, name: &str) -> GlobalRef { } fn initialize( - env: &JNIEnv, + env: &JnixEnv, this: &JObject, vpn_service: &JObject, log_dir: PathBuf, @@ -129,7 +134,7 @@ fn initialize( } fn spawn_daemon( - env: &JNIEnv, + env: &JnixEnv, this: &JObject, tun_provider: VpnServiceTunProvider, log_dir: PathBuf, @@ -210,6 +215,8 @@ pub extern "system" fn Java_net_mullvad_mullvadvpn_MullvadDaemon_generateWiregua env: JNIEnv<'env>, _: JObject, ) -> JObject<'env> { + let env = JnixEnv::from(env); + match DAEMON_INTERFACE.generate_wireguard_key() { Ok(keygen_event) => keygen_event.into_java(&env), Err(error) => { @@ -253,6 +260,7 @@ pub extern "system" fn Java_net_mullvad_mullvadvpn_MullvadDaemon_getAccountData< _: JObject<'this>, accountToken: JString, ) -> JObject<'env> { + let env = JnixEnv::from(env); let account = String::from_java(&env, accountToken); let result = DAEMON_INTERFACE.get_account_data(account); @@ -272,6 +280,8 @@ pub extern "system" fn Java_net_mullvad_mullvadvpn_MullvadDaemon_getWwwAuthToken env: JNIEnv<'env>, _: JObject<'this>, ) -> JString<'env> { + let env = JnixEnv::from(env); + match DAEMON_INTERFACE.get_www_auth_token() { Ok(token) => token.into_java(&env), Err(err) => { @@ -290,6 +300,8 @@ pub extern "system" fn Java_net_mullvad_mullvadvpn_MullvadDaemon_getCurrentLocat env: JNIEnv<'env>, _: JObject<'this>, ) -> JObject<'env> { + let env = JnixEnv::from(env); + match DAEMON_INTERFACE.get_current_location() { Ok(location) => location.into_java(&env), Err(error) => { @@ -308,6 +320,8 @@ pub extern "system" fn Java_net_mullvad_mullvadvpn_MullvadDaemon_getCurrentVersi env: JNIEnv<'env>, _: JObject<'this>, ) -> JString<'env> { + let env = JnixEnv::from(env); + match DAEMON_INTERFACE.get_current_version() { Ok(location) => location.into_java(&env), Err(error) => { @@ -326,6 +340,8 @@ pub extern "system" fn Java_net_mullvad_mullvadvpn_MullvadDaemon_getRelayLocatio env: JNIEnv<'env>, _: JObject<'this>, ) -> JObject<'env> { + let env = JnixEnv::from(env); + match DAEMON_INTERFACE.get_relay_locations() { Ok(relay_list) => relay_list.into_java(&env), Err(error) => { @@ -344,6 +360,8 @@ pub extern "system" fn Java_net_mullvad_mullvadvpn_MullvadDaemon_getSettings<'en env: JNIEnv<'env>, _: JObject<'this>, ) -> JObject<'env> { + let env = JnixEnv::from(env); + match DAEMON_INTERFACE.get_settings() { Ok(settings) => settings.into_java(&env), Err(error) => { @@ -359,6 +377,8 @@ pub extern "system" fn Java_net_mullvad_mullvadvpn_MullvadDaemon_getState<'env, env: JNIEnv<'env>, _: JObject<'this>, ) -> JObject<'env> { + let env = JnixEnv::from(env); + match DAEMON_INTERFACE.get_state() { Ok(state) => state.into_java(&env), Err(error) => { @@ -374,6 +394,8 @@ pub extern "system" fn Java_net_mullvad_mullvadvpn_MullvadDaemon_getVersionInfo< env: JNIEnv<'env>, _: JObject<'this>, ) -> JObject<'env> { + let env = JnixEnv::from(env); + match DAEMON_INTERFACE.get_version_info() { Ok(version_info) => version_info.into_java(&env), Err(error) => { @@ -392,6 +414,8 @@ pub extern "system" fn Java_net_mullvad_mullvadvpn_MullvadDaemon_getWireguardKey env: JNIEnv<'env>, _: JObject<'this>, ) -> JObject<'env> { + let env = JnixEnv::from(env); + match DAEMON_INTERFACE.get_wireguard_key() { Ok(key) => key.into_java(&env), Err(error) => { @@ -411,6 +435,7 @@ pub extern "system" fn Java_net_mullvad_mullvadvpn_MullvadDaemon_setAccount( _: JObject, accountToken: JString, ) { + let env = JnixEnv::from(env); let account = <Option<String> as FromJava>::from_java(&env, accountToken); if let Err(error) = DAEMON_INTERFACE.set_account(account) { @@ -436,6 +461,7 @@ pub extern "system" fn Java_net_mullvad_mullvadvpn_MullvadDaemon_updateRelaySett _: JObject, relaySettingsUpdate: JObject, ) { + let env = JnixEnv::from(env); let update = FromJava::from_java(&env, relaySettingsUpdate); if let Err(error) = DAEMON_INTERFACE.update_relay_settings(update) { @@ -453,6 +479,7 @@ pub extern "system" fn Java_net_mullvad_mullvadvpn_dataproxy_MullvadProblemRepor _: JObject, outputPath: JString, ) -> jboolean { + let env = JnixEnv::from(env); let output_path_string = String::from_java(&env, outputPath); let output_path = Path::new(&output_path_string); @@ -477,6 +504,7 @@ pub extern "system" fn Java_net_mullvad_mullvadvpn_dataproxy_MullvadProblemRepor userMessage: JString, outputPath: JString, ) -> jboolean { + let env = JnixEnv::from(env); let user_email = String::from_java(&env, userEmail); let user_message = String::from_java(&env, userMessage); let output_path_string = String::from_java(&env, outputPath); diff --git a/mullvad-jni/src/vpn_service_tun_provider.rs b/mullvad-jni/src/vpn_service_tun_provider.rs index 5361235f58..46f3df77f5 100644 --- a/mullvad-jni/src/vpn_service_tun_provider.rs +++ b/mullvad-jni/src/vpn_service_tun_provider.rs @@ -1,9 +1,12 @@ use crate::{get_class, into_java::IntoJava}; use ipnetwork::IpNetwork; -use jnix::jni::{ - objects::{GlobalRef, JObject, JValue}, - signature::{JavaType, Primitive}, - JNIEnv, JavaVM, +use jnix::{ + jni::{ + objects::{GlobalRef, JObject, JValue}, + signature::{JavaType, Primitive}, + JavaVM, + }, + JnixEnv, }; use std::{ fs::File, @@ -59,7 +62,7 @@ pub struct VpnServiceTunProvider { impl VpnServiceTunProvider { /// Create a new VpnServiceTunProvider interfacing with Android's VpnService. - pub fn new(env: &JNIEnv, mullvad_vpn_service: &JObject) -> Result<Self, Error> { + pub fn new(env: &JnixEnv, mullvad_vpn_service: &JObject) -> Result<Self, Error> { let jvm = env.get_java_vm().map_err(Error::GetJvmInstance)?; let class = get_class("net/mullvad/talpid/TalpidVpnService"); let object = env @@ -103,10 +106,11 @@ impl VpnServiceTunProvider { } fn open_tun(&mut self, config: TunConfig) -> Result<(), Error> { - let env = self - .jvm - .attach_current_thread_as_daemon() - .map_err(Error::AttachJvmToThread)?; + let env = JnixEnv::from( + self.jvm + .attach_current_thread_as_daemon() + .map_err(Error::AttachJvmToThread)?, + ); let create_tun_method = env .get_method_id( &self.class, @@ -194,10 +198,11 @@ impl Tun for VpnServiceTun { } fn bypass(&mut self, socket: RawFd) -> Result<(), BoxedError> { - let env = self - .jvm - .attach_current_thread_as_daemon() - .map_err(|cause| BoxedError::new(Error::AttachJvmToThread(cause)))?; + let env = JnixEnv::from( + self.jvm + .attach_current_thread_as_daemon() + .map_err(|cause| BoxedError::new(Error::AttachJvmToThread(cause)))?, + ); let create_tun_method = env .get_method_id(&self.class, "bypass", "(I)Z") .map_err(|cause| BoxedError::new(Error::FindMethod("bypass", cause)))?; |
