split main
Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
parent
7f6c19f066
commit
9cc4f3e929
7 changed files with 1031 additions and 966 deletions
168
src/config/check.rs
Normal file
168
src/config/check.rs
Normal file
|
@ -0,0 +1,168 @@
|
|||
// not unix specific, just only for UNIX sockets stuff and *nix container checks
|
||||
#[cfg(unix)]
|
||||
use std::path::Path; // not unix specific, just only for UNIX sockets stuff and *nix container checks
|
||||
|
||||
use tracing::{debug, error, info, warn};
|
||||
|
||||
use crate::{utils::error::Error, Config};
|
||||
|
||||
pub fn check(config: &Config) -> Result<(), Error> {
|
||||
config.warn_deprecated();
|
||||
config.warn_unknown_key();
|
||||
|
||||
if config.unix_socket_path.is_some() && !cfg!(unix) {
|
||||
return Err(Error::bad_config(
|
||||
"UNIX socket support is only available on *nix platforms. Please remove \"unix_socket_path\" from your \
|
||||
config.",
|
||||
));
|
||||
}
|
||||
|
||||
if config.address.is_loopback() && cfg!(unix) {
|
||||
debug!(
|
||||
"Found loopback listening address {}, running checks if we're in a container.",
|
||||
config.address
|
||||
);
|
||||
|
||||
#[cfg(unix)]
|
||||
if Path::new("/proc/vz").exists() /* Guest */ && !Path::new("/proc/bz").exists()
|
||||
/* Host */
|
||||
{
|
||||
error!(
|
||||
"You are detected using OpenVZ with a loopback/localhost listening address of {}. If you are using \
|
||||
OpenVZ for containers and you use NAT-based networking to communicate with the host and guest, this \
|
||||
will NOT work. Please change this to \"0.0.0.0\". If this is expected, you can ignore.",
|
||||
config.address
|
||||
);
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
if Path::new("/.dockerenv").exists() {
|
||||
error!(
|
||||
"You are detected using Docker with a loopback/localhost listening address of {}. If you are using a \
|
||||
reverse proxy on the host and require communication to conduwuit in the Docker container via \
|
||||
NAT-based networking, this will NOT work. Please change this to \"0.0.0.0\". If this is expected, \
|
||||
you can ignore.",
|
||||
config.address
|
||||
);
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
if Path::new("/run/.containerenv").exists() {
|
||||
error!(
|
||||
"You are detected using Podman with a loopback/localhost listening address of {}. If you are using a \
|
||||
reverse proxy on the host and require communication to conduwuit in the Podman container via \
|
||||
NAT-based networking, this will NOT work. Please change this to \"0.0.0.0\". If this is expected, \
|
||||
you can ignore.",
|
||||
config.address
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// rocksdb does not allow max_log_files to be 0
|
||||
if config.rocksdb_max_log_files == 0 && cfg!(feature = "rocksdb") {
|
||||
return Err(Error::bad_config(
|
||||
"When using RocksDB, rocksdb_max_log_files cannot be 0. Please set a value at least 1.",
|
||||
));
|
||||
}
|
||||
|
||||
// yeah, unless the user built a debug build hopefully for local testing only
|
||||
if config.server_name == "your.server.name" && !cfg!(debug_assertions) {
|
||||
return Err(Error::bad_config(
|
||||
"You must specify a valid server name for production usage of conduwuit.",
|
||||
));
|
||||
}
|
||||
|
||||
if cfg!(debug_assertions) {
|
||||
info!("Note: conduwuit was built without optimisations (i.e. debug build)");
|
||||
}
|
||||
|
||||
// check if the user specified a registration token as `""`
|
||||
if config.registration_token == Some(String::new()) {
|
||||
return Err(Error::bad_config("Registration token was specified but is empty (\"\")"));
|
||||
}
|
||||
|
||||
if config.max_request_size < 16384 {
|
||||
return Err(Error::bad_config("Max request size is less than 16KB. Please increase it."));
|
||||
}
|
||||
|
||||
// check if user specified valid IP CIDR ranges on startup
|
||||
for cidr in &config.ip_range_denylist {
|
||||
if let Err(e) = ipaddress::IPAddress::parse(cidr) {
|
||||
error!("Error parsing specified IP CIDR range from string: {e}");
|
||||
return Err(Error::bad_config("Error parsing specified IP CIDR ranges from strings"));
|
||||
}
|
||||
}
|
||||
|
||||
if config.allow_registration
|
||||
&& !config.yes_i_am_very_very_sure_i_want_an_open_registration_server_prone_to_abuse
|
||||
&& config.registration_token.is_none()
|
||||
{
|
||||
return Err(Error::bad_config(
|
||||
"!! You have `allow_registration` enabled without a token configured in your config which means you are \
|
||||
allowing ANYONE to register on your conduwuit instance without any 2nd-step (e.g. registration token).\n
|
||||
If this is not the intended behaviour, please set a registration token with the `registration_token` config option.\n
|
||||
For security and safety reasons, conduwuit will shut down. If you are extra sure this is the desired behaviour you \
|
||||
want, please set the following config option to true:
|
||||
`yes_i_am_very_very_sure_i_want_an_open_registration_server_prone_to_abuse`",
|
||||
));
|
||||
}
|
||||
|
||||
if config.allow_registration
|
||||
&& config.yes_i_am_very_very_sure_i_want_an_open_registration_server_prone_to_abuse
|
||||
&& config.registration_token.is_none()
|
||||
{
|
||||
warn!(
|
||||
"Open registration is enabled via setting \
|
||||
`yes_i_am_very_very_sure_i_want_an_open_registration_server_prone_to_abuse` and `allow_registration` to \
|
||||
true without a registration token configured. You are expected to be aware of the risks now.\n
|
||||
If this is not the desired behaviour, please set a registration token."
|
||||
);
|
||||
}
|
||||
|
||||
if config.allow_outgoing_presence && !config.allow_local_presence {
|
||||
return Err(Error::bad_config(
|
||||
"Outgoing presence requires allowing local presence. Please enable \"allow_local_presence\".",
|
||||
));
|
||||
}
|
||||
|
||||
if config.allow_outgoing_presence {
|
||||
warn!(
|
||||
"! Outgoing federated presence is not spec compliant due to relying on PDUs and EDUs combined.\nOutgoing \
|
||||
presence will not be very reliable due to this and any issues with federated outgoing presence are very \
|
||||
likely attributed to this issue.\nIncoming presence and local presence are unaffected."
|
||||
);
|
||||
}
|
||||
|
||||
if config
|
||||
.url_preview_domain_contains_allowlist
|
||||
.contains(&"*".to_owned())
|
||||
{
|
||||
warn!(
|
||||
"All URLs are allowed for URL previews via setting \"url_preview_domain_contains_allowlist\" to \"*\". \
|
||||
This opens up significant attack surface to your server. You are expected to be aware of the risks by \
|
||||
doing this."
|
||||
);
|
||||
}
|
||||
if config
|
||||
.url_preview_domain_explicit_allowlist
|
||||
.contains(&"*".to_owned())
|
||||
{
|
||||
warn!(
|
||||
"All URLs are allowed for URL previews via setting \"url_preview_domain_explicit_allowlist\" to \"*\". \
|
||||
This opens up significant attack surface to your server. You are expected to be aware of the risks by \
|
||||
doing this."
|
||||
);
|
||||
}
|
||||
if config
|
||||
.url_preview_url_contains_allowlist
|
||||
.contains(&"*".to_owned())
|
||||
{
|
||||
warn!(
|
||||
"All URLs are allowed for URL previews via setting \"url_preview_url_contains_allowlist\" to \"*\". This \
|
||||
opens up significant attack surface to your server. You are expected to be aware of the risks by doing \
|
||||
this."
|
||||
);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
|
@ -1,12 +1,18 @@
|
|||
use std::{
|
||||
collections::BTreeMap,
|
||||
fmt::{self, Write as _},
|
||||
net::{IpAddr, Ipv4Addr},
|
||||
net::{IpAddr, Ipv4Addr, SocketAddr},
|
||||
path::PathBuf,
|
||||
};
|
||||
|
||||
use either::Either;
|
||||
use figment::Figment;
|
||||
use either::{
|
||||
Either,
|
||||
Either::{Left, Right},
|
||||
};
|
||||
use figment::{
|
||||
providers::{Env, Format, Toml},
|
||||
Figment,
|
||||
};
|
||||
use itertools::Itertools;
|
||||
use regex::RegexSet;
|
||||
use ruma::{OwnedRoomId, OwnedServerName, RoomVersionId};
|
||||
|
@ -14,7 +20,9 @@ use serde::{de::IgnoredAny, Deserialize};
|
|||
use tracing::{debug, error, warn};
|
||||
|
||||
use self::proxy::ProxyConfig;
|
||||
use crate::utils::error::Error;
|
||||
|
||||
mod check;
|
||||
mod proxy;
|
||||
|
||||
#[derive(Deserialize, Clone, Debug)]
|
||||
|
@ -299,6 +307,35 @@ pub struct TlsConfig {
|
|||
const DEPRECATED_KEYS: &[&str] = &["cache_capacity"];
|
||||
|
||||
impl Config {
|
||||
/// Initialize config
|
||||
pub fn new(path: Option<PathBuf>) -> Result<Self, Error> {
|
||||
let raw_config = if let Some(config_file_env) = Env::var("CONDUIT_CONFIG") {
|
||||
Figment::new()
|
||||
.merge(Toml::file(config_file_env).nested())
|
||||
.merge(Env::prefixed("CONDUIT_").global())
|
||||
} else if let Some(config_file_arg) = path {
|
||||
Figment::new()
|
||||
.merge(Toml::file(config_file_arg).nested())
|
||||
.merge(Env::prefixed("CONDUIT_").global())
|
||||
} else {
|
||||
Figment::new().merge(Env::prefixed("CONDUIT_").global())
|
||||
};
|
||||
|
||||
let config = match raw_config.extract::<Config>() {
|
||||
Err(e) => return Err(Error::BadConfig(format!("{e}"))),
|
||||
Ok(config) => config,
|
||||
};
|
||||
|
||||
check::check(&config)?;
|
||||
|
||||
// don't start if we're listening on both UNIX sockets and TCP at same time
|
||||
if config.is_dual_listening(&raw_config) {
|
||||
return Err(Error::bad_config("dual listening on UNIX and TCP sockets not allowed."));
|
||||
};
|
||||
|
||||
Ok(config)
|
||||
}
|
||||
|
||||
/// Iterates over all the keys in the config file and warns if there is a
|
||||
/// deprecated key specified
|
||||
pub fn warn_deprecated(&self) {
|
||||
|
@ -336,7 +373,7 @@ impl Config {
|
|||
|
||||
/// Checks the presence of the `address` and `unix_socket_path` keys in the
|
||||
/// raw_config, exiting the process if both keys were detected.
|
||||
pub fn is_dual_listening(&self, raw_config: &Figment) -> bool {
|
||||
fn is_dual_listening(&self, raw_config: &Figment) -> bool {
|
||||
let check_address = raw_config.find_value("address");
|
||||
let check_unix_socket = raw_config.find_value("unix_socket_path");
|
||||
|
||||
|
@ -349,6 +386,27 @@ impl Config {
|
|||
|
||||
false
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn get_bind_addrs(&self) -> Vec<SocketAddr> {
|
||||
match &self.port.ports {
|
||||
Left(port) => {
|
||||
// Left is only 1 value, so make a vec with 1 value only
|
||||
let port_vec = [port];
|
||||
|
||||
port_vec
|
||||
.iter()
|
||||
.copied()
|
||||
.map(|port| SocketAddr::from((self.address, *port)))
|
||||
.collect::<Vec<_>>()
|
||||
},
|
||||
Right(ports) => ports
|
||||
.iter()
|
||||
.copied()
|
||||
.map(|port| SocketAddr::from((self.address, port)))
|
||||
.collect::<Vec<_>>(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for Config {
|
||||
|
@ -628,7 +686,7 @@ fn default_address() -> IpAddr { Ipv4Addr::LOCALHOST.into() }
|
|||
|
||||
fn default_port() -> ListeningPort {
|
||||
ListeningPort {
|
||||
ports: Either::Left(8008),
|
||||
ports: Left(8008),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -238,7 +238,7 @@ impl KeyValueDatabase {
|
|||
debug!("Database path does not exist, assuming this is a new setup and creating it");
|
||||
fs::create_dir_all(&config.database_path).map_err(|e| {
|
||||
error!("Failed to create database path: {e}");
|
||||
Error::BadConfig(
|
||||
Error::bad_config(
|
||||
"Database folder doesn't exists and couldn't be created (e.g. due to missing permissions). Please \
|
||||
create the database folder yourself or allow conduwuit the permissions to create directories and \
|
||||
files.",
|
||||
|
@ -250,19 +250,19 @@ impl KeyValueDatabase {
|
|||
"sqlite" => {
|
||||
debug!("Got sqlite database backend");
|
||||
#[cfg(not(feature = "sqlite"))]
|
||||
return Err(Error::BadConfig("Database backend not found."));
|
||||
return Err(Error::bad_config("Database backend not found."));
|
||||
#[cfg(feature = "sqlite")]
|
||||
Arc::new(Arc::<abstraction::sqlite::Engine>::open(&config)?)
|
||||
},
|
||||
"rocksdb" => {
|
||||
debug!("Got rocksdb database backend");
|
||||
#[cfg(not(feature = "rocksdb"))]
|
||||
return Err(Error::BadConfig("Database backend not found."));
|
||||
return Err(Error::bad_config("Database backend not found."));
|
||||
#[cfg(feature = "rocksdb")]
|
||||
Arc::new(Arc::<abstraction::rocksdb::Engine>::open(&config)?)
|
||||
},
|
||||
_ => {
|
||||
return Err(Error::BadConfig(
|
||||
return Err(Error::bad_config(
|
||||
"Database backend not found. sqlite (not recommended) and rocksdb are the only supported backends.",
|
||||
));
|
||||
},
|
||||
|
|
1421
src/main.rs
1421
src/main.rs
File diff suppressed because it is too large
Load diff
312
src/routes.rs
Normal file
312
src/routes.rs
Normal file
|
@ -0,0 +1,312 @@
|
|||
use std::future::Future;
|
||||
|
||||
use axum::{
|
||||
extract::FromRequestParts,
|
||||
response::IntoResponse,
|
||||
routing::{get, on, post, MethodFilter},
|
||||
Router,
|
||||
};
|
||||
use conduit::{
|
||||
api::{client_server, server_server},
|
||||
*,
|
||||
};
|
||||
use http::{Method, Uri};
|
||||
use ruma::api::{client::error::ErrorKind, IncomingRequest};
|
||||
use tracing::{info, warn};
|
||||
|
||||
pub fn routes() -> Router {
|
||||
Router::new()
|
||||
.ruma_route(client_server::get_supported_versions_route)
|
||||
.ruma_route(client_server::get_register_available_route)
|
||||
.ruma_route(client_server::register_route)
|
||||
.ruma_route(client_server::get_login_types_route)
|
||||
.ruma_route(client_server::login_route)
|
||||
.ruma_route(client_server::whoami_route)
|
||||
.ruma_route(client_server::logout_route)
|
||||
.ruma_route(client_server::logout_all_route)
|
||||
.ruma_route(client_server::change_password_route)
|
||||
.ruma_route(client_server::deactivate_route)
|
||||
.ruma_route(client_server::third_party_route)
|
||||
.ruma_route(client_server::request_3pid_management_token_via_email_route)
|
||||
.ruma_route(client_server::request_3pid_management_token_via_msisdn_route)
|
||||
.ruma_route(client_server::get_capabilities_route)
|
||||
.ruma_route(client_server::get_pushrules_all_route)
|
||||
.ruma_route(client_server::set_pushrule_route)
|
||||
.ruma_route(client_server::get_pushrule_route)
|
||||
.ruma_route(client_server::set_pushrule_enabled_route)
|
||||
.ruma_route(client_server::get_pushrule_enabled_route)
|
||||
.ruma_route(client_server::get_pushrule_actions_route)
|
||||
.ruma_route(client_server::set_pushrule_actions_route)
|
||||
.ruma_route(client_server::delete_pushrule_route)
|
||||
.ruma_route(client_server::get_room_event_route)
|
||||
.ruma_route(client_server::get_room_aliases_route)
|
||||
.ruma_route(client_server::get_filter_route)
|
||||
.ruma_route(client_server::create_filter_route)
|
||||
.ruma_route(client_server::set_global_account_data_route)
|
||||
.ruma_route(client_server::set_room_account_data_route)
|
||||
.ruma_route(client_server::get_global_account_data_route)
|
||||
.ruma_route(client_server::get_room_account_data_route)
|
||||
.ruma_route(client_server::set_displayname_route)
|
||||
.ruma_route(client_server::get_displayname_route)
|
||||
.ruma_route(client_server::set_avatar_url_route)
|
||||
.ruma_route(client_server::get_avatar_url_route)
|
||||
.ruma_route(client_server::get_profile_route)
|
||||
.ruma_route(client_server::set_presence_route)
|
||||
.ruma_route(client_server::get_presence_route)
|
||||
.ruma_route(client_server::upload_keys_route)
|
||||
.ruma_route(client_server::get_keys_route)
|
||||
.ruma_route(client_server::claim_keys_route)
|
||||
.ruma_route(client_server::create_backup_version_route)
|
||||
.ruma_route(client_server::update_backup_version_route)
|
||||
.ruma_route(client_server::delete_backup_version_route)
|
||||
.ruma_route(client_server::get_latest_backup_info_route)
|
||||
.ruma_route(client_server::get_backup_info_route)
|
||||
.ruma_route(client_server::add_backup_keys_route)
|
||||
.ruma_route(client_server::add_backup_keys_for_room_route)
|
||||
.ruma_route(client_server::add_backup_keys_for_session_route)
|
||||
.ruma_route(client_server::delete_backup_keys_for_room_route)
|
||||
.ruma_route(client_server::delete_backup_keys_for_session_route)
|
||||
.ruma_route(client_server::delete_backup_keys_route)
|
||||
.ruma_route(client_server::get_backup_keys_for_room_route)
|
||||
.ruma_route(client_server::get_backup_keys_for_session_route)
|
||||
.ruma_route(client_server::get_backup_keys_route)
|
||||
.ruma_route(client_server::set_read_marker_route)
|
||||
.ruma_route(client_server::create_receipt_route)
|
||||
.ruma_route(client_server::create_typing_event_route)
|
||||
.ruma_route(client_server::create_room_route)
|
||||
.ruma_route(client_server::redact_event_route)
|
||||
.ruma_route(client_server::report_event_route)
|
||||
.ruma_route(client_server::create_alias_route)
|
||||
.ruma_route(client_server::delete_alias_route)
|
||||
.ruma_route(client_server::get_alias_route)
|
||||
.ruma_route(client_server::join_room_by_id_route)
|
||||
.ruma_route(client_server::join_room_by_id_or_alias_route)
|
||||
.ruma_route(client_server::joined_members_route)
|
||||
.ruma_route(client_server::leave_room_route)
|
||||
.ruma_route(client_server::forget_room_route)
|
||||
.ruma_route(client_server::joined_rooms_route)
|
||||
.ruma_route(client_server::kick_user_route)
|
||||
.ruma_route(client_server::ban_user_route)
|
||||
.ruma_route(client_server::unban_user_route)
|
||||
.ruma_route(client_server::invite_user_route)
|
||||
.ruma_route(client_server::set_room_visibility_route)
|
||||
.ruma_route(client_server::get_room_visibility_route)
|
||||
.ruma_route(client_server::get_public_rooms_route)
|
||||
.ruma_route(client_server::get_public_rooms_filtered_route)
|
||||
.ruma_route(client_server::search_users_route)
|
||||
.ruma_route(client_server::get_member_events_route)
|
||||
.ruma_route(client_server::get_protocols_route)
|
||||
.ruma_route(client_server::send_message_event_route)
|
||||
.ruma_route(client_server::send_state_event_for_key_route)
|
||||
.ruma_route(client_server::get_state_events_route)
|
||||
.ruma_route(client_server::get_state_events_for_key_route)
|
||||
// Ruma doesn't have support for multiple paths for a single endpoint yet, and these routes
|
||||
// share one Ruma request / response type pair with {get,send}_state_event_for_key_route
|
||||
.route(
|
||||
"/_matrix/client/r0/rooms/:room_id/state/:event_type",
|
||||
get(client_server::get_state_events_for_empty_key_route)
|
||||
.put(client_server::send_state_event_for_empty_key_route),
|
||||
)
|
||||
.route(
|
||||
"/_matrix/client/v3/rooms/:room_id/state/:event_type",
|
||||
get(client_server::get_state_events_for_empty_key_route)
|
||||
.put(client_server::send_state_event_for_empty_key_route),
|
||||
)
|
||||
// These two endpoints allow trailing slashes
|
||||
.route(
|
||||
"/_matrix/client/r0/rooms/:room_id/state/:event_type/",
|
||||
get(client_server::get_state_events_for_empty_key_route)
|
||||
.put(client_server::send_state_event_for_empty_key_route),
|
||||
)
|
||||
.route(
|
||||
"/_matrix/client/v3/rooms/:room_id/state/:event_type/",
|
||||
get(client_server::get_state_events_for_empty_key_route)
|
||||
.put(client_server::send_state_event_for_empty_key_route),
|
||||
)
|
||||
.ruma_route(client_server::sync_events_route)
|
||||
.ruma_route(client_server::sync_events_v4_route)
|
||||
.ruma_route(client_server::get_context_route)
|
||||
.ruma_route(client_server::get_message_events_route)
|
||||
.ruma_route(client_server::search_events_route)
|
||||
.ruma_route(client_server::turn_server_route)
|
||||
.ruma_route(client_server::send_event_to_device_route)
|
||||
.ruma_route(client_server::get_media_config_route)
|
||||
.ruma_route(client_server::get_media_preview_route)
|
||||
.ruma_route(client_server::create_content_route)
|
||||
// legacy v1 media routes
|
||||
.route(
|
||||
"/_matrix/media/v1/preview_url",
|
||||
get(client_server::get_media_preview_v1_route)
|
||||
)
|
||||
.route(
|
||||
"/_matrix/media/v1/config",
|
||||
get(client_server::get_media_config_v1_route)
|
||||
)
|
||||
.route(
|
||||
"/_matrix/media/v1/upload",
|
||||
post(client_server::create_content_v1_route)
|
||||
)
|
||||
.route(
|
||||
"/_matrix/media/v1/download/:server_name/:media_id",
|
||||
get(client_server::get_content_v1_route)
|
||||
)
|
||||
.route(
|
||||
"/_matrix/media/v1/download/:server_name/:media_id/:file_name",
|
||||
get(client_server::get_content_as_filename_v1_route)
|
||||
)
|
||||
.route(
|
||||
"/_matrix/media/v1/thumbnail/:server_name/:media_id",
|
||||
get(client_server::get_content_thumbnail_v1_route)
|
||||
)
|
||||
.ruma_route(client_server::get_content_route)
|
||||
.ruma_route(client_server::get_content_as_filename_route)
|
||||
.ruma_route(client_server::get_content_thumbnail_route)
|
||||
.ruma_route(client_server::get_devices_route)
|
||||
.ruma_route(client_server::get_device_route)
|
||||
.ruma_route(client_server::update_device_route)
|
||||
.ruma_route(client_server::delete_device_route)
|
||||
.ruma_route(client_server::delete_devices_route)
|
||||
.ruma_route(client_server::get_tags_route)
|
||||
.ruma_route(client_server::update_tag_route)
|
||||
.ruma_route(client_server::delete_tag_route)
|
||||
.ruma_route(client_server::upload_signing_keys_route)
|
||||
.ruma_route(client_server::upload_signatures_route)
|
||||
.ruma_route(client_server::get_key_changes_route)
|
||||
.ruma_route(client_server::get_pushers_route)
|
||||
.ruma_route(client_server::set_pushers_route)
|
||||
// .ruma_route(client_server::third_party_route)
|
||||
.ruma_route(client_server::upgrade_room_route)
|
||||
.ruma_route(client_server::get_threads_route)
|
||||
.ruma_route(client_server::get_relating_events_with_rel_type_and_event_type_route)
|
||||
.ruma_route(client_server::get_relating_events_with_rel_type_route)
|
||||
.ruma_route(client_server::get_relating_events_route)
|
||||
.ruma_route(client_server::get_hierarchy_route)
|
||||
.ruma_route(server_server::get_server_version_route)
|
||||
.route("/_matrix/key/v2/server", get(server_server::get_server_keys_route))
|
||||
.route(
|
||||
"/_matrix/key/v2/server/:key_id",
|
||||
get(server_server::get_server_keys_deprecated_route),
|
||||
)
|
||||
.ruma_route(server_server::get_public_rooms_route)
|
||||
.ruma_route(server_server::get_public_rooms_filtered_route)
|
||||
.ruma_route(server_server::send_transaction_message_route)
|
||||
.ruma_route(server_server::get_event_route)
|
||||
.ruma_route(server_server::get_backfill_route)
|
||||
.ruma_route(server_server::get_missing_events_route)
|
||||
.ruma_route(server_server::get_event_authorization_route)
|
||||
.ruma_route(server_server::get_room_state_route)
|
||||
.ruma_route(server_server::get_room_state_ids_route)
|
||||
.ruma_route(server_server::create_join_event_template_route)
|
||||
.ruma_route(server_server::create_join_event_v1_route)
|
||||
.ruma_route(server_server::create_join_event_v2_route)
|
||||
.ruma_route(server_server::create_invite_route)
|
||||
.ruma_route(server_server::get_devices_route)
|
||||
.ruma_route(server_server::get_room_information_route)
|
||||
.ruma_route(server_server::get_profile_information_route)
|
||||
.ruma_route(server_server::get_keys_route)
|
||||
.ruma_route(server_server::claim_keys_route)
|
||||
.ruma_route(server_server::get_hierarchy_route)
|
||||
.route("/_conduwuit/server_version", get(client_server::conduwuit_server_version))
|
||||
.route("/_matrix/client/r0/rooms/:room_id/initialSync", get(initial_sync))
|
||||
.route("/_matrix/client/v3/rooms/:room_id/initialSync", get(initial_sync))
|
||||
.route("/client/server.json", get(client_server::syncv3_client_server_json))
|
||||
.route("/.well-known/matrix/client", get(client_server::well_known_client_route))
|
||||
.route("/.well-known/matrix/server", get(server_server::well_known_server_route))
|
||||
.route("/", get(it_works))
|
||||
.fallback(not_found)
|
||||
}
|
||||
|
||||
async fn not_found(uri: Uri) -> impl IntoResponse {
|
||||
if uri.path().contains("_matrix/") {
|
||||
warn!("Not found: {uri}");
|
||||
} else {
|
||||
info!("Not found: {uri}");
|
||||
}
|
||||
|
||||
Error::BadRequest(ErrorKind::Unrecognized, "Unrecognized request")
|
||||
}
|
||||
|
||||
async fn initial_sync(_uri: Uri) -> impl IntoResponse {
|
||||
Error::BadRequest(ErrorKind::GuestAccessForbidden, "Guest access not implemented")
|
||||
}
|
||||
|
||||
async fn it_works() -> &'static str { "hewwo from conduwuit woof!" }
|
||||
|
||||
trait RouterExt {
|
||||
fn ruma_route<H, T>(self, handler: H) -> Self
|
||||
where
|
||||
H: RumaHandler<T>,
|
||||
T: 'static;
|
||||
}
|
||||
|
||||
impl RouterExt for Router {
|
||||
fn ruma_route<H, T>(self, handler: H) -> Self
|
||||
where
|
||||
H: RumaHandler<T>,
|
||||
T: 'static,
|
||||
{
|
||||
handler.add_to_router(self)
|
||||
}
|
||||
}
|
||||
|
||||
pub trait RumaHandler<T> {
|
||||
// Can't transform to a handler without boxing or relying on the nightly-only
|
||||
// impl-trait-in-traits feature. Moving a small amount of extra logic into the
|
||||
// trait allows bypassing both.
|
||||
fn add_to_router(self, router: Router) -> Router;
|
||||
}
|
||||
|
||||
macro_rules! impl_ruma_handler {
|
||||
( $($ty:ident),* $(,)? ) => {
|
||||
#[axum::async_trait]
|
||||
#[allow(non_snake_case)]
|
||||
impl<Req, E, F, Fut, $($ty,)*> RumaHandler<($($ty,)* Ruma<Req>,)> for F
|
||||
where
|
||||
Req: IncomingRequest + Send + 'static,
|
||||
F: FnOnce($($ty,)* Ruma<Req>) -> Fut + Clone + Send + 'static,
|
||||
Fut: Future<Output = Result<Req::OutgoingResponse, E>>
|
||||
+ Send,
|
||||
E: IntoResponse,
|
||||
$( $ty: FromRequestParts<()> + Send + 'static, )*
|
||||
{
|
||||
fn add_to_router(self, mut router: Router) -> Router {
|
||||
let meta = Req::METADATA;
|
||||
let method_filter = method_to_filter(meta.method);
|
||||
|
||||
for path in meta.history.all_paths() {
|
||||
let handler = self.clone();
|
||||
|
||||
router = router.route(path, on(method_filter, |$( $ty: $ty, )* req| async move {
|
||||
handler($($ty,)* req).await.map(RumaResponse)
|
||||
}))
|
||||
}
|
||||
|
||||
router
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
impl_ruma_handler!();
|
||||
impl_ruma_handler!(T1);
|
||||
impl_ruma_handler!(T1, T2);
|
||||
impl_ruma_handler!(T1, T2, T3);
|
||||
impl_ruma_handler!(T1, T2, T3, T4);
|
||||
impl_ruma_handler!(T1, T2, T3, T4, T5);
|
||||
impl_ruma_handler!(T1, T2, T3, T4, T5, T6);
|
||||
impl_ruma_handler!(T1, T2, T3, T4, T5, T6, T7);
|
||||
impl_ruma_handler!(T1, T2, T3, T4, T5, T6, T7, T8);
|
||||
|
||||
fn method_to_filter(method: Method) -> MethodFilter {
|
||||
match method {
|
||||
Method::DELETE => MethodFilter::DELETE,
|
||||
Method::GET => MethodFilter::GET,
|
||||
Method::HEAD => MethodFilter::HEAD,
|
||||
Method::OPTIONS => MethodFilter::OPTIONS,
|
||||
Method::PATCH => MethodFilter::PATCH,
|
||||
Method::POST => MethodFilter::POST,
|
||||
Method::PUT => MethodFilter::PUT,
|
||||
Method::TRACE => MethodFilter::TRACE,
|
||||
m => panic!("Unsupported HTTP method: {m:?}"),
|
||||
}
|
||||
}
|
|
@ -118,12 +118,12 @@ impl Client {
|
|||
#[cfg(not(feature = "gzip_compression"))]
|
||||
{
|
||||
builder = builder.no_gzip();
|
||||
}
|
||||
};
|
||||
|
||||
#[cfg(not(feature = "brotli_compression"))]
|
||||
{
|
||||
builder = builder.no_brotli();
|
||||
}
|
||||
};
|
||||
|
||||
if let Some(proxy) = config.proxy.to_proxy()? {
|
||||
Ok(builder.proxy(proxy))
|
||||
|
|
|
@ -19,7 +19,7 @@ use crate::RumaResponse;
|
|||
|
||||
pub type Result<T, E = Error> = std::result::Result<T, E>;
|
||||
|
||||
#[derive(Error, Debug)]
|
||||
#[derive(Error)]
|
||||
pub enum Error {
|
||||
#[cfg(feature = "sqlite")]
|
||||
#[error("There was a problem with the connection to the sqlite database: {source}")]
|
||||
|
@ -55,11 +55,11 @@ pub enum Error {
|
|||
#[from]
|
||||
source: std::io::Error,
|
||||
},
|
||||
#[error("There was a problem with your configuration file: {0}")]
|
||||
BadConfig(String),
|
||||
#[error("{0}")]
|
||||
BadServerResponse(&'static str),
|
||||
#[error("{0}")]
|
||||
BadConfig(&'static str),
|
||||
#[error("{0}")]
|
||||
/// Don't create this directly. Use Error::bad_database instead.
|
||||
BadDatabase(&'static str),
|
||||
#[error("uiaa")]
|
||||
|
@ -78,6 +78,8 @@ pub enum Error {
|
|||
InconsistentRoomState(&'static str, ruma::OwnedRoomId),
|
||||
#[error("{0}")]
|
||||
AdminCommand(&'static str),
|
||||
#[error("{0}")]
|
||||
Error(String),
|
||||
}
|
||||
|
||||
impl Error {
|
||||
|
@ -86,9 +88,9 @@ impl Error {
|
|||
Self::BadDatabase(message)
|
||||
}
|
||||
|
||||
pub fn bad_config(message: &'static str) -> Self {
|
||||
pub fn bad_config(message: &str) -> Self {
|
||||
error!("BadConfig: {}", message);
|
||||
Self::BadConfig(message)
|
||||
Self::BadConfig(message.to_owned())
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -197,3 +199,7 @@ impl From<Infallible> for Error {
|
|||
impl axum::response::IntoResponse for Error {
|
||||
fn into_response(self) -> axum::response::Response { self.to_response().into_response() }
|
||||
}
|
||||
|
||||
impl std::fmt::Debug for Error {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "{}", self) }
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue