|
|
|
@ -83,7 +83,7 @@ use rocket::{get, post, put}; |
|
|
|
/// FedDest::Named("198.51.100.5".to_owned(), "".to_owned());
|
|
|
|
/// FedDest::Named("198.51.100.5".to_owned(), "".to_owned());
|
|
|
|
/// ```
|
|
|
|
/// ```
|
|
|
|
#[derive(Clone, Debug, PartialEq)] |
|
|
|
#[derive(Clone, Debug, PartialEq)] |
|
|
|
enum FedDest { |
|
|
|
pub enum FedDest { |
|
|
|
Literal(SocketAddr), |
|
|
|
Literal(SocketAddr), |
|
|
|
Named(String, String), |
|
|
|
Named(String, String), |
|
|
|
} |
|
|
|
} |
|
|
|
@ -109,6 +109,13 @@ impl FedDest { |
|
|
|
Self::Named(host, _) => host.clone(), |
|
|
|
Self::Named(host, _) => host.clone(), |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn port(&self) -> Option<u16> { |
|
|
|
|
|
|
|
match &self { |
|
|
|
|
|
|
|
Self::Literal(addr) => Some(addr.port()), |
|
|
|
|
|
|
|
Self::Named(_, port) => port[1..].parse().ok(), |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
#[tracing::instrument(skip(globals, request))] |
|
|
|
#[tracing::instrument(skip(globals, request))] |
|
|
|
@ -124,41 +131,34 @@ where |
|
|
|
return Err(Error::bad_config("Federation is disabled.")); |
|
|
|
return Err(Error::bad_config("Federation is disabled.")); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
let maybe_result = globals |
|
|
|
let mut write_destination_to_cache = false; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let cached_result = globals |
|
|
|
.actual_destination_cache |
|
|
|
.actual_destination_cache |
|
|
|
.read() |
|
|
|
.read() |
|
|
|
.unwrap() |
|
|
|
.unwrap() |
|
|
|
.get(destination) |
|
|
|
.get(destination) |
|
|
|
.cloned(); |
|
|
|
.cloned(); |
|
|
|
|
|
|
|
|
|
|
|
let (actual_destination, host) = if let Some(result) = maybe_result { |
|
|
|
let (actual_destination, host) = if let Some(result) = cached_result { |
|
|
|
result |
|
|
|
result |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
|
|
|
|
write_destination_to_cache = true; |
|
|
|
|
|
|
|
|
|
|
|
let result = find_actual_destination(globals, &destination).await; |
|
|
|
let result = find_actual_destination(globals, &destination).await; |
|
|
|
let (actual_destination, host) = result.clone(); |
|
|
|
|
|
|
|
let result_string = (result.0.into_https_string(), result.1.into_uri_string()); |
|
|
|
(result.0, result.1.clone().into_uri_string()) |
|
|
|
globals |
|
|
|
|
|
|
|
.actual_destination_cache |
|
|
|
|
|
|
|
.write() |
|
|
|
|
|
|
|
.unwrap() |
|
|
|
|
|
|
|
.insert(Box::<ServerName>::from(destination), result_string.clone()); |
|
|
|
|
|
|
|
let dest_hostname = actual_destination.hostname(); |
|
|
|
|
|
|
|
let host_hostname = host.hostname(); |
|
|
|
|
|
|
|
if dest_hostname != host_hostname { |
|
|
|
|
|
|
|
globals.tls_name_override.write().unwrap().insert( |
|
|
|
|
|
|
|
dest_hostname, |
|
|
|
|
|
|
|
webpki::DNSNameRef::try_from_ascii_str(&host_hostname) |
|
|
|
|
|
|
|
.unwrap() |
|
|
|
|
|
|
|
.to_owned(), |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
result_string |
|
|
|
|
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let actual_destination_str = actual_destination.clone().into_https_string(); |
|
|
|
|
|
|
|
|
|
|
|
let mut http_request = request |
|
|
|
let mut http_request = request |
|
|
|
.try_into_http_request::<Vec<u8>>(&actual_destination, SendAccessToken::IfRequired("")) |
|
|
|
.try_into_http_request::<Vec<u8>>(&actual_destination_str, SendAccessToken::IfRequired("")) |
|
|
|
.map_err(|e| { |
|
|
|
.map_err(|e| { |
|
|
|
warn!("Failed to find destination {}: {}", actual_destination, e); |
|
|
|
warn!( |
|
|
|
|
|
|
|
"Failed to find destination {}: {}", |
|
|
|
|
|
|
|
actual_destination_str, e |
|
|
|
|
|
|
|
); |
|
|
|
Error::BadServerResponse("Invalid destination") |
|
|
|
Error::BadServerResponse("Invalid destination") |
|
|
|
})?; |
|
|
|
})?; |
|
|
|
|
|
|
|
|
|
|
|
@ -232,7 +232,22 @@ where |
|
|
|
.expect("all http requests are valid reqwest requests"); |
|
|
|
.expect("all http requests are valid reqwest requests"); |
|
|
|
|
|
|
|
|
|
|
|
let url = reqwest_request.url().clone(); |
|
|
|
let url = reqwest_request.url().clone(); |
|
|
|
let response = globals.reqwest_client().execute(reqwest_request).await; |
|
|
|
|
|
|
|
|
|
|
|
let mut client = globals.reqwest_client()?; |
|
|
|
|
|
|
|
if let Some(override_name) = globals |
|
|
|
|
|
|
|
.tls_name_override |
|
|
|
|
|
|
|
.read() |
|
|
|
|
|
|
|
.unwrap() |
|
|
|
|
|
|
|
.get(&actual_destination.hostname()) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
client = client.resolve( |
|
|
|
|
|
|
|
&actual_destination.hostname(), |
|
|
|
|
|
|
|
SocketAddr::new(override_name[0], 0), |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
// port will be ignored
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let response = client.build()?.execute(reqwest_request).await; |
|
|
|
|
|
|
|
|
|
|
|
match response { |
|
|
|
match response { |
|
|
|
Ok(mut response) => { |
|
|
|
Ok(mut response) => { |
|
|
|
@ -271,6 +286,13 @@ where |
|
|
|
|
|
|
|
|
|
|
|
if status == 200 { |
|
|
|
if status == 200 { |
|
|
|
let response = T::IncomingResponse::try_from_http_response(http_response); |
|
|
|
let response = T::IncomingResponse::try_from_http_response(http_response); |
|
|
|
|
|
|
|
if response.is_ok() && write_destination_to_cache { |
|
|
|
|
|
|
|
globals.actual_destination_cache.write().unwrap().insert( |
|
|
|
|
|
|
|
Box::<ServerName>::from(destination), |
|
|
|
|
|
|
|
(actual_destination, host), |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
response.map_err(|e| { |
|
|
|
response.map_err(|e| { |
|
|
|
warn!( |
|
|
|
warn!( |
|
|
|
"Invalid 200 response from {} on: {} {}", |
|
|
|
"Invalid 200 response from {} on: {} {}", |
|
|
|
@ -343,16 +365,46 @@ async fn find_actual_destination( |
|
|
|
match get_ip_with_port(&delegated_hostname) { |
|
|
|
match get_ip_with_port(&delegated_hostname) { |
|
|
|
Some(host_and_port) => host_and_port, // 3.1: IP literal in .well-known file
|
|
|
|
Some(host_and_port) => host_and_port, // 3.1: IP literal in .well-known file
|
|
|
|
None => { |
|
|
|
None => { |
|
|
|
if let Some(pos) = destination_str.find(':') { |
|
|
|
if let Some(pos) = delegated_hostname.find(':') { |
|
|
|
// 3.2: Hostname with port in .well-known file
|
|
|
|
// 3.2: Hostname with port in .well-known file
|
|
|
|
let (host, port) = destination_str.split_at(pos); |
|
|
|
let (host, port) = delegated_hostname.split_at(pos); |
|
|
|
FedDest::Named(host.to_string(), port.to_string()) |
|
|
|
FedDest::Named(host.to_string(), port.to_string()) |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
match query_srv_record(globals, &delegated_hostname).await { |
|
|
|
if let Some(hostname_override) = |
|
|
|
|
|
|
|
query_srv_record(globals, &delegated_hostname).await |
|
|
|
|
|
|
|
{ |
|
|
|
// 3.3: SRV lookup successful
|
|
|
|
// 3.3: SRV lookup successful
|
|
|
|
Some(hostname) => hostname, |
|
|
|
let host = match delegated_hostname.find(':') { |
|
|
|
|
|
|
|
None => delegated_hostname.clone(), |
|
|
|
|
|
|
|
Some(pos) => { |
|
|
|
|
|
|
|
delegated_hostname.split_at(pos).0.to_owned() |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if let Ok(override_ip) = globals |
|
|
|
|
|
|
|
.dns_resolver() |
|
|
|
|
|
|
|
.lookup_ip(hostname_override.hostname()) |
|
|
|
|
|
|
|
.await |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
globals |
|
|
|
|
|
|
|
.tls_name_override |
|
|
|
|
|
|
|
.write() |
|
|
|
|
|
|
|
.unwrap() |
|
|
|
|
|
|
|
.insert(host.clone(), override_ip.iter().collect()); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
warn!("Using SRV record, but could not resolve to IP"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let force_port = hostname_override.port(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if let Some(port) = force_port { |
|
|
|
|
|
|
|
FedDest::Named(host, format!(":{}", port.to_string())) |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
add_port_to_hostname(&delegated_hostname) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} else { |
|
|
|
// 3.4: No SRV records, just use the hostname from .well-known
|
|
|
|
// 3.4: No SRV records, just use the hostname from .well-known
|
|
|
|
None => add_port_to_hostname(&delegated_hostname), |
|
|
|
add_port_to_hostname(&delegated_hostname) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
@ -423,6 +475,9 @@ pub async fn request_well_known( |
|
|
|
let body: serde_json::Value = serde_json::from_str( |
|
|
|
let body: serde_json::Value = serde_json::from_str( |
|
|
|
&globals |
|
|
|
&globals |
|
|
|
.reqwest_client() |
|
|
|
.reqwest_client() |
|
|
|
|
|
|
|
.ok()? |
|
|
|
|
|
|
|
.build() |
|
|
|
|
|
|
|
.ok()? |
|
|
|
.get(&format!( |
|
|
|
.get(&format!( |
|
|
|
"https://{}/.well-known/matrix/server", |
|
|
|
"https://{}/.well-known/matrix/server", |
|
|
|
destination |
|
|
|
destination |
|
|
|
@ -1980,15 +2035,9 @@ fn get_auth_chain( |
|
|
|
let mut buckets = vec![BTreeSet::new(); NUM_BUCKETS]; |
|
|
|
let mut buckets = vec![BTreeSet::new(); NUM_BUCKETS]; |
|
|
|
|
|
|
|
|
|
|
|
for id in starting_events { |
|
|
|
for id in starting_events { |
|
|
|
if let Some(pdu) = db.rooms.get_pdu(&id)? { |
|
|
|
let short = db.rooms.get_or_create_shorteventid(&id, &db.globals)?; |
|
|
|
for auth_event in &pdu.auth_events { |
|
|
|
|
|
|
|
let short = db |
|
|
|
|
|
|
|
.rooms |
|
|
|
|
|
|
|
.get_or_create_shorteventid(&auth_event, &db.globals)?; |
|
|
|
|
|
|
|
let bucket_id = (short % NUM_BUCKETS as u64) as usize; |
|
|
|
let bucket_id = (short % NUM_BUCKETS as u64) as usize; |
|
|
|
buckets[bucket_id].insert((short, auth_event.clone())); |
|
|
|
buckets[bucket_id].insert((short, id.clone())); |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
let mut full_auth_chain = HashSet::new(); |
|
|
|
let mut full_auth_chain = HashSet::new(); |
|
|
|
@ -2000,10 +2049,6 @@ fn get_auth_chain( |
|
|
|
continue; |
|
|
|
continue; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// The code below will only get the auth chains, not the events in the chunk. So let's add
|
|
|
|
|
|
|
|
// them first
|
|
|
|
|
|
|
|
full_auth_chain.extend(chunk.iter().map(|(id, _)| id)); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let chunk_key = chunk |
|
|
|
let chunk_key = chunk |
|
|
|
.iter() |
|
|
|
.iter() |
|
|
|
.map(|(short, _)| short) |
|
|
|
.map(|(short, _)| short) |
|
|
|
|