Files
continuwuity/src/admin/query/resolver.rs
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

98 lines
2.5 KiB
Rust
Raw Normal View History

2024-07-27 00:11:41 +00:00
use clap::Subcommand;
use continuwuity::{Err, Result, utils::time};
use resolvematrix::resolution::Resolution;
use ruma::OwnedServerName;
2024-07-04 11:49:13 +00:00
use crate::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)]
#[allow(clippy::enum_variant_names)]
2024-07-27 00:11:41 +00:00
/// Resolver service and caches
2025-05-24 00:28:09 +01:00
pub enum ResolverCommand {
/// Query the destinations or overrides cache, depending on the value of the
/// `overrides` flag (default false)
Cache {
2024-07-27 00:11:41 +00:00
server_name: Option<OwnedServerName>,
#[arg(short, long)]
overrides: Option<bool>,
2024-07-27 00:11:41 +00:00
},
/// Flush a given server from the resolver caches or flush them completely
///
/// * Examples:
/// * Flush a specific server:
///
/// `!admin query resolver flush-cache matrix.example.com`
///
/// * Flush all resolver caches completely:
///
/// `!admin query resolver flush-cache --all`
#[command(verbatim_doc_comment)]
FlushCache {
name: Option<OwnedServerName>,
#[arg(short, long)]
all: bool,
},
2024-07-04 11:49:13 +00:00
}
impl crate::Context<'_> {
async fn cache(
&self,
server_name: Option<OwnedServerName>,
overrides: Option<bool>,
) -> Result {
writeln!(self, "| Server Name | Destination | SNI | Override | Step | Expires |").await?;
writeln!(self, "| ----------- | ----------- | --- | -------- | ---- | ------- |").await?;
2024-07-04 11:49:13 +00:00
let entries = self.services.client.matrix_resolver.get_all_cache_entries();
2025-01-04 16:57:07 +00:00
for (host, entry) in entries {
if let Some(ors) = overrides
&& entry.is_override != ors
{
continue;
2025-01-04 16:57:07 +00:00
}
if let Some(server_name) = server_name.as_ref()
&& host != *server_name
{
continue;
2025-01-04 16:57:07 +00:00
}
let Resolution {
destination,
is_override,
host: sni_host,
resolution_step,
} = entry.resolution;
let expires = time::format(entry.expires_at, "%Y-%m-%dT%H:%M:%S%.3f%z");
self.write_str(&format!(
"| {host} | {destination:?} | {sni_host} | {is_override:?} | {resolution_step} \
| {expires} |\n"
))
.await?;
2025-01-04 16:57:07 +00:00
}
2024-07-04 11:49:13 +00:00
Ok(())
2025-01-22 02:16:51 +00:00
}
2025-01-04 16:57:07 +00:00
async fn flush_cache(&self, name: Option<OwnedServerName>, all: bool) -> Result {
if all {
self.services.client.matrix_resolver.clear_cache();
self.services.client.dns_resolver.clear_cache();
writeln!(self, "Resolver and DNS caches cleared!").await
2026-06-30 21:22:35 +02:00
} else if let Some(name) = name {
self.services
.client
.matrix_resolver
2026-06-30 21:22:35 +02:00
.remove_cache_entry(name.as_str());
self.write_str(&format!("Cleared {name} from resolver caches!"))
.await
} else {
Err!("Missing name. Supply a name or use --all to flush the whole cache.")
}
}
}