2020-06-04 08:27:10 -05:00
|
|
|
package inthttp
|
|
|
|
|
|
|
|
import (
|
2022-08-08 05:22:33 -05:00
|
|
|
"context"
|
2020-06-04 08:27:10 -05:00
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
2021-11-24 04:45:23 -06:00
|
|
|
"github.com/matrix-org/dendrite/federationapi/api"
|
2020-06-12 08:55:57 -05:00
|
|
|
"github.com/matrix-org/dendrite/internal/httputil"
|
2022-08-08 08:56:26 -05:00
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
2020-06-04 08:27:10 -05:00
|
|
|
"github.com/matrix-org/util"
|
|
|
|
)
|
|
|
|
|
2021-11-24 04:45:23 -06:00
|
|
|
// AddRoutes adds the FederationInternalAPI handlers to the http.ServeMux.
|
2020-08-17 05:40:49 -05:00
|
|
|
// nolint:gocyclo
|
2021-11-24 04:45:23 -06:00
|
|
|
func AddRoutes(intAPI api.FederationInternalAPI, internalAPIMux *mux.Router) {
|
2022-08-08 05:22:33 -05:00
|
|
|
internalAPIMux.Handle(
|
2021-11-24 04:45:23 -06:00
|
|
|
FederationAPIQueryJoinedHostServerNamesInRoomPath,
|
2022-08-08 05:22:33 -05:00
|
|
|
httputil.MakeInternalRPCAPI("QueryJoinedHostServerNamesInRoom", intAPI.QueryJoinedHostServerNamesInRoom),
|
|
|
|
)
|
2022-08-05 09:29:08 -05:00
|
|
|
|
2022-08-08 05:22:33 -05:00
|
|
|
internalAPIMux.Handle(
|
2022-08-08 03:44:31 -05:00
|
|
|
FederationAPIPerformInviteRequestPath,
|
2022-08-08 05:22:33 -05:00
|
|
|
httputil.MakeInternalRPCAPI("PerformInvite", intAPI.PerformInvite),
|
|
|
|
)
|
2022-08-08 03:44:31 -05:00
|
|
|
|
2022-08-08 05:22:33 -05:00
|
|
|
internalAPIMux.Handle(
|
2022-08-05 09:29:08 -05:00
|
|
|
FederationAPIPerformLeaveRequestPath,
|
2022-08-08 05:22:33 -05:00
|
|
|
httputil.MakeInternalRPCAPI("PerformLeave", intAPI.PerformLeave),
|
|
|
|
)
|
2022-08-05 09:29:08 -05:00
|
|
|
|
2022-08-08 05:22:33 -05:00
|
|
|
internalAPIMux.Handle(
|
2022-08-05 09:29:08 -05:00
|
|
|
FederationAPIPerformDirectoryLookupRequestPath,
|
2022-08-08 05:22:33 -05:00
|
|
|
httputil.MakeInternalRPCAPI("PerformDirectoryLookupRequest", intAPI.PerformDirectoryLookup),
|
|
|
|
)
|
2022-08-05 09:29:08 -05:00
|
|
|
|
2022-08-08 05:22:33 -05:00
|
|
|
internalAPIMux.Handle(
|
2022-08-05 09:29:08 -05:00
|
|
|
FederationAPIPerformBroadcastEDUPath,
|
2022-08-08 05:31:46 -05:00
|
|
|
httputil.MakeInternalRPCAPI("PerformBroadcastEDU", intAPI.PerformBroadcastEDU),
|
2022-08-08 05:22:33 -05:00
|
|
|
)
|
2022-08-05 09:29:08 -05:00
|
|
|
|
2020-08-13 06:16:37 -05:00
|
|
|
internalAPIMux.Handle(
|
2021-11-24 04:45:23 -06:00
|
|
|
FederationAPIPerformJoinRequestPath,
|
2022-08-08 05:22:33 -05:00
|
|
|
httputil.MakeInternalRPCAPI(
|
|
|
|
"PerformJoinRequest",
|
|
|
|
func(ctx context.Context, req *api.PerformJoinRequest, res *api.PerformJoinResponse) error {
|
|
|
|
intAPI.PerformJoin(ctx, req, res)
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
),
|
2020-06-04 08:27:10 -05:00
|
|
|
)
|
2022-08-05 09:29:08 -05:00
|
|
|
|
2020-08-20 11:03:07 -05:00
|
|
|
internalAPIMux.Handle(
|
2022-08-08 05:22:33 -05:00
|
|
|
FederationAPIPerformJoinRequestPath,
|
|
|
|
httputil.MakeInternalProxyAPI(
|
|
|
|
"GetUserDevices",
|
2022-08-08 08:56:26 -05:00
|
|
|
func(ctx context.Context, req *getUserDevices) (*gomatrixserverlib.RespUserDevices, error) {
|
2022-08-08 05:22:33 -05:00
|
|
|
res, err := intAPI.GetUserDevices(ctx, req.S, req.UserID)
|
2022-08-08 08:56:26 -05:00
|
|
|
return &res, federationClientError(err)
|
2022-08-08 05:22:33 -05:00
|
|
|
},
|
|
|
|
),
|
2020-08-20 11:03:07 -05:00
|
|
|
)
|
2022-08-08 05:22:33 -05:00
|
|
|
|
2020-08-20 11:03:07 -05:00
|
|
|
internalAPIMux.Handle(
|
2021-11-24 04:45:23 -06:00
|
|
|
FederationAPIClaimKeysPath,
|
2022-08-08 05:22:33 -05:00
|
|
|
httputil.MakeInternalProxyAPI(
|
|
|
|
"ClaimKeys",
|
2022-08-08 08:56:26 -05:00
|
|
|
func(ctx context.Context, req *claimKeys) (*gomatrixserverlib.RespClaimKeys, error) {
|
2022-08-08 05:22:33 -05:00
|
|
|
res, err := intAPI.ClaimKeys(ctx, req.S, req.OneTimeKeys)
|
2022-08-08 08:56:26 -05:00
|
|
|
return &res, federationClientError(err)
|
2022-08-08 05:22:33 -05:00
|
|
|
},
|
|
|
|
),
|
2020-08-20 11:03:07 -05:00
|
|
|
)
|
2022-08-08 05:22:33 -05:00
|
|
|
|
2020-08-20 11:03:07 -05:00
|
|
|
internalAPIMux.Handle(
|
2021-11-24 04:45:23 -06:00
|
|
|
FederationAPIQueryKeysPath,
|
2022-08-08 05:22:33 -05:00
|
|
|
httputil.MakeInternalProxyAPI(
|
|
|
|
"QueryKeys",
|
2022-08-08 08:56:26 -05:00
|
|
|
func(ctx context.Context, req *queryKeys) (*gomatrixserverlib.RespQueryKeys, error) {
|
2022-08-08 05:22:33 -05:00
|
|
|
res, err := intAPI.QueryKeys(ctx, req.S, req.Keys)
|
2022-08-08 08:56:26 -05:00
|
|
|
return &res, federationClientError(err)
|
2022-08-08 05:22:33 -05:00
|
|
|
},
|
|
|
|
),
|
2020-08-20 11:03:07 -05:00
|
|
|
)
|
2022-08-08 05:22:33 -05:00
|
|
|
|
2020-09-02 09:26:30 -05:00
|
|
|
internalAPIMux.Handle(
|
2021-11-24 04:45:23 -06:00
|
|
|
FederationAPIBackfillPath,
|
2022-08-08 05:22:33 -05:00
|
|
|
httputil.MakeInternalProxyAPI(
|
|
|
|
"Backfill",
|
2022-08-08 08:56:26 -05:00
|
|
|
func(ctx context.Context, req *backfill) (*gomatrixserverlib.Transaction, error) {
|
2022-08-08 05:22:33 -05:00
|
|
|
res, err := intAPI.Backfill(ctx, req.S, req.RoomID, req.Limit, req.EventIDs)
|
2022-08-08 08:56:26 -05:00
|
|
|
return &res, federationClientError(err)
|
2022-08-08 05:22:33 -05:00
|
|
|
},
|
|
|
|
),
|
2020-09-02 09:26:30 -05:00
|
|
|
)
|
2022-08-08 05:22:33 -05:00
|
|
|
|
2020-09-02 09:26:30 -05:00
|
|
|
internalAPIMux.Handle(
|
2021-11-24 04:45:23 -06:00
|
|
|
FederationAPILookupStatePath,
|
2022-08-08 05:22:33 -05:00
|
|
|
httputil.MakeInternalProxyAPI(
|
|
|
|
"LookupState",
|
2022-08-08 08:56:26 -05:00
|
|
|
func(ctx context.Context, req *lookupState) (*gomatrixserverlib.RespState, error) {
|
2022-08-08 05:22:33 -05:00
|
|
|
res, err := intAPI.LookupState(ctx, req.S, req.RoomID, req.EventID, req.RoomVersion)
|
2022-08-08 08:56:26 -05:00
|
|
|
return &res, federationClientError(err)
|
2022-08-08 05:22:33 -05:00
|
|
|
},
|
|
|
|
),
|
2020-09-02 09:26:30 -05:00
|
|
|
)
|
2022-08-08 05:22:33 -05:00
|
|
|
|
2020-09-02 09:26:30 -05:00
|
|
|
internalAPIMux.Handle(
|
2021-11-24 04:45:23 -06:00
|
|
|
FederationAPILookupStateIDsPath,
|
2022-08-08 05:22:33 -05:00
|
|
|
httputil.MakeInternalProxyAPI(
|
|
|
|
"LookupStateIDs",
|
2022-08-08 08:56:26 -05:00
|
|
|
func(ctx context.Context, req *lookupStateIDs) (*gomatrixserverlib.RespStateIDs, error) {
|
2022-08-08 05:47:17 -05:00
|
|
|
res, err := intAPI.LookupStateIDs(ctx, req.S, req.RoomID, req.EventID)
|
2022-08-08 08:56:26 -05:00
|
|
|
return &res, federationClientError(err)
|
2022-08-08 05:22:33 -05:00
|
|
|
},
|
|
|
|
),
|
2020-09-02 09:26:30 -05:00
|
|
|
)
|
2022-08-08 05:22:33 -05:00
|
|
|
|
2022-01-27 08:29:14 -06:00
|
|
|
internalAPIMux.Handle(
|
|
|
|
FederationAPILookupMissingEventsPath,
|
2022-08-08 05:22:33 -05:00
|
|
|
httputil.MakeInternalProxyAPI(
|
|
|
|
"LookupMissingEvents",
|
2022-08-08 08:56:26 -05:00
|
|
|
func(ctx context.Context, req *lookupMissingEvents) (*gomatrixserverlib.RespMissingEvents, error) {
|
2022-08-08 05:22:33 -05:00
|
|
|
res, err := intAPI.LookupMissingEvents(ctx, req.S, req.RoomID, req.Missing, req.RoomVersion)
|
2022-08-08 08:56:26 -05:00
|
|
|
return &res, federationClientError(err)
|
2022-08-08 05:22:33 -05:00
|
|
|
},
|
|
|
|
),
|
2022-01-27 08:29:14 -06:00
|
|
|
)
|
2022-08-08 05:22:33 -05:00
|
|
|
|
2020-09-02 09:26:30 -05:00
|
|
|
internalAPIMux.Handle(
|
2021-11-24 04:45:23 -06:00
|
|
|
FederationAPIGetEventPath,
|
2022-08-08 05:22:33 -05:00
|
|
|
httputil.MakeInternalProxyAPI(
|
|
|
|
"GetEvent",
|
2022-08-08 08:56:26 -05:00
|
|
|
func(ctx context.Context, req *getEvent) (*gomatrixserverlib.Transaction, error) {
|
2022-08-08 05:22:33 -05:00
|
|
|
res, err := intAPI.GetEvent(ctx, req.S, req.EventID)
|
2022-08-08 08:56:26 -05:00
|
|
|
return &res, federationClientError(err)
|
2022-08-08 05:22:33 -05:00
|
|
|
},
|
|
|
|
),
|
2022-01-27 08:29:14 -06:00
|
|
|
)
|
2022-08-08 05:22:33 -05:00
|
|
|
|
2022-01-27 08:29:14 -06:00
|
|
|
internalAPIMux.Handle(
|
|
|
|
FederationAPIGetEventAuthPath,
|
2022-08-08 05:22:33 -05:00
|
|
|
httputil.MakeInternalProxyAPI(
|
|
|
|
"GetEventAuth",
|
2022-08-08 08:56:26 -05:00
|
|
|
func(ctx context.Context, req *getEventAuth) (*gomatrixserverlib.RespEventAuth, error) {
|
2022-08-08 05:22:33 -05:00
|
|
|
res, err := intAPI.GetEventAuth(ctx, req.S, req.RoomVersion, req.RoomID, req.EventID)
|
2022-08-08 08:56:26 -05:00
|
|
|
return &res, federationClientError(err)
|
2022-08-08 05:22:33 -05:00
|
|
|
},
|
|
|
|
),
|
2020-09-02 09:26:30 -05:00
|
|
|
)
|
2022-08-08 05:22:33 -05:00
|
|
|
|
2020-09-22 08:40:54 -05:00
|
|
|
internalAPIMux.Handle(
|
2021-11-24 04:45:23 -06:00
|
|
|
FederationAPIQueryServerKeysPath,
|
2022-08-08 05:22:33 -05:00
|
|
|
httputil.MakeInternalRPCAPI("QueryServerKeys", intAPI.QueryServerKeys),
|
2020-09-22 08:40:54 -05:00
|
|
|
)
|
2022-08-08 05:22:33 -05:00
|
|
|
|
2020-09-22 08:40:54 -05:00
|
|
|
internalAPIMux.Handle(
|
2021-11-24 04:45:23 -06:00
|
|
|
FederationAPILookupServerKeysPath,
|
2022-08-08 05:22:33 -05:00
|
|
|
httputil.MakeInternalProxyAPI(
|
|
|
|
"LookupServerKeys",
|
2022-08-08 08:56:26 -05:00
|
|
|
func(ctx context.Context, req *lookupServerKeys) (*[]gomatrixserverlib.ServerKeys, error) {
|
2022-08-08 05:22:33 -05:00
|
|
|
res, err := intAPI.LookupServerKeys(ctx, req.S, req.KeyRequests)
|
2022-08-08 08:56:26 -05:00
|
|
|
return &res, federationClientError(err)
|
2022-08-08 05:22:33 -05:00
|
|
|
},
|
|
|
|
),
|
2020-09-22 08:40:54 -05:00
|
|
|
)
|
2022-08-08 05:22:33 -05:00
|
|
|
|
2020-12-04 08:11:01 -06:00
|
|
|
internalAPIMux.Handle(
|
2021-11-24 04:45:23 -06:00
|
|
|
FederationAPIEventRelationshipsPath,
|
2022-08-08 05:22:33 -05:00
|
|
|
httputil.MakeInternalProxyAPI(
|
|
|
|
"MSC2836EventRelationships",
|
2022-08-08 08:56:26 -05:00
|
|
|
func(ctx context.Context, req *eventRelationships) (*gomatrixserverlib.MSC2836EventRelationshipsResponse, error) {
|
2022-08-08 05:22:33 -05:00
|
|
|
res, err := intAPI.MSC2836EventRelationships(ctx, req.S, req.Req, req.RoomVer)
|
2022-08-08 08:56:26 -05:00
|
|
|
return &res, federationClientError(err)
|
2022-08-08 05:22:33 -05:00
|
|
|
},
|
|
|
|
),
|
2020-12-04 08:11:01 -06:00
|
|
|
)
|
2022-08-08 05:22:33 -05:00
|
|
|
|
2021-01-19 11:14:25 -06:00
|
|
|
internalAPIMux.Handle(
|
2021-11-24 04:45:23 -06:00
|
|
|
FederationAPISpacesSummaryPath,
|
2022-08-08 05:22:33 -05:00
|
|
|
httputil.MakeInternalProxyAPI(
|
|
|
|
"MSC2946SpacesSummary",
|
2022-08-08 08:56:26 -05:00
|
|
|
func(ctx context.Context, req *spacesReq) (*gomatrixserverlib.MSC2946SpacesResponse, error) {
|
2022-08-08 05:22:33 -05:00
|
|
|
res, err := intAPI.MSC2946Spaces(ctx, req.S, req.RoomID, req.SuggestedOnly)
|
2022-08-08 08:56:26 -05:00
|
|
|
return &res, federationClientError(err)
|
2022-08-08 05:22:33 -05:00
|
|
|
},
|
|
|
|
),
|
2021-01-19 11:14:25 -06:00
|
|
|
)
|
2022-08-08 05:22:33 -05:00
|
|
|
|
|
|
|
// TODO: Look at this shape
|
2021-11-24 04:45:23 -06:00
|
|
|
internalAPIMux.Handle(FederationAPIQueryPublicKeyPath,
|
|
|
|
httputil.MakeInternalAPI("queryPublicKeys", func(req *http.Request) util.JSONResponse {
|
|
|
|
request := api.QueryPublicKeysRequest{}
|
|
|
|
response := api.QueryPublicKeysResponse{}
|
|
|
|
if err := json.NewDecoder(req.Body).Decode(&request); err != nil {
|
|
|
|
return util.MessageResponse(http.StatusBadRequest, err.Error())
|
|
|
|
}
|
|
|
|
keys, err := intAPI.FetchKeys(req.Context(), request.Requests)
|
|
|
|
if err != nil {
|
|
|
|
return util.ErrorResponse(err)
|
|
|
|
}
|
|
|
|
response.Results = keys
|
|
|
|
return util.JSONResponse{Code: http.StatusOK, JSON: &response}
|
|
|
|
}),
|
|
|
|
)
|
2022-08-08 05:22:33 -05:00
|
|
|
|
|
|
|
// TODO: Look at this shape
|
2021-11-24 04:45:23 -06:00
|
|
|
internalAPIMux.Handle(FederationAPIInputPublicKeyPath,
|
|
|
|
httputil.MakeInternalAPI("inputPublicKeys", func(req *http.Request) util.JSONResponse {
|
|
|
|
request := api.InputPublicKeysRequest{}
|
|
|
|
response := api.InputPublicKeysResponse{}
|
|
|
|
if err := json.NewDecoder(req.Body).Decode(&request); err != nil {
|
|
|
|
return util.MessageResponse(http.StatusBadRequest, err.Error())
|
|
|
|
}
|
|
|
|
if err := intAPI.StoreKeys(req.Context(), request.Keys); err != nil {
|
|
|
|
return util.ErrorResponse(err)
|
|
|
|
}
|
|
|
|
return util.JSONResponse{Code: http.StatusOK, JSON: &response}
|
|
|
|
}),
|
|
|
|
)
|
2020-06-04 08:27:10 -05:00
|
|
|
}
|
2022-08-08 05:22:33 -05:00
|
|
|
|
|
|
|
func federationClientError(err error) *api.FederationClientError {
|
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if ferr, ok := err.(*api.FederationClientError); ok {
|
|
|
|
return ferr
|
|
|
|
} else {
|
|
|
|
return &api.FederationClientError{
|
|
|
|
Err: err.Error(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|