2015-01-26 03:45:24 -07:00
|
|
|
# -*- coding: utf-8 -*-
|
2016-01-06 21:26:29 -07:00
|
|
|
# Copyright 2015, 2016 OpenMarket Ltd
|
2018-04-17 16:41:36 -06:00
|
|
|
# Copyright 2018 New Vector Ltd
|
2015-01-26 03:45:24 -07:00
|
|
|
#
|
|
|
|
# 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-12-30 11:40:19 -07:00
|
|
|
import logging
|
2015-01-26 03:45:24 -07:00
|
|
|
|
2018-07-04 08:31:00 -06:00
|
|
|
import six
|
2018-07-09 00:09:20 -06:00
|
|
|
from six import iteritems
|
|
|
|
|
|
|
|
from canonicaljson import json
|
|
|
|
from prometheus_client import Counter
|
|
|
|
|
2017-12-30 11:40:19 -07:00
|
|
|
from twisted.internet import defer
|
2018-07-04 08:31:00 -06:00
|
|
|
from twisted.internet.abstract import isIPAddress
|
2018-07-23 15:13:19 -06:00
|
|
|
from twisted.python import failure
|
2015-01-26 03:45:24 -07:00
|
|
|
|
2018-07-04 08:31:00 -06:00
|
|
|
from synapse.api.constants import EventTypes
|
2018-07-25 15:25:41 -06:00
|
|
|
from synapse.api.errors import (
|
|
|
|
AuthError,
|
|
|
|
FederationError,
|
|
|
|
IncompatibleRoomVersionError,
|
|
|
|
NotFoundError,
|
|
|
|
SynapseError,
|
|
|
|
)
|
2017-12-30 11:40:19 -07:00
|
|
|
from synapse.crypto.event_signing import compute_event_signature
|
2018-07-09 00:09:20 -06:00
|
|
|
from synapse.federation.federation_base import FederationBase, event_from_pdu_json
|
2018-03-12 08:07:39 -06:00
|
|
|
from synapse.federation.persistence import TransactionActions
|
2017-12-30 11:40:19 -07:00
|
|
|
from synapse.federation.units import Edu, Transaction
|
2018-07-04 08:31:00 -06:00
|
|
|
from synapse.http.endpoint import parse_server_name
|
2018-07-26 04:44:22 -06:00
|
|
|
from synapse.replication.http.federation import (
|
|
|
|
ReplicationFederationSendEduRestServlet,
|
|
|
|
ReplicationGetQueryRestServlet,
|
|
|
|
)
|
2017-12-30 11:40:19 -07:00
|
|
|
from synapse.types import get_domain_from_id
|
2018-10-17 09:14:04 -06:00
|
|
|
from synapse.util import glob_to_regex
|
2018-08-10 07:50:21 -06:00
|
|
|
from synapse.util.async_helpers import Linearizer, concurrently_execute
|
2017-12-30 11:40:19 -07:00
|
|
|
from synapse.util.caches.response_cache import ResponseCache
|
2018-09-27 04:25:34 -06:00
|
|
|
from synapse.util.logcontext import nested_logging_context
|
2015-01-26 03:45:24 -07:00
|
|
|
from synapse.util.logutils import log_function
|
|
|
|
|
2017-10-06 09:07:20 -06:00
|
|
|
# when processing incoming transactions, we try to handle multiple rooms in
|
|
|
|
# parallel, up to this limit.
|
|
|
|
TRANSACTION_CONCURRENCY_LIMIT = 10
|
2015-01-26 03:45:24 -07:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2018-05-21 18:47:37 -06:00
|
|
|
received_pdus_counter = Counter("synapse_federation_server_received_pdus", "")
|
2015-03-10 09:29:22 -06:00
|
|
|
|
2018-05-21 18:47:37 -06:00
|
|
|
received_edus_counter = Counter("synapse_federation_server_received_edus", "")
|
2015-03-10 09:29:22 -06:00
|
|
|
|
2018-05-22 15:54:22 -06:00
|
|
|
received_queries_counter = Counter(
|
|
|
|
"synapse_federation_server_received_queries", "", ["type"]
|
|
|
|
)
|
2015-02-24 11:10:44 -07:00
|
|
|
|
2015-01-26 03:45:24 -07:00
|
|
|
|
2015-02-03 07:58:30 -07:00
|
|
|
class FederationServer(FederationBase):
|
2018-05-22 15:54:22 -06:00
|
|
|
|
2016-06-15 08:12:59 -06:00
|
|
|
def __init__(self, hs):
|
|
|
|
super(FederationServer, self).__init__(hs)
|
|
|
|
|
2016-07-21 03:30:12 -06:00
|
|
|
self.auth = hs.get_auth()
|
2018-03-12 08:34:31 -06:00
|
|
|
self.handler = hs.get_handlers().federation_handler
|
2016-07-21 03:30:12 -06:00
|
|
|
|
2018-08-10 07:50:21 -06:00
|
|
|
self._server_linearizer = Linearizer("fed_server")
|
|
|
|
self._transaction_linearizer = Linearizer("fed_txn_handler")
|
2016-06-15 08:12:59 -06:00
|
|
|
|
2018-03-12 08:07:39 -06:00
|
|
|
self.transaction_actions = TransactionActions(self.store)
|
|
|
|
|
2018-03-12 10:17:08 -06:00
|
|
|
self.registry = hs.get_federation_registry()
|
|
|
|
|
2016-07-21 03:30:12 -06:00
|
|
|
# We cache responses to state queries, as they take a while and often
|
|
|
|
# come in waves.
|
2018-04-10 16:14:47 -06:00
|
|
|
self._state_resp_cache = ResponseCache(hs, "state_resp", timeout_ms=30000)
|
2016-07-21 03:30:12 -06:00
|
|
|
|
2015-01-26 03:45:24 -07:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
@log_function
|
|
|
|
def on_backfill_request(self, origin, room_id, versions, limit):
|
2016-06-17 09:43:45 -06:00
|
|
|
with (yield self._server_linearizer.queue((origin, room_id))):
|
2018-07-04 08:31:00 -06:00
|
|
|
origin_host, _ = parse_server_name(origin)
|
|
|
|
yield self.check_server_matches_acl(origin_host, room_id)
|
|
|
|
|
2016-06-17 09:43:45 -06:00
|
|
|
pdus = yield self.handler.on_backfill_request(
|
|
|
|
origin, room_id, versions, limit
|
|
|
|
)
|
2015-01-26 03:45:24 -07:00
|
|
|
|
2016-06-17 09:43:45 -06:00
|
|
|
res = self._transaction_from_pdus(pdus).get_dict()
|
|
|
|
|
|
|
|
defer.returnValue((200, res))
|
2015-01-26 03:45:24 -07:00
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
@log_function
|
2018-09-03 18:23:18 -06:00
|
|
|
def on_incoming_transaction(self, origin, transaction_data):
|
2017-10-06 08:18:58 -06:00
|
|
|
# keep this as early as possible to make the calculated origin ts as
|
|
|
|
# accurate as possible.
|
2017-10-06 08:31:58 -06:00
|
|
|
request_time = self._clock.time_msec()
|
2015-03-10 09:29:22 -06:00
|
|
|
|
2017-10-06 08:18:58 -06:00
|
|
|
transaction = Transaction(**transaction_data)
|
2015-01-26 03:45:24 -07:00
|
|
|
|
2017-10-06 08:31:58 -06:00
|
|
|
if not transaction.transaction_id:
|
|
|
|
raise Exception("Transaction missing transaction_id")
|
|
|
|
|
2015-01-26 03:45:24 -07:00
|
|
|
logger.debug("[%s] Got transaction", transaction.transaction_id)
|
|
|
|
|
2017-10-06 08:31:58 -06:00
|
|
|
# use a linearizer to ensure that we don't process the same transaction
|
|
|
|
# multiple times in parallel.
|
|
|
|
with (yield self._transaction_linearizer.queue(
|
2018-09-03 18:23:18 -06:00
|
|
|
(origin, transaction.transaction_id),
|
2017-10-06 08:31:58 -06:00
|
|
|
)):
|
|
|
|
result = yield self._handle_incoming_transaction(
|
2018-09-03 18:23:18 -06:00
|
|
|
origin, transaction, request_time,
|
2017-10-06 08:31:58 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
defer.returnValue(result)
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
2018-09-03 18:23:18 -06:00
|
|
|
def _handle_incoming_transaction(self, origin, transaction, request_time):
|
2017-10-06 08:31:58 -06:00
|
|
|
""" Process an incoming transaction and return the HTTP response
|
|
|
|
|
|
|
|
Args:
|
2018-09-03 18:23:18 -06:00
|
|
|
origin (unicode): the server making the request
|
2017-10-06 08:31:58 -06:00
|
|
|
transaction (Transaction): incoming transaction
|
|
|
|
request_time (int): timestamp that the HTTP request arrived at
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Deferred[(int, object)]: http response code and body
|
|
|
|
"""
|
2018-09-03 18:23:18 -06:00
|
|
|
response = yield self.transaction_actions.have_responded(origin, transaction)
|
2015-01-26 03:45:24 -07:00
|
|
|
|
|
|
|
if response:
|
2015-01-26 07:33:11 -07:00
|
|
|
logger.debug(
|
2017-01-06 19:13:06 -07:00
|
|
|
"[%s] We've already responded to this request",
|
2015-01-26 07:33:11 -07:00
|
|
|
transaction.transaction_id
|
|
|
|
)
|
2015-01-26 03:45:24 -07:00
|
|
|
defer.returnValue(response)
|
|
|
|
return
|
|
|
|
|
|
|
|
logger.debug("[%s] Transaction is new", transaction.transaction_id)
|
|
|
|
|
2018-05-21 18:47:37 -06:00
|
|
|
received_pdus_counter.inc(len(transaction.pdus))
|
2017-10-06 08:18:58 -06:00
|
|
|
|
2018-09-03 18:23:18 -06:00
|
|
|
origin_host, _ = parse_server_name(origin)
|
2018-07-04 08:31:00 -06:00
|
|
|
|
2017-10-06 09:07:20 -06:00
|
|
|
pdus_by_room = {}
|
2017-10-06 08:18:58 -06:00
|
|
|
|
|
|
|
for p in transaction.pdus:
|
|
|
|
if "unsigned" in p:
|
|
|
|
unsigned = p["unsigned"]
|
|
|
|
if "age" in unsigned:
|
|
|
|
p["age"] = unsigned["age"]
|
|
|
|
if "age" in p:
|
|
|
|
p["age_ts"] = request_time - int(p["age"])
|
|
|
|
del p["age"]
|
|
|
|
|
2018-11-09 04:28:22 -07:00
|
|
|
# We try and pull out an event ID so that if later checks fail we
|
2018-11-08 05:11:20 -07:00
|
|
|
# can log something sensible. We don't mandate an event ID here in
|
|
|
|
# case future event formats get rid of the key.
|
|
|
|
possible_event_id = p.get("event_id", "<Unknown>")
|
|
|
|
|
|
|
|
# Now we get the room ID so that we can check that we know the
|
|
|
|
# version of the room.
|
|
|
|
room_id = p.get("room_id")
|
|
|
|
if not room_id:
|
|
|
|
logger.info(
|
|
|
|
"Ignoring PDU as does not have a room_id. Event ID: %s",
|
|
|
|
possible_event_id,
|
|
|
|
)
|
|
|
|
continue
|
|
|
|
|
|
|
|
try:
|
|
|
|
# In future we will actually use the room version to parse the
|
|
|
|
# PDU into an event.
|
|
|
|
yield self.store.get_room_version(room_id)
|
|
|
|
except NotFoundError:
|
2018-11-09 04:28:25 -07:00
|
|
|
logger.info("Ignoring PDU for unknown room_id: %s", room_id)
|
2018-11-08 05:11:20 -07:00
|
|
|
continue
|
|
|
|
|
2017-12-30 11:40:19 -07:00
|
|
|
event = event_from_pdu_json(p)
|
2017-10-06 09:07:20 -06:00
|
|
|
pdus_by_room.setdefault(room_id, []).append(event)
|
2017-10-06 08:18:58 -06:00
|
|
|
|
|
|
|
pdu_results = {}
|
2015-05-08 09:32:18 -06:00
|
|
|
|
2017-10-06 09:07:20 -06:00
|
|
|
# we can process different rooms in parallel (which is useful if they
|
|
|
|
# require callouts to other servers to fetch missing events), but
|
|
|
|
# impose a limit to avoid going too crazy with ram/cpu.
|
2018-07-04 08:31:00 -06:00
|
|
|
|
2017-10-06 09:07:20 -06:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def process_pdus_for_room(room_id):
|
|
|
|
logger.debug("Processing PDUs for %s", room_id)
|
2018-07-04 08:31:00 -06:00
|
|
|
try:
|
|
|
|
yield self.check_server_matches_acl(origin_host, room_id)
|
|
|
|
except AuthError as e:
|
|
|
|
logger.warn(
|
|
|
|
"Ignoring PDUs for room %s from banned server", room_id,
|
|
|
|
)
|
|
|
|
for pdu in pdus_by_room[room_id]:
|
|
|
|
event_id = pdu.event_id
|
|
|
|
pdu_results[event_id] = e.error_dict()
|
|
|
|
return
|
|
|
|
|
2017-10-06 09:07:20 -06:00
|
|
|
for pdu in pdus_by_room[room_id]:
|
|
|
|
event_id = pdu.event_id
|
2018-09-27 04:25:34 -06:00
|
|
|
with nested_logging_context(event_id):
|
|
|
|
try:
|
|
|
|
yield self._handle_received_pdu(
|
|
|
|
origin, pdu
|
|
|
|
)
|
|
|
|
pdu_results[event_id] = {}
|
|
|
|
except FederationError as e:
|
|
|
|
logger.warn("Error handling PDU %s: %s", event_id, e)
|
|
|
|
pdu_results[event_id] = {"error": str(e)}
|
|
|
|
except Exception as e:
|
|
|
|
f = failure.Failure()
|
|
|
|
pdu_results[event_id] = {"error": str(e)}
|
|
|
|
logger.error(
|
|
|
|
"Failed to handle PDU %s: %s",
|
|
|
|
event_id, f.getTraceback().rstrip(),
|
|
|
|
)
|
2017-10-06 09:07:20 -06:00
|
|
|
|
2018-08-10 07:50:21 -06:00
|
|
|
yield concurrently_execute(
|
2017-10-06 09:07:20 -06:00
|
|
|
process_pdus_for_room, pdus_by_room.keys(),
|
|
|
|
TRANSACTION_CONCURRENCY_LIMIT,
|
|
|
|
)
|
2015-05-08 09:32:18 -06:00
|
|
|
|
|
|
|
if hasattr(transaction, "edus"):
|
2016-03-18 07:55:16 -06:00
|
|
|
for edu in (Edu(**x) for x in transaction.edus):
|
|
|
|
yield self.received_edu(
|
2018-09-03 18:23:18 -06:00
|
|
|
origin,
|
2015-05-08 09:32:18 -06:00
|
|
|
edu.edu_type,
|
|
|
|
edu.content
|
|
|
|
)
|
2015-01-26 03:45:24 -07:00
|
|
|
|
2015-02-17 06:58:52 -07:00
|
|
|
response = {
|
2017-10-06 08:18:58 -06:00
|
|
|
"pdus": pdu_results,
|
2015-02-17 06:58:52 -07:00
|
|
|
}
|
2015-02-17 06:24:13 -07:00
|
|
|
|
2017-10-06 08:18:58 -06:00
|
|
|
logger.debug("Returning: %s", str(response))
|
|
|
|
|
2015-01-26 03:45:24 -07:00
|
|
|
yield self.transaction_actions.set_response(
|
2018-09-03 18:23:18 -06:00
|
|
|
origin,
|
2015-01-26 03:45:24 -07:00
|
|
|
transaction,
|
|
|
|
200, response
|
|
|
|
)
|
|
|
|
defer.returnValue((200, response))
|
|
|
|
|
2016-03-18 07:55:16 -06:00
|
|
|
@defer.inlineCallbacks
|
2015-01-26 03:45:24 -07:00
|
|
|
def received_edu(self, origin, edu_type, content):
|
2015-03-10 09:29:22 -06:00
|
|
|
received_edus_counter.inc()
|
2018-03-12 10:17:08 -06:00
|
|
|
yield self.registry.on_edu(edu_type, origin, content)
|
2015-01-26 03:45:24 -07:00
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
@log_function
|
|
|
|
def on_context_state_request(self, origin, room_id, event_id):
|
2016-07-21 03:30:12 -06:00
|
|
|
if not event_id:
|
|
|
|
raise NotImplementedError("Specify an event")
|
|
|
|
|
2018-07-04 08:31:00 -06:00
|
|
|
origin_host, _ = parse_server_name(origin)
|
|
|
|
yield self.check_server_matches_acl(origin_host, room_id)
|
|
|
|
|
2016-07-21 03:30:12 -06:00
|
|
|
in_room = yield self.auth.check_host_in_room(room_id, origin)
|
|
|
|
if not in_room:
|
|
|
|
raise AuthError(403, "Host not in room.")
|
|
|
|
|
2018-04-20 04:10:04 -06:00
|
|
|
# we grab the linearizer to protect ourselves from servers which hammer
|
|
|
|
# us. In theory we might already have the response to this query
|
|
|
|
# in the cache so we could return it without waiting for the linearizer
|
|
|
|
# - but that's non-trivial to get right, and anyway somewhat defeats
|
|
|
|
# the point of the linearizer.
|
|
|
|
with (yield self._server_linearizer.queue((origin, room_id))):
|
|
|
|
resp = yield self._state_resp_cache.wrap(
|
|
|
|
(room_id, event_id),
|
|
|
|
self._on_context_state_request_compute,
|
|
|
|
room_id, event_id,
|
|
|
|
)
|
2016-07-21 03:30:12 -06:00
|
|
|
|
|
|
|
defer.returnValue((200, resp))
|
2015-02-10 08:46:24 -07:00
|
|
|
|
2016-08-03 07:47:37 -06:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def on_state_ids_request(self, origin, room_id, event_id):
|
|
|
|
if not event_id:
|
|
|
|
raise NotImplementedError("Specify an event")
|
|
|
|
|
2018-07-04 08:31:00 -06:00
|
|
|
origin_host, _ = parse_server_name(origin)
|
|
|
|
yield self.check_server_matches_acl(origin_host, room_id)
|
|
|
|
|
2016-08-03 07:47:37 -06:00
|
|
|
in_room = yield self.auth.check_host_in_room(room_id, origin)
|
|
|
|
if not in_room:
|
|
|
|
raise AuthError(403, "Host not in room.")
|
|
|
|
|
2016-09-02 03:49:43 -06:00
|
|
|
state_ids = yield self.handler.get_state_ids_for_pdu(
|
2016-08-03 07:47:37 -06:00
|
|
|
room_id, event_id,
|
|
|
|
)
|
2016-09-02 03:49:43 -06:00
|
|
|
auth_chain_ids = yield self.store.get_auth_chain_ids(state_ids)
|
2016-08-03 07:47:37 -06:00
|
|
|
|
|
|
|
defer.returnValue((200, {
|
2016-09-02 03:49:43 -06:00
|
|
|
"pdu_ids": state_ids,
|
|
|
|
"auth_chain_ids": auth_chain_ids,
|
2016-08-03 07:47:37 -06:00
|
|
|
}))
|
|
|
|
|
2016-07-21 03:30:12 -06:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def _on_context_state_request_compute(self, room_id, event_id):
|
|
|
|
pdus = yield self.handler.get_state_for_pdu(
|
|
|
|
room_id, event_id,
|
|
|
|
)
|
|
|
|
auth_chain = yield self.store.get_auth_chain(
|
|
|
|
[pdu.event_id for pdu in pdus]
|
|
|
|
)
|
|
|
|
|
|
|
|
for event in auth_chain:
|
|
|
|
# We sign these again because there was a bug where we
|
|
|
|
# incorrectly signed things the first time round
|
|
|
|
if self.hs.is_mine_id(event.event_id):
|
|
|
|
event.signatures.update(
|
|
|
|
compute_event_signature(
|
|
|
|
event,
|
|
|
|
self.hs.hostname,
|
|
|
|
self.hs.config.signing_key[0]
|
|
|
|
)
|
|
|
|
)
|
2015-01-26 03:45:24 -07:00
|
|
|
|
2016-08-02 09:45:53 -06:00
|
|
|
defer.returnValue({
|
2015-01-26 03:45:24 -07:00
|
|
|
"pdus": [pdu.get_pdu_json() for pdu in pdus],
|
|
|
|
"auth_chain": [pdu.get_pdu_json() for pdu in auth_chain],
|
2016-08-02 09:45:53 -06:00
|
|
|
})
|
2015-01-26 03:45:24 -07:00
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
@log_function
|
|
|
|
def on_pdu_request(self, origin, event_id):
|
2018-06-07 09:18:57 -06:00
|
|
|
pdu = yield self.handler.get_persisted_pdu(origin, event_id)
|
2015-01-26 03:45:24 -07:00
|
|
|
|
|
|
|
if pdu:
|
|
|
|
defer.returnValue(
|
|
|
|
(200, self._transaction_from_pdus([pdu]).get_dict())
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
defer.returnValue((404, ""))
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def on_query_request(self, query_type, args):
|
2018-05-21 18:47:37 -06:00
|
|
|
received_queries_counter.labels(query_type).inc()
|
2018-03-12 10:17:08 -06:00
|
|
|
resp = yield self.registry.on_query(query_type, args)
|
|
|
|
defer.returnValue((200, resp))
|
2015-01-26 03:45:24 -07:00
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
2018-07-25 15:25:41 -06:00
|
|
|
def on_make_join_request(self, origin, room_id, user_id, supported_versions):
|
2018-07-04 08:31:00 -06:00
|
|
|
origin_host, _ = parse_server_name(origin)
|
|
|
|
yield self.check_server_matches_acl(origin_host, room_id)
|
2018-07-25 15:25:41 -06:00
|
|
|
|
|
|
|
room_version = yield self.store.get_room_version(room_id)
|
|
|
|
if room_version not in supported_versions:
|
|
|
|
logger.warn("Room version %s not in %s", room_version, supported_versions)
|
|
|
|
raise IncompatibleRoomVersionError(room_version=room_version)
|
|
|
|
|
2015-11-05 09:43:19 -07:00
|
|
|
pdu = yield self.handler.on_make_join_request(room_id, user_id)
|
2015-01-26 03:45:24 -07:00
|
|
|
time_now = self._clock.time_msec()
|
2018-07-25 15:25:41 -06:00
|
|
|
defer.returnValue({
|
|
|
|
"event": pdu.get_pdu_json(time_now),
|
|
|
|
"room_version": room_version,
|
|
|
|
})
|
2015-01-26 03:45:24 -07:00
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
2019-01-15 06:22:44 -07:00
|
|
|
def on_invite_request(self, origin, content, room_version):
|
2017-12-30 11:40:19 -07:00
|
|
|
pdu = event_from_pdu_json(content)
|
2018-07-04 08:31:00 -06:00
|
|
|
origin_host, _ = parse_server_name(origin)
|
|
|
|
yield self.check_server_matches_acl(origin_host, pdu.room_id)
|
2015-01-26 03:45:24 -07:00
|
|
|
ret_pdu = yield self.handler.on_invite_request(origin, pdu)
|
|
|
|
time_now = self._clock.time_msec()
|
2019-01-15 06:22:44 -07:00
|
|
|
defer.returnValue({"event": ret_pdu.get_pdu_json(time_now)})
|
2015-01-26 03:45:24 -07:00
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def on_send_join_request(self, origin, content):
|
|
|
|
logger.debug("on_send_join_request: content: %s", content)
|
2017-12-30 11:40:19 -07:00
|
|
|
pdu = event_from_pdu_json(content)
|
2018-07-04 08:31:00 -06:00
|
|
|
|
|
|
|
origin_host, _ = parse_server_name(origin)
|
|
|
|
yield self.check_server_matches_acl(origin_host, pdu.room_id)
|
|
|
|
|
2015-01-26 03:45:24 -07:00
|
|
|
logger.debug("on_send_join_request: pdu sigs: %s", pdu.signatures)
|
|
|
|
res_pdus = yield self.handler.on_send_join_request(origin, pdu)
|
|
|
|
time_now = self._clock.time_msec()
|
|
|
|
defer.returnValue((200, {
|
|
|
|
"state": [p.get_pdu_json(time_now) for p in res_pdus["state"]],
|
|
|
|
"auth_chain": [
|
|
|
|
p.get_pdu_json(time_now) for p in res_pdus["auth_chain"]
|
|
|
|
],
|
|
|
|
}))
|
|
|
|
|
2015-10-20 04:58:58 -06:00
|
|
|
@defer.inlineCallbacks
|
2018-07-04 08:31:00 -06:00
|
|
|
def on_make_leave_request(self, origin, room_id, user_id):
|
|
|
|
origin_host, _ = parse_server_name(origin)
|
|
|
|
yield self.check_server_matches_acl(origin_host, room_id)
|
2015-10-20 04:58:58 -06:00
|
|
|
pdu = yield self.handler.on_make_leave_request(room_id, user_id)
|
|
|
|
time_now = self._clock.time_msec()
|
|
|
|
defer.returnValue({"event": pdu.get_pdu_json(time_now)})
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def on_send_leave_request(self, origin, content):
|
|
|
|
logger.debug("on_send_leave_request: content: %s", content)
|
2017-12-30 11:40:19 -07:00
|
|
|
pdu = event_from_pdu_json(content)
|
2018-07-04 08:31:00 -06:00
|
|
|
|
|
|
|
origin_host, _ = parse_server_name(origin)
|
|
|
|
yield self.check_server_matches_acl(origin_host, pdu.room_id)
|
|
|
|
|
2015-10-20 04:58:58 -06:00
|
|
|
logger.debug("on_send_leave_request: pdu sigs: %s", pdu.signatures)
|
|
|
|
yield self.handler.on_send_leave_request(origin, pdu)
|
|
|
|
defer.returnValue((200, {}))
|
|
|
|
|
2015-01-26 03:45:24 -07:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def on_event_auth(self, origin, room_id, event_id):
|
2016-06-17 09:43:45 -06:00
|
|
|
with (yield self._server_linearizer.queue((origin, room_id))):
|
2018-07-04 08:31:00 -06:00
|
|
|
origin_host, _ = parse_server_name(origin)
|
|
|
|
yield self.check_server_matches_acl(origin_host, room_id)
|
|
|
|
|
2016-06-17 09:43:45 -06:00
|
|
|
time_now = self._clock.time_msec()
|
|
|
|
auth_pdus = yield self.handler.on_event_auth(event_id)
|
|
|
|
res = {
|
|
|
|
"auth_chain": [a.get_pdu_json(time_now) for a in auth_pdus],
|
|
|
|
}
|
|
|
|
defer.returnValue((200, res))
|
2015-01-26 03:45:24 -07:00
|
|
|
|
2015-01-29 09:50:23 -07:00
|
|
|
@defer.inlineCallbacks
|
2016-06-17 09:43:45 -06:00
|
|
|
def on_query_auth_request(self, origin, content, room_id, event_id):
|
2015-01-30 08:58:28 -07:00
|
|
|
"""
|
|
|
|
Content is a dict with keys::
|
|
|
|
auth_chain (list): A list of events that give the auth chain.
|
|
|
|
missing (list): A list of event_ids indicating what the other
|
|
|
|
side (`origin`) think we're missing.
|
|
|
|
rejects (dict): A mapping from event_id to a 2-tuple of reason
|
|
|
|
string and a proof (or None) of why the event was rejected.
|
|
|
|
The keys of this dict give the list of events the `origin` has
|
|
|
|
rejected.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
origin (str)
|
|
|
|
content (dict)
|
|
|
|
event_id (str)
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Deferred: Results in `dict` with the same format as `content`
|
|
|
|
"""
|
2016-06-17 09:43:45 -06:00
|
|
|
with (yield self._server_linearizer.queue((origin, room_id))):
|
2018-07-04 08:31:00 -06:00
|
|
|
origin_host, _ = parse_server_name(origin)
|
|
|
|
yield self.check_server_matches_acl(origin_host, room_id)
|
|
|
|
|
2016-06-17 09:43:45 -06:00
|
|
|
auth_chain = [
|
2017-12-30 11:40:19 -07:00
|
|
|
event_from_pdu_json(e)
|
2016-06-17 09:43:45 -06:00
|
|
|
for e in content["auth_chain"]
|
|
|
|
]
|
|
|
|
|
|
|
|
signed_auth = yield self._check_sigs_and_hash_and_fetch(
|
|
|
|
origin, auth_chain, outlier=True
|
|
|
|
)
|
2015-01-29 09:50:23 -07:00
|
|
|
|
2016-06-17 09:43:45 -06:00
|
|
|
ret = yield self.handler.on_query_auth(
|
|
|
|
origin,
|
|
|
|
event_id,
|
2018-08-02 04:53:52 -06:00
|
|
|
room_id,
|
2016-06-17 09:43:45 -06:00
|
|
|
signed_auth,
|
|
|
|
content.get("rejects", []),
|
|
|
|
content.get("missing", []),
|
|
|
|
)
|
2015-01-29 09:50:23 -07:00
|
|
|
|
2016-06-17 09:43:45 -06:00
|
|
|
time_now = self._clock.time_msec()
|
|
|
|
send_content = {
|
|
|
|
"auth_chain": [
|
|
|
|
e.get_pdu_json(time_now)
|
|
|
|
for e in ret["auth_chain"]
|
|
|
|
],
|
|
|
|
"rejects": ret.get("rejects", []),
|
|
|
|
"missing": ret.get("missing", []),
|
|
|
|
}
|
2015-01-29 09:50:23 -07:00
|
|
|
|
|
|
|
defer.returnValue(
|
|
|
|
(200, send_content)
|
|
|
|
)
|
|
|
|
|
2015-07-23 09:03:38 -06:00
|
|
|
@log_function
|
|
|
|
def on_query_client_keys(self, origin, content):
|
2016-08-02 11:06:31 -06:00
|
|
|
return self.on_query_request("client_keys", content)
|
2015-07-23 09:03:38 -06:00
|
|
|
|
2017-01-26 09:06:54 -07:00
|
|
|
def on_query_user_devices(self, origin, user_id):
|
|
|
|
return self.on_query_request("user_devices", user_id)
|
|
|
|
|
2015-07-23 09:03:38 -06:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
@log_function
|
|
|
|
def on_claim_client_keys(self, origin, content):
|
|
|
|
query = []
|
|
|
|
for user_id, device_keys in content.get("one_time_keys", {}).items():
|
|
|
|
for device_id, algorithm in device_keys.items():
|
|
|
|
query.append((user_id, device_id, algorithm))
|
2015-08-13 10:27:46 -06:00
|
|
|
|
2015-07-23 09:03:38 -06:00
|
|
|
results = yield self.store.claim_e2e_one_time_keys(query)
|
2015-08-13 10:27:46 -06:00
|
|
|
|
2015-07-23 09:03:38 -06:00
|
|
|
json_result = {}
|
|
|
|
for user_id, device_keys in results.items():
|
|
|
|
for device_id, keys in device_keys.items():
|
|
|
|
for key_id, json_bytes in keys.items():
|
|
|
|
json_result.setdefault(user_id, {})[device_id] = {
|
|
|
|
key_id: json.loads(json_bytes)
|
|
|
|
}
|
2015-08-13 10:27:46 -06:00
|
|
|
|
2017-05-09 12:01:39 -06:00
|
|
|
logger.info(
|
|
|
|
"Claimed one-time-keys: %s",
|
|
|
|
",".join((
|
|
|
|
"%s for %s:%s" % (key_id, user_id, device_id)
|
2018-04-15 08:51:07 -06:00
|
|
|
for user_id, user_keys in iteritems(json_result)
|
|
|
|
for device_id, device_keys in iteritems(user_keys)
|
|
|
|
for key_id, _ in iteritems(device_keys)
|
2017-05-09 12:01:39 -06:00
|
|
|
)),
|
|
|
|
)
|
|
|
|
|
2015-07-23 09:03:38 -06:00
|
|
|
defer.returnValue({"one_time_keys": json_result})
|
|
|
|
|
2015-02-19 10:24:14 -07:00
|
|
|
@defer.inlineCallbacks
|
2015-02-23 06:58:02 -07:00
|
|
|
@log_function
|
|
|
|
def on_get_missing_events(self, origin, room_id, earliest_events,
|
2018-10-16 13:37:16 -06:00
|
|
|
latest_events, limit):
|
2016-06-17 09:43:45 -06:00
|
|
|
with (yield self._server_linearizer.queue((origin, room_id))):
|
2018-07-04 08:31:00 -06:00
|
|
|
origin_host, _ = parse_server_name(origin)
|
|
|
|
yield self.check_server_matches_acl(origin_host, room_id)
|
|
|
|
|
2016-06-17 09:43:45 -06:00
|
|
|
logger.info(
|
|
|
|
"on_get_missing_events: earliest_events: %r, latest_events: %r,"
|
2018-10-16 13:37:16 -06:00
|
|
|
" limit: %d",
|
|
|
|
earliest_events, latest_events, limit,
|
2016-06-17 09:43:45 -06:00
|
|
|
)
|
2016-12-31 08:21:37 -07:00
|
|
|
|
2016-06-17 09:43:45 -06:00
|
|
|
missing_events = yield self.handler.on_get_missing_events(
|
2018-10-16 13:37:16 -06:00
|
|
|
origin, room_id, earliest_events, latest_events, limit,
|
2016-06-17 09:43:45 -06:00
|
|
|
)
|
2015-02-19 10:24:14 -07:00
|
|
|
|
2016-06-17 09:43:45 -06:00
|
|
|
if len(missing_events) < 5:
|
|
|
|
logger.info(
|
|
|
|
"Returning %d events: %r", len(missing_events), missing_events
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
logger.info("Returning %d events", len(missing_events))
|
2016-06-08 04:55:31 -06:00
|
|
|
|
2016-06-17 09:43:45 -06:00
|
|
|
time_now = self._clock.time_msec()
|
2015-02-19 10:24:14 -07:00
|
|
|
|
|
|
|
defer.returnValue({
|
|
|
|
"events": [ev.get_pdu_json(time_now) for ev in missing_events],
|
|
|
|
})
|
|
|
|
|
2016-05-05 06:42:44 -06:00
|
|
|
@log_function
|
|
|
|
def on_openid_userinfo(self, token):
|
|
|
|
ts_now_ms = self._clock.time_msec()
|
|
|
|
return self.store.get_user_id_for_open_id_token(token, ts_now_ms)
|
|
|
|
|
2015-01-26 03:45:24 -07:00
|
|
|
def _transaction_from_pdus(self, pdu_list):
|
|
|
|
"""Returns a new Transaction containing the given PDUs suitable for
|
|
|
|
transmission.
|
|
|
|
"""
|
|
|
|
time_now = self._clock.time_msec()
|
|
|
|
pdus = [p.get_pdu_json(time_now) for p in pdu_list]
|
|
|
|
return Transaction(
|
|
|
|
origin=self.server_name,
|
|
|
|
pdus=pdus,
|
|
|
|
origin_server_ts=int(time_now),
|
|
|
|
destination=None,
|
|
|
|
)
|
|
|
|
|
2017-03-09 06:15:27 -07:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def _handle_received_pdu(self, origin, pdu):
|
|
|
|
""" Process a PDU received in a federation /send/ transaction.
|
|
|
|
|
2018-04-17 16:41:36 -06:00
|
|
|
If the event is invalid, then this method throws a FederationError.
|
|
|
|
(The error will then be logged and sent back to the sender (which
|
|
|
|
probably won't do anything with it), and other events in the
|
|
|
|
transaction will be processed as normal).
|
|
|
|
|
|
|
|
It is likely that we'll then receive other events which refer to
|
|
|
|
this rejected_event in their prev_events, etc. When that happens,
|
|
|
|
we'll attempt to fetch the rejected event again, which will presumably
|
|
|
|
fail, so those second-generation events will also get rejected.
|
|
|
|
|
|
|
|
Eventually, we get to the point where there are more than 10 events
|
|
|
|
between any new events and the original rejected event. Since we
|
|
|
|
only try to backfill 10 events deep on received pdu, we then accept the
|
|
|
|
new event, possibly introducing a discontinuity in the DAG, with new
|
|
|
|
forward extremities, so normal service is approximately returned,
|
|
|
|
until we try to backfill across the discontinuity.
|
|
|
|
|
2017-03-09 06:15:27 -07:00
|
|
|
Args:
|
|
|
|
origin (str): server which sent the pdu
|
|
|
|
pdu (FrozenEvent): received pdu
|
|
|
|
|
|
|
|
Returns (Deferred): completes with None
|
2018-04-17 16:41:36 -06:00
|
|
|
|
|
|
|
Raises: FederationError if the signatures / hash do not match, or
|
|
|
|
if the event was unacceptable for any other reason (eg, too large,
|
|
|
|
too many prev_events, couldn't find the prev_events)
|
|
|
|
"""
|
2017-10-06 07:24:06 -06:00
|
|
|
# check that it's actually being sent from a valid destination to
|
|
|
|
# workaround bug #1753 in 0.18.5 and 0.18.6
|
|
|
|
if origin != get_domain_from_id(pdu.event_id):
|
|
|
|
# We continue to accept join events from any server; this is
|
|
|
|
# necessary for the federation join dance to work correctly.
|
|
|
|
# (When we join over federation, the "helper" server is
|
|
|
|
# responsible for sending out the join event, rather than the
|
|
|
|
# origin. See bug #1893).
|
|
|
|
if not (
|
|
|
|
pdu.type == 'm.room.member' and
|
|
|
|
pdu.content and
|
|
|
|
pdu.content.get("membership", None) == 'join'
|
|
|
|
):
|
|
|
|
logger.info(
|
|
|
|
"Discarding PDU %s from invalid origin %s",
|
|
|
|
pdu.event_id, origin
|
|
|
|
)
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
logger.info(
|
|
|
|
"Accepting join PDU %s from %s",
|
|
|
|
pdu.event_id, origin
|
|
|
|
)
|
|
|
|
|
2017-03-09 06:15:27 -07:00
|
|
|
# Check signature.
|
|
|
|
try:
|
|
|
|
pdu = yield self._check_sigs_and_hash(pdu)
|
|
|
|
except SynapseError as e:
|
|
|
|
raise FederationError(
|
|
|
|
"ERROR",
|
|
|
|
e.code,
|
|
|
|
e.msg,
|
|
|
|
affected=pdu.event_id,
|
|
|
|
)
|
|
|
|
|
2018-06-27 04:27:32 -06:00
|
|
|
yield self.handler.on_receive_pdu(
|
2018-09-20 06:06:55 -06:00
|
|
|
origin, pdu, sent_to_us_directly=True,
|
2018-06-27 04:27:32 -06:00
|
|
|
)
|
2017-03-09 05:20:46 -07:00
|
|
|
|
2015-01-26 03:45:24 -07:00
|
|
|
def __str__(self):
|
|
|
|
return "<ReplicationLayer(%s)>" % self.server_name
|
|
|
|
|
2015-11-05 09:43:19 -07:00
|
|
|
@defer.inlineCallbacks
|
2016-02-23 08:11:25 -07:00
|
|
|
def exchange_third_party_invite(
|
|
|
|
self,
|
|
|
|
sender_user_id,
|
|
|
|
target_user_id,
|
|
|
|
room_id,
|
|
|
|
signed,
|
|
|
|
):
|
|
|
|
ret = yield self.handler.exchange_third_party_invite(
|
|
|
|
sender_user_id,
|
|
|
|
target_user_id,
|
|
|
|
room_id,
|
|
|
|
signed,
|
|
|
|
)
|
2015-11-05 09:43:19 -07:00
|
|
|
defer.returnValue(ret)
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def on_exchange_third_party_invite_request(self, origin, room_id, event_dict):
|
|
|
|
ret = yield self.handler.on_exchange_third_party_invite_request(
|
|
|
|
origin, room_id, event_dict
|
|
|
|
)
|
|
|
|
defer.returnValue(ret)
|
2018-03-12 10:17:08 -06:00
|
|
|
|
2018-07-04 08:31:00 -06:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def check_server_matches_acl(self, server_name, room_id):
|
|
|
|
"""Check if the given server is allowed by the server ACLs in the room
|
|
|
|
|
|
|
|
Args:
|
|
|
|
server_name (str): name of server, *without any port part*
|
|
|
|
room_id (str): ID of the room to check
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
AuthError if the server does not match the ACL
|
|
|
|
"""
|
|
|
|
state_ids = yield self.store.get_current_state_ids(room_id)
|
|
|
|
acl_event_id = state_ids.get((EventTypes.ServerACL, ""))
|
|
|
|
|
|
|
|
if not acl_event_id:
|
|
|
|
return
|
|
|
|
|
|
|
|
acl_event = yield self.store.get_event(acl_event_id)
|
|
|
|
if server_matches_acl_event(server_name, acl_event):
|
|
|
|
return
|
|
|
|
|
|
|
|
raise AuthError(code=403, msg="Server is banned from room")
|
|
|
|
|
|
|
|
|
|
|
|
def server_matches_acl_event(server_name, acl_event):
|
|
|
|
"""Check if the given server is allowed by the ACL event
|
|
|
|
|
|
|
|
Args:
|
|
|
|
server_name (str): name of server, without any port part
|
|
|
|
acl_event (EventBase): m.room.server_acl event
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
bool: True if this server is allowed by the ACLs
|
|
|
|
"""
|
|
|
|
logger.debug("Checking %s against acl %s", server_name, acl_event.content)
|
|
|
|
|
|
|
|
# first of all, check if literal IPs are blocked, and if so, whether the
|
|
|
|
# server name is a literal IP
|
|
|
|
allow_ip_literals = acl_event.content.get("allow_ip_literals", True)
|
|
|
|
if not isinstance(allow_ip_literals, bool):
|
|
|
|
logger.warn("Ignorning non-bool allow_ip_literals flag")
|
|
|
|
allow_ip_literals = True
|
|
|
|
if not allow_ip_literals:
|
|
|
|
# check for ipv6 literals. These start with '['.
|
|
|
|
if server_name[0] == '[':
|
|
|
|
return False
|
|
|
|
|
|
|
|
# check for ipv4 literals. We can just lift the routine from twisted.
|
|
|
|
if isIPAddress(server_name):
|
|
|
|
return False
|
|
|
|
|
|
|
|
# next, check the deny list
|
|
|
|
deny = acl_event.content.get("deny", [])
|
|
|
|
if not isinstance(deny, (list, tuple)):
|
|
|
|
logger.warn("Ignorning non-list deny ACL %s", deny)
|
|
|
|
deny = []
|
|
|
|
for e in deny:
|
|
|
|
if _acl_entry_matches(server_name, e):
|
|
|
|
# logger.info("%s matched deny rule %s", server_name, e)
|
|
|
|
return False
|
|
|
|
|
|
|
|
# then the allow list.
|
|
|
|
allow = acl_event.content.get("allow", [])
|
|
|
|
if not isinstance(allow, (list, tuple)):
|
|
|
|
logger.warn("Ignorning non-list allow ACL %s", allow)
|
|
|
|
allow = []
|
|
|
|
for e in allow:
|
|
|
|
if _acl_entry_matches(server_name, e):
|
|
|
|
# logger.info("%s matched allow rule %s", server_name, e)
|
|
|
|
return True
|
|
|
|
|
|
|
|
# everything else should be rejected.
|
|
|
|
# logger.info("%s fell through", server_name)
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
def _acl_entry_matches(server_name, acl_entry):
|
|
|
|
if not isinstance(acl_entry, six.string_types):
|
|
|
|
logger.warn("Ignoring non-str ACL entry '%s' (is %s)", acl_entry, type(acl_entry))
|
|
|
|
return False
|
2018-10-17 09:14:04 -06:00
|
|
|
regex = glob_to_regex(acl_entry)
|
2018-07-04 08:31:00 -06:00
|
|
|
return regex.match(server_name)
|
|
|
|
|
|
|
|
|
2018-03-12 10:17:08 -06:00
|
|
|
class FederationHandlerRegistry(object):
|
|
|
|
"""Allows classes to register themselves as handlers for a given EDU or
|
|
|
|
query type for incoming federation traffic.
|
|
|
|
"""
|
|
|
|
def __init__(self):
|
|
|
|
self.edu_handlers = {}
|
|
|
|
self.query_handlers = {}
|
|
|
|
|
|
|
|
def register_edu_handler(self, edu_type, handler):
|
|
|
|
"""Sets the handler callable that will be used to handle an incoming
|
|
|
|
federation EDU of the given type.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
edu_type (str): The type of the incoming EDU to register handler for
|
2018-03-13 05:29:17 -06:00
|
|
|
handler (Callable[[str, dict]]): A callable invoked on incoming EDU
|
2018-03-12 10:17:08 -06:00
|
|
|
of the given type. The arguments are the origin server name and
|
|
|
|
the EDU contents.
|
|
|
|
"""
|
|
|
|
if edu_type in self.edu_handlers:
|
|
|
|
raise KeyError("Already have an EDU handler for %s" % (edu_type,))
|
|
|
|
|
2018-07-26 04:44:22 -06:00
|
|
|
logger.info("Registering federation EDU handler for %r", edu_type)
|
|
|
|
|
2018-03-12 10:17:08 -06:00
|
|
|
self.edu_handlers[edu_type] = handler
|
|
|
|
|
|
|
|
def register_query_handler(self, query_type, handler):
|
|
|
|
"""Sets the handler callable that will be used to handle an incoming
|
|
|
|
federation query of the given type.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
query_type (str): Category name of the query, which should match
|
|
|
|
the string used by make_query.
|
2018-03-13 05:29:17 -06:00
|
|
|
handler (Callable[[dict], Deferred[dict]]): Invoked to handle
|
2018-03-12 10:17:08 -06:00
|
|
|
incoming queries of this type. The return will be yielded
|
|
|
|
on and the result used as the response to the query request.
|
|
|
|
"""
|
|
|
|
if query_type in self.query_handlers:
|
|
|
|
raise KeyError(
|
|
|
|
"Already have a Query handler for %s" % (query_type,)
|
|
|
|
)
|
|
|
|
|
2018-07-26 04:44:22 -06:00
|
|
|
logger.info("Registering federation query handler for %r", query_type)
|
|
|
|
|
2018-03-12 10:17:08 -06:00
|
|
|
self.query_handlers[query_type] = handler
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def on_edu(self, edu_type, origin, content):
|
|
|
|
handler = self.edu_handlers.get(edu_type)
|
|
|
|
if not handler:
|
|
|
|
logger.warn("No handler registered for EDU type %s", edu_type)
|
|
|
|
|
|
|
|
try:
|
|
|
|
yield handler(origin, content)
|
|
|
|
except SynapseError as e:
|
|
|
|
logger.info("Failed to handle edu %r: %r", edu_type, e)
|
2018-10-24 03:35:01 -06:00
|
|
|
except Exception:
|
2018-03-12 10:17:08 -06:00
|
|
|
logger.exception("Failed to handle edu %r", edu_type)
|
|
|
|
|
|
|
|
def on_query(self, query_type, args):
|
|
|
|
handler = self.query_handlers.get(query_type)
|
|
|
|
if not handler:
|
|
|
|
logger.warn("No handler registered for query type %s", query_type)
|
|
|
|
raise NotFoundError("No handler for Query type '%s'" % (query_type,))
|
|
|
|
|
|
|
|
return handler(args)
|
2018-07-26 04:44:22 -06:00
|
|
|
|
|
|
|
|
|
|
|
class ReplicationFederationHandlerRegistry(FederationHandlerRegistry):
|
2018-08-09 03:23:49 -06:00
|
|
|
"""A FederationHandlerRegistry for worker processes.
|
|
|
|
|
|
|
|
When receiving EDU or queries it will check if an appropriate handler has
|
|
|
|
been registered on the worker, if there isn't one then it calls off to the
|
|
|
|
master process.
|
|
|
|
"""
|
|
|
|
|
2018-07-26 04:44:22 -06:00
|
|
|
def __init__(self, hs):
|
|
|
|
self.config = hs.config
|
|
|
|
self.http_client = hs.get_simple_http_client()
|
|
|
|
self.clock = hs.get_clock()
|
|
|
|
|
|
|
|
self._get_query_client = ReplicationGetQueryRestServlet.make_client(hs)
|
|
|
|
self._send_edu = ReplicationFederationSendEduRestServlet.make_client(hs)
|
|
|
|
|
|
|
|
super(ReplicationFederationHandlerRegistry, self).__init__()
|
|
|
|
|
|
|
|
def on_edu(self, edu_type, origin, content):
|
2018-08-09 03:23:49 -06:00
|
|
|
"""Overrides FederationHandlerRegistry
|
|
|
|
"""
|
2018-07-26 04:44:22 -06:00
|
|
|
handler = self.edu_handlers.get(edu_type)
|
|
|
|
if handler:
|
|
|
|
return super(ReplicationFederationHandlerRegistry, self).on_edu(
|
|
|
|
edu_type, origin, content,
|
|
|
|
)
|
|
|
|
|
|
|
|
return self._send_edu(
|
2018-09-05 08:10:47 -06:00
|
|
|
edu_type=edu_type,
|
|
|
|
origin=origin,
|
|
|
|
content=content,
|
2018-07-26 04:44:22 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
def on_query(self, query_type, args):
|
2018-08-09 03:23:49 -06:00
|
|
|
"""Overrides FederationHandlerRegistry
|
|
|
|
"""
|
2018-07-26 04:44:22 -06:00
|
|
|
handler = self.query_handlers.get(query_type)
|
|
|
|
if handler:
|
|
|
|
return handler(args)
|
|
|
|
|
|
|
|
return self._get_query_client(
|
2018-09-05 08:10:47 -06:00
|
|
|
query_type=query_type,
|
|
|
|
args=args,
|
2018-07-26 04:44:22 -06:00
|
|
|
)
|