diff options
| author | David Lönnhager <david.l@mullvad.net> | 2024-02-15 19:47:07 +0100 |
|---|---|---|
| committer | David Lönnhager <david.l@mullvad.net> | 2024-02-16 16:37:37 +0100 |
| commit | e471d0739446279b01022090ac4457fe337ca598 (patch) | |
| tree | a67ac87a161cae956cde7bc4023cfd12beba5a6a /mullvad-api/src/proxy.rs | |
| parent | c8a3a3be92098cf64bc9269b3c4791e41c3b500d (diff) | |
| download | mullvadvpn-e471d0739446279b01022090ac4457fe337ca598.tar.xz mullvadvpn-e471d0739446279b01022090ac4457fe337ca598.zip | |
Refactor API access methods
Diffstat (limited to 'mullvad-api/src/proxy.rs')
| -rw-r--r-- | mullvad-api/src/proxy.rs | 42 |
1 files changed, 37 insertions, 5 deletions
diff --git a/mullvad-api/src/proxy.rs b/mullvad-api/src/proxy.rs index 2b4821ba64..0915d1d23c 100644 --- a/mullvad-api/src/proxy.rs +++ b/mullvad-api/src/proxy.rs @@ -1,4 +1,3 @@ -use futures::Stream; use hyper::client::connect::Connected; use serde::{Deserialize, Serialize}; use std::{ @@ -18,6 +17,41 @@ use tokio::{ const CURRENT_CONFIG_FILENAME: &str = "api-endpoint.json"; +pub trait ConnectionModeProvider: Send { + /// Initial connection mode + fn initial(&self) -> ApiConnectionMode; + + /// Request a new connection mode from the provider + fn rotate(&self) -> impl std::future::Future<Output = ()> + Send; + + /// Receive changes to the connection mode, announced by the provider + fn receive(&mut self) -> impl std::future::Future<Output = Option<ApiConnectionMode>> + Send; +} + +pub struct StaticConnectionModeProvider { + mode: ApiConnectionMode, +} + +impl StaticConnectionModeProvider { + pub fn new(mode: ApiConnectionMode) -> Self { + Self { mode } + } +} + +impl ConnectionModeProvider for StaticConnectionModeProvider { + fn initial(&self) -> ApiConnectionMode { + self.mode.clone() + } + + fn rotate(&self) -> impl std::future::Future<Output = ()> + Send { + futures::future::ready(()) + } + + fn receive(&mut self) -> impl std::future::Future<Output = Option<ApiConnectionMode>> + Send { + futures::future::pending() + } +} + #[derive(Clone, Debug, PartialEq, Deserialize, Serialize)] pub enum ApiConnectionMode { /// Connect directly to the target. @@ -153,10 +187,8 @@ impl ApiConnectionMode { *self != ApiConnectionMode::Direct } - /// Convenience function that returns a stream that repeats - /// this config forever. - pub fn into_repeat(self) -> impl Stream<Item = ApiConnectionMode> { - futures::stream::repeat(self) + pub fn into_provider(self) -> StaticConnectionModeProvider { + StaticConnectionModeProvider::new(self) } } |
