2024-07-04 11:49:13 +00:00
|
|
|
use std::fmt::Write;
|
|
|
|
|
2024-07-27 00:11:41 +00:00
|
|
|
use clap::Subcommand;
|
2024-12-14 21:58:01 -05:00
|
|
|
use conduwuit::{utils::time, Result};
|
2024-07-04 11:49:13 +00:00
|
|
|
use ruma::{events::room::message::RoomMessageEventContent, OwnedServerName};
|
|
|
|
|
2024-07-27 00:11:41 +00:00
|
|
|
use crate::{admin_command, admin_command_dispatch};
|
2024-07-04 11:49:13 +00:00
|
|
|
|
2024-07-27 00:11:41 +00:00
|
|
|
#[admin_command_dispatch]
|
|
|
|
#[derive(Debug, Subcommand)]
|
|
|
|
/// Resolver service and caches
|
|
|
|
pub(crate) enum ResolverCommand {
|
|
|
|
/// Query the destinations cache
|
|
|
|
DestinationsCache {
|
|
|
|
server_name: Option<OwnedServerName>,
|
|
|
|
},
|
|
|
|
|
|
|
|
/// Query the overrides cache
|
|
|
|
OverridesCache {
|
|
|
|
name: Option<String>,
|
|
|
|
},
|
2024-07-04 11:49:13 +00:00
|
|
|
}
|
|
|
|
|
2024-07-27 00:11:41 +00:00
|
|
|
#[admin_command]
|
2024-12-15 00:05:47 -05:00
|
|
|
async fn destinations_cache(
|
|
|
|
&self,
|
|
|
|
server_name: Option<OwnedServerName>,
|
|
|
|
) -> Result<RoomMessageEventContent> {
|
2024-07-16 23:38:48 +00:00
|
|
|
use service::resolver::cache::CachedDest;
|
2024-07-04 11:49:13 +00:00
|
|
|
|
2025-01-04 16:57:07 +00:00
|
|
|
writeln!(self, "| Server Name | Destination | Hostname | Expires |").await?;
|
|
|
|
writeln!(self, "| ----------- | ----------- | -------- | ------- |").await?;
|
|
|
|
|
2024-07-04 11:49:13 +00:00
|
|
|
let mut out = String::new();
|
2025-01-04 16:57:07 +00:00
|
|
|
{
|
|
|
|
let map = self
|
|
|
|
.services
|
|
|
|
.resolver
|
|
|
|
.cache
|
|
|
|
.destinations
|
|
|
|
.read()
|
|
|
|
.expect("locked");
|
|
|
|
|
|
|
|
for (name, &CachedDest { ref dest, ref host, expire }) in map.iter() {
|
|
|
|
if let Some(server_name) = server_name.as_ref() {
|
|
|
|
if name != server_name {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let expire = time::format(expire, "%+");
|
|
|
|
writeln!(out, "| {name} | {dest} | {host} | {expire} |")?;
|
|
|
|
}
|
2024-07-04 11:49:13 +00:00
|
|
|
}
|
|
|
|
|
2025-01-04 16:57:07 +00:00
|
|
|
self.write_str(out.as_str()).await?;
|
|
|
|
|
|
|
|
Ok(RoomMessageEventContent::notice_plain(""))
|
2024-07-04 11:49:13 +00:00
|
|
|
}
|
|
|
|
|
2024-07-27 00:11:41 +00:00
|
|
|
#[admin_command]
|
|
|
|
async fn overrides_cache(&self, server_name: Option<String>) -> Result<RoomMessageEventContent> {
|
2024-07-16 23:38:48 +00:00
|
|
|
use service::resolver::cache::CachedOverride;
|
2024-07-04 11:49:13 +00:00
|
|
|
|
2025-01-04 16:57:07 +00:00
|
|
|
writeln!(self, "| Server Name | IP | Port | Expires |").await?;
|
|
|
|
writeln!(self, "| ----------- | --- | ----:| ------- |").await?;
|
|
|
|
|
2024-07-04 11:49:13 +00:00
|
|
|
let mut out = String::new();
|
2025-01-04 16:57:07 +00:00
|
|
|
{
|
|
|
|
let map = self
|
|
|
|
.services
|
|
|
|
.resolver
|
|
|
|
.cache
|
|
|
|
.overrides
|
|
|
|
.read()
|
|
|
|
.expect("locked");
|
|
|
|
|
|
|
|
for (name, &CachedOverride { ref ips, port, expire }) in map.iter() {
|
|
|
|
if let Some(server_name) = server_name.as_ref() {
|
|
|
|
if name != server_name {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let expire = time::format(expire, "%+");
|
|
|
|
writeln!(out, "| {name} | {ips:?} | {port} | {expire} |")?;
|
|
|
|
}
|
2024-07-04 11:49:13 +00:00
|
|
|
}
|
|
|
|
|
2025-01-04 16:57:07 +00:00
|
|
|
self.write_str(out.as_str()).await?;
|
|
|
|
|
|
|
|
Ok(RoomMessageEventContent::notice_plain(""))
|
2024-07-04 11:49:13 +00:00
|
|
|
}
|