client_reader.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Copyright 2016 OpenMarket Ltd
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. import logging
  17. import sys
  18. from twisted.internet import reactor
  19. from twisted.web.resource import NoResource
  20. import synapse
  21. from synapse import events
  22. from synapse.app import _base
  23. from synapse.config._base import ConfigError
  24. from synapse.config.homeserver import HomeServerConfig
  25. from synapse.config.logger import setup_logging
  26. from synapse.http.server import JsonResource
  27. from synapse.http.site import SynapseSite
  28. from synapse.logging.context import LoggingContext
  29. from synapse.metrics import METRICS_PREFIX, MetricsResource, RegistryProxy
  30. from synapse.replication.slave.storage._base import BaseSlavedStore
  31. from synapse.replication.slave.storage.account_data import SlavedAccountDataStore
  32. from synapse.replication.slave.storage.appservice import SlavedApplicationServiceStore
  33. from synapse.replication.slave.storage.client_ips import SlavedClientIpStore
  34. from synapse.replication.slave.storage.deviceinbox import SlavedDeviceInboxStore
  35. from synapse.replication.slave.storage.devices import SlavedDeviceStore
  36. from synapse.replication.slave.storage.directory import DirectoryStore
  37. from synapse.replication.slave.storage.events import SlavedEventStore
  38. from synapse.replication.slave.storage.groups import SlavedGroupServerStore
  39. from synapse.replication.slave.storage.keys import SlavedKeyStore
  40. from synapse.replication.slave.storage.profile import SlavedProfileStore
  41. from synapse.replication.slave.storage.push_rule import SlavedPushRuleStore
  42. from synapse.replication.slave.storage.receipts import SlavedReceiptsStore
  43. from synapse.replication.slave.storage.registration import SlavedRegistrationStore
  44. from synapse.replication.slave.storage.room import RoomStore
  45. from synapse.replication.slave.storage.transactions import SlavedTransactionStore
  46. from synapse.replication.tcp.client import ReplicationClientHandler
  47. from synapse.rest.client.v1.login import LoginRestServlet
  48. from synapse.rest.client.v1.push_rule import PushRuleRestServlet
  49. from synapse.rest.client.v1.room import (
  50. JoinedRoomMemberListRestServlet,
  51. PublicRoomListRestServlet,
  52. RoomEventContextServlet,
  53. RoomMemberListRestServlet,
  54. RoomMessageListRestServlet,
  55. RoomStateRestServlet,
  56. )
  57. from synapse.rest.client.v1.voip import VoipRestServlet
  58. from synapse.rest.client.v2_alpha.account import ThreepidRestServlet
  59. from synapse.rest.client.v2_alpha.keys import KeyChangesServlet, KeyQueryServlet
  60. from synapse.rest.client.v2_alpha.register import RegisterRestServlet
  61. from synapse.rest.client.versions import VersionsRestServlet
  62. from synapse.server import HomeServer
  63. from synapse.storage.engines import create_engine
  64. from synapse.util.httpresourcetree import create_resource_tree
  65. from synapse.util.manhole import manhole
  66. from synapse.util.versionstring import get_version_string
  67. logger = logging.getLogger("synapse.app.client_reader")
  68. class ClientReaderSlavedStore(
  69. SlavedDeviceInboxStore,
  70. SlavedDeviceStore,
  71. SlavedReceiptsStore,
  72. SlavedPushRuleStore,
  73. SlavedGroupServerStore,
  74. SlavedAccountDataStore,
  75. SlavedEventStore,
  76. SlavedKeyStore,
  77. RoomStore,
  78. DirectoryStore,
  79. SlavedApplicationServiceStore,
  80. SlavedRegistrationStore,
  81. SlavedTransactionStore,
  82. SlavedProfileStore,
  83. SlavedClientIpStore,
  84. BaseSlavedStore,
  85. ):
  86. pass
  87. class ClientReaderServer(HomeServer):
  88. DATASTORE_CLASS = ClientReaderSlavedStore
  89. def _listen_http(self, listener_config):
  90. port = listener_config["port"]
  91. bind_addresses = listener_config["bind_addresses"]
  92. site_tag = listener_config.get("tag", port)
  93. resources = {}
  94. for res in listener_config["resources"]:
  95. for name in res["names"]:
  96. if name == "metrics":
  97. resources[METRICS_PREFIX] = MetricsResource(RegistryProxy)
  98. elif name == "client":
  99. resource = JsonResource(self, canonical_json=False)
  100. PublicRoomListRestServlet(self).register(resource)
  101. RoomMemberListRestServlet(self).register(resource)
  102. JoinedRoomMemberListRestServlet(self).register(resource)
  103. RoomStateRestServlet(self).register(resource)
  104. RoomEventContextServlet(self).register(resource)
  105. RoomMessageListRestServlet(self).register(resource)
  106. RegisterRestServlet(self).register(resource)
  107. LoginRestServlet(self).register(resource)
  108. ThreepidRestServlet(self).register(resource)
  109. KeyQueryServlet(self).register(resource)
  110. KeyChangesServlet(self).register(resource)
  111. VoipRestServlet(self).register(resource)
  112. PushRuleRestServlet(self).register(resource)
  113. VersionsRestServlet(self).register(resource)
  114. resources.update({"/_matrix/client": resource})
  115. root_resource = create_resource_tree(resources, NoResource())
  116. _base.listen_tcp(
  117. bind_addresses,
  118. port,
  119. SynapseSite(
  120. "synapse.access.http.%s" % (site_tag,),
  121. site_tag,
  122. listener_config,
  123. root_resource,
  124. self.version_string,
  125. ),
  126. )
  127. logger.info("Synapse client reader now listening on port %d", port)
  128. def start_listening(self, listeners):
  129. for listener in listeners:
  130. if listener["type"] == "http":
  131. self._listen_http(listener)
  132. elif listener["type"] == "manhole":
  133. _base.listen_tcp(
  134. listener["bind_addresses"],
  135. listener["port"],
  136. manhole(
  137. username="matrix", password="rabbithole", globals={"hs": self}
  138. ),
  139. )
  140. elif listener["type"] == "metrics":
  141. if not self.get_config().enable_metrics:
  142. logger.warn(
  143. (
  144. "Metrics listener configured, but "
  145. "enable_metrics is not True!"
  146. )
  147. )
  148. else:
  149. _base.listen_metrics(listener["bind_addresses"], listener["port"])
  150. else:
  151. logger.warn("Unrecognized listener type: %s", listener["type"])
  152. self.get_tcp_replication().start_replication(self)
  153. def build_tcp_replication(self):
  154. return ReplicationClientHandler(self.get_datastore())
  155. def start(config_options):
  156. try:
  157. config = HomeServerConfig.load_config("Synapse client reader", config_options)
  158. except ConfigError as e:
  159. sys.stderr.write("\n" + str(e) + "\n")
  160. sys.exit(1)
  161. assert config.worker_app == "synapse.app.client_reader"
  162. events.USE_FROZEN_DICTS = config.use_frozen_dicts
  163. database_engine = create_engine(config.database_config)
  164. ss = ClientReaderServer(
  165. config.server_name,
  166. db_config=config.database_config,
  167. config=config,
  168. version_string="Synapse/" + get_version_string(synapse),
  169. database_engine=database_engine,
  170. )
  171. setup_logging(ss, config, use_worker_options=True)
  172. ss.setup()
  173. reactor.addSystemEventTrigger(
  174. "before", "startup", _base.start, ss, config.worker_listeners
  175. )
  176. _base.start_worker_reactor("synapse-client-reader", config)
  177. if __name__ == "__main__":
  178. with LoggingContext("main"):
  179. start(sys.argv[1:])