summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorDavid Lönnhager <david.l@mullvad.net>2024-08-13 14:32:56 +0200
committerDavid Lönnhager <david.l@mullvad.net>2024-08-14 09:13:04 +0200
commita2de5327b9241252b30f92bdc99ec9dd9efaa12a (patch)
tree7974d66337c90d4efc2f48afe5911df7b12b3fdc
parent0e7d02639763f9935100053993e94639dd0ab1a1 (diff)
downloadmullvadvpn-a2de5327b9241252b30f92bdc99ec9dd9efaa12a.tar.xz
mullvadvpn-a2de5327b9241252b30f92bdc99ec9dd9efaa12a.zip
Add typed target tunnel state
-rw-r--r--Cargo.lock2
-rw-r--r--mullvad-daemon/Cargo.toml1
-rw-r--r--mullvad-daemon/src/lib.rs39
-rw-r--r--mullvad-types/Cargo.toml1
-rw-r--r--mullvad-types/src/states.rs39
-rw-r--r--test/Cargo.lock1
6 files changed, 70 insertions, 13 deletions
diff --git a/Cargo.lock b/Cargo.lock
index d955c2ca58..f7cbefd713 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -2193,6 +2193,7 @@ dependencies = [
"clap",
"ctrlc",
"dirs",
+ "either",
"fern",
"futures",
"libc",
@@ -2394,6 +2395,7 @@ version = "0.0.0"
dependencies = [
"chrono",
"clap",
+ "either",
"intersection-derive",
"ipnetwork",
"log",
diff --git a/mullvad-daemon/Cargo.toml b/mullvad-daemon/Cargo.toml
index 41172ad486..0aaa6565ee 100644
--- a/mullvad-daemon/Cargo.toml
+++ b/mullvad-daemon/Cargo.toml
@@ -17,6 +17,7 @@ api-override = ["mullvad-api/api-override"]
[dependencies]
chrono = { workspace = true }
thiserror = { workspace = true }
+either = "1.11"
fern = { version = "0.6", features = ["colored"] }
futures = "0.3"
once_cell = { workspace = true }
diff --git a/mullvad-daemon/src/lib.rs b/mullvad-daemon/src/lib.rs
index f0bb41a1a8..224d7e4b2c 100644
--- a/mullvad-daemon/src/lib.rs
+++ b/mullvad-daemon/src/lib.rs
@@ -60,7 +60,7 @@ use mullvad_types::{
},
relay_list::RelayList,
settings::{DnsOptions, DnsState, Settings},
- states::{TargetState, TunnelState},
+ states::{Secured, TargetState, TargetStateStrict, TunnelState},
version::{AppVersion, AppVersionInfo},
wireguard::{PublicKey, QuantumResistantState, RotationInterval},
};
@@ -852,17 +852,7 @@ impl Daemon {
/// Consume the `Daemon` and run the main event loop. Blocks until an error happens or a
/// shutdown event is received.
pub async fn run(mut self) -> Result<(), Error> {
- match *self.target_state {
- TargetState::Secured => {
- self.connect_tunnel();
- }
- TargetState::Unsecured => {
- // Fetching GeoIpLocation is automatically done when connecting.
- // If TargetState is Unsecured we will not connect on lauch and
- // so we have to explicitly fetch this information.
- self.fetch_am_i_mullvad()
- }
- }
+ self.handle_initial_target_state();
while let Some(event) = self.rx.next().await {
if self.handle_event(event).await {
@@ -891,6 +881,25 @@ impl Daemon {
Ok(())
}
+ fn handle_initial_target_state(&mut self) {
+ match self.target_state.to_strict() {
+ either::Either::Right(state) => {
+ self.send_tunnel_command(Self::secured_state_to_tunnel_command(state));
+ }
+ either::Either::Left(_) => {
+ // Fetching GeoIpLocation is automatically done when connecting.
+ // If TargetState is Unsecured we will not connect on lauch and
+ // so we have to explicitly fetch this information.
+ self.fetch_am_i_mullvad()
+ }
+ }
+ }
+
+ /// Map the secured target state to a tunnel command
+ const fn secured_state_to_tunnel_command(_: TargetStateStrict<Secured>) -> TunnelCommand {
+ TunnelCommand::Connect
+ }
+
/// Destroy daemon safely, by dropping all objects in the correct order, waiting for them to
/// be destroyed, and executing shutdown tasks
async fn finalize(self) {
@@ -985,6 +994,11 @@ impl Daemon {
self.unschedule_reconnect();
}
+ if self.tunnel_state.is_disconnected() && !tunnel_state.is_disconnected() {
+ // Enable background API requests when leaving the disconnected state.
+ self.api_handle.availability.resume_background();
+ }
+
log::debug!("New tunnel state: {:?}", tunnel_state);
match tunnel_state {
@@ -2816,7 +2830,6 @@ impl Daemon {
}
fn connect_tunnel(&mut self) {
- self.api_runtime.availability_handle().resume_background();
self.send_tunnel_command(TunnelCommand::Connect);
}
diff --git a/mullvad-types/Cargo.toml b/mullvad-types/Cargo.toml
index 0337435064..4e53931b30 100644
--- a/mullvad-types/Cargo.toml
+++ b/mullvad-types/Cargo.toml
@@ -11,6 +11,7 @@ rust-version.workspace = true
workspace = true
[dependencies]
+either = "1.11"
chrono = { workspace = true, features = ["clock", "serde"] }
thiserror = { workspace = true }
ipnetwork = { workspace = true }
diff --git a/mullvad-types/src/states.rs b/mullvad-types/src/states.rs
index cae80ae9b6..3bc33f8471 100644
--- a/mullvad-types/src/states.rs
+++ b/mullvad-types/src/states.rs
@@ -1,4 +1,5 @@
use crate::{features::FeatureIndicators, location::GeoIpLocation};
+use either::Either;
use serde::{Deserialize, Serialize};
use std::fmt;
use talpid_types::{
@@ -16,6 +17,44 @@ pub enum TargetState {
Secured,
}
+impl TargetState {
+ pub const fn to_strict(
+ &self,
+ ) -> Either<TargetStateStrict<Unsecured>, TargetStateStrict<Secured>> {
+ match self {
+ TargetState::Unsecured => Either::Left(TargetStateStrict::<Unsecured>::new()),
+ TargetState::Secured => Either::Right(TargetStateStrict::<Secured>::new()),
+ }
+ }
+}
+
+#[derive(Clone, Copy)]
+pub struct TargetStateStrict<T> {
+ _state: std::marker::PhantomData<T>,
+}
+
+#[derive(Clone, Copy)]
+pub struct Unsecured;
+
+#[derive(Clone, Copy)]
+pub struct Secured;
+
+impl TargetStateStrict<Unsecured> {
+ const fn new() -> Self {
+ Self {
+ _state: std::marker::PhantomData,
+ }
+ }
+}
+
+impl TargetStateStrict<Secured> {
+ const fn new() -> Self {
+ Self {
+ _state: std::marker::PhantomData,
+ }
+ }
+}
+
impl fmt::Display for TargetState {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
diff --git a/test/Cargo.lock b/test/Cargo.lock
index adf1655d6e..998176ab5a 100644
--- a/test/Cargo.lock
+++ b/test/Cargo.lock
@@ -1917,6 +1917,7 @@ name = "mullvad-types"
version = "0.0.0"
dependencies = [
"chrono",
+ "either",
"intersection-derive",
"ipnetwork",
"log",