Skip to content

Commit

Permalink
Add from java support for relay settings
Browse files Browse the repository at this point in the history
  • Loading branch information
Pururun authored and dlon committed Oct 27, 2023
1 parent f55ce57 commit 071a0a6
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 2 deletions.
16 changes: 15 additions & 1 deletion mullvad-types/src/custom_tunnel.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::settings::TunnelOptions;
#[cfg(target_os = "android")]
use jnix::IntoJava;
use jnix::{jni::objects::JObject, FromJava, IntoJava, JnixEnv};
use serde::{Deserialize, Serialize};
use std::{
fmt, io,
Expand Down Expand Up @@ -91,6 +91,20 @@ impl fmt::Display for CustomTunnelEndpoint {
}
}

#[cfg(target_os = "android")]
impl<'env, 'sub_env> FromJava<'env, JObject<'sub_env>> for CustomTunnelEndpoint
where
'env: 'sub_env,
{
const JNI_SIGNATURE: &'static str = "Lnet/mullvad/mullvadvpn/model/CustomTunnelEndpoint;";

fn from_java(_env: &JnixEnv<'env>, _object: JObject<'sub_env>) -> Self {
panic!(
"Attempting to convert from CustomTunnelEndpoint java class. This should never happen!"
);
}
}

/// Does a DNS lookup if the host isn't an IP.
/// Returns the first IPv4 address if one exists, otherwise the first IPv6 address.
/// Rust only provides means to resolve a socket addr, not just a host, for some reason. So
Expand Down
73 changes: 72 additions & 1 deletion mullvad-types/src/relay_constraints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ where
/// relay.
#[derive(Debug, Clone, Eq, PartialEq, Deserialize, Serialize)]
#[serde(rename_all = "snake_case")]
#[cfg_attr(target_os = "android", derive(IntoJava))]
#[cfg_attr(target_os = "android", derive(IntoJava, FromJava))]
#[cfg_attr(target_os = "android", jnix(package = "net.mullvad.mullvadvpn.model"))]
pub enum RelaySettings {
CustomTunnelEndpoint(CustomTunnelEndpoint),
Expand Down Expand Up @@ -415,6 +415,77 @@ impl<'a> fmt::Display for RelayConstraintsFormatter<'a> {
}
}

#[cfg(target_os = "android")]
impl<'env, 'sub_env> FromJava<'env, JObject<'sub_env>> for RelayConstraints
where
'env: 'sub_env,
{
const JNI_SIGNATURE: &'static str = "Lnet/mullvad/mullvadvpn/model/RelayConstraints;";

fn from_java(env: &JnixEnv<'env>, object: JObject<'sub_env>) -> Self {
let object_location = env
.call_method(
object,
"component1",
"()Lnet/mullvad/mullvadvpn/model/Constraint;",
&[],
)
.expect("missing RelayConstraints.location")
.l()
.expect("RelayConstraints.location did not return an object");

let location: Constraint<LocationConstraint> = Constraint::from_java(env, object_location);

let object_providers = env
.call_method(
object,
"component2",
"()Lnet/mullvad/mullvadvpn/model/Constraint;",
&[],
)
.expect("missing RelayConstraints.providers")
.l()
.expect("RelayConstraints.providers did not return an object");

let providers: Constraint<Providers> = Constraint::from_java(env, object_providers);

let object_ownership = env
.call_method(
object,
"component3",
"()Lnet/mullvad/mullvadvpn/model/Constraint;",
&[],
)
.expect("missing RelayConstraints.providers")
.l()
.expect("RelayConstraints.providers did not return an object");

let ownership: Constraint<Ownership> = Constraint::from_java(env, object_ownership);

let object_wireguard_constraints = env
.call_method(
object,
"component4",
"()Lnet/mullvad/mullvadvpn/model/WireguardConstraints;",
&[],
)
.expect("missing RelayConstraints.wireguard_constraints")
.l()
.expect("RelayConstraints.wireguard_constraints did not return an object");

let wireguard_constraints: WireguardConstraints =
WireguardConstraints::from_java(env, object_wireguard_constraints);

RelayConstraints {
location,
providers,
ownership,
wireguard_constraints,
..Default::default()
}
}
}

/// Limits the set of [`crate::relay_list::Relay`]s used by a `RelaySelector` based on
/// location.
#[derive(Debug, Clone, Eq, PartialEq, Deserialize, Serialize, PartialOrd, Ord)]
Expand Down

0 comments on commit 071a0a6

Please sign in to comment.