使用 Dendrite 搭建 Matrix Homeserver

Dendrite 是 Matrix 协议的一个开源实现,旨在提供一个轻量级、可扩展且易于部署的 Matrix 服务器。Matrix 是一个开源的通信协议和开放标准,用于构建去中心化的实时通信系统。它支持多种类型的通信,包括文本消息、语音通话、视频通话以及文件共享等。Dendrite 作为 Matrix 生态系统中的一个重要组件,为开发者和运营商提供了构建自己通信服务的工具。

1. 构建 dendrite

1
2
3
4
5

git clone [email protected]:matrix-org/dendrite.git
cd dendrite
go build -o bin/ ./cmd/...

2. 准备数据库

2.1. 创建角色

创建一个 Dendrite 可用于连接数据库的角色,并在出现提示时选择一个新密码。

1
2
3

sudo -u postgres createuser -P dendrite

2.2. 创建数据库

使用上面的角色创建数据库 dendrite

1
2
3

sudo -u postgres createdb -O dendrite -E UTF-8 dendrite

2.3. 连接字符串

postgresql://dendrite:pass@localhost/dendrite?sslmode=disable

3. 生成签名密钥

所有 Matrix 主服务器都需要一个签名私钥,用于验证联合请求和事件。

该 generate-keys 实用程序可用于生成私钥。假设 Dendrite 是使用 构建的go build -o bin/ ./cmd/...,您应该在 bin 文件夹中找到 generate-keys 该实用程序。

要生成 Matrix 签名私钥:

1
2
3
# Generate a Matrix signing key for federation (required)
$ ./bin/generate-keys --private-key matrix_key.pem

生成的 matrix_key.pem 文件是您的新签名密钥。

重要提醒:
您必须将此密钥视为高度敏感和私密的密钥,因此切勿与任何人共享。任何人都不得以任何理由向您索要此密钥,即使是为了调试有问题的 Dendrite 服务器。
确保安全备份此密钥。如果您将来想在同一域名上重新安装 Dendrite 或任何其他 Matrix 主服务器,则可能需要此密钥。如果您丢失此密钥,则可能无法加入房间。

4. 配置 Dendrite

YAML 配置文件用于配置 Dendrite。Dendrite 存储库的顶层有一个示例配置文件:

dendrite-sample.yaml
您需要复制示例,例如 dendrite.yaml。

1
2
3
4
5

# Copy and modify the config file - you'll need to set a server name and paths to the keys
# at the very least, along with setting up the database connection strings.
$ cp dendrite-sample.yaml dendrite.yaml

然后根据您的安装进行定制。至少,您需要填充以下部分:

  1. 服务器名称

首先,您需要配置 Matrix 主服务器的服务器名称。该名称必须与您在配置域名委派时选择的域名相匹配。

在 global 部分中,将设置 server_name 为您的委托域名:

1
2
3
4
5

global:
# ...
server_name: example.com

  1. 服务器签名密钥

接下来,您应该告诉 Dendrite 在哪里找到您的服务器签名密钥。

在 global 部分中,将 设置 private_key 为服务器签名密钥的路径:

1
2
3
global:
# ...
private_key: /path/to/matrix_key.pem
  1. 使用全局连接池的数据库连接

如果要对单个 PostgreSQL 数据库使用单个连接池,则必须取消注释并配置 database 该 global 部分内的部分:

1
2
3
4
5
6
7
global:
# ...
database:
connection_string: postgres://user:pass@hostname/database?sslmode=disable
max_open_conns: 90
max_idle_conns: 5
conn_max_lifetime: -1

5. 启动 dendrite

完成所有准备和安装步骤后,您可以通过执行二进制文件来开始 Dendrite 部署 dendrite:

1
2
3
4
5
6
7
8
9
10
11
12

# Generate a self-signed certificate (optional, but a valid TLS certificate is normally
# needed for Matrix federation/clients to work properly!)
$ ./bin/generate-keys --tls-cert server.crt --tls-key server.key

# Build and run the server:
$ ./bin/dendrite --tls-cert server.crt --tls-key server.key --config dendrite.yaml

# Create an user account (add -admin for an admin user).
# Specify the localpart only, e.g. 'alice' for '@alice:domain.com'
$ ./bin/create-account --config dendrite.yaml --username alice

默认情况下,Dendrite 将在端口 8008 上监听 HTTP。如果要更改 Dendrite 监听的地址或端口,可以使用-http-bind-address 和-https-bind-address 命令行参数:

1
2
3
4
./bin/dendrite --tls-cert server.crt --tls-key server.key \
-really-enable-open-registration \
--config dendrite.yaml \

因为是测试运行dendrite,所以带了really-enable-open-registration选项,省去一些复杂的配置。
在正式环境中,不要这样做,尽量把相应的配置补上,将really-enable-open-registration选项去除掉。

让dendrite监听在指定端口的方法如下:

1
2
3
4
5
6
7

./bin/dendrite --tls-cert server.crt --tls-key server.key \
-really-enable-open-registration \
--config dendrite.yaml \
-http-bind-address 1.2.3.4:12345 \
-https-bind-address 1.2.3.4:54321

安装 matrix 客户端测试

使用 element web 进行验证

浏览器打开 https://app.element.io

修改 Homeserver 为http://localhost:8008 点击注册

注册成功后登陆, 创建房间。

update on 2024/08/10:

也可本地编译element-web,通过本地的element-web作为客户端访问Matrix Homeserver

troubleshooting

问题 1:

1
FATA[0000] Shared secret registration is not enabled, enable it by setting a shared secret in the config: 'client_api.registration_shared_secret'

6. 参考文档

Docke 搭建 Matrix Synapse Homeserver

Building/Installing Dendrite

Preparing database storage

Configuring Dendrite

matrix-js-sdk

使用 Dendrite 搭建 Matrix Homeserver

https://pengtech.net/matrix/setup_matrix_homeserver.html

作者

鹏叔

发布于

2024-07-23

更新于

2024-08-10

许可协议

评论