diff options
| author | David Lönnhager <david.l@mullvad.net> | 2022-11-17 15:42:19 +0100 |
|---|---|---|
| committer | David Lönnhager <david.l@mullvad.net> | 2022-11-28 12:53:58 +0100 |
| commit | 04fdb6a9f11e167404a1dd36552b2348bc1035d3 (patch) | |
| tree | bb4a3e2297bcbaa348d830bf6af2ecff7ad02d2d | |
| parent | 5699d3f30333a7cc90eefb987b6c7e79ac14f423 (diff) | |
| download | mullvadvpn-04fdb6a9f11e167404a1dd36552b2348bc1035d3.tar.xz mullvadvpn-04fdb6a9f11e167404a1dd36552b2348bc1035d3.zip | |
Use once_cell instead of lazy_static in mullvad-api
| -rw-r--r-- | Cargo.lock | 2 | ||||
| -rw-r--r-- | mullvad-api/Cargo.toml | 2 | ||||
| -rw-r--r-- | mullvad-api/src/lib.rs | 7 | ||||
| -rw-r--r-- | mullvad-api/src/tls_stream.rs | 23 |
4 files changed, 16 insertions, 18 deletions
diff --git a/Cargo.lock b/Cargo.lock index 700c6cd464..79083db6d6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1570,9 +1570,9 @@ dependencies = [ "http", "hyper", "ipnetwork", - "lazy_static", "log", "mullvad-types", + "once_cell", "regex", "rustls-pemfile", "serde", diff --git a/mullvad-api/Cargo.toml b/mullvad-api/Cargo.toml index e02ac22e11..6699ab432c 100644 --- a/mullvad-api/Cargo.toml +++ b/mullvad-api/Cargo.toml @@ -25,7 +25,7 @@ serde_json = "1.0" tokio = { version = "1.8", features = ["macros", "time", "rt-multi-thread", "net", "io-std", "io-util", "fs"] } tokio-rustls = "0.23" rustls-pemfile = "0.2" -lazy_static = "1.1.0" +once_cell = "1.13" mullvad-types = { path = "../mullvad-types" } talpid-types = { path = "../talpid-types" } diff --git a/mullvad-api/src/lib.rs b/mullvad-api/src/lib.rs index 25a53d016c..1237007ff3 100644 --- a/mullvad-api/src/lib.rs +++ b/mullvad-api/src/lib.rs @@ -9,6 +9,7 @@ use mullvad_types::{ account::{AccountToken, VoucherSubmission}, version::AppVersion, }; +use once_cell::sync::Lazy; use proxy::ApiConnectionMode; use std::{ collections::BTreeMap, @@ -62,9 +63,7 @@ pub const API_IP_CACHE_FILENAME: &str = "api-ip-address.txt"; const ACCOUNTS_URL_PREFIX: &str = "accounts/v1"; const APP_URL_PREFIX: &str = "app/v1"; -lazy_static::lazy_static! { - static ref API: ApiEndpoint = ApiEndpoint::get(); -} +static API: Lazy<ApiEndpoint> = Lazy::new(|| ApiEndpoint::from_env_vars()); /// A hostname and socketaddr to reach the Mullvad REST API over. struct ApiEndpoint { @@ -80,7 +79,7 @@ impl ApiEndpoint { /// /// Panics if `MULLVAD_API_ADDR` has invalid contents or if only one of /// `MULLVAD_API_ADDR` or `MULLVAD_API_HOST` has been set but not the other. - fn get() -> ApiEndpoint { + fn from_env_vars() -> ApiEndpoint { const API_HOST_DEFAULT: &str = "api.mullvad.net"; const API_IP_DEFAULT: IpAddr = IpAddr::V4(Ipv4Addr::new(45, 83, 223, 196)); const API_PORT_DEFAULT: u16 = 443; diff --git a/mullvad-api/src/tls_stream.rs b/mullvad-api/src/tls_stream.rs index cad0268ac3..ac3b4c2e24 100644 --- a/mullvad-api/src/tls_stream.rs +++ b/mullvad-api/src/tls_stream.rs @@ -7,6 +7,7 @@ use std::{ }; use hyper::client::connect::{Connected, Connection}; +use once_cell::sync::Lazy; use tokio::io::{AsyncRead, AsyncWrite, ReadBuf}; use tokio_rustls::{ rustls::{self, ClientConfig, ServerName}, @@ -24,18 +25,16 @@ where S: AsyncRead + AsyncWrite + Unpin, { pub async fn connect_https(stream: S, domain: &str) -> io::Result<TlsStream<S>> { - lazy_static::lazy_static! { - static ref TLS_CONFIG: Arc<ClientConfig> = { - let config = ClientConfig::builder() - .with_safe_default_cipher_suites() - .with_safe_default_kx_groups() - .with_protocol_versions(&[&rustls::version::TLS13]) - .unwrap() - .with_root_certificates(read_cert_store()) - .with_no_client_auth(); - Arc::new(config) - }; - } + static TLS_CONFIG: Lazy<Arc<ClientConfig>> = Lazy::new(|| { + let config = ClientConfig::builder() + .with_safe_default_cipher_suites() + .with_safe_default_kx_groups() + .with_protocol_versions(&[&rustls::version::TLS13]) + .unwrap() + .with_root_certificates(read_cert_store()) + .with_no_client_auth(); + Arc::new(config) + }); let connector = TlsConnector::from(TLS_CONFIG.clone()); |
