2022-11-02 22:58:52 +00:00
|
|
|
use crate::db::Db;
|
|
|
|
use std::sync::Arc;
|
2022-11-02 23:57:08 +00:00
|
|
|
use teloxide::{
|
|
|
|
dispatching::{Dispatcher, UpdateFilterExt},
|
|
|
|
requests::{Requester, ResponseResult},
|
|
|
|
types::{Message, Update},
|
|
|
|
utils::command::BotCommands,
|
|
|
|
Bot,
|
|
|
|
};
|
2022-11-02 22:58:52 +00:00
|
|
|
|
2022-11-02 23:57:08 +00:00
|
|
|
#[derive(BotCommands, Clone, Debug)]
|
2022-11-02 22:58:52 +00:00
|
|
|
#[command(
|
|
|
|
rename_rule = "lowercase",
|
|
|
|
description = "These commands are for administering AodeRelay"
|
|
|
|
)]
|
|
|
|
enum Command {
|
2022-11-02 23:57:08 +00:00
|
|
|
#[command(description = "Display this text.")]
|
|
|
|
Start,
|
|
|
|
|
2022-11-02 22:58:52 +00:00
|
|
|
#[command(description = "Display this text.")]
|
|
|
|
Help,
|
|
|
|
|
|
|
|
#[command(description = "Block a domain from the relay.")]
|
2022-11-02 23:04:57 +00:00
|
|
|
Block { domain: String },
|
2022-11-02 22:58:52 +00:00
|
|
|
|
|
|
|
#[command(description = "Unblock a domain from the relay.")]
|
2022-11-02 23:04:57 +00:00
|
|
|
Unblock { domain: String },
|
2022-11-02 22:58:52 +00:00
|
|
|
|
|
|
|
#[command(description = "Allow a domain to connect to the relay (for RESTRICTED_MODE)")]
|
2022-11-02 23:04:57 +00:00
|
|
|
Allow { domain: String },
|
2022-11-02 22:58:52 +00:00
|
|
|
|
|
|
|
#[command(description = "Disallow a domain to connect to the relay (for RESTRICTED_MODE)")]
|
2022-11-02 23:04:57 +00:00
|
|
|
Disallow { domain: String },
|
2022-11-08 00:49:19 +00:00
|
|
|
|
|
|
|
#[command(description = "List blocked domains")]
|
|
|
|
ListBlocks,
|
|
|
|
|
|
|
|
#[command(description = "List allowed domains")]
|
|
|
|
ListAllowed,
|
|
|
|
|
|
|
|
#[command(description = "List connected domains")]
|
|
|
|
ListConnected,
|
2022-11-02 22:58:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2022-11-02 23:57:08 +00:00
|
|
|
let command_handler = teloxide::filter_command::<Command, _>().endpoint(
|
|
|
|
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) {
|
|
|
|
bot.send_message(msg.chat.id, "You are not authorized")
|
|
|
|
.await?;
|
|
|
|
return Ok(());
|
|
|
|
}
|
2022-11-02 22:58:52 +00:00
|
|
|
|
2022-11-02 23:57:08 +00:00
|
|
|
answer(bot, msg, cmd, db).await
|
2022-11-02 22:58:52 +00:00
|
|
|
}
|
2022-11-02 23:57:08 +00:00
|
|
|
},
|
|
|
|
);
|
2022-11-02 22:58:52 +00:00
|
|
|
|
2022-11-02 23:57:08 +00:00
|
|
|
let message_handler = Update::filter_message().branch(command_handler);
|
|
|
|
|
|
|
|
Dispatcher::builder(bot, message_handler)
|
|
|
|
.build()
|
|
|
|
.dispatch()
|
|
|
|
.await;
|
2022-11-02 22:58:52 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2022-11-02 23:57:08 +00:00
|
|
|
#[tracing::instrument(skip(bot, msg, db))]
|
2022-11-02 22:58:52 +00:00
|
|
|
async fn answer(bot: Bot, msg: Message, cmd: Command, db: Db) -> ResponseResult<()> {
|
|
|
|
match cmd {
|
2022-11-02 23:57:08 +00:00
|
|
|
Command::Help | Command::Start => {
|
2022-11-02 22:58:52 +00:00
|
|
|
bot.send_message(msg.chat.id, Command::descriptions().to_string())
|
|
|
|
.await?;
|
|
|
|
}
|
2022-11-08 00:49:19 +00:00
|
|
|
Command::Block { domain } if db.add_blocks(vec![domain.clone()]).await.is_ok() => {
|
|
|
|
bot.send_message(msg.chat.id, format!("{} has been blocked", domain))
|
|
|
|
.await?;
|
|
|
|
}
|
|
|
|
Command::Unblock { domain } if db.remove_blocks(vec![domain.clone()]).await.is_ok() => {
|
|
|
|
bot.send_message(msg.chat.id, format!("{} has been unblocked", domain))
|
|
|
|
.await?;
|
2022-11-02 22:58:52 +00:00
|
|
|
}
|
2022-11-08 00:49:19 +00:00
|
|
|
Command::Allow { domain } if db.add_allows(vec![domain.clone()]).await.is_ok() => {
|
|
|
|
bot.send_message(msg.chat.id, format!("{} has been allowed", domain))
|
|
|
|
.await?;
|
|
|
|
}
|
|
|
|
Command::Disallow { domain } if db.remove_allows(vec![domain.clone()]).await.is_ok() => {
|
|
|
|
bot.send_message(msg.chat.id, format!("{} has been disallowed", domain))
|
|
|
|
.await?;
|
|
|
|
}
|
|
|
|
Command::ListAllowed => {
|
2022-11-17 20:13:41 +00:00
|
|
|
if let Ok(allowed) = db.allows().await {
|
2022-11-08 00:49:19 +00:00
|
|
|
bot.send_message(msg.chat.id, allowed.join("\n")).await?;
|
2022-11-02 22:58:52 +00:00
|
|
|
}
|
|
|
|
}
|
2022-11-08 00:49:19 +00:00
|
|
|
Command::ListBlocks => {
|
|
|
|
if let Ok(blocks) = db.blocks().await {
|
|
|
|
bot.send_message(msg.chat.id, blocks.join("\n")).await?;
|
2022-11-02 22:58:52 +00:00
|
|
|
}
|
|
|
|
}
|
2022-11-08 00:49:19 +00:00
|
|
|
Command::ListConnected => {
|
|
|
|
if let Ok(connected) = db.connected_ids().await {
|
|
|
|
bot.send_message(msg.chat.id, connected.join("\n")).await?;
|
2022-11-02 22:58:52 +00:00
|
|
|
}
|
|
|
|
}
|
2022-11-08 00:49:19 +00:00
|
|
|
_ => {
|
|
|
|
bot.send_message(msg.chat.id, "Internal server error")
|
|
|
|
.await?;
|
|
|
|
}
|
2022-11-02 22:58:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|