admin cmd to send a request/ping to /_matrix/federation/v1/version

Signed-off-by: strawberry <strawberry@puppygock.gay>
This commit is contained in:
strawberry 2024-03-31 18:57:11 -04:00 committed by June
parent 738878f6ff
commit 51afde9e98
2 changed files with 57 additions and 5 deletions

View file

@ -61,6 +61,13 @@ pub(crate) enum DebugCommand {
room_id: Box<RoomId>, room_id: Box<RoomId>,
}, },
/// - Sends a federation request to the remote server's
/// `/_matrix/federation/v1/version` endpoint and measures the latency it
/// took for the server to respond
Ping {
server: Box<ServerName>,
},
/// - Forces device lists for all local and remote users to be updated (as /// - Forces device lists for all local and remote users to be updated (as
/// having new keys available) /// having new keys available)
ForceDeviceListUpdates, ForceDeviceListUpdates,
@ -253,10 +260,10 @@ pub(crate) async fn process(command: DebugCommand, body: Vec<&str>) -> Result<Ro
), ),
)); ));
}, },
Err(_) => { Err(e) => {
return Ok(RoomMessageEventContent::text_plain( return Ok(RoomMessageEventContent::text_plain(format!(
"Remote server did not have PDU or failed sending request to remote server.", "Remote server did not have PDU or failed sending request to remote server: {e}"
)); )));
}, },
} }
}, },
@ -295,6 +302,51 @@ pub(crate) async fn process(command: DebugCommand, body: Vec<&str>) -> Result<Ro
), ),
)); ));
}, },
DebugCommand::Ping {
server,
} => {
if server == services().globals.server_name() {
return Ok(RoomMessageEventContent::text_plain(
"Not allowed to send federation requests to ourselves.",
));
}
let timer = tokio::time::Instant::now();
match services()
.sending
.send_federation_request(&server, ruma::api::federation::discovery::get_server_version::v1::Request {})
.await
{
Ok(response) => {
let ping_time = timer.elapsed();
let json_text_res = serde_json::to_string_pretty(&response.server);
if let Ok(json) = json_text_res {
return Ok(RoomMessageEventContent::text_html(
format!("Got response which took {ping_time:?} time:\n```json\n{json}\n```"),
format!(
"<p>Got response which took {ping_time:?} time:</p>\n<pre><code \
class=\"language-json\">{}\n</code></pre>\n",
HtmlEscape(&json)
),
));
}
return Ok(RoomMessageEventContent::text_plain(format!(
"Got non-JSON response which took {ping_time:?} time:\n{0:?}",
response
)));
},
Err(e) => {
error!("Failed sending federation request to specified server from ping debug command: {e}");
return Ok(RoomMessageEventContent::text_plain(format!(
"Failed sending federation request to specified server:\n\n{e}",
)));
},
}
},
DebugCommand::ForceDeviceListUpdates => { DebugCommand::ForceDeviceListUpdates => {
// Force E2EE device list updates for all users // Force E2EE device list updates for all users
for user_id in services().users.iter().filter_map(std::result::Result::ok) { for user_id in services().users.iter().filter_map(std::result::Result::ok) {

View file

@ -1095,7 +1095,7 @@ impl Service {
events_all.insert(next_id); events_all.insert(next_id);
}, },
Err(e) => { Err(e) => {
warn!("Failed to fetch event {} | {e}", next_id); warn!("Failed to fetch event {next_id}: {e}");
back_off((*next_id).to_owned()).await; back_off((*next_id).to_owned()).await;
}, },
} }