Skip to content

Commit

Permalink
Move FeatureIndicators to TunnelState
Browse files Browse the repository at this point in the history
Do not broadcast new feature indicators as a daemon event. Rather, it is
now part of the tunnel state, which ties their presence to the tunnel
states in which we would be concerned to present them to the user.
  • Loading branch information
MarkusPettersson98 committed Jul 30, 2024
1 parent 384919e commit 027f72a
Show file tree
Hide file tree
Showing 11 changed files with 80 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,6 @@ class ManagementService(
}
ManagementInterface.DaemonEvent.EventCase.REMOVE_DEVICE -> {}
ManagementInterface.DaemonEvent.EventCase.EVENT_NOT_SET -> {}
ManagementInterface.DaemonEvent.EventCase.FEATURE_INDICATORS -> {}
}
}
}
Expand Down
5 changes: 0 additions & 5 deletions mullvad-cli/src/cmds/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,6 @@ impl Status {
println!("New access method: {access_method:#?}");
}
}
DaemonEvent::FeatureIndicators(feature_indicators) => {
if args.debug {
println!("New feature indicators: {feature_indicators:#?}");
}
}
}
}
Ok(())
Expand Down
12 changes: 10 additions & 2 deletions mullvad-cli/src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ pub fn print_state(state: &TunnelState, verbose: bool) {

match state {
Error(error) => print_error_state(error),
Connected { endpoint, location } => {
Connected {
endpoint,
location,
feature_indicators: _,
} => {
println!(
"Connected to {}",
format_relay_connection(endpoint, location.as_ref(), verbose)
Expand All @@ -30,7 +34,11 @@ pub fn print_state(state: &TunnelState, verbose: bool) {
}
}
}
Connecting { endpoint, location } => {
Connecting {
endpoint,
location,
feature_indicators: _,
} => {
let ellipsis = if !verbose { "..." } else { "" };
println!(
"Connecting to {}{ellipsis}",
Expand Down
31 changes: 6 additions & 25 deletions mullvad-daemon/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -397,9 +397,6 @@ pub(crate) enum InternalDaemonEvent {
/// The split tunnel paths or state were updated.
#[cfg(any(windows, target_os = "android", target_os = "macos"))]
ExcludedPathsEvent(ExcludedPathsUpdate, oneshot::Sender<Result<(), Error>>),

/// A setting in the [SettingsPersister] was changed.
SettingsChanged,
}

#[cfg(any(windows, target_os = "android", target_os = "macos"))]
Expand Down Expand Up @@ -571,9 +568,6 @@ pub trait EventListener: Clone + Send + Sync + 'static {

/// Notify that the api access method changed.
fn notify_new_access_method_event(&self, new_access_method: AccessMethodSetting);

/// Notify that the feature indicators have changed.
fn notify_feature_indicators(&self, feature_indicators: FeatureIndicators);
}

pub struct Daemon<L: EventListener> {
Expand Down Expand Up @@ -751,11 +745,6 @@ where
settings.tunnel_options.clone(),
);

let settings_daemon_tx = internal_event_tx.clone();
settings.register_change_listener(move |_| {
let _ = settings_daemon_tx.send(InternalDaemonEvent::SettingsChanged);
});

let param_gen = parameters_generator.clone();
let (param_gen_tx, mut param_gen_rx) = mpsc::unbounded();
tokio::spawn(async move {
Expand Down Expand Up @@ -961,7 +950,6 @@ where
LocationEvent(location_data) => self.handle_location_event(location_data),
#[cfg(any(windows, target_os = "android", target_os = "macos"))]
ExcludedPathsEvent(update, tx) => self.handle_new_excluded_paths(update, tx).await,
SettingsChanged => self.handle_settings_changed(),
}
should_stop
}
Expand All @@ -980,12 +968,14 @@ where
locked_down,
},
TunnelStateTransition::Connecting(endpoint) => TunnelState::Connecting {
location: self.parameters_generator.get_last_location().await,
endpoint,
location: self.parameters_generator.get_last_location().await,
feature_indicators: self.get_feature_indicators(),
},
TunnelStateTransition::Connected(endpoint) => TunnelState::Connected {
location: self.parameters_generator.get_last_location().await,
endpoint,
location: self.parameters_generator.get_last_location().await,
feature_indicators: self.get_feature_indicators(),
},
TunnelStateTransition::Disconnecting(after_disconnect) => {
TunnelState::Disconnecting(after_disconnect)
Expand Down Expand Up @@ -1039,8 +1029,6 @@ where

self.tunnel_state = tunnel_state.clone();
self.event_listener.notify_new_state(tunnel_state);
self.event_listener
.notify_feature_indicators(self.get_feature_indicators());
self.fetch_am_i_mullvad();
}

Expand Down Expand Up @@ -1420,11 +1408,6 @@ where
let _ = tx.send(save_result.map(|_| ()));
}

fn handle_settings_changed(&mut self) {
self.event_listener
.notify_feature_indicators(self.get_feature_indicators())
}

async fn on_set_target_state(
&mut self,
tx: oneshot::Sender<bool>,
Expand Down Expand Up @@ -2893,13 +2876,11 @@ where
};

// use the booleans to filter into a list of only the active features
let active_features = generic_features
generic_features
.into_iter()
.chain(protocol_features)
.filter_map(|(active, feature)| active.then_some(feature))
.collect();

FeatureIndicators { active_features }
.collect()
}
}

Expand Down
9 changes: 0 additions & 9 deletions mullvad-daemon/src/management_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1158,15 +1158,6 @@ impl EventListener for ManagementInterfaceEventBroadcaster {
)),
})
}

