目录

Mozilla Autopush-rs 部署与配置全攻略

Autopush-rs 是 Mozilla 推送服务的 Rust 实现,采用典型的“生产者-消费者”架构,利用 Rust、Actix 以及键值数据存储。本文基于 autopush-rs 项目配置与 Sunup 客户端集成实践整理,涵盖核心原理、架构图示、详尽配置及部署指南。


1. 架构原理与组件分工

Autopush-rs 采用双端解耦设计,通过 Redis 共享状态,实现从“公网推送请求”到“私有长连接下发”的回路。

1.1 服务组件分工

  1. Autoconnect (WebSocket Server):

    • 角色: 面向客户端(如 Sunup Android 客户端)。
    • 功能: 建立并维护长连接,接收心跳,并将推送消息分发到设备。
    • 默认端口: 8080 (WebSocket), 8081 (内部路由端口)。
  2. Autoendpoint (HTTP API):

    • 角色: 面向应用服务器(如 Mastodon/Matrix 等发送通知的服务)。
    • 功能: 接收标准 WebPush 请求,查询路由信息,并通知 autoconnect 进行推送。
    • 默认端口: 8000
  3. Storage (Redis/Valkey):

    • 功能: 存储注册信息(Routers)和待发消息,并用于可靠性跟踪。

1.2 交互流程图 (Mermaid)


2. 详尽配置选项

配置选项既可以在配置文件中指定,也可以通过环境变量的形式指定。

2.1 Autoconnect 配置

选项环境变量类型默认值描述
actix_max_connectionsAUTOCONNECT__ACTIX_MAX_CONNECTIONS数字Actix Web 服务器的最大并发连接数
actix_workersAUTOCONNECT__ACTIX_WORKERS数字Actix Web 服务器每个绑定地址的工作线程数
auto_ping_intervalAUTOCONNECT__AUTO_PING_INTERVAL数字300向客户端发送 ping 的时间间隔(秒)
auto_ping_timeoutAUTOCONNECT__AUTO_PING_TIMEOUT数字4等待客户端 ping 响应的时间(秒)
crypto_keyAUTOCONNECT__CRYPTO_KEY字符串内部生成用于端点加密的加密密钥 (32 字节 Base64)
db_dsnAUTOCONNECT__DB_DSN字符串后端数据库的数据源名称
endpoint_hostnameAUTOCONNECT__ENDPOINT_HOSTNAME字符串“localhost”生成端点 URL 的公网主机名
endpoint_portAUTOCONNECT__ENDPOINT_PORT数字8082生成端点 URL 的公网端口覆盖值
endpoint_schemeAUTOCONNECT__ENDPOINT_SCHEME字符串“http”生成端点 URL 的方案 (http/https)
hostnameAUTOCONNECT__HOSTNAME字符串机器主机名,用于内部路由注册
human_logsAUTOCONNECT__HUMAN_LOGS布尔值false是否使用人类可读的日志格式
msg_limitAUTOCONNECT__MSG_LIMIT数字1000每个客户端存储的最大消息数
portAUTOCONNECT__PORT数字8080应用程序监听的 WS 端口
router_portAUTOCONNECT__ROUTER_PORT数字8081节点间通信的内部路由端口

2.2 Autoendpoint 配置

注意:部分旧版文档中提到的 AUTOENDPOINT__ 前缀在当前版本中应修改为 AUTOEND__

选项环境变量类型默认值描述
schemeAUTOEND__SCHEME字符串“http”端点 URL 方案
hostAUTOEND__HOST字符串“127.0.0.1”监听主机
portAUTOEND__PORT数字8000监听端口
db_dsnAUTOEND__DB_DSN字符串后端数据库数据源名称
crypto_keysAUTOEND__CRYPTO_KEYS字符串内部生成加密密钥列表 (逗号分隔)
auth_keysAUTOEND__AUTH_KEYS字符串身份验证密钥列表 (逗号分隔)
human_logsAUTOEND__HUMAN_LOGS布尔值false是否使用人类可读日志
max_notification_ttlAUTOEND__MAX_NOTIFICATION_TTL数字2592000通知消息最大生存时间 (秒)

3. 关键配置文件 (TOML 示例)

密钥可通过 openssl rand -base64 32 命令行生成。

