forked from mirror/grapevine
serve well-known client and server config
This way users can have a simpler time configuring this stuff and we can worry about the spec compliance parts and specifying the same thing over and over parts.
This commit is contained in:
parent
3a55684623
commit
806cc0cb28
4 changed files with 99 additions and 1 deletions
|
@ -2,3 +2,4 @@ pub(crate) mod appservice_server;
|
|||
pub(crate) mod client_server;
|
||||
pub(crate) mod ruma_wrapper;
|
||||
pub(crate) mod server_server;
|
||||
pub(crate) mod well_known;
|
||||
|
|
58
src/api/well_known.rs
Normal file
58
src/api/well_known.rs
Normal file
|
@ -0,0 +1,58 @@
|
|||
#![warn(missing_docs, clippy::missing_docs_in_private_items)]
|
||||
|
||||
//! Handle requests for `/.well-known/matrix/...` files
|
||||
|
||||
use http::StatusCode;
|
||||
use ruma::api::{
|
||||
client::discovery::discover_homeserver as client,
|
||||
federation::discovery::discover_homeserver as server,
|
||||
};
|
||||
|
||||
use crate::{services, Ar, Ra};
|
||||
|
||||
/// Handler for `/.well-known/matrix/server`
|
||||
pub(crate) async fn server(
|
||||
_: Ar<server::Request>,
|
||||
) -> Result<Ra<server::Response>, StatusCode> {
|
||||
let Some(authority) =
|
||||
services().globals.config.server_discovery.server.authority.clone()
|
||||
else {
|
||||
return Err(StatusCode::NOT_FOUND);
|
||||
};
|
||||
|
||||
if authority == services().globals.config.server_name {
|
||||
// Delegation isn't needed in this case
|
||||
return Err(StatusCode::NOT_FOUND);
|
||||
}
|
||||
|
||||
Ok(Ra(server::Response::new(authority)))
|
||||
}
|
||||
|
||||
/// Handler for `/.well-known/matrix/client`
|
||||
pub(crate) async fn client(_: Ar<client::Request>) -> Ra<client::Response> {
|
||||
let authority = services()
|
||||
.globals
|
||||
.config
|
||||
.server_discovery
|
||||
.client
|
||||
.authority
|
||||
.clone()
|
||||
.unwrap_or_else(|| services().globals.config.server_name.clone());
|
||||
|
||||
let scheme = if services().globals.config.server_discovery.client.insecure {
|
||||
"http"
|
||||
} else {
|
||||
"https"
|
||||
};
|
||||
|
||||
let base_url = format!("{scheme}://{authority}");
|
||||
|
||||
// I wish ruma used an actual URL type instead of `String`
|
||||
Ra(client::Response {
|
||||
homeserver: client::HomeserverInfo::new(base_url.clone()),
|
||||
identity_server: None,
|
||||
sliding_sync_proxy: Some(client::SlidingSyncProxyInfo {
|
||||
url: base_url,
|
||||
}),
|
||||
})
|
||||
}
|
|
@ -30,7 +30,13 @@ pub(crate) struct Config {
|
|||
pub(crate) listen: Vec<ListenConfig>,
|
||||
pub(crate) tls: Option<TlsConfig>,
|
||||
|
||||
/// The name of this homeserver
|
||||
///
|
||||
/// This is the value that will appear e.g. in user IDs and room aliases.
|
||||
pub(crate) server_name: OwnedServerName,
|
||||
|
||||
#[serde(default)]
|
||||
pub(crate) server_discovery: ServerDiscovery,
|
||||
pub(crate) database: DatabaseConfig,
|
||||
#[serde(default)]
|
||||
pub(crate) federation: FederationConfig,
|
||||
|
@ -63,6 +69,35 @@ pub(crate) struct Config {
|
|||
pub(crate) emergency_password: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Default, Deserialize)]
|
||||
pub(crate) struct ServerDiscovery {
|
||||
/// Server-server discovery configuration
|
||||
#[serde(default)]
|
||||
pub(crate) server: ServerServerDiscovery,
|
||||
|
||||
/// Client-server discovery configuration
|
||||
#[serde(default)]
|
||||
pub(crate) client: ClientServerDiscovery,
|
||||
}
|
||||
|
||||
/// Server-server discovery configuration
|
||||
#[derive(Debug, Default, Deserialize)]
|
||||
pub(crate) struct ServerServerDiscovery {
|
||||
/// The alternative authority to make server-server API requests to
|
||||
pub(crate) authority: Option<OwnedServerName>,
|
||||
}
|
||||
|
||||
/// Client-server discovery configuration
|
||||
#[derive(Debug, Default, Deserialize)]
|
||||
pub(crate) struct ClientServerDiscovery {
|
||||
/// The alternative authority to make client-server API requests to
|
||||
pub(crate) authority: Option<OwnedServerName>,
|
||||
|
||||
/// Controls whether HTTPS is used
|
||||
#[serde(default)]
|
||||
pub(crate) insecure: bool,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub(crate) struct TlsConfig {
|
||||
pub(crate) certs: String,
|
||||
|
|
|
@ -50,7 +50,7 @@ mod service;
|
|||
mod utils;
|
||||
|
||||
pub(crate) use api::ruma_wrapper::{Ar, Ra};
|
||||
use api::{client_server, server_server};
|
||||
use api::{client_server, server_server, well_known};
|
||||
pub(crate) use config::{Config, ListenConfig};
|
||||
pub(crate) use database::KeyValueDatabase;
|
||||
pub(crate) use service::{pdu::PduEvent, Services};
|
||||
|
@ -491,6 +491,10 @@ fn routes(config: &Config) -> Router {
|
|||
.route("/", get(it_works))
|
||||
.fallback(not_found);
|
||||
|
||||
let router = router
|
||||
.route("/.well-known/matrix/client", get(well_known::client))
|
||||
.route("/.well-known/matrix/server", get(well_known::server));
|
||||
|
||||
if config.federation.enable {
|
||||
router
|
||||
.ruma_route(s2s::get_server_version_route)
|
||||
|
|
Loading…
Reference in a new issue