2020-11-15 11:17:21 +00:00
|
|
|
use std::{collections::HashMap, sync::RwLock, time::Duration, time::Instant};
|
2020-11-14 22:13:06 +00:00
|
|
|
|
2020-06-11 08:03:08 +00:00
|
|
|
use log::error;
|
2020-11-14 22:13:06 +00:00
|
|
|
use ruma::{
|
|
|
|
api::client::{error::ErrorKind, r0::uiaa::UiaaInfo},
|
|
|
|
events::room::message,
|
|
|
|
};
|
2020-05-03 15:25:31 +00:00
|
|
|
use thiserror::Error;
|
|
|
|
|
2020-11-14 22:13:06 +00:00
|
|
|
use crate::{database::admin::AdminCommand, Database};
|
|
|
|
|
2020-07-26 03:08:00 +00:00
|
|
|
#[cfg(feature = "conduit_bin")]
|
|
|
|
use {
|
|
|
|
crate::RumaResponse,
|
|
|
|
http::StatusCode,
|
|
|
|
rocket::{
|
|
|
|
response::{self, Responder},
|
|
|
|
Request,
|
|
|
|
},
|
|
|
|
ruma::api::client::{error::Error as RumaError, r0::uiaa::UiaaResponse},
|
|
|
|
};
|
|
|
|
|
2020-05-03 15:25:31 +00:00
|
|
|
pub type Result<T> = std::result::Result<T, Error>;
|
|
|
|
|
|
|
|
#[derive(Error, Debug)]
|
|
|
|
pub enum Error {
|
2020-06-09 13:13:17 +00:00
|
|
|
#[error("There was a problem with the connection to the database.")]
|
2020-05-03 15:25:31 +00:00
|
|
|
SledError {
|
|
|
|
#[from]
|
|
|
|
source: sled::Error,
|
|
|
|
},
|
2020-06-09 13:13:17 +00:00
|
|
|
#[error("Could not generate an image.")]
|
2020-05-19 16:31:34 +00:00
|
|
|
ImageError {
|
|
|
|
#[from]
|
|
|
|
source: image::error::ImageError,
|
|
|
|
},
|
2020-12-23 14:53:41 +00:00
|
|
|
#[error("Could not connect to server: {source}")]
|
2020-08-14 09:31:31 +00:00
|
|
|
ReqwestError {
|
|
|
|
#[from]
|
|
|
|
source: reqwest::Error,
|
|
|
|
},
|
|
|
|
#[error("{0}")]
|
|
|
|
BadServerResponse(&'static str),
|
2020-06-09 13:13:17 +00:00
|
|
|
#[error("{0}")]
|
|
|
|
BadConfig(&'static str),
|
|
|
|
#[error("{0}")]
|
2020-06-11 08:03:08 +00:00
|
|
|
/// Don't create this directly. Use Error::bad_database instead.
|
2020-05-03 15:25:31 +00:00
|
|
|
BadDatabase(&'static str),
|
2020-06-09 13:13:17 +00:00
|
|
|
#[error("uiaa")]
|
|
|
|
Uiaa(UiaaInfo),
|
|
|
|
|
|
|
|
#[error("{0}: {1}")]
|
|
|
|
BadRequest(ErrorKind, &'static str),
|
|
|
|
#[error("{0}")]
|
|
|
|
Conflict(&'static str), // This is only needed for when a room alias already exists
|
|
|
|
}
|
|
|
|
|
2020-06-11 08:03:08 +00:00
|
|
|
impl Error {
|
|
|
|
pub fn bad_database(message: &'static str) -> Self {
|
|
|
|
error!("BadDatabase: {}", message);
|
|
|
|
Self::BadDatabase(message)
|
|
|
|
}
|
2020-11-14 22:13:06 +00:00
|
|
|
|
|
|
|
pub fn bad_config(message: &'static str) -> Self {
|
|
|
|
error!("BadConfig: {}", message);
|
|
|
|
Self::BadConfig(message)
|
|
|
|
}
|
2020-06-11 08:03:08 +00:00
|
|
|
}
|
|
|
|
|
2020-07-26 03:08:00 +00:00
|
|
|
#[cfg(feature = "conduit_bin")]
|
2020-07-14 16:23:26 +00:00
|
|
|
impl<'r, 'o> Responder<'r, 'o> for Error
|
|
|
|
where
|
|
|
|
'o: 'r,
|
|
|
|
{
|
|
|
|
fn respond_to(self, r: &'r Request<'_>) -> response::Result<'o> {
|
2020-06-09 13:13:17 +00:00
|
|
|
if let Self::Uiaa(uiaainfo) = &self {
|
2020-07-14 16:23:26 +00:00
|
|
|
return RumaResponse::from(UiaaResponse::AuthResponse(uiaainfo.clone())).respond_to(r);
|
2020-06-09 13:13:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let message = format!("{}", self);
|
|
|
|
|
|
|
|
use ErrorKind::*;
|
|
|
|
let (kind, status_code) = match self {
|
|
|
|
Self::BadRequest(kind, _) => (
|
2020-09-08 15:32:03 +00:00
|
|
|
kind.clone(),
|
2020-06-09 13:13:17 +00:00
|
|
|
match kind {
|
|
|
|
Forbidden | GuestAccessForbidden | ThreepidAuthFailed | ThreepidDenied => {
|
|
|
|
StatusCode::FORBIDDEN
|
|
|
|
}
|
2020-09-08 15:32:03 +00:00
|
|
|
Unauthorized | UnknownToken { .. } | MissingToken => StatusCode::UNAUTHORIZED,
|
2020-06-09 13:13:17 +00:00
|
|
|
NotFound => StatusCode::NOT_FOUND,
|
2020-09-08 15:32:03 +00:00
|
|
|
LimitExceeded { .. } => StatusCode::TOO_MANY_REQUESTS,
|
2020-06-09 13:13:17 +00:00
|
|
|
UserDeactivated => StatusCode::FORBIDDEN,
|
|
|
|
TooLarge => StatusCode::PAYLOAD_TOO_LARGE,
|
|
|
|
_ => StatusCode::BAD_REQUEST,
|
|
|
|
},
|
|
|
|
),
|
|
|
|
Self::Conflict(_) => (Unknown, StatusCode::CONFLICT),
|
|
|
|
_ => (Unknown, StatusCode::INTERNAL_SERVER_ERROR),
|
|
|
|
};
|
|
|
|
|
|
|
|
RumaResponse::from(RumaError {
|
|
|
|
kind,
|
|
|
|
message,
|
|
|
|
status_code,
|
|
|
|
})
|
|
|
|
.respond_to(r)
|
|
|
|
}
|
2020-05-03 15:25:31 +00:00
|
|
|
}
|
2020-11-14 22:13:06 +00:00
|
|
|
|
|
|
|
pub struct ConduitLogger {
|
|
|
|
pub db: Database,
|
|
|
|
pub last_logs: RwLock<HashMap<String, Instant>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl log::Log for ConduitLogger {
|
|
|
|
fn enabled(&self, _metadata: &log::Metadata<'_>) -> bool {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
|
|
|
fn log(&self, record: &log::Record<'_>) {
|
|
|
|
let output = format!("{} - {}", record.level(), record.args());
|
|
|
|
|
2020-12-19 15:00:11 +00:00
|
|
|
eprintln!("{}", output);
|
2020-11-14 22:13:06 +00:00
|
|
|
|
|
|
|
if self.enabled(record.metadata())
|
|
|
|
&& record
|
|
|
|
.module_path()
|
|
|
|
.map_or(false, |path| path.starts_with("conduit::"))
|
|
|
|
{
|
|
|
|
if self
|
|
|
|
.last_logs
|
|
|
|
.read()
|
|
|
|
.unwrap()
|
|
|
|
.get(&output)
|
|
|
|
.map_or(false, |i| i.elapsed() < Duration::from_secs(60 * 30))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Ok(mut_last_logs) = &mut self.last_logs.try_write() {
|
|
|
|
mut_last_logs.insert(output.clone(), Instant::now());
|
|
|
|
}
|
|
|
|
|
2020-12-08 09:33:44 +00:00
|
|
|
self.db.admin.send(AdminCommand::SendMessage(
|
|
|
|
message::MessageEventContent::text_plain(output),
|
2020-11-14 22:13:06 +00:00
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn flush(&self) {}
|
|
|
|
}
|