summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorDavid Lönnhager <david.l@mullvad.net>2021-04-22 18:51:42 +0200
committerDavid Lönnhager <david.l@mullvad.net>2021-04-29 12:17:20 +0200
commit727bb2a5546808789c919cc6252a0cf47e320a64 (patch)
tree7e0b78f45553f1ea36df3f9a000d72adac886a0d
parent5a73049c0841bab3b2704e90cf83a954facbd943 (diff)
downloadmullvadvpn-727bb2a5546808789c919cc6252a0cf47e320a64.tar.xz
mullvadvpn-727bb2a5546808789c919cc6252a0cf47e320a64.zip
Implement From for RelayListCountry
-rw-r--r--mullvad-daemon/src/management_interface.rs105
-rw-r--r--mullvad-management-interface/src/types.rs93
2 files changed, 100 insertions, 98 deletions
diff --git a/mullvad-daemon/src/management_interface.rs b/mullvad-daemon/src/management_interface.rs
index 0c6c2f67b6..530078f9a2 100644
--- a/mullvad-daemon/src/management_interface.rs
+++ b/mullvad-daemon/src/management_interface.rs
@@ -14,7 +14,7 @@ use mullvad_types::{
relay_constraints::{
BridgeConstraints, BridgeSettings, BridgeState, Constraint, Providers, RelaySettingsUpdate,
},
- relay_list::{Relay, RelayList, RelayListCountry},
+ relay_list::RelayList,
settings::Settings,
states::{TargetState, TunnelState},
version,
@@ -199,9 +199,9 @@ impl ManagementService for ManagementServiceImpl {
tokio::sync::mpsc::channel(cmp::max(1, locations.countries.len()));
tokio::spawn(async move {
- for country in &locations.countries {
+ for country in locations.countries.into_iter() {
if let Err(error) = stream_tx
- .send(Ok(convert_relay_list_country(country)))
+ .send(Ok(types::RelayListCountry::from(country)))
.await
{
log::error!(
@@ -792,99 +792,6 @@ fn convert_public_key(public_key: &wireguard::PublicKey) -> types::PublicKey {
}
}
-fn convert_relay_list_country(country: &RelayListCountry) -> types::RelayListCountry {
- let mut proto_country = types::RelayListCountry {
- name: country.name.clone(),
- code: country.code.clone(),
- cities: Vec::with_capacity(country.cities.len()),
- };
-
- for city in &country.cities {
- proto_country.cities.push(types::RelayListCity {
- name: city.name.clone(),
- code: city.code.clone(),
- latitude: city.latitude,
- longitude: city.longitude,
- relays: city
- .relays
- .iter()
- .map(|relay| convert_relay(relay))
- .collect(),
- });
- }
-
- proto_country
-}
-
-fn convert_relay(relay: &Relay) -> types::Relay {
- types::Relay {
- hostname: relay.hostname.clone(),
- ipv4_addr_in: relay.ipv4_addr_in.to_string(),
- ipv6_addr_in: relay
- .ipv6_addr_in
- .map(|addr| addr.to_string())
- .unwrap_or_default(),
- include_in_country: relay.include_in_country,
- active: relay.active,
- owned: relay.owned,
- provider: relay.provider.clone(),
- weight: relay.weight,
- tunnels: Some(types::RelayTunnels {
- openvpn: relay
- .tunnels
- .openvpn
- .iter()
- .map(|endpoint| types::OpenVpnEndpointData {
- port: u32::from(endpoint.port),
- protocol: i32::from(types::TransportProtocol::from(endpoint.protocol)),
- })
- .collect(),
- wireguard: relay
- .tunnels
- .wireguard
- .iter()
- .map(|endpoint| {
- let port_ranges = endpoint
- .port_ranges
- .iter()
- .map(|range| types::PortRange {
- first: u32::from(range.0),
- last: u32::from(range.1),
- })
- .collect();
- types::WireguardEndpointData {
- port_ranges,
- ipv4_gateway: endpoint.ipv4_gateway.to_string(),
- ipv6_gateway: endpoint.ipv6_gateway.to_string(),
- public_key: endpoint.public_key.as_bytes().to_vec(),
- }
- })
- .collect(),
- }),
- bridges: Some(types::RelayBridges {
- shadowsocks: relay
- .bridges
- .shadowsocks
- .iter()
- .map(|endpoint| types::ShadowsocksEndpointData {
- port: u32::from(endpoint.port),
- cipher: endpoint.cipher.clone(),
- password: endpoint.password.clone(),
- protocol: i32::from(types::TransportProtocol::from(endpoint.protocol)),
- })
- .collect(),
- }),
- location: relay.location.as_ref().map(|location| types::Location {
- country: location.country.clone(),
- country_code: location.country_code.clone(),
- city: location.city.clone(),
- city_code: location.city_code.clone(),
- latitude: location.latitude,
- longitude: location.longitude,
- }),
- }
-}
-
fn convert_state(state: TunnelState) -> types::TunnelState {
use talpid_types::tunnel::{
ActionAfterDisconnect, ErrorStateCause, FirewallPolicyError, ParameterGenerationError,
@@ -1135,8 +1042,10 @@ impl EventListener for ManagementInterfaceEventBroadcaster {
countries: Vec::new(),
};
new_list.countries.reserve(relay_list.countries.len());
- for country in &relay_list.countries {
- new_list.countries.push(convert_relay_list_country(country));
+ for country in relay_list.countries.into_iter() {
+ new_list
+ .countries
+ .push(types::RelayListCountry::from(country));
}
self.notify(types::DaemonEvent {
event: Some(daemon_event::Event::RelayList(new_list)),
diff --git a/mullvad-management-interface/src/types.rs b/mullvad-management-interface/src/types.rs
index fe3779b80b..bb90225745 100644
--- a/mullvad-management-interface/src/types.rs
+++ b/mullvad-management-interface/src/types.rs
@@ -290,6 +290,99 @@ impl From<&mullvad_types::settings::TunnelOptions> for TunnelOptions {
}
}
+impl From<mullvad_types::relay_list::RelayListCountry> for RelayListCountry {
+ fn from(country: mullvad_types::relay_list::RelayListCountry) -> Self {
+ let mut proto_country = RelayListCountry {
+ name: country.name,
+ code: country.code,
+ cities: Vec::with_capacity(country.cities.len()),
+ };
+
+ for city in country.cities.into_iter() {
+ proto_country.cities.push(RelayListCity {
+ name: city.name,
+ code: city.code,
+ latitude: city.latitude,
+ longitude: city.longitude,
+ relays: city.relays.into_iter().map(Relay::from).collect(),
+ });
+ }
+
+ proto_country
+ }
+}
+
+impl From<mullvad_types::relay_list::Relay> for Relay {
+ fn from(relay: mullvad_types::relay_list::Relay) -> Self {
+ Self {
+ hostname: relay.hostname,
+ ipv4_addr_in: relay.ipv4_addr_in.to_string(),
+ ipv6_addr_in: relay
+ .ipv6_addr_in
+ .map(|addr| addr.to_string())
+ .unwrap_or_default(),
+ include_in_country: relay.include_in_country,
+ active: relay.active,
+ owned: relay.owned,
+ provider: relay.provider,
+ weight: relay.weight,
+ tunnels: Some(RelayTunnels {
+ openvpn: relay
+ .tunnels
+ .openvpn
+ .iter()
+ .map(|endpoint| OpenVpnEndpointData {
+ port: u32::from(endpoint.port),
+ protocol: i32::from(TransportProtocol::from(endpoint.protocol)),
+ })
+ .collect(),
+ wireguard: relay
+ .tunnels
+ .wireguard
+ .iter()
+ .map(|endpoint| {
+ let port_ranges = endpoint
+ .port_ranges
+ .iter()
+ .map(|range| PortRange {
+ first: u32::from(range.0),
+ last: u32::from(range.1),
+ })
+ .collect();
+ WireguardEndpointData {
+ port_ranges,
+ ipv4_gateway: endpoint.ipv4_gateway.to_string(),
+ ipv6_gateway: endpoint.ipv6_gateway.to_string(),
+ public_key: endpoint.public_key.as_bytes().to_vec(),
+ }
+ })
+ .collect(),
+ }),
+ bridges: Some(RelayBridges {
+ shadowsocks: relay
+ .bridges
+ .shadowsocks
+ .into_iter()
+ .map(|endpoint| ShadowsocksEndpointData {
+ port: u32::from(endpoint.port),
+ cipher: endpoint.cipher,
+ password: endpoint.password,
+ protocol: i32::from(TransportProtocol::from(endpoint.protocol)),
+ })
+ .collect(),
+ }),
+ location: relay.location.map(|location| Location {
+ country: location.country,
+ country_code: location.country_code,
+ city: location.city,
+ city_code: location.city_code,
+ latitude: location.latitude,
+ longitude: location.longitude,
+ }),
+ }
+ }
+}
+
impl From<TransportProtocol> for talpid_types::net::TransportProtocol {
fn from(protocol: TransportProtocol) -> Self {
match protocol {