2017-09-08 16:17:12 +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.
|
|
|
|
|
2017-10-11 19:16:53 +02:00
|
|
|
package routing
|
2017-06-07 15:32:53 +02:00
|
|
|
|
|
|
|
import (
|
2017-09-13 12:03:41 +02:00
|
|
|
"context"
|
2017-06-07 15:32:53 +02:00
|
|
|
"encoding/json"
|
2017-08-23 16:13:47 +02:00
|
|
|
"net/http"
|
2020-09-28 12:32:59 +02:00
|
|
|
"sync"
|
2020-09-07 13:32:40 +02:00
|
|
|
"time"
|
2017-08-23 16:13:47 +02:00
|
|
|
|
2022-09-07 11:45:12 +02:00
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
2023-04-19 16:50:33 +02:00
|
|
|
"github.com/matrix-org/gomatrixserverlib/fclient"
|
2022-09-07 11:45:12 +02:00
|
|
|
"github.com/matrix-org/util"
|
|
|
|
|
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"
|
2017-06-07 15:32:53 +02:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/api"
|
2020-12-02 18:41:00 +01:00
|
|
|
"github.com/matrix-org/dendrite/setup/config"
|
2023-02-20 14:58:03 +01:00
|
|
|
userAPI "github.com/matrix-org/dendrite/userapi/api"
|
2023-05-10 00:46:49 +02:00
|
|
|
"github.com/matrix-org/gomatrixserverlib/spec"
|
2017-06-07 15:32:53 +02:00
|
|
|
)
|
|
|
|
|
2021-03-23 16:22:00 +01:00
|
|
|
const (
|
2023-06-06 15:16:55 +02:00
|
|
|
// Event was passed to the Roomserver
|
2021-03-23 16:22:00 +01:00
|
|
|
MetricsOutcomeOK = "ok"
|
|
|
|
// Event failed to be processed
|
|
|
|
MetricsOutcomeFail = "fail"
|
|
|
|
// Event failed auth checks
|
|
|
|
MetricsOutcomeRejected = "rejected"
|
|
|
|
// Terminated the transaction
|
|
|
|
MetricsOutcomeFatal = "fatal"
|
|
|
|
// The event has missing auth_events we need to fetch
|
|
|
|
MetricsWorkMissingAuthEvents = "missing_auth_events"
|
|
|
|
// No work had to be done as we had all prev/auth events
|
|
|
|
MetricsWorkDirect = "direct"
|
|
|
|
// The event has missing prev_events we need to call /g_m_e for
|
|
|
|
MetricsWorkMissingPrevEvents = "missing_prev_events"
|
|
|
|
)
|
|
|
|
|
2021-11-08 10:24:16 +01:00
|
|
|
var inFlightTxnsPerOrigin sync.Map // transaction ID -> chan util.JSONResponse
|
2021-07-02 13:33:27 +02:00
|
|
|
|
2017-06-07 15:32:53 +02:00
|
|
|
// Send implements /_matrix/federation/v1/send/{txnID}
|
|
|
|
func Send(
|
2017-09-04 14:14:01 +02:00
|
|
|
httpReq *http.Request,
|
2023-04-19 16:50:33 +02:00
|
|
|
request *fclient.FederationRequest,
|
2017-06-07 15:32:53 +02:00
|
|
|
txnID gomatrixserverlib.TransactionID,
|
2020-08-10 15:18:04 +02:00
|
|
|
cfg *config.FederationAPI,
|
2022-05-05 20:30:38 +02:00
|
|
|
rsAPI api.FederationRoomserverAPI,
|
2023-02-20 14:58:03 +01:00
|
|
|
keyAPI userAPI.FederationUserAPI,
|
2020-06-15 17:57:59 +02:00
|
|
|
keys gomatrixserverlib.JSONVerifier,
|
2023-04-24 18:23:25 +02:00
|
|
|
federation fclient.FederationClient,
|
2021-03-30 11:01:32 +02:00
|
|
|
mu *internal.MutexByRoom,
|
2022-03-29 14:14:35 +02:00
|
|
|
producer *producers.SyncAPIProducer,
|
2017-06-07 15:32:53 +02:00
|
|
|
) util.JSONResponse {
|
2021-11-08 10:24:16 +01:00
|
|
|
// First we should check if this origin has already submitted this
|
|
|
|
// txn ID to us. If they have and the txnIDs map contains an entry,
|
|
|
|
// the transaction is still being worked on. The new client can wait
|
|
|
|
// for it to complete rather than creating more work.
|
|
|
|
index := string(request.Origin()) + "\000" + string(txnID)
|
|
|
|
v, ok := inFlightTxnsPerOrigin.LoadOrStore(index, make(chan util.JSONResponse, 1))
|
|
|
|
ch := v.(chan util.JSONResponse)
|
|
|
|
if ok {
|
|
|
|
// This origin already submitted this txn ID to us, and the work
|
|
|
|
// is still taking place, so we'll just wait for it to finish.
|
|
|
|
ctx, cancel := context.WithTimeout(httpReq.Context(), time.Minute*5)
|
|
|
|
defer cancel()
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
// If the caller gives up then return straight away. We don't
|
|
|
|
// want to attempt to process what they sent us any further.
|
|
|
|
return util.JSONResponse{Code: http.StatusRequestTimeout}
|
|
|
|
case res := <-ch:
|
|
|
|
// The original task just finished processing so let's return
|
|
|
|
// the result of it.
|
|
|
|
if res.Code == 0 {
|
|
|
|
return util.JSONResponse{Code: http.StatusAccepted}
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Otherwise, store that we're currently working on this txn from
|
|
|
|
// this origin. When we're done processing, close the channel.
|
|
|
|
defer close(ch)
|
|
|
|
defer inFlightTxnsPerOrigin.Delete(index)
|
|
|
|
|
2020-03-27 17:28:22 +01:00
|
|
|
var txnEvents struct {
|
2020-03-30 17:40:28 +02:00
|
|
|
PDUs []json.RawMessage `json:"pdus"`
|
|
|
|
EDUs []gomatrixserverlib.EDU `json:"edus"`
|
2020-03-27 17:28:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := json.Unmarshal(request.Content(), &txnEvents); err != nil {
|
2017-06-07 15:32:53 +02:00
|
|
|
return util.JSONResponse{
|
2018-03-13 16:55:45 +01:00
|
|
|
Code: http.StatusBadRequest,
|
2023-05-10 00:46:49 +02:00
|
|
|
JSON: spec.NotJSON("The request body could not be decoded into valid JSON. " + err.Error()),
|
2017-06-07 15:32:53 +02:00
|
|
|
}
|
|
|
|
}
|
2020-06-23 14:15:15 +02: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-10 00:46:49 +02:00
|
|
|
JSON: spec.BadJSON("max 50 pdus / 100 edus"),
|
2020-06-23 14:15:15 +02:00
|
|
|
}
|
|
|
|
}
|
2017-06-07 15:32:53 +02:00
|
|
|
|
2023-01-23 18:55:12 +01:00
|
|
|
t := internal.NewTxnReq(
|
|
|
|
rsAPI,
|
|
|
|
keyAPI,
|
|
|
|
cfg.Matrix.ServerName,
|
|
|
|
keys,
|
|
|
|
mu,
|
|
|
|
producer,
|
|
|
|
cfg.Matrix.Presence.EnableInbound,
|
|
|
|
txnEvents.PDUs,
|
|
|
|
txnEvents.EDUs,
|
|
|
|
request.Origin(),
|
|
|
|
txnID,
|
|
|
|
cfg.Matrix.ServerName)
|
2017-06-07 15:32:53 +02:00
|
|
|
|
2022-01-31 11:48:28 +01:00
|
|
|
util.GetLogger(httpReq.Context()).Debugf("Received transaction %q from %q containing %d PDUs, %d EDUs", txnID, request.Origin(), len(t.PDUs), len(t.EDUs))
|
2020-03-27 17:28:22 +01:00
|
|
|
|
2023-01-23 18:55:12 +01:00
|
|
|
resp, jsonErr := t.ProcessTransaction(httpReq.Context())
|
2020-06-23 14:15:15 +02:00
|
|
|
if jsonErr != nil {
|
|
|
|
util.GetLogger(httpReq.Context()).WithField("jsonErr", jsonErr).Error("t.processTransaction failed")
|
|
|
|
return *jsonErr
|
2017-06-07 15:32:53 +02:00
|
|
|
}
|
2020-05-13 14:01:45 +02:00
|
|
|
|
|
|
|
// https://matrix.org/docs/spec/server_server/r0.1.3#put-matrix-federation-v1-send-txnid
|
|
|
|
// Status code 200:
|
|
|
|
// The result of processing the transaction. The server is to use this response
|
|
|
|
// even in the event of one or more PDUs failing to be processed.
|
2021-11-08 10:24:16 +01:00
|
|
|
res := util.JSONResponse{
|
2020-05-13 14:01:45 +02:00
|
|
|
Code: http.StatusOK,
|
|
|
|
JSON: resp,
|
2017-06-07 15:32:53 +02:00
|
|
|
}
|
2021-11-08 10:24:16 +01:00
|
|
|
ch <- res
|
|
|
|
return res
|
2017-06-07 15:32:53 +02:00
|
|
|
}
|