-
-
Notifications
You must be signed in to change notification settings - Fork 109
fix missing MX resolver eg. on android #2852
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,8 +3,11 @@ | |
mod data; | ||
|
||
use crate::config::Config; | ||
use crate::context::Context; | ||
use crate::provider::data::{PROVIDER_DATA, PROVIDER_IDS, PROVIDER_UPDATED}; | ||
use async_std_resolver::resolver_from_system_conf; | ||
use async_std_resolver::{ | ||
config, resolver, resolver_from_system_conf, AsyncStdResolver, ResolveError, | ||
}; | ||
use chrono::{NaiveDateTime, NaiveTime}; | ||
|
||
#[derive(Debug, Display, Copy, Clone, PartialEq, FromPrimitive, ToPrimitive)] | ||
|
@@ -81,6 +84,22 @@ pub struct Provider { | |
pub oauth2_authorizer: Option<Oauth2Authorizer>, | ||
} | ||
|
||
/// Get resolver to query MX records. | ||
/// | ||
/// We first try resolver_from_system_conf() which reads the system's resolver from `/etc/resolv.conf`. | ||
/// This does not work at least on some Androids, therefore we use use ResolverConfig::default() | ||
/// which default eg. to google's 8.8.8.8 or 8.8.4.4 as a fallback. | ||
async fn get_resolver() -> Result<AsyncStdResolver, ResolveError> { | ||
if let Ok(resolver) = resolver_from_system_conf().await { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Log the error here? |
||
return Ok(resolver); | ||
} | ||
resolver( | ||
config::ResolverConfig::default(), | ||
config::ResolverOpts::default(), | ||
) | ||
.await | ||
} | ||
|
||
/// Returns provider for the given domain. | ||
/// | ||
/// This function looks up domain in offline database first. If not | ||
|
@@ -89,15 +108,19 @@ pub struct Provider { | |
/// | ||
/// For compatibility, email address can be passed to this function | ||
/// instead of the domain. | ||
pub async fn get_provider_info(domain: &str, skip_mx: bool) -> Option<&'static Provider> { | ||
pub async fn get_provider_info( | ||
context: &Context, | ||
domain: &str, | ||
skip_mx: bool, | ||
) -> Option<&'static Provider> { | ||
let domain = domain.rsplitn(2, '@').next()?; | ||
|
||
if let Some(provider) = get_provider_by_domain(domain) { | ||
return Some(provider); | ||
} | ||
|
||
if !skip_mx { | ||
if let Some(provider) = get_provider_by_mx(domain).await { | ||
if let Some(provider) = get_provider_by_mx(context, domain).await { | ||
return Some(provider); | ||
} | ||
} | ||
|
@@ -117,8 +140,8 @@ pub fn get_provider_by_domain(domain: &str) -> Option<&'static Provider> { | |
/// Finds a provider based on MX record for the given domain. | ||
/// | ||
/// For security reasons, only Gmail can be configured this way. | ||
pub async fn get_provider_by_mx(domain: &str) -> Option<&'static Provider> { | ||
if let Ok(resolver) = resolver_from_system_conf().await { | ||
pub async fn get_provider_by_mx(context: &Context, domain: &str) -> Option<&'static Provider> { | ||
if let Ok(resolver) = get_resolver().await { | ||
let mut fqdn: String = domain.to_string(); | ||
if !fqdn.ends_with('.') { | ||
fqdn.push('.'); | ||
|
@@ -143,6 +166,8 @@ pub async fn get_provider_by_mx(domain: &str) -> Option<&'static Provider> { | |
} | ||
} | ||
} | ||
} else { | ||
warn!(context, "cannot get a resolver to check MX records."); | ||
} | ||
|
||
None | ||
|
@@ -169,6 +194,8 @@ mod tests { | |
|
||
use super::*; | ||
use crate::dc_tools::time; | ||
use crate::test_utils::TestContext; | ||
use anyhow::Result; | ||
use chrono::NaiveDate; | ||
|
||
#[test] | ||
|
@@ -218,12 +245,13 @@ mod tests { | |
|
||
#[async_std::test] | ||
async fn test_get_provider_info() { | ||
assert!(get_provider_info("", false).await.is_none()); | ||
assert!(get_provider_info("google.com", false).await.unwrap().id == "gmail"); | ||
let t = TestContext::new().await; | ||
assert!(get_provider_info(&t, "", false).await.is_none()); | ||
assert!(get_provider_info(&t, "google.com", false).await.unwrap().id == "gmail"); | ||
|
||
// get_provider_info() accepts email addresses for backwards compatibility | ||
assert!( | ||
get_provider_info("example@google.com", false) | ||
get_provider_info(&t, "example@google.com", false) | ||
.await | ||
.unwrap() | ||
.id | ||
|
@@ -242,4 +270,10 @@ mod tests { | |
assert!(get_provider_update_timestamp() <= time()); | ||
assert!(get_provider_update_timestamp() > timestamp_past); | ||
} | ||
|
||
#[async_std::test] | ||
async fn test_get_resolver() -> Result<()> { | ||
assert!(get_resolver().await.is_ok()); | ||
Ok(()) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's ok to simply import
anyhow::Result
at the top of the module rather than the test and return it here, soanyhow::Result
is used as much as possible, preferrably across the whole crate.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i also first tried that, getting "expected struct
anyhow::Error
, found structResolveError
" - so i assumed i have to convert the object somehow and did not want to dive into that yesterday :)Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Something like
?
will convert theResult
automatically.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that's clever! i did not thought too much about these conversions in the past :)
i did a successor pr that uses map_err() at #2853 - but you would prefer the one above?