diff options
| author | Markus Pettersson <markus.pettersson@mullvad.net> | 2024-01-16 10:17:35 +0100 |
|---|---|---|
| committer | Markus Pettersson <markus.pettersson@mullvad.net> | 2024-01-16 12:51:15 +0100 |
| commit | 2aac8e447b09916c214b2bfccfc192603d1e85a8 (patch) | |
| tree | 5052952471cc4643da6624d59fb67bf648f3cb71 /mullvad-api/src | |
| parent | b700b79d16c4980ee2028b4b5fb67caa785fbcfd (diff) | |
| download | mullvadvpn-2aac8e447b09916c214b2bfccfc192603d1e85a8.tar.xz mullvadvpn-2aac8e447b09916c214b2bfccfc192603d1e85a8.zip | |
Do not try to resolve API address if `MULLVAD_API_HOST` is not set
If the `api-override` feature is enabled, we allow overriding the ip
address to the Mullvad API by settings the `MULLVAD_API_ADDRESS` env
variable or have it be resolved automatically if `MULLVAD_API_HOST`
is overriden.
This commit fixes a bug where we would try to resolve the
API address when `MULLVAD_API_ADDRESS` was not set even if
`MULLVAD_API_ADDRESS` was not set either.
Diffstat (limited to 'mullvad-api/src')
| -rw-r--r-- | mullvad-api/src/lib.rs | 41 |
1 files changed, 24 insertions, 17 deletions
diff --git a/mullvad-api/src/lib.rs b/mullvad-api/src/lib.rs index adba236764..40ab7395cd 100644 --- a/mullvad-api/src/lib.rs +++ b/mullvad-api/src/lib.rs @@ -148,35 +148,42 @@ impl ApiEndpoint { let disable_tls_var = Self::read_var(ApiEndpoint::DISABLE_TLS_VAR); let mut api = ApiEndpoint { - host: host_var.clone(), + host: None, address: None, disable_address_cache: true, disable_tls: false, }; - api.address = match address_var { - Some(user_addr) => { - let addr = user_addr.parse().unwrap_or_else(|_| { - panic!( - "{api_addr}={user_addr} is not a valid socketaddr", - api_addr = ApiEndpoint::API_ADDR_VAR, - ) - }); - Some(addr) - } - None => { + match (host_var, address_var) { + (None, None) => {} + (Some(host), None) => { use std::net::ToSocketAddrs; log::debug!( - "{api_addr} not found. Resolving API IP from {api_host}", + "{api_addr} not found. Resolving API IP address from {api_host}={host}", api_addr = ApiEndpoint::API_ADDR_VAR, api_host = ApiEndpoint::API_HOST_VAR ); - format!("{}:{}", api.host(), ApiEndpoint::API_PORT_DEFAULT) + api.address = format!("{}:{}", host, ApiEndpoint::API_PORT_DEFAULT) .to_socket_addrs() - .expect("failed to resolve API host") - .next() + .unwrap_or_else(|_| { + panic!( + "Unable to resolve API IP address from host {host}:{port}", + port = ApiEndpoint::API_PORT_DEFAULT, + ) + }) + .next(); } - }; + (host, Some(address)) => { + let addr = address.parse().unwrap_or_else(|_| { + panic!( + "{api_addr}={address} is not a valid socketaddr", + api_addr = ApiEndpoint::API_ADDR_VAR, + ) + }); + api.address = Some(addr); + api.host = host; + } + } if api.host.is_none() && api.address.is_none() { if disable_tls_var.is_some() { |
