refactor well-known stuff to use proper ruma types, config types, etc

this does deprecate the original `well_known_` prefixed config options
with a dedicated/proper config sub-block (`[config.well_known]`)

Signed-off-by: strawberry <strawberry@puppygock.gay>
This commit is contained in:
strawberry 2024-04-11 20:17:30 -04:00 committed by June
parent 993c0102d9
commit bfa68e7bc5
9 changed files with 134 additions and 66 deletions

1
Cargo.lock generated
View file

@ -522,6 +522,7 @@ dependencies = [
"tracing-flame", "tracing-flame",
"tracing-opentelemetry", "tracing-opentelemetry",
"tracing-subscriber", "tracing-subscriber",
"url",
"webpage", "webpage",
] ]

View file

@ -75,6 +75,9 @@ http = "0.2.12"
# used to replace the channels of the tokio runtime # used to replace the channels of the tokio runtime
loole = "0.3.0" loole = "0.3.0"
# Validating urls in config, was already a transitive dependency
url = { version = "2", features = ["serde"] }
# standard date and time tools # standard date and time tools
[dependencies.chrono] [dependencies.chrono]
version = "0.4.37" version = "0.4.37"

View file

@ -2,6 +2,8 @@
# This is the official example config for conduwuit. # This is the official example config for conduwuit.
# If you use it for your server, you will need to adjust it to your own needs. # If you use it for your server, you will need to adjust it to your own needs.
# At the very least, change the server_name field! # At the very least, change the server_name field!
#
# This documentation can also be found at https://conduwuit.puppyirl.gay/configuration.html
# ============================================================================= # =============================================================================
[global] [global]
@ -247,14 +249,6 @@ url_preview_max_spider_size = 384_000
# Useful if the domain contains allowlist is still too broad for you but you still want to allow all the subdomains under a root domain. # Useful if the domain contains allowlist is still too broad for you but you still want to allow all the subdomains under a root domain.
url_preview_check_root_domain = false url_preview_check_root_domain = false
# A single contact and/or support page for /.well-known/matrix/support
# All options here are strings. Currently only supports 1 single contact.
# No default.
#well_known_support_page = ""
#well_known_support_role = ""
#well_known_support_email = ""
#well_known_support_mxid = ""
# Config option to allow or disallow incoming federation requests that obtain the profiles # Config option to allow or disallow incoming federation requests that obtain the profiles
# of our local users from `/_matrix/federation/v1/query/profile` # of our local users from `/_matrix/federation/v1/query/profile`
# #
@ -281,12 +275,6 @@ allow_profile_lookup_federation_requests = true
# Defaults to false. # Defaults to false.
#allow_check_for_updates = false #allow_check_for_updates = false
# If you are using delegation via well-known files and you cannot serve them from your reverse proxy, you can
# uncomment these to serve them directly from conduwuit. This requires proxying all requests to conduwuit, not just `/_matrix` to work.
#well_known_server = "matrix.example.com:443"
#well_known_client = "https://matrix.example.com"
# Note that whatever you put will show up in the well-known JSON values.
# Set to false to disable users from joining or creating room versions that aren't 100% officially supported by conduwuit. # Set to false to disable users from joining or creating room versions that aren't 100% officially supported by conduwuit.
# conduwuit officially supports room versions 6 - 10. conduwuit has experimental/unstable support for 3 - 5, and 11. # conduwuit officially supports room versions 6 - 10. conduwuit has experimental/unstable support for 3 - 5, and 11.
# Defaults to true. # Defaults to true.
@ -623,3 +611,20 @@ allow_profile_lookup_federation_requests = true
# This config option is only available if conduwuit was built with `axum_dual_protocol` feature (not default feature) # This config option is only available if conduwuit was built with `axum_dual_protocol` feature (not default feature)
# Defaults to false # Defaults to false
#dual_protocol = false #dual_protocol = false
# If you are using delegation via well-known files and you cannot serve them from your reverse proxy, you can
# uncomment these to serve them directly from conduwuit. This requires proxying all requests to conduwuit, not just `/_matrix` to work.
#
#[global.well_known]
#server = "matrix.example.com:443"
#client = "https://matrix.example.com"
#
# A single contact and/or support page for /.well-known/matrix/support
# All options here are strings. Currently only supports 1 single contact.
# No default.
#
#well_known_support_page = ""
#well_known_support_role = ""
#well_known_support_email = ""
#well_known_support_mxid = ""

View file

