Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify configuration selection for encrypted dns #6882

Merged
merged 1 commit into from
Oct 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 14 additions & 17 deletions mullvad-ios/src/encrypted_dns_proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,29 +82,26 @@ impl EncryptedDnsProxyState {
if self.should_reset() {
self.reset();
}

// TODO: currently, the randomized order of proxy config retrieval depends on the random
// iteration order of a given HashSet instance. Since for now, there will be only 2
// different configurations, it barely matters. In the future, we should use `rand`
// instead, so that the behavior is explicit and clear.

// First, try getting an obfuscated configuration, if there exist any.
let config = if let Some(obfuscated_config) = self
.configurations
.difference(&self.tried_configurations)
.find(|config| config.obfuscation.is_some())
.cloned()
{
obfuscated_config
} else {
// If no obfuscated configurations exist, try the next untried configuration.
self.configurations
.difference(&self.tried_configurations)
.next()
.cloned()?
let selected_config = {
// First, create an iterator for the difference between all configs and tried configs.
let mut difference = self.configurations.difference(&self.tried_configurations);
// Pick the first configuration if there are any. If there are none, one can only assume
// that the configuration set is empty, so an early return is fine.
let first_config = difference.next()?;
// See if there are any unused obfuscated configurations in the rest of the set.
let obfuscated_config = difference.find(|config| config.obfuscation.is_some());

// If there is an obfuscated configuration, use that. Otherwise, use the first one.
obfuscated_config.unwrap_or(first_config).clone()
};

self.tried_configurations.insert(config.clone());
Some(config)
self.tried_configurations.insert(selected_config.clone());
Some(selected_config)
}

/// Fetch a config, but error out only when no existing configuration was there.
Expand Down
Loading