server.py 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. from ._base import Config
  16. class ServerConfig(Config):
  17. def read_config(self, config):
  18. self.server_name = config["server_name"]
  19. self.pid_file = self.abspath(config.get("pid_file"))
  20. self.web_client = config["web_client"]
  21. self.web_client_location = config.get("web_client_location", None)
  22. self.soft_file_limit = config["soft_file_limit"]
  23. self.daemonize = config.get("daemonize")
  24. self.print_pidfile = config.get("print_pidfile")
  25. self.user_agent_suffix = config.get("user_agent_suffix")
  26. self.use_frozen_dicts = config.get("use_frozen_dicts", True)
  27. self.start_pushers = config.get("start_pushers", True)
  28. self.listeners = config.get("listeners", [])
  29. bind_port = config.get("bind_port")
  30. if bind_port:
  31. self.listeners = []
  32. bind_host = config.get("bind_host", "")
  33. gzip_responses = config.get("gzip_responses", True)
  34. names = ["client", "webclient"] if self.web_client else ["client"]
  35. self.listeners.append({
  36. "port": bind_port,
  37. "bind_address": bind_host,
  38. "tls": True,
  39. "type": "http",
  40. "resources": [
  41. {
  42. "names": names,
  43. "compress": gzip_responses,
  44. },
  45. {
  46. "names": ["federation"],
  47. "compress": False,
  48. }
  49. ]
  50. })
  51. unsecure_port = config.get("unsecure_port", bind_port - 400)
  52. if unsecure_port:
  53. self.listeners.append({
  54. "port": unsecure_port,
  55. "bind_address": bind_host,
  56. "tls": False,
  57. "type": "http",
  58. "resources": [
  59. {
  60. "names": names,
  61. "compress": gzip_responses,
  62. },
  63. {
  64. "names": ["federation"],
  65. "compress": False,
  66. }
  67. ]
  68. })
  69. manhole = config.get("manhole")
  70. if manhole:
  71. self.listeners.append({
  72. "port": manhole,
  73. "bind_address": "127.0.0.1",
  74. "type": "manhole",
  75. })
  76. metrics_port = config.get("metrics_port")
  77. if metrics_port:
  78. self.listeners.append({
  79. "port": metrics_port,
  80. "bind_address": config.get("metrics_bind_host", "127.0.0.1"),
  81. "tls": False,
  82. "type": "http",
  83. "resources": [
  84. {
  85. "names": ["metrics"],
  86. "compress": False,
  87. },
  88. ]
  89. })
  90. # Attempt to guess the content_addr for the v0 content repostitory
  91. content_addr = config.get("content_addr")
  92. if not content_addr:
  93. for listener in self.listeners:
  94. if listener["type"] == "http" and not listener.get("tls", False):
  95. unsecure_port = listener["port"]
  96. break
  97. else:
  98. raise RuntimeError("Could not determine 'content_addr'")
  99. host = self.server_name
  100. if ':' not in host:
  101. host = "%s:%d" % (host, unsecure_port)
  102. else:
  103. host = host.split(':')[0]
  104. host = "%s:%d" % (host, unsecure_port)
  105. content_addr = "http://%s" % (host,)
  106. self.content_addr = content_addr
  107. def default_config(self, server_name, **kwargs):
  108. if ":" in server_name:
  109. bind_port = int(server_name.split(":")[1])
  110. unsecure_port = bind_port - 400
  111. else:
  112. bind_port = 8448
  113. unsecure_port = 8008
  114. pid_file = self.abspath("homeserver.pid")
  115. return """\
  116. ## Server ##
  117. # The domain name of the server, with optional explicit port.
  118. # This is used by remote servers to connect to this server,
  119. # e.g. matrix.org, localhost:8080, etc.
  120. # This is also the last part of your UserID.
  121. server_name: "%(server_name)s"
  122. # When running as a daemon, the file to store the pid in
  123. pid_file: %(pid_file)s
  124. # Whether to serve a web client from the HTTP/HTTPS root resource.
  125. web_client: True
  126. # Set the soft limit on the number of file descriptors synapse can use
  127. # Zero is used to indicate synapse should set the soft limit to the
  128. # hard limit.
  129. soft_file_limit: 0
  130. # List of ports that Synapse should listen on, their purpose and their
  131. # configuration.
  132. listeners:
  133. # Main HTTPS listener
  134. # For when matrix traffic is sent directly to synapse.
  135. -
  136. # The port to listen for HTTPS requests on.
  137. port: %(bind_port)s
  138. # Local interface to listen on.
  139. # The empty string will cause synapse to listen on all interfaces.
  140. bind_address: ''
  141. # This is a 'http' listener, allows us to specify 'resources'.
  142. type: http
  143. tls: true
  144. # Use the X-Forwarded-For (XFF) header as the client IP and not the
  145. # actual client IP.
  146. x_forwarded: false
  147. # List of HTTP resources to serve on this listener.
  148. resources:
  149. -
  150. # List of resources to host on this listener.
  151. names:
  152. - client # The client-server APIs, both v1 and v2
  153. - webclient # The bundled webclient.
  154. # Should synapse compress HTTP responses to clients that support it?
  155. # This should be disabled if running synapse behind a load balancer
  156. # that can do automatic compression.
  157. compress: true
  158. - names: [federation] # Federation APIs
  159. compress: false
  160. # Unsecure HTTP listener,
  161. # For when matrix traffic passes through loadbalancer that unwraps TLS.
  162. - port: %(unsecure_port)s
  163. tls: false
  164. bind_address: ''
  165. type: http
  166. x_forwarded: false
  167. resources:
  168. - names: [client, webclient]
  169. compress: true
  170. - names: [federation]
  171. compress: false
  172. # Turn on the twisted ssh manhole service on localhost on the given
  173. # port.
  174. # - port: 9000
  175. # bind_address: 127.0.0.1
  176. # type: manhole
  177. """ % locals()
  178. def read_arguments(self, args):
  179. if args.manhole is not None:
  180. self.manhole = args.manhole
  181. if args.daemonize is not None:
  182. self.daemonize = args.daemonize
  183. if args.print_pidfile is not None:
  184. self.print_pidfile = args.print_pidfile
  185. def add_arguments(self, parser):
  186. server_group = parser.add_argument_group("server")
  187. server_group.add_argument("-D", "--daemonize", action='store_true',
  188. default=None,
  189. help="Daemonize the home server")
  190. server_group.add_argument("--print-pidfile", action='store_true',
  191. default=None,
  192. help="Print the path to the pidfile just"
  193. " before daemonizing")
  194. server_group.add_argument("--manhole", metavar="PORT", dest="manhole",
  195. type=int,
  196. help="Turn on the twisted telnet manhole"
  197. " service on the given port.")