@ -199,15 +199,13 @@ pub async fn login_route(body: Ruma<login::v3::Request>) -> Result<login::v3::Re
} }
// send client well-known if specified so the client knows to reconfigure itself // send client well-known if specified so the client knows to reconfigure itself
let client_discovery_info = DiscoveryInfo::new(HomeserverInfo::new( let client_discovery_info: Option<DiscoveryInfo> = services()
services() .globals
.globals .well_known_client()
.well_known_client() .as_ref()
.to_owned() .map(|server| DiscoveryInfo::new(HomeserverInfo::new(server.to_string())));
.unwrap_or_default(),
));
info!("{} logged in", user_id); info!("{user_id} logged in");
// home_server is deprecated but apparently must still be sent despite it being // home_server is deprecated but apparently must still be sent despite it being
// deprecated over 6 years ago. initially i thought this macro was unnecessary, // deprecated over 6 years ago. initially i thought this macro was unnecessary,
@ -217,13 +215,7 @@ pub async fn login_route(body: Ruma<login::v3::Request>) -> Result<login::v3::Re
user_id, user_id,
access_token: token, access_token: token,
device_id, device_id,
well_known: { well_known: client_discovery_info,
if client_discovery_info.homeserver.base_url.as_str() == "" {
None
} else {
Some(client_discovery_info)
}
},
expires_in: None, expires_in: None,
home_server: Some(services().globals.server_name().to_owned()), home_server: Some(services().globals.server_name().to_owned()),
refresh_token: None, refresh_token: None,

View file

