hive-matrix-mcp: resolve a bare server name through matrix-sdk's discovery before connecting
This commit is contained in:
parent
b8851743e8
commit
e0a5f737ac
1 changed files with 67 additions and 1 deletions
|
|
@ -85,6 +85,8 @@ pub async fn build_and_restore(
|
|||
state_dir: &Path,
|
||||
is_primary: bool,
|
||||
) -> Result<Client> {
|
||||
let resolved = resolve_homeserver(homeserver).await?;
|
||||
let homeserver = resolved.as_str();
|
||||
let Some((user_id, device_id)) = whoami(homeserver, token.expose()).await? else {
|
||||
// Homeserver rejected our token — stale session after a homeserver
|
||||
// state wipe or token expiry. Discard it so we don't loop on it
|
||||
|
|
@ -167,6 +169,29 @@ pub async fn build_and_restore(
|
|||
Ok(client)
|
||||
}
|
||||
|
||||
/// The homeserver URL a configured `homeserver` names. A value with a scheme
|
||||
/// already is the URL and is used as given, with no lookup; a bare server name
|
||||
/// goes through [`discover`].
|
||||
async fn resolve_homeserver(homeserver: &str) -> Result<String> {
|
||||
if homeserver.contains("://") {
|
||||
return Ok(homeserver.to_owned());
|
||||
}
|
||||
discover(homeserver).await
|
||||
}
|
||||
|
||||
/// `homeserver`'s URL by matrix-sdk's own discovery
|
||||
/// (`.well-known/matrix/client`). The whoami probe runs before the real client
|
||||
/// exists and needs the same URL, so a store-less client is built here just to
|
||||
/// ask it.
|
||||
async fn discover(homeserver: &str) -> Result<String> {
|
||||
let probe = Client::builder()
|
||||
.server_name_or_homeserver_url(homeserver)
|
||||
.build()
|
||||
.await
|
||||
.with_context(|| format!("discover the matrix homeserver for {homeserver}"))?;
|
||||
Ok(probe.homeserver().as_str().trim_end_matches('/').to_owned())
|
||||
}
|
||||
|
||||
/// Whoami probe — used at startup to recover the `user_id` + `device_id` the
|
||||
/// registration response carried but hive-c0re didn't persist alongside the
|
||||
/// access token. Cheaper than teaching the hive-c0re side to persist them,
|
||||
|
|
@ -438,6 +463,45 @@ mod tests {
|
|||
|
||||
use super::*;
|
||||
|
||||
#[tokio::test]
|
||||
async fn a_server_name_resolves_through_its_well_known() {
|
||||
use wiremock::matchers::{method, path};
|
||||
use wiremock::{Mock, MockServer, ResponseTemplate};
|
||||
|
||||
// The homeserver itself: answers the client-versions probe.
|
||||
let homeserver = MockServer::start().await;
|
||||
Mock::given(method("GET"))
|
||||
.and(path("/_matrix/client/versions"))
|
||||
.respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
|
||||
"versions": ["v1.11"],
|
||||
})))
|
||||
.mount(&homeserver)
|
||||
.await;
|
||||
// A bare name (here the loopback host:port) whose well-known names it.
|
||||
let name = MockServer::start().await;
|
||||
Mock::given(method("GET"))
|
||||
.and(path("/.well-known/matrix/client"))
|
||||
.respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
|
||||
"m.homeserver": { "base_url": format!("{}/", homeserver.uri()) },
|
||||
})))
|
||||
.mount(&name)
|
||||
.await;
|
||||
|
||||
// `discover` takes a URL here because the sdk only tries https for a
|
||||
// bare name, which a loopback stub cannot serve.
|
||||
assert_eq!(
|
||||
discover(&name.uri()).await.expect("discovered"),
|
||||
homeserver.uri()
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn a_homeserver_url_is_used_as_given_without_a_lookup() {
|
||||
// Nothing listens here: any lookup would fail.
|
||||
let url = "http://127.0.0.1:1";
|
||||
assert_eq!(resolve_homeserver(url).await.expect("as given"), url);
|
||||
}
|
||||
|
||||
/// An account as the homeserver and this device describe it, recording
|
||||
/// which of the two writes [`recover_or_enable`] made.
|
||||
#[derive(Default)]
|
||||
|
|
|
|||
Loading…
Reference in a new issue