fmt
This commit is contained in:
10
rustfmt.toml
Normal file
10
rustfmt.toml
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
max_width = 120
|
||||||
|
hard_tabs = false
|
||||||
|
tab_spaces = 4
|
||||||
|
edition = "2021"
|
||||||
|
reorder_imports = true
|
||||||
|
reorder_modules = true
|
||||||
|
format_strings = true
|
||||||
|
normalize_comments = true
|
||||||
|
wrap_comments = true
|
||||||
|
comment_width = 80
|
||||||
@@ -80,11 +80,7 @@ mod tests {
|
|||||||
fn parses_double_quoted_argument() {
|
fn parses_double_quoted_argument() {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
split_command_line(r#"/cmd arg1 "arg2 value""#),
|
split_command_line(r#"/cmd arg1 "arg2 value""#),
|
||||||
vec![
|
vec!["/cmd".to_string(), "arg1".to_string(), "arg2 value".to_string()]
|
||||||
"/cmd".to_string(),
|
|
||||||
"arg1".to_string(),
|
|
||||||
"arg2 value".to_string()
|
|
||||||
]
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -41,10 +41,7 @@ pub async fn global_usage(ctx: Arc<Mutex<BotContext>>) -> String {
|
|||||||
|
|
||||||
usage += "\nCommands from plugins:\n";
|
usage += "\nCommands from plugins:\n";
|
||||||
for cmd in ctx_lock.plugin_commands.values() {
|
for cmd in ctx_lock.plugin_commands.values() {
|
||||||
usage += &format!(
|
usage += &format!("{} (from {}) - {}\n", &cmd.usage, &cmd.plugin_id, &cmd.description);
|
||||||
"{} (from {}) - {}\n",
|
|
||||||
&cmd.usage, &cmd.plugin_id, &cmd.description
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
usage += "\nDeltaChat Remote Control Bot by slavasil, 2026\n";
|
usage += "\nDeltaChat Remote Control Bot by slavasil, 2026\n";
|
||||||
@@ -65,12 +62,7 @@ pub async fn auth_command(
|
|||||||
let dchat_ctx_lock = dchat_ctx.lock().await;
|
let dchat_ctx_lock = dchat_ctx.lock().await;
|
||||||
|
|
||||||
if !AUTH_REQUIRED {
|
if !AUTH_REQUIRED {
|
||||||
chat::send_text_msg(
|
chat::send_text_msg(&dchat_ctx_lock, chat_id, "Authentication is disabled".to_owned()).await?;
|
||||||
&dchat_ctx_lock,
|
|
||||||
chat_id,
|
|
||||||
"Authentication is disabled".to_owned(),
|
|
||||||
)
|
|
||||||
.await?;
|
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -80,23 +72,13 @@ pub async fn auth_command(
|
|||||||
}
|
}
|
||||||
|
|
||||||
let Some(password) = args.first() else {
|
let Some(password) = args.first() else {
|
||||||
chat::send_text_msg(
|
chat::send_text_msg(&dchat_ctx_lock, chat_id, "Usage: /auth <password>".to_owned()).await?;
|
||||||
&dchat_ctx_lock,
|
|
||||||
chat_id,
|
|
||||||
"Usage: /auth <password>".to_owned(),
|
|
||||||
)
|
|
||||||
.await?;
|
|
||||||
return Ok(());
|
return Ok(());
|
||||||
};
|
};
|
||||||
|
|
||||||
if *password == ctx_lock.config.auth.password {
|
if *password == ctx_lock.config.auth.password {
|
||||||
ctx_lock.authed_contacts.insert(contact_id);
|
ctx_lock.authed_contacts.insert(contact_id);
|
||||||
chat::send_text_msg(
|
chat::send_text_msg(&dchat_ctx_lock, chat_id, "Authentication successful!".to_owned()).await?;
|
||||||
&dchat_ctx_lock,
|
|
||||||
chat_id,
|
|
||||||
"Authentication successful!".to_owned(),
|
|
||||||
)
|
|
||||||
.await?;
|
|
||||||
} else {
|
} else {
|
||||||
chat::send_text_msg(&dchat_ctx_lock, chat_id, "Incorrect password".to_owned()).await?;
|
chat::send_text_msg(&dchat_ctx_lock, chat_id, "Incorrect password".to_owned()).await?;
|
||||||
}
|
}
|
||||||
@@ -201,12 +183,7 @@ async fn send_magic_packet(mac: MacAddress) -> AnyhowResult<()> {
|
|||||||
|
|
||||||
fn ssh_unlock_disk_usage(config: &BotConfig) -> String {
|
fn ssh_unlock_disk_usage(config: &BotConfig) -> String {
|
||||||
if !config.machines.is_empty() {
|
if !config.machines.is_empty() {
|
||||||
let list = config
|
let list = config.machines.keys().cloned().collect::<Vec<_>>().join("|");
|
||||||
.machines
|
|
||||||
.keys()
|
|
||||||
.cloned()
|
|
||||||
.collect::<Vec<_>>()
|
|
||||||
.join("|");
|
|
||||||
if config.default_machine().is_some() {
|
if config.default_machine().is_some() {
|
||||||
format!("Usage: /ssh-unlock-disk [{list}] <password>")
|
format!("Usage: /ssh-unlock-disk [{list}] <password>")
|
||||||
} else {
|
} else {
|
||||||
@@ -235,12 +212,7 @@ pub async fn ssh_unlock_disk_command(
|
|||||||
|
|
||||||
if args.is_empty() || args.len() > 2 {
|
if args.is_empty() || args.len() > 2 {
|
||||||
log::warn!("wrong number of args");
|
log::warn!("wrong number of args");
|
||||||
chat::send_text_msg(
|
chat::send_text_msg(&dchat_ctx_lock, chat_id, ssh_unlock_disk_usage(&ctx_lock.config)).await?;
|
||||||
&dchat_ctx_lock,
|
|
||||||
chat_id,
|
|
||||||
ssh_unlock_disk_usage(&ctx_lock.config),
|
|
||||||
)
|
|
||||||
.await?;
|
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -255,12 +227,7 @@ pub async fn ssh_unlock_disk_command(
|
|||||||
None
|
None
|
||||||
}
|
}
|
||||||
}) else {
|
}) else {
|
||||||
chat::send_text_msg(
|
chat::send_text_msg(&dchat_ctx_lock, chat_id, ssh_unlock_disk_usage(&ctx_lock.config)).await?;
|
||||||
&dchat_ctx_lock,
|
|
||||||
chat_id,
|
|
||||||
ssh_unlock_disk_usage(&ctx_lock.config),
|
|
||||||
)
|
|
||||||
.await?;
|
|
||||||
return Ok(());
|
return Ok(());
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -284,8 +251,7 @@ pub async fn ssh_unlock_disk_command(
|
|||||||
|
|
||||||
let password = args[args.len() - 1].to_owned();
|
let password = args[args.len() - 1].to_owned();
|
||||||
let host = machine.static_ip.to_string();
|
let host = machine.static_ip.to_string();
|
||||||
let ssh_key =
|
let ssh_key = russh::keys::PrivateKey::from_openssh(ssh_key).context("Invalid remote unlock SSH key")?;
|
||||||
russh::keys::PrivateKey::from_openssh(ssh_key).context("Invalid remote unlock SSH key")?;
|
|
||||||
|
|
||||||
drop(ctx_lock);
|
drop(ctx_lock);
|
||||||
drop(dchat_ctx_lock);
|
drop(dchat_ctx_lock);
|
||||||
@@ -311,10 +277,7 @@ pub async fn ssh_unlock_disk_command(
|
|||||||
SSH_TIMEOUT,
|
SSH_TIMEOUT,
|
||||||
connection.authenticate_publickey(
|
connection.authenticate_publickey(
|
||||||
"root",
|
"root",
|
||||||
PrivateKeyWithHashAlg::new(
|
PrivateKeyWithHashAlg::new(Arc::new(ssh_key), connection.best_supported_rsa_hash().await?.flatten()),
|
||||||
Arc::new(ssh_key),
|
|
||||||
connection.best_supported_rsa_hash().await?.flatten(),
|
|
||||||
),
|
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
@@ -350,10 +313,7 @@ pub async fn ssh_unlock_disk_command(
|
|||||||
.context("Cannot send password via SSH")?;
|
.context("Cannot send password via SSH")?;
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
let Some(msg) = timeout(SSH_TIMEOUT, channel.wait())
|
let Some(msg) = timeout(SSH_TIMEOUT, channel.wait()).await.context("Timed out")? else {
|
||||||
.await
|
|
||||||
.context("Timed out")?
|
|
||||||
else {
|
|
||||||
break;
|
break;
|
||||||
};
|
};
|
||||||
log::info!("Received message from SSH server: {:?}", msg);
|
log::info!("Received message from SSH server: {:?}", msg);
|
||||||
@@ -372,12 +332,7 @@ pub async fn ssh_unlock_disk_command(
|
|||||||
|
|
||||||
fn ssh_exec_usage(config: &BotConfig) -> String {
|
fn ssh_exec_usage(config: &BotConfig) -> String {
|
||||||
if !config.machines.is_empty() {
|
if !config.machines.is_empty() {
|
||||||
let list = config
|
let list = config.machines.keys().cloned().collect::<Vec<_>>().join("|");
|
||||||
.machines
|
|
||||||
.keys()
|
|
||||||
.cloned()
|
|
||||||
.collect::<Vec<_>>()
|
|
||||||
.join("|");
|
|
||||||
format!("Usage: /ssh-exec <username>@({list}|<hostname>|<IP address>) <command...>")
|
format!("Usage: /ssh-exec <username>@({list}|<hostname>|<IP address>) <command...>")
|
||||||
} else {
|
} else {
|
||||||
"Command /ssh-exec unavailable: no machines configured".to_string()
|
"Command /ssh-exec unavailable: no machines configured".to_string()
|
||||||
@@ -499,22 +454,14 @@ pub async fn ssh_exec_command(
|
|||||||
russh::ChannelMsg::Data { data } => {
|
russh::ChannelMsg::Data { data } => {
|
||||||
let dchat_ctx_lock = dchat_ctx.lock().await;
|
let dchat_ctx_lock = dchat_ctx.lock().await;
|
||||||
report_text += &String::from_utf8_lossy(&data);
|
report_text += &String::from_utf8_lossy(&data);
|
||||||
chat::send_edit_request(
|
chat::send_edit_request(&dchat_ctx_lock, report_message_id, report_text.clone())
|
||||||
&dchat_ctx_lock,
|
|
||||||
report_message_id,
|
|
||||||
report_text.clone(),
|
|
||||||
)
|
|
||||||
.await
|
.await
|
||||||
.context("Cannot edit message")?;
|
.context("Cannot edit message")?;
|
||||||
}
|
}
|
||||||
russh::ChannelMsg::ExtendedData { data, ext: 1 } => {
|
russh::ChannelMsg::ExtendedData { data, ext: 1 } => {
|
||||||
let dchat_ctx_lock = dchat_ctx.lock().await;
|
let dchat_ctx_lock = dchat_ctx.lock().await;
|
||||||
report_text += &String::from_utf8_lossy(&data);
|
report_text += &String::from_utf8_lossy(&data);
|
||||||
chat::send_edit_request(
|
chat::send_edit_request(&dchat_ctx_lock, report_message_id, report_text.clone())
|
||||||
&dchat_ctx_lock,
|
|
||||||
report_message_id,
|
|
||||||
report_text.clone(),
|
|
||||||
)
|
|
||||||
.await
|
.await
|
||||||
.context("Cannot edit message")?;
|
.context("Cannot edit message")?;
|
||||||
}
|
}
|
||||||
@@ -531,11 +478,7 @@ pub async fn ssh_exec_command(
|
|||||||
|
|
||||||
let dchat_ctx_lock = dchat_ctx.lock().await;
|
let dchat_ctx_lock = dchat_ctx.lock().await;
|
||||||
report_text += "\n\nRead timed out";
|
report_text += "\n\nRead timed out";
|
||||||
chat::send_edit_request(
|
chat::send_edit_request(&dchat_ctx_lock, report_message_id, report_text.clone())
|
||||||
&dchat_ctx_lock,
|
|
||||||
report_message_id,
|
|
||||||
report_text.clone(),
|
|
||||||
)
|
|
||||||
.await
|
.await
|
||||||
.context("Cannot edit message")?;
|
.context("Cannot edit message")?;
|
||||||
|
|
||||||
|
|||||||
84
src/main.rs
84
src/main.rs
@@ -7,10 +7,7 @@ mod ssh;
|
|||||||
mod proto {
|
mod proto {
|
||||||
pub(crate) mod deltachat_remotecontrol_bot {
|
pub(crate) mod deltachat_remotecontrol_bot {
|
||||||
pub(crate) mod plugin {
|
pub(crate) mod plugin {
|
||||||
include!(concat!(
|
include!(concat!(env!("OUT_DIR"), "/deltachat_remotecontrol_bot.plugin.rs"));
|
||||||
env!("OUT_DIR"),
|
|
||||||
"/deltachat_remotecontrol_bot.plugin.rs"
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub(crate) use deltachat_remotecontrol_bot::plugin::*;
|
pub(crate) use deltachat_remotecontrol_bot::plugin::*;
|
||||||
@@ -82,12 +79,7 @@ async fn run_bot(bot_context: Arc<Mutex<BotContext>>) -> AnyhowResult<()> {
|
|||||||
|
|
||||||
let dchat_db_filename = dchat_db_dir.join("deltachat.sqlite");
|
let dchat_db_filename = dchat_db_dir.join("deltachat.sqlite");
|
||||||
|
|
||||||
let dchat_ctx = Context::new(
|
let dchat_ctx = Context::new(&dchat_db_filename, 0, Events::default(), StockStrings::default())
|
||||||
&dchat_db_filename,
|
|
||||||
0,
|
|
||||||
Events::default(),
|
|
||||||
StockStrings::default(),
|
|
||||||
)
|
|
||||||
.await
|
.await
|
||||||
.context("Failed to open Delta Chat client DB")?;
|
.context("Failed to open Delta Chat client DB")?;
|
||||||
|
|
||||||
@@ -114,9 +106,7 @@ async fn run_bot(bot_context: Arc<Mutex<BotContext>>) -> AnyhowResult<()> {
|
|||||||
avatar_path = data_path().join(&avatar_path);
|
avatar_path = data_path().join(&avatar_path);
|
||||||
}
|
}
|
||||||
if let Some(avatar_str) = avatar_path.to_str() {
|
if let Some(avatar_str) = avatar_path.to_str() {
|
||||||
dchat_ctx
|
dchat_ctx.set_config(Config::Selfavatar, Some(avatar_str)).await?;
|
||||||
.set_config(Config::Selfavatar, Some(avatar_str))
|
|
||||||
.await?;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -156,12 +146,7 @@ async fn send_command_not_found(
|
|||||||
chat_id: ChatId,
|
chat_id: ChatId,
|
||||||
) -> AnyhowResult<()> {
|
) -> AnyhowResult<()> {
|
||||||
let dchat_ctx_lock = dchat_ctx.lock().await;
|
let dchat_ctx_lock = dchat_ctx.lock().await;
|
||||||
chat::send_text_msg(
|
chat::send_text_msg(&dchat_ctx_lock, chat_id, commands::global_usage(bot_ctx).await).await?;
|
||||||
&dchat_ctx_lock,
|
|
||||||
chat_id,
|
|
||||||
commands::global_usage(bot_ctx).await,
|
|
||||||
)
|
|
||||||
.await?;
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -208,9 +193,7 @@ async fn handle_message(
|
|||||||
};
|
};
|
||||||
|
|
||||||
let Some(plugin) = ctx_lock.plugins.get(&cmd.plugin_id) else {
|
let Some(plugin) = ctx_lock.plugins.get(&cmd.plugin_id) else {
|
||||||
bail!(
|
bail!("Inconsistency in plugin_cmd_aliases hashmap: command references a plugin that is not loaded");
|
||||||
"Inconsistency in plugin_cmd_aliases hashmap: command references a plugin that is not loaded"
|
|
||||||
);
|
|
||||||
};
|
};
|
||||||
let (plugin_conn, plugin_name) = {
|
let (plugin_conn, plugin_name) = {
|
||||||
let lock = plugin.lock().await;
|
let lock = plugin.lock().await;
|
||||||
@@ -218,11 +201,7 @@ async fn handle_message(
|
|||||||
bail!("Plugin disconnected");
|
bail!("Plugin disconnected");
|
||||||
};
|
};
|
||||||
let plugin_name = lock.plugin_id.clone();
|
let plugin_name = lock.plugin_id.clone();
|
||||||
log::info!(
|
log::info!("Delegating command /{} to plugin {}", &cmd.name, &plugin_name);
|
||||||
"Delegating command /{} to plugin {}",
|
|
||||||
&cmd.name,
|
|
||||||
&plugin_name
|
|
||||||
);
|
|
||||||
(conn, plugin_name)
|
(conn, plugin_name)
|
||||||
};
|
};
|
||||||
let contact_id = msg.get_from_id();
|
let contact_id = msg.get_from_id();
|
||||||
@@ -239,11 +218,7 @@ async fn handle_message(
|
|||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
log::debug!("Started plugin command task");
|
log::debug!("Started plugin command task");
|
||||||
match plugin_conn
|
match plugin_conn
|
||||||
.execute_command(
|
.execute_command(cmd_name.clone(), issuer_id, commands::args::split_command_line(&text))
|
||||||
cmd_name.clone(),
|
|
||||||
issuer_id,
|
|
||||||
commands::args::split_command_line(&text),
|
|
||||||
)
|
|
||||||
.await
|
.await
|
||||||
{
|
{
|
||||||
Ok(mut replies) => {
|
Ok(mut replies) => {
|
||||||
@@ -259,21 +234,15 @@ async fn handle_message(
|
|||||||
);
|
);
|
||||||
if reply.edit {
|
if reply.edit {
|
||||||
if let Some(msg_id) = last_message_id {
|
if let Some(msg_id) = last_message_id {
|
||||||
if let Some(proto::command_reply::Reply::Text(text)) =
|
if let Some(proto::command_reply::Reply::Text(text)) = reply.reply {
|
||||||
reply.reply
|
|
||||||
{
|
|
||||||
let dchat_ctx_lock = dchat_ctx.lock().await;
|
let dchat_ctx_lock = dchat_ctx.lock().await;
|
||||||
log::debug!(
|
log::debug!(
|
||||||
"Command {} :: /{}: editing last message",
|
"Command {} :: /{}: editing last message",
|
||||||
&plugin_name,
|
&plugin_name,
|
||||||
&cmd_name
|
&cmd_name
|
||||||
);
|
);
|
||||||
if let Err(e) = chat::send_edit_request(
|
if let Err(e) =
|
||||||
&dchat_ctx_lock,
|
chat::send_edit_request(&dchat_ctx_lock, msg_id, text).await
|
||||||
msg_id,
|
|
||||||
text,
|
|
||||||
)
|
|
||||||
.await
|
|
||||||
{
|
{
|
||||||
log::error!("Cannot edit message: {e}");
|
log::error!("Cannot edit message: {e}");
|
||||||
continue;
|
continue;
|
||||||
@@ -285,22 +254,10 @@ async fn handle_message(
|
|||||||
&plugin_name
|
&plugin_name
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
} else {
|
} else if let Some(proto::command_reply::Reply::Text(text)) = reply.reply {
|
||||||
if let Some(proto::command_reply::Reply::Text(text)) =
|
|
||||||
reply.reply
|
|
||||||
{
|
|
||||||
let dchat_ctx_lock = dchat_ctx.lock().await;
|
let dchat_ctx_lock = dchat_ctx.lock().await;
|
||||||
log::debug!(
|
log::debug!("Command {} :: /{}: sending message", &plugin_name, &cmd_name);
|
||||||
"Command {} :: /{}: sending message",
|
let msg_id = match chat::send_text_msg(&dchat_ctx_lock, chat_id, text).await
|
||||||
&plugin_name,
|
|
||||||
&cmd_name
|
|
||||||
);
|
|
||||||
let msg_id = match chat::send_text_msg(
|
|
||||||
&dchat_ctx_lock,
|
|
||||||
chat_id,
|
|
||||||
text,
|
|
||||||
)
|
|
||||||
.await
|
|
||||||
{
|
{
|
||||||
Ok(msg_id) => msg_id,
|
Ok(msg_id) => msg_id,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
@@ -311,14 +268,8 @@ async fn handle_message(
|
|||||||
last_message_id = Some(msg_id);
|
last_message_id = Some(msg_id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
log::error!(
|
log::error!("Error while executing {} :: /{}: {}", &plugin_name, &cmd_name, e);
|
||||||
"Error while executing {} :: /{}: {}",
|
|
||||||
&plugin_name,
|
|
||||||
&cmd_name,
|
|
||||||
e
|
|
||||||
);
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -374,12 +325,7 @@ async fn main() {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let requested_plugins: Vec<PluginConfig> = config
|
let requested_plugins: Vec<PluginConfig> = config.plugins.clone().into_iter().filter(|p| p.enabled).collect();
|
||||||
.plugins
|
|
||||||
.clone()
|
|
||||||
.into_iter()
|
|
||||||
.filter(|p| p.enabled)
|
|
||||||
.collect();
|
|
||||||
|
|
||||||
let bot_context = Arc::new(Mutex::new(BotContext::new(config)));
|
let bot_context = Arc::new(Mutex::new(BotContext::new(config)));
|
||||||
|
|
||||||
|
|||||||
15
src/paths.rs
15
src/paths.rs
@@ -16,11 +16,7 @@ pub(crate) fn default_config_paths() -> Vec<PathBuf> {
|
|||||||
paths.push(config_home.join(APP_CONFIG_DIR).join(CONFIG_FILENAME));
|
paths.push(config_home.join(APP_CONFIG_DIR).join(CONFIG_FILENAME));
|
||||||
}
|
}
|
||||||
|
|
||||||
paths.push(
|
paths.push(PathBuf::from("/etc").join(APP_CONFIG_DIR).join(CONFIG_FILENAME));
|
||||||
PathBuf::from("/etc")
|
|
||||||
.join(APP_CONFIG_DIR)
|
|
||||||
.join(CONFIG_FILENAME),
|
|
||||||
);
|
|
||||||
|
|
||||||
paths
|
paths
|
||||||
}
|
}
|
||||||
@@ -28,14 +24,7 @@ pub(crate) fn default_config_paths() -> Vec<PathBuf> {
|
|||||||
pub(crate) fn data_path() -> PathBuf {
|
pub(crate) fn data_path() -> PathBuf {
|
||||||
std::env::var("XDG_DATA_HOME")
|
std::env::var("XDG_DATA_HOME")
|
||||||
.map(|dh| PathBuf::from(dh).join(APP_DATA_DIR))
|
.map(|dh| PathBuf::from(dh).join(APP_DATA_DIR))
|
||||||
.or_else(|_| {
|
.or_else(|_| std::env::var("HOME").map(|h| PathBuf::from(h).join(".local").join("share").join(APP_DATA_DIR)))
|
||||||
std::env::var("HOME").map(|h| {
|
|
||||||
PathBuf::from(h)
|
|
||||||
.join(".local")
|
|
||||||
.join("share")
|
|
||||||
.join(APP_DATA_DIR)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
.unwrap_or(PathBuf::from("."))
|
.unwrap_or(PathBuf::from("."))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -58,24 +58,17 @@ impl Display for PluginRequestType {
|
|||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
pub(crate) trait PluginConnection: Send + Sync + Debug {
|
pub(crate) trait PluginConnection: Send + Sync + Debug {
|
||||||
async fn initialize_plugin(
|
async fn initialize_plugin(&self, config: String)
|
||||||
&self,
|
-> Result<proto::PluginInitializeResponse, PluginConnectionError>;
|
||||||
config: String,
|
|
||||||
) -> Result<proto::PluginInitializeResponse, PluginConnectionError>;
|
|
||||||
|
|
||||||
async fn request_plugin_command_list(
|
async fn request_plugin_command_list(&self) -> Result<proto::PluginCommandListResponse, PluginConnectionError>;
|
||||||
&self,
|
|
||||||
) -> Result<proto::PluginCommandListResponse, PluginConnectionError>;
|
|
||||||
|
|
||||||
async fn execute_command(
|
async fn execute_command(
|
||||||
self: Arc<Self>,
|
self: Arc<Self>,
|
||||||
command_id: String,
|
command_id: String,
|
||||||
issuer_id: String,
|
issuer_id: String,
|
||||||
argv: Vec<String>,
|
argv: Vec<String>,
|
||||||
) -> Result<
|
) -> Result<mpsc::Receiver<Result<proto::CommandReply, PluginConnectionError>>, PluginConnectionError>;
|
||||||
mpsc::Receiver<Result<proto::CommandReply, PluginConnectionError>>,
|
|
||||||
PluginConnectionError,
|
|
||||||
>;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
@@ -138,8 +131,7 @@ pub(crate) async fn try_load_plugin(
|
|||||||
cmd.env("DCRCBOT_PLUGIN_TRANSPORT", "stdio");
|
cmd.env("DCRCBOT_PLUGIN_TRANSPORT", "stdio");
|
||||||
|
|
||||||
let plugin_process = cmd.spawn().context("Failed to start the plugin")?;
|
let plugin_process = cmd.spawn().context("Failed to start the plugin")?;
|
||||||
let plugin =
|
let plugin = stdio::initialize_stdio_plugin(plugin_process, unique_name.clone(), config_json).await?;
|
||||||
stdio::initialize_stdio_plugin(plugin_process, unique_name.clone(), config_json).await?;
|
|
||||||
|
|
||||||
let mut ctx_lock = ctx.lock().await;
|
let mut ctx_lock = ctx.lock().await;
|
||||||
for cmd in plugin.lock().await.commands.iter().cloned() {
|
for cmd in plugin.lock().await.commands.iter().cloned() {
|
||||||
@@ -147,12 +139,8 @@ pub(crate) async fn try_load_plugin(
|
|||||||
if ctx_lock.plugin_commands.contains_key(&cmd.name) {
|
if ctx_lock.plugin_commands.contains_key(&cmd.name) {
|
||||||
bail!("duplicate command specification");
|
bail!("duplicate command specification");
|
||||||
}
|
}
|
||||||
ctx_lock
|
ctx_lock.plugin_commands.insert(cmd.name.clone(), Arc::clone(&cmd));
|
||||||
.plugin_commands
|
ctx_lock.plugin_cmd_aliases.insert(cmd.name.clone(), Arc::clone(&cmd));
|
||||||
.insert(cmd.name.clone(), Arc::clone(&cmd));
|
|
||||||
ctx_lock
|
|
||||||
.plugin_cmd_aliases
|
|
||||||
.insert(cmd.name.clone(), Arc::clone(&cmd));
|
|
||||||
for alias in cmd.aliases.iter() {
|
for alias in cmd.aliases.iter() {
|
||||||
log::debug!(
|
log::debug!(
|
||||||
"adding command alias /{} -> /{} of plugin {}",
|
"adding command alias /{} -> /{} of plugin {}",
|
||||||
@@ -160,9 +148,7 @@ pub(crate) async fn try_load_plugin(
|
|||||||
&cmd.name,
|
&cmd.name,
|
||||||
&unique_name
|
&unique_name
|
||||||
);
|
);
|
||||||
ctx_lock
|
ctx_lock.plugin_cmd_aliases.insert(alias.to_owned(), Arc::clone(&cmd));
|
||||||
.plugin_cmd_aliases
|
|
||||||
.insert(alias.to_owned(), Arc::clone(&cmd));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -10,9 +10,7 @@ use tokio::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
plugin::{
|
plugin::{LoadedPlugin, PluginCommand, PluginConnection, PluginConnectionError, PluginRequestType},
|
||||||
LoadedPlugin, PluginCommand, PluginConnection, PluginConnectionError, PluginRequestType,
|
|
||||||
},
|
|
||||||
proto,
|
proto,
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -85,10 +83,7 @@ impl StdioPluginConnection {
|
|||||||
|
|
||||||
async fn stdout_reader_loop(self: Arc<Self>) {
|
async fn stdout_reader_loop(self: Arc<Self>) {
|
||||||
loop {
|
loop {
|
||||||
let frame =
|
let frame = match Self::read_length_delimited(self.buffered_stdout.lock().await.deref_mut()).await {
|
||||||
match Self::read_length_delimited(self.buffered_stdout.lock().await.deref_mut())
|
|
||||||
.await
|
|
||||||
{
|
|
||||||
Ok(frame) => frame,
|
Ok(frame) => frame,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
log::error!(
|
log::error!(
|
||||||
@@ -133,9 +128,7 @@ impl StdioPluginConnection {
|
|||||||
self.pending_requests.lock().await.clear();
|
self.pending_requests.lock().await.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn read_length_delimited<R>(
|
async fn read_length_delimited<R>(reader: &mut R) -> Result<Vec<u8>, Box<dyn Error + Send + Sync>>
|
||||||
reader: &mut R,
|
|
||||||
) -> Result<Vec<u8>, Box<dyn Error + Send + Sync>>
|
|
||||||
where
|
where
|
||||||
R: AsyncRead + Unpin,
|
R: AsyncRead + Unpin,
|
||||||
{
|
{
|
||||||
|
|||||||
23
src/ssh.rs
23
src/ssh.rs
@@ -36,11 +36,7 @@ pub(crate) async fn connect_ssh<A: ToSocketAddrs + Debug, K: AsRef<[u8]>, P: Int
|
|||||||
"Connecting to SSH {:?} {} key & {} password",
|
"Connecting to SSH {:?} {} key & {} password",
|
||||||
&addr,
|
&addr,
|
||||||
if key.is_some() { "with" } else { "without" },
|
if key.is_some() { "with" } else { "without" },
|
||||||
if password.is_some() {
|
if password.is_some() { "with" } else { "without" }
|
||||||
"with"
|
|
||||||
} else {
|
|
||||||
"without"
|
|
||||||
}
|
|
||||||
);
|
);
|
||||||
let mut connection = tokio::time::timeout(
|
let mut connection = tokio::time::timeout(
|
||||||
connect_timeout,
|
connect_timeout,
|
||||||
@@ -65,12 +61,7 @@ pub(crate) async fn connect_ssh<A: ToSocketAddrs + Debug, K: AsRef<[u8]>, P: Int
|
|||||||
if !auth_success {
|
if !auth_success {
|
||||||
if let Some(key) = key {
|
if let Some(key) = key {
|
||||||
log::info!("Trying to authenticate using key");
|
log::info!("Trying to authenticate using key");
|
||||||
match tokio::time::timeout(
|
match tokio::time::timeout(auth_timeout, connection.authenticate_publickey(username, key)).await {
|
||||||
auth_timeout,
|
|
||||||
connection.authenticate_publickey(username, key),
|
|
||||||
)
|
|
||||||
.await
|
|
||||||
{
|
|
||||||
Ok(Ok(AuthResult::Success)) => {
|
Ok(Ok(AuthResult::Success)) => {
|
||||||
auth_success = true;
|
auth_success = true;
|
||||||
}
|
}
|
||||||
@@ -78,8 +69,7 @@ pub(crate) async fn connect_ssh<A: ToSocketAddrs + Debug, K: AsRef<[u8]>, P: Int
|
|||||||
remaining_methods,
|
remaining_methods,
|
||||||
partial_success,
|
partial_success,
|
||||||
})) => {
|
})) => {
|
||||||
if partial_success || !remaining_methods.contains(&russh::MethodKind::Password)
|
if partial_success || !remaining_methods.contains(&russh::MethodKind::Password) {
|
||||||
{
|
|
||||||
if partial_success {
|
if partial_success {
|
||||||
anyhow::bail!("SSH auth failed: multi-factor auth is not supported");
|
anyhow::bail!("SSH auth failed: multi-factor auth is not supported");
|
||||||
} else {
|
} else {
|
||||||
@@ -102,12 +92,7 @@ pub(crate) async fn connect_ssh<A: ToSocketAddrs + Debug, K: AsRef<[u8]>, P: Int
|
|||||||
if !auth_success {
|
if !auth_success {
|
||||||
if let Some(password) = password {
|
if let Some(password) = password {
|
||||||
log::info!("Trying to authenticate using password");
|
log::info!("Trying to authenticate using password");
|
||||||
match tokio::time::timeout(
|
match tokio::time::timeout(auth_timeout, connection.authenticate_password(username, password)).await {
|
||||||
auth_timeout,
|
|
||||||
connection.authenticate_password(username, password),
|
|
||||||
)
|
|
||||||
.await
|
|
||||||
{
|
|
||||||
Ok(Ok(AuthResult::Success)) => {
|
Ok(Ok(AuthResult::Success)) => {
|
||||||
auth_success = true;
|
auth_success = true;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user