From b734c1aff50bb8757c4b0a720c73805ec51aab87 Mon Sep 17 00:00:00 2001 From: Jonas Zohren Date: Fri, 29 Oct 2021 21:33:34 +0200 Subject: [PATCH] feat: Add option to serve .well-known requests for standalone usage (wip) Signed-off-by: Jonas Zohren --- src/database.rs | 5 +++++ src/database/globals.rs | 4 ++++ src/main.rs | 14 ++++++++++++-- src/well_known.rs | 27 +++++++++++++++++++++++++++ 4 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 src/well_known.rs diff --git a/src/database.rs b/src/database.rs index 8cf4f64..3457017 100644 --- a/src/database.rs +++ b/src/database.rs @@ -74,6 +74,11 @@ pub struct Config { trusted_servers: Vec>, #[serde(default = "default_log")] pub log: String, + /// If enabled, Conduit will serve /.wellknown/matrix files that point to the configured server_name. + /// This should make it easier, if you only host Conduit on the same domain as the server_name. + /// E.g. setting server_name: "matrix.example.com" -> other servers are directed to "https://matrix.example.com:443" + #[serde(default = "false_fn")] + pub serve_wellknown: bool, #[serde(flatten)] catchall: BTreeMap, diff --git a/src/database/globals.rs b/src/database/globals.rs index f1cbbd9..a2217b1 100644 --- a/src/database/globals.rs +++ b/src/database/globals.rs @@ -226,6 +226,10 @@ impl Globals { self.jwt_decoding_key.as_ref() } + pub fn serve_wellknown(&self) -> bool { + self.config.serve_wellknown + } + /// TODO: the key valid until timestamp is only honored in room version > 4 /// Remove the outdated keys and insert the new ones. /// diff --git a/src/main.rs b/src/main.rs index 84dfb1f..6ac7746 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,6 +10,7 @@ pub mod appservice_server; pub mod client_server; pub mod server_server; +pub mod well_known; mod database; mod error; @@ -40,8 +41,17 @@ use tokio::sync::RwLock; use tracing_subscriber::{prelude::*, EnvFilter}; fn setup_rocket(config: Figment, data: Arc>) -> rocket::Rocket { - rocket::custom(config) - .manage(data) + let rocket_builder = rocket::custom(config).manage(data); + + // If configured, serve .well-known/matrix files + if data.lock().globals.serve_wellknown() { + rocket_builder.mount( + "/", + routes![well_known::well_known_server, well_known::well_known_client,], + ); + } + + rocket_builder .mount( "/", routes![ diff --git a/src/well_known.rs b/src/well_known.rs new file mode 100644 index 0000000..dd94a26 --- /dev/null +++ b/src/well_known.rs @@ -0,0 +1,27 @@ +#[cfg(feature = "conduit_bin")] +use rocket::get; +use rocket::response::content; +use crate::database::DatabaseGuard; +//use serde::Serialize; + +//#[derive(Serialize)] +//pub struct WellKnownServerInfo { +// m_server: String, +//} + +#[cfg_attr(feature = "conduit_bin", get("/.well-known/matrix/server"))] +#[tracing::instrument(skip(db))] +pub fn well_known_server(db: DatabaseGuard) -> content::Json { + // Pull the server_name from config + let server_name = db.globals.server_name().clone(); + content::Json(format!("{{\"m.server\": \"{}:443\"}}", server_name)) +} + + +#[cfg_attr(feature = "conduit_bin", get("/.well-known/matrix/client"))] +#[tracing::instrument(skip(db))] +pub fn well_known_client(db: DatabaseGuard) -> content::Json { + // Pull the server_name from config + let server_name = db.globals.server_name().clone(); + content::Json(format!("{{\"m.homeserver\": {{ \"base_url\": \"https://{}\"}}}}", server_name)) +}