<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>去中心化 | Easton Man's Blog</title>
	<atom:link href="https://blog.eastonman.com/blog/tag/%E5%8E%BB%E4%B8%AD%E5%BF%83%E5%8C%96/feed/" rel="self" type="application/rss+xml" />
	<link>https://blog.eastonman.com</link>
	<description>临渊羡鱼，不如退而结网</description>
	<lastBuildDate>Tue, 03 Aug 2021 03:06:06 +0000</lastBuildDate>
	<language>zh-Hans</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.8.5</generator>

<image>
	<url>https://blog.eastonman.com/wp-content/uploads/2021/02/cropped-Logo-e1613298891313-32x32.png</url>
	<title>去中心化 | Easton Man's Blog</title>
	<link>https://blog.eastonman.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>去中心化通讯协议 Matrix</title>
		<link>https://blog.eastonman.com/blog/2020/10/matrix/</link>
					<comments>https://blog.eastonman.com/blog/2020/10/matrix/#comments</comments>
		
		<dc:creator><![CDATA[Easton Man]]></dc:creator>
		<pubDate>Wed, 07 Oct 2020 01:39:42 +0000</pubDate>
				<category><![CDATA[技术]]></category>
		<category><![CDATA[Distributed]]></category>
		<category><![CDATA[Matrix]]></category>
		<category><![CDATA[Synapse]]></category>
		<category><![CDATA[去中心化]]></category>
		<guid isPermaLink="false">https://blog.eastonman.com/?p=206</guid>

					<description><![CDATA[<p>在现在这个时代，所有的聊天软件不可避免地会遭到审查，那么如何能够将个人隐私牢牢地掌握在自己手中呢？Matrix就是一个新兴的去中心化通讯协议</p>
The post <a href="https://blog.eastonman.com/blog/2020/10/matrix/">去中心化通讯协议 Matrix</a> first appeared on <a href="https://blog.eastonman.com">Easton Man's Blog</a>.]]></description>
										<content:encoded><![CDATA[<p class="wpwc-reading-time">预计阅读时间： 5 分钟</p>
<h2 class="wp-block-heading">写在前面</h2>



<p>在现在这个时代，所有的聊天软件不可避免地会遭到审查，那么如何能够将个人隐私牢牢地掌握在自己手中呢？有不少人使用较为安全的支持e2e加密的软件Telegram，但是国内无法正常访问，本文教你如何搭建类似的e2e加密的国内可以正常使用的聊天软件——Matrix。</p>



<h2 class="wp-block-heading">Matrix</h2>



<p><a href="https://matrix.org/">Matrix</a>是一个去中心化的通讯协议，功能丰富，架构健壮。与客户端直接通讯的服务端可以自行部署，无需担心在国内无法访问。</p>



<figure class="wp-block-image size-large"><img fetchpriority="high" decoding="async" width="875" height="674" src="https://blog.eastonman.com/wp-content/uploads/2020/09/image.png" alt="" class="wp-image-208" srcset="https://blog.eastonman.com/wp-content/uploads/2020/09/image.png 875w, https://blog.eastonman.com/wp-content/uploads/2020/09/image-300x231.png 300w, https://blog.eastonman.com/wp-content/uploads/2020/09/image-768x592.png 768w" sizes="(max-width: 875px) 100vw, 875px" /></figure>



<p>由于使用的是http api，所以部署非常方便，第三方客户端的开发也十分便捷。目前matrix生态十分丰富，还有不少bot、bridge等增加它的功能。</p>



<h2 class="wp-block-heading">Synapse</h2>



<p>Synapse是一个最常用的matrix服务端软件，使用Python编写，由matrix官方维护，是我们部署matrix服务端的最佳选择。</p>



<p>Matrix官方还有开发一个Golang的版本，但是仍在Beta阶段，短期内不建议部署。</p>



<p>因为Python版本多环境复杂，我们首先考虑使用Docker进行部署，官方的docker 镜像位于<a href="https://hub.docker.com/r/matrixdotorg/synapse">Docker Hub</a></p>



<p>，直接使用问题也不大，有一点小问题我们很快就会讲到。</p>



<p>因为环境变量较多，我们自然是选择docker-compose 来部署和管理，docker-compose的安装教程位于<a href="https://docs.docker.com/compose/install/">https://docs.docker.com/compose/install/</a>，没有安装的可以按照官网提示安装。</p>



<p>synapse官方有example位于<a href="https://github.com/matrix-org/synapse/blob/master/contrib/docker/docker-compose.yml">https://github.com/matrix-org/synapse/blob/master/contrib/docker/docker-compose.yml</a>，使用了traefic作为反向代理，不太符合我的需求，在此提供一份我使用的compose file</p>



<pre class="EnlighterJSRAW" data-enlighter-language="yaml" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">version: '2.4'

volumes:
  db:
    name: synapse_db

networks:
  synapse:
    name: synapse
    ipam:
      config:
        - subnet: 172.23.1.0/24

services:

  synapse:
    #image: reg.techminer.org/library/synapse:latest
    image: matrixdotorg/synapse:latest
    container_name: synapse
    # Since synapse does not retry to connect to the database, restart upon
    # failure
    restart: unless-stopped
    # See the readme for a full documentation of the environment settings
    environment:
      - SYNAPSE_CONFIG_PATH=/data/homeserver.yaml
    networks:
      synapse:
        ipv4_address: 172.23.1.2
    volumes:
      - ./files:/data
    depends_on:
      - db

  db:
    image: postgres:12-alpine
    container_name: synapse_db
    restart: unless-stopped
    # Change that password, of course!
    environment:
      - POSTGRES_USER=synapse
      - POSTGRES_PASSWORD=MammalMatrixPretty
      # ensure the database gets created correctly
      # https://github.com/matrix-org/synapse/blob/master/docs/postgres.md#set-up-database
      - POSTGRES_INITDB_ARGS=--encoding=UTF-8 --lc-collate=C --lc-ctype=C
    volumes:
      - db:/var/lib/postgresql/data
    networks:
      synapse:</pre>



<p>然后我们<code>docker-compose up -d</code>就可以开始运行了。</p>



<p>方便起见我把tls放在反向代理上，就不在synapse中配置cert了，印象中Matrix要求使用tls也就是https用于通信，所以一个有效的证书是避不开的了。对于证书我推荐使用<a href="https://github.com/acmesh-official/acme.sh">Acme.sh</a>，文档见链接。TLS的配置我就不赘述了。</p>



<p>此处需要注意的是反向代理需要一些额外的配置，官方已经有<a href="https://github.com/matrix-org/synapse/blob/develop/docs/reverse_proxy.md">建议</a>，此处列举一个我使用的HAProxy配置。</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">frontend https
  bind :::443 v4v6 ssl crt /etc/ssl/haproxy/ strict-sni alpn h2,http/1.1

  # Matrix client traffic
  acl matrix-host hdr(host) -i matrix.example.com
  acl matrix-path path_beg /_matrix
  acl matrix-path path_beg /_synapse/client

  use_backend matrix if matrix-host matrix-path

frontend matrix-federation
  bind :::8448 v4v6 ssl crt /etc/ssl/haproxy/synapse.pem alpn h2,http/1.1
  default_backend matrix

backend matrix
  server matrix 127.0.0.1:8008</pre>



<h2 class="wp-block-heading">Client</h2>



<p>由于Matrix是一个开放的协议，任何人都可以为它编写客户端。</p>



<p>所有的客户端见<a href="https://matrix.org/clients/">官方页面</a>。</p>



<p>目前开发得比较完善的客户端是<a href="https://element.io/">Element</a>，原名Riot.im。</p>



<p></p>



<p>至此，我们已经安装好服务端和客户端了，可以按照指示创建一个新用户并愉快地开始聊天了。</p>



<h2 class="wp-block-heading">Known Bugs</h2>



<p><s>Python的Twisted库Bug导致发注册邮件等无法使用TLSv1.2，所以如果使用的邮件服务商不支持旧的TLS版本，那么还需要更改Dockerfile使用nightly的Twisted。</s> 2021-08-03 Update: Twisted库已经修复了这个Bug。</p>



<p>目前其实就算是Element开发得也不是很完善，然后发布版本缺乏测试，经常引入新bug。</p>



<p>还有就是synapse这个服务端是用Python写的，性能非常抱歉，尤其是相当消耗硬盘I/O，3000iops的ssd都能给占满活动时间。</p>



<h2 class="wp-block-heading">Conclusion</h2>



<p>其实主要还是感受一下这样的去中心化即时聊天形式，matrix协议使用的去中心化方式带来的federation查询时主要消耗性能的地方，尤其是对于曾经存在过的HomeServer，Synapse没有做区分，导致不停地重复查询已经不可能查询到的federation API。</p>



<p>如果是真正想要体验良好的聊天体验的话还是建议使用<a href="https://telegram.org/">Telegram</a>。</p>The post <a href="https://blog.eastonman.com/blog/2020/10/matrix/">去中心化通讯协议 Matrix</a> first appeared on <a href="https://blog.eastonman.com">Easton Man's Blog</a>.]]></content:encoded>
					
					<wfw:commentRss>https://blog.eastonman.com/blog/2020/10/matrix/feed/</wfw:commentRss>
			<slash:comments>5</slash:comments>
		
		
			</item>
	</channel>
</rss>