3.1 autoconnect.toml

# --- 内部识别 (至关重要) ---
# 此 hostname 会存入 Redis。Autoendpoint 必须能通过此名称访问到本容器。
hostname = "autoconnect" 
port = 8080              
router_port = 8081       

# --- 推送地址生成 (给手机客户端看) ---
endpoint_scheme = "https"
endpoint_hostname = "push-api.example.com"
endpoint_port = 443      

# --- 存储与安全 ---
db_dsn = "redis://valkey:6379"
crypto_key = "[mqCGb8D-N7mqx6iWJov9wm70Us6kA9veeXdb8QUuzLQ=]"

3.2 autoendpoint.toml

host = "0.0.0.0"         
port = 8000              

db_dsn = "redis://valkey:6379"
# 必须与 autoconnect 的 crypto_key 保持一致
crypto_keys = "[mqCGb8D-N7mqx6iWJov9wm70Us6kA9veeXdb8QUuzLQ=]"

scheme = "http"
human_logs = true

4. 部署指南

4.1 Docker Compose 部署

---
# https://github.com/orgs/forkdo/packages?repo_name=autopush-rs
services:
  autoendpoint:
    container_name: autoendpoint
    env_file:
      - path: ./.env
        required: false
    hostname: autoendpoint
    image: ghcr.io/forkdo/autoendpoint:latest
    ports:
      - 8000:8000
    restart: unless-stopped

  autoconnect:
    container_name: autoconnect
    env_file:
      - path: ./.env
        required: false
    hostname: autoconnect
    image: ghcr.io/forkdo/autoconnect:latest
    ports:
      - 8080:8080
    restart: unless-stopped

4.2 环境变量 (.env)

# CUSTOM
TZ=Asia/Shanghai

################################# autoendpoint 对应项 ###############
AUTOEND__HOST="autoendpoint"
AUTOEND__PORT=8000
AUTOEND__DB_DSN="redis://valkey:6379"

# 密钥需为随机 32 字节 Base64
# openssl rand -base64 32
AUTOEND__CRYPTO_KEYS="[CRYPTO_KEY]"
AUTOEND__AUTH_KEYS="[AUTH_KEY]"
AUTOEND__HUMAN_LOGS=true

########################## autoconnect 对应项 ############################
AUTOCONNECT__HOSTNAME="autoconnect"
AUTOCONNECT__PORT=8080
AUTOCONNECT__ROUTER_PORT=8081

# 外部应用可访问的配置
AUTOCONNECT__ENDPOINT_SCHEME="https"
AUTOCONNECT__ENDPOINT_HOSTNAME="push-api.example.com"
AUTOCONNECT__ENDPOINT_PORT=443

AUTOCONNECT__DB_DSN="redis://valkey:6379"
AUTOCONNECT__CRYPTO_KEY="[CRYPTO_KEY]"
AUTOCONNECT__HUMAN_LOGS=true

################################ Valkey ###################################
VALKEY_ARGS=--save 60 1000

4.3 Nginx 反向代理配置

    location / {
        proxy_pass http://127.0.0.1:8000; # 指向 Autoendpoint (API) 或 Autoconnect (WS)
        proxy_http_version 1.1;

        proxy_set_header Host $http_host;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Real-IP $remote_addr;

        proxy_connect_timeout 3m;
        proxy_send_timeout 3m;
        proxy_read_timeout 3m;

        client_max_body_size 0; # Stream request body to backend
    }

5. 部署核心 Checklist

  1. 域名绑定分工:
    • WebSocket 域名 (push.domain.com): 建议指向 autoconnect8080 端口。
    • Push API 域名 (push-api.domain.com): 建议指向 autoendpoint8000 端口。
  2. Hostname 陷阱:
    • autoconnect.toml 里的 hostname 绝对不能127.0.0.1。在 Docker 环境下应填写 autoconnect 容器服务名,否则 autoendpoint 无法通过网络回调。
  3. 密钥同步:
    • crypto_key (connect) 和 crypto_keys (endpoint) 必须完全相同,用于令牌的加密与验证。
  4. 生效触发:
    • 修改配置并重启服务后,客户端必须重新打开并建立一次连接,以覆盖 Redis 中旧的路由信息。

相关链接: