|
|
@ -5,6 +5,7 @@ use crate::{
|
|
|
|
pdu::PduBuilder,
|
|
|
|
pdu::PduBuilder,
|
|
|
|
server_server, Database, PduEvent,
|
|
|
|
server_server, Database, PduEvent,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
use regex::Regex;
|
|
|
|
use rocket::{
|
|
|
|
use rocket::{
|
|
|
|
futures::{channel::mpsc, stream::StreamExt},
|
|
|
|
futures::{channel::mpsc, stream::StreamExt},
|
|
|
|
http::RawStr,
|
|
|
|
http::RawStr,
|
|
|
@ -14,6 +15,7 @@ use ruma::{
|
|
|
|
EventId, RoomId, RoomVersionId, UserId,
|
|
|
|
EventId, RoomId, RoomVersionId, UserId,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
use serde_json::value::to_raw_value;
|
|
|
|
use serde_json::value::to_raw_value;
|
|
|
|
|
|
|
|
use structopt::StructOpt;
|
|
|
|
use tokio::sync::{MutexGuard, RwLock, RwLockReadGuard};
|
|
|
|
use tokio::sync::{MutexGuard, RwLock, RwLockReadGuard};
|
|
|
|
use tracing::warn;
|
|
|
|
use tracing::warn;
|
|
|
|
|
|
|
|
|
|
|
@ -146,78 +148,98 @@ impl Admin {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub fn parse_admin_command(db: &Database, command_line: &str, body: Vec<&str>) -> AdminCommand {
|
|
|
|
pub fn parse_admin_command(db: &Database, command_line: &str, body: Vec<&str>) -> AdminCommand {
|
|
|
|
let mut parts = command_line.split_whitespace().skip(1);
|
|
|
|
let mut argv: Vec<_> = command_line.split_whitespace().skip(1).collect();
|
|
|
|
|
|
|
|
|
|
|
|
let command_name = match parts.next() {
|
|
|
|
let command_name = match argv.get(0) {
|
|
|
|
Some(command) => command,
|
|
|
|
Some(command) => *command,
|
|
|
|
None => {
|
|
|
|
None => {
|
|
|
|
let message = "No command given. Use <code>help</code> for a list of commands.";
|
|
|
|
let markdown_message = "No command given. Use `help` for a list of commands.";
|
|
|
|
|
|
|
|
let html_message = markdown_to_html(&markdown_message);
|
|
|
|
|
|
|
|
|
|
|
|
return AdminCommand::SendMessage(RoomMessageEventContent::text_html(
|
|
|
|
return AdminCommand::SendMessage(RoomMessageEventContent::text_html(
|
|
|
|
html_to_markdown(message),
|
|
|
|
markdown_message,
|
|
|
|
message,
|
|
|
|
html_message,
|
|
|
|
));
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
let args: Vec<_> = parts.collect();
|
|
|
|
// Backwards compatibility with `register_appservice`-style commands
|
|
|
|
|
|
|
|
let command_with_dashes;
|
|
|
|
|
|
|
|
if command_line.contains("_") {
|
|
|
|
|
|
|
|
command_with_dashes = command_name.replace("_", "-");
|
|
|
|
|
|
|
|
argv[0] = &command_with_dashes;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
match try_parse_admin_command(db, command_name, args, body) {
|
|
|
|
match try_parse_admin_command(db, argv, body) {
|
|
|
|
Ok(admin_command) => admin_command,
|
|
|
|
Ok(admin_command) => admin_command,
|
|
|
|
Err(error) => {
|
|
|
|
Err(error) => {
|
|
|
|
let message = format!(
|
|
|
|
let markdown_message = format!(
|
|
|
|
"Encountered error while handling <code>{}</code> command:\n\
|
|
|
|
"Encountered an error while handling the `{}` command:\n\
|
|
|
|
<pre>{}</pre>",
|
|
|
|
```\n{}\n```",
|
|
|
|
command_name, error,
|
|
|
|
command_name, error,
|
|
|
|
);
|
|
|
|
);
|
|
|
|
|
|
|
|
let html_message = markdown_to_html(&markdown_message);
|
|
|
|
|
|
|
|
|
|
|
|
AdminCommand::SendMessage(RoomMessageEventContent::text_html(
|
|
|
|
AdminCommand::SendMessage(RoomMessageEventContent::text_html(
|
|
|
|
html_to_markdown(&message),
|
|
|
|
markdown_message,
|
|
|
|
message,
|
|
|
|
html_message,
|
|
|
|
))
|
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Helper for `RoomMessageEventContent::text_html`, which needs the content as
|
|
|
|
#[derive(StructOpt)]
|
|
|
|
// both markdown and HTML.
|
|
|
|
enum AdminCommands {
|
|
|
|
fn html_to_markdown(text: &str) -> String {
|
|
|
|
#[structopt(verbatim_doc_comment)]
|
|
|
|
text.replace("<p>", "")
|
|
|
|
/// Register a bridge using its registration YAML
|
|
|
|
.replace("</p>", "\n")
|
|
|
|
///
|
|
|
|
.replace("<pre>", "```\n")
|
|
|
|
/// This command needs a YAML generated by an appservice (such as a mautrix
|
|
|
|
.replace("</pre>", "\n```")
|
|
|
|
/// bridge), which must be provided in a code-block below the command.
|
|
|
|
.replace("<code>", "`")
|
|
|
|
///
|
|
|
|
.replace("</code>", "`")
|
|
|
|
/// Example:
|
|
|
|
.replace("<li>", "* ")
|
|
|
|
/// ````
|
|
|
|
.replace("</li>", "")
|
|
|
|
/// @conduit:example.com: register-appservice
|
|
|
|
.replace("<ul>\n", "")
|
|
|
|
/// ```
|
|
|
|
.replace("</ul>\n", "")
|
|
|
|
/// yaml content here
|
|
|
|
|
|
|
|
/// ```
|
|
|
|
|
|
|
|
/// ````
|
|
|
|
|
|
|
|
RegisterAppservice,
|
|
|
|
|
|
|
|
/// Unregister a bridge using its ID
|
|
|
|
|
|
|
|
UnregisterAppservice { appservice_identifier: String },
|
|
|
|
|
|
|
|
/// List all the currently registered bridges
|
|
|
|
|
|
|
|
ListAppservices,
|
|
|
|
|
|
|
|
/// Get the auth_chain of a PDU
|
|
|
|
|
|
|
|
GetAuthChain { event_id: Box<EventId> },
|
|
|
|
|
|
|
|
/// Parse and print a PDU from a JSON
|
|
|
|
|
|
|
|
ParsePdu,
|
|
|
|
|
|
|
|
/// Retrieve and print a PDU by ID from the Conduit database
|
|
|
|
|
|
|
|
GetPdu { event_id: Box<EventId> },
|
|
|
|
|
|
|
|
/// Print database memory usage statistics
|
|
|
|
|
|
|
|
DatabaseMemoryUsage,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const HELP_TEXT: &'static str = r#"
|
|
|
|
|
|
|
|
<p>The following commands are available:</p>
|
|
|
|
|
|
|
|
<ul>
|
|
|
|
|
|
|
|
<li><code>register_appservice</code>: Register a bridge using its registration YAML</li>
|
|
|
|
|
|
|
|
<li><code>unregister_appservice</code>: Unregister a bridge using its ID</li>
|
|
|
|
|
|
|
|
<li><code>list_appservices</code>: List all the currently registered bridges</li>
|
|
|
|
|
|
|
|
<li><code>get_auth_chain</code>: Get the `auth_chain` of a PDU</li>
|
|
|
|
|
|
|
|
<li><code>parse_pdu</code>: Parse and print a PDU from a JSON</li>
|
|
|
|
|
|
|
|
<li><code>get_pdu</code>: Retrieve and print a PDU by ID from the Conduit database</li>
|
|
|
|
|
|
|
|
<li><code>database_memory_usage</code>: Print database memory usage statistics</li>
|
|
|
|
|
|
|
|
<ul>
|
|
|
|
|
|
|
|
"#;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn try_parse_admin_command(
|
|
|
|
pub fn try_parse_admin_command(
|
|
|
|
db: &Database,
|
|
|
|
db: &Database,
|
|
|
|
command: &str,
|
|
|
|
mut argv: Vec<&str>,
|
|
|
|
args: Vec<&str>,
|
|
|
|
|
|
|
|
body: Vec<&str>,
|
|
|
|
body: Vec<&str>,
|
|
|
|
) -> Result<AdminCommand> {
|
|
|
|
) -> Result<AdminCommand> {
|
|
|
|
let command = match command {
|
|
|
|
argv.insert(0, "@conduit:example.com:");
|
|
|
|
"help" => AdminCommand::SendMessage(RoomMessageEventContent::text_html(
|
|
|
|
let command = match AdminCommands::from_iter_safe(argv) {
|
|
|
|
html_to_markdown(HELP_TEXT),
|
|
|
|
Ok(command) => command,
|
|
|
|
HELP_TEXT,
|
|
|
|
Err(error) => {
|
|
|
|
)),
|
|
|
|
println!("Before:\n{}\n", error.to_string());
|
|
|
|
"register_appservice" => {
|
|
|
|
let markdown_message = usage_to_markdown(&error.to_string())
|
|
|
|
|
|
|
|
.replace("example.com", db.globals.server_name().as_str());
|
|
|
|
|
|
|
|
let html_message = markdown_to_html(&markdown_message);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return Ok(AdminCommand::SendMessage(
|
|
|
|
|
|
|
|
RoomMessageEventContent::text_html(markdown_message, html_message),
|
|
|
|
|
|
|
|
));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let admin_command = match command {
|
|
|
|
|
|
|
|
AdminCommands::RegisterAppservice => {
|
|
|
|
if body.len() > 2 && body[0].trim() == "```" && body.last().unwrap().trim() == "```" {
|
|
|
|
if body.len() > 2 && body[0].trim() == "```" && body.last().unwrap().trim() == "```" {
|
|
|
|
let appservice_config = body[1..body.len() - 1].join("\n");
|
|
|
|
let appservice_config = body[1..body.len() - 1].join("\n");
|
|
|
|
let parsed_config = serde_yaml::from_str::<serde_yaml::Value>(&appservice_config);
|
|
|
|
let parsed_config = serde_yaml::from_str::<serde_yaml::Value>(&appservice_config);
|
|
|
@ -233,47 +255,35 @@ pub fn try_parse_admin_command(
|
|
|
|
))
|
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"unregister_appservice" => {
|
|
|
|
AdminCommands::UnregisterAppservice {
|
|
|
|
if args.len() == 1 {
|
|
|
|
appservice_identifier,
|
|
|
|
AdminCommand::UnregisterAppservice(args[0].to_owned())
|
|
|
|
} => AdminCommand::UnregisterAppservice(appservice_identifier),
|
|
|
|
|
|
|
|
AdminCommands::ListAppservices => AdminCommand::ListAppservices,
|
|
|
|
|
|
|
|
AdminCommands::GetAuthChain { event_id } => {
|
|
|
|
|
|
|
|
let event_id = Arc::<EventId>::from(event_id);
|
|
|
|
|
|
|
|
if let Some(event) = db.rooms.get_pdu_json(&event_id)? {
|
|
|
|
|
|
|
|
let room_id_str = event
|
|
|
|
|
|
|
|
.get("room_id")
|
|
|
|
|
|
|
|
.and_then(|val| val.as_str())
|
|
|
|
|
|
|
|
.ok_or_else(|| Error::bad_database("Invalid event in database"))?;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let room_id = <&RoomId>::try_from(room_id_str).map_err(|_| {
|
|
|
|
|
|
|
|
Error::bad_database("Invalid room id field in event in database")
|
|
|
|
|
|
|
|
})?;
|
|
|
|
|
|
|
|
let start = Instant::now();
|
|
|
|
|
|
|
|
let count = server_server::get_auth_chain(room_id, vec![event_id], db)?.count();
|
|
|
|
|
|
|
|
let elapsed = start.elapsed();
|
|
|
|
|
|
|
|
return Ok(AdminCommand::SendMessage(
|
|
|
|
|
|
|
|
RoomMessageEventContent::text_plain(format!(
|
|
|
|
|
|
|
|
"Loaded auth chain with length {} in {:?}",
|
|
|
|
|
|
|
|
count, elapsed
|
|
|
|
|
|
|
|
)),
|
|
|
|
|
|
|
|
));
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
AdminCommand::SendMessage(RoomMessageEventContent::text_plain(
|
|
|
|
AdminCommand::SendMessage(RoomMessageEventContent::text_plain("Event not found."))
|
|
|
|
"Missing appservice identifier",
|
|
|
|
|
|
|
|
))
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"list_appservices" => AdminCommand::ListAppservices,
|
|
|
|
AdminCommands::ParsePdu => {
|
|
|
|
"get_auth_chain" => {
|
|
|
|
|
|
|
|
if args.len() == 1 {
|
|
|
|
|
|
|
|
if let Ok(event_id) = EventId::parse_arc(args[0]) {
|
|
|
|
|
|
|
|
if let Some(event) = db.rooms.get_pdu_json(&event_id)? {
|
|
|
|
|
|
|
|
let room_id_str = event
|
|
|
|
|
|
|
|
.get("room_id")
|
|
|
|
|
|
|
|
.and_then(|val| val.as_str())
|
|
|
|
|
|
|
|
.ok_or_else(|| Error::bad_database("Invalid event in database"))?;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let room_id = <&RoomId>::try_from(room_id_str).map_err(|_| {
|
|
|
|
|
|
|
|
Error::bad_database("Invalid room id field in event in database")
|
|
|
|
|
|
|
|
})?;
|
|
|
|
|
|
|
|
let start = Instant::now();
|
|
|
|
|
|
|
|
let count =
|
|
|
|
|
|
|
|
server_server::get_auth_chain(room_id, vec![event_id], db)?.count();
|
|
|
|
|
|
|
|
let elapsed = start.elapsed();
|
|
|
|
|
|
|
|
return Ok(AdminCommand::SendMessage(
|
|
|
|
|
|
|
|
RoomMessageEventContent::text_plain(format!(
|
|
|
|
|
|
|
|
"Loaded auth chain with length {} in {:?}",
|
|
|
|
|
|
|
|
count, elapsed
|
|
|
|
|
|
|
|
)),
|
|
|
|
|
|
|
|
));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
AdminCommand::SendMessage(RoomMessageEventContent::text_plain(
|
|
|
|
|
|
|
|
"Usage: get_auth_chain <event-id>",
|
|
|
|
|
|
|
|
))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
"parse_pdu" => {
|
|
|
|
|
|
|
|
if body.len() > 2 && body[0].trim() == "```" && body.last().unwrap().trim() == "```" {
|
|
|
|
if body.len() > 2 && body[0].trim() == "```" && body.last().unwrap().trim() == "```" {
|
|
|
|
let string = body[1..body.len() - 1].join("\n");
|
|
|
|
let string = body[1..body.len() - 1].join("\n");
|
|
|
|
match serde_json::from_str(&string) {
|
|
|
|
match serde_json::from_str(&string) {
|
|
|
@ -312,59 +322,83 @@ pub fn try_parse_admin_command(
|
|
|
|
))
|
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"get_pdu" => {
|
|
|
|
AdminCommands::GetPdu { event_id } => {
|
|
|
|
if args.len() == 1 {
|
|
|
|
let mut outlier = false;
|
|
|
|
if let Ok(event_id) = EventId::parse(args[0]) {
|
|
|
|
let mut pdu_json = db.rooms.get_non_outlier_pdu_json(&event_id)?;
|
|
|
|
let mut outlier = false;
|
|
|
|
if pdu_json.is_none() {
|
|
|
|
let mut pdu_json = db.rooms.get_non_outlier_pdu_json(&event_id)?;
|
|
|
|
outlier = true;
|
|
|
|
if pdu_json.is_none() {
|
|
|
|
pdu_json = db.rooms.get_pdu_json(&event_id)?;
|
|
|
|
outlier = true;
|
|
|
|
}
|
|
|
|
pdu_json = db.rooms.get_pdu_json(&event_id)?;
|
|
|
|
match pdu_json {
|
|
|
|
}
|
|
|
|
Some(json) => {
|
|
|
|
match pdu_json {
|
|
|
|
let json_text =
|
|
|
|
Some(json) => {
|
|
|
|
serde_json::to_string_pretty(&json).expect("canonical json is valid json");
|
|
|
|
let json_text = serde_json::to_string_pretty(&json)
|
|
|
|
AdminCommand::SendMessage(RoomMessageEventContent::text_html(
|
|
|
|
.expect("canonical json is valid json");
|
|
|
|
format!(
|
|
|
|
AdminCommand::SendMessage(
|
|
|
|
"{}\n```json\n{}\n```",
|
|
|
|
RoomMessageEventContent::text_html(
|
|
|
|
if outlier {
|
|
|
|
format!("{}\n```json\n{}\n```",
|
|
|
|
"PDU is outlier"
|
|
|
|
if outlier {
|
|
|
|
} else {
|
|
|
|
"PDU is outlier"
|
|
|
|
"PDU was accepted"
|
|
|
|
} else { "PDU was accepted"}, json_text),
|
|
|
|
},
|
|
|
|
format!("<p>{}</p>\n<pre><code class=\"language-json\">{}\n</code></pre>\n",
|
|
|
|
json_text
|
|
|
|
if outlier {
|
|
|
|
),
|
|
|
|
"PDU is outlier"
|
|
|
|
format!(
|
|
|
|
} else { "PDU was accepted"}, RawStr::new(&json_text).html_escape())
|
|
|
|
"<p>{}</p>\n<pre><code class=\"language-json\">{}\n</code></pre>\n",
|
|
|
|
),
|
|
|
|
if outlier {
|
|
|
|
)
|
|
|
|
"PDU is outlier"
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
None => AdminCommand::SendMessage(RoomMessageEventContent::text_plain(
|
|
|
|
"PDU was accepted"
|
|
|
|
"PDU not found.",
|
|
|
|
},
|
|
|
|
)),
|
|
|
|
RawStr::new(&json_text).html_escape()
|
|
|
|
}
|
|
|
|
),
|
|
|
|
} else {
|
|
|
|
|
|
|
|
AdminCommand::SendMessage(RoomMessageEventContent::text_plain(
|
|
|
|
|
|
|
|
"Event ID could not be parsed.",
|
|
|
|
|
|
|
|
))
|
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
None => {
|
|
|
|
AdminCommand::SendMessage(RoomMessageEventContent::text_plain(
|
|
|
|
AdminCommand::SendMessage(RoomMessageEventContent::text_plain("PDU not found."))
|
|
|
|
"Usage: get_pdu <eventid>",
|
|
|
|
}
|
|
|
|
))
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"database_memory_usage" => AdminCommand::ShowMemoryUsage,
|
|
|
|
AdminCommands::DatabaseMemoryUsage => AdminCommand::ShowMemoryUsage,
|
|
|
|
_ => {
|
|
|
|
|
|
|
|
let message = format!(
|
|
|
|
|
|
|
|
"Unrecognized command <code>{}</code>, try <code>help</code> for a list of commands.",
|
|
|
|
|
|
|
|
command,
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
AdminCommand::SendMessage(RoomMessageEventContent::text_html(
|
|
|
|
|
|
|
|
html_to_markdown(&message),
|
|
|
|
|
|
|
|
message,
|
|
|
|
|
|
|
|
))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Ok(command)
|
|
|
|
Ok(admin_command)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn usage_to_markdown(text: &str) -> String {
|
|
|
|
|
|
|
|
// For the conduit admin room, subcommands become main commands
|
|
|
|
|
|
|
|
let text = text.replace("SUBCOMMAND", "COMMAND");
|
|
|
|
|
|
|
|
let text = text.replace("subcommand", "command");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Put the first line (command name and version text) on its own paragraph
|
|
|
|
|
|
|
|
let re = Regex::new("^(.*?)\n").expect("Regex compilation should not fail");
|
|
|
|
|
|
|
|
let text = re.replace_all(&text, "*$1*\n\n");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Wrap command names in backticks
|
|
|
|
|
|
|
|
// (?m) enables multi-line mode for ^ and $
|
|
|
|
|
|
|
|
let re = Regex::new("(?m)^ ([a-z-]+) +(.*)$").expect("Regex compilation should not fail");
|
|
|
|
|
|
|
|
let text = re.replace_all(&text, " `$1`: $2");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Add * to list items
|
|
|
|
|
|
|
|
let re = Regex::new("(?m)^ (.*)$").expect("Regex compilation should not fail");
|
|
|
|
|
|
|
|
let text = re.replace_all(&text, "* $1");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Turn section names to headings
|
|
|
|
|
|
|
|
let re = Regex::new("(?m)^([A-Z-]+):$").expect("Regex compilation should not fail");
|
|
|
|
|
|
|
|
let text = re.replace_all(&text, "#### $1");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
text.to_string()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn markdown_to_html(text: &str) -> String {
|
|
|
|
|
|
|
|
// CommonMark's spec allows HTML tags; however, CLI required arguments look
|
|
|
|
|
|
|
|
// very much like tags so escape them.
|
|
|
|
|
|
|
|
let text = text.replace("<", "<").replace(">", ">");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let mut html_output = String::new();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let parser = pulldown_cmark::Parser::new(&text);
|
|
|
|
|
|
|
|
pulldown_cmark::html::push_html(&mut html_output, parser);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
html_output
|
|
|
|
}
|
|
|
|
}
|
|
|
|