matrix(の実装であるsynapse)をFreeBSDで動かす

気がつけば2週間。
decentralizedなコミュニケーションツール “matrix” の実装である “synapse” をFreeBSDで動かした。
メモを見ながら書くので抜けている可能性大。

How To: Install a Matrix.org Home Serverを参考に。

まずインストールする。
実はpkg install py27-matrix-synapseで入るらしいが、試してない。bleeding-edgeを試したいから、ということにしておこう。

# pkg install python27 py27-pip py27-pillow py27-sqlite3 py27-lxml ca_root_nss
# pip install incremental cffi
# pip install https://github.com/matrix-org/synapse/tarball/master

設定ファイルを作る。

# mkdir /path/to/homeserver
# cd /path/to/homeserver
# python2.7 -m synapse.app.homeserver
            --server-name synapse.example.org \
            --config-path homeserver.yaml \
            --generate-config \
            --report-stats=no

homeserver.yamlを好きにいじる。server_nameは一度起動すると変更できない(データベースを初期化しないといけない)ので、ここでしっかり決めておく。
ちなみにFederation(外部matrixサーバとの相互接続)するなら、外部から到達可能な名前にするか、DNSのSRVレコードを書く必要がある。

うちはreverse proxyの後ろにいるので、no_tls: Trueにした。
tls_fingerprintsの値については、外向けの証明書のコピーを置いてtls_certificate_pathに指定する(tls_fingerprintsは空リストのまま)。
homeserver.yamlのコメントにも書いてあるとおり、以下でも取得できる。FreeBSDにbase64はないので、openssl sha256までで止めておけばいい。(どうせbase64で出てくる)
Let’s Encryptを使っていると期限が短いので、この周りも含めて自動更新するスクリプトでも書いておいたほうがいい。

# You can calculate a fingerprint from a given TLS listener via:
# openssl s_client -connect $host:$port < /dev/null 2> /dev/null |
#   openssl x509 -outform DER | openssl sha256 -binary | base64 | tr -d '='
# or by checking matrix.org/federationtester/api/report?server_name=$host

これで設定おわり。起動する。

# /usr/local/bin/synctl start /path/to/homeserver/homeserver.yaml

初期ユーザを作る。
register_new_matrix_userコマンドは、homeserver.yamlに書いてあるsecretを使ってユーザを作るようだ。
reverse proxyの後ろにいる場合、末尾のhttps://localhost:8448のをhttp://localhost:8008などに読み替える必要があるので注意。

# /usr/local/bin/register_new_matrix_user -c homeserver.yaml https://localhost:8448

あとはブラウザでアクセスしたり、riot.imからアクセスしたりする。
Federationは http://matrix.org/federationtester/api/report?server_name=サーバ名 でチェックできる。ConnectionReports→サーバのIPアドレス→Checks→AllChecksOKがtrueならばっちり。

あとはbotを書くなりなんなりと。

早速zabbixからの監視はbot送りにして、discordとのブリッジをさせてみたが、特に大きな問題はなく動いている。
「特に大きな問題はなく」のところはまた今度。きっとそのうち。