Skip to content

Commit

Permalink
Get rid of extraneous calls to clone
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkusPettersson98 committed Sep 28, 2023
1 parent d837da8 commit c68d028
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 46 deletions.
8 changes: 3 additions & 5 deletions mullvad-api/src/https_client_with_sni.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ enum InnerConnectionMode {

impl InnerConnectionMode {
async fn connect(
&self,
self,
hostname: &str,
addr: &SocketAddr,
#[cfg(target_os = "android")] socket_bypass_tx: Option<mpsc::Sender<SocketBypassRequest>>,
Expand All @@ -102,8 +102,7 @@ impl InnerConnectionMode {
.await
}
// Set up a Shadowsocks-connection.
InnerConnectionMode::Shadowsocks(config) => {
let shadowsocks = config.clone();
InnerConnectionMode::Shadowsocks(shadowsocks) => {
let first_hop = shadowsocks.params.peer;
let make_proxy_stream = |tcp_stream| async {
Ok(ProxyClientStream::from_stream(
Expand All @@ -123,8 +122,7 @@ impl InnerConnectionMode {
.await
}
// Set up a SOCKS5-connection.
InnerConnectionMode::Socks5(config) => {
let socks = config.clone();
InnerConnectionMode::Socks5(socks) => {
let first_hop = socks.peer;
let make_proxy_stream = |tcp_stream| async {
tokio_socks::tcp::Socks5Stream::connect_with_socket(tcp_stream, addr)
Expand Down
80 changes: 39 additions & 41 deletions mullvad-daemon/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use mullvad_api::{
ApiEndpointUpdateCallback,
};
use mullvad_relay_selector::RelaySelector;
use mullvad_types::access_method::AccessMethodSetting;
use mullvad_types::access_method::{AccessMethod, AccessMethodSetting, BuiltInAccessMethod};
use std::{
net::SocketAddr,
path::PathBuf,
Expand Down Expand Up @@ -106,10 +106,13 @@ impl ApiConnectionModeProvider {
log::debug!("Rotating Access mode!");
let access_method = {
let mut access_methods_picker = self.connection_modes.lock().unwrap();
access_methods_picker.next()
access_methods_picker
.next()
.map(|access_method_setting| access_method_setting.access_method)
.unwrap_or(AccessMethod::from(BuiltInAccessMethod::Direct))
};

let connection_mode = self.from(access_method.as_ref());
let connection_mode = self.from(access_method);
log::info!("New API connection mode selected: {}", connection_mode);
connection_mode
}
Expand All @@ -118,45 +121,40 @@ impl ApiConnectionModeProvider {
/// [`ApiConnectionMode`]s require extra logic/data from
/// [`ApiConnectionModeProvider`] the standard [`std::convert::From`] trait
/// can not be implemented.
fn from(&mut self, access_method: Option<&AccessMethodSetting>) -> ApiConnectionMode {
use mullvad_types::access_method::{self, AccessMethod, BuiltInAccessMethod};
fn from(&mut self, access_method: AccessMethod) -> ApiConnectionMode {
use mullvad_types::access_method;
match access_method {
None => ApiConnectionMode::Direct,
Some(access_method) => match &access_method.access_method {
AccessMethod::BuiltIn(access_method) => match access_method {
BuiltInAccessMethod::Direct => ApiConnectionMode::Direct,
BuiltInAccessMethod::Bridge => self
.relay_selector
.get_bridge_forced()
.and_then(|settings| match settings {
ProxySettings::Shadowsocks(ss_settings) => {
let ss_settings: access_method::Shadowsocks =
access_method::Shadowsocks::new(
ss_settings.peer,
ss_settings.cipher,
ss_settings.password,
);
Some(ApiConnectionMode::Proxied(ProxyConfig::Shadowsocks(
ss_settings,
)))
}
_ => {
log::error!("Received unexpected proxy settings type");
None
}
})
.unwrap_or(ApiConnectionMode::Direct),
},
AccessMethod::Custom(access_method) => match &access_method {
access_method::CustomAccessMethod::Shadowsocks(shadowsocks_config) => {
ApiConnectionMode::Proxied(ProxyConfig::Shadowsocks(
shadowsocks_config.clone(),
))
}
access_method::CustomAccessMethod::Socks5(socks_config) => {
ApiConnectionMode::Proxied(ProxyConfig::Socks(socks_config.clone()))
}
},
AccessMethod::BuiltIn(access_method) => match access_method {
BuiltInAccessMethod::Direct => ApiConnectionMode::Direct,
BuiltInAccessMethod::Bridge => self
.relay_selector
.get_bridge_forced()
.and_then(|settings| match settings {
ProxySettings::Shadowsocks(ss_settings) => {
let ss_settings: access_method::Shadowsocks =
access_method::Shadowsocks::new(
ss_settings.peer,
ss_settings.cipher,
ss_settings.password,
);
Some(ApiConnectionMode::Proxied(ProxyConfig::Shadowsocks(
ss_settings,
)))
}
_ => {
log::error!("Received unexpected proxy settings type");
None
}
})
.unwrap_or(ApiConnectionMode::Direct),
},
AccessMethod::Custom(access_method) => match access_method {
access_method::CustomAccessMethod::Shadowsocks(shadowsocks_config) => {
ApiConnectionMode::Proxied(ProxyConfig::Shadowsocks(shadowsocks_config))
}
access_method::CustomAccessMethod::Socks5(socks_config) => {
ApiConnectionMode::Proxied(ProxyConfig::Socks(socks_config))
}
},
}
}
Expand Down

0 comments on commit c68d028

Please sign in to comment.