fn notify_feature_indicators(&self, features: mullvad_types::features::FeatureIndicators) {
log::debug!("Broadcasting new feature indicators");
self.notify(types::DaemonEvent {
event: Some(daemon_event::Event::FeatureIndicators(
types::FeatureIndicators::from(features),
)),
})
}
}

impl ManagementInterfaceEventBroadcaster {
Expand Down
11 changes: 8 additions & 3 deletions mullvad-management-interface/proto/management_interface.proto
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,14 @@ message TunnelState {
GeoIpLocation disconnected_location = 1;
bool locked_down = 2;
}
message Connecting { TunnelStateRelayInfo relay_info = 1; }
message Connected { TunnelStateRelayInfo relay_info = 1; }
message Connecting {
TunnelStateRelayInfo relay_info = 1;
FeatureIndicators feature_indicators = 2;
}
message Connected {
TunnelStateRelayInfo relay_info = 1;
FeatureIndicators feature_indicators = 2;
}
message Disconnecting { AfterDisconnect after_disconnect = 1; }
message Error { ErrorState error_state = 1; }

Expand Down Expand Up @@ -649,7 +655,6 @@ message DaemonEvent {
DeviceEvent device = 5;
RemoveDeviceEvent remove_device = 6;
AccessMethodSetting new_access_method = 7;
FeatureIndicators feature_indicators = 8;
}
}

Expand Down
6 changes: 1 addition & 5 deletions mullvad-management-interface/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use mullvad_types::wireguard::DaitaSettings;
use mullvad_types::{
access_method::AccessMethodSetting,
device::{DeviceEvent, RemoveDeviceEvent},
features::FeatureIndicators,
relay_list::RelayList,
settings::Settings,
states::TunnelState,
Expand All @@ -21,6 +20,7 @@ use mullvad_types::{
account::{AccountData, AccountToken, VoucherSubmission},
custom_list::{CustomList, Id},
device::{Device, DeviceId, DeviceState},
features::FeatureIndicators,
relay_constraints::{
BridgeSettings, BridgeState, ObfuscationSettings, RelayOverride, RelaySettings,
},
Expand Down Expand Up @@ -51,7 +51,6 @@ pub enum DaemonEvent {
Device(DeviceEvent),
RemoveDevice(RemoveDeviceEvent),
NewAccessMethod(AccessMethodSetting),
FeatureIndicators(FeatureIndicators),
}

impl TryFrom<types::daemon_event::Event> for DaemonEvent {
Expand Down Expand Up @@ -82,9 +81,6 @@ impl TryFrom<types::daemon_event::Event> for DaemonEvent {
.map(DaemonEvent::NewAccessMethod)
.map_err(Error::InvalidResponse)
}
types::daemon_event::Event::FeatureIndicators(event) => Ok(
DaemonEvent::FeatureIndicators(FeatureIndicators::from(event)),
),
}
}
}
Expand Down
13 changes: 5 additions & 8 deletions mullvad-management-interface/src/types/conversions/features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,10 @@ impl From<proto::FeatureIndicator> for mullvad_types::features::FeatureIndicator

impl From<proto::FeatureIndicators> for mullvad_types::features::FeatureIndicators {
fn from(features: proto::FeatureIndicators) -> Self {
Self {
active_features: features
.active_features()
.map(mullvad_types::features::FeatureIndicator::from)
.collect(),
}
features
.active_features()
.map(mullvad_types::features::FeatureIndicator::from)
.collect()
}
}

Expand All @@ -57,8 +55,7 @@ impl From<mullvad_types::features::FeatureIndicators> for proto::FeatureIndicato
let mut proto_features = Self::default();

features
.active_features
.into_iter()
.active_features()
.map(proto::FeatureIndicator::from)
.for_each(|feature| proto_features.push_active_features(feature));

