Use enums for options to send_request(), add allow_loopback

This commit is contained in:
Lambda 2024-09-21 10:47:55 +00:00 committed by Benjamin Lee
parent 94d523ebcb
commit 6022d56094
No known key found for this signature in database
GPG key ID: FB9624E2885D55A4
2 changed files with 31 additions and 7 deletions

View file

@ -134,11 +134,24 @@ impl FedDest {
} }
} }
#[tracing::instrument(skip(request, log_error), fields(url))] #[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum LogRequestError {
Yes,
No,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum AllowLoopbackRequests {
Yes,
No,
}
#[tracing::instrument(skip(request, log_error, allow_loopback), fields(url))]
pub(crate) async fn send_request<T>( pub(crate) async fn send_request<T>(
destination: &ServerName, destination: &ServerName,
request: T, request: T,
log_error: bool, log_error: LogRequestError,
allow_loopback: AllowLoopbackRequests,
) -> Result<T::IncomingResponse> ) -> Result<T::IncomingResponse>
where where
T: OutgoingRequest + Debug, T: OutgoingRequest + Debug,
@ -147,7 +160,9 @@ where
return Err(Error::BadConfig("Federation is disabled.")); return Err(Error::BadConfig("Federation is disabled."));
} }
if destination == services().globals.server_name() { if destination == services().globals.server_name()
&& allow_loopback == AllowLoopbackRequests::No
{
return Err(Error::bad_config( return Err(Error::bad_config(
"Won't send federation request to ourselves", "Won't send federation request to ourselves",
)); ));
@ -273,7 +288,7 @@ where
services().globals.federation_client().execute(reqwest_request).await; services().globals.federation_client().execute(reqwest_request).await;
let mut response = response.inspect_err(|error| { let mut response = response.inspect_err(|error| {
if log_error { if log_error == LogRequestError::Yes {
warn!(%error, "Could not send request"); warn!(%error, "Could not send request");
} }
})?; })?;

View file

@ -41,7 +41,10 @@ use tracing::{debug, error, warn, Span};
use super::rooms::timeline::PduId; use super::rooms::timeline::PduId;
use crate::{ use crate::{
api::{appservice_server, server_server}, api::{
appservice_server,
server_server::{self, AllowLoopbackRequests, LogRequestError},
},
services, services,
utils::{calculate_hash, debug_slice_truncated}, utils::{calculate_hash, debug_slice_truncated},
Config, Error, PduEvent, Result, Config, Error, PduEvent, Result,
@ -677,7 +680,12 @@ impl Service {
debug!("Got permit"); debug!("Got permit");
let response = tokio::time::timeout( let response = tokio::time::timeout(
Duration::from_secs(2 * 60), Duration::from_secs(2 * 60),
server_server::send_request(destination, request, true), server_server::send_request(
destination,
request,
LogRequestError::Yes,
AllowLoopbackRequests::No,
),
) )
.await .await
.map_err(|_| { .map_err(|_| {
@ -901,7 +909,8 @@ async fn handle_federation_event(
}))) })))
.into(), .into(),
}, },
false, LogRequestError::No,
AllowLoopbackRequests::No,
) )
.await?; .await?;