2020-05-03 15:25:31 +00:00
|
|
|
#![warn(rust_2018_idioms)]
|
2021-03-04 14:26:34 +00:00
|
|
|
#![allow(clippy::suspicious_else_formatting)]
|
2021-05-05 16:14:49 +00:00
|
|
|
#![deny(clippy::dbg_macro)]
|
2020-04-09 18:47:03 +00:00
|
|
|
|
2020-12-08 09:33:44 +00:00
|
|
|
pub mod appservice_server;
|
2020-08-14 09:31:31 +00:00
|
|
|
pub mod client_server;
|
2020-08-14 09:34:15 +00:00
|
|
|
pub mod server_server;
|
2020-06-12 11:18:25 +00:00
|
|
|
|
2020-03-30 11:46:18 +00:00
|
|
|
mod database;
|
2020-05-03 15:25:31 +00:00
|
|
|
mod error;
|
2020-04-04 09:53:37 +00:00
|
|
|
mod pdu;
|
2020-02-15 21:42:21 +00:00
|
|
|
mod ruma_wrapper;
|
2020-03-29 11:48:44 +00:00
|
|
|
mod utils;
|
2020-02-15 21:42:21 +00:00
|
|
|
|
2021-06-08 16:10:00 +00:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
2021-02-28 11:41:03 +00:00
|
|
|
use database::Config;
|
2020-03-30 11:46:18 +00:00
|
|
|
pub use database::Database;
|
2021-02-28 11:41:03 +00:00
|
|
|
pub use error::{Error, Result};
|
2021-08-19 09:01:18 +00:00
|
|
|
use opentelemetry::trace::{FutureExt, Tracer};
|
2020-04-04 09:53:37 +00:00
|
|
|
pub use pdu::PduEvent;
|
2020-07-26 03:08:00 +00:00
|
|
|
pub use rocket::State;
|
2021-02-06 14:27:43 +00:00
|
|
|
use ruma::api::client::error::ErrorKind;
|
2020-06-09 13:13:17 +00:00
|
|
|
pub use ruma_wrapper::{ConduitResult, Ruma, RumaResponse};
|
2020-03-30 11:46:18 +00:00
|
|
|
|
2021-01-27 02:53:03 +00:00
|
|
|
use rocket::{
|
|
|
|
catch, catchers,
|
|
|
|
figment::{
|
|
|
|
providers::{Env, Format, Toml},
|
|
|
|
Figment,
|
|
|
|
},
|
|
|
|
routes, Request,
|
2020-12-31 20:07:05 +00:00
|
|
|
};
|
2021-07-14 07:07:08 +00:00
|
|
|
use tokio::sync::RwLock;
|
2021-07-29 06:36:01 +00:00
|
|
|
use tracing_subscriber::{prelude::*, EnvFilter};
|
2020-04-03 15:27:08 +00:00
|
|
|
|
2021-07-14 07:07:08 +00:00
|
|
|
fn setup_rocket(config: Figment, data: Arc<RwLock<Database>>) -> rocket::Rocket<rocket::Build> {
|
2021-04-26 16:01:41 +00:00
|
|
|
rocket::custom(config)
|
2021-04-23 16:54:17 +00:00
|
|
|
.manage(data)
|
2020-02-18 21:07:57 +00:00
|
|
|
.mount(
|
|
|
|
"/",
|
|
|
|
routes![
|
2020-04-11 07:30:11 +00:00
|
|
|
client_server::get_supported_versions_route,
|
2020-05-01 21:17:25 +00:00
|
|
|
client_server::get_register_available_route,
|
2020-04-11 07:30:11 +00:00
|
|
|
client_server::register_route,
|
2020-07-31 12:40:28 +00:00
|
|
|
client_server::get_login_types_route,
|
2020-04-11 07:30:11 +00:00
|
|
|
client_server::login_route,
|
2020-07-27 15:38:00 +00:00
|
|
|
client_server::whoami_route,
|
2020-05-24 20:10:09 +00:00
|
|
|
client_server::logout_route,
|
2020-07-05 06:35:19 +00:00
|
|
|
client_server::logout_all_route,
|
2020-07-02 18:38:25 +00:00
|
|
|
client_server::change_password_route,
|
2020-07-05 05:48:19 +00:00
|
|
|
client_server::deactivate_route,
|
2021-08-09 19:29:35 +00:00
|
|
|
client_server::third_party_route,
|
2020-04-19 12:14:47 +00:00
|
|
|
client_server::get_capabilities_route,
|
2020-04-11 07:30:11 +00:00
|
|
|
client_server::get_pushrules_all_route,
|
2020-05-01 18:26:57 +00:00
|
|
|
client_server::set_pushrule_route,
|
2021-01-24 15:05:52 +00:00
|
|
|
client_server::get_pushrule_route,
|
2020-05-01 18:26:57 +00:00
|
|
|
client_server::set_pushrule_enabled_route,
|
2021-01-24 15:05:52 +00:00
|
|
|
client_server::get_pushrule_enabled_route,
|
|
|
|
client_server::get_pushrule_actions_route,
|
|
|
|
client_server::set_pushrule_actions_route,
|
|
|
|
client_server::delete_pushrule_route,
|
2020-07-11 12:08:37 +00:00
|
|
|
client_server::get_room_event_route,
|
2021-07-11 09:28:55 +00:00
|
|
|
client_server::get_room_aliases_route,
|
2020-04-11 07:30:11 +00:00
|
|
|
client_server::get_filter_route,
|
|
|
|
client_server::create_filter_route,
|
|
|
|
client_server::set_global_account_data_route,
|
2021-03-24 07:48:28 +00:00
|
|
|
client_server::set_room_account_data_route,
|
2020-04-11 07:30:11 +00:00
|
|
|
client_server::get_global_account_data_route,
|
2021-03-24 07:48:28 +00:00
|
|
|
client_server::get_room_account_data_route,
|
2020-04-11 07:30:11 +00:00
|
|
|
client_server::set_displayname_route,
|
|
|
|
client_server::get_displayname_route,
|
|
|
|
client_server::set_avatar_url_route,
|
|
|
|
client_server::get_avatar_url_route,
|
|
|
|
client_server::get_profile_route,
|
|
|
|
client_server::set_presence_route,
|
2021-05-14 09:03:18 +00:00
|
|
|
client_server::get_presence_route,
|
2020-04-11 07:30:11 +00:00
|
|
|
client_server::upload_keys_route,
|
2020-05-17 17:56:40 +00:00
|
|
|
client_server::get_keys_route,
|
|
|
|
client_server::claim_keys_route,
|
2020-06-16 10:11:38 +00:00
|
|
|
client_server::create_backup_route,
|
|
|
|
client_server::update_backup_route,
|
2020-08-27 12:48:20 +00:00
|
|
|
client_server::delete_backup_route,
|
2020-06-16 10:11:38 +00:00
|
|
|
client_server::get_latest_backup_route,
|
|
|
|
client_server::get_backup_route,
|
2020-08-27 12:48:20 +00:00
|
|
|
client_server::add_backup_key_sessions_route,
|
2020-06-16 10:11:38 +00:00
|
|
|
client_server::add_backup_keys_route,
|
2020-08-27 12:48:20 +00:00
|
|
|
client_server::delete_backup_key_session_route,
|
|
|
|
client_server::delete_backup_key_sessions_route,
|
|
|
|
client_server::delete_backup_keys_route,
|
|
|
|
client_server::get_backup_key_session_route,
|
|
|
|
client_server::get_backup_key_sessions_route,
|
2020-06-16 10:11:38 +00:00
|
|
|
client_server::get_backup_keys_route,
|
2020-04-11 07:30:11 +00:00
|
|
|
client_server::set_read_marker_route,
|
2021-03-02 13:32:30 +00:00
|
|
|
client_server::create_receipt_route,
|
2020-04-12 19:12:50 +00:00
|
|
|
client_server::create_typing_event_route,
|
2020-04-11 07:30:11 +00:00
|
|
|
client_server::create_room_route,
|
2020-05-25 21:24:13 +00:00
|
|
|
client_server::redact_event_route,
|
|
|
|
client_server::create_alias_route,
|
|
|
|
client_server::delete_alias_route,
|
2020-04-11 07:30:11 +00:00
|
|
|
client_server::get_alias_route,
|
|
|
|
client_server::join_room_by_id_route,
|
|
|
|
client_server::join_room_by_id_or_alias_route,
|
2020-07-10 06:49:05 +00:00
|
|
|
client_server::joined_members_route,
|
2020-04-19 12:14:47 +00:00
|
|
|
client_server::leave_room_route,
|
2020-04-28 17:56:34 +00:00
|
|
|
client_server::forget_room_route,
|
2020-07-25 16:35:22 +00:00
|
|
|
client_server::joined_rooms_route,
|
2020-06-04 13:02:27 +00:00
|
|
|
client_server::kick_user_route,
|
|
|
|
client_server::ban_user_route,
|
|
|
|
client_server::unban_user_route,
|
2020-04-14 11:54:32 +00:00
|
|
|
client_server::invite_user_route,
|
2020-05-25 21:24:13 +00:00
|
|
|
client_server::set_room_visibility_route,
|
|
|
|
client_server::get_room_visibility_route,
|
2020-05-19 14:28:03 +00:00
|
|
|
client_server::get_public_rooms_route,
|
2020-04-11 07:30:11 +00:00
|
|
|
client_server::get_public_rooms_filtered_route,
|
2020-04-14 11:54:32 +00:00
|
|
|
client_server::search_users_route,
|
2020-04-19 12:14:47 +00:00
|
|
|
client_server::get_member_events_route,
|
2020-04-11 07:30:11 +00:00
|
|
|
client_server::get_protocols_route,
|
2020-08-12 21:32:39 +00:00
|
|
|
client_server::send_message_event_route,
|
|
|
|
client_server::send_state_event_for_key_route,
|
|
|
|
client_server::send_state_event_for_empty_key_route,
|
2020-05-18 07:22:07 +00:00
|
|
|
client_server::get_state_events_route,
|
|
|
|
client_server::get_state_events_for_key_route,
|
|
|
|
client_server::get_state_events_for_empty_key_route,
|
2020-07-27 15:36:54 +00:00
|
|
|
client_server::sync_events_route,
|
2020-06-04 11:58:55 +00:00
|
|
|
client_server::get_context_route,
|
2020-04-28 17:56:34 +00:00
|
|
|
client_server::get_message_events_route,
|
2020-08-18 10:15:27 +00:00
|
|
|
client_server::search_events_route,
|
2020-04-14 11:54:32 +00:00
|
|
|
client_server::turn_server_route,
|
2020-05-01 18:26:57 +00:00
|
|
|
client_server::send_event_to_device_route,
|
|
|
|
client_server::get_media_config_route,
|
2020-05-18 15:53:34 +00:00
|
|
|
client_server::create_content_route,
|
|
|
|
client_server::get_content_route,
|
|
|
|
client_server::get_content_thumbnail_route,
|
2020-06-03 11:41:30 +00:00
|
|
|
client_server::get_devices_route,
|
|
|
|
client_server::get_device_route,
|
|
|
|
client_server::update_device_route,
|
|
|
|
client_server::delete_device_route,
|
|
|
|
client_server::delete_devices_route,
|
2020-07-26 20:33:20 +00:00
|
|
|
client_server::get_tags_route,
|
|
|
|
client_server::update_tag_route,
|
|
|
|
client_server::delete_tag_route,
|
2020-04-11 07:30:11 +00:00
|
|
|
client_server::options_route,
|
2020-06-16 10:11:38 +00:00
|
|
|
client_server::upload_signing_keys_route,
|
|
|
|
client_server::upload_signatures_route,
|
2020-07-29 15:37:26 +00:00
|
|
|
client_server::get_key_changes_route,
|
2020-07-30 16:14:47 +00:00
|
|
|
client_server::get_pushers_route,
|
2020-06-16 10:11:38 +00:00
|
|
|
client_server::set_pushers_route,
|
2021-01-27 02:53:03 +00:00
|
|
|
// client_server::third_party_route,
|
2020-08-06 11:21:53 +00:00
|
|
|
client_server::upgrade_room_route,
|
2020-12-05 20:03:43 +00:00
|
|
|
server_server::get_server_version_route,
|
|
|
|
server_server::get_server_keys_route,
|
|
|
|
server_server::get_server_keys_deprecated_route,
|
2020-08-14 09:29:32 +00:00
|
|
|
server_server::get_public_rooms_route,
|
2020-09-17 12:44:47 +00:00
|
|
|
server_server::get_public_rooms_filtered_route,
|
2020-08-14 09:29:32 +00:00
|
|
|
server_server::send_transaction_message_route,
|
2021-04-07 13:56:57 +00:00
|
|
|
server_server::get_event_route,
|
2020-09-25 10:26:29 +00:00
|
|
|
server_server::get_missing_events_route,
|
2021-06-14 09:36:18 +00:00
|
|
|
server_server::get_event_authorization_route,
|
2021-06-14 08:52:27 +00:00
|
|
|
server_server::get_room_state_route,
|
2021-06-14 09:36:18 +00:00
|
|
|
server_server::get_room_state_ids_route,
|
2021-04-16 16:18:29 +00:00
|
|
|
server_server::create_join_event_template_route,
|
2021-07-25 17:28:54 +00:00
|
|
|
server_server::create_join_event_v1_route,
|
|
|
|
server_server::create_join_event_v2_route,
|
2021-04-11 19:01:27 +00:00
|
|
|
server_server::create_invite_route,
|
2021-04-21 08:51:34 +00:00
|
|
|
server_server::get_devices_route,
|
2021-04-16 16:18:29 +00:00
|
|
|
server_server::get_room_information_route,
|
2020-10-05 20:19:22 +00:00
|
|
|
server_server::get_profile_information_route,
|
2021-05-20 21:46:52 +00:00
|
|
|
server_server::get_keys_route,
|
2021-05-28 11:44:40 +00:00
|
|
|
server_server::claim_keys_route,
|
2020-02-18 21:07:57 +00:00
|
|
|
],
|
|
|
|
)
|
2021-04-23 16:54:17 +00:00
|
|
|
.register(
|
|
|
|
"/",
|
|
|
|
catchers![
|
|
|
|
not_found_catcher,
|
|
|
|
forbidden_catcher,
|
|
|
|
unknown_token_catcher,
|
|
|
|
missing_token_catcher,
|
|
|
|
bad_json_catcher
|
|
|
|
],
|
2021-04-26 16:01:41 +00:00
|
|
|
)
|
2020-04-10 11:36:57 +00:00
|
|
|
}
|
|
|
|
|
2020-06-16 11:53:15 +00:00
|
|
|
#[rocket::main]
|
|
|
|
async fn main() {
|
2021-04-24 11:34:35 +00:00
|
|
|
// Force log level off, so we can use our own logger
|
|
|
|
std::env::set_var("CONDUIT_LOG_LEVEL", "off");
|
|
|
|
|
|
|
|
let raw_config =
|
2021-07-19 09:55:39 +00:00
|
|
|
Figment::from(default_config())
|
2021-04-24 11:34:35 +00:00
|
|
|
.merge(
|
|
|
|
Toml::file(Env::var("CONDUIT_CONFIG").expect(
|
|
|
|
"The CONDUIT_CONFIG env var needs to be set. Example: /etc/conduit.toml",
|
|
|
|
))
|
|
|
|
.nested(),
|
|
|
|
)
|
|
|
|
.merge(Env::prefixed("CONDUIT_").global());
|
|
|
|
|
2021-07-14 07:07:08 +00:00
|
|
|
std::env::set_var("RUST_LOG", "warn");
|
|
|
|
|
2021-09-07 18:41:14 +00:00
|
|
|
let config = match raw_config.extract::<Config>() {
|
|
|
|
Ok(s) => s,
|
|
|
|
Err(e) => {
|
|
|
|
eprintln!("It looks like your config is invalid. The following error occured while parsing it: {}", e);
|
|
|
|
std::process::exit(1);
|
|
|
|
}
|
|
|
|
};
|
2021-04-24 11:34:35 +00:00
|
|
|
|
2021-07-29 06:36:01 +00:00
|
|
|
let start = async {
|
|
|
|
config.warn_deprecated();
|
|
|
|
|
2021-09-07 18:41:14 +00:00
|
|
|
let db = match Database::load_or_create(&config).await {
|
|
|
|
Ok(db) => db,
|
|
|
|
Err(e) => {
|
|
|
|
eprintln!(
|
|
|
|
"The database couldn't be loaded or created. The following error occured: {}",
|
|
|
|
e
|
|
|
|
);
|
|
|
|
std::process::exit(1);
|
|
|
|
}
|
|
|
|
};
|
2021-07-29 06:36:01 +00:00
|
|
|
|
|
|
|
let rocket = setup_rocket(raw_config, Arc::clone(&db))
|
|
|
|
.ignite()
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
Database::start_on_shutdown_tasks(db, rocket.shutdown()).await;
|
|
|
|
|
|
|
|
rocket.launch().await.unwrap();
|
|
|
|
};
|
2021-04-24 11:34:35 +00:00
|
|
|
|
2021-02-28 11:41:03 +00:00
|
|
|
if config.allow_jaeger {
|
2021-08-19 09:01:18 +00:00
|
|
|
opentelemetry::global::set_text_map_propagator(opentelemetry_jaeger::Propagator::new());
|
2021-07-29 06:36:01 +00:00
|
|
|
let tracer = opentelemetry_jaeger::new_pipeline()
|
2021-08-19 09:01:18 +00:00
|
|
|
.install_batch(opentelemetry::runtime::Tokio)
|
2021-02-28 11:41:03 +00:00
|
|
|
.unwrap();
|
|
|
|
|
2021-07-29 06:36:01 +00:00
|
|
|
let span = tracer.start("conduit");
|
2021-08-19 09:01:18 +00:00
|
|
|
start.with_current_context().await;
|
2021-07-29 06:36:01 +00:00
|
|
|
drop(span);
|
2021-08-19 09:01:18 +00:00
|
|
|
|
|
|
|
println!("exporting");
|
|
|
|
opentelemetry::global::shutdown_tracer_provider();
|
2021-02-28 11:41:03 +00:00
|
|
|
} else {
|
2021-07-14 07:07:08 +00:00
|
|
|
std::env::set_var("RUST_LOG", &config.log);
|
|
|
|
|
2021-07-29 06:36:01 +00:00
|
|
|
let registry = tracing_subscriber::Registry::default();
|
|
|
|
if config.tracing_flame {
|
|
|
|
let (flame_layer, _guard) =
|
|
|
|
tracing_flame::FlameLayer::with_file("./tracing.folded").unwrap();
|
|
|
|
let flame_layer = flame_layer.with_empty_samples(false);
|
2021-07-14 07:07:08 +00:00
|
|
|
|
2021-07-29 06:36:01 +00:00
|
|
|
let filter_layer = EnvFilter::new("trace,h2=off");
|
2021-07-14 07:07:08 +00:00
|
|
|
|
2021-07-29 06:36:01 +00:00
|
|
|
let subscriber = registry.with(filter_layer).with(flame_layer);
|
|
|
|
tracing::subscriber::set_global_default(subscriber).unwrap();
|
|
|
|
start.await;
|
|
|
|
} else {
|
|
|
|
let fmt_layer = tracing_subscriber::fmt::Layer::new();
|
|
|
|
let filter_layer = EnvFilter::try_from_default_env()
|
|
|
|
.or_else(|_| EnvFilter::try_new("info"))
|
|
|
|
.unwrap();
|
2021-07-14 12:50:07 +00:00
|
|
|
|
2021-07-29 06:36:01 +00:00
|
|
|
let subscriber = registry.with(filter_layer).with(fmt_layer);
|
|
|
|
tracing::subscriber::set_global_default(subscriber).unwrap();
|
|
|
|
start.await;
|
|
|
|
}
|
|
|
|
}
|
2020-02-15 21:42:21 +00:00
|
|
|
}
|
2020-12-19 15:00:11 +00:00
|
|
|
|
|
|
|
#[catch(404)]
|
2020-12-22 17:45:35 +00:00
|
|
|
fn not_found_catcher(_: &Request<'_>) -> String {
|
2020-12-19 15:00:11 +00:00
|
|
|
"404 Not Found".to_owned()
|
|
|
|
}
|
2021-02-06 14:27:43 +00:00
|
|
|
|
|
|
|
#[catch(580)]
|
|
|
|
fn forbidden_catcher() -> Result<()> {
|
|
|
|
Err(Error::BadRequest(ErrorKind::Forbidden, "Forbidden."))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[catch(581)]
|
|
|
|
fn unknown_token_catcher() -> Result<()> {
|
|
|
|
Err(Error::BadRequest(
|
|
|
|
ErrorKind::UnknownToken { soft_logout: false },
|
|
|
|
"Unknown token.",
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[catch(582)]
|
|
|
|
fn missing_token_catcher() -> Result<()> {
|
|
|
|
Err(Error::BadRequest(ErrorKind::MissingToken, "Missing token."))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[catch(583)]
|
|
|
|
fn bad_json_catcher() -> Result<()> {
|
|
|
|
Err(Error::BadRequest(ErrorKind::BadJson, "Bad json."))
|
|
|
|
}
|
2021-07-19 09:55:39 +00:00
|
|
|
|
|
|
|
fn default_config() -> rocket::Config {
|
|
|
|
let mut config = rocket::Config::release_default();
|
|
|
|
|
|
|
|
{
|
|
|
|
let mut shutdown = &mut config.shutdown;
|
|
|
|
|
|
|
|
#[cfg(unix)]
|
|
|
|
{
|
|
|
|
use rocket::config::Sig;
|
|
|
|
|
2021-07-19 11:44:53 +00:00
|
|
|
shutdown.signals.insert(Sig::Term);
|
|
|
|
shutdown.signals.insert(Sig::Int);
|
2021-07-19 09:55:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Once shutdown is triggered, this is the amount of seconds before rocket
|
|
|
|
// will forcefully start shutting down connections, this gives enough time to /sync
|
|
|
|
// requests and the like (which havent gotten the memo, somehow) to still complete gracefully.
|
|
|
|
shutdown.grace = 35;
|
|
|
|
|
|
|
|
// After the grace period, rocket starts shutting down connections, and waits at least this
|
|
|
|
// many seconds before forcefully shutting all of them down.
|
|
|
|
shutdown.mercy = 10;
|
|
|
|
}
|
|
|
|
|
|
|
|
config
|
|
|
|
}
|