server.py 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2014-2016 OpenMarket Ltd
  3. # Copyright 2017-2018 New Vector Ltd
  4. # Copyright 2019 The Matrix.org Foundation C.I.C.
  5. #
  6. # Licensed under the Apache License, Version 2.0 (the "License");
  7. # you may not use this file except in compliance with the License.
  8. # You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing, software
  13. # distributed under the License is distributed on an "AS IS" BASIS,
  14. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. # See the License for the specific language governing permissions and
  16. # limitations under the License.
  17. # This file provides some classes for setting up (partially-populated)
  18. # homeservers; either as a full homeserver as a real application, or a small
  19. # partial one for unit test mocking.
  20. # Imports required for the default HomeServer() implementation
  21. import abc
  22. import functools
  23. import logging
  24. import os
  25. from typing import TYPE_CHECKING, Any, Callable, Dict, List, Optional, TypeVar, cast
  26. import twisted
  27. from twisted.mail.smtp import sendmail
  28. from twisted.web.iweb import IPolicyForHTTPS
  29. from synapse.api.auth import Auth
  30. from synapse.api.filtering import Filtering
  31. from synapse.api.ratelimiting import Ratelimiter
  32. from synapse.appservice.api import ApplicationServiceApi
  33. from synapse.appservice.scheduler import ApplicationServiceScheduler
  34. from synapse.config.homeserver import HomeServerConfig
  35. from synapse.crypto import context_factory
  36. from synapse.crypto.context_factory import RegularPolicyForHTTPS
  37. from synapse.crypto.keyring import Keyring
  38. from synapse.events.builder import EventBuilderFactory
  39. from synapse.events.spamcheck import SpamChecker
  40. from synapse.events.third_party_rules import ThirdPartyEventRules
  41. from synapse.events.utils import EventClientSerializer
  42. from synapse.federation.federation_client import FederationClient
  43. from synapse.federation.federation_server import (
  44. FederationHandlerRegistry,
  45. FederationServer,
  46. )
  47. from synapse.federation.send_queue import FederationRemoteSendQueue
  48. from synapse.federation.sender import FederationSender
  49. from synapse.federation.transport.client import TransportLayerClient
  50. from synapse.groups.attestations import GroupAttestationSigning, GroupAttestionRenewer
  51. from synapse.groups.groups_server import GroupsServerHandler, GroupsServerWorkerHandler
  52. from synapse.handlers.account_validity import AccountValidityHandler
  53. from synapse.handlers.acme import AcmeHandler
  54. from synapse.handlers.admin import AdminHandler
  55. from synapse.handlers.appservice import ApplicationServicesHandler
  56. from synapse.handlers.auth import AuthHandler, MacaroonGenerator
  57. from synapse.handlers.cas_handler import CasHandler
  58. from synapse.handlers.deactivate_account import DeactivateAccountHandler
  59. from synapse.handlers.device import DeviceHandler, DeviceWorkerHandler
  60. from synapse.handlers.devicemessage import DeviceMessageHandler
  61. from synapse.handlers.directory import DirectoryHandler
  62. from synapse.handlers.e2e_keys import E2eKeysHandler
  63. from synapse.handlers.e2e_room_keys import E2eRoomKeysHandler
  64. from synapse.handlers.events import EventHandler, EventStreamHandler
  65. from synapse.handlers.federation import FederationHandler
  66. from synapse.handlers.groups_local import GroupsLocalHandler, GroupsLocalWorkerHandler
  67. from synapse.handlers.identity import IdentityHandler
  68. from synapse.handlers.initial_sync import InitialSyncHandler
  69. from synapse.handlers.message import EventCreationHandler, MessageHandler
  70. from synapse.handlers.pagination import PaginationHandler
  71. from synapse.handlers.password_policy import PasswordPolicyHandler
  72. from synapse.handlers.presence import PresenceHandler
  73. from synapse.handlers.profile import ProfileHandler
  74. from synapse.handlers.read_marker import ReadMarkerHandler
  75. from synapse.handlers.receipts import ReceiptsHandler
  76. from synapse.handlers.register import RegistrationHandler
  77. from synapse.handlers.room import (
  78. RoomContextHandler,
  79. RoomCreationHandler,
  80. RoomShutdownHandler,
  81. )
  82. from synapse.handlers.room_list import RoomListHandler
  83. from synapse.handlers.room_member import RoomMemberMasterHandler
  84. from synapse.handlers.room_member_worker import RoomMemberWorkerHandler
  85. from synapse.handlers.search import SearchHandler
  86. from synapse.handlers.set_password import SetPasswordHandler
  87. from synapse.handlers.sso import SsoHandler
  88. from synapse.handlers.stats import StatsHandler
  89. from synapse.handlers.sync import SyncHandler
  90. from synapse.handlers.typing import FollowerTypingHandler, TypingWriterHandler
  91. from synapse.handlers.user_directory import UserDirectoryHandler
  92. from synapse.http.client import InsecureInterceptableContextFactory, SimpleHttpClient
  93. from synapse.http.matrixfederationclient import MatrixFederationHttpClient
  94. from synapse.module_api import ModuleApi
  95. from synapse.notifier import Notifier
  96. from synapse.push.action_generator import ActionGenerator
  97. from synapse.push.pusherpool import PusherPool
  98. from synapse.replication.tcp.client import ReplicationDataHandler
  99. from synapse.replication.tcp.handler import ReplicationCommandHandler
  100. from synapse.replication.tcp.resource import ReplicationStreamer
  101. from synapse.replication.tcp.streams import STREAMS_MAP, Stream
  102. from synapse.rest.media.v1.media_repository import (
  103. MediaRepository,
  104. MediaRepositoryResource,
  105. )
  106. from synapse.secrets import Secrets
  107. from synapse.server_notices.server_notices_manager import ServerNoticesManager
  108. from synapse.server_notices.server_notices_sender import ServerNoticesSender
  109. from synapse.server_notices.worker_server_notices_sender import (
  110. WorkerServerNoticesSender,
  111. )
  112. from synapse.state import StateHandler, StateResolutionHandler
  113. from synapse.storage import Databases, DataStore, Storage
  114. from synapse.streams.events import EventSources
  115. from synapse.types import DomainSpecificString
  116. from synapse.util import Clock
  117. from synapse.util.distributor import Distributor
  118. from synapse.util.ratelimitutils import FederationRateLimiter
  119. from synapse.util.stringutils import random_string
  120. logger = logging.getLogger(__name__)
  121. if TYPE_CHECKING:
  122. from synapse.handlers.oidc_handler import OidcHandler
  123. from synapse.handlers.saml_handler import SamlHandler
  124. T = TypeVar("T", bound=Callable[..., Any])
  125. def cache_in_self(builder: T) -> T:
  126. """Wraps a function called e.g. `get_foo`, checking if `self.foo` exists and
  127. returning if so. If not, calls the given function and sets `self.foo` to it.
  128. Also ensures that dependency cycles throw an exception correctly, rather
  129. than overflowing the stack.
  130. """
  131. if not builder.__name__.startswith("get_"):
  132. raise Exception(
  133. "@cache_in_self can only be used on functions starting with `get_`"
  134. )
  135. depname = builder.__name__[len("get_") :]
  136. building = [False]
  137. @functools.wraps(builder)
  138. def _get(self):
  139. try:
  140. return getattr(self, depname)
  141. except AttributeError:
  142. pass
  143. # Prevent cyclic dependencies from deadlocking
  144. if building[0]:
  145. raise ValueError("Cyclic dependency while building %s" % (depname,))
  146. building[0] = True
  147. try:
  148. dep = builder(self)
  149. setattr(self, depname, dep)
  150. finally:
  151. building[0] = False
  152. return dep
  153. # We cast here as we need to tell mypy that `_get` has the same signature as
  154. # `builder`.
  155. return cast(T, _get)
  156. class HomeServer(metaclass=abc.ABCMeta):
  157. """A basic homeserver object without lazy component builders.
  158. This will need all of the components it requires to either be passed as
  159. constructor arguments, or the relevant methods overriding to create them.
  160. Typically this would only be used for unit tests.
  161. Dependencies should be added by creating a `def get_<depname>(self)`
  162. function, wrapping it in `@cache_in_self`.
  163. Attributes:
  164. config (synapse.config.homeserver.HomeserverConfig):
  165. _listening_services (list[twisted.internet.tcp.Port]): TCP ports that
  166. we are listening on to provide HTTP services.
  167. """
  168. REQUIRED_ON_BACKGROUND_TASK_STARTUP = [
  169. "account_validity",
  170. "auth",
  171. "deactivate_account",
  172. "message",
  173. "pagination",
  174. "profile",
  175. "stats",
  176. ]
  177. # This is overridden in derived application classes
  178. # (such as synapse.app.homeserver.SynapseHomeServer) and gives the class to be
  179. # instantiated during setup() for future return by get_datastore()
  180. DATASTORE_CLASS = abc.abstractproperty()
  181. def __init__(
  182. self,
  183. hostname: str,
  184. config: HomeServerConfig,
  185. reactor=None,
  186. version_string="Synapse",
  187. ):
  188. """
  189. Args:
  190. hostname : The hostname for the server.
  191. config: The full config for the homeserver.
  192. """
  193. if not reactor:
  194. from twisted.internet import reactor as _reactor
  195. reactor = _reactor
  196. self._reactor = reactor
  197. self.hostname = hostname
  198. # the key we use to sign events and requests
  199. self.signing_key = config.key.signing_key[0]
  200. self.config = config
  201. self._listening_services = [] # type: List[twisted.internet.tcp.Port]
  202. self.start_time = None # type: Optional[int]
  203. self._instance_id = random_string(5)
  204. self._instance_name = config.worker_name or "master"
  205. self.clock = Clock(reactor)
  206. self.distributor = Distributor()
  207. self.registration_ratelimiter = Ratelimiter(
  208. clock=self.clock,
  209. rate_hz=config.rc_registration.per_second,
  210. burst_count=config.rc_registration.burst_count,
  211. )
  212. self.version_string = version_string
  213. self.datastores = None # type: Optional[Databases]
  214. def get_instance_id(self) -> str:
  215. """A unique ID for this synapse process instance.
  216. This is used to distinguish running instances in worker-based
  217. deployments.
  218. """
  219. return self._instance_id
  220. def get_instance_name(self) -> str:
  221. """A unique name for this synapse process.
  222. Used to identify the process over replication and in config. Does not
  223. change over restarts.
  224. """
  225. return self._instance_name
  226. def setup(self) -> None:
  227. logger.info("Setting up.")
  228. self.start_time = int(self.get_clock().time())
  229. self.datastores = Databases(self.DATASTORE_CLASS, self)
  230. logger.info("Finished setting up.")
  231. # Register background tasks required by this server. This must be done
  232. # somewhat manually due to the background tasks not being registered
  233. # unless handlers are instantiated.
  234. if self.config.run_background_tasks:
  235. self.setup_background_tasks()
  236. def setup_background_tasks(self) -> None:
  237. """
  238. Some handlers have side effects on instantiation (like registering
  239. background updates). This function causes them to be fetched, and
  240. therefore instantiated, to run those side effects.
  241. """
  242. for i in self.REQUIRED_ON_BACKGROUND_TASK_STARTUP:
  243. getattr(self, "get_" + i + "_handler")()
  244. def get_reactor(self) -> twisted.internet.base.ReactorBase:
  245. """
  246. Fetch the Twisted reactor in use by this HomeServer.
  247. """
  248. return self._reactor
  249. def get_ip_from_request(self, request) -> str:
  250. # X-Forwarded-For is handled by our custom request type.
  251. return request.getClientIP()
  252. def is_mine(self, domain_specific_string: DomainSpecificString) -> bool:
  253. return domain_specific_string.domain == self.hostname
  254. def is_mine_id(self, string: str) -> bool:
  255. return string.split(":", 1)[1] == self.hostname
  256. def get_clock(self) -> Clock:
  257. return self.clock
  258. def get_datastore(self) -> DataStore:
  259. if not self.datastores:
  260. raise Exception("HomeServer.setup must be called before getting datastores")
  261. return self.datastores.main
  262. def get_datastores(self) -> Databases:
  263. if not self.datastores:
  264. raise Exception("HomeServer.setup must be called before getting datastores")
  265. return self.datastores
  266. def get_config(self) -> HomeServerConfig:
  267. return self.config
  268. def get_distributor(self) -> Distributor:
  269. return self.distributor
  270. def get_registration_ratelimiter(self) -> Ratelimiter:
  271. return self.registration_ratelimiter
  272. @cache_in_self
  273. def get_federation_client(self) -> FederationClient:
  274. return FederationClient(self)
  275. @cache_in_self
  276. def get_federation_server(self) -> FederationServer:
  277. return FederationServer(self)
  278. @cache_in_self
  279. def get_notifier(self) -> Notifier:
  280. return Notifier(self)
  281. @cache_in_self
  282. def get_auth(self) -> Auth:
  283. return Auth(self)
  284. @cache_in_self
  285. def get_http_client_context_factory(self) -> IPolicyForHTTPS:
  286. return (
  287. InsecureInterceptableContextFactory()
  288. if self.config.use_insecure_ssl_client_just_for_testing_do_not_use
  289. else RegularPolicyForHTTPS()
  290. )
  291. @cache_in_self
  292. def get_simple_http_client(self) -> SimpleHttpClient:
  293. return SimpleHttpClient(self)
  294. @cache_in_self
  295. def get_proxied_http_client(self) -> SimpleHttpClient:
  296. return SimpleHttpClient(
  297. self,
  298. http_proxy=os.getenvb(b"http_proxy"),
  299. https_proxy=os.getenvb(b"HTTPS_PROXY"),
  300. )
  301. @cache_in_self
  302. def get_room_creation_handler(self) -> RoomCreationHandler:
  303. return RoomCreationHandler(self)
  304. @cache_in_self
  305. def get_room_shutdown_handler(self) -> RoomShutdownHandler:
  306. return RoomShutdownHandler(self)
  307. @cache_in_self
  308. def get_sendmail(self) -> sendmail:
  309. return sendmail
  310. @cache_in_self
  311. def get_state_handler(self) -> StateHandler:
  312. return StateHandler(self)
  313. @cache_in_self
  314. def get_state_resolution_handler(self) -> StateResolutionHandler:
  315. return StateResolutionHandler(self)
  316. @cache_in_self
  317. def get_presence_handler(self) -> PresenceHandler:
  318. return PresenceHandler(self)
  319. @cache_in_self
  320. def get_typing_handler(self):
  321. if self.config.worker.writers.typing == self.get_instance_name():
  322. return TypingWriterHandler(self)
  323. else:
  324. return FollowerTypingHandler(self)
  325. @cache_in_self
  326. def get_sso_handler(self) -> SsoHandler:
  327. return SsoHandler(self)
  328. @cache_in_self
  329. def get_sync_handler(self) -> SyncHandler:
  330. return SyncHandler(self)
  331. @cache_in_self
  332. def get_room_list_handler(self) -> RoomListHandler:
  333. return RoomListHandler(self)
  334. @cache_in_self
  335. def get_auth_handler(self) -> AuthHandler:
  336. return AuthHandler(self)
  337. @cache_in_self
  338. def get_macaroon_generator(self) -> MacaroonGenerator:
  339. return MacaroonGenerator(self)
  340. @cache_in_self
  341. def get_device_handler(self):
  342. if self.config.worker_app:
  343. return DeviceWorkerHandler(self)
  344. else:
  345. return DeviceHandler(self)
  346. @cache_in_self
  347. def get_device_message_handler(self) -> DeviceMessageHandler:
  348. return DeviceMessageHandler(self)
  349. @cache_in_self
  350. def get_directory_handler(self) -> DirectoryHandler:
  351. return DirectoryHandler(self)
  352. @cache_in_self
  353. def get_e2e_keys_handler(self) -> E2eKeysHandler:
  354. return E2eKeysHandler(self)
  355. @cache_in_self
  356. def get_e2e_room_keys_handler(self) -> E2eRoomKeysHandler:
  357. return E2eRoomKeysHandler(self)
  358. @cache_in_self
  359. def get_acme_handler(self) -> AcmeHandler:
  360. return AcmeHandler(self)
  361. @cache_in_self
  362. def get_admin_handler(self) -> AdminHandler:
  363. return AdminHandler(self)
  364. @cache_in_self
  365. def get_application_service_api(self) -> ApplicationServiceApi:
  366. return ApplicationServiceApi(self)
  367. @cache_in_self
  368. def get_application_service_scheduler(self) -> ApplicationServiceScheduler:
  369. return ApplicationServiceScheduler(self)
  370. @cache_in_self
  371. def get_application_service_handler(self) -> ApplicationServicesHandler:
  372. return ApplicationServicesHandler(self)
  373. @cache_in_self
  374. def get_event_handler(self) -> EventHandler:
  375. return EventHandler(self)
  376. @cache_in_self
  377. def get_event_stream_handler(self) -> EventStreamHandler:
  378. return EventStreamHandler(self)
  379. @cache_in_self
  380. def get_federation_handler(self) -> FederationHandler:
  381. return FederationHandler(self)
  382. @cache_in_self
  383. def get_identity_handler(self) -> IdentityHandler:
  384. return IdentityHandler(self)
  385. @cache_in_self
  386. def get_initial_sync_handler(self) -> InitialSyncHandler:
  387. return InitialSyncHandler(self)
  388. @cache_in_self
  389. def get_profile_handler(self):
  390. return ProfileHandler(self)
  391. @cache_in_self
  392. def get_event_creation_handler(self) -> EventCreationHandler:
  393. return EventCreationHandler(self)
  394. @cache_in_self
  395. def get_deactivate_account_handler(self) -> DeactivateAccountHandler:
  396. return DeactivateAccountHandler(self)
  397. @cache_in_self
  398. def get_search_handler(self) -> SearchHandler:
  399. return SearchHandler(self)
  400. @cache_in_self
  401. def get_set_password_handler(self) -> SetPasswordHandler:
  402. return SetPasswordHandler(self)
  403. @cache_in_self
  404. def get_event_sources(self) -> EventSources:
  405. return EventSources(self)
  406. @cache_in_self
  407. def get_keyring(self) -> Keyring:
  408. return Keyring(self)
  409. @cache_in_self
  410. def get_event_builder_factory(self) -> EventBuilderFactory:
  411. return EventBuilderFactory(self)
  412. @cache_in_self
  413. def get_filtering(self) -> Filtering:
  414. return Filtering(self)
  415. @cache_in_self
  416. def get_pusherpool(self) -> PusherPool:
  417. return PusherPool(self)
  418. @cache_in_self
  419. def get_http_client(self) -> MatrixFederationHttpClient:
  420. tls_client_options_factory = context_factory.FederationPolicyForHTTPS(
  421. self.config
  422. )
  423. return MatrixFederationHttpClient(self, tls_client_options_factory)
  424. @cache_in_self
  425. def get_media_repository_resource(self) -> MediaRepositoryResource:
  426. # build the media repo resource. This indirects through the HomeServer
  427. # to ensure that we only have a single instance of
  428. return MediaRepositoryResource(self)
  429. @cache_in_self
  430. def get_media_repository(self) -> MediaRepository:
  431. return MediaRepository(self)
  432. @cache_in_self
  433. def get_federation_transport_client(self) -> TransportLayerClient:
  434. return TransportLayerClient(self)
  435. @cache_in_self
  436. def get_federation_sender(self):
  437. if self.should_send_federation():
  438. return FederationSender(self)
  439. elif not self.config.worker_app:
  440. return FederationRemoteSendQueue(self)
  441. else:
  442. raise Exception("Workers cannot send federation traffic")
  443. @cache_in_self
  444. def get_receipts_handler(self) -> ReceiptsHandler:
  445. return ReceiptsHandler(self)
  446. @cache_in_self
  447. def get_read_marker_handler(self) -> ReadMarkerHandler:
  448. return ReadMarkerHandler(self)
  449. @cache_in_self
  450. def get_tcp_replication(self) -> ReplicationCommandHandler:
  451. return ReplicationCommandHandler(self)
  452. @cache_in_self
  453. def get_action_generator(self) -> ActionGenerator:
  454. return ActionGenerator(self)
  455. @cache_in_self
  456. def get_user_directory_handler(self) -> UserDirectoryHandler:
  457. return UserDirectoryHandler(self)
  458. @cache_in_self
  459. def get_groups_local_handler(self):
  460. if self.config.worker_app:
  461. return GroupsLocalWorkerHandler(self)
  462. else:
  463. return GroupsLocalHandler(self)
  464. @cache_in_self
  465. def get_groups_server_handler(self):
  466. if self.config.worker_app:
  467. return GroupsServerWorkerHandler(self)
  468. else:
  469. return GroupsServerHandler(self)
  470. @cache_in_self
  471. def get_groups_attestation_signing(self) -> GroupAttestationSigning:
  472. return GroupAttestationSigning(self)
  473. @cache_in_self
  474. def get_groups_attestation_renewer(self) -> GroupAttestionRenewer:
  475. return GroupAttestionRenewer(self)
  476. @cache_in_self
  477. def get_secrets(self) -> Secrets:
  478. return Secrets()
  479. @cache_in_self
  480. def get_stats_handler(self) -> StatsHandler:
  481. return StatsHandler(self)
  482. @cache_in_self
  483. def get_spam_checker(self):
  484. return SpamChecker(self)
  485. @cache_in_self
  486. def get_third_party_event_rules(self) -> ThirdPartyEventRules:
  487. return ThirdPartyEventRules(self)
  488. @cache_in_self
  489. def get_room_member_handler(self):
  490. if self.config.worker_app:
  491. return RoomMemberWorkerHandler(self)
  492. return RoomMemberMasterHandler(self)
  493. @cache_in_self
  494. def get_federation_registry(self) -> FederationHandlerRegistry:
  495. return FederationHandlerRegistry(self)
  496. @cache_in_self
  497. def get_server_notices_manager(self):
  498. if self.config.worker_app:
  499. raise Exception("Workers cannot send server notices")
  500. return ServerNoticesManager(self)
  501. @cache_in_self
  502. def get_server_notices_sender(self):
  503. if self.config.worker_app:
  504. return WorkerServerNoticesSender(self)
  505. return ServerNoticesSender(self)
  506. @cache_in_self
  507. def get_message_handler(self) -> MessageHandler:
  508. return MessageHandler(self)
  509. @cache_in_self
  510. def get_pagination_handler(self) -> PaginationHandler:
  511. return PaginationHandler(self)
  512. @cache_in_self
  513. def get_room_context_handler(self) -> RoomContextHandler:
  514. return RoomContextHandler(self)
  515. @cache_in_self
  516. def get_registration_handler(self) -> RegistrationHandler:
  517. return RegistrationHandler(self)
  518. @cache_in_self
  519. def get_account_validity_handler(self) -> AccountValidityHandler:
  520. return AccountValidityHandler(self)
  521. @cache_in_self
  522. def get_cas_handler(self) -> CasHandler:
  523. return CasHandler(self)
  524. @cache_in_self
  525. def get_saml_handler(self) -> "SamlHandler":
  526. from synapse.handlers.saml_handler import SamlHandler
  527. return SamlHandler(self)
  528. @cache_in_self
  529. def get_oidc_handler(self) -> "OidcHandler":
  530. from synapse.handlers.oidc_handler import OidcHandler
  531. return OidcHandler(self)
  532. @cache_in_self
  533. def get_event_client_serializer(self) -> EventClientSerializer:
  534. return EventClientSerializer(self)
  535. @cache_in_self
  536. def get_password_policy_handler(self) -> PasswordPolicyHandler:
  537. return PasswordPolicyHandler(self)
  538. @cache_in_self
  539. def get_storage(self) -> Storage:
  540. return Storage(self, self.get_datastores())
  541. @cache_in_self
  542. def get_replication_streamer(self) -> ReplicationStreamer:
  543. return ReplicationStreamer(self)
  544. @cache_in_self
  545. def get_replication_data_handler(self) -> ReplicationDataHandler:
  546. return ReplicationDataHandler(self)
  547. @cache_in_self
  548. def get_replication_streams(self) -> Dict[str, Stream]:
  549. return {stream.NAME: stream(self) for stream in STREAMS_MAP.values()}
  550. @cache_in_self
  551. def get_federation_ratelimiter(self) -> FederationRateLimiter:
  552. return FederationRateLimiter(self.clock, config=self.config.rc_federation)
  553. @cache_in_self
  554. def get_module_api(self) -> ModuleApi:
  555. return ModuleApi(self, self.get_auth_handler())
  556. async def remove_pusher(self, app_id: str, push_key: str, user_id: str):
  557. return await self.get_pusherpool().remove_pusher(app_id, push_key, user_id)
  558. def should_send_federation(self) -> bool:
  559. "Should this server be sending federation traffic directly?"
  560. return self.config.send_federation and (
  561. not self.config.worker_app
  562. or self.config.worker_app == "synapse.app.federation_sender"
  563. )