1
0

README.rst 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  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 3PID: email
  11. 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 the web client at http://matrix.org/beta or via an IRC bridge at
  17. 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 for clarity and
  43. simplicity. It is intended to showcase the concept of Matrix and let folks see
  44. the spec in the context of a codebase and let you run your own homeserver and
  45. generally help bootstrap the ecosystem.
  46. In Matrix, every user runs one or more Matrix clients, which connect through to
  47. a Matrix homeserver which stores all their personal chat history and user
  48. account information - much as a mail client connects through to an IMAP/SMTP
  49. server. Just like email, you can either run your own Matrix homeserver and
  50. control and own your own communications and history or use one hosted by
  51. someone else (e.g. matrix.org) - there is no single point of control or
  52. mandatory service provider in Matrix, unlike WhatsApp, Facebook, Hangouts, etc.
  53. Synapse ships with two basic demo Matrix clients: webclient (a basic group chat
  54. web client demo implemented in AngularJS) and cmdclient (a basic Python
  55. command line utility which lets you easily see what the JSON APIs are up to).
  56. Meanwhile, iOS and Android SDKs and clients are available from:
  57. - https://github.com/matrix-org/matrix-ios-sdk
  58. - https://github.com/matrix-org/matrix-ios-kit
  59. - https://github.com/matrix-org/matrix-ios-console
  60. - https://github.com/matrix-org/matrix-android-sdk
  61. We'd like to invite you to join #matrix:matrix.org (via
  62. https://matrix.org/beta), run a homeserver, take a look at the Matrix spec at
  63. https://matrix.org/docs/spec and API docs at https://matrix.org/docs/api,
  64. experiment with the APIs and the demo clients, and report any bugs via
  65. https://matrix.org/jira.
  66. Thanks for using Matrix!
  67. [1] End-to-end encryption is currently in development
  68. Synapse Installation
  69. ====================
  70. Synapse is the reference python/twisted Matrix homeserver implementation.
  71. System requirements:
  72. - POSIX-compliant system (tested on Linux & OS X)
  73. - Python 2.7
  74. Synapse is written in python but some of the libraries is uses are written in
  75. C. So before we can install synapse itself we need a working C compiler and the
  76. header files for python C extensions.
  77. Installing prerequisites on Ubuntu or Debian::
  78. sudo apt-get install build-essential python2.7-dev libffi-dev \
  79. python-pip python-setuptools sqlite3 \
  80. libssl-dev python-virtualenv libjpeg-dev
  81. Installing prerequisites on ArchLinux::
  82. sudo pacman -S base-devel python2 python-pip \
  83. python-setuptools python-virtualenv sqlite3
  84. Installing prerequisites on Mac OS X::
  85. xcode-select --install
  86. sudo easy_install pip
  87. sudo pip install virtualenv
  88. To install the synapse homeserver run::
  89. virtualenv -p python2.7 ~/.synapse
  90. source ~/.synapse/bin/activate
  91. pip install --process-dependency-links https://github.com/matrix-org/synapse/tarball/master
  92. This installs synapse, along with the libraries it uses, into a virtual
  93. environment under ``~/.synapse``. Feel free to pick a different directory
  94. if you prefer.
  95. In case of problems, please see the _Troubleshooting section below.
  96. Alternatively, Silvio Fricke has contributed a Dockerfile to automate the
  97. above in Docker at https://registry.hub.docker.com/u/silviof/docker-matrix/.
  98. To set up your homeserver, run (in your virtualenv, as before)::
  99. cd ~/.synapse
  100. python -m synapse.app.homeserver \
  101. --server-name machine.my.domain.name \
  102. --config-path homeserver.yaml \
  103. --generate-config
  104. Substituting your host and domain name as appropriate.
  105. This will generate you a config file that you can then customise, but it will
  106. also generate a set of keys for you. These keys will allow your Home Server to
  107. identify itself to other Home Servers, so don't lose or delete them. It would be
  108. wise to back them up somewhere safe. If, for whatever reason, you do need to
  109. change your Home Server's keys, you may find that other Home Servers have the
  110. old key cached. If you update the signing key, you should change the name of the
  111. key in the <server name>.signing.key file (the second word, which by default is
  112. , 'auto') to something different.
  113. By default, registration of new users is disabled. You can either enable
  114. registration in the config by specifying ``enable_registration: true``
  115. (it is then recommended to also set up CAPTCHA), or
  116. you can use the command line to register new users::
  117. $ source ~/.synapse/bin/activate
  118. $ register_new_matrix_user -c homeserver.yaml https://localhost:8448
  119. New user localpart: erikj
  120. Password:
  121. Confirm password:
  122. Success!
  123. For reliable VoIP calls to be routed via this homeserver, you MUST configure
  124. a TURN server. See docs/turn-howto.rst for details.
  125. Using PostgreSQL
  126. ================
  127. As of Synapse 0.9, `PostgreSQL <http://www.postgresql.org>`_ is supported as an
  128. alternative to the `SQLite <http://sqlite.org/>`_ database that Synapse has
  129. traditionally used for convenience and simplicity.
  130. The advantages of Postgres include:
  131. * significant performance improvements due to the superior threading and
  132. caching model, smarter query optimiser
  133. * allowing the DB to be run on separate hardware
  134. * allowing basic active/backup high-availability with a "hot spare" synapse
  135. pointing at the same DB master, as well as enabling DB replication in
  136. synapse itself.
  137. The only disadvantage is that the code is relatively new as of April 2015 and
  138. may have a few regressions relative to SQLite.
  139. For information on how to install and use PostgreSQL, please see
  140. `docs/postgres.rst <docs/postgres.rst>`_.
  141. Running Synapse
  142. ===============
  143. To actually run your new homeserver, pick a working directory for Synapse to run
  144. (e.g. ``~/.synapse``), and::
  145. cd ~/.synapse
  146. source ./bin/activate
  147. synctl start
  148. Platform Specific Instructions
  149. ==============================
  150. ArchLinux
  151. ---------
  152. The quickest way to get up and running with ArchLinux is probably with Ivan
  153. Shapovalov's AUR package from
  154. https://aur.archlinux.org/packages/matrix-synapse/, which should pull in all
  155. the necessary dependencies.
  156. Alternatively, to install using pip a few changes may be needed as ArchLinux
  157. defaults to python 3, but synapse currently assumes python 2.7 by default:
  158. pip may be outdated (6.0.7-1 and needs to be upgraded to 6.0.8-1 )::
  159. sudo pip2.7 install --upgrade pip
  160. You also may need to explicitly specify python 2.7 again during the install
  161. request::
  162. pip2.7 install --process-dependency-links \
  163. https://github.com/matrix-org/synapse/tarball/master
  164. If you encounter an error with lib bcrypt causing an Wrong ELF Class:
  165. ELFCLASS32 (x64 Systems), you may need to reinstall py-bcrypt to correctly
  166. compile it under the right architecture. (This should not be needed if
  167. installing under virtualenv)::
  168. sudo pip2.7 uninstall py-bcrypt
  169. sudo pip2.7 install py-bcrypt
  170. During setup of Synapse you need to call python2.7 directly again::
  171. cd ~/.synapse
  172. python2.7 -m synapse.app.homeserver \
  173. --server-name machine.my.domain.name \
  174. --config-path homeserver.yaml \
  175. --generate-config
  176. ...substituting your host and domain name as appropriate.
  177. Windows Install
  178. ---------------
  179. Synapse can be installed on Cygwin. It requires the following Cygwin packages:
  180. - gcc
  181. - git
  182. - libffi-devel
  183. - openssl (and openssl-devel, python-openssl)
  184. - python
  185. - python-setuptools
  186. The content repository requires additional packages and will be unable to process
  187. uploads without them:
  188. - libjpeg8
  189. - libjpeg8-devel
  190. - zlib
  191. If you choose to install Synapse without these packages, you will need to reinstall
  192. ``pillow`` for changes to be applied, e.g. ``pip uninstall pillow`` ``pip install
  193. pillow --user``
  194. Troubleshooting:
  195. - You may need to upgrade ``setuptools`` to get this to work correctly:
  196. ``pip install setuptools --upgrade``.
  197. - You may encounter errors indicating that ``ffi.h`` is missing, even with
  198. ``libffi-devel`` installed. If you do, copy the ``.h`` files:
  199. ``cp /usr/lib/libffi-3.0.13/include/*.h /usr/include``
  200. - You may need to install libsodium from source in order to install PyNacl. If
  201. you do, you may need to create a symlink to ``libsodium.a`` so ``ld`` can find
  202. it: ``ln -s /usr/local/lib/libsodium.a /usr/lib/libsodium.a``
  203. Troubleshooting
  204. ===============
  205. Troubleshooting Installation
  206. ----------------------------
  207. Synapse requires pip 1.7 or later, so if your OS provides too old a version and
  208. you get errors about ``error: no such option: --process-dependency-links`` you
  209. may need to manually upgrade it::
  210. sudo pip install --upgrade pip
  211. If pip crashes mid-installation for reason (e.g. lost terminal), pip may
  212. refuse to run until you remove the temporary installation directory it
  213. created. To reset the installation::
  214. rm -rf /tmp/pip_install_matrix
  215. pip seems to leak *lots* of memory during installation. For instance, a Linux
  216. host with 512MB of RAM may run out of memory whilst installing Twisted. If this
  217. happens, you will have to individually install the dependencies which are
  218. failing, e.g.::
  219. pip install twisted
  220. On OS X, if you encounter clang: error: unknown argument: '-mno-fused-madd' you
  221. will need to export CFLAGS=-Qunused-arguments.
  222. Troubleshooting Running
  223. -----------------------
  224. If synapse fails with ``missing "sodium.h"`` crypto errors, you may need
  225. to manually upgrade PyNaCL, as synapse uses NaCl (http://nacl.cr.yp.to/) for
  226. encryption and digital signatures.
  227. Unfortunately PyNACL currently has a few issues
  228. (https://github.com/pyca/pynacl/issues/53) and
  229. (https://github.com/pyca/pynacl/issues/79) that mean it may not install
  230. correctly, causing all tests to fail with errors about missing "sodium.h". To
  231. fix try re-installing from PyPI or directly from
  232. (https://github.com/pyca/pynacl)::
  233. # Install from PyPI
  234. pip install --user --upgrade --force pynacl
  235. # Install from github
  236. pip install --user https://github.com/pyca/pynacl/tarball/master
  237. ArchLinux
  238. ~~~~~~~~~
  239. If running `$ synctl start` fails with 'returned non-zero exit status 1',
  240. you will need to explicitly call Python2.7 - either running as::
  241. python2.7 -m synapse.app.homeserver --daemonize -c homeserver.yaml
  242. ...or by editing synctl with the correct python executable.
  243. Synapse Development
  244. ===================
  245. To check out a synapse for development, clone the git repo into a working
  246. directory of your choice::
  247. git clone https://github.com/matrix-org/synapse.git
  248. cd synapse
  249. Synapse has a number of external dependencies, that are easiest
  250. to install using pip and a virtualenv::
  251. virtualenv env
  252. source env/bin/activate
  253. python synapse/python_dependencies.py | xargs -n1 pip install
  254. pip install setuptools_trial mock
  255. This will run a process of downloading and installing all the needed
  256. dependencies into a virtual env.
  257. Once this is done, you may wish to run Synapse's unit tests, to
  258. check that everything is installed as it should be::
  259. python setup.py test
  260. This should end with a 'PASSED' result::
  261. Ran 143 tests in 0.601s
  262. PASSED (successes=143)
  263. Upgrading an existing Synapse
  264. =============================
  265. IMPORTANT: Before upgrading an existing synapse to a new version, please
  266. refer to UPGRADE.rst for any additional instructions.
  267. Otherwise, simply re-install the new codebase over the current one - e.g.
  268. by ``pip install --process-dependency-links
  269. https://github.com/matrix-org/synapse/tarball/master``
  270. if using pip, or by ``git pull`` if running off a git working copy.
  271. Setting up Federation
  272. =====================
  273. In order for other homeservers to send messages to your server, it will need to
  274. be publicly visible on the internet, and they will need to know its host name.
  275. You have two choices here, which will influence the form of your Matrix user
  276. IDs:
  277. 1) Use the machine's own hostname as available on public DNS in the form of
  278. its A or AAAA records. This is easier to set up initially, perhaps for
  279. testing, but lacks the flexibility of SRV.
  280. 2) Set up a SRV record for your domain name. This requires you create a SRV
  281. record in DNS, but gives the flexibility to run the server on your own
  282. choice of TCP port, on a machine that might not be the same name as the
  283. domain name.
  284. For the first form, simply pass the required hostname (of the machine) as the
  285. --server-name parameter::
  286. python -m synapse.app.homeserver \
  287. --server-name machine.my.domain.name \
  288. --config-path homeserver.yaml \
  289. --generate-config
  290. python -m synapse.app.homeserver --config-path homeserver.yaml
  291. Alternatively, you can run ``synctl start`` to guide you through the process.
  292. For the second form, first create your SRV record and publish it in DNS. This
  293. needs to be named _matrix._tcp.YOURDOMAIN, and point at at least one hostname
  294. and port where the server is running. (At the current time synapse does not
  295. support clustering multiple servers into a single logical homeserver). The DNS
  296. record would then look something like::
  297. $ dig -t srv _matrix._tcp.machine.my.domain.name
  298. _matrix._tcp IN SRV 10 0 8448 machine.my.domain.name.
  299. At this point, you should then run the homeserver with the hostname of this
  300. SRV record, as that is the name other machines will expect it to have::
  301. python -m synapse.app.homeserver \
  302. --server-name YOURDOMAIN \
  303. --config-path homeserver.yaml \
  304. --generate-config
  305. python -m synapse.app.homeserver --config-path homeserver.yaml
  306. You may additionally want to pass one or more "-v" options, in order to
  307. increase the verbosity of logging output; at least for initial testing.
  308. Running a Demo Federation of Synapses
  309. -------------------------------------
  310. If you want to get up and running quickly with a trio of homeservers in a
  311. private federation (``localhost:8080``, ``localhost:8081`` and
  312. ``localhost:8082``) which you can then access through the webclient running at
  313. http://localhost:8080. Simply run::
  314. demo/start.sh
  315. This is mainly useful just for development purposes.
  316. Running The Demo Web Client
  317. ===========================
  318. The homeserver runs a web client by default at https://localhost:8448/.
  319. If this is the first time you have used the client from that browser (it uses
  320. HTML5 local storage to remember its config), you will need to log in to your
  321. account. If you don't yet have an account, because you've just started the
  322. homeserver for the first time, then you'll need to register one.
  323. Registering A New Account
  324. -------------------------
  325. Your new user name will be formed partly from the hostname your server is
  326. running as, and partly from a localpart you specify when you create the
  327. account. Your name will take the form of::
  328. @localpart:my.domain.here
  329. (pronounced "at localpart on my dot domain dot here")
  330. Specify your desired localpart in the topmost box of the "Register for an
  331. account" form, and click the "Register" button. Hostnames can contain ports if
  332. required due to lack of SRV records (e.g. @matthew:localhost:8448 on an
  333. internal synapse sandbox running on localhost).
  334. If registration fails, you may need to enable it in the homeserver (see
  335. `Synapse Installation`_ above)
  336. Logging In To An Existing Account
  337. ---------------------------------
  338. Just enter the ``@localpart:my.domain.here`` Matrix user ID and password into
  339. the form and click the Login button.
  340. Identity Servers
  341. ================
  342. The job of authenticating 3PIDs and tracking which 3PIDs are associated with a
  343. given Matrix user is very security-sensitive, as there is obvious risk of spam
  344. if it is too easy to sign up for Matrix accounts or harvest 3PID data.
  345. Meanwhile the job of publishing the end-to-end encryption public keys for
  346. Matrix users is also very security-sensitive for similar reasons.
  347. Therefore the role of managing trusted identity in the Matrix ecosystem is
  348. farmed out to a cluster of known trusted ecosystem partners, who run 'Matrix
  349. Identity Servers' such as ``sydent``, whose role is purely to authenticate and
  350. track 3PID logins and publish end-user public keys.
  351. It's currently early days for identity servers as Matrix is not yet using 3PIDs
  352. as the primary means of identity and E2E encryption is not complete. As such,
  353. we are running a single identity server (https://matrix.org) at the current
  354. time.
  355. Where's the spec?!
  356. ==================
  357. The source of the matrix spec lives at https://github.com/matrix-org/matrix-doc.
  358. A recent HTML snapshot of this lives at http://matrix.org/docs/spec
  359. Building Internal API Documentation
  360. ===================================
  361. Before building internal API documentation install sphinx and
  362. sphinxcontrib-napoleon::
  363. pip install sphinx
  364. pip install sphinxcontrib-napoleon
  365. Building internal API documentation::
  366. python setup.py build_sphinx