2023-01-23 17:55:12 +00:00
|
|
|
// Copyright 2022 The Matrix.org Foundation C.I.C.
|
|
|
|
//
|
|
|
|
// 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 (
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/matrix-org/dendrite/relayapi/api"
|
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
2023-04-06 08:55:01 +00:00
|
|
|
"github.com/matrix-org/gomatrixserverlib/fclient"
|
2023-04-19 14:50:33 +00:00
|
|
|
"github.com/matrix-org/gomatrixserverlib/spec"
|
2023-01-23 17:55:12 +00:00
|
|
|
"github.com/matrix-org/util"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
2023-01-29 19:26:16 +00:00
|
|
|
// SendTransactionToRelay implements PUT /_matrix/federation/v1/send_relay/{txnID}/{userID}
|
2023-01-23 17:55:12 +00:00
|
|
|
// This endpoint can be extracted into a separate relay server service.
|
|
|
|
func SendTransactionToRelay(
|
|
|
|
httpReq *http.Request,
|
2023-04-19 14:50:33 +00:00
|
|
|
fedReq *fclient.FederationRequest,
|
2023-01-23 17:55:12 +00:00
|
|
|
relayAPI api.RelayInternalAPI,
|
|
|
|
txnID gomatrixserverlib.TransactionID,
|
2023-04-19 14:50:33 +00:00
|
|
|
userID spec.UserID,
|
2023-01-23 17:55:12 +00:00
|
|
|
) util.JSONResponse {
|
2023-04-28 19:31:21 +00:00
|
|
|
logrus.Infof("Processing send_relay for %s", userID.String())
|
2023-01-29 19:26:16 +00:00
|
|
|
|
2023-04-06 08:55:01 +00:00
|
|
|
var txnEvents fclient.RelayEvents
|
2023-01-23 17:55:12 +00:00
|
|
|
if err := json.Unmarshal(fedReq.Content(), &txnEvents); err != nil {
|
|
|
|
logrus.Info("The request body could not be decoded into valid JSON." + err.Error())
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
2023-05-09 22:46:49 +00:00
|
|
|
JSON: spec.NotJSON("The request body could not be decoded into valid JSON." + err.Error()),
|
2023-01-23 17:55:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Transactions are limited in size; they can have at most 50 PDUs and 100 EDUs.
|
|
|
|
// https://matrix.org/docs/spec/server_server/latest#transactions
|
|
|
|
if len(txnEvents.PDUs) > 50 || len(txnEvents.EDUs) > 100 {
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
2023-05-09 22:46:49 +00:00
|
|
|
JSON: spec.BadJSON("max 50 pdus / 100 edus"),
|
2023-01-23 17:55:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
t := gomatrixserverlib.Transaction{}
|
|
|
|
t.PDUs = txnEvents.PDUs
|
|
|
|
t.EDUs = txnEvents.EDUs
|
|
|
|
t.Origin = fedReq.Origin()
|
|
|
|
t.TransactionID = txnID
|
|
|
|
t.Destination = userID.Domain()
|
|
|
|
|
|
|
|
util.GetLogger(httpReq.Context()).Warnf("Received transaction %q from %q containing %d PDUs, %d EDUs", txnID, fedReq.Origin(), len(t.PDUs), len(t.EDUs))
|
|
|
|
|
|
|
|
err := relayAPI.PerformStoreTransaction(httpReq.Context(), t, userID)
|
|
|
|
if err != nil {
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusInternalServerError,
|
2023-05-09 22:46:49 +00:00
|
|
|
JSON: spec.BadJSON("could not store the transaction for forwarding"),
|
2023-01-23 17:55:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return util.JSONResponse{Code: 200}
|
|
|
|
}
|