2024-07-14 23:46:03 +00:00
|
|
|
mod args;
|
|
|
|
mod auth;
|
|
|
|
mod handler;
|
|
|
|
mod request;
|
|
|
|
mod response;
|
2024-07-30 01:25:07 +00:00
|
|
|
pub mod state;
|
2024-07-27 07:17:07 +00:00
|
|
|
|
2024-08-30 02:54:47 +00:00
|
|
|
use std::str::FromStr;
|
|
|
|
|
2024-04-04 03:33:47 +00:00
|
|
|
use axum::{
|
2024-08-30 02:54:47 +00:00
|
|
|
response::{IntoResponse, Redirect},
|
2024-05-28 04:52:58 +00:00
|
|
|
routing::{any, get, post},
|
2024-04-04 03:33:47 +00:00
|
|
|
Router,
|
|
|
|
};
|
2024-07-15 03:56:27 +00:00
|
|
|
use conduit::{err, Server};
|
2024-08-30 02:54:47 +00:00
|
|
|
use http::{uri, Uri};
|
2024-04-04 03:33:47 +00:00
|
|
|
|
2024-07-14 23:46:03 +00:00
|
|
|
use self::handler::RouterExt;
|
2024-07-30 01:25:07 +00:00
|
|
|
pub(super) use self::{args::Args as Ruma, response::RumaResponse, state::State};
|
2024-07-14 23:46:03 +00:00
|
|
|
use crate::{client, server};
|
2024-04-23 02:36:11 +00:00
|
|
|
|
2024-07-15 03:56:27 +00:00
|
|
|
pub fn build(router: Router<State>, server: &Server) -> Router<State> {
|
2024-05-09 22:59:08 +00:00
|
|
|
let config = &server.config;
|
2024-08-27 03:35:00 +00:00
|
|
|
let mut router = router
|
2024-08-30 23:37:21 +00:00
|
|
|
.ruma_route(client::appservice_ping)
|
2024-06-06 18:21:02 +00:00
|
|
|
.ruma_route(client::get_supported_versions_route)
|
|
|
|
.ruma_route(client::get_register_available_route)
|
|
|
|
.ruma_route(client::register_route)
|
|
|
|
.ruma_route(client::get_login_types_route)
|
|
|
|
.ruma_route(client::login_route)
|
|
|
|
.ruma_route(client::whoami_route)
|
|
|
|
.ruma_route(client::logout_route)
|
|
|
|
.ruma_route(client::logout_all_route)
|
|
|
|
.ruma_route(client::change_password_route)
|
|
|
|
.ruma_route(client::deactivate_route)
|
|
|
|
.ruma_route(client::third_party_route)
|
|
|
|
.ruma_route(client::request_3pid_management_token_via_email_route)
|
|
|
|
.ruma_route(client::request_3pid_management_token_via_msisdn_route)
|
|
|
|
.ruma_route(client::check_registration_token_validity)
|
|
|
|
.ruma_route(client::get_capabilities_route)
|
|
|
|
.ruma_route(client::get_pushrules_all_route)
|
|
|
|
.ruma_route(client::set_pushrule_route)
|
|
|
|
.ruma_route(client::get_pushrule_route)
|
|
|
|
.ruma_route(client::set_pushrule_enabled_route)
|
|
|
|
.ruma_route(client::get_pushrule_enabled_route)
|
|
|
|
.ruma_route(client::get_pushrule_actions_route)
|
|
|
|
.ruma_route(client::set_pushrule_actions_route)
|
|
|
|
.ruma_route(client::delete_pushrule_route)
|
|
|
|
.ruma_route(client::get_room_event_route)
|
|
|
|
.ruma_route(client::get_room_aliases_route)
|
|
|
|
.ruma_route(client::get_filter_route)
|
|
|
|
.ruma_route(client::create_filter_route)
|
2024-07-03 01:51:11 +00:00
|
|
|
.ruma_route(client::create_openid_token_route)
|
2024-06-06 18:21:02 +00:00
|
|
|
.ruma_route(client::set_global_account_data_route)
|
|
|
|
.ruma_route(client::set_room_account_data_route)
|
|
|
|
.ruma_route(client::get_global_account_data_route)
|
|
|
|
.ruma_route(client::get_room_account_data_route)
|
|
|
|
.ruma_route(client::set_displayname_route)
|
|
|
|
.ruma_route(client::get_displayname_route)
|
|
|
|
.ruma_route(client::set_avatar_url_route)
|
|
|
|
.ruma_route(client::get_avatar_url_route)
|
|
|
|
.ruma_route(client::get_profile_route)
|
|
|
|
.ruma_route(client::set_presence_route)
|
|
|
|
.ruma_route(client::get_presence_route)
|
|
|
|
.ruma_route(client::upload_keys_route)
|
|
|
|
.ruma_route(client::get_keys_route)
|
|
|
|
.ruma_route(client::claim_keys_route)
|
|
|
|
.ruma_route(client::create_backup_version_route)
|
|
|
|
.ruma_route(client::update_backup_version_route)
|
|
|
|
.ruma_route(client::delete_backup_version_route)
|
|
|
|
.ruma_route(client::get_latest_backup_info_route)
|
|
|
|
.ruma_route(client::get_backup_info_route)
|
|
|
|
.ruma_route(client::add_backup_keys_route)
|
|
|
|
.ruma_route(client::add_backup_keys_for_room_route)
|
|
|
|
.ruma_route(client::add_backup_keys_for_session_route)
|
|
|
|
.ruma_route(client::delete_backup_keys_for_room_route)
|
|
|
|
.ruma_route(client::delete_backup_keys_for_session_route)
|
|
|
|
.ruma_route(client::delete_backup_keys_route)
|
|
|
|
.ruma_route(client::get_backup_keys_for_room_route)
|
|
|
|
.ruma_route(client::get_backup_keys_for_session_route)
|
|
|
|
.ruma_route(client::get_backup_keys_route)
|
|
|
|
.ruma_route(client::set_read_marker_route)
|
|
|
|
.ruma_route(client::create_receipt_route)
|
|
|
|
.ruma_route(client::create_typing_event_route)
|
|
|
|
.ruma_route(client::create_room_route)
|
|
|
|
.ruma_route(client::redact_event_route)
|
|
|
|
.ruma_route(client::report_event_route)
|
|
|
|
.ruma_route(client::create_alias_route)
|
|
|
|
.ruma_route(client::delete_alias_route)
|
|
|
|
.ruma_route(client::get_alias_route)
|
|
|
|
.ruma_route(client::join_room_by_id_route)
|
|
|
|
.ruma_route(client::join_room_by_id_or_alias_route)
|
|
|
|
.ruma_route(client::joined_members_route)
|
|
|
|
.ruma_route(client::leave_room_route)
|
|
|
|
.ruma_route(client::forget_room_route)
|
|
|
|
.ruma_route(client::joined_rooms_route)
|
|
|
|
.ruma_route(client::kick_user_route)
|
|
|
|
.ruma_route(client::ban_user_route)
|
|
|
|
.ruma_route(client::unban_user_route)
|
|
|
|
.ruma_route(client::invite_user_route)
|
|
|
|
.ruma_route(client::set_room_visibility_route)
|
|
|
|
.ruma_route(client::get_room_visibility_route)
|
|
|
|
.ruma_route(client::get_public_rooms_route)
|
|
|
|
.ruma_route(client::get_public_rooms_filtered_route)
|
|
|
|
.ruma_route(client::search_users_route)
|
|
|
|
.ruma_route(client::get_member_events_route)
|
|
|
|
.ruma_route(client::get_protocols_route)
|
2024-07-05 20:39:57 +00:00
|
|
|
.route("/_matrix/client/unstable/thirdparty/protocols",
|
2024-07-14 23:46:03 +00:00
|
|
|
get(client::get_protocols_route_unstable))
|
2024-06-06 18:21:02 +00:00
|
|
|
.ruma_route(client::send_message_event_route)
|
|
|
|
.ruma_route(client::send_state_event_for_key_route)
|
|
|
|
.ruma_route(client::get_state_events_route)
|
|
|
|
.ruma_route(client::get_state_events_for_key_route)
|
2024-04-04 03:33:47 +00:00
|
|
|
// 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",
|
2024-06-06 18:21:02 +00:00
|
|
|
get(client::get_state_events_for_empty_key_route)
|
|
|
|
.put(client::send_state_event_for_empty_key_route),
|
2024-04-04 03:33:47 +00:00
|
|
|
)
|
|
|
|
.route(
|
|
|
|
"/_matrix/client/v3/rooms/:room_id/state/:event_type",
|
2024-06-06 18:21:02 +00:00
|
|
|
get(client::get_state_events_for_empty_key_route)
|
|
|
|
.put(client::send_state_event_for_empty_key_route),
|
2024-04-04 03:33:47 +00:00
|
|
|
)
|
|
|
|
// These two endpoints allow trailing slashes
|
|
|
|
.route(
|
|
|
|
"/_matrix/client/r0/rooms/:room_id/state/:event_type/",
|
2024-06-06 18:21:02 +00:00
|
|
|
get(client::get_state_events_for_empty_key_route)
|
|
|
|
.put(client::send_state_event_for_empty_key_route),
|
2024-04-04 03:33:47 +00:00
|
|
|
)
|
|
|
|
.route(
|
|
|
|
"/_matrix/client/v3/rooms/:room_id/state/:event_type/",
|
2024-06-06 18:21:02 +00:00
|
|
|
get(client::get_state_events_for_empty_key_route)
|
|
|
|
.put(client::send_state_event_for_empty_key_route),
|
2024-04-04 03:33:47 +00:00
|
|
|
)
|
2024-06-06 18:21:02 +00:00
|
|
|
.ruma_route(client::sync_events_route)
|
|
|
|
.ruma_route(client::sync_events_v4_route)
|
|
|
|
.ruma_route(client::get_context_route)
|
|
|
|
.ruma_route(client::get_message_events_route)
|
|
|
|
.ruma_route(client::search_events_route)
|
|
|
|
.ruma_route(client::turn_server_route)
|
|
|
|
.ruma_route(client::send_event_to_device_route)
|
|
|
|
.ruma_route(client::create_content_route)
|
2024-08-27 11:19:57 +00:00
|
|
|
.ruma_route(client::get_content_thumbnail_route)
|
|
|
|
.ruma_route(client::get_content_route)
|
|
|
|
.ruma_route(client::get_content_as_filename_route)
|
|
|
|
.ruma_route(client::get_media_preview_route)
|
|
|
|
.ruma_route(client::get_media_config_route)
|
2024-06-06 18:21:02 +00:00
|
|
|
.ruma_route(client::get_devices_route)
|
|
|
|
.ruma_route(client::get_device_route)
|
|
|
|
.ruma_route(client::update_device_route)
|
|
|
|
.ruma_route(client::delete_device_route)
|
|
|
|
.ruma_route(client::delete_devices_route)
|
|
|
|
.ruma_route(client::get_tags_route)
|
|
|
|
.ruma_route(client::update_tag_route)
|
|
|
|
.ruma_route(client::delete_tag_route)
|
|
|
|
.ruma_route(client::upload_signing_keys_route)
|
|
|
|
.ruma_route(client::upload_signatures_route)
|
|
|
|
.ruma_route(client::get_key_changes_route)
|
|
|
|
.ruma_route(client::get_pushers_route)
|
|
|
|
.ruma_route(client::set_pushers_route)
|
|
|
|
.ruma_route(client::upgrade_room_route)
|
|
|
|
.ruma_route(client::get_threads_route)
|
|
|
|
.ruma_route(client::get_relating_events_with_rel_type_and_event_type_route)
|
|
|
|
.ruma_route(client::get_relating_events_with_rel_type_route)
|
|
|
|
.ruma_route(client::get_relating_events_route)
|
|
|
|
.ruma_route(client::get_hierarchy_route)
|
2024-07-14 23:46:03 +00:00
|
|
|
.ruma_route(client::get_mutual_rooms_route)
|
|
|
|
.ruma_route(client::get_room_summary)
|
|
|
|
.route(
|
|
|
|
"/_matrix/client/unstable/im.nheko.summary/rooms/:room_id_or_alias/summary",
|
|
|
|
get(client::get_room_summary_legacy)
|
|
|
|
)
|
|
|
|
.ruma_route(client::well_known_support)
|
|
|
|
.ruma_route(client::well_known_client)
|
|
|
|
.route("/_conduwuit/server_version", get(client::conduwuit_server_version))
|
2024-04-04 03:33:47 +00:00
|
|
|
.route("/_matrix/client/r0/rooms/:room_id/initialSync", get(initial_sync))
|
|
|
|
.route("/_matrix/client/v3/rooms/:room_id/initialSync", get(initial_sync))
|
2024-06-06 18:21:02 +00:00
|
|
|
.route("/client/server.json", get(client::syncv3_client_server_json));
|
2024-04-12 23:57:44 +00:00
|
|
|
|
|
|
|
if config.allow_federation {
|
2024-08-27 03:35:00 +00:00
|
|
|
router = router
|
2024-06-05 04:32:58 +00:00
|
|
|
.ruma_route(server::get_server_version_route)
|
|
|
|
.route("/_matrix/key/v2/server", get(server::get_server_keys_route))
|
|
|
|
.route("/_matrix/key/v2/server/:key_id", get(server::get_server_keys_deprecated_route))
|
|
|
|
.ruma_route(server::get_public_rooms_route)
|
|
|
|
.ruma_route(server::get_public_rooms_filtered_route)
|
|
|
|
.ruma_route(server::send_transaction_message_route)
|
|
|
|
.ruma_route(server::get_event_route)
|
|
|
|
.ruma_route(server::get_backfill_route)
|
|
|
|
.ruma_route(server::get_missing_events_route)
|
|
|
|
.ruma_route(server::get_event_authorization_route)
|
|
|
|
.ruma_route(server::get_room_state_route)
|
|
|
|
.ruma_route(server::get_room_state_ids_route)
|
|
|
|
.ruma_route(server::create_leave_event_template_route)
|
|
|
|
.ruma_route(server::create_leave_event_v1_route)
|
|
|
|
.ruma_route(server::create_leave_event_v2_route)
|
|
|
|
.ruma_route(server::create_join_event_template_route)
|
|
|
|
.ruma_route(server::create_join_event_v1_route)
|
|
|
|
.ruma_route(server::create_join_event_v2_route)
|
|
|
|
.ruma_route(server::create_invite_route)
|
|
|
|
.ruma_route(server::get_devices_route)
|
|
|
|
.ruma_route(server::get_room_information_route)
|
|
|
|
.ruma_route(server::get_profile_information_route)
|
|
|
|
.ruma_route(server::get_keys_route)
|
|
|
|
.ruma_route(server::claim_keys_route)
|
2024-07-03 01:51:11 +00:00
|
|
|
.ruma_route(server::get_openid_userinfo_route)
|
2024-06-05 04:32:58 +00:00
|
|
|
.ruma_route(server::get_hierarchy_route)
|
|
|
|
.ruma_route(server::well_known_server)
|
2024-08-27 02:56:17 +00:00
|
|
|
.ruma_route(server::get_content_route)
|
|
|
|
.ruma_route(server::get_content_thumbnail_route)
|
2024-08-27 03:35:00 +00:00
|
|
|
.route("/_conduwuit/local_user_count", get(client::conduwuit_local_user_count));
|
2024-04-12 23:57:44 +00:00
|
|
|
} else {
|
2024-08-27 03:35:00 +00:00
|
|
|
router = router
|
2024-04-12 23:57:44 +00:00
|
|
|
.route("/_matrix/federation/*path", any(federation_disabled))
|
|
|
|
.route("/.well-known/matrix/server", any(federation_disabled))
|
|
|
|
.route("/_matrix/key/*path", any(federation_disabled))
|
2024-08-27 03:35:00 +00:00
|
|
|
.route("/_conduwuit/local_user_count", any(federation_disabled));
|
2024-04-12 23:57:44 +00:00
|
|
|
}
|
2024-08-27 03:35:00 +00:00
|
|
|
|
|
|
|
if config.allow_legacy_media {
|
|
|
|
router = router
|
|
|
|
.ruma_route(client::get_media_config_legacy_route)
|
|
|
|
.ruma_route(client::get_media_preview_legacy_route)
|
|
|
|
.ruma_route(client::get_content_legacy_route)
|
|
|
|
.ruma_route(client::get_content_as_filename_legacy_route)
|
|
|
|
.ruma_route(client::get_content_thumbnail_legacy_route)
|
|
|
|
.route("/_matrix/media/v1/config", get(client::get_media_config_legacy_legacy_route))
|
|
|
|
.route("/_matrix/media/v1/upload", post(client::create_content_legacy_route))
|
|
|
|
.route(
|
|
|
|
"/_matrix/media/v1/preview_url",
|
|
|
|
get(client::get_media_preview_legacy_legacy_route),
|
|
|
|
)
|
|
|
|
.route(
|
|
|
|
"/_matrix/media/v1/download/:server_name/:media_id",
|
|
|
|
get(client::get_content_legacy_legacy_route),
|
|
|
|
)
|
|
|
|
.route(
|
|
|
|
"/_matrix/media/v1/download/:server_name/:media_id/:file_name",
|
|
|
|
get(client::get_content_as_filename_legacy_legacy_route),
|
|
|
|
)
|
|
|
|
.route(
|
|
|
|
"/_matrix/media/v1/thumbnail/:server_name/:media_id",
|
|
|
|
get(client::get_content_thumbnail_legacy_legacy_route),
|
|
|
|
);
|
|
|
|
} else {
|
2024-08-30 08:12:30 +00:00
|
|
|
router = router
|
|
|
|
.route("/_matrix/media/v1/*path", any(legacy_media_disabled))
|
|
|
|
.route("/_matrix/media/v3/config", any(legacy_media_disabled))
|
|
|
|
.route("/_matrix/media/v3/download/*path", any(legacy_media_disabled))
|
|
|
|
.route("/_matrix/media/v3/thumbnail/*path", any(legacy_media_disabled))
|
2024-08-30 16:27:36 +00:00
|
|
|
.route("/_matrix/media/v3/preview_url", any(redirect_legacy_preview))
|
|
|
|
.route("/_matrix/media/r0/config", any(legacy_media_disabled))
|
|
|
|
.route("/_matrix/media/r0/download/*path", any(legacy_media_disabled))
|
|
|
|
.route("/_matrix/media/r0/thumbnail/*path", any(legacy_media_disabled))
|
|
|
|
.route("/_matrix/media/r0/preview_url", any(redirect_legacy_preview));
|
2024-08-27 03:35:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
router
|
2024-04-04 03:33:47 +00:00
|
|
|
}
|
|
|
|
|
2024-08-30 02:54:47 +00:00
|
|
|
async fn redirect_legacy_preview(uri: Uri) -> impl IntoResponse {
|
|
|
|
let path = "/_matrix/client/v1/media/preview_url";
|
|
|
|
let query = uri.query().unwrap_or_default();
|
|
|
|
|
|
|
|
let path_and_query = format!("{path}?{query}");
|
|
|
|
let path_and_query = uri::PathAndQuery::from_str(&path_and_query)
|
|
|
|
.expect("Failed to build PathAndQuery for media preview redirect URI");
|
|
|
|
|
|
|
|
let uri = uri::Builder::new()
|
|
|
|
.path_and_query(path_and_query)
|
|
|
|
.build()
|
|
|
|
.expect("Failed to build URI for redirect")
|
|
|
|
.to_string();
|
|
|
|
|
|
|
|
Redirect::temporary(&uri)
|
|
|
|
}
|
|
|
|
|
2024-04-04 03:33:47 +00:00
|
|
|
async fn initial_sync(_uri: Uri) -> impl IntoResponse {
|
2024-07-14 23:46:03 +00:00
|
|
|
err!(Request(GuestAccessForbidden("Guest access not implemented")))
|
2024-04-04 03:33:47 +00:00
|
|
|
}
|
|
|
|
|
2024-08-27 11:19:57 +00:00
|
|
|
async fn legacy_media_disabled() -> impl IntoResponse { err!(Request(Forbidden("Unauthenticated media is disabled."))) }
|
2024-08-27 03:35:00 +00:00
|
|
|
|
2024-08-27 11:19:57 +00:00
|
|
|
async fn federation_disabled() -> impl IntoResponse { err!(Request(Forbidden("Federation is disabled."))) }
|