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