2017-05-19 17:06:41 +02:00
|
|
|
// Copyright 2017 Vector Creations Ltd
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package routing
|
|
|
|
|
|
|
|
import (
|
2022-04-28 12:45:56 +02:00
|
|
|
"context"
|
2024-08-16 12:37:59 +02:00
|
|
|
"encoding/json"
|
2022-04-28 12:45:56 +02:00
|
|
|
"fmt"
|
2017-08-03 16:10:39 +02:00
|
|
|
"net/http"
|
2022-05-06 13:39:26 +02:00
|
|
|
"sync"
|
|
|
|
"time"
|
2017-08-03 16:10:39 +02:00
|
|
|
|
2022-05-06 13:39:26 +02:00
|
|
|
"github.com/getsentry/sentry-go"
|
2017-05-19 17:06:41 +02:00
|
|
|
"github.com/gorilla/mux"
|
2022-05-06 13:39:26 +02:00
|
|
|
fedInternal "github.com/matrix-org/dendrite/federationapi/internal"
|
2022-03-29 14:14:35 +02:00
|
|
|
"github.com/matrix-org/dendrite/federationapi/producers"
|
2021-03-30 11:01:32 +02:00
|
|
|
"github.com/matrix-org/dendrite/internal"
|
2020-06-12 15:55:57 +02:00
|
|
|
"github.com/matrix-org/dendrite/internal/httputil"
|
2022-04-28 12:45:56 +02:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/api"
|
2018-08-08 17:17:09 +02:00
|
|
|
roomserverAPI "github.com/matrix-org/dendrite/roomserver/api"
|
2020-12-02 18:41:00 +01:00
|
|
|
"github.com/matrix-org/dendrite/setup/config"
|
2020-06-16 15:53:19 +02:00
|
|
|
userapi "github.com/matrix-org/dendrite/userapi/api"
|
2017-06-07 15:32:53 +02:00
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
2023-04-19 16:50:33 +02:00
|
|
|
"github.com/matrix-org/gomatrixserverlib/fclient"
|
|
|
|
"github.com/matrix-org/gomatrixserverlib/spec"
|
2017-05-19 17:06:41 +02:00
|
|
|
"github.com/matrix-org/util"
|
2022-04-08 12:24:40 +02:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2021-09-10 11:05:31 +02:00
|
|
|
"github.com/sirupsen/logrus"
|
2017-05-19 17:06:41 +02:00
|
|
|
)
|
|
|
|
|
2023-01-23 18:55:12 +01:00
|
|
|
const (
|
|
|
|
SendRouteName = "Send"
|
|
|
|
QueryDirectoryRouteName = "QueryDirectory"
|
|
|
|
QueryProfileRouteName = "QueryProfile"
|
|
|
|
)
|
|
|
|
|
2017-05-19 17:06:41 +02:00
|
|
|
// Setup registers HTTP handlers with the given ServeMux.
|
2020-06-04 12:14:08 +02:00
|
|
|
// The provided publicAPIMux MUST have `UseEncodedPath()` enabled or else routes will incorrectly
|
|
|
|
// path unescape twice (once from the router, once from MakeFedAPI). We need to have this enabled
|
|
|
|
// so we can decode paths like foo/bar%2Fbaz as [foo, bar/baz] - by default it will decode to [foo, bar, baz]
|
2019-07-03 17:38:50 +02:00
|
|
|
//
|
|
|
|
// Due to Setup being used to call many other functions, a gocyclo nolint is
|
|
|
|
// applied:
|
|
|
|
// nolint: gocyclo
|
2017-06-07 15:32:53 +02:00
|
|
|
func Setup(
|
2023-03-22 09:21:32 +01:00
|
|
|
routers httputil.Routers,
|
|
|
|
dendriteCfg *config.Dendrite,
|
2022-05-05 20:30:38 +02:00
|
|
|
rsAPI roomserverAPI.FederationRoomserverAPI,
|
2022-05-06 13:39:26 +02:00
|
|
|
fsAPI *fedInternal.FederationInternalAPI,
|
2020-06-15 17:57:59 +02:00
|
|
|
keys gomatrixserverlib.JSONVerifier,
|
2023-04-24 18:23:25 +02:00
|
|
|
federation fclient.FederationClient,
|
2022-05-05 20:30:38 +02:00
|
|
|
userAPI userapi.FederationUserAPI,
|
2021-01-22 17:08:47 +01:00
|
|
|
mscCfg *config.MSCs,
|
2023-03-22 09:21:32 +01:00
|
|
|
producer *producers.SyncAPIProducer, enableMetrics bool,
|
2017-06-07 15:32:53 +02:00
|
|
|
) {
|
2023-03-22 09:21:32 +01:00
|
|
|
fedMux := routers.Federation
|
|
|
|
keyMux := routers.Keys
|
|
|
|
wkMux := routers.WellKnown
|
|
|
|
cfg := &dendriteCfg.FederationAPI
|
2023-01-20 12:45:56 +01:00
|
|
|
|
2023-03-22 09:21:32 +01:00
|
|
|
if enableMetrics {
|
2023-01-20 12:45:56 +01:00
|
|
|
prometheus.MustRegister(
|
2023-01-23 18:55:12 +01:00
|
|
|
internal.PDUCountTotal, internal.EDUCountTotal,
|
2023-01-20 12:45:56 +01:00
|
|
|
)
|
|
|
|
}
|
2022-04-08 12:24:40 +02:00
|
|
|
|
2020-08-13 13:16:37 +02:00
|
|
|
v2keysmux := keyMux.PathPrefix("/v2").Subrouter()
|
|
|
|
v1fedmux := fedMux.PathPrefix("/v1").Subrouter()
|
|
|
|
v2fedmux := fedMux.PathPrefix("/v2").Subrouter()
|
2023-07-06 17:15:24 +02:00
|
|
|
v3fedmux := fedMux.PathPrefix("/v3").Subrouter()
|
2017-05-19 17:06:41 +02:00
|
|
|
|
2022-05-06 13:39:26 +02:00
|
|
|
wakeup := &FederationWakeups{
|
2020-06-02 13:42:36 +02:00
|
|
|
FsAPI: fsAPI,
|
|
|
|
}
|
|
|
|
|
2020-06-12 15:55:57 +02:00
|
|
|
localKeys := httputil.MakeExternalAPI("localkeys", func(req *http.Request) util.JSONResponse {
|
2023-04-19 16:50:33 +02:00
|
|
|
return LocalKeys(cfg, spec.ServerName(req.Host))
|
2017-05-25 17:08:28 +02:00
|
|
|
})
|
|
|
|
|
2020-09-22 15:40:54 +02:00
|
|
|
notaryKeys := httputil.MakeExternalAPI("notarykeys", func(req *http.Request) util.JSONResponse {
|
|
|
|
vars, err := httputil.URLDecodeMapValues(mux.Vars(req))
|
|
|
|
if err != nil {
|
|
|
|
return util.ErrorResponse(err)
|
|
|
|
}
|
|
|
|
var pkReq *gomatrixserverlib.PublicKeyNotaryLookupRequest
|
2023-04-19 16:50:33 +02:00
|
|
|
serverName := spec.ServerName(vars["serverName"])
|
2020-09-22 15:40:54 +02:00
|
|
|
keyID := gomatrixserverlib.KeyID(vars["keyID"])
|
|
|
|
if serverName != "" && keyID != "" {
|
|
|
|
pkReq = &gomatrixserverlib.PublicKeyNotaryLookupRequest{
|
2023-04-19 16:50:33 +02:00
|
|
|
ServerKeys: map[spec.ServerName]map[gomatrixserverlib.KeyID]gomatrixserverlib.PublicKeyNotaryQueryCriteria{
|
2020-09-22 15:40:54 +02:00
|
|
|
serverName: {
|
|
|
|
keyID: gomatrixserverlib.PublicKeyNotaryQueryCriteria{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NotaryKeys(req, cfg, fsAPI, pkReq)
|
|
|
|
})
|
|
|
|
|
2021-09-10 11:05:31 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2017-05-25 17:08:28 +02:00
|
|
|
// 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
|
|
|
|
// {keyID} argument and always return a response containing all of the keys.
|
2018-03-13 16:55:45 +01:00
|
|
|
v2keysmux.Handle("/server/{keyID}", localKeys).Methods(http.MethodGet)
|
|
|
|
v2keysmux.Handle("/server/", localKeys).Methods(http.MethodGet)
|
2019-08-19 19:01:53 +02:00
|
|
|
v2keysmux.Handle("/server", localKeys).Methods(http.MethodGet)
|
2020-09-22 15:40:54 +02:00
|
|
|
v2keysmux.Handle("/query", notaryKeys).Methods(http.MethodPost)
|
|
|
|
v2keysmux.Handle("/query/{serverName}/{keyID}", notaryKeys).Methods(http.MethodGet)
|
2017-05-19 17:06:41 +02:00
|
|
|
|
2021-03-30 11:01:32 +02:00
|
|
|
mu := internal.NewMutexByRoom()
|
2022-05-06 13:39:26 +02:00
|
|
|
v1fedmux.Handle("/send/{txnID}", MakeFedAPI(
|
2022-10-26 13:59:19 +02:00
|
|
|
"federation_send", cfg.Matrix.ServerName, cfg.Matrix.IsLocalServerName, keys, wakeup,
|
2023-04-19 16:50:33 +02:00
|
|
|
func(httpReq *http.Request, request *fclient.FederationRequest, vars map[string]string) util.JSONResponse {
|
2017-10-11 19:16:53 +02:00
|
|
|
return Send(
|
2017-09-04 14:14:01 +02:00
|
|
|
httpReq, request, gomatrixserverlib.TransactionID(vars["txnID"]),
|
2023-05-03 11:21:27 +02:00
|
|
|
cfg, rsAPI, userAPI, keys, federation, mu, producer,
|
2017-06-07 15:32:53 +02:00
|
|
|
)
|
|
|
|
},
|
2023-01-23 18:55:12 +01:00
|
|
|
)).Methods(http.MethodPut, http.MethodOptions).Name(SendRouteName)
|
2017-08-23 16:13:47 +02:00
|
|
|
|
2022-05-06 13:39:26 +02:00
|
|
|
v1fedmux.Handle("/invite/{roomID}/{eventID}", MakeFedAPI(
|
2022-10-26 13:59:19 +02:00
|
|
|
"federation_invite", cfg.Matrix.ServerName, cfg.Matrix.IsLocalServerName, keys, wakeup,
|
2023-04-19 16:50:33 +02:00
|
|
|
func(httpReq *http.Request, request *fclient.FederationRequest, vars map[string]string) util.JSONResponse {
|
2020-09-04 11:40:58 +02:00
|
|
|
if roomserverAPI.IsServerBannedFromRoom(httpReq.Context(), rsAPI, vars["roomID"], request.Origin()) {
|
2020-08-11 19:19:11 +02:00
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusForbidden,
|
2023-05-10 00:46:49 +02:00
|
|
|
JSON: spec.Forbidden("Forbidden by server ACLs"),
|
2020-08-11 19:19:11 +02:00
|
|
|
}
|
|
|
|
}
|
2023-05-31 18:33:49 +02:00
|
|
|
|
|
|
|
roomID, err := spec.NewRoomID(vars["roomID"])
|
|
|
|
if err != nil {
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
JSON: spec.InvalidParam("Invalid RoomID"),
|
|
|
|
}
|
|
|
|
}
|
2020-08-17 12:40:49 +02:00
|
|
|
return InviteV1(
|
2023-05-31 18:33:49 +02:00
|
|
|
httpReq, request, *roomID, vars["eventID"],
|
2020-06-25 18:07:40 +02:00
|
|
|
cfg, rsAPI, keys,
|
|
|
|
)
|
|
|
|
},
|
|
|
|
)).Methods(http.MethodPut, http.MethodOptions)
|
|
|
|
|
2022-05-06 13:39:26 +02:00
|
|
|
v2fedmux.Handle("/invite/{roomID}/{eventID}", MakeFedAPI(
|
2022-10-26 13:59:19 +02:00
|
|
|
"federation_invite", cfg.Matrix.ServerName, cfg.Matrix.IsLocalServerName, keys, wakeup,
|
2023-04-19 16:50:33 +02:00
|
|
|
func(httpReq *http.Request, request *fclient.FederationRequest, vars map[string]string) util.JSONResponse {
|
2020-09-04 11:40:58 +02:00
|
|
|
if roomserverAPI.IsServerBannedFromRoom(httpReq.Context(), rsAPI, vars["roomID"], request.Origin()) {
|
2020-08-11 19:19:11 +02:00
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusForbidden,
|
2023-05-10 00:46:49 +02:00
|
|
|
JSON: spec.Forbidden("Forbidden by server ACLs"),
|
2020-08-11 19:19:11 +02:00
|
|
|
}
|
|
|
|
}
|
2023-05-31 18:33:49 +02:00
|
|
|
|
|
|
|
roomID, err := spec.NewRoomID(vars["roomID"])
|
|
|
|
if err != nil {
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
JSON: spec.InvalidParam("Invalid RoomID"),
|
|
|
|
}
|
|
|
|
}
|
2020-06-25 18:07:40 +02:00
|
|
|
return InviteV2(
|
2023-05-31 18:33:49 +02:00
|
|
|
httpReq, request, *roomID, vars["eventID"],
|
2020-06-10 13:17:54 +02:00
|
|
|
cfg, rsAPI, keys,
|
2017-08-23 16:13:47 +02:00
|
|
|
)
|
|
|
|
},
|
2018-03-13 16:55:45 +01:00
|
|
|
)).Methods(http.MethodPut, http.MethodOptions)
|
2017-09-06 13:38:22 +02:00
|
|
|
|
2023-07-06 17:15:24 +02:00
|
|
|
v3fedmux.Handle("/invite/{roomID}/{userID}", MakeFedAPI(
|
|
|
|
"federation_invite", cfg.Matrix.ServerName, cfg.Matrix.IsLocalServerName, keys, wakeup,
|
|
|
|
func(httpReq *http.Request, request *fclient.FederationRequest, vars map[string]string) util.JSONResponse {
|
|
|
|
if roomserverAPI.IsServerBannedFromRoom(httpReq.Context(), rsAPI, vars["roomID"], request.Origin()) {
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusForbidden,
|
|
|
|
JSON: spec.Forbidden("Forbidden by server ACLs"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
userID, err := spec.NewUserID(vars["userID"], true)
|
|
|
|
if err != nil {
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
JSON: spec.InvalidParam("Invalid UserID"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
roomID, err := spec.NewRoomID(vars["roomID"])
|
|
|
|
if err != nil {
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
JSON: spec.InvalidParam("Invalid RoomID"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return InviteV3(
|
|
|
|
httpReq, request, *roomID, *userID,
|
|
|
|
cfg, rsAPI, keys,
|
|
|
|
)
|
|
|
|
},
|
|
|
|
)).Methods(http.MethodPut, http.MethodOptions)
|
|
|
|
|
2020-06-12 15:55:57 +02:00
|
|
|
v1fedmux.Handle("/3pid/onbind", httputil.MakeExternalAPI("3pid_onbind",
|
2017-09-11 20:08:24 +02:00
|
|
|
func(req *http.Request) util.JSONResponse {
|
2020-06-16 15:53:19 +02:00
|
|
|
return CreateInvitesFrom3PIDInvites(req, rsAPI, cfg, federation, userAPI)
|
2017-09-08 16:17:12 +02:00
|
|
|
},
|
2018-03-13 16:55:45 +01:00
|
|
|
)).Methods(http.MethodPost, http.MethodOptions)
|
2017-09-08 16:17:12 +02:00
|
|
|
|
2022-05-06 13:39:26 +02:00
|
|
|
v1fedmux.Handle("/exchange_third_party_invite/{roomID}", MakeFedAPI(
|
2022-10-26 13:59:19 +02:00
|
|
|
"exchange_third_party_invite", cfg.Matrix.ServerName, cfg.Matrix.IsLocalServerName, keys, wakeup,
|
2023-04-19 16:50:33 +02:00
|
|
|
func(httpReq *http.Request, request *fclient.FederationRequest, vars map[string]string) util.JSONResponse {
|
2017-10-11 19:16:53 +02:00
|
|
|
return ExchangeThirdPartyInvite(
|
2020-06-10 13:17:54 +02:00
|
|
|
httpReq, request, vars["roomID"], rsAPI, cfg, federation,
|
2017-09-12 18:15:13 +02:00
|
|
|
)
|
|
|
|
},
|
2018-03-13 16:55:45 +01:00
|
|
|
)).Methods(http.MethodPut, http.MethodOptions)
|
2017-09-12 18:15:13 +02:00
|
|
|
|
2022-05-06 13:39:26 +02:00
|
|
|
v1fedmux.Handle("/event/{eventID}", MakeFedAPI(
|
2022-10-26 13:59:19 +02:00
|
|
|
"federation_get_event", cfg.Matrix.ServerName, cfg.Matrix.IsLocalServerName, keys, wakeup,
|
2023-04-19 16:50:33 +02:00
|
|
|
func(httpReq *http.Request, request *fclient.FederationRequest, vars map[string]string) util.JSONResponse {
|
2017-10-25 12:27:44 +02:00
|
|
|
return GetEvent(
|
2020-05-01 11:48:17 +02:00
|
|
|
httpReq.Context(), request, rsAPI, vars["eventID"], cfg.Matrix.ServerName,
|
2017-09-06 13:38:22 +02:00
|
|
|
)
|
|
|
|
},
|
2018-03-13 16:55:45 +01:00
|
|
|
)).Methods(http.MethodGet)
|
2017-09-25 12:16:47 +02:00
|
|
|
|
2022-05-06 13:39:26 +02:00
|
|
|
v1fedmux.Handle("/state/{roomID}", MakeFedAPI(
|
2022-10-26 13:59:19 +02:00
|
|
|
"federation_get_state", cfg.Matrix.ServerName, cfg.Matrix.IsLocalServerName, keys, wakeup,
|
2023-04-19 16:50:33 +02:00
|
|
|
func(httpReq *http.Request, request *fclient.FederationRequest, vars map[string]string) util.JSONResponse {
|
2020-09-04 11:40:58 +02:00
|
|
|
if roomserverAPI.IsServerBannedFromRoom(httpReq.Context(), rsAPI, vars["roomID"], request.Origin()) {
|
2020-08-11 19:19:11 +02:00
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusForbidden,
|
2023-05-10 00:46:49 +02:00
|
|
|
JSON: spec.Forbidden("Forbidden by server ACLs"),
|
2020-08-11 19:19:11 +02:00
|
|
|
}
|
|
|
|
}
|
2018-06-22 12:46:19 +02:00
|
|
|
return GetState(
|
2020-05-01 11:48:17 +02:00
|
|
|
httpReq.Context(), request, rsAPI, vars["roomID"],
|
2018-06-22 12:46:19 +02:00
|
|
|
)
|
|
|
|
},
|
|
|
|
)).Methods(http.MethodGet)
|
|
|
|
|
2022-05-06 13:39:26 +02:00
|
|
|
v1fedmux.Handle("/state_ids/{roomID}", MakeFedAPI(
|
2022-10-26 13:59:19 +02:00
|
|
|
"federation_get_state_ids", cfg.Matrix.ServerName, cfg.Matrix.IsLocalServerName, keys, wakeup,
|
2023-04-19 16:50:33 +02:00
|
|
|
func(httpReq *http.Request, request *fclient.FederationRequest, vars map[string]string) util.JSONResponse {
|
2020-09-04 11:40:58 +02:00
|
|
|
if roomserverAPI.IsServerBannedFromRoom(httpReq.Context(), rsAPI, vars["roomID"], request.Origin()) {
|
2020-08-11 19:19:11 +02:00
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusForbidden,
|
2023-05-10 00:46:49 +02:00
|
|
|
JSON: spec.Forbidden("Forbidden by server ACLs"),
|
2020-08-11 19:19:11 +02:00
|
|
|
}
|
|
|
|
}
|
2018-06-22 12:46:19 +02:00
|
|
|
return GetStateIDs(
|
2020-05-01 11:48:17 +02:00
|
|
|
httpReq.Context(), request, rsAPI, vars["roomID"],
|
2018-06-22 12:46:19 +02:00
|
|
|
)
|
|
|
|
},
|
|
|
|
)).Methods(http.MethodGet)
|
|
|
|
|
2022-05-06 13:39:26 +02:00
|
|
|
v1fedmux.Handle("/event_auth/{roomID}/{eventID}", MakeFedAPI(
|
2022-10-26 13:59:19 +02:00
|
|
|
"federation_get_event_auth", cfg.Matrix.ServerName, cfg.Matrix.IsLocalServerName, keys, wakeup,
|
2023-04-19 16:50:33 +02:00
|
|
|
func(httpReq *http.Request, request *fclient.FederationRequest, vars map[string]string) util.JSONResponse {
|
2020-09-04 11:40:58 +02:00
|
|
|
if roomserverAPI.IsServerBannedFromRoom(httpReq.Context(), rsAPI, vars["roomID"], request.Origin()) {
|
2020-08-11 19:19:11 +02:00
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusForbidden,
|
2023-05-10 00:46:49 +02:00
|
|
|
JSON: spec.Forbidden("Forbidden by server ACLs"),
|
2020-08-11 19:19:11 +02:00
|
|
|
}
|
|
|
|
}
|
2020-03-14 21:08:54 +01:00
|
|
|
return GetEventAuth(
|
2020-05-01 11:48:17 +02:00
|
|
|
httpReq.Context(), request, rsAPI, vars["roomID"], vars["eventID"],
|
2020-03-14 21:08:54 +01:00
|
|
|
)
|
|
|
|
},
|
|
|
|
)).Methods(http.MethodGet)
|
|
|
|
|
2022-05-06 13:39:26 +02:00
|
|
|
v1fedmux.Handle("/query/directory", MakeFedAPI(
|
2022-10-26 13:59:19 +02:00
|
|
|
"federation_query_room_alias", cfg.Matrix.ServerName, cfg.Matrix.IsLocalServerName, keys, wakeup,
|
2023-04-19 16:50:33 +02:00
|
|
|
func(httpReq *http.Request, request *fclient.FederationRequest, vars map[string]string) util.JSONResponse {
|
2017-11-20 15:33:49 +01:00
|
|
|
return RoomAliasToID(
|
2020-06-02 13:42:36 +02:00
|
|
|
httpReq, federation, cfg, rsAPI, fsAPI,
|
2017-11-20 15:33:49 +01:00
|
|
|
)
|
|
|
|
},
|
2023-01-23 18:55:12 +01:00
|
|
|
)).Methods(http.MethodGet).Name(QueryDirectoryRouteName)
|
2017-11-20 15:33:49 +01:00
|
|
|
|
2022-05-06 13:39:26 +02:00
|
|
|
v1fedmux.Handle("/query/profile", MakeFedAPI(
|
2022-10-26 13:59:19 +02:00
|
|
|
"federation_query_profile", cfg.Matrix.ServerName, cfg.Matrix.IsLocalServerName, keys, wakeup,
|
2023-04-19 16:50:33 +02:00
|
|
|
func(httpReq *http.Request, request *fclient.FederationRequest, vars map[string]string) util.JSONResponse {
|
2017-11-05 19:03:54 +01:00
|
|
|
return GetProfile(
|
2020-06-16 15:53:19 +02:00
|
|
|
httpReq, userAPI, cfg,
|
2017-11-05 19:03:54 +01:00
|
|
|
)
|
|
|
|
},
|
2023-01-23 18:55:12 +01:00
|
|
|
)).Methods(http.MethodGet).Name(QueryProfileRouteName)
|
2017-11-05 19:03:54 +01:00
|
|
|
|
2022-05-06 13:39:26 +02:00
|
|
|
v1fedmux.Handle("/user/devices/{userID}", MakeFedAPI(
|
2022-10-26 13:59:19 +02:00
|
|
|
"federation_user_devices", cfg.Matrix.ServerName, cfg.Matrix.IsLocalServerName, keys, wakeup,
|
2023-04-19 16:50:33 +02:00
|
|
|
func(httpReq *http.Request, request *fclient.FederationRequest, vars map[string]string) util.JSONResponse {
|
2018-06-26 12:32:43 +02:00
|
|
|
return GetUserDevices(
|
2023-02-20 14:58:03 +01:00
|
|
|
httpReq, userAPI, vars["userID"],
|
2018-06-26 12:32:43 +02:00
|
|
|
)
|
|
|
|
},
|
|
|
|
)).Methods(http.MethodGet)
|
|
|
|
|
2021-01-22 17:08:47 +01:00
|
|
|
if mscCfg.Enabled("msc2444") {
|
2022-05-06 13:39:26 +02:00
|
|
|
v1fedmux.Handle("/peek/{roomID}/{peekID}", MakeFedAPI(
|
2022-10-26 13:59:19 +02:00
|
|
|
"federation_peek", cfg.Matrix.ServerName, cfg.Matrix.IsLocalServerName, keys, wakeup,
|
2023-04-19 16:50:33 +02:00
|
|
|
func(httpReq *http.Request, request *fclient.FederationRequest, vars map[string]string) util.JSONResponse {
|
2021-01-22 17:08:47 +01:00
|
|
|
if roomserverAPI.IsServerBannedFromRoom(httpReq.Context(), rsAPI, vars["roomID"], request.Origin()) {
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusForbidden,
|
2023-05-10 00:46:49 +02:00
|
|
|
JSON: spec.Forbidden("Forbidden by server ACLs"),
|
2021-01-22 17:08:47 +01:00
|
|
|
}
|
2021-01-22 15:55:08 +01:00
|
|
|
}
|
2021-01-22 17:08:47 +01:00
|
|
|
roomID := vars["roomID"]
|
|
|
|
peekID := vars["peekID"]
|
|
|
|
queryVars := httpReq.URL.Query()
|
|
|
|
remoteVersions := []gomatrixserverlib.RoomVersion{}
|
|
|
|
if vers, ok := queryVars["ver"]; ok {
|
|
|
|
// The remote side supplied a ?ver= so use that to build up the list
|
|
|
|
// of supported room versions
|
|
|
|
for _, v := range vers {
|
|
|
|
remoteVersions = append(remoteVersions, gomatrixserverlib.RoomVersion(v))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// The remote side didn't supply a ?ver= so just assume that they only
|
|
|
|
// support room version 1
|
|
|
|
remoteVersions = append(remoteVersions, gomatrixserverlib.RoomVersionV1)
|
2021-01-22 15:55:08 +01:00
|
|
|
}
|
2021-01-22 17:08:47 +01:00
|
|
|
return Peek(
|
|
|
|
httpReq, request, cfg, rsAPI, roomID, peekID, remoteVersions,
|
|
|
|
)
|
|
|
|
},
|
|
|
|
)).Methods(http.MethodPut, http.MethodDelete)
|
|
|
|
}
|
2021-01-22 15:55:08 +01:00
|
|
|
|
2022-05-06 13:39:26 +02:00
|
|
|
v1fedmux.Handle("/make_join/{roomID}/{userID}", MakeFedAPI(
|
2022-10-26 13:59:19 +02:00
|
|
|
"federation_make_join", cfg.Matrix.ServerName, cfg.Matrix.IsLocalServerName, keys, wakeup,
|
2023-04-19 16:50:33 +02:00
|
|
|
func(httpReq *http.Request, request *fclient.FederationRequest, vars map[string]string) util.JSONResponse {
|
2020-09-04 11:40:58 +02:00
|
|
|
if roomserverAPI.IsServerBannedFromRoom(httpReq.Context(), rsAPI, vars["roomID"], request.Origin()) {
|
2020-08-11 19:19:11 +02:00
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusForbidden,
|
2023-05-10 00:46:49 +02:00
|
|
|
JSON: spec.Forbidden("Forbidden by server ACLs"),
|
2020-08-11 19:19:11 +02:00
|
|
|
}
|
|
|
|
}
|
Federation for v3/v4 rooms (#954)
* Update gomatrixserverlib
* Default to room version 4
* Update gomatrixserverlib
* Limit prev_events and auth_events
* Fix auth_events, prev_events
* Fix linter issues
* Update gomatrixserverlib
* Fix getState
* Update sytest-whitelist
* Squashed commit of the following:
commit 067b87506357c996fd6ddb11271db9469ad4ce80
Author: Neil Alexander <neilalexander@users.noreply.github.com>
Date: Fri Apr 3 14:29:06 2020 +0100
Invites v2 endpoint (#952)
* Start converting v1 invite endpoint to v2
* Update gomatrixserverlib
* Early federationsender code for sending invites
* Sending invites sorta happens now
* Populate invite request with stripped state
* Remodel a bit, don't reflect received invites
* Handle invite_room_state
* Handle room versions a bit better
* Update gomatrixserverlib
* Tweak order in destinationQueue.next
* Revert check in processMessage
* Tweak federation sender destination queue code a bit
* Add comments
commit 955244c09298d0e6c870377dad3af2ffa1f5e578
Author: Ben B <benne@klimlive.de>
Date: Fri Apr 3 12:40:50 2020 +0200
use custom http client instead of the http DefaultClient (#823)
This commit replaces the default client from the http lib with a custom one.
The previously used default client doesn't come with a timeout. This could cause
unwanted locks.
That solution chosen here creates a http client in the base component dendrite
with a constant timeout of 30 seconds. If it should be necessary to overwrite
this, we could include the timeout in the dendrite configuration.
Here it would be a good idea to extend the type "Address" by a timeout and
create an http client for each service.
Closes #820
Signed-off-by: Benedikt Bongartz <benne@klimlive.de>
Co-authored-by: Kegsay <kegan@matrix.org>
* Update sytest-whitelist, sytest-blacklist
* Update go.mod/go.sum
* Add some error wrapping for debug
* Add a NOTSPEC to common/events.go
* Perform state resolution at send_join
* Set default room version to v2 again
* Tweak GetCapabilities
* Add comments to ResolveConflictsAdhoc
* Update sytest-blacklist
* go mod tidy
* Update sytest-whitelist, sytest-blacklist
* Update versions
* Updates from review comments
* Update sytest-blacklist, sytest-whitelist
* Check room versions compatible at make_join, add some comments, update gomatrixserverlib, other tweaks
* Set default room version back to v2
* Update gomatrixserverlib, sytest-whitelist
2020-04-09 16:46:06 +02:00
|
|
|
queryVars := httpReq.URL.Query()
|
|
|
|
remoteVersions := []gomatrixserverlib.RoomVersion{}
|
|
|
|
if vers, ok := queryVars["ver"]; ok {
|
2021-01-22 15:55:08 +01:00
|
|
|
// The remote side supplied a ?ver= so use that to build up the list
|
Federation for v3/v4 rooms (#954)
* Update gomatrixserverlib
* Default to room version 4
* Update gomatrixserverlib
* Limit prev_events and auth_events
* Fix auth_events, prev_events
* Fix linter issues
* Update gomatrixserverlib
* Fix getState
* Update sytest-whitelist
* Squashed commit of the following:
commit 067b87506357c996fd6ddb11271db9469ad4ce80
Author: Neil Alexander <neilalexander@users.noreply.github.com>
Date: Fri Apr 3 14:29:06 2020 +0100
Invites v2 endpoint (#952)
* Start converting v1 invite endpoint to v2
* Update gomatrixserverlib
* Early federationsender code for sending invites
* Sending invites sorta happens now
* Populate invite request with stripped state
* Remodel a bit, don't reflect received invites
* Handle invite_room_state
* Handle room versions a bit better
* Update gomatrixserverlib
* Tweak order in destinationQueue.next
* Revert check in processMessage
* Tweak federation sender destination queue code a bit
* Add comments
commit 955244c09298d0e6c870377dad3af2ffa1f5e578
Author: Ben B <benne@klimlive.de>
Date: Fri Apr 3 12:40:50 2020 +0200
use custom http client instead of the http DefaultClient (#823)
This commit replaces the default client from the http lib with a custom one.
The previously used default client doesn't come with a timeout. This could cause
unwanted locks.
That solution chosen here creates a http client in the base component dendrite
with a constant timeout of 30 seconds. If it should be necessary to overwrite
this, we could include the timeout in the dendrite configuration.
Here it would be a good idea to extend the type "Address" by a timeout and
create an http client for each service.
Closes #820
Signed-off-by: Benedikt Bongartz <benne@klimlive.de>
Co-authored-by: Kegsay <kegan@matrix.org>
* Update sytest-whitelist, sytest-blacklist
* Update go.mod/go.sum
* Add some error wrapping for debug
* Add a NOTSPEC to common/events.go
* Perform state resolution at send_join
* Set default room version to v2 again
* Tweak GetCapabilities
* Add comments to ResolveConflictsAdhoc
* Update sytest-blacklist
* go mod tidy
* Update sytest-whitelist, sytest-blacklist
* Update versions
* Updates from review comments
* Update sytest-blacklist, sytest-whitelist
* Check room versions compatible at make_join, add some comments, update gomatrixserverlib, other tweaks
* Set default room version back to v2
* Update gomatrixserverlib, sytest-whitelist
2020-04-09 16:46:06 +02:00
|
|
|
// of supported room versions
|
|
|
|
for _, v := range vers {
|
|
|
|
remoteVersions = append(remoteVersions, gomatrixserverlib.RoomVersion(v))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// The remote side didn't supply a ?ver= so just assume that they only
|
|
|
|
// support room version 1, as per the spec
|
|
|
|
// https://matrix.org/docs/spec/server_server/r0.1.3#get-matrix-federation-v1-make-join-roomid-userid
|
|
|
|
remoteVersions = append(remoteVersions, gomatrixserverlib.RoomVersionV1)
|
|
|
|
}
|
2023-05-17 02:33:27 +02:00
|
|
|
|
|
|
|
userID, err := spec.NewUserID(vars["userID"], true)
|
|
|
|
if err != nil {
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
2023-05-19 18:27:01 +02:00
|
|
|
JSON: spec.InvalidParam("Invalid UserID"),
|
2023-05-17 02:33:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
roomID, err := spec.NewRoomID(vars["roomID"])
|
|
|
|
if err != nil {
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
2023-05-19 18:27:01 +02:00
|
|
|
JSON: spec.InvalidParam("Invalid RoomID"),
|
2023-05-17 02:33:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
logrus.Debugf("Processing make_join for user %s, room %s", userID.String(), roomID.String())
|
2017-11-29 10:38:56 +01:00
|
|
|
return MakeJoin(
|
2023-05-17 02:33:27 +02:00
|
|
|
httpReq, request, cfg, rsAPI, *roomID, *userID, remoteVersions,
|
2017-11-29 10:38:56 +01:00
|
|
|
)
|
|
|
|
},
|
2018-03-13 16:55:45 +01:00
|
|
|
)).Methods(http.MethodGet)
|
2017-11-29 10:38:56 +01:00
|
|
|
|
2022-05-06 13:39:26 +02:00
|
|
|
v1fedmux.Handle("/send_join/{roomID}/{eventID}", MakeFedAPI(
|
2022-10-26 13:59:19 +02:00
|
|
|
"federation_send_join", cfg.Matrix.ServerName, cfg.Matrix.IsLocalServerName, keys, wakeup,
|
2023-04-19 16:50:33 +02:00
|
|
|
func(httpReq *http.Request, request *fclient.FederationRequest, vars map[string]string) util.JSONResponse {
|
2020-09-04 11:40:58 +02:00
|
|
|
if roomserverAPI.IsServerBannedFromRoom(httpReq.Context(), rsAPI, vars["roomID"], request.Origin()) {
|
2020-08-11 19:19:11 +02:00
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusForbidden,
|
2023-05-10 00:46:49 +02:00
|
|
|
JSON: spec.Forbidden("Forbidden by server ACLs"),
|
2020-08-11 19:19:11 +02:00
|
|
|
}
|
|
|
|
}
|
2020-05-12 17:24:28 +02:00
|
|
|
eventID := vars["eventID"]
|
2023-05-19 18:27:01 +02:00
|
|
|
roomID, err := spec.NewRoomID(vars["roomID"])
|
|
|
|
if err != nil {
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
JSON: spec.InvalidParam("Invalid RoomID"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-12 17:24:28 +02:00
|
|
|
res := SendJoin(
|
2023-05-19 18:27:01 +02:00
|
|
|
httpReq, request, cfg, rsAPI, keys, *roomID, eventID,
|
2020-05-12 17:24:28 +02:00
|
|
|
)
|
2020-06-17 14:55:55 +02:00
|
|
|
// not all responses get wrapped in [code, body]
|
|
|
|
var body interface{}
|
|
|
|
body = []interface{}{
|
|
|
|
res.Code, res.JSON,
|
|
|
|
}
|
2023-05-17 02:33:27 +02:00
|
|
|
jerr, ok := res.JSON.(spec.MatrixError)
|
2020-06-17 14:55:55 +02:00
|
|
|
if ok {
|
|
|
|
body = jerr
|
|
|
|
}
|
|
|
|
|
2020-05-12 17:24:28 +02:00
|
|
|
return util.JSONResponse{
|
|
|
|
Headers: res.Headers,
|
|
|
|
Code: res.Code,
|
2020-06-17 14:55:55 +02:00
|
|
|
JSON: body,
|
2020-05-12 17:24:28 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
)).Methods(http.MethodPut)
|
|
|
|
|
2022-05-06 13:39:26 +02:00
|
|
|
v2fedmux.Handle("/send_join/{roomID}/{eventID}", MakeFedAPI(
|
2022-10-26 13:59:19 +02:00
|
|
|
"federation_send_join", cfg.Matrix.ServerName, cfg.Matrix.IsLocalServerName, keys, wakeup,
|
2023-04-19 16:50:33 +02:00
|
|
|
func(httpReq *http.Request, request *fclient.FederationRequest, vars map[string]string) util.JSONResponse {
|
2020-09-04 11:40:58 +02:00
|
|
|
if roomserverAPI.IsServerBannedFromRoom(httpReq.Context(), rsAPI, vars["roomID"], request.Origin()) {
|
2020-08-11 19:19:11 +02:00
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusForbidden,
|
2023-05-10 00:46:49 +02:00
|
|
|
JSON: spec.Forbidden("Forbidden by server ACLs"),
|
2020-08-11 19:19:11 +02:00
|
|
|
}
|
|
|
|
}
|
Federation for v3/v4 rooms (#954)
* Update gomatrixserverlib
* Default to room version 4
* Update gomatrixserverlib
* Limit prev_events and auth_events
* Fix auth_events, prev_events
* Fix linter issues
* Update gomatrixserverlib
* Fix getState
* Update sytest-whitelist
* Squashed commit of the following:
commit 067b87506357c996fd6ddb11271db9469ad4ce80
Author: Neil Alexander <neilalexander@users.noreply.github.com>
Date: Fri Apr 3 14:29:06 2020 +0100
Invites v2 endpoint (#952)
* Start converting v1 invite endpoint to v2
* Update gomatrixserverlib
* Early federationsender code for sending invites
* Sending invites sorta happens now
* Populate invite request with stripped state
* Remodel a bit, don't reflect received invites
* Handle invite_room_state
* Handle room versions a bit better
* Update gomatrixserverlib
* Tweak order in destinationQueue.next
* Revert check in processMessage
* Tweak federation sender destination queue code a bit
* Add comments
commit 955244c09298d0e6c870377dad3af2ffa1f5e578
Author: Ben B <benne@klimlive.de>
Date: Fri Apr 3 12:40:50 2020 +0200
use custom http client instead of the http DefaultClient (#823)
This commit replaces the default client from the http lib with a custom one.
The previously used default client doesn't come with a timeout. This could cause
unwanted locks.
That solution chosen here creates a http client in the base component dendrite
with a constant timeout of 30 seconds. If it should be necessary to overwrite
this, we could include the timeout in the dendrite configuration.
Here it would be a good idea to extend the type "Address" by a timeout and
create an http client for each service.
Closes #820
Signed-off-by: Benedikt Bongartz <benne@klimlive.de>
Co-authored-by: Kegsay <kegan@matrix.org>
* Update sytest-whitelist, sytest-blacklist
* Update go.mod/go.sum
* Add some error wrapping for debug
* Add a NOTSPEC to common/events.go
* Perform state resolution at send_join
* Set default room version to v2 again
* Tweak GetCapabilities
* Add comments to ResolveConflictsAdhoc
* Update sytest-blacklist
* go mod tidy
* Update sytest-whitelist, sytest-blacklist
* Update versions
* Updates from review comments
* Update sytest-blacklist, sytest-whitelist
* Check room versions compatible at make_join, add some comments, update gomatrixserverlib, other tweaks
* Set default room version back to v2
* Update gomatrixserverlib, sytest-whitelist
2020-04-09 16:46:06 +02:00
|
|
|
eventID := vars["eventID"]
|
2023-05-19 18:27:01 +02:00
|
|
|
roomID, err := spec.NewRoomID(vars["roomID"])
|
|
|
|
if err != nil {
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
JSON: spec.InvalidParam("Invalid RoomID"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-29 10:38:56 +01:00
|
|
|
return SendJoin(
|
2023-05-19 18:27:01 +02:00
|
|
|
httpReq, request, cfg, rsAPI, keys, *roomID, eventID,
|
2017-11-29 10:38:56 +01:00
|
|
|
)
|
|
|
|
},
|
2018-03-13 16:55:45 +01:00
|
|
|
)).Methods(http.MethodPut)
|
2017-11-29 10:38:56 +01:00
|
|
|
|
2023-05-23 19:37:04 +02:00
|
|
|
v1fedmux.Handle("/make_leave/{roomID}/{userID}", MakeFedAPI(
|
2022-10-26 13:59:19 +02:00
|
|
|
"federation_make_leave", cfg.Matrix.ServerName, cfg.Matrix.IsLocalServerName, keys, wakeup,
|
2023-04-19 16:50:33 +02:00
|
|
|
func(httpReq *http.Request, request *fclient.FederationRequest, vars map[string]string) util.JSONResponse {
|
2020-09-04 11:40:58 +02:00
|
|
|
if roomserverAPI.IsServerBannedFromRoom(httpReq.Context(), rsAPI, vars["roomID"], request.Origin()) {
|
2020-08-11 19:19:11 +02:00
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusForbidden,
|
2023-05-10 00:46:49 +02:00
|
|
|
JSON: spec.Forbidden("Forbidden by server ACLs"),
|
2020-08-11 19:19:11 +02:00
|
|
|
}
|
|
|
|
}
|
2023-05-23 19:37:04 +02:00
|
|
|
roomID, err := spec.NewRoomID(vars["roomID"])
|
|
|
|
if err != nil {
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
JSON: spec.InvalidParam("Invalid RoomID"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
userID, err := spec.NewUserID(vars["userID"], true)
|
|
|
|
if err != nil {
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
JSON: spec.InvalidParam("Invalid UserID"),
|
|
|
|
}
|
|
|
|
}
|
2018-07-11 12:07:44 +02:00
|
|
|
return MakeLeave(
|
2023-05-23 19:37:04 +02:00
|
|
|
httpReq, request, cfg, rsAPI, *roomID, *userID,
|
2018-07-11 12:07:44 +02:00
|
|
|
)
|
|
|
|
},
|
|
|
|
)).Methods(http.MethodGet)
|
|
|
|
|
2022-05-06 13:39:26 +02:00
|
|
|
v1fedmux.Handle("/send_leave/{roomID}/{eventID}", MakeFedAPI(
|
2022-10-26 13:59:19 +02:00
|
|
|
"federation_send_leave", cfg.Matrix.ServerName, cfg.Matrix.IsLocalServerName, keys, wakeup,
|
2023-04-19 16:50:33 +02:00
|
|
|
func(httpReq *http.Request, request *fclient.FederationRequest, vars map[string]string) util.JSONResponse {
|
2020-09-04 11:40:58 +02:00
|
|
|
if roomserverAPI.IsServerBannedFromRoom(httpReq.Context(), rsAPI, vars["roomID"], request.Origin()) {
|
2020-08-11 19:19:11 +02:00
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusForbidden,
|
2023-05-10 00:46:49 +02:00
|
|
|
JSON: spec.Forbidden("Forbidden by server ACLs"),
|
2020-08-11 19:19:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
roomID := vars["roomID"]
|
|
|
|
eventID := vars["eventID"]
|
|
|
|
res := SendLeave(
|
|
|
|
httpReq, request, cfg, rsAPI, keys, roomID, eventID,
|
|
|
|
)
|
|
|
|
// not all responses get wrapped in [code, body]
|
|
|
|
var body interface{}
|
|
|
|
body = []interface{}{
|
|
|
|
res.Code, res.JSON,
|
|
|
|
}
|
2023-05-17 02:33:27 +02:00
|
|
|
jerr, ok := res.JSON.(spec.MatrixError)
|
2020-08-11 19:19:11 +02:00
|
|
|
if ok {
|
|
|
|
body = jerr
|
|
|
|
}
|
|
|
|
|
|
|
|
return util.JSONResponse{
|
|
|
|
Headers: res.Headers,
|
|
|
|
Code: res.Code,
|
|
|
|
JSON: body,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)).Methods(http.MethodPut)
|
|
|
|
|
2022-05-06 13:39:26 +02:00
|
|
|
v2fedmux.Handle("/send_leave/{roomID}/{eventID}", MakeFedAPI(
|
2022-10-26 13:59:19 +02:00
|
|
|
"federation_send_leave", cfg.Matrix.ServerName, cfg.Matrix.IsLocalServerName, keys, wakeup,
|
2023-04-19 16:50:33 +02:00
|
|
|
func(httpReq *http.Request, request *fclient.FederationRequest, vars map[string]string) util.JSONResponse {
|
2020-09-04 11:40:58 +02:00
|
|
|
if roomserverAPI.IsServerBannedFromRoom(httpReq.Context(), rsAPI, vars["roomID"], request.Origin()) {
|
2020-08-11 19:19:11 +02:00
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusForbidden,
|
2023-05-10 00:46:49 +02:00
|
|
|
JSON: spec.Forbidden("Forbidden by server ACLs"),
|
2020-08-11 19:19:11 +02:00
|
|
|
}
|
|
|
|
}
|
2018-07-11 12:07:44 +02:00
|
|
|
roomID := vars["roomID"]
|
Federation for v3/v4 rooms (#954)
* Update gomatrixserverlib
* Default to room version 4
* Update gomatrixserverlib
* Limit prev_events and auth_events
* Fix auth_events, prev_events
* Fix linter issues
* Update gomatrixserverlib
* Fix getState
* Update sytest-whitelist
* Squashed commit of the following:
commit 067b87506357c996fd6ddb11271db9469ad4ce80
Author: Neil Alexander <neilalexander@users.noreply.github.com>
Date: Fri Apr 3 14:29:06 2020 +0100
Invites v2 endpoint (#952)
* Start converting v1 invite endpoint to v2
* Update gomatrixserverlib
* Early federationsender code for sending invites
* Sending invites sorta happens now
* Populate invite request with stripped state
* Remodel a bit, don't reflect received invites
* Handle invite_room_state
* Handle room versions a bit better
* Update gomatrixserverlib
* Tweak order in destinationQueue.next
* Revert check in processMessage
* Tweak federation sender destination queue code a bit
* Add comments
commit 955244c09298d0e6c870377dad3af2ffa1f5e578
Author: Ben B <benne@klimlive.de>
Date: Fri Apr 3 12:40:50 2020 +0200
use custom http client instead of the http DefaultClient (#823)
This commit replaces the default client from the http lib with a custom one.
The previously used default client doesn't come with a timeout. This could cause
unwanted locks.
That solution chosen here creates a http client in the base component dendrite
with a constant timeout of 30 seconds. If it should be necessary to overwrite
this, we could include the timeout in the dendrite configuration.
Here it would be a good idea to extend the type "Address" by a timeout and
create an http client for each service.
Closes #820
Signed-off-by: Benedikt Bongartz <benne@klimlive.de>
Co-authored-by: Kegsay <kegan@matrix.org>
* Update sytest-whitelist, sytest-blacklist
* Update go.mod/go.sum
* Add some error wrapping for debug
* Add a NOTSPEC to common/events.go
* Perform state resolution at send_join
* Set default room version to v2 again
* Tweak GetCapabilities
* Add comments to ResolveConflictsAdhoc
* Update sytest-blacklist
* go mod tidy
* Update sytest-whitelist, sytest-blacklist
* Update versions
* Updates from review comments
* Update sytest-blacklist, sytest-whitelist
* Check room versions compatible at make_join, add some comments, update gomatrixserverlib, other tweaks
* Set default room version back to v2
* Update gomatrixserverlib, sytest-whitelist
2020-04-09 16:46:06 +02:00
|
|
|
eventID := vars["eventID"]
|
2018-07-11 12:07:44 +02:00
|
|
|
return SendLeave(
|
2020-06-10 13:17:54 +02:00
|
|
|
httpReq, request, cfg, rsAPI, keys, roomID, eventID,
|
2018-07-11 12:07:44 +02:00
|
|
|
)
|
|
|
|
},
|
|
|
|
)).Methods(http.MethodPut)
|
|
|
|
|
2020-06-12 15:55:57 +02:00
|
|
|
v1fedmux.Handle("/version", httputil.MakeExternalAPI(
|
2017-09-25 12:16:47 +02:00
|
|
|
"federation_version",
|
|
|
|
func(httpReq *http.Request) util.JSONResponse {
|
2017-10-25 12:27:44 +02:00
|
|
|
return Version()
|
2017-09-25 12:16:47 +02:00
|
|
|
},
|
2018-03-13 16:55:45 +01:00
|
|
|
)).Methods(http.MethodGet)
|
2018-06-26 12:25:49 +02:00
|
|
|
|
2022-05-06 13:39:26 +02:00
|
|
|
v1fedmux.Handle("/get_missing_events/{roomID}", MakeFedAPI(
|
2022-10-26 13:59:19 +02:00
|
|
|
"federation_get_missing_events", cfg.Matrix.ServerName, cfg.Matrix.IsLocalServerName, keys, wakeup,
|
2023-04-19 16:50:33 +02:00
|
|
|
func(httpReq *http.Request, request *fclient.FederationRequest, vars map[string]string) util.JSONResponse {
|
2020-09-04 11:40:58 +02:00
|
|
|
if roomserverAPI.IsServerBannedFromRoom(httpReq.Context(), rsAPI, vars["roomID"], request.Origin()) {
|
2020-08-11 19:19:11 +02:00
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusForbidden,
|
2023-05-10 00:46:49 +02:00
|
|
|
JSON: spec.Forbidden("Forbidden by server ACLs"),
|
2020-08-11 19:19:11 +02:00
|
|
|
}
|
|
|
|
}
|
2020-05-01 11:48:17 +02:00
|
|
|
return GetMissingEvents(httpReq, request, rsAPI, vars["roomID"])
|
2018-06-26 12:25:49 +02:00
|
|
|
},
|
2019-01-14 11:20:19 +01:00
|
|
|
)).Methods(http.MethodPost)
|
2018-11-07 12:38:01 +01:00
|
|
|
|
2022-05-06 13:39:26 +02:00
|
|
|
v1fedmux.Handle("/backfill/{roomID}", MakeFedAPI(
|
2022-10-26 13:59:19 +02:00
|
|
|
"federation_backfill", cfg.Matrix.ServerName, cfg.Matrix.IsLocalServerName, keys, wakeup,
|
2023-04-19 16:50:33 +02:00
|
|
|
func(httpReq *http.Request, request *fclient.FederationRequest, vars map[string]string) util.JSONResponse {
|
2020-09-04 11:40:58 +02:00
|
|
|
if roomserverAPI.IsServerBannedFromRoom(httpReq.Context(), rsAPI, vars["roomID"], request.Origin()) {
|
2020-08-11 19:19:11 +02:00
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusForbidden,
|
2023-05-10 00:46:49 +02:00
|
|
|
JSON: spec.Forbidden("Forbidden by server ACLs"),
|
2020-08-11 19:19:11 +02:00
|
|
|
}
|
|
|
|
}
|
2020-05-01 11:48:17 +02:00
|
|
|
return Backfill(httpReq, request, rsAPI, vars["roomID"], cfg)
|
2018-11-07 12:38:01 +01:00
|
|
|
},
|
|
|
|
)).Methods(http.MethodGet)
|
2020-07-02 16:41:18 +02:00
|
|
|
|
|
|
|
v1fedmux.Handle("/publicRooms",
|
|
|
|
httputil.MakeExternalAPI("federation_public_rooms", func(req *http.Request) util.JSONResponse {
|
2020-09-07 13:38:09 +02:00
|
|
|
return GetPostPublicRooms(req, rsAPI)
|
2020-07-02 16:41:18 +02:00
|
|
|
}),
|
2021-08-27 16:48:27 +02:00
|
|
|
).Methods(http.MethodGet, http.MethodPost)
|
2020-07-22 18:04:57 +02:00
|
|
|
|
2022-05-06 13:39:26 +02:00
|
|
|
v1fedmux.Handle("/user/keys/claim", MakeFedAPI(
|
2022-10-26 13:59:19 +02:00
|
|
|
"federation_keys_claim", cfg.Matrix.ServerName, cfg.Matrix.IsLocalServerName, keys, wakeup,
|
2023-04-19 16:50:33 +02:00
|
|
|
func(httpReq *http.Request, request *fclient.FederationRequest, vars map[string]string) util.JSONResponse {
|
2023-02-20 14:58:03 +01:00
|
|
|
return ClaimOneTimeKeys(httpReq, request, userAPI, cfg.Matrix.ServerName)
|
2020-07-22 18:04:57 +02:00
|
|
|
},
|
|
|
|
)).Methods(http.MethodPost)
|
|
|
|
|
2022-05-06 13:39:26 +02:00
|
|
|
v1fedmux.Handle("/user/keys/query", MakeFedAPI(
|
2022-10-26 13:59:19 +02:00
|
|
|
"federation_keys_query", cfg.Matrix.ServerName, cfg.Matrix.IsLocalServerName, keys, wakeup,
|
2023-04-19 16:50:33 +02:00
|
|
|
func(httpReq *http.Request, request *fclient.FederationRequest, vars map[string]string) util.JSONResponse {
|
2023-02-20 14:58:03 +01:00
|
|
|
return QueryDeviceKeys(httpReq, request, userAPI, cfg.Matrix.ServerName)
|
2020-07-22 18:04:57 +02:00
|
|
|
},
|
|
|
|
)).Methods(http.MethodPost)
|
2021-04-07 14:26:20 +02:00
|
|
|
|
|
|
|
v1fedmux.Handle("/openid/userinfo",
|
|
|
|
httputil.MakeExternalAPI("federation_openid_userinfo", func(req *http.Request) util.JSONResponse {
|
|
|
|
return GetOpenIDUserInfo(req, userAPI)
|
|
|
|
}),
|
|
|
|
).Methods(http.MethodGet)
|
2023-07-20 16:06:05 +02:00
|
|
|
|
|
|
|
v1fedmux.Handle("/hierarchy/{roomID}", MakeFedAPI(
|
|
|
|
"federation_room_hierarchy", cfg.Matrix.ServerName, cfg.Matrix.IsLocalServerName, keys, wakeup,
|
|
|
|
func(httpReq *http.Request, request *fclient.FederationRequest, vars map[string]string) util.JSONResponse {
|
|
|
|
return QueryRoomHierarchy(httpReq, request, vars["roomID"], rsAPI)
|
|
|
|
},
|
|
|
|
)).Methods(http.MethodGet)
|
2017-05-19 17:06:41 +02:00
|
|
|
}
|
2022-04-28 12:45:56 +02:00
|
|
|
|
|
|
|
func ErrorIfLocalServerNotInRoom(
|
|
|
|
ctx context.Context,
|
2022-05-05 20:30:38 +02:00
|
|
|
rsAPI api.FederationRoomserverAPI,
|
2022-04-28 12:45:56 +02:00
|
|
|
roomID string,
|
|
|
|
) *util.JSONResponse {
|
|
|
|
// Check if we think we're in this room. If we aren't then
|
|
|
|
// we won't waste CPU cycles serving this request.
|
|
|
|
joinedReq := &api.QueryServerJoinedToRoomRequest{
|
|
|
|
RoomID: roomID,
|
|
|
|
}
|
|
|
|
joinedRes := &api.QueryServerJoinedToRoomResponse{}
|
|
|
|
if err := rsAPI.QueryServerJoinedToRoom(ctx, joinedReq, joinedRes); err != nil {
|
|
|
|
res := util.ErrorResponse(err)
|
|
|
|
return &res
|
|
|
|
}
|
|
|
|
if !joinedRes.IsInRoom {
|
|
|
|
return &util.JSONResponse{
|
|
|
|
Code: http.StatusNotFound,
|
2023-05-10 00:46:49 +02:00
|
|
|
JSON: spec.NotFound(fmt.Sprintf("This server is not joined to room %s", roomID)),
|
2022-04-28 12:45:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2022-05-06 13:39:26 +02:00
|
|
|
|
|
|
|
// MakeFedAPI makes an http.Handler that checks matrix federation authentication.
|
|
|
|
func MakeFedAPI(
|
2023-04-19 16:50:33 +02:00
|
|
|
metricsName string, serverName spec.ServerName,
|
|
|
|
isLocalServerName func(spec.ServerName) bool,
|
2022-05-06 13:39:26 +02:00
|
|
|
keyRing gomatrixserverlib.JSONVerifier,
|
|
|
|
wakeup *FederationWakeups,
|
2023-04-19 16:50:33 +02:00
|
|
|
f func(*http.Request, *fclient.FederationRequest, map[string]string) util.JSONResponse,
|
2022-05-06 13:39:26 +02:00
|
|
|
) http.Handler {
|
|
|
|
h := func(req *http.Request) util.JSONResponse {
|
2023-04-19 16:50:33 +02:00
|
|
|
fedReq, errResp := fclient.VerifyHTTPRequest(
|
2022-10-26 13:59:19 +02:00
|
|
|
req, time.Now(), serverName, isLocalServerName, keyRing,
|
2022-05-06 13:39:26 +02:00
|
|
|
)
|
|
|
|
if fedReq == nil {
|
|
|
|
return errResp
|
|
|
|
}
|
|
|
|
// add the user to Sentry, if enabled
|
|
|
|
hub := sentry.GetHubFromContext(req.Context())
|
|
|
|
if hub != nil {
|
2024-01-24 19:24:04 +01:00
|
|
|
// clone the hub, so we don't send garbage events with e.g. mismatching rooms/event_ids
|
|
|
|
hub = hub.Clone()
|
2022-05-06 13:39:26 +02:00
|
|
|
hub.Scope().SetTag("origin", string(fedReq.Origin()))
|
|
|
|
hub.Scope().SetTag("uri", fedReq.RequestURI())
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
if hub != nil {
|
|
|
|
hub.CaptureException(fmt.Errorf("%s panicked", req.URL.Path))
|
|
|
|
}
|
|
|
|
// re-panic to return the 500
|
|
|
|
panic(r)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
go wakeup.Wakeup(req.Context(), fedReq.Origin())
|
|
|
|
vars, err := httputil.URLDecodeMapValues(mux.Vars(req))
|
|
|
|
if err != nil {
|
2023-05-17 02:33:27 +02:00
|
|
|
return util.MatrixErrorResponse(400, string(spec.ErrorUnrecognized), "badly encoded query params")
|
2022-05-06 13:39:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
jsonRes := f(req, fedReq, vars)
|
|
|
|
// do not log 4xx as errors as they are client fails, not server fails
|
|
|
|
if hub != nil && jsonRes.Code >= 500 {
|
|
|
|
hub.Scope().SetExtra("response", jsonRes)
|
|
|
|
hub.CaptureException(fmt.Errorf("%s returned HTTP %d", req.URL.Path, jsonRes.Code))
|
|
|
|
}
|
|
|
|
return jsonRes
|
|
|
|
}
|
|
|
|
return httputil.MakeExternalAPI(metricsName, h)
|
|
|
|
}
|
|
|
|
|
2024-08-16 12:37:59 +02:00
|
|
|
// MakeFedHTTPAPI makes an http.Handler that checks matrix federation authentication.
|
|
|
|
func MakeFedHTTPAPI(
|
|
|
|
serverName spec.ServerName,
|
|
|
|
isLocalServerName func(spec.ServerName) bool,
|
|
|
|
keyRing gomatrixserverlib.JSONVerifier,
|
|
|
|
f func(http.ResponseWriter, *http.Request),
|
|
|
|
) http.Handler {
|
|
|
|
h := func(w http.ResponseWriter, req *http.Request) {
|
|
|
|
fedReq, errResp := fclient.VerifyHTTPRequest(
|
|
|
|
req, time.Now(), serverName, isLocalServerName, keyRing,
|
|
|
|
)
|
|
|
|
|
|
|
|
enc := json.NewEncoder(w)
|
|
|
|
logger := util.GetLogger(req.Context())
|
|
|
|
if fedReq == nil {
|
|
|
|
|
|
|
|
logger.Debugf("VerifyUserFromRequest %s -> HTTP %d", req.RemoteAddr, errResp.Code)
|
|
|
|
w.WriteHeader(errResp.Code)
|
|
|
|
if err := enc.Encode(errResp); err != nil {
|
|
|
|
logger.WithError(err).Error("failed to encode JSON response")
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// add the user to Sentry, if enabled
|
|
|
|
hub := sentry.GetHubFromContext(req.Context())
|
|
|
|
if hub != nil {
|
|
|
|
// clone the hub, so we don't send garbage events with e.g. mismatching rooms/event_ids
|
|
|
|
hub = hub.Clone()
|
|
|
|
hub.Scope().SetTag("origin", string(fedReq.Origin()))
|
|
|
|
hub.Scope().SetTag("uri", fedReq.RequestURI())
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
if hub != nil {
|
|
|
|
hub.CaptureException(fmt.Errorf("%s panicked", req.URL.Path))
|
|
|
|
}
|
|
|
|
// re-panic to return the 500
|
|
|
|
panic(r)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
f(w, req)
|
|
|
|
}
|
|
|
|
|
|
|
|
return http.HandlerFunc(h)
|
|
|
|
}
|
|
|
|
|
2022-05-06 13:39:26 +02:00
|
|
|
type FederationWakeups struct {
|
|
|
|
FsAPI *fedInternal.FederationInternalAPI
|
|
|
|
origins sync.Map
|
|
|
|
}
|
|
|
|
|
2023-04-19 16:50:33 +02:00
|
|
|
func (f *FederationWakeups) Wakeup(ctx context.Context, origin spec.ServerName) {
|
2022-05-06 13:39:26 +02:00
|
|
|
key, keyok := f.origins.Load(origin)
|
|
|
|
if keyok {
|
|
|
|
lastTime, ok := key.(time.Time)
|
|
|
|
if ok && time.Since(lastTime) < time.Minute {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2023-04-19 16:50:33 +02:00
|
|
|
f.FsAPI.MarkServersAlive([]spec.ServerName{origin})
|
2022-05-06 13:39:26 +02:00
|
|
|
f.origins.Store(origin, time.Now())
|
|
|
|
}
|