8

使用去中心化通讯协议Matrix

 3 years ago
source link: https://blog.eastonman.com/blog/2020/10/matrix/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

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

Matrix

Matrix是一个去中心化的通讯协议,功能丰富,架构健壮。与客户端直接通讯的服务端可以自行部署,无需担心在国内无法访问。

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

Synapse

Synapse是一个最常用的matrix服务端软件,基于python开发,由matrix官方维护,是我们部署matrix服务端的最佳选择。

因为python版本多环境复杂,我们首先考虑使用Docker进行部署,官方的docker 镜像位于Docker Hub

,直接使用问题也不大,有一点小问题我们很快就会讲到。

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

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

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:

然后我们docker-compose up -d就可以开始运行了。

方便起见我把tls放在反向代理上,就不在synapse中配置cert了,印象中Matrix要求使用tls也就是https用于通信,所以一个有效的证书是避不开的了。对于证书我推荐使用Acme.sh,文档见链接。TLS的配置我就不赘述了。

此处需要注意的是反向代理需要一些额外的配置,官方已经有建议,此处列举一个我使用的HAProxy配置。

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

Client

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

所有的客户端见官方页面

目前开发得比较完善的客户端是Element,原名Riot.im。

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

Known Bugs

目前其实就算是element开发得也不是很完善,然后发布版本缺乏测试,经常引入新bug。

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

Conclusion

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

如果是真正想要体验良好的聊天体验的话还是建议使用Telegram

浏览量: 362

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK