summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorDavid Lönnhager <david.l@mullvad.net>2020-09-07 17:29:05 +0200
committerDavid Lönnhager <david.l@mullvad.net>2020-09-07 17:51:16 +0200
commitca8c485cf9c72b7584461a0720fdacf90bf672a4 (patch)
treec9a24fe13f62570baa68a8336dcd0fe607ddf20e
parentdee58a912fe6243374c82523ec3752853e239de0 (diff)
downloadmullvadvpn-ca8c485cf9c72b7584461a0720fdacf90bf672a4.tar.xz
mullvadvpn-ca8c485cf9c72b7584461a0720fdacf90bf672a4.zip
Use new-style futures for geoip requests
-rw-r--r--Cargo.lock1
-rw-r--r--mullvad-daemon/Cargo.toml3
-rw-r--r--mullvad-daemon/src/geoip.rs75
-rw-r--r--mullvad-daemon/src/lib.rs5
4 files changed, 38 insertions, 46 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 6503332d77..84cbd3e506 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -1126,7 +1126,6 @@ dependencies = [
"duct 0.13.4 (registry+https://github.com/rust-lang/crates.io-index)",
"err-derive 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)",
"fern 0.5.9 (registry+https://github.com/rust-lang/crates.io-index)",
- "futures 0.1.29 (registry+https://github.com/rust-lang/crates.io-index)",
"futures 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)",
"ipnetwork 0.16.0 (registry+https://github.com/rust-lang/crates.io-index)",
"lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
diff --git a/mullvad-daemon/Cargo.toml b/mullvad-daemon/Cargo.toml
index dabaa1241a..2d3f28a196 100644
--- a/mullvad-daemon/Cargo.toml
+++ b/mullvad-daemon/Cargo.toml
@@ -13,8 +13,7 @@ chrono = { version = "0.4", features = ["serde"] }
clap = "2.25"
err-derive = "0.2.1"
fern = { version = "0.5", features = ["colored"] }
-futures01 = { package = "futures", version = "0.1" }
-futures = { package = "futures", version = "0.3", features = [ "compat" ]}
+futures = "0.3"
ipnetwork = "0.16"
lazy_static = "1.0"
log = "0.4"
diff --git a/mullvad-daemon/src/geoip.rs b/mullvad-daemon/src/geoip.rs
index 991ced7e5f..953780b6f2 100644
--- a/mullvad-daemon/src/geoip.rs
+++ b/mullvad-daemon/src/geoip.rs
@@ -1,53 +1,50 @@
-use futures01::{self, Future};
+use futures::join;
use mullvad_rpc::{self, rest::RequestServiceHandle};
use mullvad_types::location::{AmIMullvad, GeoIpLocation};
-
const URI_V4: &str = "https://ipv4.am.i.mullvad.net/json";
const URI_V6: &str = "https://ipv6.am.i.mullvad.net/json";
-pub fn send_location_request(
+pub async fn send_location_request(
request_sender: RequestServiceHandle,
-) -> impl Future<Item = GeoIpLocation, Error = mullvad_rpc::rest::Error> {
- let v4_future =
- send_location_request_internal(URI_V4, request_sender.clone()).map(GeoIpLocation::from);
- let v6_future = send_location_request_internal(URI_V6, request_sender).map(GeoIpLocation::from);
+) -> Result<GeoIpLocation, mullvad_rpc::rest::Error> {
+ let v4_sender = request_sender.clone();
+ let v4_future = async move {
+ let location = send_location_request_internal(URI_V4, v4_sender).await?;
+ Ok(GeoIpLocation::from(location))
+ };
+ let v6_sender = request_sender.clone();
+ let v6_future = async move {
+ let location = send_location_request_internal(URI_V6, v6_sender).await?;
+ Ok::<GeoIpLocation, mullvad_rpc::rest::Error>(GeoIpLocation::from(location))
+ };
- v4_future.then(
- |v4_result: Result<GeoIpLocation, mullvad_rpc::rest::Error>| {
- v6_future.then(
- |v6_result: Result<GeoIpLocation, mullvad_rpc::rest::Error>| match (
- v4_result, v6_result,
- ) {
- (Ok(mut v4), Ok(v6)) => {
- v4.ipv6 = v6.ipv6;
- v4.mullvad_exit_ip = v4.mullvad_exit_ip && v6.mullvad_exit_ip;
- Ok(v4)
- }
- (Ok(v4), Err(e)) => {
- log::debug!("Unable to fetch IPv6 GeoIP location: {}", e);
- Ok(v4)
- }
- (Err(e), Ok(v6)) => {
- log::debug!("Unable to fetch IPv4 GeoIP location: {}", e);
- Ok(v6)
- }
- (Err(e_v4), Err(_)) => Err(e_v4),
- },
- )
- },
- )
+ let (v4_result, v6_result) = join!(v4_future, v6_future);
+
+ match (v4_result, v6_result) {
+ (Ok(mut v4), Ok(v6)) => {
+ v4.ipv6 = v6.ipv6;
+ v4.mullvad_exit_ip = v4.mullvad_exit_ip && v6.mullvad_exit_ip;
+ Ok(v4)
+ }
+ (Ok(v4), Err(e)) => {
+ log::debug!("Unable to fetch IPv6 GeoIP location: {}", e);
+ Ok(v4)
+ }
+ (Err(e), Ok(v6)) => {
+ log::debug!("Unable to fetch IPv4 GeoIP location: {}", e);
+ Ok(v6)
+ }
+ (Err(e_v4), Err(_)) => Err(e_v4),
+ }
}
-fn send_location_request_internal(
+async fn send_location_request_internal(
uri: &'static str,
service: RequestServiceHandle,
-) -> impl Future<Item = AmIMullvad, Error = mullvad_rpc::rest::Error> {
+) -> Result<AmIMullvad, mullvad_rpc::rest::Error> {
let future_service = service.clone();
- let future = async move {
- let request = mullvad_rpc::rest::RestRequest::get(uri)?;
- let response = future_service.request(request).await?;
- mullvad_rpc::rest::deserialize_body(response).await
- };
- service.compat_spawn(future)
+ let request = mullvad_rpc::rest::RestRequest::get(uri)?;
+ let response = future_service.request(request).await?;
+ mullvad_rpc::rest::deserialize_body(response).await
}
diff --git a/mullvad-daemon/src/lib.rs b/mullvad-daemon/src/lib.rs
index 5a0b3e551b..430ace18f0 100644
--- a/mullvad-daemon/src/lib.rs
+++ b/mullvad-daemon/src/lib.rs
@@ -21,11 +21,9 @@ mod version_check;
use futures::{
channel::{mpsc, oneshot},
- compat::Future01CompatExt,
future::{abortable, AbortHandle, Future},
StreamExt,
};
-use futures01::Future as Future01;
use log::{debug, error, info, warn};
use mullvad_rpc::AccountsProxy;
use mullvad_types::{
@@ -1207,11 +1205,10 @@ where
async {
geoip::send_location_request(https_handle)
+ .await
.map_err(|e| {
warn!("Unable to fetch GeoIP location: {}", e.display_chain());
})
- .compat()
- .await
}
}