Skip to content
This repository has been archived by the owner on Nov 25, 2024. It is now read-only.

Added .well-known/matrix/server endpoint #1988

Merged
merged 3 commits into from
Sep 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions build/docker/config/dendrite-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions build/gobind-pinecone/monolith.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,7 @@ func (m *DendriteMonolith) Start() {
base.PublicClientAPIMux,
base.PublicFederationAPIMux,
base.PublicKeyAPIMux,
base.PublicWellKnownAPIMux,
base.PublicMediaAPIMux,
base.SynapseAdminMux,
)
Expand Down
1 change: 1 addition & 0 deletions build/gobind-yggdrasil/monolith.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ func (m *DendriteMonolith) Start() {
base.PublicClientAPIMux,
base.PublicFederationAPIMux,
base.PublicKeyAPIMux,
base.PublicWellKnownAPIMux,
base.PublicMediaAPIMux,
base.SynapseAdminMux,
)
Expand Down
1 change: 1 addition & 0 deletions cmd/dendrite-demo-libp2p/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ func main() {
base.Base.PublicClientAPIMux,
base.Base.PublicFederationAPIMux,
base.Base.PublicKeyAPIMux,
base.Base.PublicWellKnownAPIMux,
base.Base.PublicMediaAPIMux,
base.Base.SynapseAdminMux,
)
Expand Down
1 change: 1 addition & 0 deletions cmd/dendrite-demo-pinecone/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ func main() {
base.PublicClientAPIMux,
base.PublicFederationAPIMux,
base.PublicKeyAPIMux,
base.PublicWellKnownAPIMux,
base.PublicMediaAPIMux,
base.SynapseAdminMux,
)
Expand Down
1 change: 1 addition & 0 deletions cmd/dendrite-demo-yggdrasil/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ func main() {
base.PublicClientAPIMux,
base.PublicFederationAPIMux,
base.PublicKeyAPIMux,
base.PublicWellKnownAPIMux,
base.PublicMediaAPIMux,
base.SynapseAdminMux,
)
Expand Down
1 change: 1 addition & 0 deletions cmd/dendrite-monolith-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ func main() {
base.PublicClientAPIMux,
base.PublicFederationAPIMux,
base.PublicKeyAPIMux,
base.PublicWellKnownAPIMux,
base.PublicMediaAPIMux,
base.SynapseAdminMux,
)
Expand Down
2 changes: 1 addition & 1 deletion cmd/dendrite-polylith-multi/personalities/federationapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions cmd/dendritejs-pinecone/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ func startup() {
base.PublicClientAPIMux,
base.PublicFederationAPIMux,
base.PublicKeyAPIMux,
base.PublicWellKnownAPIMux,
base.PublicMediaAPIMux,
base.SynapseAdminMux,
)
Expand Down
4 changes: 4 additions & 0 deletions dendrite-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions federationapi/federationapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion federationapi/federationapi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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://"))
Expand Down
18 changes: 17 additions & 1 deletion federationapi/routing/routing.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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,
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions internal/httputil/paths.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ const (
PublicFederationPathPrefix = "/_matrix/federation/"
PublicKeyPathPrefix = "/_matrix/key/"
PublicMediaPathPrefix = "/_matrix/media/"
PublicWellKnownPrefix = "/.well-known/matrix/"
InternalPathPrefix = "/api/"
)
3 changes: 3 additions & 0 deletions setup/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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() {
Expand Down
3 changes: 3 additions & 0 deletions setup/config/config_global.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
1 change: 1 addition & 0 deletions setup/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions setup/monolith.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
)
Expand Down