add support for serving client+server well-known and /client/server.json endpoints from conduwuit

the last endpoint is a non-standard health check endpoint used by at
least Element Web as a weird way to determine if syncv3 is available

there can also be some valid use-cases for serving well-knowns from the
application itself

Signed-off-by: strawberry <strawberry@puppygock.gay>
This commit is contained in:
strawberry 2024-01-07 21:24:55 -05:00 committed by June
parent bb2f213ac3
commit 8586b15e1a
5 changed files with 49 additions and 17 deletions

View file

@ -45,9 +45,7 @@ pub async fn get_supported_versions_route(
} }
/// # `GET /.well-known/matrix/client` /// # `GET /.well-known/matrix/client`
pub async fn well_known_client_route( pub async fn well_known_client_route() -> Result<impl IntoResponse> {
_body: Ruma<get_supported_versions::Request>,
) -> Result<impl IntoResponse> {
let client_url = match services().globals.well_known_client() { let client_url = match services().globals.well_known_client() {
Some(url) => url.clone(), Some(url) => url.clone(),
None => return Err(Error::BadRequest(ErrorKind::NotFound, "Not found.")), None => return Err(Error::BadRequest(ErrorKind::NotFound, "Not found.")),
@ -58,3 +56,22 @@ pub async fn well_known_client_route(
"org.matrix.msc3575.proxy": {"url": client_url} "org.matrix.msc3575.proxy": {"url": client_url}
}))) })))
} }
/// # `GET /client/server.json`
///
/// Endpoint provided by sliding sync proxy used by some clients such as Element Web
/// as a non-standard health check.
pub async fn syncv3_client_server_json() -> Result<impl IntoResponse> {
let server_url = match services().globals.well_known_client() {
Some(url) => url.clone(),
None => match services().globals.well_known_server() {
Some(url) => url.clone(),
None => return Err(Error::BadRequest(ErrorKind::NotFound, "Not found.")),
},
};
Ok(Json(serde_json::json!({
"server": server_url,
"version": format!("{} {}", env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION"))
})))
}

View file

@ -2060,6 +2060,18 @@ pub async fn claim_keys_route(
}) })
} }
/// # `GET /.well-known/matrix/server`
pub async fn well_known_server_route() -> Result<impl IntoResponse> {
let server_url = match services().globals.well_known_server() {
Some(url) => url.clone(),
None => return Err(Error::BadRequest(ErrorKind::NotFound, "Not found.")),
};
Ok(Json(serde_json::json!({
"m.server": server_url
})))
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::{add_port_to_hostname, get_ip_with_port, FedDest}; use super::{add_port_to_hostname, get_ip_with_port, FedDest};

View file

@ -70,6 +70,7 @@ pub struct Config {
#[serde(default = "default_default_room_version")] #[serde(default = "default_default_room_version")]
pub default_room_version: RoomVersionId, pub default_room_version: RoomVersionId,
pub well_known_client: Option<String>, pub well_known_client: Option<String>,
pub well_known_server: Option<String>,
#[serde(default)] #[serde(default)]
pub allow_jaeger: bool, pub allow_jaeger: bool,
#[serde(default)] #[serde(default)]

View file

@ -514,7 +514,18 @@ fn routes() -> Router {
"/_matrix/client/v3/rooms/:room_id/initialSync", "/_matrix/client/v3/rooms/:room_id/initialSync",
get(initial_sync), get(initial_sync),
) )
//.route("/client/server.json", get(syncv3_client_server_json)) .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)) .route("/", get(it_works))
.fallback(not_found) .fallback(not_found)
} }
@ -572,19 +583,6 @@ async fn it_works() -> &'static str {
"hewwo from conduwuit woof!" "hewwo from conduwuit woof!"
} }
/*
// TODO: add /client/server.json support by querying our client well-known for the true matrix homeserver URL
async fn syncv3_client_server_json(uri: Uri) -> impl IntoResponse {
let server_name = services().globals.server_name().to_string();
let response = services().globals.default_client().get(&format!("https://{server_name"))
let server = uri.scheme_str().unwrap_or("https").to_owned() + "://" + uri.host().unwrap();
let version = format!("cowonduit {}", env!("CARGO_PKG_VERSION").to_owned());
let body = format!("{{\"server\":\"{server}\",\"version\":\"{version}\"}}");
Json(body)
}
*/
trait RouterExt { trait RouterExt {
fn ruma_route<H, T>(self, handler: H) -> Self fn ruma_route<H, T>(self, handler: H) -> Self
where where

View file

@ -509,6 +509,10 @@ impl Service<'_> {
&self.config.well_known_client &self.config.well_known_client
} }
pub fn well_known_server(&self) -> &Option<String> {
&self.config.well_known_server
}
pub fn unix_socket_path(&self) -> &Option<PathBuf> { pub fn unix_socket_path(&self) -> &Option<PathBuf> {
&self.config.unix_socket_path &self.config.unix_socket_path
} }