Expand Down
51 changes: 35 additions & 16 deletions mullvad-management-interface/src/types/conversions/states.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,22 +39,28 @@ impl From<mullvad_types::states::TunnelState> for proto::TunnelState {
disconnected_location: disconnected_location.map(proto::GeoIpLocation::from),
locked_down,
}),
MullvadTunnelState::Connecting { endpoint, location } => {
proto::tunnel_state::State::Connecting(proto::tunnel_state::Connecting {
relay_info: Some(proto::TunnelStateRelayInfo {
tunnel_endpoint: Some(proto::TunnelEndpoint::from(endpoint)),
location: location.map(proto::GeoIpLocation::from),
}),
})
}
MullvadTunnelState::Connected { endpoint, location } => {
proto::tunnel_state::State::Connected(proto::tunnel_state::Connected {
relay_info: Some(proto::TunnelStateRelayInfo {
tunnel_endpoint: Some(proto::TunnelEndpoint::from(endpoint)),
location: location.map(proto::GeoIpLocation::from),
}),
})
}
MullvadTunnelState::Connecting {
endpoint,
location,
feature_indicators: features_indicators,
} => proto::tunnel_state::State::Connecting(proto::tunnel_state::Connecting {
relay_info: Some(proto::TunnelStateRelayInfo {
tunnel_endpoint: Some(proto::TunnelEndpoint::from(endpoint)),
location: location.map(proto::GeoIpLocation::from),
}),
feature_indicators: Some(proto::FeatureIndicators::from(features_indicators)),
}),
MullvadTunnelState::Connected {
endpoint,
location,
feature_indicators: features_indicators,
} => proto::tunnel_state::State::Connected(proto::tunnel_state::Connected {
relay_info: Some(proto::TunnelStateRelayInfo {
tunnel_endpoint: Some(proto::TunnelEndpoint::from(endpoint)),
location: location.map(proto::GeoIpLocation::from),
}),
feature_indicators: Some(proto::FeatureIndicators::from(features_indicators)),
}),
MullvadTunnelState::Disconnecting(after_disconnect) => {
proto::tunnel_state::State::Disconnecting(proto::tunnel_state::Disconnecting {
after_disconnect: match after_disconnect {
Expand Down Expand Up @@ -229,23 +235,36 @@ impl TryFrom<proto::TunnelState> for mullvad_types::states::TunnelState {
tunnel_endpoint: Some(tunnel_endpoint),
location,
}),
feature_indicators,
})) => MullvadState::Connecting {
endpoint: talpid_net::TunnelEndpoint::try_from(tunnel_endpoint)?,
location: location
.map(mullvad_types::location::GeoIpLocation::try_from)
.transpose()?,
feature_indicators: feature_indicators
.map(mullvad_types::features::FeatureIndicators::from)
.ok_or(FromProtobufTypeError::InvalidArgument(
"Missing feature indicators",
))?,
},
Some(proto::tunnel_state::State::Connected(proto::tunnel_state::Connected {
relay_info:
Some(proto::TunnelStateRelayInfo {
tunnel_endpoint: Some(tunnel_endpoint),
location,
}),
feature_indicators,
})) => MullvadState::Connected {
endpoint: talpid_net::TunnelEndpoint::try_from(tunnel_endpoint)?,
location: location
.map(mullvad_types::location::GeoIpLocation::try_from)
.transpose()?,

feature_indicators: feature_indicators
.map(mullvad_types::features::FeatureIndicators::from)
.ok_or(FromProtobufTypeError::InvalidArgument(
"Missing feature indicators",
))?,
},
Some(proto::tunnel_state::State::Disconnecting(
proto::tunnel_state::Disconnecting { after_disconnect },
Expand Down
14 changes: 12 additions & 2 deletions mullvad-types/src/features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,18 @@ use serde::{Deserialize, Serialize};
///
/// Note that the feature indicators are not ordered.
#[derive(Default, Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct FeatureIndicators {
pub active_features: HashSet<FeatureIndicator>,
pub struct FeatureIndicators(HashSet<FeatureIndicator>);

impl FeatureIndicators {
pub fn active_features(&self) -> impl Iterator<Item = FeatureIndicator> {
self.0.clone().into_iter()
}
}

impl FromIterator<FeatureIndicator> for FeatureIndicators {
fn from_iter<T: IntoIterator<Item = FeatureIndicator>>(iter: T) -> Self {
Self(iter.into_iter().collect())
}
}

/// All possible feature indicators. These represent a subset of all VPN settings in a
Expand Down
4 changes: 3 additions & 1 deletion mullvad-types/src/states.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::location::GeoIpLocation;
use crate::{features::FeatureIndicators, location::GeoIpLocation};
use serde::{Deserialize, Serialize};
use std::fmt;
use talpid_types::{
Expand Down Expand Up @@ -38,10 +38,12 @@ pub enum TunnelState {
Connecting {
endpoint: TunnelEndpoint,
location: Option<GeoIpLocation>,
feature_indicators: FeatureIndicators,
},
Connected {
endpoint: TunnelEndpoint,
location: Option<GeoIpLocation>,
feature_indicators: FeatureIndicators,
},
Disconnecting(ActionAfterDisconnect),
Error(ErrorState),
Expand Down

0 comments on commit 027f72a

Please sign in to comment.