federation_client.py 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2015, 2016 OpenMarket Ltd
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. import copy
  16. import itertools
  17. import logging
  18. from typing import (
  19. Any,
  20. Awaitable,
  21. Callable,
  22. Dict,
  23. Iterable,
  24. List,
  25. Optional,
  26. Sequence,
  27. Tuple,
  28. TypeVar,
  29. )
  30. from prometheus_client import Counter
  31. from twisted.internet import defer
  32. from twisted.internet.defer import Deferred
  33. from synapse.api.constants import EventTypes, Membership
  34. from synapse.api.errors import (
  35. CodeMessageException,
  36. Codes,
  37. FederationDeniedError,
  38. HttpResponseException,
  39. SynapseError,
  40. UnsupportedRoomVersionError,
  41. )
  42. from synapse.api.room_versions import (
  43. KNOWN_ROOM_VERSIONS,
  44. EventFormatVersions,
  45. RoomVersion,
  46. RoomVersions,
  47. )
  48. from synapse.events import EventBase, builder
  49. from synapse.federation.federation_base import FederationBase, event_from_pdu_json
  50. from synapse.logging.context import make_deferred_yieldable, preserve_fn
  51. from synapse.logging.utils import log_function
  52. from synapse.types import JsonDict
  53. from synapse.util import unwrapFirstError
  54. from synapse.util.caches.expiringcache import ExpiringCache
  55. from synapse.util.retryutils import NotRetryingDestination
  56. logger = logging.getLogger(__name__)
  57. sent_queries_counter = Counter("synapse_federation_client_sent_queries", "", ["type"])
  58. PDU_RETRY_TIME_MS = 1 * 60 * 1000
  59. T = TypeVar("T")
  60. class InvalidResponseError(RuntimeError):
  61. """Helper for _try_destination_list: indicates that the server returned a response
  62. we couldn't parse
  63. """
  64. pass
  65. class FederationClient(FederationBase):
  66. def __init__(self, hs):
  67. super(FederationClient, self).__init__(hs)
  68. self.pdu_destination_tried = {}
  69. self._clock.looping_call(self._clear_tried_cache, 60 * 1000)
  70. self.state = hs.get_state_handler()
  71. self.transport_layer = hs.get_federation_transport_client()
  72. self.hostname = hs.hostname
  73. self.signing_key = hs.signing_key
  74. self._get_pdu_cache = ExpiringCache(
  75. cache_name="get_pdu_cache",
  76. clock=self._clock,
  77. max_len=1000,
  78. expiry_ms=120 * 1000,
  79. reset_expiry_on_get=False,
  80. )
  81. def _clear_tried_cache(self):
  82. """Clear pdu_destination_tried cache"""
  83. now = self._clock.time_msec()
  84. old_dict = self.pdu_destination_tried
  85. self.pdu_destination_tried = {}
  86. for event_id, destination_dict in old_dict.items():
  87. destination_dict = {
  88. dest: time
  89. for dest, time in destination_dict.items()
  90. if time + PDU_RETRY_TIME_MS > now
  91. }
  92. if destination_dict:
  93. self.pdu_destination_tried[event_id] = destination_dict
  94. @log_function
  95. def make_query(
  96. self,
  97. destination,
  98. query_type,
  99. args,
  100. retry_on_dns_fail=False,
  101. ignore_backoff=False,
  102. ):
  103. """Sends a federation Query to a remote homeserver of the given type
  104. and arguments.
  105. Args:
  106. destination (str): Domain name of the remote homeserver
  107. query_type (str): Category of the query type; should match the
  108. handler name used in register_query_handler().
  109. args (dict): Mapping of strings to strings containing the details
  110. of the query request.
  111. ignore_backoff (bool): true to ignore the historical backoff data
  112. and try the request anyway.
  113. Returns:
  114. a Deferred which will eventually yield a JSON object from the
  115. response
  116. """
  117. sent_queries_counter.labels(query_type).inc()
  118. return self.transport_layer.make_query(
  119. destination,
  120. query_type,
  121. args,
  122. retry_on_dns_fail=retry_on_dns_fail,
  123. ignore_backoff=ignore_backoff,
  124. )
  125. @log_function
  126. def query_client_keys(self, destination, content, timeout):
  127. """Query device keys for a device hosted on a remote server.
  128. Args:
  129. destination (str): Domain name of the remote homeserver
  130. content (dict): The query content.
  131. Returns:
  132. a Deferred which will eventually yield a JSON object from the
  133. response
  134. """
  135. sent_queries_counter.labels("client_device_keys").inc()
  136. return self.transport_layer.query_client_keys(destination, content, timeout)
  137. @log_function
  138. def query_user_devices(self, destination, user_id, timeout=30000):
  139. """Query the device keys for a list of user ids hosted on a remote
  140. server.
  141. """
  142. sent_queries_counter.labels("user_devices").inc()
  143. return self.transport_layer.query_user_devices(destination, user_id, timeout)
  144. @log_function
  145. def claim_client_keys(self, destination, content, timeout):
  146. """Claims one-time keys for a device hosted on a remote server.
  147. Args:
  148. destination (str): Domain name of the remote homeserver
  149. content (dict): The query content.
  150. Returns:
  151. a Deferred which will eventually yield a JSON object from the
  152. response
  153. """
  154. sent_queries_counter.labels("client_one_time_keys").inc()
  155. return self.transport_layer.claim_client_keys(destination, content, timeout)
  156. async def backfill(
  157. self, dest: str, room_id: str, limit: int, extremities: Iterable[str]
  158. ) -> Optional[List[EventBase]]:
  159. """Requests some more historic PDUs for the given room from the
  160. given destination server.
  161. Args:
  162. dest (str): The remote homeserver to ask.
  163. room_id (str): The room_id to backfill.
  164. limit (int): The maximum number of events to return.
  165. extremities (list): our current backwards extremities, to backfill from
  166. """
  167. logger.debug("backfill extrem=%s", extremities)
  168. # If there are no extremities then we've (probably) reached the start.
  169. if not extremities:
  170. return None
  171. transaction_data = await self.transport_layer.backfill(
  172. dest, room_id, extremities, limit
  173. )
  174. logger.debug("backfill transaction_data=%r", transaction_data)
  175. room_version = await self.store.get_room_version(room_id)
  176. pdus = [
  177. event_from_pdu_json(p, room_version, outlier=False)
  178. for p in transaction_data["pdus"]
  179. ]
  180. # FIXME: We should handle signature failures more gracefully.
  181. pdus[:] = await make_deferred_yieldable(
  182. defer.gatherResults(
  183. self._check_sigs_and_hashes(room_version, pdus), consumeErrors=True,
  184. ).addErrback(unwrapFirstError)
  185. )
  186. return pdus
  187. async def get_pdu(
  188. self,
  189. destinations: Iterable[str],
  190. event_id: str,
  191. room_version: RoomVersion,
  192. outlier: bool = False,
  193. timeout: Optional[int] = None,
  194. ) -> Optional[EventBase]:
  195. """Requests the PDU with given origin and ID from the remote home
  196. servers.
  197. Will attempt to get the PDU from each destination in the list until
  198. one succeeds.
  199. Args:
  200. destinations: Which homeservers to query
  201. event_id: event to fetch
  202. room_version: version of the room
  203. outlier: Indicates whether the PDU is an `outlier`, i.e. if
  204. it's from an arbitary point in the context as opposed to part
  205. of the current block of PDUs. Defaults to `False`
  206. timeout: How long to try (in ms) each destination for before
  207. moving to the next destination. None indicates no timeout.
  208. Returns:
  209. The requested PDU, or None if we were unable to find it.
  210. """
  211. # TODO: Rate limit the number of times we try and get the same event.
  212. ev = self._get_pdu_cache.get(event_id)
  213. if ev:
  214. return ev
  215. pdu_attempts = self.pdu_destination_tried.setdefault(event_id, {})
  216. signed_pdu = None
  217. for destination in destinations:
  218. now = self._clock.time_msec()
  219. last_attempt = pdu_attempts.get(destination, 0)
  220. if last_attempt + PDU_RETRY_TIME_MS > now:
  221. continue
  222. try:
  223. transaction_data = await self.transport_layer.get_event(
  224. destination, event_id, timeout=timeout
  225. )
  226. logger.debug(
  227. "retrieved event id %s from %s: %r",
  228. event_id,
  229. destination,
  230. transaction_data,
  231. )
  232. pdu_list = [
  233. event_from_pdu_json(p, room_version, outlier=outlier)
  234. for p in transaction_data["pdus"]
  235. ] # type: List[EventBase]
  236. if pdu_list and pdu_list[0]:
  237. pdu = pdu_list[0]
  238. # Check signatures are correct.
  239. signed_pdu = await self._check_sigs_and_hash(room_version, pdu)
  240. break
  241. pdu_attempts[destination] = now
  242. except SynapseError as e:
  243. logger.info(
  244. "Failed to get PDU %s from %s because %s", event_id, destination, e
  245. )
  246. continue
  247. except NotRetryingDestination as e:
  248. logger.info(str(e))
  249. continue
  250. except FederationDeniedError as e:
  251. logger.info(str(e))
  252. continue
  253. except Exception as e:
  254. pdu_attempts[destination] = now
  255. logger.info(
  256. "Failed to get PDU %s from %s because %s", event_id, destination, e
  257. )
  258. continue
  259. if signed_pdu:
  260. self._get_pdu_cache[event_id] = signed_pdu
  261. return signed_pdu
  262. async def get_room_state_ids(
  263. self, destination: str, room_id: str, event_id: str
  264. ) -> Tuple[List[str], List[str]]:
  265. """Calls the /state_ids endpoint to fetch the state at a particular point
  266. in the room, and the auth events for the given event
  267. Returns:
  268. a tuple of (state event_ids, auth event_ids)
  269. """
  270. result = await self.transport_layer.get_room_state_ids(
  271. destination, room_id, event_id=event_id
  272. )
  273. state_event_ids = result["pdu_ids"]
  274. auth_event_ids = result.get("auth_chain_ids", [])
  275. if not isinstance(state_event_ids, list) or not isinstance(
  276. auth_event_ids, list
  277. ):
  278. raise Exception("invalid response from /state_ids")
  279. return state_event_ids, auth_event_ids
  280. async def _check_sigs_and_hash_and_fetch(
  281. self,
  282. origin: str,
  283. pdus: List[EventBase],
  284. room_version: RoomVersion,
  285. outlier: bool = False,
  286. include_none: bool = False,
  287. ) -> List[EventBase]:
  288. """Takes a list of PDUs and checks the signatures and hashs of each
  289. one. If a PDU fails its signature check then we check if we have it in
  290. the database and if not then request if from the originating server of
  291. that PDU.
  292. If a PDU fails its content hash check then it is redacted.
  293. The given list of PDUs are not modified, instead the function returns
  294. a new list.
  295. Args:
  296. origin
  297. pdu
  298. room_version
  299. outlier: Whether the events are outliers or not
  300. include_none: Whether to include None in the returned list
  301. for events that have failed their checks
  302. Returns:
  303. Deferred : A list of PDUs that have valid signatures and hashes.
  304. """
  305. deferreds = self._check_sigs_and_hashes(room_version, pdus)
  306. @defer.inlineCallbacks
  307. def handle_check_result(pdu: EventBase, deferred: Deferred):
  308. try:
  309. res = yield make_deferred_yieldable(deferred)
  310. except SynapseError:
  311. res = None
  312. if not res:
  313. # Check local db.
  314. res = yield self.store.get_event(
  315. pdu.event_id, allow_rejected=True, allow_none=True
  316. )
  317. if not res and pdu.origin != origin:
  318. try:
  319. res = yield defer.ensureDeferred(
  320. self.get_pdu(
  321. destinations=[pdu.origin],
  322. event_id=pdu.event_id,
  323. room_version=room_version,
  324. outlier=outlier,
  325. timeout=10000,
  326. )
  327. )
  328. except SynapseError:
  329. pass
  330. if not res:
  331. logger.warning(
  332. "Failed to find copy of %s with valid signature", pdu.event_id
  333. )
  334. return res
  335. handle = preserve_fn(handle_check_result)
  336. deferreds2 = [handle(pdu, deferred) for pdu, deferred in zip(pdus, deferreds)]
  337. valid_pdus = await make_deferred_yieldable(
  338. defer.gatherResults(deferreds2, consumeErrors=True)
  339. ).addErrback(unwrapFirstError)
  340. if include_none:
  341. return valid_pdus
  342. else:
  343. return [p for p in valid_pdus if p]
  344. async def get_event_auth(self, destination, room_id, event_id):
  345. res = await self.transport_layer.get_event_auth(destination, room_id, event_id)
  346. room_version = await self.store.get_room_version(room_id)
  347. auth_chain = [
  348. event_from_pdu_json(p, room_version, outlier=True)
  349. for p in res["auth_chain"]
  350. ]
  351. signed_auth = await self._check_sigs_and_hash_and_fetch(
  352. destination, auth_chain, outlier=True, room_version=room_version
  353. )
  354. signed_auth.sort(key=lambda e: e.depth)
  355. return signed_auth
  356. async def _try_destination_list(
  357. self,
  358. description: str,
  359. destinations: Iterable[str],
  360. callback: Callable[[str], Awaitable[T]],
  361. ) -> T:
  362. """Try an operation on a series of servers, until it succeeds
  363. Args:
  364. description: description of the operation we're doing, for logging
  365. destinations: list of server_names to try
  366. callback: Function to run for each server. Passed a single
  367. argument: the server_name to try.
  368. If the callback raises a CodeMessageException with a 300/400 code,
  369. attempts to perform the operation stop immediately and the exception is
  370. reraised.
  371. Otherwise, if the callback raises an Exception the error is logged and the
  372. next server tried. Normally the stacktrace is logged but this is
  373. suppressed if the exception is an InvalidResponseError.
  374. Returns:
  375. The result of callback, if it succeeds
  376. Raises:
  377. SynapseError if the chosen remote server returns a 300/400 code, or
  378. no servers were reachable.
  379. """
  380. for destination in destinations:
  381. if destination == self.server_name:
  382. continue
  383. try:
  384. res = await callback(destination)
  385. return res
  386. except InvalidResponseError as e:
  387. logger.warning("Failed to %s via %s: %s", description, destination, e)
  388. except UnsupportedRoomVersionError:
  389. raise
  390. except HttpResponseException as e:
  391. if not 500 <= e.code < 600:
  392. raise e.to_synapse_error()
  393. else:
  394. logger.warning(
  395. "Failed to %s via %s: %i %s",
  396. description,
  397. destination,
  398. e.code,
  399. e.args[0],
  400. )
  401. except Exception:
  402. logger.warning(
  403. "Failed to %s via %s", description, destination, exc_info=True
  404. )
  405. raise SynapseError(502, "Failed to %s via any server" % (description,))
  406. async def make_membership_event(
  407. self,
  408. destinations: Iterable[str],
  409. room_id: str,
  410. user_id: str,
  411. membership: str,
  412. content: dict,
  413. params: Dict[str, str],
  414. ) -> Tuple[str, EventBase, RoomVersion]:
  415. """
  416. Creates an m.room.member event, with context, without participating in the room.
  417. Does so by asking one of the already participating servers to create an
  418. event with proper context.
  419. Returns a fully signed and hashed event.
  420. Note that this does not append any events to any graphs.
  421. Args:
  422. destinations: Candidate homeservers which are probably
  423. participating in the room.
  424. room_id: The room in which the event will happen.
  425. user_id: The user whose membership is being evented.
  426. membership: The "membership" property of the event. Must be one of
  427. "join" or "leave".
  428. content: Any additional data to put into the content field of the
  429. event.
  430. params: Query parameters to include in the request.
  431. Returns:
  432. `(origin, event, room_version)` where origin is the remote
  433. homeserver which generated the event, and room_version is the
  434. version of the room.
  435. Raises:
  436. UnsupportedRoomVersionError: if remote responds with
  437. a room version we don't understand.
  438. SynapseError: if the chosen remote server returns a 300/400 code.
  439. RuntimeError: if no servers were reachable.
  440. """
  441. valid_memberships = {Membership.JOIN, Membership.LEAVE}
  442. if membership not in valid_memberships:
  443. raise RuntimeError(
  444. "make_membership_event called with membership='%s', must be one of %s"
  445. % (membership, ",".join(valid_memberships))
  446. )
  447. async def send_request(destination: str) -> Tuple[str, EventBase, RoomVersion]:
  448. ret = await self.transport_layer.make_membership_event(
  449. destination, room_id, user_id, membership, params
  450. )
  451. # Note: If not supplied, the room version may be either v1 or v2,
  452. # however either way the event format version will be v1.
  453. room_version_id = ret.get("room_version", RoomVersions.V1.identifier)
  454. room_version = KNOWN_ROOM_VERSIONS.get(room_version_id)
  455. if not room_version:
  456. raise UnsupportedRoomVersionError()
  457. pdu_dict = ret.get("event", None)
  458. if not isinstance(pdu_dict, dict):
  459. raise InvalidResponseError("Bad 'event' field in response")
  460. logger.debug("Got response to make_%s: %s", membership, pdu_dict)
  461. pdu_dict["content"].update(content)
  462. # The protoevent received over the JSON wire may not have all
  463. # the required fields. Lets just gloss over that because
  464. # there's some we never care about
  465. if "prev_state" not in pdu_dict:
  466. pdu_dict["prev_state"] = []
  467. ev = builder.create_local_event_from_event_dict(
  468. self._clock,
  469. self.hostname,
  470. self.signing_key,
  471. room_version=room_version,
  472. event_dict=pdu_dict,
  473. )
  474. return destination, ev, room_version
  475. return await self._try_destination_list(
  476. "make_" + membership, destinations, send_request
  477. )
  478. async def send_join(
  479. self, destinations: Iterable[str], pdu: EventBase, room_version: RoomVersion
  480. ) -> Dict[str, Any]:
  481. """Sends a join event to one of a list of homeservers.
  482. Doing so will cause the remote server to add the event to the graph,
  483. and send the event out to the rest of the federation.
  484. Args:
  485. destinations: Candidate homeservers which are probably
  486. participating in the room.
  487. pdu: event to be sent
  488. room_version: the version of the room (according to the server that
  489. did the make_join)
  490. Returns:
  491. a dict with members ``origin`` (a string
  492. giving the server the event was sent to, ``state`` (?) and
  493. ``auth_chain``.
  494. Raises:
  495. SynapseError: if the chosen remote server returns a 300/400 code.
  496. RuntimeError: if no servers were reachable.
  497. """
  498. async def send_request(destination) -> Dict[str, Any]:
  499. content = await self._do_send_join(destination, pdu)
  500. logger.debug("Got content: %s", content)
  501. state = [
  502. event_from_pdu_json(p, room_version, outlier=True)
  503. for p in content.get("state", [])
  504. ]
  505. auth_chain = [
  506. event_from_pdu_json(p, room_version, outlier=True)
  507. for p in content.get("auth_chain", [])
  508. ]
  509. pdus = {p.event_id: p for p in itertools.chain(state, auth_chain)}
  510. create_event = None
  511. for e in state:
  512. if (e.type, e.state_key) == (EventTypes.Create, ""):
  513. create_event = e
  514. break
  515. if create_event is None:
  516. # If the state doesn't have a create event then the room is
  517. # invalid, and it would fail auth checks anyway.
  518. raise SynapseError(400, "No create event in state")
  519. # the room version should be sane.
  520. create_room_version = create_event.content.get(
  521. "room_version", RoomVersions.V1.identifier
  522. )
  523. if create_room_version != room_version.identifier:
  524. # either the server that fulfilled the make_join, or the server that is
  525. # handling the send_join, is lying.
  526. raise InvalidResponseError(
  527. "Unexpected room version %s in create event"
  528. % (create_room_version,)
  529. )
  530. valid_pdus = await self._check_sigs_and_hash_and_fetch(
  531. destination,
  532. list(pdus.values()),
  533. outlier=True,
  534. room_version=room_version,
  535. )
  536. valid_pdus_map = {p.event_id: p for p in valid_pdus}
  537. # NB: We *need* to copy to ensure that we don't have multiple
  538. # references being passed on, as that causes... issues.
  539. signed_state = [
  540. copy.copy(valid_pdus_map[p.event_id])
  541. for p in state
  542. if p.event_id in valid_pdus_map
  543. ]
  544. signed_auth = [
  545. valid_pdus_map[p.event_id]
  546. for p in auth_chain
  547. if p.event_id in valid_pdus_map
  548. ]
  549. # NB: We *need* to copy to ensure that we don't have multiple
  550. # references being passed on, as that causes... issues.
  551. for s in signed_state:
  552. s.internal_metadata = copy.deepcopy(s.internal_metadata)
  553. # double-check that the same create event has ended up in the auth chain
  554. auth_chain_create_events = [
  555. e.event_id
  556. for e in signed_auth
  557. if (e.type, e.state_key) == (EventTypes.Create, "")
  558. ]
  559. if auth_chain_create_events != [create_event.event_id]:
  560. raise InvalidResponseError(
  561. "Unexpected create event(s) in auth chain: %s"
  562. % (auth_chain_create_events,)
  563. )
  564. return {
  565. "state": signed_state,
  566. "auth_chain": signed_auth,
  567. "origin": destination,
  568. }
  569. return await self._try_destination_list("send_join", destinations, send_request)
  570. async def _do_send_join(self, destination: str, pdu: EventBase):
  571. time_now = self._clock.time_msec()
  572. try:
  573. content = await self.transport_layer.send_join_v2(
  574. destination=destination,
  575. room_id=pdu.room_id,
  576. event_id=pdu.event_id,
  577. content=pdu.get_pdu_json(time_now),
  578. )
  579. return content
  580. except HttpResponseException as e:
  581. if e.code in [400, 404]:
  582. err = e.to_synapse_error()
  583. # If we receive an error response that isn't a generic error, or an
  584. # unrecognised endpoint error, we assume that the remote understands
  585. # the v2 invite API and this is a legitimate error.
  586. if err.errcode not in [Codes.UNKNOWN, Codes.UNRECOGNIZED]:
  587. raise err
  588. else:
  589. raise e.to_synapse_error()
  590. logger.debug("Couldn't send_join with the v2 API, falling back to the v1 API")
  591. resp = await self.transport_layer.send_join_v1(
  592. destination=destination,
  593. room_id=pdu.room_id,
  594. event_id=pdu.event_id,
  595. content=pdu.get_pdu_json(time_now),
  596. )
  597. # We expect the v1 API to respond with [200, content], so we only return the
  598. # content.
  599. return resp[1]
  600. async def send_invite(
  601. self, destination: str, room_id: str, event_id: str, pdu: EventBase,
  602. ) -> EventBase:
  603. room_version = await self.store.get_room_version(room_id)
  604. content = await self._do_send_invite(destination, pdu, room_version)
  605. pdu_dict = content["event"]
  606. logger.debug("Got response to send_invite: %s", pdu_dict)
  607. pdu = event_from_pdu_json(pdu_dict, room_version)
  608. # Check signatures are correct.
  609. pdu = await self._check_sigs_and_hash(room_version, pdu)
  610. # FIXME: We should handle signature failures more gracefully.
  611. return pdu
  612. async def _do_send_invite(
  613. self, destination: str, pdu: EventBase, room_version: RoomVersion
  614. ) -> JsonDict:
  615. """Actually sends the invite, first trying v2 API and falling back to
  616. v1 API if necessary.
  617. Returns:
  618. The event as a dict as returned by the remote server
  619. """
  620. time_now = self._clock.time_msec()
  621. try:
  622. content = await self.transport_layer.send_invite_v2(
  623. destination=destination,
  624. room_id=pdu.room_id,
  625. event_id=pdu.event_id,
  626. content={
  627. "event": pdu.get_pdu_json(time_now),
  628. "room_version": room_version.identifier,
  629. "invite_room_state": pdu.unsigned.get("invite_room_state", []),
  630. },
  631. )
  632. return content
  633. except HttpResponseException as e:
  634. if e.code in [400, 404]:
  635. err = e.to_synapse_error()
  636. # If we receive an error response that isn't a generic error, we
  637. # assume that the remote understands the v2 invite API and this
  638. # is a legitimate error.
  639. if err.errcode != Codes.UNKNOWN:
  640. raise err
  641. # Otherwise, we assume that the remote server doesn't understand
  642. # the v2 invite API. That's ok provided the room uses old-style event
  643. # IDs.
  644. if room_version.event_format != EventFormatVersions.V1:
  645. raise SynapseError(
  646. 400,
  647. "User's homeserver does not support this room version",
  648. Codes.UNSUPPORTED_ROOM_VERSION,
  649. )
  650. elif e.code == 403:
  651. raise e.to_synapse_error()
  652. else:
  653. raise
  654. # Didn't work, try v1 API.
  655. # Note the v1 API returns a tuple of `(200, content)`
  656. _, content = await self.transport_layer.send_invite_v1(
  657. destination=destination,
  658. room_id=pdu.room_id,
  659. event_id=pdu.event_id,
  660. content=pdu.get_pdu_json(time_now),
  661. )
  662. return content
  663. async def send_leave(self, destinations: Iterable[str], pdu: EventBase) -> None:
  664. """Sends a leave event to one of a list of homeservers.
  665. Doing so will cause the remote server to add the event to the graph,
  666. and send the event out to the rest of the federation.
  667. This is mostly useful to reject received invites.
  668. Args:
  669. destinations: Candidate homeservers which are probably
  670. participating in the room.
  671. pdu: event to be sent
  672. Raises:
  673. SynapseError if the chosen remote server returns a 300/400 code.
  674. RuntimeError if no servers were reachable.
  675. """
  676. async def send_request(destination: str) -> None:
  677. content = await self._do_send_leave(destination, pdu)
  678. logger.debug("Got content: %s", content)
  679. return await self._try_destination_list(
  680. "send_leave", destinations, send_request
  681. )
  682. async def _do_send_leave(self, destination, pdu):
  683. time_now = self._clock.time_msec()
  684. try:
  685. content = await self.transport_layer.send_leave_v2(
  686. destination=destination,
  687. room_id=pdu.room_id,
  688. event_id=pdu.event_id,
  689. content=pdu.get_pdu_json(time_now),
  690. )
  691. return content
  692. except HttpResponseException as e:
  693. if e.code in [400, 404]:
  694. err = e.to_synapse_error()
  695. # If we receive an error response that isn't a generic error, or an
  696. # unrecognised endpoint error, we assume that the remote understands
  697. # the v2 invite API and this is a legitimate error.
  698. if err.errcode not in [Codes.UNKNOWN, Codes.UNRECOGNIZED]:
  699. raise err
  700. else:
  701. raise e.to_synapse_error()
  702. logger.debug("Couldn't send_leave with the v2 API, falling back to the v1 API")
  703. resp = await self.transport_layer.send_leave_v1(
  704. destination=destination,
  705. room_id=pdu.room_id,
  706. event_id=pdu.event_id,
  707. content=pdu.get_pdu_json(time_now),
  708. )
  709. # We expect the v1 API to respond with [200, content], so we only return the
  710. # content.
  711. return resp[1]
  712. def get_public_rooms(
  713. self,
  714. remote_server: str,
  715. limit: Optional[int] = None,
  716. since_token: Optional[str] = None,
  717. search_filter: Optional[Dict] = None,
  718. include_all_networks: bool = False,
  719. third_party_instance_id: Optional[str] = None,
  720. ):
  721. """Get the list of public rooms from a remote homeserver
  722. Args:
  723. remote_server: The name of the remote server
  724. limit: Maximum amount of rooms to return
  725. since_token: Used for result pagination
  726. search_filter: A filter dictionary to send the remote homeserver
  727. and filter the result set
  728. include_all_networks: Whether to include results from all third party instances
  729. third_party_instance_id: Whether to only include results from a specific third
  730. party instance
  731. Returns:
  732. Deferred[Dict[str, Any]]: The response from the remote server, or None if
  733. `remote_server` is the same as the local server_name
  734. Raises:
  735. HttpResponseException: There was an exception returned from the remote server
  736. SynapseException: M_FORBIDDEN when the remote server has disallowed publicRoom
  737. requests over federation
  738. """
  739. return self.transport_layer.get_public_rooms(
  740. remote_server,
  741. limit,
  742. since_token,
  743. search_filter,
  744. include_all_networks=include_all_networks,
  745. third_party_instance_id=third_party_instance_id,
  746. )
  747. async def get_missing_events(
  748. self,
  749. destination: str,
  750. room_id: str,
  751. earliest_events_ids: Sequence[str],
  752. latest_events: Iterable[EventBase],
  753. limit: int,
  754. min_depth: int,
  755. timeout: int,
  756. ) -> List[EventBase]:
  757. """Tries to fetch events we are missing. This is called when we receive
  758. an event without having received all of its ancestors.
  759. Args:
  760. destination
  761. room_id
  762. earliest_events_ids: List of event ids. Effectively the
  763. events we expected to receive, but haven't. `get_missing_events`
  764. should only return events that didn't happen before these.
  765. latest_events: List of events we have received that we don't
  766. have all previous events for.
  767. limit: Maximum number of events to return.
  768. min_depth: Minimum depth of events to return.
  769. timeout: Max time to wait in ms
  770. """
  771. try:
  772. content = await self.transport_layer.get_missing_events(
  773. destination=destination,
  774. room_id=room_id,
  775. earliest_events=earliest_events_ids,
  776. latest_events=[e.event_id for e in latest_events],
  777. limit=limit,
  778. min_depth=min_depth,
  779. timeout=timeout,
  780. )
  781. room_version = await self.store.get_room_version(room_id)
  782. events = [
  783. event_from_pdu_json(e, room_version) for e in content.get("events", [])
  784. ]
  785. signed_events = await self._check_sigs_and_hash_and_fetch(
  786. destination, events, outlier=False, room_version=room_version
  787. )
  788. except HttpResponseException as e:
  789. if not e.code == 400:
  790. raise
  791. # We are probably hitting an old server that doesn't support
  792. # get_missing_events
  793. signed_events = []
  794. return signed_events
  795. async def forward_third_party_invite(self, destinations, room_id, event_dict):
  796. for destination in destinations:
  797. if destination == self.server_name:
  798. continue
  799. try:
  800. await self.transport_layer.exchange_third_party_invite(
  801. destination=destination, room_id=room_id, event_dict=event_dict
  802. )
  803. return None
  804. except CodeMessageException:
  805. raise
  806. except Exception as e:
  807. logger.exception(
  808. "Failed to send_third_party_invite via %s: %s", destination, str(e)
  809. )
  810. raise RuntimeError("Failed to send to any server.")
  811. @defer.inlineCallbacks
  812. def get_room_complexity(self, destination, room_id):
  813. """
  814. Fetch the complexity of a remote room from another server.
  815. Args:
  816. destination (str): The remote server
  817. room_id (str): The room ID to ask about.
  818. Returns:
  819. Deferred[dict] or Deferred[None]: Dict contains the complexity
  820. metric versions, while None means we could not fetch the complexity.
  821. """
  822. try:
  823. complexity = yield self.transport_layer.get_room_complexity(
  824. destination=destination, room_id=room_id
  825. )
  826. defer.returnValue(complexity)
  827. except CodeMessageException as e:
  828. # We didn't manage to get it -- probably a 404. We are okay if other
  829. # servers don't give it to us.
  830. logger.debug(
  831. "Failed to fetch room complexity via %s for %s, got a %d",
  832. destination,
  833. room_id,
  834. e.code,
  835. )
  836. except Exception:
  837. logger.exception(
  838. "Failed to fetch room complexity via %s for %s", destination, room_id
  839. )
  840. # If we don't manage to find it, return None. It's not an error if a
  841. # server doesn't give it to us.
  842. defer.returnValue(None)