2015-01-26 11:45:24 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
2016-01-07 05:26:29 +01:00
|
|
|
# Copyright 2015, 2016 OpenMarket Ltd
|
2015-01-26 11:45:24 +01: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 19:40:19 +01:00
|
|
|
import copy
|
|
|
|
import itertools
|
|
|
|
import logging
|
2020-01-27 15:30:57 +01:00
|
|
|
from typing import Dict, Iterable
|
2017-12-30 19:40:19 +01:00
|
|
|
|
2018-07-09 08:09:20 +02:00
|
|
|
from prometheus_client import Counter
|
|
|
|
|
2015-01-26 11:45:24 +01:00
|
|
|
from twisted.internet import defer
|
|
|
|
|
2019-04-01 11:24:38 +02:00
|
|
|
from synapse.api.constants import EventTypes, Membership
|
2015-03-05 17:08:02 +01:00
|
|
|
from synapse.api.errors import (
|
2018-07-09 08:09:20 +02:00
|
|
|
CodeMessageException,
|
2019-02-23 15:31:08 +01:00
|
|
|
Codes,
|
2018-07-09 08:09:20 +02:00
|
|
|
FederationDeniedError,
|
|
|
|
HttpResponseException,
|
|
|
|
SynapseError,
|
2020-01-27 15:30:57 +01:00
|
|
|
UnsupportedRoomVersionError,
|
2015-03-05 17:08:02 +01:00
|
|
|
)
|
2019-04-01 11:24:38 +02:00
|
|
|
from synapse.api.room_versions import (
|
|
|
|
KNOWN_ROOM_VERSIONS,
|
|
|
|
EventFormatVersions,
|
|
|
|
RoomVersions,
|
|
|
|
)
|
2019-01-25 18:19:31 +01:00
|
|
|
from synapse.events import builder, room_version_to_event_format
|
2018-07-09 08:09:20 +02:00
|
|
|
from synapse.federation.federation_base import FederationBase, event_from_pdu_json
|
2019-12-10 18:42:46 +01:00
|
|
|
from synapse.logging.context import make_deferred_yieldable
|
2019-07-03 16:07:04 +02:00
|
|
|
from synapse.logging.utils import log_function
|
2019-12-10 18:42:46 +01:00
|
|
|
from synapse.util import unwrapFirstError
|
2017-12-30 19:40:19 +01:00
|
|
|
from synapse.util.caches.expiringcache import ExpiringCache
|
2017-03-23 01:12:21 +01:00
|
|
|
from synapse.util.retryutils import NotRetryingDestination
|
2015-02-17 18:20:56 +01:00
|
|
|
|
2018-05-22 02:47:37 +02:00
|
|
|
logger = logging.getLogger(__name__)
|
2015-03-10 16:29:22 +01:00
|
|
|
|
2018-05-22 02:47:37 +02:00
|
|
|
sent_queries_counter = Counter("synapse_federation_client_sent_queries", "", ["type"])
|
2015-02-24 19:10:44 +01:00
|
|
|
|
2015-01-26 11:45:24 +01:00
|
|
|
|
2016-08-10 12:31:46 +02:00
|
|
|
PDU_RETRY_TIME_MS = 1 * 60 * 1000
|
|
|
|
|
|
|
|
|
2018-08-01 12:24:19 +02:00
|
|
|
class InvalidResponseError(RuntimeError):
|
|
|
|
"""Helper for _try_destination_list: indicates that the server returned a response
|
|
|
|
we couldn't parse
|
|
|
|
"""
|
2019-06-20 11:32:02 +02:00
|
|
|
|
2018-08-01 12:24:19 +02:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
2015-02-03 15:58:30 +01:00
|
|
|
class FederationClient(FederationBase):
|
2016-06-15 16:12:59 +02:00
|
|
|
def __init__(self, hs):
|
|
|
|
super(FederationClient, self).__init__(hs)
|
2015-02-16 19:02:39 +01:00
|
|
|
|
2016-08-10 12:31:46 +02:00
|
|
|
self.pdu_destination_tried = {}
|
2019-06-20 11:32:02 +02:00
|
|
|
self._clock.looping_call(self._clear_tried_cache, 60 * 1000)
|
2016-08-26 15:54:30 +02:00
|
|
|
self.state = hs.get_state_handler()
|
2018-03-12 15:07:39 +01:00
|
|
|
self.transport_layer = hs.get_federation_transport_client()
|
2016-08-10 12:31:46 +02:00
|
|
|
|
2019-01-25 18:19:31 +01:00
|
|
|
self.hostname = hs.hostname
|
|
|
|
self.signing_key = hs.config.signing_key[0]
|
2019-01-16 16:13:07 +01:00
|
|
|
|
2018-09-21 15:19:46 +02:00
|
|
|
self._get_pdu_cache = ExpiringCache(
|
|
|
|
cache_name="get_pdu_cache",
|
|
|
|
clock=self._clock,
|
|
|
|
max_len=1000,
|
|
|
|
expiry_ms=120 * 1000,
|
|
|
|
reset_expiry_on_get=False,
|
|
|
|
)
|
|
|
|
|
2016-08-10 12:31:46 +02:00
|
|
|
def _clear_tried_cache(self):
|
|
|
|
"""Clear pdu_destination_tried cache"""
|
|
|
|
now = self._clock.time_msec()
|
|
|
|
|
|
|
|
old_dict = self.pdu_destination_tried
|
|
|
|
self.pdu_destination_tried = {}
|
|
|
|
|
|
|
|
for event_id, destination_dict in old_dict.items():
|
|
|
|
destination_dict = {
|
|
|
|
dest: time
|
|
|
|
for dest, time in destination_dict.items()
|
|
|
|
if time + PDU_RETRY_TIME_MS > now
|
|
|
|
}
|
|
|
|
if destination_dict:
|
|
|
|
self.pdu_destination_tried[event_id] = destination_dict
|
|
|
|
|
2015-01-26 11:45:24 +01:00
|
|
|
@log_function
|
2019-06-20 11:32:02 +02:00
|
|
|
def make_query(
|
|
|
|
self,
|
|
|
|
destination,
|
|
|
|
query_type,
|
|
|
|
args,
|
|
|
|
retry_on_dns_fail=False,
|
|
|
|
ignore_backoff=False,
|
|
|
|
):
|
2015-01-26 11:45:24 +01:00
|
|
|
"""Sends a federation Query to a remote homeserver of the given type
|
|
|
|
and arguments.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
destination (str): Domain name of the remote homeserver
|
|
|
|
query_type (str): Category of the query type; should match the
|
|
|
|
handler name used in register_query_handler().
|
|
|
|
args (dict): Mapping of strings to strings containing the details
|
|
|
|
of the query request.
|
2017-03-23 12:10:36 +01:00
|
|
|
ignore_backoff (bool): true to ignore the historical backoff data
|
|
|
|
and try the request anyway.
|
2015-01-26 11:45:24 +01:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
a Deferred which will eventually yield a JSON object from the
|
|
|
|
response
|
|
|
|
"""
|
2018-05-22 02:47:37 +02:00
|
|
|
sent_queries_counter.labels(query_type).inc()
|
2015-03-10 16:29:22 +01:00
|
|
|
|
2015-01-26 11:45:24 +01:00
|
|
|
return self.transport_layer.make_query(
|
2019-06-20 11:32:02 +02:00
|
|
|
destination,
|
|
|
|
query_type,
|
|
|
|
args,
|
|
|
|
retry_on_dns_fail=retry_on_dns_fail,
|
2017-03-23 12:10:36 +01:00
|
|
|
ignore_backoff=ignore_backoff,
|
2015-01-26 11:45:24 +01:00
|
|
|
)
|
|
|
|
|
2015-07-23 17:03:38 +02:00
|
|
|
@log_function
|
2016-09-12 19:17:09 +02:00
|
|
|
def query_client_keys(self, destination, content, timeout):
|
2015-07-23 17:03:38 +02:00
|
|
|
"""Query device keys for a device hosted on a remote server.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
destination (str): Domain name of the remote homeserver
|
|
|
|
content (dict): The query content.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
a Deferred which will eventually yield a JSON object from the
|
|
|
|
response
|
|
|
|
"""
|
2018-05-22 02:47:37 +02:00
|
|
|
sent_queries_counter.labels("client_device_keys").inc()
|
2019-06-20 11:32:02 +02:00
|
|
|
return self.transport_layer.query_client_keys(destination, content, timeout)
|
2015-07-23 17:03:38 +02:00
|
|
|
|
2017-01-26 17:06:54 +01:00
|
|
|
@log_function
|
|
|
|
def query_user_devices(self, destination, user_id, timeout=30000):
|
|
|
|
"""Query the device keys for a list of user ids hosted on a remote
|
|
|
|
server.
|
|
|
|
"""
|
2018-05-22 02:47:37 +02:00
|
|
|
sent_queries_counter.labels("user_devices").inc()
|
2019-06-20 11:32:02 +02:00
|
|
|
return self.transport_layer.query_user_devices(destination, user_id, timeout)
|
2017-01-26 17:06:54 +01:00
|
|
|
|
2015-07-23 17:03:38 +02:00
|
|
|
@log_function
|
2016-09-12 19:17:09 +02:00
|
|
|
def claim_client_keys(self, destination, content, timeout):
|
2015-07-23 17:03:38 +02:00
|
|
|
"""Claims one-time keys for a device hosted on a remote server.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
destination (str): Domain name of the remote homeserver
|
|
|
|
content (dict): The query content.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
a Deferred which will eventually yield a JSON object from the
|
|
|
|
response
|
|
|
|
"""
|
2018-05-22 02:47:37 +02:00
|
|
|
sent_queries_counter.labels("client_one_time_keys").inc()
|
2019-06-20 11:32:02 +02:00
|
|
|
return self.transport_layer.claim_client_keys(destination, content, timeout)
|
2015-07-23 17:03:38 +02:00
|
|
|
|
2015-01-26 11:45:24 +01:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
@log_function
|
2019-01-23 21:21:33 +01:00
|
|
|
def backfill(self, dest, room_id, limit, extremities):
|
2015-01-26 11:45:24 +01:00
|
|
|
"""Requests some more historic PDUs for the given context from the
|
|
|
|
given destination server.
|
|
|
|
|
|
|
|
Args:
|
2019-11-12 14:08:12 +01:00
|
|
|
dest (str): The remote homeserver to ask.
|
2019-01-23 21:21:33 +01:00
|
|
|
room_id (str): The room_id to backfill.
|
2015-01-26 11:45:24 +01:00
|
|
|
limit (int): The maximum number of PDUs to return.
|
|
|
|
extremities (list): List of PDU id and origins of the first pdus
|
|
|
|
we have seen from the context
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Deferred: Results in the received PDUs.
|
|
|
|
"""
|
|
|
|
logger.debug("backfill extrem=%s", extremities)
|
|
|
|
|
|
|
|
# If there are no extremeties then we've (probably) reached the start.
|
|
|
|
if not extremities:
|
|
|
|
return
|
|
|
|
|
|
|
|
transaction_data = yield self.transport_layer.backfill(
|
2019-06-20 11:32:02 +02:00
|
|
|
dest, room_id, extremities, limit
|
|
|
|
)
|
2015-01-26 11:45:24 +01:00
|
|
|
|
2019-10-24 19:17:33 +02:00
|
|
|
logger.debug("backfill transaction_data=%r", transaction_data)
|
2015-01-26 11:45:24 +01:00
|
|
|
|
2019-01-23 21:21:33 +01:00
|
|
|
room_version = yield self.store.get_room_version(room_id)
|
|
|
|
format_ver = room_version_to_event_format(room_version)
|
|
|
|
|
2015-01-26 11:45:24 +01:00
|
|
|
pdus = [
|
2019-01-23 21:21:33 +01:00
|
|
|
event_from_pdu_json(p, format_ver, outlier=False)
|
2015-01-26 11:45:24 +01:00
|
|
|
for p in transaction_data["pdus"]
|
|
|
|
]
|
|
|
|
|
2015-05-20 12:59:02 +02:00
|
|
|
# FIXME: We should handle signature failures more gracefully.
|
2019-07-03 16:07:04 +02:00
|
|
|
pdus[:] = yield make_deferred_yieldable(
|
2019-06-20 11:32:02 +02:00
|
|
|
defer.gatherResults(
|
|
|
|
self._check_sigs_and_hashes(room_version, pdus), consumeErrors=True
|
|
|
|
).addErrback(unwrapFirstError)
|
|
|
|
)
|
2015-01-26 15:33:11 +01:00
|
|
|
|
2019-07-23 15:00:55 +02:00
|
|
|
return pdus
|
2015-01-26 11:45:24 +01:00
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
@log_function
|
2019-06-20 11:32:02 +02:00
|
|
|
def get_pdu(
|
|
|
|
self, destinations, event_id, room_version, outlier=False, timeout=None
|
|
|
|
):
|
2015-01-26 11:45:24 +01:00
|
|
|
"""Requests the PDU with given origin and ID from the remote home
|
|
|
|
servers.
|
|
|
|
|
|
|
|
Will attempt to get the PDU from each destination in the list until
|
|
|
|
one succeeds.
|
|
|
|
|
|
|
|
Args:
|
2019-11-12 14:08:12 +01:00
|
|
|
destinations (list): Which homeservers to query
|
2017-03-09 14:15:12 +01:00
|
|
|
event_id (str): event to fetch
|
2019-01-23 18:19:58 +01:00
|
|
|
room_version (str): version of the room
|
2015-01-26 11:45:24 +01:00
|
|
|
outlier (bool): Indicates whether the PDU is an `outlier`, i.e. if
|
|
|
|
it's from an arbitary point in the context as opposed to part
|
|
|
|
of the current block of PDUs. Defaults to `False`
|
2015-05-22 16:18:04 +02:00
|
|
|
timeout (int): How long to try (in ms) each destination for before
|
|
|
|
moving to the next destination. None indicates no timeout.
|
2015-01-26 11:45:24 +01:00
|
|
|
|
|
|
|
Returns:
|
2019-06-05 11:35:40 +02:00
|
|
|
Deferred: Results in the requested PDU, or None if we were unable to find
|
|
|
|
it.
|
2015-01-26 11:45:24 +01:00
|
|
|
"""
|
|
|
|
|
|
|
|
# TODO: Rate limit the number of times we try and get the same event.
|
|
|
|
|
2018-09-21 15:19:46 +02:00
|
|
|
ev = self._get_pdu_cache.get(event_id)
|
|
|
|
if ev:
|
2019-07-23 15:00:55 +02:00
|
|
|
return ev
|
2015-02-16 19:02:39 +01:00
|
|
|
|
2016-08-10 12:31:46 +02:00
|
|
|
pdu_attempts = self.pdu_destination_tried.setdefault(event_id, {})
|
|
|
|
|
2019-01-23 21:21:33 +01:00
|
|
|
format_ver = room_version_to_event_format(room_version)
|
|
|
|
|
2016-09-01 11:55:02 +02:00
|
|
|
signed_pdu = None
|
2015-01-26 11:45:24 +01:00
|
|
|
for destination in destinations:
|
2016-08-10 12:31:46 +02:00
|
|
|
now = self._clock.time_msec()
|
|
|
|
last_attempt = pdu_attempts.get(destination, 0)
|
|
|
|
if last_attempt + PDU_RETRY_TIME_MS > now:
|
|
|
|
continue
|
|
|
|
|
2015-01-26 11:45:24 +01:00
|
|
|
try:
|
2017-03-23 01:12:21 +01:00
|
|
|
transaction_data = yield self.transport_layer.get_event(
|
2019-06-20 11:32:02 +02:00
|
|
|
destination, event_id, timeout=timeout
|
2015-01-26 11:45:24 +01:00
|
|
|
)
|
2015-01-26 15:33:11 +01:00
|
|
|
|
2019-06-05 11:35:40 +02:00
|
|
|
logger.debug(
|
|
|
|
"retrieved event id %s from %s: %r",
|
|
|
|
event_id,
|
|
|
|
destination,
|
|
|
|
transaction_data,
|
|
|
|
)
|
2015-02-17 18:20:56 +01:00
|
|
|
|
2017-03-23 01:12:21 +01:00
|
|
|
pdu_list = [
|
2019-01-23 21:21:33 +01:00
|
|
|
event_from_pdu_json(p, format_ver, outlier=outlier)
|
2017-03-23 01:12:21 +01:00
|
|
|
for p in transaction_data["pdus"]
|
|
|
|
]
|
2015-01-26 15:33:11 +01:00
|
|
|
|
2017-03-23 01:12:21 +01:00
|
|
|
if pdu_list and pdu_list[0]:
|
|
|
|
pdu = pdu_list[0]
|
2015-01-26 15:33:11 +01:00
|
|
|
|
2017-03-23 01:12:21 +01:00
|
|
|
# Check signatures are correct.
|
2019-01-29 18:21:48 +01:00
|
|
|
signed_pdu = yield self._check_sigs_and_hash(room_version, pdu)
|
2015-01-26 15:33:11 +01:00
|
|
|
|
2017-03-23 01:12:21 +01:00
|
|
|
break
|
2015-01-26 15:33:11 +01:00
|
|
|
|
2016-08-10 12:31:46 +02:00
|
|
|
pdu_attempts[destination] = now
|
|
|
|
|
2016-08-10 14:39:12 +02:00
|
|
|
except SynapseError as e:
|
2015-02-16 19:02:39 +01:00
|
|
|
logger.info(
|
2019-06-20 11:32:02 +02:00
|
|
|
"Failed to get PDU %s from %s because %s", event_id, destination, e
|
2015-02-16 19:02:39 +01:00
|
|
|
)
|
2019-06-04 19:05:06 +02:00
|
|
|
continue
|
2015-02-17 18:20:56 +01:00
|
|
|
except NotRetryingDestination as e:
|
2018-09-12 15:23:32 +02:00
|
|
|
logger.info(str(e))
|
2015-02-17 18:20:56 +01:00
|
|
|
continue
|
2018-01-22 19:11:18 +01:00
|
|
|
except FederationDeniedError as e:
|
2018-09-12 15:23:32 +02:00
|
|
|
logger.info(str(e))
|
2018-01-22 19:11:18 +01:00
|
|
|
continue
|
2015-01-26 11:45:24 +01:00
|
|
|
except Exception as e:
|
2016-08-10 12:31:46 +02:00
|
|
|
pdu_attempts[destination] = now
|
|
|
|
|
2015-01-26 11:45:24 +01:00
|
|
|
logger.info(
|
2019-06-20 11:32:02 +02:00
|
|
|
"Failed to get PDU %s from %s because %s", event_id, destination, e
|
2015-01-26 11:45:24 +01:00
|
|
|
)
|
|
|
|
continue
|
|
|
|
|
2018-09-21 15:19:46 +02:00
|
|
|
if signed_pdu:
|
2016-09-01 11:55:02 +02:00
|
|
|
self._get_pdu_cache[event_id] = signed_pdu
|
2015-02-16 19:02:39 +01:00
|
|
|
|
2019-07-23 15:00:55 +02:00
|
|
|
return signed_pdu
|
2015-01-26 11:45:24 +01:00
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
2019-12-10 18:42:46 +01:00
|
|
|
def get_room_state_ids(self, destination: str, room_id: str, event_id: str):
|
|
|
|
"""Calls the /state_ids endpoint to fetch the state at a particular point
|
|
|
|
in the room, and the auth events for the given event
|
2015-01-26 11:45:24 +01:00
|
|
|
|
|
|
|
Returns:
|
2019-12-10 18:42:46 +01:00
|
|
|
Tuple[List[str], List[str]]: a tuple of (state event_ids, auth event_ids)
|
2015-01-26 11:45:24 +01:00
|
|
|
"""
|
2019-12-09 12:37:26 +01:00
|
|
|
result = yield self.transport_layer.get_room_state_ids(
|
2019-06-20 11:32:02 +02:00
|
|
|
destination, room_id, event_id=event_id
|
2016-08-03 16:04:29 +02:00
|
|
|
)
|
|
|
|
|
2019-12-09 12:37:26 +01:00
|
|
|
state_event_ids = result["pdu_ids"]
|
|
|
|
auth_event_ids = result.get("auth_chain_ids", [])
|
2015-01-26 11:45:24 +01:00
|
|
|
|
2019-12-10 18:42:46 +01:00
|
|
|
if not isinstance(state_event_ids, list) or not isinstance(
|
|
|
|
auth_event_ids, list
|
|
|
|
):
|
|
|
|
raise Exception("invalid response from /state_ids")
|
2016-08-03 15:47:37 +02:00
|
|
|
|
2019-12-10 18:42:46 +01:00
|
|
|
return state_event_ids, auth_event_ids
|
2016-08-03 15:47:37 +02:00
|
|
|
|
2015-01-26 11:45:24 +01:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
@log_function
|
|
|
|
def get_event_auth(self, destination, room_id, event_id):
|
2019-06-20 11:32:02 +02:00
|
|
|
res = yield self.transport_layer.get_event_auth(destination, room_id, event_id)
|
2015-01-26 11:45:24 +01:00
|
|
|
|
2019-01-23 21:21:33 +01:00
|
|
|
room_version = yield self.store.get_room_version(room_id)
|
|
|
|
format_ver = room_version_to_event_format(room_version)
|
|
|
|
|
2015-01-26 11:45:24 +01:00
|
|
|
auth_chain = [
|
2019-06-20 11:32:02 +02:00
|
|
|
event_from_pdu_json(p, format_ver, outlier=True) for p in res["auth_chain"]
|
2015-01-26 11:45:24 +01:00
|
|
|
]
|
|
|
|
|
2015-02-02 17:56:01 +01:00
|
|
|
signed_auth = yield self._check_sigs_and_hash_and_fetch(
|
2019-06-20 11:32:02 +02:00
|
|
|
destination, auth_chain, outlier=True, room_version=room_version
|
2015-02-02 17:56:01 +01:00
|
|
|
)
|
2015-01-26 15:33:11 +01:00
|
|
|
|
2015-02-02 17:56:01 +01:00
|
|
|
signed_auth.sort(key=lambda e: e.depth)
|
2015-01-26 11:45:24 +01:00
|
|
|
|
2019-07-23 15:00:55 +02:00
|
|
|
return signed_auth
|
2015-01-26 11:45:24 +01:00
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
2018-08-01 12:24:19 +02:00
|
|
|
def _try_destination_list(self, description, destinations, callback):
|
|
|
|
"""Try an operation on a series of servers, until it succeeds
|
|
|
|
|
|
|
|
Args:
|
|
|
|
description (unicode): description of the operation we're doing, for logging
|
|
|
|
|
|
|
|
destinations (Iterable[unicode]): list of server_names to try
|
|
|
|
|
|
|
|
callback (callable): Function to run for each server. Passed a single
|
|
|
|
argument: the server_name to try. May return a deferred.
|
|
|
|
|
|
|
|
If the callback raises a CodeMessageException with a 300/400 code,
|
|
|
|
attempts to perform the operation stop immediately and the exception is
|
|
|
|
reraised.
|
|
|
|
|
|
|
|
Otherwise, if the callback raises an Exception the error is logged and the
|
|
|
|
next server tried. Normally the stacktrace is logged but this is
|
|
|
|
suppressed if the exception is an InvalidResponseError.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
The [Deferred] result of callback, if it succeeds
|
|
|
|
|
|
|
|
Raises:
|
2019-08-01 14:47:31 +02:00
|
|
|
SynapseError if the chosen remote server returns a 300/400 code, or
|
|
|
|
no servers were reachable.
|
2018-08-01 12:24:19 +02:00
|
|
|
"""
|
|
|
|
for destination in destinations:
|
|
|
|
if destination == self.server_name:
|
|
|
|
continue
|
|
|
|
|
|
|
|
try:
|
|
|
|
res = yield callback(destination)
|
2019-07-23 15:00:55 +02:00
|
|
|
return res
|
2018-08-01 12:24:19 +02:00
|
|
|
except InvalidResponseError as e:
|
2019-10-31 11:23:24 +01:00
|
|
|
logger.warning("Failed to %s via %s: %s", description, destination, e)
|
2020-01-27 15:30:57 +01:00
|
|
|
except UnsupportedRoomVersionError:
|
|
|
|
raise
|
2018-08-01 14:47:07 +02:00
|
|
|
except HttpResponseException as e:
|
2018-08-01 12:24:19 +02:00
|
|
|
if not 500 <= e.code < 600:
|
2018-08-01 15:58:16 +02:00
|
|
|
raise e.to_synapse_error()
|
2018-08-01 12:24:19 +02:00
|
|
|
else:
|
2019-10-31 11:23:24 +01:00
|
|
|
logger.warning(
|
2018-08-01 12:24:19 +02:00
|
|
|
"Failed to %s via %s: %i %s",
|
2019-06-20 11:32:02 +02:00
|
|
|
description,
|
|
|
|
destination,
|
|
|
|
e.code,
|
|
|
|
e.args[0],
|
2018-08-01 12:24:19 +02:00
|
|
|
)
|
|
|
|
except Exception:
|
2019-10-31 11:23:24 +01:00
|
|
|
logger.warning(
|
|
|
|
"Failed to %s via %s", description, destination, exc_info=1
|
|
|
|
)
|
2018-08-01 12:24:19 +02:00
|
|
|
|
2019-08-01 14:47:31 +02:00
|
|
|
raise SynapseError(502, "Failed to %s via any server" % (description,))
|
2018-08-01 12:24:19 +02:00
|
|
|
|
2019-06-20 11:32:02 +02:00
|
|
|
def make_membership_event(
|
2020-01-27 15:30:57 +01:00
|
|
|
self,
|
|
|
|
destinations: Iterable[str],
|
|
|
|
room_id: str,
|
|
|
|
user_id: str,
|
|
|
|
membership: str,
|
|
|
|
content: dict,
|
|
|
|
params: Dict[str, str],
|
2019-06-20 11:32:02 +02:00
|
|
|
):
|
2015-10-20 12:58:58 +02:00
|
|
|
"""
|
|
|
|
Creates an m.room.member event, with context, without participating in the room.
|
|
|
|
|
|
|
|
Does so by asking one of the already participating servers to create an
|
|
|
|
event with proper context.
|
|
|
|
|
2019-01-24 19:08:08 +01:00
|
|
|
Returns a fully signed and hashed event.
|
|
|
|
|
2015-10-20 12:58:58 +02:00
|
|
|
Note that this does not append any events to any graphs.
|
|
|
|
|
|
|
|
Args:
|
2020-01-27 15:30:57 +01:00
|
|
|
destinations: Candidate homeservers which are probably
|
2015-10-20 12:58:58 +02:00
|
|
|
participating in the room.
|
2020-01-27 15:30:57 +01:00
|
|
|
room_id: The room in which the event will happen.
|
|
|
|
user_id: The user whose membership is being evented.
|
|
|
|
membership: The "membership" property of the event. Must be one of
|
|
|
|
"join" or "leave".
|
|
|
|
content: Any additional data to put into the content field of the
|
|
|
|
event.
|
|
|
|
params: Query parameters to include in the request.
|
2015-10-20 12:58:58 +02:00
|
|
|
Return:
|
2020-01-27 15:30:57 +01:00
|
|
|
Deferred[Tuple[str, FrozenEvent, RoomVersion]]: resolves to a tuple of
|
|
|
|
`(origin, event, room_version)` where origin is the remote
|
|
|
|
homeserver which generated the event, and room_version is the
|
|
|
|
version of the room.
|
|
|
|
|
|
|
|
Fails with a `UnsupportedRoomVersionError` if remote responds with
|
|
|
|
a room version we don't understand.
|
2017-04-21 01:46:54 +02:00
|
|
|
|
2018-08-01 14:47:07 +02:00
|
|
|
Fails with a ``SynapseError`` if the chosen remote server
|
2017-04-21 01:46:54 +02:00
|
|
|
returns a 300/400 code.
|
|
|
|
|
|
|
|
Fails with a ``RuntimeError`` if no servers were reachable.
|
2015-10-20 12:58:58 +02:00
|
|
|
"""
|
|
|
|
valid_memberships = {Membership.JOIN, Membership.LEAVE}
|
|
|
|
if membership not in valid_memberships:
|
|
|
|
raise RuntimeError(
|
2019-06-20 11:32:02 +02:00
|
|
|
"make_membership_event called with membership='%s', must be one of %s"
|
|
|
|
% (membership, ",".join(valid_memberships))
|
2015-10-20 12:58:58 +02:00
|
|
|
)
|
2015-06-26 10:52:24 +02:00
|
|
|
|
2018-08-01 12:24:19 +02:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def send_request(destination):
|
|
|
|
ret = yield self.transport_layer.make_membership_event(
|
2019-06-20 11:32:02 +02:00
|
|
|
destination, room_id, user_id, membership, params
|
2018-08-01 12:24:19 +02:00
|
|
|
)
|
2015-01-26 11:45:24 +01:00
|
|
|
|
2019-01-23 17:50:06 +01:00
|
|
|
# Note: If not supplied, the room version may be either v1 or v2,
|
|
|
|
# however either way the event format version will be v1.
|
2020-01-27 15:30:57 +01:00
|
|
|
room_version_id = ret.get("room_version", RoomVersions.V1.identifier)
|
|
|
|
room_version = KNOWN_ROOM_VERSIONS.get(room_version_id)
|
|
|
|
if not room_version:
|
|
|
|
raise UnsupportedRoomVersionError()
|
|
|
|
|
|
|
|
event_format = room_version_to_event_format(room_version_id)
|
2019-01-23 17:50:06 +01:00
|
|
|
|
2018-08-01 16:35:29 +02:00
|
|
|
pdu_dict = ret.get("event", None)
|
|
|
|
if not isinstance(pdu_dict, dict):
|
|
|
|
raise InvalidResponseError("Bad 'event' field in response")
|
2015-01-26 11:45:24 +01:00
|
|
|
|
2018-08-01 12:24:19 +02:00
|
|
|
logger.debug("Got response to make_%s: %s", membership, pdu_dict)
|
2015-01-26 11:45:24 +01:00
|
|
|
|
2018-08-01 12:24:19 +02:00
|
|
|
pdu_dict["content"].update(content)
|
2015-11-12 17:19:55 +01:00
|
|
|
|
2018-08-01 12:24:19 +02:00
|
|
|
# The protoevent received over the JSON wire may not have all
|
|
|
|
# the required fields. Lets just gloss over that because
|
|
|
|
# there's some we never care about
|
|
|
|
if "prev_state" not in pdu_dict:
|
|
|
|
pdu_dict["prev_state"] = []
|
2015-11-13 18:27:25 +01:00
|
|
|
|
2019-01-25 18:19:31 +01:00
|
|
|
ev = builder.create_local_event_from_event_dict(
|
2019-06-20 11:32:02 +02:00
|
|
|
self._clock,
|
|
|
|
self.hostname,
|
|
|
|
self.signing_key,
|
|
|
|
format_version=event_format,
|
|
|
|
event_dict=pdu_dict,
|
2019-01-23 20:44:37 +01:00
|
|
|
)
|
2017-01-13 14:21:04 +01:00
|
|
|
|
2020-01-27 15:30:57 +01:00
|
|
|
return (destination, ev, room_version)
|
2015-02-04 17:28:12 +01:00
|
|
|
|
2018-08-01 12:24:19 +02:00
|
|
|
return self._try_destination_list(
|
2019-06-20 11:32:02 +02:00
|
|
|
"make_" + membership, destinations, send_request
|
2018-08-01 12:24:19 +02:00
|
|
|
)
|
2015-01-26 11:45:24 +01:00
|
|
|
|
2019-01-23 21:21:33 +01:00
|
|
|
def send_join(self, destinations, pdu, event_format_version):
|
2017-04-21 01:46:54 +02:00
|
|
|
"""Sends a join event to one of a list of homeservers.
|
|
|
|
|
|
|
|
Doing so will cause the remote server to add the event to the graph,
|
|
|
|
and send the event out to the rest of the federation.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
destinations (str): Candidate homeservers which are probably
|
|
|
|
participating in the room.
|
|
|
|
pdu (BaseEvent): event to be sent
|
2019-01-23 21:21:33 +01:00
|
|
|
event_format_version (int): The event format version
|
2017-04-21 01:46:54 +02:00
|
|
|
|
|
|
|
Return:
|
|
|
|
Deferred: resolves to a dict with members ``origin`` (a string
|
|
|
|
giving the serer the event was sent to, ``state`` (?) and
|
|
|
|
``auth_chain``.
|
|
|
|
|
2018-08-01 14:47:07 +02:00
|
|
|
Fails with a ``SynapseError`` if the chosen remote server
|
2017-04-21 01:46:54 +02:00
|
|
|
returns a 300/400 code.
|
|
|
|
|
|
|
|
Fails with a ``RuntimeError`` if no servers were reachable.
|
|
|
|
"""
|
|
|
|
|
2018-08-06 16:15:19 +02:00
|
|
|
def check_authchain_validity(signed_auth_chain):
|
|
|
|
for e in signed_auth_chain:
|
|
|
|
if e.type == EventTypes.Create:
|
|
|
|
create_event = e
|
|
|
|
break
|
|
|
|
else:
|
2019-06-20 11:32:02 +02:00
|
|
|
raise InvalidResponseError("no %s in auth chain" % (EventTypes.Create,))
|
2018-08-06 16:15:19 +02:00
|
|
|
|
|
|
|
# the room version should be sane.
|
|
|
|
room_version = create_event.content.get("room_version", "1")
|
|
|
|
if room_version not in KNOWN_ROOM_VERSIONS:
|
|
|
|
# This shouldn't be possible, because the remote server should have
|
|
|
|
# rejected the join attempt during make_join.
|
|
|
|
raise InvalidResponseError(
|
2019-06-20 11:32:02 +02:00
|
|
|
"room appears to have unsupported version %s" % (room_version,)
|
|
|
|
)
|
2018-08-06 16:15:19 +02:00
|
|
|
|
2018-08-01 12:24:19 +02:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def send_request(destination):
|
2019-11-11 17:20:53 +01:00
|
|
|
content = yield self._do_send_join(destination, pdu)
|
2015-01-26 11:45:24 +01:00
|
|
|
|
2018-08-01 12:24:19 +02:00
|
|
|
logger.debug("Got content: %s", content)
|
2015-01-26 11:45:24 +01:00
|
|
|
|
2018-08-01 12:24:19 +02:00
|
|
|
state = [
|
2019-01-23 21:21:33 +01:00
|
|
|
event_from_pdu_json(p, event_format_version, outlier=True)
|
2018-08-01 12:24:19 +02:00
|
|
|
for p in content.get("state", [])
|
|
|
|
]
|
2015-01-26 11:45:24 +01:00
|
|
|
|
2018-08-01 12:24:19 +02:00
|
|
|
auth_chain = [
|
2019-01-23 21:21:33 +01:00
|
|
|
event_from_pdu_json(p, event_format_version, outlier=True)
|
2018-08-01 12:24:19 +02:00
|
|
|
for p in content.get("auth_chain", [])
|
|
|
|
]
|
2015-01-26 11:45:24 +01:00
|
|
|
|
2019-06-20 11:32:02 +02:00
|
|
|
pdus = {p.event_id: p for p in itertools.chain(state, auth_chain)}
|
2015-06-26 10:52:24 +02:00
|
|
|
|
2019-01-23 18:19:58 +01:00
|
|
|
room_version = None
|
|
|
|
for e in state:
|
|
|
|
if (e.type, e.state_key) == (EventTypes.Create, ""):
|
2019-04-01 11:24:38 +02:00
|
|
|
room_version = e.content.get(
|
|
|
|
"room_version", RoomVersions.V1.identifier
|
|
|
|
)
|
2019-01-23 18:19:58 +01:00
|
|
|
break
|
|
|
|
|
|
|
|
if room_version is None:
|
2019-01-24 19:31:23 +01:00
|
|
|
# If the state doesn't have a create event then the room is
|
|
|
|
# invalid, and it would fail auth checks anyway.
|
2019-01-23 18:19:58 +01:00
|
|
|
raise SynapseError(400, "No create event in state")
|
|
|
|
|
2018-08-01 12:24:19 +02:00
|
|
|
valid_pdus = yield self._check_sigs_and_hash_and_fetch(
|
2019-06-20 11:32:02 +02:00
|
|
|
destination,
|
|
|
|
list(pdus.values()),
|
2018-08-01 12:24:19 +02:00
|
|
|
outlier=True,
|
2019-01-23 18:19:58 +01:00
|
|
|
room_version=room_version,
|
2018-08-01 12:24:19 +02:00
|
|
|
)
|
2015-06-26 10:52:24 +02:00
|
|
|
|
2019-06-20 11:32:02 +02:00
|
|
|
valid_pdus_map = {p.event_id: p for p in valid_pdus}
|
2015-06-26 10:52:24 +02:00
|
|
|
|
2018-08-01 12:24:19 +02:00
|
|
|
# NB: We *need* to copy to ensure that we don't have multiple
|
|
|
|
# references being passed on, as that causes... issues.
|
|
|
|
signed_state = [
|
|
|
|
copy.copy(valid_pdus_map[p.event_id])
|
|
|
|
for p in state
|
|
|
|
if p.event_id in valid_pdus_map
|
|
|
|
]
|
2015-06-26 10:52:24 +02:00
|
|
|
|
2018-08-01 12:24:19 +02:00
|
|
|
signed_auth = [
|
|
|
|
valid_pdus_map[p.event_id]
|
|
|
|
for p in auth_chain
|
|
|
|
if p.event_id in valid_pdus_map
|
|
|
|
]
|
2015-01-26 15:33:11 +01:00
|
|
|
|
2018-08-01 12:24:19 +02:00
|
|
|
# NB: We *need* to copy to ensure that we don't have multiple
|
|
|
|
# references being passed on, as that causes... issues.
|
|
|
|
for s in signed_state:
|
|
|
|
s.internal_metadata = copy.deepcopy(s.internal_metadata)
|
2015-02-04 17:28:12 +01:00
|
|
|
|
2018-08-06 16:15:19 +02:00
|
|
|
check_authchain_validity(signed_auth)
|
2015-01-26 11:45:24 +01:00
|
|
|
|
2019-07-23 15:00:55 +02:00
|
|
|
return {
|
|
|
|
"state": signed_state,
|
|
|
|
"auth_chain": signed_auth,
|
|
|
|
"origin": destination,
|
|
|
|
}
|
2019-06-20 11:32:02 +02:00
|
|
|
|
2018-08-01 12:24:19 +02:00
|
|
|
return self._try_destination_list("send_join", destinations, send_request)
|
2015-01-26 11:45:24 +01:00
|
|
|
|
2019-11-11 16:47:47 +01:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def _do_send_join(self, destination, pdu):
|
|
|
|
time_now = self._clock.time_msec()
|
|
|
|
|
|
|
|
try:
|
|
|
|
content = yield self.transport_layer.send_join_v2(
|
|
|
|
destination=destination,
|
|
|
|
room_id=pdu.room_id,
|
|
|
|
event_id=pdu.event_id,
|
|
|
|
content=pdu.get_pdu_json(time_now),
|
|
|
|
)
|
|
|
|
|
|
|
|
return content
|
|
|
|
except HttpResponseException as e:
|
|
|
|
if e.code in [400, 404]:
|
|
|
|
err = e.to_synapse_error()
|
|
|
|
|
|
|
|
# If we receive an error response that isn't a generic error, or an
|
|
|
|
# unrecognised endpoint error, we assume that the remote understands
|
|
|
|
# the v2 invite API and this is a legitimate error.
|
2019-11-11 17:56:55 +01:00
|
|
|
if err.errcode not in [Codes.UNKNOWN, Codes.UNRECOGNIZED]:
|
2019-11-11 16:47:47 +01:00
|
|
|
raise err
|
|
|
|
else:
|
|
|
|
raise e.to_synapse_error()
|
|
|
|
|
|
|
|
logger.debug("Couldn't send_join with the v2 API, falling back to the v1 API")
|
|
|
|
|
|
|
|
resp = yield self.transport_layer.send_join_v1(
|
|
|
|
destination=destination,
|
|
|
|
room_id=pdu.room_id,
|
|
|
|
event_id=pdu.event_id,
|
|
|
|
content=pdu.get_pdu_json(time_now),
|
|
|
|
)
|
|
|
|
|
|
|
|
# We expect the v1 API to respond with [200, content], so we only return the
|
|
|
|
# content.
|
|
|
|
return resp[1]
|
|
|
|
|
2015-01-26 11:45:24 +01:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def send_invite(self, destination, room_id, event_id, pdu):
|
2019-01-28 15:55:53 +01:00
|
|
|
room_version = yield self.store.get_room_version(room_id)
|
|
|
|
|
|
|
|
content = yield self._do_send_invite(destination, pdu, room_version)
|
2015-01-26 11:45:24 +01:00
|
|
|
|
|
|
|
pdu_dict = content["event"]
|
|
|
|
|
|
|
|
logger.debug("Got response to send_invite: %s", pdu_dict)
|
|
|
|
|
2019-01-23 21:21:33 +01:00
|
|
|
room_version = yield self.store.get_room_version(room_id)
|
|
|
|
format_ver = room_version_to_event_format(room_version)
|
|
|
|
|
|
|
|
pdu = event_from_pdu_json(pdu_dict, format_ver)
|
2015-01-26 15:33:11 +01:00
|
|
|
|
|
|
|
# Check signatures are correct.
|
2019-01-29 18:21:48 +01:00
|
|
|
pdu = yield self._check_sigs_and_hash(room_version, pdu)
|
2015-01-26 15:33:11 +01:00
|
|
|
|
|
|
|
# FIXME: We should handle signature failures more gracefully.
|
|
|
|
|
2019-07-23 15:00:55 +02:00
|
|
|
return pdu
|
2015-01-26 11:45:24 +01:00
|
|
|
|
2019-01-28 15:55:53 +01:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def _do_send_invite(self, destination, pdu, room_version):
|
|
|
|
"""Actually sends the invite, first trying v2 API and falling back to
|
|
|
|
v1 API if necessary.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
destination (str): Target server
|
|
|
|
pdu (FrozenEvent)
|
|
|
|
room_version (str)
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
dict: The event as a dict as returned by the remote server
|
|
|
|
"""
|
|
|
|
time_now = self._clock.time_msec()
|
|
|
|
|
|
|
|
try:
|
|
|
|
content = yield self.transport_layer.send_invite_v2(
|
|
|
|
destination=destination,
|
|
|
|
room_id=pdu.room_id,
|
|
|
|
event_id=pdu.event_id,
|
|
|
|
content={
|
|
|
|
"event": pdu.get_pdu_json(time_now),
|
|
|
|
"room_version": room_version,
|
|
|
|
"invite_room_state": pdu.unsigned.get("invite_room_state", []),
|
|
|
|
},
|
|
|
|
)
|
2019-07-23 15:00:55 +02:00
|
|
|
return content
|
2019-01-28 15:55:53 +01:00
|
|
|
except HttpResponseException as e:
|
|
|
|
if e.code in [400, 404]:
|
2019-02-23 15:31:08 +01:00
|
|
|
err = e.to_synapse_error()
|
|
|
|
|
|
|
|
# If we receive an error response that isn't a generic error, we
|
|
|
|
# assume that the remote understands the v2 invite API and this
|
|
|
|
# is a legitimate error.
|
|
|
|
if err.errcode != Codes.UNKNOWN:
|
|
|
|
raise err
|
|
|
|
|
|
|
|
# Otherwise, we assume that the remote server doesn't understand
|
2019-04-01 11:24:38 +02:00
|
|
|
# the v2 invite API. That's ok provided the room uses old-style event
|
|
|
|
# IDs.
|
|
|
|
v = KNOWN_ROOM_VERSIONS.get(room_version)
|
|
|
|
if v.event_format != EventFormatVersions.V1:
|
2019-02-23 15:31:08 +01:00
|
|
|
raise SynapseError(
|
|
|
|
400,
|
|
|
|
"User's homeserver does not support this room version",
|
|
|
|
Codes.UNSUPPORTED_ROOM_VERSION,
|
|
|
|
)
|
2019-01-28 15:55:53 +01:00
|
|
|
elif e.code == 403:
|
|
|
|
raise e.to_synapse_error()
|
|
|
|
else:
|
|
|
|
raise
|
|
|
|
|
|
|
|
# Didn't work, try v1 API.
|
|
|
|
# Note the v1 API returns a tuple of `(200, content)`
|
|
|
|
|
|
|
|
_, content = yield self.transport_layer.send_invite_v1(
|
|
|
|
destination=destination,
|
|
|
|
room_id=pdu.room_id,
|
|
|
|
event_id=pdu.event_id,
|
|
|
|
content=pdu.get_pdu_json(time_now),
|
|
|
|
)
|
2019-07-23 15:00:55 +02:00
|
|
|
return content
|
2019-01-28 15:55:53 +01:00
|
|
|
|
2015-10-20 12:58:58 +02:00
|
|
|
def send_leave(self, destinations, pdu):
|
2017-04-21 01:46:54 +02:00
|
|
|
"""Sends a leave event to one of a list of homeservers.
|
|
|
|
|
|
|
|
Doing so will cause the remote server to add the event to the graph,
|
|
|
|
and send the event out to the rest of the federation.
|
|
|
|
|
|
|
|
This is mostly useful to reject received invites.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
destinations (str): Candidate homeservers which are probably
|
|
|
|
participating in the room.
|
|
|
|
pdu (BaseEvent): event to be sent
|
|
|
|
|
|
|
|
Return:
|
|
|
|
Deferred: resolves to None.
|
|
|
|
|
2018-08-01 14:47:07 +02:00
|
|
|
Fails with a ``SynapseError`` if the chosen remote server
|
2018-08-01 12:24:19 +02:00
|
|
|
returns a 300/400 code.
|
2017-04-21 01:46:54 +02:00
|
|
|
|
|
|
|
Fails with a ``RuntimeError`` if no servers were reachable.
|
|
|
|
"""
|
2019-06-20 11:32:02 +02:00
|
|
|
|
2018-08-01 12:24:19 +02:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def send_request(destination):
|
2019-11-11 17:26:53 +01:00
|
|
|
content = yield self._do_send_leave(destination, pdu)
|
|
|
|
|
|
|
|
logger.debug("Got content: %s", content)
|
|
|
|
return None
|
|
|
|
|
|
|
|
return self._try_destination_list("send_leave", destinations, send_request)
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def _do_send_leave(self, destination, pdu):
|
|
|
|
time_now = self._clock.time_msec()
|
|
|
|
|
|
|
|
try:
|
|
|
|
content = yield self.transport_layer.send_leave_v2(
|
2018-08-01 12:24:19 +02:00
|
|
|
destination=destination,
|
|
|
|
room_id=pdu.room_id,
|
|
|
|
event_id=pdu.event_id,
|
|
|
|
content=pdu.get_pdu_json(time_now),
|
|
|
|
)
|
2015-10-20 12:58:58 +02:00
|
|
|
|
2019-11-11 17:26:53 +01:00
|
|
|
return content
|
|
|
|
except HttpResponseException as e:
|
|
|
|
if e.code in [400, 404]:
|
|
|
|
err = e.to_synapse_error()
|
2015-10-20 12:58:58 +02:00
|
|
|
|
2019-11-11 17:26:53 +01:00
|
|
|
# If we receive an error response that isn't a generic error, or an
|
|
|
|
# unrecognised endpoint error, we assume that the remote understands
|
|
|
|
# the v2 invite API and this is a legitimate error.
|
2019-11-11 17:56:55 +01:00
|
|
|
if err.errcode not in [Codes.UNKNOWN, Codes.UNRECOGNIZED]:
|
2019-11-11 17:26:53 +01:00
|
|
|
raise err
|
|
|
|
else:
|
|
|
|
raise e.to_synapse_error()
|
|
|
|
|
|
|
|
logger.debug("Couldn't send_leave with the v2 API, falling back to the v1 API")
|
|
|
|
|
|
|
|
resp = yield self.transport_layer.send_leave_v1(
|
|
|
|
destination=destination,
|
|
|
|
room_id=pdu.room_id,
|
|
|
|
event_id=pdu.event_id,
|
|
|
|
content=pdu.get_pdu_json(time_now),
|
|
|
|
)
|
|
|
|
|
|
|
|
# We expect the v1 API to respond with [200, content], so we only return the
|
|
|
|
# content.
|
|
|
|
return resp[1]
|
2015-10-20 12:58:58 +02:00
|
|
|
|
2019-06-20 11:32:02 +02:00
|
|
|
def get_public_rooms(
|
|
|
|
self,
|
|
|
|
destination,
|
|
|
|
limit=None,
|
|
|
|
since_token=None,
|
|
|
|
search_filter=None,
|
|
|
|
include_all_networks=False,
|
|
|
|
third_party_instance_id=None,
|
|
|
|
):
|
2016-09-15 11:36:19 +02:00
|
|
|
if destination == self.server_name:
|
|
|
|
return
|
2016-05-31 18:20:07 +02:00
|
|
|
|
2016-09-16 11:24:15 +02:00
|
|
|
return self.transport_layer.get_public_rooms(
|
2019-06-20 11:32:02 +02:00
|
|
|
destination,
|
|
|
|
limit,
|
|
|
|
since_token,
|
|
|
|
search_filter,
|
2016-12-06 11:43:48 +01:00
|
|
|
include_all_networks=include_all_networks,
|
|
|
|
third_party_instance_id=third_party_instance_id,
|
2016-09-16 11:24:15 +02:00
|
|
|
)
|
2016-05-31 18:20:07 +02:00
|
|
|
|
2015-02-23 14:58:02 +01:00
|
|
|
@defer.inlineCallbacks
|
2019-06-20 11:32:02 +02:00
|
|
|
def get_missing_events(
|
|
|
|
self,
|
|
|
|
destination,
|
|
|
|
room_id,
|
|
|
|
earliest_events_ids,
|
|
|
|
latest_events,
|
|
|
|
limit,
|
|
|
|
min_depth,
|
|
|
|
timeout,
|
|
|
|
):
|
2015-03-05 17:31:13 +01:00
|
|
|
"""Tries to fetch events we are missing. This is called when we receive
|
|
|
|
an event without having received all of its ancestors.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
destination (str)
|
|
|
|
room_id (str)
|
|
|
|
earliest_events_ids (list): List of event ids. Effectively the
|
|
|
|
events we expected to receive, but haven't. `get_missing_events`
|
|
|
|
should only return events that didn't happen before these.
|
|
|
|
latest_events (list): List of events we have received that we don't
|
|
|
|
have all previous events for.
|
|
|
|
limit (int): Maximum number of events to return.
|
|
|
|
min_depth (int): Minimum depth of events tor return.
|
2016-12-31 16:21:37 +01:00
|
|
|
timeout (int): Max time to wait in ms
|
2015-03-05 17:31:13 +01:00
|
|
|
"""
|
2015-03-05 17:08:02 +01:00
|
|
|
try:
|
|
|
|
content = yield self.transport_layer.get_missing_events(
|
|
|
|
destination=destination,
|
|
|
|
room_id=room_id,
|
|
|
|
earliest_events=earliest_events_ids,
|
|
|
|
latest_events=[e.event_id for e in latest_events],
|
|
|
|
limit=limit,
|
|
|
|
min_depth=min_depth,
|
2016-12-31 16:21:37 +01:00
|
|
|
timeout=timeout,
|
2015-03-05 17:08:02 +01:00
|
|
|
)
|
|
|
|
|
2019-01-23 21:21:33 +01:00
|
|
|
room_version = yield self.store.get_room_version(room_id)
|
|
|
|
format_ver = room_version_to_event_format(room_version)
|
|
|
|
|
2015-03-05 17:08:02 +01:00
|
|
|
events = [
|
2019-06-20 11:32:02 +02:00
|
|
|
event_from_pdu_json(e, format_ver) for e in content.get("events", [])
|
2015-03-05 17:08:02 +01:00
|
|
|
]
|
|
|
|
|
|
|
|
signed_events = yield self._check_sigs_and_hash_and_fetch(
|
2019-06-20 11:32:02 +02:00
|
|
|
destination, events, outlier=False, room_version=room_version
|
2015-03-05 17:08:02 +01:00
|
|
|
)
|
|
|
|
except HttpResponseException as e:
|
|
|
|
if not e.code == 400:
|
|
|
|
raise
|
2015-02-23 14:58:02 +01:00
|
|
|
|
2015-03-05 17:31:13 +01:00
|
|
|
# We are probably hitting an old server that doesn't support
|
|
|
|
# get_missing_events
|
2015-03-05 17:08:02 +01:00
|
|
|
signed_events = []
|
|
|
|
|
2019-07-23 15:00:55 +02:00
|
|
|
return signed_events
|
2015-02-23 14:58:02 +01:00
|
|
|
|
2015-11-05 17:43:19 +01:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def forward_third_party_invite(self, destinations, room_id, event_dict):
|
|
|
|
for destination in destinations:
|
|
|
|
if destination == self.server_name:
|
|
|
|
continue
|
|
|
|
|
|
|
|
try:
|
|
|
|
yield self.transport_layer.exchange_third_party_invite(
|
2019-06-20 11:32:02 +02:00
|
|
|
destination=destination, room_id=room_id, event_dict=event_dict
|
2015-11-05 17:43:19 +01:00
|
|
|
)
|
2019-07-23 15:00:55 +02:00
|
|
|
return None
|
2015-11-05 17:43:19 +01:00
|
|
|
except CodeMessageException:
|
|
|
|
raise
|
|
|
|
except Exception as e:
|
|
|
|
logger.exception(
|
2019-06-20 11:32:02 +02:00
|
|
|
"Failed to send_third_party_invite via %s: %s", destination, str(e)
|
2015-11-05 17:43:19 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
raise RuntimeError("Failed to send to any server.")
|
2019-07-29 18:47:27 +02:00
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def get_room_complexity(self, destination, room_id):
|
|
|
|
"""
|
|
|
|
Fetch the complexity of a remote room from another server.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
destination (str): The remote server
|
|
|
|
room_id (str): The room ID to ask about.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Deferred[dict] or Deferred[None]: Dict contains the complexity
|
|
|
|
metric versions, while None means we could not fetch the complexity.
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
complexity = yield self.transport_layer.get_room_complexity(
|
|
|
|
destination=destination, room_id=room_id
|
|
|
|
)
|
|
|
|
defer.returnValue(complexity)
|
|
|
|
except CodeMessageException as e:
|
|
|
|
# We didn't manage to get it -- probably a 404. We are okay if other
|
|
|
|
# servers don't give it to us.
|
|
|
|
logger.debug(
|
|
|
|
"Failed to fetch room complexity via %s for %s, got a %d",
|
|
|
|
destination,
|
|
|
|
room_id,
|
|
|
|
e.code,
|
|
|
|
)
|
|
|
|
except Exception:
|
|
|
|
logger.exception(
|
|
|
|
"Failed to fetch room complexity via %s for %s", destination, room_id
|
|
|
|
)
|
|
|
|
|
|
|
|
# If we don't manage to find it, return None. It's not an error if a
|
|
|
|
# server doesn't give it to us.
|
|
|
|
defer.returnValue(None)
|