server.py 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2014-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. # This file provides some classes for setting up (partially-populated)
  16. # homeservers; either as a full homeserver as a real application, or a small
  17. # partial one for unit test mocking.
  18. # Imports required for the default HomeServer() implementation
  19. from twisted.web.client import BrowserLikePolicyForHTTPS
  20. from twisted.enterprise import adbapi
  21. from synapse.appservice.scheduler import ApplicationServiceScheduler
  22. from synapse.appservice.api import ApplicationServiceApi
  23. from synapse.federation import initialize_http_replication
  24. from synapse.http.client import SimpleHttpClient, InsecureInterceptableContextFactory
  25. from synapse.notifier import Notifier
  26. from synapse.api.auth import Auth
  27. from synapse.handlers import Handlers
  28. from synapse.handlers.presence import PresenceHandler
  29. from synapse.handlers.sync import SyncHandler
  30. from synapse.handlers.typing import TypingHandler
  31. from synapse.handlers.room import RoomListHandler
  32. from synapse.handlers.appservice import ApplicationServicesHandler
  33. from synapse.state import StateHandler
  34. from synapse.storage import DataStore
  35. from synapse.util import Clock
  36. from synapse.util.distributor import Distributor
  37. from synapse.streams.events import EventSources
  38. from synapse.api.ratelimiting import Ratelimiter
  39. from synapse.crypto.keyring import Keyring
  40. from synapse.push.pusherpool import PusherPool
  41. from synapse.events.builder import EventBuilderFactory
  42. from synapse.api.filtering import Filtering
  43. from synapse.http.matrixfederationclient import MatrixFederationHttpClient
  44. import logging
  45. logger = logging.getLogger(__name__)
  46. class HomeServer(object):
  47. """A basic homeserver object without lazy component builders.
  48. This will need all of the components it requires to either be passed as
  49. constructor arguments, or the relevant methods overriding to create them.
  50. Typically this would only be used for unit tests.
  51. For every dependency in the DEPENDENCIES list below, this class creates one
  52. method,
  53. def get_DEPENDENCY(self)
  54. which returns the value of that dependency. If no value has yet been set
  55. nor was provided to the constructor, it will attempt to call a lazy builder
  56. method called
  57. def build_DEPENDENCY(self)
  58. which must be implemented by the subclass. This code may call any of the
  59. required "get" methods on the instance to obtain the sub-dependencies that
  60. one requires.
  61. """
  62. DEPENDENCIES = [
  63. 'config',
  64. 'clock',
  65. 'http_client',
  66. 'db_pool',
  67. 'persistence_service',
  68. 'replication_layer',
  69. 'datastore',
  70. 'handlers',
  71. 'v1auth',
  72. 'auth',
  73. 'rest_servlet_factory',
  74. 'state_handler',
  75. 'presence_handler',
  76. 'sync_handler',
  77. 'typing_handler',
  78. 'room_list_handler',
  79. 'application_service_api',
  80. 'application_service_scheduler',
  81. 'application_service_handler',
  82. 'notifier',
  83. 'distributor',
  84. 'client_resource',
  85. 'resource_for_federation',
  86. 'resource_for_static_content',
  87. 'resource_for_web_client',
  88. 'resource_for_content_repo',
  89. 'resource_for_server_key',
  90. 'resource_for_server_key_v2',
  91. 'resource_for_media_repository',
  92. 'resource_for_metrics',
  93. 'event_sources',
  94. 'ratelimiter',
  95. 'keyring',
  96. 'pusherpool',
  97. 'event_builder_factory',
  98. 'filtering',
  99. 'http_client_context_factory',
  100. 'simple_http_client',
  101. ]
  102. def __init__(self, hostname, **kwargs):
  103. """
  104. Args:
  105. hostname : The hostname for the server.
  106. """
  107. self.hostname = hostname
  108. self._building = {}
  109. self.clock = Clock()
  110. self.distributor = Distributor()
  111. self.ratelimiter = Ratelimiter()
  112. # Other kwargs are explicit dependencies
  113. for depname in kwargs:
  114. setattr(self, depname, kwargs[depname])
  115. def setup(self):
  116. logger.info("Setting up.")
  117. self.datastore = DataStore(self.get_db_conn(), self)
  118. logger.info("Finished setting up.")
  119. def get_ip_from_request(self, request):
  120. # X-Forwarded-For is handled by our custom request type.
  121. return request.getClientIP()
  122. def is_mine(self, domain_specific_string):
  123. return domain_specific_string.domain == self.hostname
  124. def is_mine_id(self, string):
  125. return string.split(":", 1)[1] == self.hostname
  126. def build_replication_layer(self):
  127. return initialize_http_replication(self)
  128. def build_handlers(self):
  129. return Handlers(self)
  130. def build_notifier(self):
  131. return Notifier(self)
  132. def build_auth(self):
  133. return Auth(self)
  134. def build_http_client_context_factory(self):
  135. return (
  136. InsecureInterceptableContextFactory()
  137. if self.config.use_insecure_ssl_client_just_for_testing_do_not_use
  138. else BrowserLikePolicyForHTTPS()
  139. )
  140. def build_simple_http_client(self):
  141. return SimpleHttpClient(self)
  142. def build_v1auth(self):
  143. orf = Auth(self)
  144. # Matrix spec makes no reference to what HTTP status code is returned,
  145. # but the V1 API uses 403 where it means 401, and the webclient
  146. # relies on this behaviour, so V1 gets its own copy of the auth
  147. # with backwards compat behaviour.
  148. orf.TOKEN_NOT_FOUND_HTTP_STATUS = 403
  149. return orf
  150. def build_state_handler(self):
  151. return StateHandler(self)
  152. def build_presence_handler(self):
  153. return PresenceHandler(self)
  154. def build_typing_handler(self):
  155. return TypingHandler(self)
  156. def build_sync_handler(self):
  157. return SyncHandler(self)
  158. def build_room_list_handler(self):
  159. return RoomListHandler(self)
  160. def build_application_service_api(self):
  161. return ApplicationServiceApi(self)
  162. def build_application_service_scheduler(self):
  163. return ApplicationServiceScheduler(self)
  164. def build_application_service_handler(self):
  165. return ApplicationServicesHandler(self)
  166. def build_event_sources(self):
  167. return EventSources(self)
  168. def build_keyring(self):
  169. return Keyring(self)
  170. def build_event_builder_factory(self):
  171. return EventBuilderFactory(
  172. clock=self.get_clock(),
  173. hostname=self.hostname,
  174. )
  175. def build_filtering(self):
  176. return Filtering(self)
  177. def build_pusherpool(self):
  178. return PusherPool(self)
  179. def build_http_client(self):
  180. return MatrixFederationHttpClient(self)
  181. def build_db_pool(self):
  182. name = self.db_config["name"]
  183. return adbapi.ConnectionPool(
  184. name,
  185. **self.db_config.get("args", {})
  186. )
  187. def remove_pusher(self, app_id, push_key, user_id):
  188. return self.get_pusherpool().remove_pusher(app_id, push_key, user_id)
  189. def _make_dependency_method(depname):
  190. def _get(hs):
  191. try:
  192. return getattr(hs, depname)
  193. except AttributeError:
  194. pass
  195. try:
  196. builder = getattr(hs, "build_%s" % (depname))
  197. except AttributeError:
  198. builder = None
  199. if builder:
  200. # Prevent cyclic dependencies from deadlocking
  201. if depname in hs._building:
  202. raise ValueError("Cyclic dependency while building %s" % (
  203. depname,
  204. ))
  205. hs._building[depname] = 1
  206. dep = builder()
  207. setattr(hs, depname, dep)
  208. del hs._building[depname]
  209. return dep
  210. raise NotImplementedError(
  211. "%s has no %s nor a builder for it" % (
  212. type(hs).__name__, depname,
  213. )
  214. )
  215. setattr(HomeServer, "get_%s" % (depname), _get)
  216. # Build magic accessors for every dependency
  217. for depname in HomeServer.DEPENDENCIES:
  218. _make_dependency_method(depname)