Add basic administration via telegram

This commit is contained in:
asonix
2022-11-02 17:58:52 -05:00
parent 279ac9400d
commit bd172753fb
6 changed files with 440 additions and 5 deletions
+17
View File
@@ -30,6 +30,8 @@ pub(crate) struct ParsedConfig {
sled_path: PathBuf,
source_repo: IriString,
opentelemetry_url: Option<IriString>,
telegram_token: Option<String>,
telegram_admin_handle: Option<String>,
}
#[derive(Clone)]
@@ -45,6 +47,8 @@ pub struct Config {
sled_path: PathBuf,
source_repo: IriString,
opentelemetry_url: Option<IriString>,
telegram_token: Option<String>,
telegram_admin_handle: Option<String>,
}
#[derive(Debug)]
@@ -78,6 +82,8 @@ impl std::fmt::Debug for Config {
"opentelemetry_url",
&self.opentelemetry_url.as_ref().map(|url| url.to_string()),
)
.field("telegram_token", &"[redacted]")
.field("telegram_admin_handle", &self.telegram_admin_handle)
.finish()
}
}
@@ -96,6 +102,8 @@ impl Config {
.set_default("sled_path", "./sled/db-0-34")?
.set_default("source_repo", "https://git.asonix.dog/asonix/relay")?
.set_default("opentelemetry_url", None as Option<&str>)?
.set_default("telegram_token", None as Option<&str>)?
.set_default("telegram_admin_handle", None as Option<&str>)?
.add_source(Environment::default())
.build()?;
@@ -116,6 +124,8 @@ impl Config {
sled_path: config.sled_path,
source_repo: config.source_repo,
opentelemetry_url: config.opentelemetry_url,
telegram_token: config.telegram_token,
telegram_admin_handle: config.telegram_admin_handle,
})
}
@@ -225,6 +235,13 @@ impl Config {
self.opentelemetry_url.as_ref()
}
pub(crate) fn telegram_info(&self) -> Option<(&str, &str)> {
self.telegram_token.as_deref().and_then(|token| {
let handle = self.telegram_admin_handle.as_deref()?;
Some((token, handle))
})
}
pub(crate) fn generate_url(&self, kind: UrlKind) -> IriString {
self.do_generate_url(kind).expect("Generated valid IRI")
}
+5
View File
@@ -22,6 +22,7 @@ mod jobs;
mod middleware;
mod requests;
mod routes;
mod telegram;
use self::{
args::Args,
@@ -118,6 +119,10 @@ async fn main() -> Result<(), anyhow::Error> {
let (manager, job_server) =
create_workers(state.clone(), actors.clone(), media.clone(), config.clone());
if let Some((token, admin_handle)) = config.telegram_info() {
telegram::start(admin_handle.to_owned(), db.clone(), token);
}
let bind_address = config.bind_address();
HttpServer::new(move || {
App::new()
+90
View File
@@ -0,0 +1,90 @@
use crate::db::Db;
use activitystreams::iri_string::types::IriString;
use std::sync::Arc;
use teloxide::{prelude::*, utils::command::BotCommands};
#[derive(BotCommands, Clone)]
#[command(
rename_rule = "lowercase",
description = "These commands are for administering AodeRelay"
)]
enum Command {
#[command(description = "Display this text.")]
Help,
#[command(description = "Block a domain from the relay.")]
Block { domain: IriString },
#[command(description = "Unblock a domain from the relay.")]
Unblock { domain: IriString },
#[command(description = "Allow a domain to connect to the relay (for RESTRICTED_MODE)")]
Allow { domain: IriString },
#[command(description = "Disallow a domain to connect to the relay (for RESTRICTED_MODE)")]
Disallow { domain: IriString },
}
pub(crate) fn start(admin_handle: String, db: Db, token: &str) {
let bot = Bot::new(token);
let admin_handle = Arc::new(admin_handle);
actix_rt::spawn(async move {
teloxide::repl(bot, move |bot: Bot, msg: Message, cmd: Command| {
let admin_handle = admin_handle.clone();
let db = db.clone();
async move {
if !is_admin(&admin_handle, &msg) {
return Ok(());
}
answer(bot, msg, cmd, db).await
}
})
.await;
});
}
fn is_admin(admin_handle: &str, message: &Message) -> bool {
message
.from()
.and_then(|user| user.username.as_deref())
.map(|username| username == admin_handle)
.unwrap_or(false)
}
async fn answer(bot: Bot, msg: Message, cmd: Command, db: Db) -> ResponseResult<()> {
match cmd {
Command::Help => {
bot.send_message(msg.chat.id, Command::descriptions().to_string())
.await?;
}
Command::Block { domain } => {
if db.add_blocks(vec![domain.to_string()]).await.is_ok() {
bot.send_message(msg.chat.id, format!("{} has been blocked", domain))
.await?;
}
}
Command::Unblock { domain } => {
if db.remove_blocks(vec![domain.to_string()]).await.is_ok() {
bot.send_message(msg.chat.id, format!("{} has been unblocked", domain))
.await?;
}
}
Command::Allow { domain } => {
if db.add_allows(vec![domain.to_string()]).await.is_ok() {
bot.send_message(msg.chat.id, format!("{} has been allowed", domain))
.await?;
}
}
Command::Disallow { domain } => {
if db.remove_allows(vec![domain.to_string()]).await.is_ok() {
bot.send_message(msg.chat.id, format!("{} has been disallwoed", domain))
.await?;
}
}
}
Ok(())
}