2017-05-19 15:06:41 +00: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 (
|
2017-08-03 14:10:39 +00:00
|
|
|
"net/http"
|
|
|
|
|
2017-05-19 15:06:41 +00:00
|
|
|
"github.com/gorilla/mux"
|
2018-08-20 09:45:17 +00:00
|
|
|
appserviceAPI "github.com/matrix-org/dendrite/appservice/api"
|
2017-09-13 12:31:46 +00:00
|
|
|
"github.com/matrix-org/dendrite/clientapi/auth/storage/accounts"
|
2018-06-26 10:32:43 +00:00
|
|
|
"github.com/matrix-org/dendrite/clientapi/auth/storage/devices"
|
2017-06-07 13:32:53 +00:00
|
|
|
"github.com/matrix-org/dendrite/clientapi/producers"
|
2017-09-04 12:14:01 +00:00
|
|
|
"github.com/matrix-org/dendrite/common"
|
2017-06-19 14:21:04 +00:00
|
|
|
"github.com/matrix-org/dendrite/common/config"
|
2018-08-08 15:17:09 +00:00
|
|
|
roomserverAPI "github.com/matrix-org/dendrite/roomserver/api"
|
2017-06-07 13:32:53 +00:00
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
2017-05-19 15:06:41 +00:00
|
|
|
"github.com/matrix-org/util"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2017-06-07 13:32:53 +00:00
|
|
|
pathPrefixV2Keys = "/_matrix/key/v2"
|
|
|
|
pathPrefixV1Federation = "/_matrix/federation/v1"
|
2017-05-19 15:06:41 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Setup registers HTTP handlers with the given ServeMux.
|
2019-07-03 15:38:50 +00:00
|
|
|
//
|
|
|
|
// Due to Setup being used to call many other functions, a gocyclo nolint is
|
|
|
|
// applied:
|
|
|
|
// nolint: gocyclo
|
2017-06-07 13:32:53 +00:00
|
|
|
func Setup(
|
2017-08-03 14:10:39 +00:00
|
|
|
apiMux *mux.Router,
|
2017-06-19 14:21:04 +00:00
|
|
|
cfg config.Dendrite,
|
2018-08-08 15:17:09 +00:00
|
|
|
query roomserverAPI.RoomserverQueryAPI,
|
|
|
|
aliasAPI roomserverAPI.RoomserverAliasAPI,
|
2018-08-20 09:45:17 +00:00
|
|
|
asAPI appserviceAPI.AppServiceQueryAPI,
|
2017-06-07 13:32:53 +00:00
|
|
|
producer *producers.RoomserverProducer,
|
|
|
|
keys gomatrixserverlib.KeyRing,
|
|
|
|
federation *gomatrixserverlib.FederationClient,
|
2017-09-13 12:31:46 +00:00
|
|
|
accountDB *accounts.Database,
|
2018-06-26 10:32:43 +00:00
|
|
|
deviceDB *devices.Database,
|
2017-06-07 13:32:53 +00:00
|
|
|
) {
|
2017-05-19 15:06:41 +00:00
|
|
|
v2keysmux := apiMux.PathPrefix(pathPrefixV2Keys).Subrouter()
|
2017-06-07 13:32:53 +00:00
|
|
|
v1fedmux := apiMux.PathPrefix(pathPrefixV1Federation).Subrouter()
|
2017-05-19 15:06:41 +00:00
|
|
|
|
2017-09-28 13:50:40 +00:00
|
|
|
localKeys := common.MakeExternalAPI("localkeys", func(req *http.Request) util.JSONResponse {
|
2017-10-25 10:27:44 +00:00
|
|
|
return LocalKeys(cfg)
|
2017-05-25 15:08:28 +00: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 15:55:45 +00:00
|
|
|
v2keysmux.Handle("/server/{keyID}", localKeys).Methods(http.MethodGet)
|
|
|
|
v2keysmux.Handle("/server/", localKeys).Methods(http.MethodGet)
|
2017-05-19 15:06:41 +00:00
|
|
|
|
2017-09-04 12:14:01 +00:00
|
|
|
v1fedmux.Handle("/send/{txnID}/", common.MakeFedAPI(
|
|
|
|
"federation_send", cfg.Matrix.ServerName, keys,
|
|
|
|
func(httpReq *http.Request, request *gomatrixserverlib.FederationRequest) util.JSONResponse {
|
2019-07-03 15:38:50 +00:00
|
|
|
vars, err := common.URLDecodeMapValues(mux.Vars(httpReq))
|
|
|
|
if err != nil {
|
|
|
|
return util.ErrorResponse(err)
|
|
|
|
}
|
2017-10-11 17:16:53 +00:00
|
|
|
return Send(
|
2017-09-04 12:14:01 +00:00
|
|
|
httpReq, request, gomatrixserverlib.TransactionID(vars["txnID"]),
|
2017-06-07 13:32:53 +00:00
|
|
|
cfg, query, producer, keys, federation,
|
|
|
|
)
|
|
|
|
},
|
2018-03-13 15:55:45 +00:00
|
|
|
)).Methods(http.MethodPut, http.MethodOptions)
|
2017-08-23 14:13:47 +00:00
|
|
|
|
2017-09-04 12:14:01 +00:00
|
|
|
v1fedmux.Handle("/invite/{roomID}/{eventID}", common.MakeFedAPI(
|
|
|
|
"federation_invite", cfg.Matrix.ServerName, keys,
|
|
|
|
func(httpReq *http.Request, request *gomatrixserverlib.FederationRequest) util.JSONResponse {
|
2019-07-03 15:38:50 +00:00
|
|
|
vars, err := common.URLDecodeMapValues(mux.Vars(httpReq))
|
|
|
|
if err != nil {
|
|
|
|
return util.ErrorResponse(err)
|
|
|
|
}
|
2017-10-11 17:16:53 +00:00
|
|
|
return Invite(
|
2017-09-04 12:14:01 +00:00
|
|
|
httpReq, request, vars["roomID"], vars["eventID"],
|
2017-08-23 14:13:47 +00:00
|
|
|
cfg, producer, keys,
|
|
|
|
)
|
|
|
|
},
|
2018-03-13 15:55:45 +00:00
|
|
|
)).Methods(http.MethodPut, http.MethodOptions)
|
2017-09-06 11:38:22 +00:00
|
|
|
|
2017-09-28 13:50:40 +00:00
|
|
|
v1fedmux.Handle("/3pid/onbind", common.MakeExternalAPI("3pid_onbind",
|
2017-09-11 18:08:24 +00:00
|
|
|
func(req *http.Request) util.JSONResponse {
|
2018-08-20 09:45:17 +00:00
|
|
|
return CreateInvitesFrom3PIDInvites(req, query, asAPI, cfg, producer, federation, accountDB)
|
2017-09-08 14:17:12 +00:00
|
|
|
},
|
2018-03-13 15:55:45 +00:00
|
|
|
)).Methods(http.MethodPost, http.MethodOptions)
|
2017-09-08 14:17:12 +00:00
|
|
|
|
2017-09-12 16:15:13 +00:00
|
|
|
v1fedmux.Handle("/exchange_third_party_invite/{roomID}", common.MakeFedAPI(
|
|
|
|
"exchange_third_party_invite", cfg.Matrix.ServerName, keys,
|
|
|
|
func(httpReq *http.Request, request *gomatrixserverlib.FederationRequest) util.JSONResponse {
|
2019-07-03 15:38:50 +00:00
|
|
|
vars, err := common.URLDecodeMapValues(mux.Vars(httpReq))
|
|
|
|
if err != nil {
|
|
|
|
return util.ErrorResponse(err)
|
|
|
|
}
|
2017-10-11 17:16:53 +00:00
|
|
|
return ExchangeThirdPartyInvite(
|
2017-09-12 16:15:13 +00:00
|
|
|
httpReq, request, vars["roomID"], query, cfg, federation, producer,
|
|
|
|
)
|
|
|
|
},
|
2018-03-13 15:55:45 +00:00
|
|
|
)).Methods(http.MethodPut, http.MethodOptions)
|
2017-09-12 16:15:13 +00:00
|
|
|
|
2017-09-06 11:38:22 +00:00
|
|
|
v1fedmux.Handle("/event/{eventID}", common.MakeFedAPI(
|
|
|
|
"federation_get_event", cfg.Matrix.ServerName, keys,
|
|
|
|
func(httpReq *http.Request, request *gomatrixserverlib.FederationRequest) util.JSONResponse {
|
2019-07-03 15:38:50 +00:00
|
|
|
vars, err := common.URLDecodeMapValues(mux.Vars(httpReq))
|
|
|
|
if err != nil {
|
|
|
|
return util.ErrorResponse(err)
|
|
|
|
}
|
2017-10-25 10:27:44 +00:00
|
|
|
return GetEvent(
|
2018-07-04 10:05:20 +00:00
|
|
|
httpReq.Context(), request, query, vars["eventID"],
|
2017-09-06 11:38:22 +00:00
|
|
|
)
|
|
|
|
},
|
2018-03-13 15:55:45 +00:00
|
|
|
)).Methods(http.MethodGet)
|
2017-09-25 10:16:47 +00:00
|
|
|
|
2018-07-11 10:13:04 +00:00
|
|
|
v1fedmux.Handle("/state/{roomID}", common.MakeFedAPI(
|
2018-06-22 10:46:19 +00:00
|
|
|
"federation_get_event_auth", cfg.Matrix.ServerName, keys,
|
|
|
|
func(httpReq *http.Request, request *gomatrixserverlib.FederationRequest) util.JSONResponse {
|
2019-07-03 15:38:50 +00:00
|
|
|
vars, err := common.URLDecodeMapValues(mux.Vars(httpReq))
|
|
|
|
if err != nil {
|
|
|
|
return util.ErrorResponse(err)
|
|
|
|
}
|
2018-06-22 10:46:19 +00:00
|
|
|
return GetState(
|
2018-08-06 13:07:29 +00:00
|
|
|
httpReq.Context(), request, query, vars["roomID"],
|
2018-06-22 10:46:19 +00:00
|
|
|
)
|
|
|
|
},
|
|
|
|
)).Methods(http.MethodGet)
|
|
|
|
|
2018-07-11 10:13:04 +00:00
|
|
|
v1fedmux.Handle("/state_ids/{roomID}", common.MakeFedAPI(
|
2018-06-22 10:46:19 +00:00
|
|
|
"federation_get_event_auth", cfg.Matrix.ServerName, keys,
|
|
|
|
func(httpReq *http.Request, request *gomatrixserverlib.FederationRequest) util.JSONResponse {
|
2019-07-03 15:38:50 +00:00
|
|
|
vars, err := common.URLDecodeMapValues(mux.Vars(httpReq))
|
|
|
|
if err != nil {
|
|
|
|
return util.ErrorResponse(err)
|
|
|
|
}
|
2018-06-22 10:46:19 +00:00
|
|
|
return GetStateIDs(
|
2018-08-06 13:07:29 +00:00
|
|
|
httpReq.Context(), request, query, vars["roomID"],
|
2018-06-22 10:46:19 +00:00
|
|
|
)
|
|
|
|
},
|
|
|
|
)).Methods(http.MethodGet)
|
|
|
|
|
2018-08-07 13:41:50 +00:00
|
|
|
v1fedmux.Handle("/query/directory", common.MakeFedAPI(
|
2017-11-20 14:33:49 +00:00
|
|
|
"federation_query_room_alias", cfg.Matrix.ServerName, keys,
|
|
|
|
func(httpReq *http.Request, request *gomatrixserverlib.FederationRequest) util.JSONResponse {
|
|
|
|
return RoomAliasToID(
|
|
|
|
httpReq, federation, cfg, aliasAPI,
|
|
|
|
)
|
|
|
|
},
|
2018-03-13 15:55:45 +00:00
|
|
|
)).Methods(http.MethodGet)
|
2017-11-20 14:33:49 +00:00
|
|
|
|
2017-11-05 18:03:54 +00:00
|
|
|
v1fedmux.Handle("/query/profile", common.MakeFedAPI(
|
|
|
|
"federation_query_profile", cfg.Matrix.ServerName, keys,
|
|
|
|
func(httpReq *http.Request, request *gomatrixserverlib.FederationRequest) util.JSONResponse {
|
|
|
|
return GetProfile(
|
2018-08-20 09:45:17 +00:00
|
|
|
httpReq, accountDB, cfg, asAPI,
|
2017-11-05 18:03:54 +00:00
|
|
|
)
|
|
|
|
},
|
2018-03-13 15:55:45 +00:00
|
|
|
)).Methods(http.MethodGet)
|
2017-11-05 18:03:54 +00:00
|
|
|
|
2018-07-25 12:58:10 +00:00
|
|
|
v1fedmux.Handle("/user/devices/{userID}", common.MakeFedAPI(
|
|
|
|
"federation_user_devices", cfg.Matrix.ServerName, keys,
|
2018-06-26 10:32:43 +00:00
|
|
|
func(httpReq *http.Request, request *gomatrixserverlib.FederationRequest) util.JSONResponse {
|
2019-07-03 15:38:50 +00:00
|
|
|
vars, err := common.URLDecodeMapValues(mux.Vars(httpReq))
|
|
|
|
if err != nil {
|
|
|
|
return util.ErrorResponse(err)
|
|
|
|
}
|
2018-06-26 10:32:43 +00:00
|
|
|
return GetUserDevices(
|
|
|
|
httpReq, deviceDB, vars["userID"],
|
|
|
|
)
|
|
|
|
},
|
|
|
|
)).Methods(http.MethodGet)
|
|
|
|
|
2017-11-29 09:38:56 +00:00
|
|
|
v1fedmux.Handle("/make_join/{roomID}/{userID}", common.MakeFedAPI(
|
|
|
|
"federation_make_join", cfg.Matrix.ServerName, keys,
|
|
|
|
func(httpReq *http.Request, request *gomatrixserverlib.FederationRequest) util.JSONResponse {
|
2019-07-03 15:38:50 +00:00
|
|
|
vars, err := common.URLDecodeMapValues(mux.Vars(httpReq))
|
|
|
|
if err != nil {
|
|
|
|
return util.ErrorResponse(err)
|
|
|
|
}
|
2017-11-29 09:38:56 +00:00
|
|
|
roomID := vars["roomID"]
|
|
|
|
userID := vars["userID"]
|
|
|
|
return MakeJoin(
|
2018-07-17 15:45:30 +00:00
|
|
|
httpReq, request, cfg, query, roomID, userID,
|
2017-11-29 09:38:56 +00:00
|
|
|
)
|
|
|
|
},
|
2018-03-13 15:55:45 +00:00
|
|
|
)).Methods(http.MethodGet)
|
2017-11-29 09:38:56 +00:00
|
|
|
|
|
|
|
v1fedmux.Handle("/send_join/{roomID}/{userID}", common.MakeFedAPI(
|
|
|
|
"federation_send_join", cfg.Matrix.ServerName, keys,
|
|
|
|
func(httpReq *http.Request, request *gomatrixserverlib.FederationRequest) util.JSONResponse {
|
2019-07-03 15:38:50 +00:00
|
|
|
vars, err := common.URLDecodeMapValues(mux.Vars(httpReq))
|
|
|
|
if err != nil {
|
|
|
|
return util.ErrorResponse(err)
|
|
|
|
}
|
2017-11-29 09:38:56 +00:00
|
|
|
roomID := vars["roomID"]
|
|
|
|
userID := vars["userID"]
|
|
|
|
return SendJoin(
|
2018-08-06 13:07:29 +00:00
|
|
|
httpReq, request, cfg, query, producer, keys, roomID, userID,
|
2017-11-29 09:38:56 +00:00
|
|
|
)
|
|
|
|
},
|
2018-03-13 15:55:45 +00:00
|
|
|
)).Methods(http.MethodPut)
|
2017-11-29 09:38:56 +00:00
|
|
|
|
2018-07-11 10:07:44 +00:00
|
|
|
v1fedmux.Handle("/make_leave/{roomID}/{userID}", common.MakeFedAPI(
|
|
|
|
"federation_make_leave", cfg.Matrix.ServerName, keys,
|
|
|
|
func(httpReq *http.Request, request *gomatrixserverlib.FederationRequest) util.JSONResponse {
|
2019-07-03 15:38:50 +00:00
|
|
|
vars, err := common.URLDecodeMapValues(mux.Vars(httpReq))
|
|
|
|
if err != nil {
|
|
|
|
return util.ErrorResponse(err)
|
|
|
|
}
|
2018-07-11 10:07:44 +00:00
|
|
|
roomID := vars["roomID"]
|
|
|
|
userID := vars["userID"]
|
|
|
|
return MakeLeave(
|
|
|
|
httpReq, request, cfg, query, roomID, userID,
|
|
|
|
)
|
|
|
|
},
|
|
|
|
)).Methods(http.MethodGet)
|
|
|
|
|
|
|
|
v1fedmux.Handle("/send_leave/{roomID}/{userID}", common.MakeFedAPI(
|
|
|
|
"federation_send_leave", cfg.Matrix.ServerName, keys,
|
|
|
|
func(httpReq *http.Request, request *gomatrixserverlib.FederationRequest) util.JSONResponse {
|
2019-07-03 15:38:50 +00:00
|
|
|
vars, err := common.URLDecodeMapValues(mux.Vars(httpReq))
|
|
|
|
if err != nil {
|
|
|
|
return util.ErrorResponse(err)
|
|
|
|
}
|
2018-07-11 10:07:44 +00:00
|
|
|
roomID := vars["roomID"]
|
|
|
|
userID := vars["userID"]
|
|
|
|
return SendLeave(
|
|
|
|
httpReq, request, cfg, producer, keys, roomID, userID,
|
|
|
|
)
|
|
|
|
},
|
|
|
|
)).Methods(http.MethodPut)
|
|
|
|
|
2017-09-28 13:50:40 +00:00
|
|
|
v1fedmux.Handle("/version", common.MakeExternalAPI(
|
2017-09-25 10:16:47 +00:00
|
|
|
"federation_version",
|
|
|
|
func(httpReq *http.Request) util.JSONResponse {
|
2017-10-25 10:27:44 +00:00
|
|
|
return Version()
|
2017-09-25 10:16:47 +00:00
|
|
|
},
|
2018-03-13 15:55:45 +00:00
|
|
|
)).Methods(http.MethodGet)
|
2018-06-26 10:25:49 +00:00
|
|
|
|
2019-01-14 10:20:19 +00:00
|
|
|
v1fedmux.Handle("/get_missing_events/{roomID}", common.MakeFedAPI(
|
2018-06-26 10:25:49 +00:00
|
|
|
"federation_get_missing_events", cfg.Matrix.ServerName, keys,
|
|
|
|
func(httpReq *http.Request, request *gomatrixserverlib.FederationRequest) util.JSONResponse {
|
2019-07-03 15:38:50 +00:00
|
|
|
vars, err := common.URLDecodeMapValues(mux.Vars(httpReq))
|
|
|
|
if err != nil {
|
|
|
|
return util.ErrorResponse(err)
|
|
|
|
}
|
2018-06-26 10:25:49 +00:00
|
|
|
return GetMissingEvents(httpReq, request, query, vars["roomID"])
|
|
|
|
},
|
2019-01-14 10:20:19 +00:00
|
|
|
)).Methods(http.MethodPost)
|
2018-11-07 11:38:01 +00:00
|
|
|
|
|
|
|
v1fedmux.Handle("/backfill/{roomID}/", common.MakeFedAPI(
|
|
|
|
"federation_backfill", cfg.Matrix.ServerName, keys,
|
|
|
|
func(httpReq *http.Request, request *gomatrixserverlib.FederationRequest) util.JSONResponse {
|
2019-07-03 15:38:50 +00:00
|
|
|
vars, err := common.URLDecodeMapValues(mux.Vars(httpReq))
|
|
|
|
if err != nil {
|
|
|
|
return util.ErrorResponse(err)
|
|
|
|
}
|
2018-11-07 11:38:01 +00:00
|
|
|
return Backfill(httpReq, request, query, vars["roomID"], cfg)
|
|
|
|
},
|
|
|
|
)).Methods(http.MethodGet)
|
2017-05-19 15:06:41 +00:00
|
|
|
}
|