From 2b320c128fc1411654284246f82dd6a4913f64aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johan=20Swetz=C3=A9n?= Date: Thu, 28 Jul 2022 20:50:49 +0200 Subject: [PATCH] Add mautrix syncproxy to wsproxy for Android SMS --- ...iguring-playbook-bridge-mautrix-wsproxy.md | 1 + group_vars/matrix_servers | 6 +++ .../defaults/main.yml | 34 +++++++++++++-- .../tasks/setup_install.yml | 28 ++++++++++++ .../tasks/setup_uninstall.yml | 23 ++++++++++ .../tasks/validate_config.yml | 1 + .../templates/config.yaml.j2 | 10 ++++- ...atrix-mautrix-wsproxy-syncproxy.service.j2 | 43 +++++++++++++++++++ .../systemd/matrix-mautrix-wsproxy.service.j2 | 2 +- 9 files changed, 142 insertions(+), 6 deletions(-) create mode 100644 roles/matrix-bridge-mautrix-wsproxy/templates/systemd/matrix-mautrix-wsproxy-syncproxy.service.j2 diff --git a/docs/configuring-playbook-bridge-mautrix-wsproxy.md b/docs/configuring-playbook-bridge-mautrix-wsproxy.md index 6da96eab..cd8118a7 100644 --- a/docs/configuring-playbook-bridge-mautrix-wsproxy.md +++ b/docs/configuring-playbook-bridge-mautrix-wsproxy.md @@ -10,6 +10,7 @@ Use the following playbook configuration: matrix_mautrix_wsproxy_enabled: true matrix_mautrix_wsproxy_appservice_token: 'random string' matrix_mautrix_wsproxy_homeserver_token: 'random string' +matrix_mautrix_wsproxy_syncproxy_shared_secret: 'random string' ``` diff --git a/group_vars/matrix_servers b/group_vars/matrix_servers index da06e803..dae44e23 100755 --- a/group_vars/matrix_servers +++ b/group_vars/matrix_servers @@ -1848,6 +1848,12 @@ matrix_postgres_additional_databases: | 'password': matrix_mautrix_signal_database_password, }] if (matrix_mautrix_signal_enabled and matrix_mautrix_signal_database_engine == 'postgres' and matrix_mautrix_signal_database_hostname == 'matrix-postgres') else []) + + ([{ + 'name': matrix_mautrix_wsproxy_syncproxy_database_name, + 'username': matrix_mautrix_wsproxy_syncproxy_database_username, + 'password': matrix_mautrix_wsproxy_syncproxy_database_password, + }] if (matrix_mautrix_wsproxy_enabled and matrix_mautrix_wsproxy_syncproxy_database_engine == 'postgres' and matrix_mautrix_wsproxy_syncproxy_database_hostname == 'matrix-postgres') else []) + + ([{ 'name': matrix_mautrix_telegram_database_name, 'username': matrix_mautrix_telegram_database_username, diff --git a/roles/matrix-bridge-mautrix-wsproxy/defaults/main.yml b/roles/matrix-bridge-mautrix-wsproxy/defaults/main.yml index 57234ef5..94602d80 100644 --- a/roles/matrix-bridge-mautrix-wsproxy/defaults/main.yml +++ b/roles/matrix-bridge-mautrix-wsproxy/defaults/main.yml @@ -33,7 +33,7 @@ matrix_mautrix_wsproxy_systemd_wanted_services_list: [] matrix_mautrix_wsproxy_appservice_token: '' matrix_mautrix_wsproxy_homeserver_token: '' -matrix_mautrix_wsproxy_appservice_bot_username: imessagebot +matrix_mautrix_wsproxy_appservice_bot_username: androidsmsbot # Default mautrix-wsproxy configuration template which covers the generic use case. # You can customize it by controlling the various variables inside it. @@ -58,7 +58,7 @@ matrix_mautrix_wsproxy_configuration_extension: "{{ matrix_mautrix_wsproxy_confi matrix_mautrix_wsproxy_configuration: "{{ matrix_mautrix_wsproxy_configuration_yaml|from_yaml|combine(matrix_mautrix_wsproxy_configuration_extension, recursive=True) }}" matrix_mautrix_wsproxy_registration_yaml: | - id: imessage + id: androidsms url: {{ matrix_mautrix_wsproxy_appservice_address }} as_token: "{{ matrix_mautrix_wsproxy_appservice_token }}" hs_token: "{{ matrix_mautrix_wsproxy_homeserver_token }}" @@ -66,9 +66,37 @@ matrix_mautrix_wsproxy_registration_yaml: | rate_limited: false namespaces: users: - - regex: '@imessage_.+:{{ matrix_mautrix_wsproxy_homeserver_domain|regex_escape }}$' + - regex: '@androidsms_.+:{{ matrix_mautrix_wsproxy_homeserver_domain|regex_escape }}$' exclusive: true - exclusive: true regex: '^@{{ matrix_mautrix_wsproxy_appservice_bot_username|regex_escape }}:{{ matrix_mautrix_wsproxy_homeserver_domain|regex_escape }}$' matrix_mautrix_wsproxy_registration: "{{ matrix_mautrix_wsproxy_registration_yaml|from_yaml }}" + +# Syncproxy-related configuration fields +matrix_mautrix_wsproxy_syncproxy_version: latest +# See: https://mau.dev/mautrix/wsproxy/container_registry +matrix_mautrix_wsproxy_syncproxy_docker_image: "dock.mau.dev/mautrix/syncproxy:{{ matrix_mautrix_wsproxy_syncproxy_version }}" +matrix_mautrix_wsproxy_syncproxy_docker_image_force_pull: "{{ matrix_mautrix_wsproxy_syncproxy_docker_image.endswith(':latest') }}" +matrix_mautrix_wsproxy_syncproxy_container_extra_arguments: [] + +matrix_mautrix_wsproxy_syncproxy_systemd_required_services_list: ['docker.service', 'matrix-mautrix-wsproxy.service'] +matrix_mautrix_wsproxy_syncproxy_systemd_wanted_services_list: [] + +matrix_mautrix_wsproxy_syncproxy_shared_secret: '' +matrix_mautrix_wsproxy_syncproxy_port: 29332 +matrix_mautrix_wsproxy_syncproxy_appservice_address: "http://matrix-mautrix-wsproxy-syncproxy:{{ matrix_mautrix_wsproxy_syncproxy_port }}" + +# Database-related configuration fields +# +# This bridge supports Postgres and SQLite. +# +matrix_mautrix_wsproxy_syncproxy_database_engine: 'postgres' + +matrix_mautrix_wsproxy_syncproxy_database_username: 'matrix_mautrix_wsproxy_syncproxy' +matrix_mautrix_wsproxy_syncproxy_database_password: 'some-password' +matrix_mautrix_wsproxy_syncproxy_database_hostname: 'matrix-postgres' +matrix_mautrix_wsproxy_syncproxy_database_port: 5432 +matrix_mautrix_wsproxy_syncproxy_database_name: 'matrix_mautrix_wsproxy_syncproxy' + +matrix_mautrix_signal_wsproxy_syncproxy_connection_string: 'postgres://{{ matrix_mautrix_wsproxy_syncproxy_database_username }}:{{ matrix_mautrix_wsproxy_syncproxy_database_password }}@{{ matrix_mautrix_wsproxy_syncproxy_database_hostname }}:{{ matrix_mautrix_wsproxy_syncproxy_database_port }}/{{ matrix_mautrix_wsproxy_syncproxy_database_name }}' \ No newline at end of file diff --git a/roles/matrix-bridge-mautrix-wsproxy/tasks/setup_install.yml b/roles/matrix-bridge-mautrix-wsproxy/tasks/setup_install.yml index 268665ad..aab1d3c5 100644 --- a/roles/matrix-bridge-mautrix-wsproxy/tasks/setup_install.yml +++ b/roles/matrix-bridge-mautrix-wsproxy/tasks/setup_install.yml @@ -11,6 +11,9 @@ - ansible.builtin.set_fact: matrix_mautrix_wsproxy_requires_restart: false +- ansible.builtin.set_fact: + matrix_mautrix_wsproxy_syncproxy_requires_restart: false + - name: Ensure Mautrix wsproxy image is pulled docker_image: name: "{{ matrix_mautrix_wsproxy_docker_image }}" @@ -18,6 +21,13 @@ force_source: "{{ matrix_mautrix_wsproxy_docker_image_force_pull if ansible_version.major > 2 or ansible_version.minor >= 8 else omit }}" force: "{{ omit if ansible_version.major > 2 or ansible_version.minor >= 8 else matrix_mautrix_wsproxy_docker_image_force_pull }}" +- name: Ensure Mautrix syncproxy image is pulled + docker_image: + name: "{{ matrix_mautrix_wsproxy_syncproxy_docker_image }}" + source: "{{ 'pull' if ansible_version.major > 2 or ansible_version.minor > 7 else omit }}" + force_source: "{{ matrix_mautrix_wsproxy_syncproxy_docker_image_force_pull if ansible_version.major > 2 or ansible_version.minor >= 8 else omit }}" + force: "{{ omit if ansible_version.major > 2 or ansible_version.minor >= 8 else matrix_mautrix_wsproxy_syncproxy_docker_image_force_pull }}" + - name: Ensure Mautrix wsproxy paths exists ansible.builtin.file: path: "{{ item }}" @@ -68,3 +78,21 @@ name: "matrix-mautrix-wsproxy.service" state: restarted when: "matrix_mautrix_wsproxy_requires_restart|bool" + +- name: Ensure matrix-mautrix-wsproxy-syncproxy.service installed + ansible.builtin.template: + src: "{{ role_path }}/templates/systemd/matrix-mautrix-wsproxy-syncproxy.service.j2" + dest: "{{ matrix_systemd_path }}/matrix-mautrix-wsproxy.service" + mode: 0644 + register: matrix_mautrix_wsproxy_syncproxy_systemd_service_result + +- name: Ensure systemd reloaded after matrix-mautrix-wsproxy-syncproxy.service installation + ansible.builtin.service: + daemon_reload: true + when: "matrix_mautrix_wsproxy_syncproxy_systemd_service_result.changed" + +- name: Ensure matrix-mautrix-wsproxy-syncproxy.service restarted, if necessary + ansible.builtin.service: + name: "matrix-mautrix-wsproxy-syncproxy.service" + state: restarted + when: "matrix_mautrix_wsproxy_syncproxy_requires_restart|bool" diff --git a/roles/matrix-bridge-mautrix-wsproxy/tasks/setup_uninstall.yml b/roles/matrix-bridge-mautrix-wsproxy/tasks/setup_uninstall.yml index 069d06d4..7c693ad1 100644 --- a/roles/matrix-bridge-mautrix-wsproxy/tasks/setup_uninstall.yml +++ b/roles/matrix-bridge-mautrix-wsproxy/tasks/setup_uninstall.yml @@ -22,3 +22,26 @@ ansible.builtin.service: daemon_reload: true when: "matrix_mautrix_wsproxy_service_stat.stat.exists" + +- name: Check existence of matrix-mautrix-wsproxy-syncproxy service + ansible.builtin.stat: + path: "{{ matrix_systemd_path }}/matrix-mautrix-wsproxy-syncproxy.service" + register: matrix_mautrix_wsproxy_syncproxy_service_stat + +- name: Ensure matrix-mautrix-wsproxy-syncproxy is stopped + ansible.builtin.service: + name: matrix-mautrix-wsproxy-syncproxy + state: stopped + daemon_reload: true + when: "matrix_mautrix_wsproxy_syncproxy_service_stat.stat.exists" + +- name: Ensure matrix-mautrix-wsproxy-syncproxy.service doesn't exist + ansible.builtin.file: + path: "{{ matrix_systemd_path }}/matrix-mautrix-wsproxy-syncproxy.service" + state: absent + when: "matrix_mautrix_wsproxy_syncproxy_service_stat.stat.exists" + +- name: Ensure systemd reloaded after matrix-mautrix-wsproxy-syncproxy.service removal + ansible.builtin.service: + daemon_reload: true + when: "matrix_mautrix_wsproxy_syncproxy_service_stat.stat.exists" diff --git a/roles/matrix-bridge-mautrix-wsproxy/tasks/validate_config.yml b/roles/matrix-bridge-mautrix-wsproxy/tasks/validate_config.yml index 315bff1f..1a4ba894 100644 --- a/roles/matrix-bridge-mautrix-wsproxy/tasks/validate_config.yml +++ b/roles/matrix-bridge-mautrix-wsproxy/tasks/validate_config.yml @@ -8,3 +8,4 @@ with_items: - "matrix_mautrix_wsproxy_appservice_token" - "matrix_mautrix_wsproxy_homeserver_token" + - "matrix_mautrix_wsproxy_syncproxy_shared_secret" diff --git a/roles/matrix-bridge-mautrix-wsproxy/templates/config.yaml.j2 b/roles/matrix-bridge-mautrix-wsproxy/templates/config.yaml.j2 index cfbe816f..873832fb 100644 --- a/roles/matrix-bridge-mautrix-wsproxy/templates/config.yaml.j2 +++ b/roles/matrix-bridge-mautrix-wsproxy/templates/config.yaml.j2 @@ -1,5 +1,11 @@ listen_address: 0.0.0.0:29331 appservices: - - id: imessage + - id: androidsms as: "{{ matrix_mautrix_wsproxy_appservice_token }}" - hs: "{{ matrix_mautrix_wsproxy_homeserver_token }}" \ No newline at end of file + hs: "{{ matrix_mautrix_wsproxy_homeserver_token }}" +sync_proxy: + # The URL that mautrix-wsproxy can use to reach mautrix-syncproxy + url: "{{ matrix_mautrix_wsproxy_syncproxy_appservice_address }}" + # The URL that mautrix-syncproxy can use to reach mautrix-wsproxy + wsproxy_url: "{{ matrix_mautrix_wsproxy_appservice_address }}" + shared_secret: "{{ matrix_mautrix_wsproxy_syncproxy_shared_secret }}" diff --git a/roles/matrix-bridge-mautrix-wsproxy/templates/systemd/matrix-mautrix-wsproxy-syncproxy.service.j2 b/roles/matrix-bridge-mautrix-wsproxy/templates/systemd/matrix-mautrix-wsproxy-syncproxy.service.j2 new file mode 100644 index 00000000..d468ec0d --- /dev/null +++ b/roles/matrix-bridge-mautrix-wsproxy/templates/systemd/matrix-mautrix-wsproxy-syncproxy.service.j2 @@ -0,0 +1,43 @@ +#jinja2: lstrip_blocks: "True" +[Unit] +Description=Matrix Mautrix wsproxy bridge +{% for service in matrix_mautrix_wsproxy_syncproxy_systemd_required_services_list %} +Requires={{ service }} +After={{ service }} +{% endfor %} +{% for service in matrix_mautrix_wsproxy_syncproxy_systemd_wanted_services_list %} +Wants={{ service }} +{% endfor %} +DefaultDependencies=no + +[Service] +Type=simple +Environment="HOME={{ matrix_systemd_unit_home_path }}" +ExecStartPre=-{{ matrix_host_command_sh }} -c '{{ matrix_host_command_docker }} kill matrix-mautrix-wsproxy-syncproxy 2>/dev/null' +ExecStartPre=-{{ matrix_host_command_sh }} -c '{{ matrix_host_command_docker }} rm matrix-mautrix-wsproxy-syncproxy 2>/dev/null' + +# Intentional delay, so that the homeserver (we likely depend on) can manage to start. +ExecStartPre={{ matrix_host_command_sleep }} 5 + +ExecStart={{ matrix_host_command_docker }} run --rm --name matrix-mautrix-wsproxy-syncproxy \ + --log-driver=none \ + --user={{ matrix_user_uid }}:{{ matrix_user_gid }} \ + --cap-drop=ALL \ + --network={{ matrix_docker_network }} \ + -p {{ matrix_mautrix_wsproxy_syncproxy_port }}:29331 \ + -e DATABASE_URL={{ matrix_mautrix_signal_wsproxy_syncproxy_connection_string }} + -e HOMESERVER_URL={{ matrix_homeserver_container_url }} + -e SHARED_SECRET={{ matrix_mautrix_wsproxy_syncproxy_shared_secret }} + {% for arg in matrix_mautrix_wsproxy_syncproxy_container_extra_arguments %} + {{ arg }} \ + {% endfor %} + {{ matrix_mautrix_wsproxy_syncproxy_docker_image }} + +ExecStop=-{{ matrix_host_command_sh }} -c '{{ matrix_host_command_docker }} kill matrix-mautrix-wsproxy-syncproxy 2>/dev/null' +ExecStop=-{{ matrix_host_command_sh }} -c '{{ matrix_host_command_docker }} rm matrix-mautrix-wsproxy-syncproxy 2>/dev/null' +Restart=always +RestartSec=30 +SyslogIdentifier=matrix-mautrix-wsproxy-syncproxy + +[Install] +WantedBy=multi-user.target diff --git a/roles/matrix-bridge-mautrix-wsproxy/templates/systemd/matrix-mautrix-wsproxy.service.j2 b/roles/matrix-bridge-mautrix-wsproxy/templates/systemd/matrix-mautrix-wsproxy.service.j2 index 1dd17982..8f3844d6 100644 --- a/roles/matrix-bridge-mautrix-wsproxy/templates/systemd/matrix-mautrix-wsproxy.service.j2 +++ b/roles/matrix-bridge-mautrix-wsproxy/templates/systemd/matrix-mautrix-wsproxy.service.j2 @@ -25,7 +25,7 @@ ExecStart={{ matrix_host_command_docker }} run --rm --name matrix-mautrix-wsprox --cap-drop=ALL \ --network={{ matrix_docker_network }} \ -v {{ matrix_mautrix_wsproxy_config_path }}:/data:z \ - -p 29331:29331 \ + -p {{ matrix_mautrix_wsproxy_port }}:29331 \ {% for arg in matrix_mautrix_wsproxy_container_extra_arguments %} {{ arg }} \ {% endfor %}