1
0

README.rst 18 KB

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