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.

76 lines
1.9 KiB
Rust
Raw Normal View History

2024-07-27 00:11:41 +00:00
use clap::Subcommand;
2025-01-22 02:16:51 +00:00
use futures::StreamExt;
use ruma::OwnedServerName;
2025-04-22 01:41:02 +00:00
use tuwunel_core::{Result, utils::time};
2024-07-04 11:49:13 +00:00
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]
async fn destinations_cache(&self, server_name: Option<OwnedServerName>) -> Result {
2025-04-22 01:41:02 +00:00
use tuwunel_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?;
2025-04-22 04:42:26 +00:00
let mut destinations = self
.services
.resolver
.cache
.destinations()
.boxed();
2024-07-04 11:49:13 +00:00
2025-01-22 02:16:51 +00:00
while let Some((name, CachedDest { dest, host, expire })) = destinations.next().await {
2026-01-15 17:39:33 -05:00
if let Some(server_name) = server_name.as_ref()
&& name != server_name
{
continue;
2025-01-04 16:57:07 +00:00
}
2024-07-04 11:49:13 +00:00
2025-01-22 02:16:51 +00:00
let expire = time::format(expire, "%+");
self.write_str(&format!("| {name} | {dest} | {host} | {expire} |\n"))
.await?;
}
2025-01-04 16:57:07 +00:00
Ok(())
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 {
2025-04-22 01:41:02 +00:00
use tuwunel_service::resolver::cache::CachedOverride;
2024-07-04 11:49:13 +00:00
2025-01-22 23:07:13 +00:00
writeln!(self, "| Server Name | IP | Port | Expires | Overriding |").await?;
writeln!(self, "| ----------- | --- | ----:| ------- | ---------- |").await?;
2025-01-04 16:57:07 +00:00
2025-01-22 02:16:51 +00:00
let mut overrides = self.services.resolver.cache.overrides().boxed();
2024-07-04 11:49:13 +00:00
2025-01-22 23:07:13 +00:00
while let Some((name, CachedOverride { ips, port, expire, overriding })) =
overrides.next().await
{
2026-01-15 17:39:33 -05:00
if let Some(server_name) = server_name.as_ref()
&& name != server_name
{
continue;
2025-01-04 16:57:07 +00:00
}
2024-07-04 11:49:13 +00:00
2025-01-22 02:16:51 +00:00
let expire = time::format(expire, "%+");
2025-01-22 23:07:13 +00:00
self.write_str(&format!("| {name} | {ips:?} | {port} | {expire} | {overriding:?} |\n"))
2025-01-22 02:16:51 +00:00
.await?;
}
2025-01-04 16:57:07 +00:00
Ok(())
2024-07-04 11:49:13 +00:00
}