|
|
|
@ -30,7 +30,6 @@ use std::{ |
|
|
|
sync::Arc, |
|
|
|
sync::Arc, |
|
|
|
time::{Duration, SystemTime}, |
|
|
|
time::{Duration, SystemTime}, |
|
|
|
}; |
|
|
|
}; |
|
|
|
use trust_dns_resolver::AsyncResolver; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub async fn request_well_known( |
|
|
|
pub async fn request_well_known( |
|
|
|
globals: &crate::database::globals::Globals, |
|
|
|
globals: &crate::database::globals::Globals, |
|
|
|
@ -66,35 +65,25 @@ where |
|
|
|
return Err(Error::bad_config("Federation is disabled.")); |
|
|
|
return Err(Error::bad_config("Federation is disabled.")); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
let resolver = AsyncResolver::tokio_from_system_conf().await.map_err(|_| { |
|
|
|
let maybe_result = globals |
|
|
|
Error::bad_config("Failed to set up trust dns resolver with system config.") |
|
|
|
.actual_destination_cache |
|
|
|
})?; |
|
|
|
.read() |
|
|
|
|
|
|
|
.unwrap() |
|
|
|
let mut host = None; |
|
|
|
.get(&destination) |
|
|
|
|
|
|
|
.cloned(); |
|
|
|
|
|
|
|
|
|
|
|
let actual_destination = "https://".to_owned() |
|
|
|
let (actual_destination, host) = if let Some(result) = maybe_result { |
|
|
|
+ &if let Some(mut delegated_hostname) = |
|
|
|
println!("Loaded {} -> {:?}", destination, result); |
|
|
|
request_well_known(globals, &destination.as_str()).await |
|
|
|
result |
|
|
|
{ |
|
|
|
|
|
|
|
if let Ok(Some(srv)) = resolver |
|
|
|
|
|
|
|
.srv_lookup(format!("_matrix._tcp.{}", delegated_hostname)) |
|
|
|
|
|
|
|
.await |
|
|
|
|
|
|
|
.map(|srv| srv.iter().next().map(|result| result.target().to_string())) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
host = Some(delegated_hostname); |
|
|
|
|
|
|
|
srv.trim_end_matches('.').to_owned() |
|
|
|
|
|
|
|
} else { |
|
|
|
} else { |
|
|
|
if delegated_hostname.find(':').is_none() { |
|
|
|
let result = find_actual_destination(globals, &destination).await; |
|
|
|
delegated_hostname += ":8448"; |
|
|
|
globals |
|
|
|
} |
|
|
|
.actual_destination_cache |
|
|
|
delegated_hostname |
|
|
|
.write() |
|
|
|
} |
|
|
|
.unwrap() |
|
|
|
} else { |
|
|
|
.insert(destination.clone(), result.clone()); |
|
|
|
let mut destination = destination.as_str().to_owned(); |
|
|
|
println!("Saving {} -> {:?}", destination, result); |
|
|
|
if destination.find(':').is_none() { |
|
|
|
result |
|
|
|
destination += ":8448"; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
destination |
|
|
|
|
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
let mut http_request = request |
|
|
|
let mut http_request = request |
|
|
|
@ -232,6 +221,42 @@ where |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Returns: actual_destination, host header
|
|
|
|
|
|
|
|
async fn find_actual_destination( |
|
|
|
|
|
|
|
globals: &crate::database::globals::Globals, |
|
|
|
|
|
|
|
destination: &Box<ServerName>, |
|
|
|
|
|
|
|
) -> (String, Option<String>) { |
|
|
|
|
|
|
|
let mut host = None; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let actual_destination = "https://".to_owned() |
|
|
|
|
|
|
|
+ &if let Some(mut delegated_hostname) = |
|
|
|
|
|
|
|
request_well_known(globals, destination.as_str()).await |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
if let Ok(Some(srv)) = globals |
|
|
|
|
|
|
|
.dns_resolver() |
|
|
|
|
|
|
|
.srv_lookup(format!("_matrix._tcp.{}", delegated_hostname)) |
|
|
|
|
|
|
|
.await |
|
|
|
|
|
|
|
.map(|srv| srv.iter().next().map(|result| result.target().to_string())) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
host = Some(delegated_hostname); |
|
|
|
|
|
|
|
srv.trim_end_matches('.').to_owned() |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
if delegated_hostname.find(':').is_none() { |
|
|
|
|
|
|
|
delegated_hostname += ":8448"; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
delegated_hostname |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
let mut destination = destination.as_str().to_owned(); |
|
|
|
|
|
|
|
if destination.find(':').is_none() { |
|
|
|
|
|
|
|
destination += ":8448"; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
destination |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(actual_destination, host) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
#[cfg_attr(feature = "conduit_bin", get("/_matrix/federation/v1/version"))] |
|
|
|
#[cfg_attr(feature = "conduit_bin", get("/_matrix/federation/v1/version"))] |
|
|
|
pub fn get_server_version_route( |
|
|
|
pub fn get_server_version_route( |
|
|
|
db: State<'_, Database>, |
|
|
|
db: State<'_, Database>, |
|
|
|
|