From 1cd4d50181585d664ca7337e3f48ea00a1b1f5c6 Mon Sep 17 00:00:00 2001 From: Ryan W Date: Fri, 10 Sep 2021 10:05:31 +0100 Subject: [PATCH] Added .well-known/matrix/server endpoint (#1988) * Added .well-known/matrix/server endpoint Signed-off-by: Ryan Whittington * Replaced tabs with spaces Signed-off-by: Ryan Whittington --- build/docker/config/dendrite-config.yaml | 4 ++++ build/gobind-pinecone/monolith.go | 1 + build/gobind-yggdrasil/monolith.go | 1 + cmd/dendrite-demo-libp2p/main.go | 1 + cmd/dendrite-demo-pinecone/main.go | 1 + cmd/dendrite-demo-yggdrasil/main.go | 1 + cmd/dendrite-monolith-server/main.go | 1 + .../personalities/federationapi.go | 2 +- cmd/dendritejs-pinecone/main.go | 1 + dendrite-config.yaml | 4 ++++ federationapi/federationapi.go | 4 ++-- federationapi/federationapi_test.go | 2 +- federationapi/routing/routing.go | 18 +++++++++++++++++- internal/httputil/paths.go | 1 + setup/base.go | 3 +++ setup/config/config_global.go | 3 +++ setup/config/config_test.go | 1 + setup/monolith.go | 4 ++-- 18 files changed, 46 insertions(+), 7 deletions(-) diff --git a/build/docker/config/dendrite-config.yaml b/build/docker/config/dendrite-config.yaml index ffcf6a451..bc5d66994 100644 --- a/build/docker/config/dendrite-config.yaml +++ b/build/docker/config/dendrite-config.yaml @@ -52,6 +52,10 @@ global: # considered valid by other homeservers. key_validity_period: 168h0m0s + # The server name to delegate server-server communications to, with optional port + # e.g. localhost:443 + well_known_server_name: "" + # Lists of domains that the server will trust as identity servers to verify third # party identifiers such as phone numbers and email addresses. trusted_third_party_id_servers: diff --git a/build/gobind-pinecone/monolith.go b/build/gobind-pinecone/monolith.go index 310e3a04a..3372b6def 100644 --- a/build/gobind-pinecone/monolith.go +++ b/build/gobind-pinecone/monolith.go @@ -332,6 +332,7 @@ func (m *DendriteMonolith) Start() { base.PublicClientAPIMux, base.PublicFederationAPIMux, base.PublicKeyAPIMux, + base.PublicWellKnownAPIMux, base.PublicMediaAPIMux, base.SynapseAdminMux, ) diff --git a/build/gobind-yggdrasil/monolith.go b/build/gobind-yggdrasil/monolith.go index 6fc5f244b..693e92368 100644 --- a/build/gobind-yggdrasil/monolith.go +++ b/build/gobind-yggdrasil/monolith.go @@ -156,6 +156,7 @@ func (m *DendriteMonolith) Start() { base.PublicClientAPIMux, base.PublicFederationAPIMux, base.PublicKeyAPIMux, + base.PublicWellKnownAPIMux, base.PublicMediaAPIMux, base.SynapseAdminMux, ) diff --git a/cmd/dendrite-demo-libp2p/main.go b/cmd/dendrite-demo-libp2p/main.go index 4caa617f1..7606e418c 100644 --- a/cmd/dendrite-demo-libp2p/main.go +++ b/cmd/dendrite-demo-libp2p/main.go @@ -198,6 +198,7 @@ func main() { base.Base.PublicClientAPIMux, base.Base.PublicFederationAPIMux, base.Base.PublicKeyAPIMux, + base.Base.PublicWellKnownAPIMux, base.Base.PublicMediaAPIMux, base.Base.SynapseAdminMux, ) diff --git a/cmd/dendrite-demo-pinecone/main.go b/cmd/dendrite-demo-pinecone/main.go index e919e7494..5f9146804 100644 --- a/cmd/dendrite-demo-pinecone/main.go +++ b/cmd/dendrite-demo-pinecone/main.go @@ -210,6 +210,7 @@ func main() { base.PublicClientAPIMux, base.PublicFederationAPIMux, base.PublicKeyAPIMux, + base.PublicWellKnownAPIMux, base.PublicMediaAPIMux, base.SynapseAdminMux, ) diff --git a/cmd/dendrite-demo-yggdrasil/main.go b/cmd/dendrite-demo-yggdrasil/main.go index dbdb9a76a..1b5e1a51a 100644 --- a/cmd/dendrite-demo-yggdrasil/main.go +++ b/cmd/dendrite-demo-yggdrasil/main.go @@ -145,6 +145,7 @@ func main() { base.PublicClientAPIMux, base.PublicFederationAPIMux, base.PublicKeyAPIMux, + base.PublicWellKnownAPIMux, base.PublicMediaAPIMux, base.SynapseAdminMux, ) diff --git a/cmd/dendrite-monolith-server/main.go b/cmd/dendrite-monolith-server/main.go index 4d8e2ee15..ec8751df3 100644 --- a/cmd/dendrite-monolith-server/main.go +++ b/cmd/dendrite-monolith-server/main.go @@ -161,6 +161,7 @@ func main() { base.PublicClientAPIMux, base.PublicFederationAPIMux, base.PublicKeyAPIMux, + base.PublicWellKnownAPIMux, base.PublicMediaAPIMux, base.SynapseAdminMux, ) diff --git a/cmd/dendrite-polylith-multi/personalities/federationapi.go b/cmd/dendrite-polylith-multi/personalities/federationapi.go index 5ff085282..5488fbf32 100644 --- a/cmd/dendrite-polylith-multi/personalities/federationapi.go +++ b/cmd/dendrite-polylith-multi/personalities/federationapi.go @@ -30,7 +30,7 @@ func FederationAPI(base *setup.BaseDendrite, cfg *config.Dendrite) { keyAPI := base.KeyServerHTTPClient() federationapi.AddPublicRoutes( - base.PublicFederationAPIMux, base.PublicKeyAPIMux, + base.PublicFederationAPIMux, base.PublicKeyAPIMux, base.PublicWellKnownAPIMux, &base.Cfg.FederationAPI, userAPI, federation, keyRing, rsAPI, fsAPI, base.EDUServerClient(), keyAPI, &base.Cfg.MSCs, nil, diff --git a/cmd/dendritejs-pinecone/main.go b/cmd/dendritejs-pinecone/main.go index b44c609c6..2b40be438 100644 --- a/cmd/dendritejs-pinecone/main.go +++ b/cmd/dendritejs-pinecone/main.go @@ -221,6 +221,7 @@ func startup() { base.PublicClientAPIMux, base.PublicFederationAPIMux, base.PublicKeyAPIMux, + base.PublicWellKnownAPIMux, base.PublicMediaAPIMux, base.SynapseAdminMux, ) diff --git a/dendrite-config.yaml b/dendrite-config.yaml index 31b830663..5f2a9de2d 100644 --- a/dendrite-config.yaml +++ b/dendrite-config.yaml @@ -54,6 +54,10 @@ global: # considered valid by other homeservers. key_validity_period: 168h0m0s + # The server name to delegate server-server communications to, with optional port + # e.g. localhost:443 + well_known_server_name: "" + # Lists of domains that the server will trust as identity servers to verify third # party identifiers such as phone numbers and email addresses. trusted_third_party_id_servers: diff --git a/federationapi/federationapi.go b/federationapi/federationapi.go index b3297434a..c40d77a62 100644 --- a/federationapi/federationapi.go +++ b/federationapi/federationapi.go @@ -30,7 +30,7 @@ import ( // AddPublicRoutes sets up and registers HTTP handlers on the base API muxes for the FederationAPI component. func AddPublicRoutes( - fedRouter, keyRouter *mux.Router, + fedRouter, keyRouter, wellKnownRouter *mux.Router, cfg *config.FederationAPI, userAPI userapi.UserInternalAPI, federation *gomatrixserverlib.FederationClient, @@ -43,7 +43,7 @@ func AddPublicRoutes( servers federationAPI.ServersInRoomProvider, ) { routing.Setup( - fedRouter, keyRouter, cfg, rsAPI, + fedRouter, keyRouter, wellKnownRouter, cfg, rsAPI, eduAPI, federationSenderAPI, keyRing, federation, userAPI, keyAPI, mscCfg, servers, diff --git a/federationapi/federationapi_test.go b/federationapi/federationapi_test.go index 505a11dae..cb4d81032 100644 --- a/federationapi/federationapi_test.go +++ b/federationapi/federationapi_test.go @@ -31,7 +31,7 @@ func TestRoomsV3URLEscapeDoNot404(t *testing.T) { fsAPI := base.FederationSenderHTTPClient() // TODO: This is pretty fragile, as if anything calls anything on these nils this test will break. // Unfortunately, it makes little sense to instantiate these dependencies when we just want to test routing. - federationapi.AddPublicRoutes(base.PublicFederationAPIMux, base.PublicKeyAPIMux, &cfg.FederationAPI, nil, nil, keyRing, nil, fsAPI, nil, nil, &cfg.MSCs, nil) + federationapi.AddPublicRoutes(base.PublicFederationAPIMux, base.PublicKeyAPIMux, base.PublicWellKnownAPIMux, &cfg.FederationAPI, nil, nil, keyRing, nil, fsAPI, nil, nil, &cfg.MSCs, nil) baseURL, cancel := test.ListenAndServe(t, base.PublicFederationAPIMux, true) defer cancel() serverName := gomatrixserverlib.ServerName(strings.TrimPrefix(baseURL, "https://")) diff --git a/federationapi/routing/routing.go b/federationapi/routing/routing.go index 503e512e6..7446f1fbc 100644 --- a/federationapi/routing/routing.go +++ b/federationapi/routing/routing.go @@ -30,6 +30,7 @@ import ( userapi "github.com/matrix-org/dendrite/userapi/api" "github.com/matrix-org/gomatrixserverlib" "github.com/matrix-org/util" + "github.com/sirupsen/logrus" ) // Setup registers HTTP handlers with the given ServeMux. @@ -41,7 +42,7 @@ import ( // applied: // nolint: gocyclo func Setup( - fedMux, keyMux *mux.Router, + fedMux, keyMux, wkMux *mux.Router, cfg *config.FederationAPI, rsAPI roomserverAPI.RoomserverInternalAPI, eduAPI eduserverAPI.EDUServerInputAPI, @@ -85,6 +86,21 @@ func Setup( return NotaryKeys(req, cfg, fsAPI, pkReq) }) + if cfg.Matrix.WellKnownServerName != "" { + logrus.Infof("Setting m.server as %s at /.well-known/matrix/server", cfg.Matrix.WellKnownServerName) + wkMux.Handle("/server", httputil.MakeExternalAPI("wellknown", func(req *http.Request) util.JSONResponse { + return util.JSONResponse{ + Code: http.StatusOK, + JSON: struct { + ServerName string `json:"m.server"` + }{ + ServerName: cfg.Matrix.WellKnownServerName, + }, + } + }), + ).Methods(http.MethodGet, http.MethodOptions) + } + // Ignore the {keyID} argument as we only have a single server key so we always // return that key. // Even if we had more than one server key, we would probably still ignore the diff --git a/internal/httputil/paths.go b/internal/httputil/paths.go index b0f4b8cb8..a1009fc2e 100644 --- a/internal/httputil/paths.go +++ b/internal/httputil/paths.go @@ -19,5 +19,6 @@ const ( PublicFederationPathPrefix = "/_matrix/federation/" PublicKeyPathPrefix = "/_matrix/key/" PublicMediaPathPrefix = "/_matrix/media/" + PublicWellKnownPrefix = "/.well-known/matrix/" InternalPathPrefix = "/api/" ) diff --git a/setup/base.go b/setup/base.go index 1a52d1c26..d4acdbfb9 100644 --- a/setup/base.go +++ b/setup/base.go @@ -76,6 +76,7 @@ type BaseDendrite struct { PublicFederationAPIMux *mux.Router PublicKeyAPIMux *mux.Router PublicMediaAPIMux *mux.Router + PublicWellKnownAPIMux *mux.Router InternalAPIMux *mux.Router SynapseAdminMux *mux.Router UseHTTPAPIs bool @@ -198,6 +199,7 @@ func NewBaseDendrite(cfg *config.Dendrite, componentName string, useHTTPAPIs boo PublicFederationAPIMux: mux.NewRouter().SkipClean(true).PathPrefix(httputil.PublicFederationPathPrefix).Subrouter().UseEncodedPath(), PublicKeyAPIMux: mux.NewRouter().SkipClean(true).PathPrefix(httputil.PublicKeyPathPrefix).Subrouter().UseEncodedPath(), PublicMediaAPIMux: mux.NewRouter().SkipClean(true).PathPrefix(httputil.PublicMediaPathPrefix).Subrouter().UseEncodedPath(), + PublicWellKnownAPIMux: mux.NewRouter().SkipClean(true).PathPrefix(httputil.PublicWellKnownPrefix).Subrouter().UseEncodedPath(), InternalAPIMux: mux.NewRouter().SkipClean(true).PathPrefix(httputil.InternalPathPrefix).Subrouter().UseEncodedPath(), SynapseAdminMux: mux.NewRouter().SkipClean(true).PathPrefix("/_synapse/").Subrouter().UseEncodedPath(), apiHttpClient: &apiClient, @@ -394,6 +396,7 @@ func (b *BaseDendrite) SetupAndServeHTTP( } externalRouter.PathPrefix("/_synapse/").Handler(b.SynapseAdminMux) externalRouter.PathPrefix(httputil.PublicMediaPathPrefix).Handler(b.PublicMediaAPIMux) + externalRouter.PathPrefix(httputil.PublicWellKnownPrefix).Handler(b.PublicWellKnownAPIMux) if internalAddr != NoListener && internalAddr != externalAddr { go func() { diff --git a/setup/config/config_global.go b/setup/config/config_global.go index 90a92f2bc..d5d9f7f51 100644 --- a/setup/config/config_global.go +++ b/setup/config/config_global.go @@ -34,6 +34,9 @@ type Global struct { // Defaults to 24 hours. KeyValidityPeriod time.Duration `yaml:"key_validity_period"` + // The server name to delegate server-server communications to, with optional port + WellKnownServerName string `yaml:"well_known_server_name"` + // Disables federation. Dendrite will not be able to make any outbound HTTP requests // to other servers and the federation API will not be exposed. DisableFederation bool `yaml:"disable_federation"` diff --git a/setup/config/config_test.go b/setup/config/config_test.go index 4107b6845..5c51a363f 100644 --- a/setup/config/config_test.go +++ b/setup/config/config_test.go @@ -39,6 +39,7 @@ global: private_key: matrix_key.pem key_id: ed25519:auto key_validity_period: 168h0m0s + well_known_server_name: "localhost:443" trusted_third_party_id_servers: - matrix.org - vector.im diff --git a/setup/monolith.go b/setup/monolith.go index 5ceb4ed30..a77cdd56a 100644 --- a/setup/monolith.go +++ b/setup/monolith.go @@ -57,7 +57,7 @@ type Monolith struct { } // AddAllPublicRoutes attaches all public paths to the given router -func (m *Monolith) AddAllPublicRoutes(process *process.ProcessContext, csMux, ssMux, keyMux, mediaMux, synapseMux *mux.Router) { +func (m *Monolith) AddAllPublicRoutes(process *process.ProcessContext, csMux, ssMux, keyMux, wkMux, mediaMux, synapseMux *mux.Router) { clientapi.AddPublicRoutes( csMux, synapseMux, &m.Config.ClientAPI, m.AccountDB, m.FedClient, m.RoomserverAPI, @@ -66,7 +66,7 @@ func (m *Monolith) AddAllPublicRoutes(process *process.ProcessContext, csMux, ss &m.Config.MSCs, ) federationapi.AddPublicRoutes( - ssMux, keyMux, &m.Config.FederationAPI, m.UserAPI, m.FedClient, + ssMux, keyMux, wkMux, &m.Config.FederationAPI, m.UserAPI, m.FedClient, m.KeyRing, m.RoomserverAPI, m.FederationSenderAPI, m.EDUInternalAPI, m.KeyAPI, &m.Config.MSCs, nil, )