@ -3,6 +3,7 @@ use std::collections::BTreeMap;
use axum::{response::IntoResponse, Json}; use axum::{response::IntoResponse, Json};
use ruma::api::client::{ use ruma::api::client::{
discovery::{ discovery::{
discover_homeserver::{self, HomeserverInfo, SlidingSyncProxyInfo},
discover_support::{self, Contact}, discover_support::{self, Contact},
get_supported_versions, get_supported_versions,
}, },
@ -57,23 +58,35 @@ pub async fn get_supported_versions_route(
} }
/// # `GET /.well-known/matrix/client` /// # `GET /.well-known/matrix/client`
pub async fn well_known_client_route() -> Result<impl IntoResponse> { ///
/// Returns the .well-known URL if it is configured, otherwise returns 404.
pub async fn well_known_client(_body: Ruma<discover_homeserver::Request>) -> Result<discover_homeserver::Response> {
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.to_string(),
None => return Err(Error::BadRequest(ErrorKind::NotFound, "Not found.")), None => return Err(Error::BadRequest(ErrorKind::NotFound, "Not found.")),
}; };
Ok(Json(serde_json::json!({ Ok(discover_homeserver::Response {
"m.homeserver": {"base_url": client_url}, homeserver: HomeserverInfo {
"org.matrix.msc3575.proxy": {"url": client_url} base_url: client_url.clone(),
}))) },
identity_server: None,
sliding_sync_proxy: Some(SlidingSyncProxyInfo {
url: client_url,
}),
tile_server: None,
})
} }
/// # `GET /.well-known/matrix/support` /// # `GET /.well-known/matrix/support`
/// ///
/// Server support contact and support page of a homeserver's domain. /// Server support contact and support page of a homeserver's domain.
pub async fn well_known_support(_body: Ruma<discover_support::Request>) -> Result<discover_support::Response> { pub async fn well_known_support(_body: Ruma<discover_support::Request>) -> Result<discover_support::Response> {
let support_page = services().globals.well_known_support_page().clone(); let support_page = services()
.globals
.well_known_support_page()
.as_ref()
.map(ToString::to_string);
let role = services().globals.well_known_support_role().clone(); let role = services().globals.well_known_support_role().clone();
@ -120,9 +133,9 @@ pub async fn well_known_support(_body: Ruma<discover_support::Request>) -> Resul
/// Web as a non-standard health check. /// Web as a non-standard health check.
pub async fn syncv3_client_server_json() -> Result<impl IntoResponse> { pub async fn syncv3_client_server_json() -> Result<impl IntoResponse> {
let server_url = match services().globals.well_known_client() { let server_url = match services().globals.well_known_client() {
Some(url) => url.clone(), Some(url) => url.to_string(),
None => match services().globals.well_known_server() { None => match services().globals.well_known_server() {
Some(url) => url.clone(), Some(url) => url.to_string(),
None => return Err(Error::BadRequest(ErrorKind::NotFound, "Not found.")), None => return Err(Error::BadRequest(ErrorKind::NotFound, "Not found.")),
}, },
}; };

View file

@ -18,7 +18,7 @@ use ruma::{
backfill::get_backfill, backfill::get_backfill,
device::get_devices::{self, v1::UserDevice}, device::get_devices::{self, v1::UserDevice},
directory::{get_public_rooms, get_public_rooms_filtered}, directory::{get_public_rooms, get_public_rooms_filtered},
discovery::{get_server_keys, get_server_version, ServerSigningKeys, VerifyKey}, discovery::{discover_homeserver, get_server_keys, get_server_version, ServerSigningKeys, VerifyKey},
event::{get_event, get_missing_events, get_room_state, get_room_state_ids}, event::{get_event, get_missing_events, get_room_state, get_room_state_ids},
keys::{claim_keys, get_keys}, keys::{claim_keys, get_keys},
membership::{create_invite, create_join_event, prepare_join_event}, membership::{create_invite, create_join_event, prepare_join_event},
@ -1559,19 +1559,19 @@ pub async fn claim_keys_route(body: Ruma<claim_keys::v1::Request>) -> Result<cla
} }
/// # `GET /.well-known/matrix/server` /// # `GET /.well-known/matrix/server`
pub async fn well_known_server_route() -> Result<impl IntoResponse> { ///
/// Returns the .well-known URL if it is configured, otherwise returns 404.
pub async fn well_known_server(_body: Ruma<discover_homeserver::Request>) -> Result<discover_homeserver::Response> {
if !services().globals.allow_federation() { if !services().globals.allow_federation() {
return Err(Error::bad_config("Federation is disabled.")); return Err(Error::bad_config("Federation is disabled."));
} }
let server_url = match services().globals.well_known_server() { Ok(discover_homeserver::Response {
Some(url) => url.clone(), server: match services().globals.well_known_server() {
None => return Err(Error::BadRequest(ErrorKind::NotFound, "Not found.")), Some(server_name) => server_name.to_owned(),
}; None => return Err(Error::BadRequest(ErrorKind::NotFound, "Not found.")),
},
Ok(Json(serde_json::json!({ })
"m.server": server_url
})))
} }
/// # `GET /_matrix/federation/v1/hierarchy/{roomId}` /// # `GET /_matrix/federation/v1/hierarchy/{roomId}`

View file

@ -20,6 +20,7 @@ use ruma::{
}; };
use serde::{de::IgnoredAny, Deserialize}; use serde::{de::IgnoredAny, Deserialize};
use tracing::{debug, error, warn}; use tracing::{debug, error, warn};
use url::Url;
use self::proxy::ProxyConfig; use self::proxy::ProxyConfig;
use crate::utils::error::Error; use crate::utils::error::Error;
@ -158,8 +159,7 @@ pub struct Config {
pub allow_unstable_room_versions: bool, pub allow_unstable_room_versions: bool,
#[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: WellKnownConfig,
pub well_known_server: Option<String>,
#[serde(default)] #[serde(default)]
pub allow_jaeger: bool, pub allow_jaeger: bool,
#[serde(default)] #[serde(default)]
@ -264,11 +264,6 @@ pub struct Config {
#[serde(default = "default_ip_range_denylist")] #[serde(default = "default_ip_range_denylist")]
pub ip_range_denylist: Vec<String>, pub ip_range_denylist: Vec<String>,
pub well_known_support_page: Option<String>,
pub well_known_support_role: Option<ContactRole>,
pub well_known_support_email: Option<String>,
pub well_known_support_mxid: Option<OwnedUserId>,
#[serde(default = "Vec::new")] #[serde(default = "Vec::new")]
pub url_preview_domain_contains_allowlist: Vec<String>, pub url_preview_domain_contains_allowlist: Vec<String>,
#[serde(default = "Vec::new")] #[serde(default = "Vec::new")]
@ -319,7 +314,25 @@ pub struct TlsConfig {
pub dual_protocol: bool, pub dual_protocol: bool,
} }
const DEPRECATED_KEYS: &[&str] = &["cache_capacity"]; #[derive(Clone, Debug, Deserialize)]
pub struct WellKnownConfig {
pub client: Option<Url>,
pub server: Option<OwnedServerName>,
pub support_page: Option<Url>,
pub support_role: Option<ContactRole>,
pub support_email: Option<String>,
pub support_mxid: Option<OwnedUserId>,
}
const DEPRECATED_KEYS: &[&str] = &[
"cache_capacity",
"well_known_client",
"well_known_server",
"well_known_support_page",
"well_known_support_role",
"well_known_support_email",
"well_known_support_mxid",
];
impl Config { impl Config {
/// Initialize config /// Initialize config
@ -367,8 +380,8 @@ impl Config {
if was_deprecated { if was_deprecated {
warn!( warn!(
"Read conduit documentation and check your configuration if any new configuration parameters should \ "Read conduwuit config documentation at https://conduwuit.puppyirl.gay/configuration.html and check \
be adjusted" your configuration if any new configuration parameters should be adjusted"
); );
} }
} }
@ -690,6 +703,46 @@ impl fmt::Display for Config {
("Sentry.io send server_name in logs", &self.sentry_send_server_name.to_string()), ("Sentry.io send server_name in logs", &self.sentry_send_server_name.to_string()),
#[cfg(feature = "sentry_telemetry")] #[cfg(feature = "sentry_telemetry")]
("Sentry.io tracing sample rate", &self.sentry_traces_sample_rate.to_string()), ("Sentry.io tracing sample rate", &self.sentry_traces_sample_rate.to_string()),
(
"Well-known server name",
&if let Some(server) = &self.well_known.server {
server.to_string()
} else {
String::new()
},
),
(
"Well-known support email",
&if let Some(support_email) = &self.well_known.support_email {
support_email.to_string()
} else {
String::new()
},
),
(
"Well-known support Matrix ID",
&if let Some(support_mxid) = &self.well_known.support_mxid {
support_mxid.to_string()
} else {
String::new()
},
),
(
"Well-known support role",
&if let Some(support_role) = &self.well_known.support_role {
support_role.to_string()
} else {
String::new()
},
),
(
"Well-known support page/URL",
&if let Some(support_page) = &self.well_known.support_page {
support_page.to_string()
} else {
String::new()
},
),
]; ];
let mut msg: String = "Active config values:\n\n".to_owned(); let mut msg: String = "Active config values:\n\n".to_owned();

View file

@ -208,12 +208,12 @@ pub fn routes() -> Router {
.ruma_route(server_server::get_hierarchy_route) .ruma_route(server_server::get_hierarchy_route)
.ruma_route(client_server::get_mutual_rooms_route) .ruma_route(client_server::get_mutual_rooms_route)
.ruma_route(client_server::well_known_support) .ruma_route(client_server::well_known_support)
.ruma_route(client_server::well_known_client)
.ruma_route(server_server::well_known_server)
.route("/_conduwuit/server_version", get(client_server::conduwuit_server_version)) .route("/_conduwuit/server_version", get(client_server::conduwuit_server_version))
.route("/_matrix/client/r0/rooms/:room_id/initialSync", get(initial_sync)) .route("/_matrix/client/r0/rooms/:room_id/initialSync", get(initial_sync))
.route("/_matrix/client/v3/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("/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)
} }

View file

@ -27,6 +27,7 @@ use ruma::{
use tokio::sync::{broadcast, watch::Receiver, Mutex, RwLock, Semaphore}; use tokio::sync::{broadcast, watch::Receiver, Mutex, RwLock, Semaphore};
use tracing::{error, info}; use tracing::{error, info};
use tracing_subscriber::{EnvFilter, Registry}; use tracing_subscriber::{EnvFilter, Registry};
use url::Url;
use crate::{services, Config, Result}; use crate::{services, Config, Result};
@ -314,13 +315,13 @@ impl Service<'_> {
pub fn ip_range_denylist(&self) -> &[String] { &self.config.ip_range_denylist } pub fn ip_range_denylist(&self) -> &[String] { &self.config.ip_range_denylist }
pub fn well_known_support_page(&self) -> &Option<String> { &self.config.well_known_support_page } pub fn well_known_support_page(&self) -> &Option<Url> { &self.config.well_known.support_page }
pub fn well_known_support_role(&self) -> &Option<ContactRole> { &self.config.well_known_support_role } pub fn well_known_support_role(&self) -> &Option<ContactRole> { &self.config.well_known.support_role }
pub fn well_known_support_email(&self) -> &Option<String> { &self.config.well_known_support_email } pub fn well_known_support_email(&self) -> &Option<String> { &self.config.well_known.support_email }
pub fn well_known_support_mxid(&self) -> &Option<OwnedUserId> { &self.config.well_known_support_mxid } pub fn well_known_support_mxid(&self) -> &Option<OwnedUserId> { &self.config.well_known.support_mxid }
pub fn block_non_admin_invites(&self) -> bool { self.config.block_non_admin_invites } pub fn block_non_admin_invites(&self) -> bool { self.config.block_non_admin_invites }
@ -401,9 +402,9 @@ impl Service<'_> {
r r
} }
pub fn well_known_client(&self) -> &Option<String> { &self.config.well_known_client } pub fn well_known_client(&self) -> &Option<Url> { &self.config.well_known.client }
pub fn well_known_server(&self) -> &Option<String> { &self.config.well_known_server } pub fn well_known_server(&self) -> &Option<OwnedServerName> { &self.config.well_known.server }
pub fn unix_socket_path(&self) -> &Option<PathBuf> { &self.config.unix_socket_path } pub fn unix_socket_path(&self) -> &Option<PathBuf> { &self.config.unix_socket_path }