2017-09-08 14:17:12 +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.
|
|
|
|
|
2017-10-11 17:16:53 +00:00
|
|
|
package routing
|
2017-06-07 13:32:53 +00:00
|
|
|
|
|
|
|
import (
|
2017-09-13 10:03:41 +00:00
|
|
|
"context"
|
2017-06-07 13:32:53 +00:00
|
|
|
"encoding/json"
|
2017-08-23 14:13:47 +00:00
|
|
|
"net/http"
|
2020-09-28 10:32:59 +00:00
|
|
|
"sync"
|
2020-09-07 11:32:40 +00:00
|
|
|
"time"
|
2017-08-23 14:13:47 +00:00
|
|
|
|
2022-09-07 09:45:12 +00:00
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
2023-04-19 14:50:33 +00:00
|
|
|
"github.com/matrix-org/gomatrixserverlib/fclient"
|
2022-09-07 09:45:12 +00:00
|
|
|
"github.com/matrix-org/util"
|
|
|
|
|
2022-03-29 12:14:35 +00:00
|
|
|
"github.com/matrix-org/dendrite/federationapi/producers"
|
2021-03-30 09:01:32 +00:00
|
|
|
"github.com/matrix-org/dendrite/internal"
|
2017-06-07 13:32:53 +00:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/api"
|
2020-12-02 17:41:00 +00:00
|
|
|
"github.com/matrix-org/dendrite/setup/config"
|
2023-02-20 13:58:03 +00:00
|
|
|
userAPI "github.com/matrix-org/dendrite/userapi/api"
|
2023-05-09 22:46:49 +00:00
|
|
|
"github.com/matrix-org/gomatrixserverlib/spec"
|
2017-06-07 13:32:53 +00:00
|
|
|
)
|
|
|
|
|
2021-03-23 15:22:00 +00:00
|
|
|
const (
|
2023-06-06 13:16:55 +00:00
|
|
|
// Event was passed to the Roomserver
|
2021-03-23 15:22:00 +00: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 09:24:16 +00:00
|
|
|
var inFlightTxnsPerOrigin sync.Map // transaction ID -> chan util.JSONResponse
|
2021-07-02 11:33:27 +00:00
|
|
|
|
2017-06-07 13:32:53 +00:00
|
|
|
// Send implements /_matrix/federation/v1/send/{txnID}
|
|
|
|
func Send(
|
2017-09-04 12:14:01 +00:00
|
|
|
httpReq *http.Request,
|
2023-04-19 14:50:33 +00:00
|
|
|
request *fclient.FederationRequest,
|
2017-06-07 13:32:53 +00:00
|
|
|
txnID gomatrixserverlib.TransactionID,
|
2020-08-10 13:18:04 +00:00
|
|
|
cfg *config.FederationAPI,
|
2022-05-05 18:30:38 +00:00
|
|
|
rsAPI api.FederationRoomserverAPI,
|
2023-02-20 13:58:03 +00:00
|
|
|
keyAPI userAPI.FederationUserAPI,
|
2020-06-15 15:57:59 +00:00
|
|
|
keys gomatrixserverlib.JSONVerifier,
|
2023-04-24 16:23:25 +00:00
|
|
|
federation fclient.FederationClient,
|
2021-03-30 09:01:32 +00:00
|
|
|
mu *internal.MutexByRoom,
|
2022-03-29 12:14:35 +00:00
|
|
|
producer *producers.SyncAPIProducer,
|
2017-06-07 13:32:53 +00:00
|
|
|
) util.JSONResponse {
|
2021-11-08 09:24:16 +00: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 16:28:22 +00:00
|
|
|
var txnEvents struct {
|
2020-03-30 15:40:28 +00:00
|
|
|
PDUs []json.RawMessage `json:"pdus"`
|
|
|
|
EDUs []gomatrixserverlib.EDU `json:"edus"`
|
2020-03-27 16:28:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := json.Unmarshal(request.Content(), &txnEvents); err != nil {
|
2017-06-07 13:32:53 +00:00
|
|
|
return util.JSONResponse{
|
2018-03-13 15:55:45 +00:00
|
|
|
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()),
|
2017-06-07 13:32:53 +00:00
|
|
|
}
|
|
|
|
}
|
2020-06-23 12:15:15 +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"),
|
2020-06-23 12:15:15 +00:00
|
|
|
}
|
|
|
|
}
|
2017-06-07 13:32:53 +00:00
|
|
|
|
2023-01-23 17:55:12 +00: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 13:32:53 +00:00
|
|
|
|
2022-01-31 10:48:28 +00: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 16:28:22 +00:00
|
|
|
|
2023-01-23 17:55:12 +00:00
|
|
|
resp, jsonErr := t.ProcessTransaction(httpReq.Context())
|
2020-06-23 12:15:15 +00:00
|
|
|
if jsonErr != nil {
|
|
|
|
util.GetLogger(httpReq.Context()).WithField("jsonErr", jsonErr).Error("t.processTransaction failed")
|
|
|
|
return *jsonErr
|
2017-06-07 13:32:53 +00:00
|
|
|
}
|
2020-05-13 12:01:45 +00: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 09:24:16 +00:00
|
|
|
res := util.JSONResponse{
|
2020-05-13 12:01:45 +00:00
|
|
|
Code: http.StatusOK,
|
|
|
|
JSON: resp,
|
2017-06-07 13:32:53 +00:00
|
|
|
}
|
2021-11-08 09:24:16 +00:00
|
|
|
ch <- res
|
|
|
|
return res
|
2017-06-07 13:32:53 +00:00
|
|
|
}
|