README.rst 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. ================
  2. Synapse |shield|
  3. ================
  4. .. |shield| image:: https://img.shields.io/matrix/synapse:matrix.org?label=support&logo=matrix
  5. :alt: (get support on #synapse:matrix.org)
  6. :target: https://matrix.to/#/#synapse:matrix.org
  7. .. contents::
  8. Introduction
  9. ============
  10. Matrix is an ambitious new ecosystem for open federated Instant Messaging and
  11. VoIP. The basics you need to know to get up and running are:
  12. - Everything in Matrix happens in a room. Rooms are distributed and do not
  13. exist on any single server. Rooms can be located using convenience aliases
  14. like ``#matrix:matrix.org`` or ``#test:localhost:8448``.
  15. - Matrix user IDs look like ``@matthew:matrix.org`` (although in the future
  16. you will normally refer to yourself and others using a third party identifier
  17. (3PID): email address, phone number, etc rather than manipulating Matrix user IDs)
  18. The overall architecture is::
  19. client <----> homeserver <=====================> homeserver <----> client
  20. https://somewhere.org/_matrix https://elsewhere.net/_matrix
  21. ``#matrix:matrix.org`` is the official support room for Matrix, and can be
  22. accessed by any client from https://matrix.org/docs/projects/try-matrix-now.html or
  23. via IRC bridge at irc://irc.freenode.net/matrix.
  24. Synapse is currently in rapid development, but as of version 0.5 we believe it
  25. is sufficiently stable to be run as an internet-facing service for real usage!
  26. About Matrix
  27. ============
  28. Matrix specifies a set of pragmatic RESTful HTTP JSON APIs as an open standard,
  29. which handle:
  30. - Creating and managing fully distributed chat rooms with no
  31. single points of control or failure
  32. - Eventually-consistent cryptographically secure synchronisation of room
  33. state across a global open network of federated servers and services
  34. - Sending and receiving extensible messages in a room with (optional)
  35. end-to-end encryption[1]
  36. - Inviting, joining, leaving, kicking, banning room members
  37. - Managing user accounts (registration, login, logout)
  38. - Using 3rd Party IDs (3PIDs) such as email addresses, phone numbers,
  39. Facebook accounts to authenticate, identify and discover users on Matrix.
  40. - Placing 1:1 VoIP and Video calls
  41. These APIs are intended to be implemented on a wide range of servers, services
  42. and clients, letting developers build messaging and VoIP functionality on top
  43. of the entirely open Matrix ecosystem rather than using closed or proprietary
  44. solutions. The hope is for Matrix to act as the building blocks for a new
  45. generation of fully open and interoperable messaging and VoIP apps for the
  46. internet.
  47. Synapse is a reference "homeserver" implementation of Matrix from the core
  48. development team at matrix.org, written in Python/Twisted. It is intended to
  49. showcase the concept of Matrix and let folks see the spec in the context of a
  50. codebase and let you run your own homeserver and generally help bootstrap the
  51. ecosystem.
  52. In Matrix, every user runs one or more Matrix clients, which connect through to
  53. a Matrix homeserver. The homeserver stores all their personal chat history and
  54. user account information - much as a mail client connects through to an
  55. IMAP/SMTP server. Just like email, you can either run your own Matrix
  56. homeserver and control and own your own communications and history or use one
  57. hosted by someone else (e.g. matrix.org) - there is no single point of control
  58. or mandatory service provider in Matrix, unlike WhatsApp, Facebook, Hangouts,
  59. etc.
  60. We'd like to invite you to join #matrix:matrix.org (via
  61. https://matrix.org/docs/projects/try-matrix-now.html), run a homeserver, take a look
  62. at the `Matrix spec <https://matrix.org/docs/spec>`_, and experiment with the
  63. `APIs <https://matrix.org/docs/api>`_ and `Client SDKs
  64. <https://matrix.org/docs/projects/try-matrix-now.html#client-sdks>`_.
  65. Thanks for using Matrix!
  66. [1] End-to-end encryption is currently in beta: `blog post <https://matrix.org/blog/2016/11/21/matrixs-olm-end-to-end-encryption-security-assessment-released-and-implemented-cross-platform-on-riot-at-last>`_.
  67. Support
  68. =======
  69. For support installing or managing Synapse, please join |room|_ (from a matrix.org
  70. account if necessary) and ask questions there. We do not use GitHub issues for
  71. support requests, only for bug reports and feature requests.
  72. .. |room| replace:: ``#synapse:matrix.org``
  73. .. _room: https://matrix.to/#/#synapse:matrix.org
  74. Synapse Installation
  75. ====================
  76. .. _federation:
  77. * For details on how to install synapse, see `<INSTALL.md>`_.
  78. * For specific details on how to configure Synapse for federation see `docs/federate.md <docs/federate.md>`_
  79. Connecting to Synapse from a client
  80. ===================================
  81. The easiest way to try out your new Synapse installation is by connecting to it
  82. from a web client.
  83. Unless you are running a test instance of Synapse on your local machine, in
  84. general, you will need to enable TLS support before you can successfully
  85. connect from a client: see `<INSTALL.md#tls-certificates>`_.
  86. An easy way to get started is to login or register via Riot at
  87. https://riot.im/app/#/login or https://riot.im/app/#/register respectively.
  88. You will need to change the server you are logging into from ``matrix.org``
  89. and instead specify a Homeserver URL of ``https://<server_name>:8448``
  90. (or just ``https://<server_name>`` if you are using a reverse proxy).
  91. (Leave the identity server as the default - see `Identity servers`_.)
  92. If you prefer to use another client, refer to our
  93. `client breakdown <https://matrix.org/docs/projects/clients-matrix>`_.
  94. If all goes well you should at least be able to log in, create a room, and
  95. start sending messages.
  96. .. _`client-user-reg`:
  97. Registering a new user from a client
  98. ------------------------------------
  99. By default, registration of new users via Matrix clients is disabled. To enable
  100. it, specify ``enable_registration: true`` in ``homeserver.yaml``. (It is then
  101. recommended to also set up CAPTCHA - see `<docs/CAPTCHA_SETUP.md>`_.)
  102. Once ``enable_registration`` is set to ``true``, it is possible to register a
  103. user via `riot.im <https://riot.im/app/#/register>`_ or other Matrix clients.
  104. Your new user name will be formed partly from the ``server_name``, and partly
  105. from a localpart you specify when you create the account. Your name will take
  106. the form of::
  107. @localpart:my.domain.name
  108. (pronounced "at localpart on my dot domain dot name").
  109. As when logging in, you will need to specify a "Custom server". Specify your
  110. desired ``localpart`` in the 'User name' box.
  111. ACME setup
  112. ==========
  113. For details on having Synapse manage your federation TLS certificates
  114. automatically, please see `<docs/ACME.md>`_.
  115. Security Note
  116. =============
  117. Matrix serves raw user generated data in some APIs - specifically the `content
  118. repository endpoints <https://matrix.org/docs/spec/client_server/latest.html#get-matrix-media-r0-download-servername-mediaid>`_.
  119. Whilst we have tried to mitigate against possible XSS attacks (e.g.
  120. https://github.com/matrix-org/synapse/pull/1021) we recommend running
  121. matrix homeservers on a dedicated domain name, to limit any malicious user generated
  122. content served to web browsers a matrix API from being able to attack webapps hosted
  123. on the same domain. This is particularly true of sharing a matrix webclient and
  124. server on the same domain.
  125. See https://github.com/vector-im/riot-web/issues/1977 and
  126. https://developer.github.com/changes/2014-04-25-user-content-security for more details.
  127. Upgrading an existing Synapse
  128. =============================
  129. The instructions for upgrading synapse are in `UPGRADE.rst`_.
  130. Please check these instructions as upgrading may require extra steps for some
  131. versions of synapse.
  132. .. _UPGRADE.rst: UPGRADE.rst
  133. Using PostgreSQL
  134. ================
  135. Synapse offers two database engines:
  136. * `SQLite <https://sqlite.org/>`_
  137. * `PostgreSQL <https://www.postgresql.org>`_
  138. By default Synapse uses SQLite in and doing so trades performance for convenience.
  139. SQLite is only recommended in Synapse for testing purposes or for servers with
  140. light workloads.
  141. Almost all installations should opt to use PostreSQL. Advantages include:
  142. * significant performance improvements due to the superior threading and
  143. caching model, smarter query optimiser
  144. * allowing the DB to be run on separate hardware
  145. * allowing basic active/backup high-availability with a "hot spare" synapse
  146. pointing at the same DB master, as well as enabling DB replication in
  147. synapse itself.
  148. For information on how to install and use PostgreSQL, please see
  149. `docs/postgres.md <docs/postgres.md>`_.
  150. .. _reverse-proxy:
  151. Using a reverse proxy with Synapse
  152. ==================================
  153. It is recommended to put a reverse proxy such as
  154. `nginx <https://nginx.org/en/docs/http/ngx_http_proxy_module.html>`_,
  155. `Apache <https://httpd.apache.org/docs/current/mod/mod_proxy_http.html>`_,
  156. `Caddy <https://caddyserver.com/docs/proxy>`_ or
  157. `HAProxy <https://www.haproxy.org/>`_ in front of Synapse. One advantage of
  158. doing so is that it means that you can expose the default https port (443) to
  159. Matrix clients without needing to run Synapse with root privileges.
  160. For information on configuring one, see `<docs/reverse_proxy.md>`_.
  161. Identity Servers
  162. ================
  163. Identity servers have the job of mapping email addresses and other 3rd Party
  164. IDs (3PIDs) to Matrix user IDs, as well as verifying the ownership of 3PIDs
  165. before creating that mapping.
  166. **They are not where accounts or credentials are stored - these live on home
  167. servers. Identity Servers are just for mapping 3rd party IDs to matrix IDs.**
  168. This process is very security-sensitive, as there is obvious risk of spam if it
  169. is too easy to sign up for Matrix accounts or harvest 3PID data. In the longer
  170. term, we hope to create a decentralised system to manage it (`matrix-doc #712
  171. <https://github.com/matrix-org/matrix-doc/issues/712>`_), but in the meantime,
  172. the role of managing trusted identity in the Matrix ecosystem is farmed out to
  173. a cluster of known trusted ecosystem partners, who run 'Matrix Identity
  174. Servers' such as `Sydent <https://github.com/matrix-org/sydent>`_, whose role
  175. is purely to authenticate and track 3PID logins and publish end-user public
  176. keys.
  177. You can host your own copy of Sydent, but this will prevent you reaching other
  178. users in the Matrix ecosystem via their email address, and prevent them finding
  179. you. We therefore recommend that you use one of the centralised identity servers
  180. at ``https://matrix.org`` or ``https://vector.im`` for now.
  181. To reiterate: the Identity server will only be used if you choose to associate
  182. an email address with your account, or send an invite to another user via their
  183. email address.
  184. Password reset
  185. ==============
  186. If a user has registered an email address to their account using an identity
  187. server, they can request a password-reset token via clients such as Riot.
  188. A manual password reset can be done via direct database access as follows.
  189. First calculate the hash of the new password::
  190. $ ~/synapse/env/bin/hash_password
  191. Password:
  192. Confirm password:
  193. $2a$12$xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
  194. Then update the `users` table in the database::
  195. UPDATE users SET password_hash='$2a$12$xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
  196. WHERE name='@test:test.com';
  197. Synapse Development
  198. ===================
  199. Before setting up a development environment for synapse, make sure you have the
  200. system dependencies (such as the python header files) installed - see
  201. `Installing from source <INSTALL.md#installing-from-source>`_.
  202. To check out a synapse for development, clone the git repo into a working
  203. directory of your choice::
  204. git clone https://github.com/matrix-org/synapse.git
  205. cd synapse
  206. Synapse has a number of external dependencies, that are easiest
  207. to install using pip and a virtualenv::
  208. virtualenv -p python3 env
  209. source env/bin/activate
  210. python -m pip install --no-use-pep517 -e ".[all]"
  211. This will run a process of downloading and installing all the needed
  212. dependencies into a virtual env.
  213. Once this is done, you may wish to run Synapse's unit tests, to
  214. check that everything is installed as it should be::
  215. python -m twisted.trial tests
  216. This should end with a 'PASSED' result::
  217. Ran 143 tests in 0.601s
  218. PASSED (successes=143)
  219. Running the Integration Tests
  220. =============================
  221. Synapse is accompanied by `SyTest <https://github.com/matrix-org/sytest>`_,
  222. a Matrix homeserver integration testing suite, which uses HTTP requests to
  223. access the API as a Matrix client would. It is able to run Synapse directly from
  224. the source tree, so installation of the server is not required.
  225. Testing with SyTest is recommended for verifying that changes related to the
  226. Client-Server API are functioning correctly. See the `installation instructions
  227. <https://github.com/matrix-org/sytest#installing>`_ for details.
  228. Building Internal API Documentation
  229. ===================================
  230. Before building internal API documentation install sphinx and
  231. sphinxcontrib-napoleon::
  232. pip install sphinx
  233. pip install sphinxcontrib-napoleon
  234. Building internal API documentation::
  235. python setup.py build_sphinx
  236. Troubleshooting
  237. ===============
  238. Running out of File Handles
  239. ---------------------------
  240. If synapse runs out of file handles, it typically fails badly - live-locking
  241. at 100% CPU, and/or failing to accept new TCP connections (blocking the
  242. connecting client). Matrix currently can legitimately use a lot of file handles,
  243. thanks to busy rooms like #matrix:matrix.org containing hundreds of participating
  244. servers. The first time a server talks in a room it will try to connect
  245. simultaneously to all participating servers, which could exhaust the available
  246. file descriptors between DNS queries & HTTPS sockets, especially if DNS is slow
  247. to respond. (We need to improve the routing algorithm used to be better than
  248. full mesh, but as of March 2019 this hasn't happened yet).
  249. If you hit this failure mode, we recommend increasing the maximum number of
  250. open file handles to be at least 4096 (assuming a default of 1024 or 256).
  251. This is typically done by editing ``/etc/security/limits.conf``
  252. Separately, Synapse may leak file handles if inbound HTTP requests get stuck
  253. during processing - e.g. blocked behind a lock or talking to a remote server etc.
  254. This is best diagnosed by matching up the 'Received request' and 'Processed request'
  255. log lines and looking for any 'Processed request' lines which take more than
  256. a few seconds to execute. Please let us know at #synapse:matrix.org if
  257. you see this failure mode so we can help debug it, however.
  258. Help!! Synapse is slow and eats all my RAM/CPU!
  259. -----------------------------------------------
  260. First, ensure you are running the latest version of Synapse, using Python 3
  261. with a PostgreSQL database.
  262. Synapse's architecture is quite RAM hungry currently - we deliberately
  263. cache a lot of recent room data and metadata in RAM in order to speed up
  264. common requests. We'll improve this in the future, but for now the easiest
  265. way to either reduce the RAM usage (at the risk of slowing things down)
  266. is to set the almost-undocumented ``SYNAPSE_CACHE_FACTOR`` environment
  267. variable. The default is 0.5, which can be decreased to reduce RAM usage
  268. in memory constrained enviroments, or increased if performance starts to
  269. degrade.
  270. However, degraded performance due to a low cache factor, common on
  271. machines with slow disks, often leads to explosions in memory use due
  272. backlogged requests. In this case, reducing the cache factor will make
  273. things worse. Instead, try increasing it drastically. 2.0 is a good
  274. starting value.
  275. Using `libjemalloc <http://jemalloc.net/>`_ can also yield a significant
  276. improvement in overall memory use, and especially in terms of giving back
  277. RAM to the OS. To use it, the library must simply be put in the
  278. LD_PRELOAD environment variable when launching Synapse. On Debian, this
  279. can be done by installing the ``libjemalloc1`` package and adding this
  280. line to ``/etc/default/matrix-synapse``::
  281. LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libjemalloc.so.1
  282. This can make a significant difference on Python 2.7 - it's unclear how
  283. much of an improvement it provides on Python 3.x.
  284. If you're encountering high CPU use by the Synapse process itself, you
  285. may be affected by a bug with presence tracking that leads to a
  286. massive excess of outgoing federation requests (see `discussion
  287. <https://github.com/matrix-org/synapse/issues/3971>`_). If metrics
  288. indicate that your server is also issuing far more outgoing federation
  289. requests than can be accounted for by your users' activity, this is a
  290. likely cause. The misbehavior can be worked around by setting
  291. ``use_presence: false`` in the Synapse config file.
  292. People can't accept room invitations from me
  293. --------------------------------------------
  294. The typical failure mode here is that you send an invitation to someone
  295. to join a room or direct chat, but when they go to accept it, they get an
  296. error (typically along the lines of "Invalid signature"). They might see
  297. something like the following in their logs::
  298. 2019-09-11 19:32:04,271 - synapse.federation.transport.server - 288 - WARNING - GET-11752 - authenticate_request failed: 401: Invalid signature for server <server> with key ed25519:a_EqML: Unable to verify signature for <server>
  299. This is normally caused by a misconfiguration in your reverse-proxy. See
  300. `<docs/reverse_proxy.md>`_ and double-check that your settings are correct.