support querying _matrix-fed and _matrix

_matrix-fed is the new IANA registered SRV record service name
per MSC4040. _matrix is now considered deprecated in Matrix 1.8.
see 3.3 and 3.4 of https://spec.matrix.org/v1.8/server-server-api/#resolving-server-names

Signed-off-by: girlbossceo <june@girlboss.ceo>
This commit is contained in:
girlbossceo 2023-09-02 17:53:46 -04:00
parent 6695b8d8b6
commit 618036dc31

View file

@ -6,6 +6,7 @@ use crate::{
services, utils, Error, PduEvent, Result, Ruma, services, utils, Error, PduEvent, Result, Ruma,
}; };
use axum::{response::IntoResponse, Json}; use axum::{response::IntoResponse, Json};
use futures_util::future::TryFutureExt;
use get_profile_information::v1::ProfileField; use get_profile_information::v1::ProfileField;
use http::header::{HeaderValue, AUTHORIZATION}; use http::header::{HeaderValue, AUTHORIZATION};
@ -54,8 +55,9 @@ use std::{
sync::{Arc, RwLock}, sync::{Arc, RwLock},
time::{Duration, Instant, SystemTime}, time::{Duration, Instant, SystemTime},
}; };
use trust_dns_resolver::{error::ResolveError, lookup::SrvLookup};
use tracing::{debug, error, warn}; use tracing::{debug, error, info, warn};
/// Wraps either an literal IP address plus port, or a hostname plus complement /// Wraps either an literal IP address plus port, or a hostname plus complement
/// (colon-plus-port if it was specified). /// (colon-plus-port if it was specified).
@ -476,25 +478,40 @@ async fn find_actual_destination(destination: &'_ ServerName) -> (FedDest, FedDe
} }
async fn query_srv_record(hostname: &'_ str) -> Option<FedDest> { async fn query_srv_record(hostname: &'_ str) -> Option<FedDest> {
let hostname = hostname.trim_end_matches('.'); fn handle_successful_srv(srv: SrvLookup) -> Option<FedDest> {
if let Ok(Some(host_port)) = services() srv.iter().next().map(|result| {
.globals FedDest::Named(
.dns_resolver() result.target().to_string().trim_end_matches('.').to_owned(),
.srv_lookup(format!("_matrix._tcp.{hostname}.")) format!(":{}", result.port()),
.await )
.map(|srv| {
srv.iter().next().map(|result| {
FedDest::Named(
result.target().to_string().trim_end_matches('.').to_owned(),
format!(":{}", result.port()),
)
})
}) })
{
Some(host_port)
} else {
None
} }
async fn lookup_srv(hostname: &str) -> Result<SrvLookup, ResolveError> {
debug!("querying SRV for {:?}", hostname);
let hostname = hostname.trim_end_matches('.');
services()
.globals
.dns_resolver()
.srv_lookup(format!("{}", hostname))
.await
}
let first_hostname = format!("_matrix-fed._tcp.{hostname}.");
let second_hostname = format!("_matrix._tcp.{hostname}.");
lookup_srv(&first_hostname)
.or_else(|_| {
info!(
"Querying deprecated _matrix SRV record for host {:?}",
hostname
);
lookup_srv(&second_hostname)
})
.and_then(|srv_lookup| async { Ok(handle_successful_srv(srv_lookup)) })
.await
.ok()
.flatten()
} }
async fn request_well_known(destination: &str) -> Option<String> { async fn request_well_known(destination: &str) -> Option<String> {