chore: cleanup string-based code in find_actual_destination
parent
0b56589dce
commit
b4c001de2f
|
@ -45,6 +45,35 @@ use std::{
|
||||||
#[cfg(feature = "conduit_bin")]
|
#[cfg(feature = "conduit_bin")]
|
||||||
use rocket::{get, post, put};
|
use rocket::{get, post, put};
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, PartialEq)]
|
||||||
|
enum FederationDestination {
|
||||||
|
Literal(SocketAddr),
|
||||||
|
Named(String, String),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl FederationDestination {
|
||||||
|
fn into_url(self) -> String {
|
||||||
|
match self {
|
||||||
|
Self::Literal(addr) => format!("https://{}", addr),
|
||||||
|
Self::Named(host, port) => format!("https://{}{}", host, port),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn into_uri(self) -> String {
|
||||||
|
match self {
|
||||||
|
Self::Literal(addr) => addr.to_string(),
|
||||||
|
Self::Named(host, ref port) => host + port,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn host(&self) -> String {
|
||||||
|
match &self {
|
||||||
|
Self::Literal(addr) => addr.ip().to_string(),
|
||||||
|
Self::Named(host, _) => host.clone()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[tracing::instrument(skip(globals))]
|
#[tracing::instrument(skip(globals))]
|
||||||
pub async fn send_request<T: OutgoingRequest>(
|
pub async fn send_request<T: OutgoingRequest>(
|
||||||
globals: &crate::database::globals::Globals,
|
globals: &crate::database::globals::Globals,
|
||||||
|
@ -69,17 +98,17 @@ where
|
||||||
result
|
result
|
||||||
} else {
|
} else {
|
||||||
let result = find_actual_destination(globals, &destination).await;
|
let result = find_actual_destination(globals, &destination).await;
|
||||||
|
let (actual_destination, host) = result.clone();
|
||||||
|
let result = (result.0.into_url(), result.1.into_uri());
|
||||||
globals
|
globals
|
||||||
.actual_destination_cache
|
.actual_destination_cache
|
||||||
.write()
|
.write()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.insert(Box::<ServerName>::from(destination), result.clone());
|
.insert(Box::<ServerName>::from(destination), result.clone());
|
||||||
let actual_destination = result.0.strip_prefix("https://").unwrap().splitn(2, ':').next().unwrap();
|
|
||||||
let host = result.1.splitn(2, ':').next().unwrap_or(&result.1);
|
|
||||||
if actual_destination != host {
|
if actual_destination != host {
|
||||||
globals.tls_name_override.write().unwrap().insert(
|
globals.tls_name_override.write().unwrap().insert(
|
||||||
actual_destination.to_owned(),
|
actual_destination.host(),
|
||||||
webpki::DNSNameRef::try_from_ascii_str(&host)
|
webpki::DNSNameRef::try_from_ascii_str(&host.host())
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.to_owned(),
|
.to_owned(),
|
||||||
);
|
);
|
||||||
|
@ -212,22 +241,23 @@ where
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tracing::instrument]
|
#[tracing::instrument]
|
||||||
fn get_ip_with_port(destination_str: String) -> Option<String> {
|
fn get_ip_with_port(destination_str: &str) -> Option<FederationDestination> {
|
||||||
if destination_str.parse::<SocketAddr>().is_ok() {
|
if let Ok(destination) = destination_str.parse::<SocketAddr>() {
|
||||||
Some(destination_str)
|
Some(FederationDestination::Literal(destination))
|
||||||
} else if let Ok(ip_addr) = destination_str.parse::<IpAddr>() {
|
} else if let Ok(ip_addr) = destination_str.parse::<IpAddr>() {
|
||||||
Some(SocketAddr::new(ip_addr, 8448).to_string())
|
Some(FederationDestination::Literal(SocketAddr::new(ip_addr, 8448)))
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tracing::instrument]
|
#[tracing::instrument]
|
||||||
fn add_port_to_hostname(destination_str: String) -> String {
|
fn add_port_to_hostname(destination_str: &str) -> FederationDestination {
|
||||||
match destination_str.find(':') {
|
let (host, port) = match destination_str.find(':') {
|
||||||
None => destination_str.to_owned() + ":8448",
|
None => (destination_str, ":8448"),
|
||||||
Some(_) => destination_str.to_string(),
|
Some(pos) => destination_str.split_at(pos),
|
||||||
}
|
};
|
||||||
|
FederationDestination::Named(host.to_string(), port.to_string())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns: actual_destination, host header
|
/// Returns: actual_destination, host header
|
||||||
|
@ -237,36 +267,37 @@ fn add_port_to_hostname(destination_str: String) -> String {
|
||||||
async fn find_actual_destination(
|
async fn find_actual_destination(
|
||||||
globals: &crate::database::globals::Globals,
|
globals: &crate::database::globals::Globals,
|
||||||
destination: &'_ ServerName,
|
destination: &'_ ServerName,
|
||||||
) -> (String, String) {
|
) -> (FederationDestination, FederationDestination) {
|
||||||
let destination_str = destination.as_str().to_owned();
|
let destination_str = destination.as_str().to_owned();
|
||||||
let mut host = destination_str.clone();
|
let mut hostname = destination_str.clone();
|
||||||
let actual_destination = "https://".to_owned()
|
let actual_destination = match get_ip_with_port(&destination_str) {
|
||||||
+ &match get_ip_with_port(destination_str.clone()) {
|
|
||||||
Some(host_port) => {
|
Some(host_port) => {
|
||||||
// 1: IP literal with provided or default port
|
// 1: IP literal with provided or default port
|
||||||
host_port
|
host_port
|
||||||
}
|
}
|
||||||
None => {
|
None => {
|
||||||
if destination_str.find(':').is_some() {
|
if let Some(pos) = destination_str.find(':') {
|
||||||
// 2: Hostname with included port
|
// 2: Hostname with included port
|
||||||
destination_str
|
let (host, port) = destination_str.split_at(pos);
|
||||||
|
FederationDestination::Named(host.to_string(), port.to_string())
|
||||||
} else {
|
} else {
|
||||||
match request_well_known(globals, &destination.as_str()).await {
|
match request_well_known(globals, &destination.as_str()).await {
|
||||||
// 3: A .well-known file is available
|
// 3: A .well-known file is available
|
||||||
Some(delegated_hostname) => {
|
Some(delegated_hostname) => {
|
||||||
host = delegated_hostname.clone();
|
hostname = delegated_hostname.clone();
|
||||||
match get_ip_with_port(delegated_hostname.clone()) {
|
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 destination_str.find(':').is_some() {
|
if let Some(pos) = destination_str.find(':') {
|
||||||
// 3.2: Hostname with port in .well-known file
|
// 3.2: Hostname with port in .well-known file
|
||||||
destination_str
|
let (host, port) = destination_str.split_at(pos);
|
||||||
|
FederationDestination::Named(host.to_string(), port.to_string())
|
||||||
} else {
|
} else {
|
||||||
match query_srv_record(globals, &delegated_hostname).await {
|
match query_srv_record(globals, &delegated_hostname).await {
|
||||||
// 3.3: SRV lookup successful
|
// 3.3: SRV lookup successful
|
||||||
Some(hostname) => hostname,
|
Some(hostname) => hostname,
|
||||||
// 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),
|
None => add_port_to_hostname(&delegated_hostname),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -278,7 +309,7 @@ async fn find_actual_destination(
|
||||||
// 4: SRV record found
|
// 4: SRV record found
|
||||||
Some(hostname) => hostname,
|
Some(hostname) => hostname,
|
||||||
// 5: No SRV record found
|
// 5: No SRV record found
|
||||||
None => add_port_to_hostname(destination_str.to_string()),
|
None => add_port_to_hostname(&destination_str),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -286,24 +317,32 @@ async fn find_actual_destination(
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
(actual_destination, host)
|
let hostname = get_ip_with_port(&hostname).unwrap_or_else(|| {
|
||||||
|
match hostname.find(':') {
|
||||||
|
Some(pos) => {
|
||||||
|
let (host, port) = hostname.split_at(pos);
|
||||||
|
FederationDestination::Named(host.to_string(), port.to_string())
|
||||||
|
}
|
||||||
|
None => FederationDestination::Named(hostname, "".to_string())
|
||||||
|
}
|
||||||
|
});
|
||||||
|
(actual_destination, hostname)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tracing::instrument(skip(globals))]
|
#[tracing::instrument(skip(globals))]
|
||||||
async fn query_srv_record(
|
async fn query_srv_record(
|
||||||
globals: &crate::database::globals::Globals,
|
globals: &crate::database::globals::Globals,
|
||||||
hostname: &'_ str,
|
hostname: &'_ str,
|
||||||
) -> Option<String> {
|
) -> Option<FederationDestination> {
|
||||||
if let Ok(Some(host_port)) = globals
|
if let Ok(Some(host_port)) = globals
|
||||||
.dns_resolver()
|
.dns_resolver()
|
||||||
.srv_lookup(format!("_matrix._tcp.{}", hostname))
|
.srv_lookup(format!("_matrix._tcp.{}", hostname))
|
||||||
.await
|
.await
|
||||||
.map(|srv| {
|
.map(|srv| {
|
||||||
srv.iter().next().map(|result| {
|
srv.iter().next().map(|result| {
|
||||||
format!(
|
FederationDestination::Named(
|
||||||
"{}:{}",
|
result.target().to_string().trim_end_matches('.').to_string(),
|
||||||
result.target().to_string().trim_end_matches('.'),
|
format!(":{}", result.port())
|
||||||
result.port().to_string()
|
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in New Issue