1
0
Fork 0
mirror of https://gitlab.com/famedly/conduit.git synced 2025-04-22 14:10:16 +03:00

Merge branch 'appservice-ping' into 'next'

feat(appservice): pinging

See merge request 
This commit is contained in:
Matthias Ahouansou 2025-03-08 14:39:52 +00:00
commit 244dbbe6dd
11 changed files with 126 additions and 45 deletions

40
Cargo.lock generated
View file

@ -2190,7 +2190,7 @@ dependencies = [
[[package]]
name = "ruma"
version = "0.12.1"
source = "git+https://github.com/ruma/ruma.git#9e6099161d4ed295e694fa0d5de2b28a23840a4f"
source = "git+https://github.com/ruma/ruma.git#7c03b554c0d3e8b0a27fc0fadba36a4d636935d3"
dependencies = [
"assign",
"js_int",
@ -2201,7 +2201,6 @@ dependencies = [
"ruma-events",
"ruma-federation-api",
"ruma-push-gateway-api",
"ruma-server-util",
"ruma-signatures",
"ruma-state-res",
"web-time",
@ -2210,7 +2209,7 @@ dependencies = [
[[package]]
name = "ruma-appservice-api"
version = "0.12.1"
source = "git+https://github.com/ruma/ruma.git#9e6099161d4ed295e694fa0d5de2b28a23840a4f"
source = "git+https://github.com/ruma/ruma.git#7c03b554c0d3e8b0a27fc0fadba36a4d636935d3"
dependencies = [
"js_int",
"ruma-common",
@ -2222,7 +2221,7 @@ dependencies = [
[[package]]
name = "ruma-client-api"
version = "0.20.1"
source = "git+https://github.com/ruma/ruma.git#9e6099161d4ed295e694fa0d5de2b28a23840a4f"
source = "git+https://github.com/ruma/ruma.git#7c03b554c0d3e8b0a27fc0fadba36a4d636935d3"
dependencies = [
"as_variant",
"assign",
@ -2245,7 +2244,7 @@ dependencies = [
[[package]]
name = "ruma-common"
version = "0.15.1"
source = "git+https://github.com/ruma/ruma.git#9e6099161d4ed295e694fa0d5de2b28a23840a4f"
source = "git+https://github.com/ruma/ruma.git#7c03b554c0d3e8b0a27fc0fadba36a4d636935d3"
dependencies = [
"as_variant",
"base64 0.22.1",
@ -2276,7 +2275,7 @@ dependencies = [
[[package]]
name = "ruma-events"
version = "0.30.1"
source = "git+https://github.com/ruma/ruma.git#9e6099161d4ed295e694fa0d5de2b28a23840a4f"
source = "git+https://github.com/ruma/ruma.git#7c03b554c0d3e8b0a27fc0fadba36a4d636935d3"
dependencies = [
"as_variant",
"indexmap 2.2.6",
@ -2299,10 +2298,12 @@ dependencies = [
[[package]]
name = "ruma-federation-api"
version = "0.11.0"
source = "git+https://github.com/ruma/ruma.git#9e6099161d4ed295e694fa0d5de2b28a23840a4f"
source = "git+https://github.com/ruma/ruma.git#7c03b554c0d3e8b0a27fc0fadba36a4d636935d3"
dependencies = [
"bytes",
"headers",
"http 1.1.0",
"http-auth",
"httparse",
"js_int",
"memchr",
@ -2312,12 +2313,14 @@ dependencies = [
"ruma-events",
"serde",
"serde_json",
"thiserror 2.0.11",
"tracing",
]
[[package]]
name = "ruma-identifiers-validation"
version = "0.10.1"
source = "git+https://github.com/ruma/ruma.git#9e6099161d4ed295e694fa0d5de2b28a23840a4f"
source = "git+https://github.com/ruma/ruma.git#7c03b554c0d3e8b0a27fc0fadba36a4d636935d3"
dependencies = [
"js_int",
"thiserror 2.0.11",
@ -2326,7 +2329,7 @@ dependencies = [
[[package]]
name = "ruma-macros"
version = "0.15.1"
source = "git+https://github.com/ruma/ruma.git#9e6099161d4ed295e694fa0d5de2b28a23840a4f"
source = "git+https://github.com/ruma/ruma.git#7c03b554c0d3e8b0a27fc0fadba36a4d636935d3"
dependencies = [
"cfg-if",
"proc-macro-crate",
@ -2341,7 +2344,7 @@ dependencies = [
[[package]]
name = "ruma-push-gateway-api"
version = "0.11.0"
source = "git+https://github.com/ruma/ruma.git#9e6099161d4ed295e694fa0d5de2b28a23840a4f"
source = "git+https://github.com/ruma/ruma.git#7c03b554c0d3e8b0a27fc0fadba36a4d636935d3"
dependencies = [
"js_int",
"ruma-common",
@ -2350,23 +2353,10 @@ dependencies = [
"serde_json",
]
[[package]]
name = "ruma-server-util"
version = "0.5.0"
source = "git+https://github.com/ruma/ruma.git#9e6099161d4ed295e694fa0d5de2b28a23840a4f"
dependencies = [
"headers",
"http 1.1.0",
"http-auth",
"ruma-common",
"thiserror 2.0.11",
"tracing",
]
[[package]]
name = "ruma-signatures"
version = "0.17.0"
source = "git+https://github.com/ruma/ruma.git#9e6099161d4ed295e694fa0d5de2b28a23840a4f"
source = "git+https://github.com/ruma/ruma.git#7c03b554c0d3e8b0a27fc0fadba36a4d636935d3"
dependencies = [
"base64 0.22.1",
"ed25519-dalek",
@ -2382,7 +2372,7 @@ dependencies = [
[[package]]
name = "ruma-state-res"
version = "0.13.0"
source = "git+https://github.com/ruma/ruma.git#9e6099161d4ed295e694fa0d5de2b28a23840a4f"
source = "git+https://github.com/ruma/ruma.git#7c03b554c0d3e8b0a27fc0fadba36a4d636935d3"
dependencies = [
"js_int",
"ruma-common",

View file

@ -160,7 +160,6 @@ features = [
"push-gateway-api-c",
"rand",
"ring-compat",
"server-util",
"state-res",
"unstable-msc2448",
"unstable-msc3575",

View file

@ -1,8 +1,6 @@
use crate::{services, utils, Error, Result};
use crate::{services, utils, Error, Result, MATRIX_VERSIONS};
use bytes::BytesMut;
use ruma::api::{
appservice::Registration, IncomingResponse, MatrixVersion, OutgoingRequest, SendAccessToken,
};
use ruma::api::{appservice::Registration, IncomingResponse, OutgoingRequest, SendAccessToken};
use std::{fmt::Debug, mem, time::Duration};
use tracing::warn;
@ -30,7 +28,7 @@ where
.try_into_http_request::<BytesMut>(
&destination,
SendAccessToken::IfRequired(hs_token),
&[MatrixVersion::V1_0],
MATRIX_VERSIONS,
)
.unwrap()
.map(|body| body.freeze());

View file

@ -0,0 +1,80 @@
use std::time::Instant;
use ruma::api::{
appservice::ping::send_ping,
client::{appservice::request_ping, error::ErrorKind},
};
use crate::{api::appservice_server, Error, Result, Ruma};
/// # `POST /_matrix/client/v1/appservice/{appserviceId}/ping`
///
/// Allows an appservice to check whether the server and
/// appservice can connect, and how fast their connection is
pub async fn ping_appservice_route(
body: Ruma<request_ping::v1::Request>,
) -> Result<request_ping::v1::Response> {
let Ruma::<request_ping::v1::Request> {
appservice_info,
body,
..
} = body;
let registration = appservice_info
.expect("Only appservices can call this endpoint")
.registration;
if registration.id != body.appservice_id {
return Err(Error::BadRequest(
ErrorKind::forbidden(),
"Appservice ID specified in path does not match the requesting access token",
));
}
if registration.url.is_some() {
let start = Instant::now();
let response = appservice_server::send_request(
registration,
send_ping::v1::Request {
transaction_id: body.transaction_id,
},
)
.await;
let elapsed = start.elapsed();
if let Err(error) = response {
Err(match error {
Error::ReqwestError { source } => {
if source.is_timeout() {
Error::BadRequest(
ErrorKind::ConnectionTimeout,
"Connection to appservice timed-out",
)
} else if let Some(status_code) = source.status() {
Error::BadRequest(
ErrorKind::BadStatus {
status: Some(status_code),
body: Some(source.to_string()),
},
"Ping returned error status",
)
} else {
Error::BadRequest(ErrorKind::ConnectionFailed, "Failed to ping appservice")
}
}
Error::BadServerResponse(_) => Error::BadRequest(
ErrorKind::ConnectionFailed,
"Recieved invalid response from appservice",
),
e => e,
})
} else {
Ok(request_ping::v1::Response::new(elapsed))
}
} else {
Err(Error::BadRequest(
ErrorKind::UrlNotSet,
"Appservice doesn't have a URL configured",
))
}
}

View file

@ -1,5 +1,6 @@
mod account;
mod alias;
mod appservice;
mod backup;
mod capabilities;
mod config;
@ -37,6 +38,7 @@ mod well_known;
pub use account::*;
pub use alias::*;
pub use appservice::*;
pub use backup::*;
pub use capabilities::*;
pub use config::*;

View file

@ -15,8 +15,10 @@ use axum_extra::{
use bytes::{BufMut, BytesMut};
use http::{Request, StatusCode};
use ruma::{
api::{client::error::ErrorKind, AuthScheme, IncomingRequest, OutgoingResponse},
server_util::authorization::XMatrix,
api::{
client::error::ErrorKind, federation::authentication::XMatrix, AuthScheme, IncomingRequest,
OutgoingResponse,
},
CanonicalJsonValue, MilliSecondsSinceUnixEpoch, OwnedDeviceId, OwnedUserId, UserId,
};
use serde::Deserialize;
@ -110,7 +112,10 @@ where
));
}
}
(AuthScheme::AccessToken, Token::Appservice(info)) => {
(
AuthScheme::AccessToken | AuthScheme::AppserviceToken,
Token::Appservice(info),
) => {
let user_id = query_params
.user_id
.map_or_else(
@ -144,11 +149,11 @@ where
}
(
AuthScheme::None
| AuthScheme::AppserviceToken
| AuthScheme::AppserviceTokenOptional
| AuthScheme::AccessTokenOptional,
Token::Appservice(info),
) => (None, None, None, Some(*info)),
(AuthScheme::AccessToken, Token::None) => {
(AuthScheme::AppserviceToken | AuthScheme::AccessToken, Token::None) => {
return Err(Error::BadRequest(
ErrorKind::MissingToken,
"Missing access token.",
@ -287,7 +292,7 @@ where
}
(
AuthScheme::None
| AuthScheme::AppserviceToken
| AuthScheme::AppserviceTokenOptional
| AuthScheme::AccessTokenOptional,
Token::None,
) => (None, None, None, None),
@ -297,7 +302,10 @@ where
"Only server signatures should be used on this endpoint.",
));
}
(AuthScheme::AppserviceToken, Token::User(_)) => {
(
AuthScheme::AppserviceToken | AuthScheme::AppserviceTokenOptional,
Token::User(_),
) => {
return Err(Error::BadRequest(
ErrorKind::Unauthorized,
"Only appservice access tokens should be used on this endpoint.",

View file

@ -7,7 +7,7 @@ use crate::{
media::FileMeta,
pdu::{gen_event_id_canonical_json, PduBuilder},
},
services, utils, Error, PduEvent, Result, Ruma,
services, utils, Error, PduEvent, Result, Ruma, MATRIX_VERSIONS,
};
use axum::{response::IntoResponse, Json};
use axum_extra::headers::{CacheControl, Header};
@ -44,8 +44,7 @@ use ruma::{
send_transaction_message,
},
},
EndpointError, IncomingResponse, MatrixVersion, OutgoingRequest, OutgoingResponse,
SendAccessToken,
EndpointError, IncomingResponse, OutgoingRequest, OutgoingResponse, SendAccessToken,
},
directory::{Filter, RoomNetwork},
events::{
@ -215,7 +214,7 @@ where
.try_into_http_request::<Vec<u8>>(
&actual_destination_str,
SendAccessToken::IfRequired(""),
&[MatrixVersion::V1_11],
MATRIX_VERSIONS,
)
.map_err(|e| {
warn!(

View file

@ -13,10 +13,12 @@ use std::sync::RwLock;
pub use api::ruma_wrapper::{Ruma, RumaResponse};
pub use config::Config;
pub use database::KeyValueDatabase;
use ruma::api::MatrixVersion;
pub use service::{pdu::PduEvent, Services};
pub use utils::error::{Error, Result};
pub static SERVICES: RwLock<Option<&'static Services>> = RwLock::new(None);
pub const MATRIX_VERSIONS: &[MatrixVersion] = &[MatrixVersion::V1_13];
pub fn services() -> &'static Services {
SERVICES

View file

@ -279,6 +279,7 @@ async fn unrecognized_method(
fn routes(config: &Config) -> Router {
let router = Router::new()
.ruma_route(client_server::ping_appservice_route)
.ruma_route(client_server::get_supported_versions_route)
.ruma_route(client_server::get_register_available_route)
.ruma_route(client_server::register_route)

View file

@ -2,7 +2,7 @@ mod data;
pub use data::Data;
use ruma::{events::AnySyncTimelineEvent, push::PushConditionPowerLevelsCtx};
use crate::{services, Error, PduEvent, Result};
use crate::{services, Error, PduEvent, Result, MATRIX_VERSIONS};
use bytes::BytesMut;
use ruma::{
api::{
@ -11,7 +11,7 @@ use ruma::{
self,
v1::{Device, Notification, NotificationCounts, NotificationPriority},
},
IncomingResponse, MatrixVersion, OutgoingRequest, SendAccessToken,
IncomingResponse, OutgoingRequest, SendAccessToken,
},
events::{room::power_levels::RoomPowerLevelsEventContent, StateEventType, TimelineEventType},
push::{Action, PushConditionRoomCtx, PushFormat, Ruleset, Tweak},
@ -58,7 +58,7 @@ impl Service {
.try_into_http_request::<BytesMut>(
&destination,
SendAccessToken::IfRequired(""),
&[MatrixVersion::V1_0],
MATRIX_VERSIONS,
)
.map_err(|e| {
warn!("Failed to find destination {}: {}", destination, e);

View file

@ -122,6 +122,8 @@ impl Error {
LimitExceeded { .. } => StatusCode::TOO_MANY_REQUESTS,
UserDeactivated => StatusCode::FORBIDDEN,
TooLarge => StatusCode::PAYLOAD_TOO_LARGE,
ConnectionTimeout => StatusCode::GATEWAY_TIMEOUT,
BadStatus { .. } | ConnectionFailed => StatusCode::BAD_GATEWAY,
_ => StatusCode::BAD_REQUEST,
},
),