From 7b3c6f0c196a6d4adbe594e0c2857c8486ded963 Mon Sep 17 00:00:00 2001 From: hanthor Date: Tue, 11 May 2021 15:18:28 -0400 Subject: [PATCH 1/9] Inital work, copeid from mautrix-amp PR --- group_vars/matrix_servers | 46 ++++++ .../defaults/main.yml | 113 +++++++++++++ .../tasks/init.yml | 16 ++ .../tasks/main.yml | 21 +++ .../tasks/setup_install.yml | 115 ++++++++++++++ .../tasks/setup_uninstall.yml | 24 +++ .../tasks/validate_config.yml | 11 ++ .../templates/config.yaml.j2 | 150 ++++++++++++++++++ .../matrix-mautrix-whatsapp.service.j2 | 43 +++++ setup.yml | 1 + 10 files changed, 540 insertions(+) create mode 100644 roles/matrix-bridge-mautrix-wsproxy/defaults/main.yml create mode 100644 roles/matrix-bridge-mautrix-wsproxy/tasks/init.yml create mode 100644 roles/matrix-bridge-mautrix-wsproxy/tasks/main.yml create mode 100644 roles/matrix-bridge-mautrix-wsproxy/tasks/setup_install.yml create mode 100644 roles/matrix-bridge-mautrix-wsproxy/tasks/setup_uninstall.yml create mode 100644 roles/matrix-bridge-mautrix-wsproxy/tasks/validate_config.yml create mode 100644 roles/matrix-bridge-mautrix-wsproxy/templates/config.yaml.j2 create mode 100644 roles/matrix-bridge-mautrix-wsproxy/templates/systemd/matrix-mautrix-whatsapp.service.j2 diff --git a/group_vars/matrix_servers b/group_vars/matrix_servers index 517e6922..713688b5 100755 --- a/group_vars/matrix_servers +++ b/group_vars/matrix_servers @@ -460,6 +460,46 @@ matrix_mautrix_whatsapp_database_password: "{{ matrix_synapse_macaroon_secret_ke # ###################################################################### +###################################################################### +# +# matrix-bridge-mautrix-wsproxy +# +###################################################################### + +# We don't enable bridges by default. +matrix_mautrix_wsproxy_enabled: false + +matrix_mautrix_wsproxy_systemd_required_services_list: | + {{ + ['docker.service'] + + + (['matrix-synapse.service'] if matrix_synapse_enabled else []) + + + (['matrix-postgres.service'] if matrix_postgres_enabled else []) + }} + +matrix_mautrix_wsproxy_homeserver_domain: '{{ matrix_domain }}' + +matrix_mautrix_wsproxy_homeserver_address: "{{ 'http://matrix-synapse:8008' if matrix_synapse_enabled else '' }}" + +matrix_mautrix_wsproxy_homeserver_token: "{{ matrix_synapse_macaroon_secret_key | password_hash('sha512', 'wsproxy.hs.token') | to_uuid }}" + +matrix_mautrix_wsproxy_appservice_token: "{{ matrix_synapse_macaroon_secret_key | password_hash('sha512', 'wsproxy.as.token') | to_uuid }}" + +matrix_mautrix_wsproxy_login_shared_secret: "{{ matrix_synapse_ext_password_provider_shared_secret_auth_shared_secret if matrix_synapse_ext_password_provider_shared_secret_auth_enabled else '' }}" + +# Postgres is the default, except if not using `matrix_postgres` (internal postgres) +matrix_mautrix_wsproxy_database_engine: "{{ 'postgres' if matrix_postgres_enabled else 'sqlite' }}" +matrix_mautrix_wsproxy_database_password: "{{ matrix_synapse_macaroon_secret_key | password_hash('sha512', 'mauwsproxy.db') | to_uuid }}" + +###################################################################### +# +# /matrix-bridge-mautrix-wsproxy +# +###################################################################### + + + ###################################################################### # # matrix-sms-bridge @@ -1371,6 +1411,12 @@ matrix_postgres_additional_databases: | 'password': matrix_mautrix_whatsapp_database_password, }] if (matrix_mautrix_whatsapp_enabled and matrix_mautrix_whatsapp_database_engine == 'postgres' and matrix_mautrix_whatsapp_database_hostname == 'matrix-postgres') else []) + + ([{ + 'name': matrix_mautrix_wsproxy_database_name, + 'username': matrix_mautrix_wsproxy_database_username, + 'password': matrix_mautrix_wsproxy_database_password, + }] if (matrix_mautrix_wsproxy_enabled and matrix_mautrix_wsproxy_database_engine == 'postgres' and matrix_mautrix_wsproxy_database_hostname == 'matrix-postgres') else []) + + ([{ 'name': matrix_mx_puppet_skype_database_name, 'username': matrix_mx_puppet_skype_database_username, diff --git a/roles/matrix-bridge-mautrix-wsproxy/defaults/main.yml b/roles/matrix-bridge-mautrix-wsproxy/defaults/main.yml new file mode 100644 index 00000000..0ecf6514 --- /dev/null +++ b/roles/matrix-bridge-mautrix-wsproxy/defaults/main.yml @@ -0,0 +1,113 @@ +# mautrix-wsproxy is a Matrix <-> websocket bridge +# See: https://github.com/tulir/mautrix-wsproxy + +matrix_mautrix_wsproxy_enabled: true + +matrix_mautrix_wsproxy_version: latest +# See: https://mau.dev/tulir/mautrix-wsproxy/container_registry +matrix_mautrix_wsproxy_docker_image: "dock.mau.dev/tulir/mautrix-wsproxy:{{ matrix_mautrix_wsproxy_version }}" +matrix_mautrix_wsproxy_docker_image_force_pull: "{{ matrix_mautrix_wsproxy_docker_image.endswith(':latest') }}" + +matrix_mautrix_wsproxy_base_path: "{{ matrix_base_data_path }}/mautrix-wsproxy" +matrix_mautrix_wsproxy_config_path: "{{ matrix_mautrix_wsproxy_base_path }}/config" + +matrix_mautrix_wsproxy_homeserver_address: "{{ matrix_homeserver_container_url }}" +matrix_mautrix_wsproxy_homeserver_domain: "{{ matrix_domain }}" +matrix_mautrix_wsproxy_appservice_address: "http://matrix-mautrix-wsproxy:8080" + +# A list of extra arguments to pass to the container +matrix_mautrix_wsproxy_container_extra_arguments: [] + +# List of systemd services that matrix-mautrix-wsproxy.service depends on. +matrix_mautrix_wsproxy_systemd_required_services_list: ['docker.service'] + +# List of systemd services that matrix-mautrix-wsproxy.service wants +matrix_mautrix_wsproxy_systemd_wanted_services_list: [] + +matrix_mautrix_wsproxy_appservice_token: '' +matrix_mautrix_wsproxy_homeserver_token: '' + +matrix_mautrix_wsproxy_appservice_bot_username: wsproxy + + +# Database-related configuration fields. +# +# To use SQLite, stick to these defaults. +# +# To use Postgres: +# - change the engine (`matrix_mautrix_wsproxy_database_engine: 'postgres'`) +# - adjust your database credentials via the `matrix_mautrix_wsproxy_postgres_*` variables +matrix_mautrix_wsproxy_database_engine: 'sqlite' + +matrix_mautrix_wsproxy_sqlite_database_path_local: "{{ matrix_mautrix_wsproxy_data_path }}/mautrix-wsproxy.db" +matrix_mautrix_wsproxy_sqlite_database_path_in_container: "/data/mautrix-wsproxy.db" + +matrix_mautrix_wsproxy_database_username: 'matrix_mautrix_wsproxy' +matrix_mautrix_wsproxy_database_password: 'some-password' +matrix_mautrix_wsproxy_database_hostname: 'matrix-postgres' +matrix_mautrix_wsproxy_database_port: 5432 +matrix_mautrix_wsproxy_database_name: 'matrix_mautrix_wsproxy' + +matrix_mautrix_wsproxy_database_connection_string: 'postgresql://{{ matrix_mautrix_wsproxy_database_username }}:{{ matrix_mautrix_wsproxy_database_password }}@{{ matrix_mautrix_wsproxy_database_hostname }}:{{ matrix_mautrix_wsproxy_database_port }}/{{ matrix_mautrix_wsproxy_database_name }}?sslmode=disable' + +matrix_mautrix_wsproxy_appservice_database_type: "{{ + { + 'sqlite': 'sqlite3', + 'postgres':'postgres', + }[matrix_mautrix_wsproxy_database_engine] +}}" + +matrix_mautrix_wsproxy_appservice_database_uri: "{{ + { + 'sqlite': matrix_mautrix_wsproxy_sqlite_database_path_in_container, + 'postgres': matrix_mautrix_wsproxy_database_connection_string, + }[matrix_mautrix_wsproxy_database_engine] +}}" + + +# Can be set to enable automatic double-puppeting via Shared Secret Auth (https://github.com/devture/matrix-synapse-shared-secret-auth). +matrix_mautrix_wsproxy_login_shared_secret: '' + +# Default mautrix-wsproxy configuration template which covers the generic use case. +# You can customize it by controlling the various variables inside it. +# +# For a more advanced customization, you can extend the default (see `matrix_mautrix_wsproxy_configuration_extension_yaml`) +# or completely replace this variable with your own template. +matrix_mautrix_wsproxy_configuration_yaml: "{{ lookup('template', 'templates/config.yaml.j2') }}" + +matrix_mautrix_wsproxy_configuration_extension_yaml: | + # Your custom YAML configuration goes here. + # This configuration extends the default starting configuration (`matrix_mautrix_wsproxy_configuration_yaml`). + # + # You can override individual variables from the default configuration, or introduce new ones. + # + # If you need something more special, you can take full control by + # completely redefining `matrix_mautrix_wsproxy_configuration_yaml`. + +matrix_mautrix_wsproxy_configuration_extension: "{{ matrix_mautrix_wsproxy_configuration_extension_yaml|from_yaml if matrix_mautrix_wsproxy_configuration_extension_yaml|from_yaml is mapping else {} }}" + +# Holds the final configuration (a combination of the default and its extension). +# You most likely don't need to touch this variable. Instead, see `matrix_mautrix_wsproxy_configuration_yaml`. +matrix_mautrix_wsproxy_configuration: "{{ matrix_mautrix_wsproxy_configuration_yaml|from_yaml|combine(matrix_mautrix_wsproxy_configuration_extension, recursive=True) }}" + +matrix_mautrix_wsproxy_registration_yaml: | + # appservice -> id from the config + id: wsproxy + # appservice -> as_token and hs_token from the config + as_token: random string + hs_token: random string + namespaces: + users: + # The localpart here is username_template from the config, but .+ instead of {{.}} + - regex: '@imessage_.+:{{ matrix_mautrix_telegram_homeserver_domain|regex_escape }}$' + exclusive: true + # Localpart here is appservice -> bot -> username from the config + - regex: '@imessagebot:{{ matrix_mautrix_telegram_homeserver_domain|regex_escape }}$' + exclusive: true + # Address that Synapse uses to contact mautrix-wsproxy + url: {{ matrix_mautrix_wsproxy_appservice_address }} + # Put a new random string here, it doesn't affect anything else + sender_localpart: random string + rate_limited: false + +matrix_mautrix_wsproxy_registration: "{{ matrix_mautrix_wsproxy_registration_yaml|from_yaml }}" diff --git a/roles/matrix-bridge-mautrix-wsproxy/tasks/init.yml b/roles/matrix-bridge-mautrix-wsproxy/tasks/init.yml new file mode 100644 index 00000000..f320bc74 --- /dev/null +++ b/roles/matrix-bridge-mautrix-wsproxy/tasks/init.yml @@ -0,0 +1,16 @@ +- set_fact: + matrix_systemd_services_list: "{{ matrix_systemd_services_list + ['matrix-mautrix-whatsapp.service'] }}" + when: matrix_mautrix_whatsapp_enabled|bool + +# If the matrix-synapse role is not used, these variables may not exist. +- set_fact: + matrix_synapse_container_extra_arguments: > + {{ matrix_synapse_container_extra_arguments|default([]) }} + + + ["--mount type=bind,src={{ matrix_mautrix_whatsapp_config_path }}/registration.yaml,dst=/matrix-mautrix-whatsapp-registration.yaml,ro"] + + matrix_synapse_app_service_config_files: > + {{ matrix_synapse_app_service_config_files|default([]) }} + + + {{ ["/matrix-mautrix-whatsapp-registration.yaml"] }} + when: matrix_mautrix_whatsapp_enabled|bool diff --git a/roles/matrix-bridge-mautrix-wsproxy/tasks/main.yml b/roles/matrix-bridge-mautrix-wsproxy/tasks/main.yml new file mode 100644 index 00000000..a01b4034 --- /dev/null +++ b/roles/matrix-bridge-mautrix-wsproxy/tasks/main.yml @@ -0,0 +1,21 @@ +- import_tasks: "{{ role_path }}/tasks/init.yml" + tags: + - always + +- import_tasks: "{{ role_path }}/tasks/validate_config.yml" + when: "run_setup|bool and matrix_mautrix_wsproxy_enabled|bool" + tags: + - setup-all + - setup-mautrix-wsproxy + +- import_tasks: "{{ role_path }}/tasks/setup_install.yml" + when: "run_setup and matrix_mautrix_wsproxy_enabled" + tags: + - setup-all + - setup-mautrix-wsproxy + +- import_tasks: "{{ role_path }}/tasks/setup_uninstall.yml" + when: "run_setup and not matrix_mautrix_wsproxy_enabled" + tags: + - setup-all + - setup-mautrix-wsproxy diff --git a/roles/matrix-bridge-mautrix-wsproxy/tasks/setup_install.yml b/roles/matrix-bridge-mautrix-wsproxy/tasks/setup_install.yml new file mode 100644 index 00000000..214bdcc8 --- /dev/null +++ b/roles/matrix-bridge-mautrix-wsproxy/tasks/setup_install.yml @@ -0,0 +1,115 @@ +--- + +# If the matrix-synapse role is not used, `matrix_synapse_role_executed` won't exist. +# We don't want to fail in such cases. +- name: Fail if matrix-synapse role already executed + fail: + msg: >- + The matrix-bridge-mautrix-wsproxy role needs to execute before the matrix-synapse role. + when: "matrix_synapse_role_executed|default(False)" + +- set_fact: + matrix_mautrix_wsproxy_requires_restart: false + +- block: + - name: Check if an SQLite database already exists + stat: + path: "{{ matrix_mautrix_wsproxy_sqlite_database_path_local }}" + register: matrix_mautrix_wsproxy_sqlite_database_path_local_stat_result + + - block: + - set_fact: + matrix_postgres_db_migration_request: + src: "{{ matrix_mautrix_wsproxy_sqlite_database_path_local }}" + dst: "{{ matrix_mautrix_wsproxy_database_connection_string }}" + caller: "{{ role_path|basename }}" + engine_variable_name: 'matrix_mautrix_wsproxy_database_engine' + engine_old: 'sqlite' + systemd_services_to_stop: ['matrix-mautrix-wsproxy.service'] + pgloader_options: ['--with "quote identifiers"'] + + - import_tasks: "{{ role_path }}/../matrix-postgres/tasks/util/migrate_db_to_postgres.yml" + + - set_fact: + matrix_mautrix_wsproxy_requires_restart: true + when: "matrix_mautrix_wsproxy_sqlite_database_path_local_stat_result.stat.exists|bool" + when: "matrix_mautrix_wsproxy_database_engine == 'postgres'" + +- name: Ensure Mautrix wsproxy image is pulled + docker_image: + name: "{{ matrix_mautrix_wsproxy_docker_image }}" + source: "{{ 'pull' if ansible_version.major > 2 or ansible_version.minor > 7 else omit }}" + 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 wsproxy paths exists + file: + path: "{{ item }}" + state: directory + mode: 0750 + owner: "{{ matrix_user_username }}" + group: "{{ matrix_user_groupname }}" + with_items: + - "{{ matrix_mautrix_wsproxy_base_path }}" + - "{{ matrix_mautrix_wsproxy_config_path }}" + - "{{ matrix_mautrix_wsproxy_data_path }}" + +- name: Check if an old database file exists + stat: + path: "{{ matrix_mautrix_wsproxy_base_path }}/mautrix-wsproxy.db" + register: matrix_mautrix_wsproxy_stat_database + +- name: Check if an old matrix state file exists + stat: + path: "{{ matrix_mautrix_wsproxy_base_path }}/mx-state.json" + register: matrix_mautrix_wsproxy_stat_mx_state + +- name: (Data relocation) Ensure matrix-mautrix-wsproxy.service is stopped + service: + name: matrix-mautrix-wsproxy + state: stopped + daemon_reload: yes + failed_when: false + when: "matrix_mautrix_wsproxy_stat_database.stat.exists" + +- name: (Data relocation) Move mautrix-wsproxy database file to ./data directory + command: "mv {{ matrix_mautrix_wsproxy_base_path }}/mautrix-wsproxy.db {{ matrix_mautrix_wsproxy_data_path }}/mautrix-wsproxy.db" + when: "matrix_mautrix_wsproxy_stat_database.stat.exists" + +- name: (Data relocation) Move mautrix-wsproxy mx-state file to ./data directory + command: "mv {{ matrix_mautrix_wsproxy_base_path }}/mx-state.json {{ matrix_mautrix_wsproxy_data_path }}/mx-state.json" + when: "matrix_mautrix_wsproxy_stat_mx_state.stat.exists" + +- name: Ensure mautrix-wsproxy config.yaml installed + copy: + content: "{{ matrix_mautrix_wsproxy_configuration|to_nice_yaml }}" + dest: "{{ matrix_mautrix_wsproxy_config_path }}/config.yaml" + mode: 0644 + owner: "{{ matrix_user_username }}" + group: "{{ matrix_user_groupname }}" + +- name: Ensure mautrix-wsproxy registration.yaml installed + copy: + content: "{{ matrix_mautrix_wsproxy_registration|to_nice_yaml }}" + dest: "{{ matrix_mautrix_wsproxy_config_path }}/registration.yaml" + mode: 0644 + owner: "{{ matrix_user_username }}" + group: "{{ matrix_user_groupname }}" + +- name: Ensure matrix-mautrix-wsproxy.service installed + template: + src: "{{ role_path }}/templates/systemd/matrix-mautrix-wsproxy.service.j2" + dest: "{{ matrix_systemd_path }}/matrix-mautrix-wsproxy.service" + mode: 0644 + register: matrix_mautrix_wsproxy_systemd_service_result + +- name: Ensure systemd reloaded after matrix-mautrix-wsproxy.service installation + service: + daemon_reload: yes + when: "matrix_mautrix_wsproxy_systemd_service_result.changed" + +- name: Ensure matrix-mautrix-wsproxy.service restarted, if necessary + service: + name: "matrix-mautrix-wsproxy.service" + state: restarted + when: "matrix_mautrix_wsproxy_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 new file mode 100644 index 00000000..345aa39c --- /dev/null +++ b/roles/matrix-bridge-mautrix-wsproxy/tasks/setup_uninstall.yml @@ -0,0 +1,24 @@ +--- + +- name: Check existence of matrix-mautrix-wsproxy service + stat: + path: "{{ matrix_systemd_path }}/matrix-mautrix-wsproxy.service" + register: matrix_mautrix_wsproxy_service_stat + +- name: Ensure matrix-mautrix-wsproxy is stopped + service: + name: matrix-mautrix-wsproxy + state: stopped + daemon_reload: yes + when: "matrix_mautrix_wsproxy_service_stat.stat.exists" + +- name: Ensure matrix-mautrix-wsproxy.service doesn't exist + file: + path: "{{ matrix_systemd_path }}/matrix-mautrix-wsproxy.service" + state: absent + when: "matrix_mautrix_wsproxy_service_stat.stat.exists" + +- name: Ensure systemd reloaded after matrix-mautrix-wsproxy.service removal + service: + daemon_reload: yes + when: "matrix_mautrix_wsproxy_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 new file mode 100644 index 00000000..b08762bf --- /dev/null +++ b/roles/matrix-bridge-mautrix-wsproxy/tasks/validate_config.yml @@ -0,0 +1,11 @@ +--- + +- name: Fail if required settings not defined + fail: + msg: >- + You need to define a required configuration setting (`{{ item }}`). + when: "vars[item] == ''" + with_items: + - "matrix_mautrix_wsproxy_appservice_token" + - "matrix_mautrix_wsproxy_homeserver_token" + diff --git a/roles/matrix-bridge-mautrix-wsproxy/templates/config.yaml.j2 b/roles/matrix-bridge-mautrix-wsproxy/templates/config.yaml.j2 new file mode 100644 index 00000000..a486278d --- /dev/null +++ b/roles/matrix-bridge-mautrix-wsproxy/templates/config.yaml.j2 @@ -0,0 +1,150 @@ +# Homeserver details +homeserver: + # The address that this appservice can use to connect to the homeserver. + address: {{ matrix_mautrix_wsproxy_homeserver_address }} + # The domain of the homeserver (for MXIDs, etc). + domain: {{ matrix_mautrix_wsproxy_homeserver_domain }} + # Whether or not to verify the SSL certificate of the homeserver. + # Only applies if address starts with https:// + verify_ssl: true + +# Application service host/registration related details +# Changing these values requires regeneration of the registration. +appservice: + # The address that the homeserver can use to connect to this appservice. + address: {{ matrix_mautrix_wsproxy_appservice_address }} + # When using https:// the TLS certificate and key files for the address. + tls_cert: false + tls_key: false + + # The hostname and port where this appservice should listen. + hostname: 0.0.0.0 + port: 8080 + # The maximum body size of appservice API requests (from the homeserver) in mebibytes + # Usually 1 is enough, but on high-traffic bridges you might need to increase this to avoid 413s + max_body_size: 1 + + # The full URI to the database. Only Postgres is currently supported. + database: {{ matrix_mautrix_wsproxy_database_connection_string }} + + # Provisioning API part of the web server for automated portal creation and fetching information. + # Used by things like mautrix-manager (https://github.com/tulir/mautrix-manager). + provisioning: + # Whether or not the provisioning API should be enabled. + enabled: true + # The prefix to use in the provisioning API endpoints. + prefix: /_matrix/provision/v1 + # The shared secret to authorize users of the API. + # Set to "generate" to generate and save a new token. + shared_secret: generate + + # The unique ID of this appservice. + id: wsproxy + # Username of the appservice bot. + bot_username: {{ matrix_mautrix_wsproxy_appservice_bot_username|to_json }} + # Display name and avatar for bot. Set to "remove" to remove display name/avatar, leave empty + # to leave display name/avatar as-is. + bot_displayname: Android Messages bridge bot + bot_avatar: mxc://maunium.net/VuvevQiMRlOxuBVMBNEZZrxi + + # Community ID for bridged users (changes registration file) and rooms. + # Must be created manually. + # + # Example: "+imessage:example.com". Set to false to disable. + community_id: false + + # Authentication tokens for AS <-> HS communication. Autogenerated; do not modify. + as_token: "{{ matrix_mautrix_wsproxy_appservice_token }}" + hs_token: "{{ matrix_mautrix_wsproxy_homeserver_token }}" + +# Prometheus telemetry config. Requires prometheus-client to be installed. +metrics: + enabled: false + listen_port: 8000 + +# Bridge config +bridge: + # Localpart template of MXIDs for remote users. + # {userid} is replaced with the user ID (phone or name converted into a mxid-friendly format). + username_template: "imessage_{userid}" + # Displayname template for remote users. + # {displayname} is replaced with the display name of the user. + # {phone} is replaced with the phone number or name of the user. + displayname_template: "{displayname} (iMessage)" + + # Maximum length of displayname + displayname_max_length: 100 + + # Number of conversations to sync (and create portals for) on login. + # Set 0 to disable automatic syncing. + initial_conversation_sync: 10 + # Shared secret for https://github.com/devture/matrix-synapse-shared-secret-auth + # + # If set, custom puppets will be enabled automatically for local users + # instead of users having to find an access token and run `login-matrix` + # manually. + login_shared_secret: null + # Whether or not created rooms should have federation enabled. + # If false, created portal rooms will never be federated. + federate_rooms: true + # Settings for backfilling messages from the Messages app. + backfill: + # If using double puppeting, should notifications be disabled + # while the initial backfill is in progress? + disable_notifications: false + # End-to-bridge encryption support options. These require matrix-nio to be installed with pip + # and login_shared_secret to be configured in order to get a device for the bridge bot. + # + # Additionally, https://github.com/matrix-org/synapse/pull/5758 is required if using a normal + # application service. + encryption: + # Allow encryption, work in group chat rooms with e2ee enabled + allow: false + # Default to encryption, force-enable encryption in all portals the bridge creates + # This will cause the bridge bot to be in private chats for the encryption to work properly. + default: false + # Options for automatic key sharing. + key_sharing: + # Enable key sharing? If enabled, key requests for rooms where users are in will be fulfilled. + # You must use a client that supports requesting keys from other users to use this feature. + allow: false + # Require the requesting device to have a valid cross-signing signature? + # This doesn't require that the bridge has verified the device, only that the user has verified it. + # Not yet implemented. + require_cross_signing: false + # Require devices to be verified by the bridge? + # Verification by the bridge is not yet implemented. + require_verification: true + # Whether or not to explicitly set the avatar and room name for private + # chat portal rooms. This will be implicitly enabled if encryption.default is true. + private_chat_portal_meta: false + # Whether or not the bridge should send a read receipt from the bridge bot when a message has + # been sent. + delivery_receipts: false + # Whether or not delivery errors should be reported as messages in the Matrix room. + delivery_error_reports: false + # Set this to true to tell the bridge to re-send m.bridge events to all rooms on the next run. + # This field will automatically be changed back to false after it, + # except if the config file is not writable. + resend_bridge_info: false + + # The prefix for commands. Only required in non-management rooms. + command_prefix: "!im" + + # This bridge only supports a single user + user: "{{ matrix_mautrix_wsproxy_bridge_user }}" + +logging: + # The directory for log files. Will be created if not found. + directory: ./logs + # Available variables: .Date for the file date and .Index for different log files on the same day. + file_name_format: "{{ '{{.Date}}-{{.Index}}.log' }}" + # Date format for file names in the Go time format: https://golang.org/pkg/time/#pkg-constants + file_date_format: "2006-01-02" + # Log file permissions. + file_mode: 0600 + # Timestamp format for log entries in the Go time format. + timestamp_format: "Jan _2, 2006 15:04:05" + # Minimum severity for log messages. + # Options: debug, info, warn, error, fatal + print_level: debug \ No newline at end of file diff --git a/roles/matrix-bridge-mautrix-wsproxy/templates/systemd/matrix-mautrix-whatsapp.service.j2 b/roles/matrix-bridge-mautrix-wsproxy/templates/systemd/matrix-mautrix-whatsapp.service.j2 new file mode 100644 index 00000000..7b31cd3f --- /dev/null +++ b/roles/matrix-bridge-mautrix-wsproxy/templates/systemd/matrix-mautrix-whatsapp.service.j2 @@ -0,0 +1,43 @@ +#jinja2: lstrip_blocks: "True" +[Unit] +Description=Matrix Mautrix wsproxy bridge +{% for service in matrix_mautrix_wsproxy_systemd_required_services_list %} +Requires={{ service }} +After={{ service }} +{% endfor %} +{% for service in matrix_mautrix_wsproxy_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 2>/dev/null' +ExecStartPre=-{{ matrix_host_command_sh }} -c '{{ matrix_host_command_docker }} rm matrix-mautrix-wsproxy 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 \ + --log-driver=none \ + --user={{ matrix_user_uid }}:{{ matrix_user_gid }} \ + --cap-drop=ALL \ + --network={{ matrix_docker_network }} \ + -v {{ matrix_mautrix_wsproxy_config_path }}:/config:z \ + -v {{ matrix_mautrix_wsproxy_data_path }}:/data:z \ + --workdir=/data \ + {% for arg in matrix_mautrix_wsproxy_container_extra_arguments %} + {{ arg }} \ + {% endfor %} + {{ matrix_mautrix_wsproxy_docker_image }} \ + /usr/bin/mautrix-wsproxy -c /config/config.yaml -r /config/registration.yaml + +ExecStop=-{{ matrix_host_command_sh }} -c '{{ matrix_host_command_docker }} kill matrix-mautrix-wsproxy 2>/dev/null' +ExecStop=-{{ matrix_host_command_sh }} -c '{{ matrix_host_command_docker }} rm matrix-mautrix-wsproxy 2>/dev/null' +Restart=always +RestartSec=30 +SyslogIdentifier=matrix-mautrix-wsproxy + +[Install] +WantedBy=multi-user.target diff --git a/setup.yml b/setup.yml index 4e2879e5..16394cbd 100755 --- a/setup.yml +++ b/setup.yml @@ -24,6 +24,7 @@ - matrix-bridge-mautrix-signal - matrix-bridge-mautrix-telegram - matrix-bridge-mautrix-whatsapp + - matrix-bridge-mautrix-wsproxy - matrix-bridge-mx-puppet-discord - matrix-bridge-mx-puppet-groupme - matrix-bridge-mx-puppet-steam From ed6ded3eac2085a5dcd893c7cea4430215ad9018 Mon Sep 17 00:00:00 2001 From: hanthor Date: Wed, 12 May 2021 10:59:17 -0400 Subject: [PATCH 2/9] Some fixes leftover code copeid over from whatsapp --- .../defaults/main.yml | 39 ------------------- .../tasks/setup_install.yml | 37 ------------------ ...e.j2 => matrix-mautrix-wsproxy.service.j2} | 0 3 files changed, 76 deletions(-) rename roles/matrix-bridge-mautrix-wsproxy/templates/systemd/{matrix-mautrix-whatsapp.service.j2 => matrix-mautrix-wsproxy.service.j2} (100%) diff --git a/roles/matrix-bridge-mautrix-wsproxy/defaults/main.yml b/roles/matrix-bridge-mautrix-wsproxy/defaults/main.yml index 0ecf6514..f66e17f8 100644 --- a/roles/matrix-bridge-mautrix-wsproxy/defaults/main.yml +++ b/roles/matrix-bridge-mautrix-wsproxy/defaults/main.yml @@ -29,45 +29,6 @@ matrix_mautrix_wsproxy_homeserver_token: '' matrix_mautrix_wsproxy_appservice_bot_username: wsproxy - -# Database-related configuration fields. -# -# To use SQLite, stick to these defaults. -# -# To use Postgres: -# - change the engine (`matrix_mautrix_wsproxy_database_engine: 'postgres'`) -# - adjust your database credentials via the `matrix_mautrix_wsproxy_postgres_*` variables -matrix_mautrix_wsproxy_database_engine: 'sqlite' - -matrix_mautrix_wsproxy_sqlite_database_path_local: "{{ matrix_mautrix_wsproxy_data_path }}/mautrix-wsproxy.db" -matrix_mautrix_wsproxy_sqlite_database_path_in_container: "/data/mautrix-wsproxy.db" - -matrix_mautrix_wsproxy_database_username: 'matrix_mautrix_wsproxy' -matrix_mautrix_wsproxy_database_password: 'some-password' -matrix_mautrix_wsproxy_database_hostname: 'matrix-postgres' -matrix_mautrix_wsproxy_database_port: 5432 -matrix_mautrix_wsproxy_database_name: 'matrix_mautrix_wsproxy' - -matrix_mautrix_wsproxy_database_connection_string: 'postgresql://{{ matrix_mautrix_wsproxy_database_username }}:{{ matrix_mautrix_wsproxy_database_password }}@{{ matrix_mautrix_wsproxy_database_hostname }}:{{ matrix_mautrix_wsproxy_database_port }}/{{ matrix_mautrix_wsproxy_database_name }}?sslmode=disable' - -matrix_mautrix_wsproxy_appservice_database_type: "{{ - { - 'sqlite': 'sqlite3', - 'postgres':'postgres', - }[matrix_mautrix_wsproxy_database_engine] -}}" - -matrix_mautrix_wsproxy_appservice_database_uri: "{{ - { - 'sqlite': matrix_mautrix_wsproxy_sqlite_database_path_in_container, - 'postgres': matrix_mautrix_wsproxy_database_connection_string, - }[matrix_mautrix_wsproxy_database_engine] -}}" - - -# Can be set to enable automatic double-puppeting via Shared Secret Auth (https://github.com/devture/matrix-synapse-shared-secret-auth). -matrix_mautrix_wsproxy_login_shared_secret: '' - # Default mautrix-wsproxy configuration template which covers the generic use case. # You can customize it by controlling the various variables inside it. # diff --git a/roles/matrix-bridge-mautrix-wsproxy/tasks/setup_install.yml b/roles/matrix-bridge-mautrix-wsproxy/tasks/setup_install.yml index 214bdcc8..77f40047 100644 --- a/roles/matrix-bridge-mautrix-wsproxy/tasks/setup_install.yml +++ b/roles/matrix-bridge-mautrix-wsproxy/tasks/setup_install.yml @@ -11,30 +11,6 @@ - set_fact: matrix_mautrix_wsproxy_requires_restart: false -- block: - - name: Check if an SQLite database already exists - stat: - path: "{{ matrix_mautrix_wsproxy_sqlite_database_path_local }}" - register: matrix_mautrix_wsproxy_sqlite_database_path_local_stat_result - - - block: - - set_fact: - matrix_postgres_db_migration_request: - src: "{{ matrix_mautrix_wsproxy_sqlite_database_path_local }}" - dst: "{{ matrix_mautrix_wsproxy_database_connection_string }}" - caller: "{{ role_path|basename }}" - engine_variable_name: 'matrix_mautrix_wsproxy_database_engine' - engine_old: 'sqlite' - systemd_services_to_stop: ['matrix-mautrix-wsproxy.service'] - pgloader_options: ['--with "quote identifiers"'] - - - import_tasks: "{{ role_path }}/../matrix-postgres/tasks/util/migrate_db_to_postgres.yml" - - - set_fact: - matrix_mautrix_wsproxy_requires_restart: true - when: "matrix_mautrix_wsproxy_sqlite_database_path_local_stat_result.stat.exists|bool" - when: "matrix_mautrix_wsproxy_database_engine == 'postgres'" - - name: Ensure Mautrix wsproxy image is pulled docker_image: name: "{{ matrix_mautrix_wsproxy_docker_image }}" @@ -54,24 +30,11 @@ - "{{ matrix_mautrix_wsproxy_config_path }}" - "{{ matrix_mautrix_wsproxy_data_path }}" -- name: Check if an old database file exists - stat: - path: "{{ matrix_mautrix_wsproxy_base_path }}/mautrix-wsproxy.db" - register: matrix_mautrix_wsproxy_stat_database - - name: Check if an old matrix state file exists stat: path: "{{ matrix_mautrix_wsproxy_base_path }}/mx-state.json" register: matrix_mautrix_wsproxy_stat_mx_state -- name: (Data relocation) Ensure matrix-mautrix-wsproxy.service is stopped - service: - name: matrix-mautrix-wsproxy - state: stopped - daemon_reload: yes - failed_when: false - when: "matrix_mautrix_wsproxy_stat_database.stat.exists" - - name: (Data relocation) Move mautrix-wsproxy database file to ./data directory command: "mv {{ matrix_mautrix_wsproxy_base_path }}/mautrix-wsproxy.db {{ matrix_mautrix_wsproxy_data_path }}/mautrix-wsproxy.db" when: "matrix_mautrix_wsproxy_stat_database.stat.exists" diff --git a/roles/matrix-bridge-mautrix-wsproxy/templates/systemd/matrix-mautrix-whatsapp.service.j2 b/roles/matrix-bridge-mautrix-wsproxy/templates/systemd/matrix-mautrix-wsproxy.service.j2 similarity index 100% rename from roles/matrix-bridge-mautrix-wsproxy/templates/systemd/matrix-mautrix-whatsapp.service.j2 rename to roles/matrix-bridge-mautrix-wsproxy/templates/systemd/matrix-mautrix-wsproxy.service.j2 From 8494db19a6677135b89809d39cd6e8a9eec04e76 Mon Sep 17 00:00:00 2001 From: hanthor Date: Wed, 12 May 2021 13:10:55 -0400 Subject: [PATCH 3/9] Got it to run and register --- group_vars/matrix_servers | 8 ------ .../defaults/main.yml | 26 ++++++++----------- .../tasks/init.yml | 10 +++---- .../tasks/setup_install.yml | 9 ------- .../templates/config.yaml.j2 | 8 +----- .../systemd/matrix-mautrix-wsproxy.service.j2 | 2 -- 6 files changed, 17 insertions(+), 46 deletions(-) diff --git a/group_vars/matrix_servers b/group_vars/matrix_servers index 713688b5..53746c08 100755 --- a/group_vars/matrix_servers +++ b/group_vars/matrix_servers @@ -489,8 +489,6 @@ matrix_mautrix_wsproxy_appservice_token: "{{ matrix_synapse_macaroon_secret_key matrix_mautrix_wsproxy_login_shared_secret: "{{ matrix_synapse_ext_password_provider_shared_secret_auth_shared_secret if matrix_synapse_ext_password_provider_shared_secret_auth_enabled else '' }}" # Postgres is the default, except if not using `matrix_postgres` (internal postgres) -matrix_mautrix_wsproxy_database_engine: "{{ 'postgres' if matrix_postgres_enabled else 'sqlite' }}" -matrix_mautrix_wsproxy_database_password: "{{ matrix_synapse_macaroon_secret_key | password_hash('sha512', 'mauwsproxy.db') | to_uuid }}" ###################################################################### # @@ -1411,12 +1409,6 @@ matrix_postgres_additional_databases: | 'password': matrix_mautrix_whatsapp_database_password, }] if (matrix_mautrix_whatsapp_enabled and matrix_mautrix_whatsapp_database_engine == 'postgres' and matrix_mautrix_whatsapp_database_hostname == 'matrix-postgres') else []) + - ([{ - 'name': matrix_mautrix_wsproxy_database_name, - 'username': matrix_mautrix_wsproxy_database_username, - 'password': matrix_mautrix_wsproxy_database_password, - }] if (matrix_mautrix_wsproxy_enabled and matrix_mautrix_wsproxy_database_engine == 'postgres' and matrix_mautrix_wsproxy_database_hostname == 'matrix-postgres') else []) - + ([{ 'name': matrix_mx_puppet_skype_database_name, 'username': matrix_mx_puppet_skype_database_username, diff --git a/roles/matrix-bridge-mautrix-wsproxy/defaults/main.yml b/roles/matrix-bridge-mautrix-wsproxy/defaults/main.yml index f66e17f8..3a3b70bc 100644 --- a/roles/matrix-bridge-mautrix-wsproxy/defaults/main.yml +++ b/roles/matrix-bridge-mautrix-wsproxy/defaults/main.yml @@ -52,23 +52,19 @@ 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: | - # appservice -> id from the config id: wsproxy - # appservice -> as_token and hs_token from the config - as_token: random string - hs_token: random string - namespaces: - users: - # The localpart here is username_template from the config, but .+ instead of {{.}} - - regex: '@imessage_.+:{{ matrix_mautrix_telegram_homeserver_domain|regex_escape }}$' - exclusive: true - # Localpart here is appservice -> bot -> username from the config - - regex: '@imessagebot:{{ matrix_mautrix_telegram_homeserver_domain|regex_escape }}$' - exclusive: true - # Address that Synapse uses to contact mautrix-wsproxy url: {{ matrix_mautrix_wsproxy_appservice_address }} - # Put a new random string here, it doesn't affect anything else - sender_localpart: random string + as_token: "{{ matrix_mautrix_wsproxy_appservice_token }}" + hs_token: "{{ matrix_mautrix_wsproxy_homeserver_token }}" + # See https://github.com/tulir/mautrix-signal/issues/43 + sender_localpart: _bot_{{ matrix_mautrix_wsproxy_appservice_bot_username }} rate_limited: false + namespaces: + users: + - regex: '^@wsproxy_[0-9]+:{{ 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 }}$' + de.sorunome.msc2409.push_ephemeral: true matrix_mautrix_wsproxy_registration: "{{ matrix_mautrix_wsproxy_registration_yaml|from_yaml }}" diff --git a/roles/matrix-bridge-mautrix-wsproxy/tasks/init.yml b/roles/matrix-bridge-mautrix-wsproxy/tasks/init.yml index f320bc74..3f37d704 100644 --- a/roles/matrix-bridge-mautrix-wsproxy/tasks/init.yml +++ b/roles/matrix-bridge-mautrix-wsproxy/tasks/init.yml @@ -1,16 +1,16 @@ - set_fact: - matrix_systemd_services_list: "{{ matrix_systemd_services_list + ['matrix-mautrix-whatsapp.service'] }}" - when: matrix_mautrix_whatsapp_enabled|bool + matrix_systemd_services_list: "{{ matrix_systemd_services_list + ['matrix-mautrix-wsproxy.service'] }}" + when: matrix_mautrix_wsproxy_enabled|bool # If the matrix-synapse role is not used, these variables may not exist. - set_fact: matrix_synapse_container_extra_arguments: > {{ matrix_synapse_container_extra_arguments|default([]) }} + - ["--mount type=bind,src={{ matrix_mautrix_whatsapp_config_path }}/registration.yaml,dst=/matrix-mautrix-whatsapp-registration.yaml,ro"] + ["--mount type=bind,src={{ matrix_mautrix_wsproxy_config_path }}/registration.yaml,dst=/matrix-mautrix-wsproxy-registration.yaml,ro"] matrix_synapse_app_service_config_files: > {{ matrix_synapse_app_service_config_files|default([]) }} + - {{ ["/matrix-mautrix-whatsapp-registration.yaml"] }} - when: matrix_mautrix_whatsapp_enabled|bool + {{ ["/matrix-mautrix-wsproxy-registration.yaml"] }} + when: matrix_mautrix_wsproxy_enabled|bool diff --git a/roles/matrix-bridge-mautrix-wsproxy/tasks/setup_install.yml b/roles/matrix-bridge-mautrix-wsproxy/tasks/setup_install.yml index 77f40047..c1488b75 100644 --- a/roles/matrix-bridge-mautrix-wsproxy/tasks/setup_install.yml +++ b/roles/matrix-bridge-mautrix-wsproxy/tasks/setup_install.yml @@ -28,21 +28,12 @@ with_items: - "{{ matrix_mautrix_wsproxy_base_path }}" - "{{ matrix_mautrix_wsproxy_config_path }}" - - "{{ matrix_mautrix_wsproxy_data_path }}" - name: Check if an old matrix state file exists stat: path: "{{ matrix_mautrix_wsproxy_base_path }}/mx-state.json" register: matrix_mautrix_wsproxy_stat_mx_state -- name: (Data relocation) Move mautrix-wsproxy database file to ./data directory - command: "mv {{ matrix_mautrix_wsproxy_base_path }}/mautrix-wsproxy.db {{ matrix_mautrix_wsproxy_data_path }}/mautrix-wsproxy.db" - when: "matrix_mautrix_wsproxy_stat_database.stat.exists" - -- name: (Data relocation) Move mautrix-wsproxy mx-state file to ./data directory - command: "mv {{ matrix_mautrix_wsproxy_base_path }}/mx-state.json {{ matrix_mautrix_wsproxy_data_path }}/mx-state.json" - when: "matrix_mautrix_wsproxy_stat_mx_state.stat.exists" - - name: Ensure mautrix-wsproxy config.yaml installed copy: content: "{{ matrix_mautrix_wsproxy_configuration|to_nice_yaml }}" diff --git a/roles/matrix-bridge-mautrix-wsproxy/templates/config.yaml.j2 b/roles/matrix-bridge-mautrix-wsproxy/templates/config.yaml.j2 index a486278d..b1332f20 100644 --- a/roles/matrix-bridge-mautrix-wsproxy/templates/config.yaml.j2 +++ b/roles/matrix-bridge-mautrix-wsproxy/templates/config.yaml.j2 @@ -24,9 +24,6 @@ appservice: # Usually 1 is enough, but on high-traffic bridges you might need to increase this to avoid 413s max_body_size: 1 - # The full URI to the database. Only Postgres is currently supported. - database: {{ matrix_mautrix_wsproxy_database_connection_string }} - # Provisioning API part of the web server for automated portal creation and fetching information. # Used by things like mautrix-manager (https://github.com/tulir/mautrix-manager). provisioning: @@ -44,7 +41,7 @@ appservice: bot_username: {{ matrix_mautrix_wsproxy_appservice_bot_username|to_json }} # Display name and avatar for bot. Set to "remove" to remove display name/avatar, leave empty # to leave display name/avatar as-is. - bot_displayname: Android Messages bridge bot + bot_displayname: iMessage bridge bot bot_avatar: mxc://maunium.net/VuvevQiMRlOxuBVMBNEZZrxi # Community ID for bridged users (changes registration file) and rooms. @@ -131,9 +128,6 @@ bridge: # The prefix for commands. Only required in non-management rooms. command_prefix: "!im" - # This bridge only supports a single user - user: "{{ matrix_mautrix_wsproxy_bridge_user }}" - logging: # The directory for log files. Will be created if not found. directory: ./logs 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 7b31cd3f..b1b376ac 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,8 +25,6 @@ ExecStart={{ matrix_host_command_docker }} run --rm --name matrix-mautrix-wsprox --cap-drop=ALL \ --network={{ matrix_docker_network }} \ -v {{ matrix_mautrix_wsproxy_config_path }}:/config:z \ - -v {{ matrix_mautrix_wsproxy_data_path }}:/data:z \ - --workdir=/data \ {% for arg in matrix_mautrix_wsproxy_container_extra_arguments %} {{ arg }} \ {% endfor %} From 948ec46c073ed141a2cd0342f997cf13e08d92ca Mon Sep 17 00:00:00 2001 From: hanthor Date: Wed, 12 May 2021 14:36:53 -0400 Subject: [PATCH 4/9] Fixed service issue with docker image --- roles/matrix-bridge-mautrix-wsproxy/defaults/main.yml | 1 + roles/matrix-bridge-mautrix-wsproxy/tasks/setup_install.yml | 1 + .../templates/systemd/matrix-mautrix-wsproxy.service.j2 | 5 ++++- 3 files changed, 6 insertions(+), 1 deletion(-) diff --git a/roles/matrix-bridge-mautrix-wsproxy/defaults/main.yml b/roles/matrix-bridge-mautrix-wsproxy/defaults/main.yml index 3a3b70bc..1bddcae4 100644 --- a/roles/matrix-bridge-mautrix-wsproxy/defaults/main.yml +++ b/roles/matrix-bridge-mautrix-wsproxy/defaults/main.yml @@ -10,6 +10,7 @@ matrix_mautrix_wsproxy_docker_image_force_pull: "{{ matrix_mautrix_wsproxy_docke matrix_mautrix_wsproxy_base_path: "{{ matrix_base_data_path }}/mautrix-wsproxy" matrix_mautrix_wsproxy_config_path: "{{ matrix_mautrix_wsproxy_base_path }}/config" +matrix_mautrix_wsproxy_data_path: "{{ matrix_mautrix_wsproxy_base_path }}/data" matrix_mautrix_wsproxy_homeserver_address: "{{ matrix_homeserver_container_url }}" matrix_mautrix_wsproxy_homeserver_domain: "{{ matrix_domain }}" diff --git a/roles/matrix-bridge-mautrix-wsproxy/tasks/setup_install.yml b/roles/matrix-bridge-mautrix-wsproxy/tasks/setup_install.yml index c1488b75..3e4069ab 100644 --- a/roles/matrix-bridge-mautrix-wsproxy/tasks/setup_install.yml +++ b/roles/matrix-bridge-mautrix-wsproxy/tasks/setup_install.yml @@ -28,6 +28,7 @@ with_items: - "{{ matrix_mautrix_wsproxy_base_path }}" - "{{ matrix_mautrix_wsproxy_config_path }}" + - "{{ matrix_mautrix_wsproxy_data_path }}" - name: Check if an old matrix state file exists stat: 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 b1b376ac..6c3aa5aa 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,11 +25,14 @@ ExecStart={{ matrix_host_command_docker }} run --rm --name matrix-mautrix-wsprox --cap-drop=ALL \ --network={{ matrix_docker_network }} \ -v {{ matrix_mautrix_wsproxy_config_path }}:/config:z \ + -v {{ matrix_mautrix_wsproxy_data_path }}:/data:z \ + -e AS_TOKEN='{{ matrix_mautrix_wsproxy_appservice_token }}' \ + -e HS_TOKEN='{{ matrix_mautrix_wsproxy_homeserver_token }}' \ {% for arg in matrix_mautrix_wsproxy_container_extra_arguments %} {{ arg }} \ {% endfor %} {{ matrix_mautrix_wsproxy_docker_image }} \ - /usr/bin/mautrix-wsproxy -c /config/config.yaml -r /config/registration.yaml + /usr/bin/mautrix-wsproxy -config /config/config.yaml ExecStop=-{{ matrix_host_command_sh }} -c '{{ matrix_host_command_docker }} kill matrix-mautrix-wsproxy 2>/dev/null' ExecStop=-{{ matrix_host_command_sh }} -c '{{ matrix_host_command_docker }} rm matrix-mautrix-wsproxy 2>/dev/null' From ad2910a9a0681dedba3ea8abc129f64b0899bd5d Mon Sep 17 00:00:00 2001 From: hanthor Date: Wed, 12 May 2021 15:10:19 -0400 Subject: [PATCH 5/9] I now realize I need 2 roles wsproxy and imessage --- .../templates/config.yaml.j2 | 149 +----------------- 1 file changed, 5 insertions(+), 144 deletions(-) diff --git a/roles/matrix-bridge-mautrix-wsproxy/templates/config.yaml.j2 b/roles/matrix-bridge-mautrix-wsproxy/templates/config.yaml.j2 index b1332f20..c69c2d75 100644 --- a/roles/matrix-bridge-mautrix-wsproxy/templates/config.yaml.j2 +++ b/roles/matrix-bridge-mautrix-wsproxy/templates/config.yaml.j2 @@ -1,144 +1,5 @@ -# Homeserver details -homeserver: - # The address that this appservice can use to connect to the homeserver. - address: {{ matrix_mautrix_wsproxy_homeserver_address }} - # The domain of the homeserver (for MXIDs, etc). - domain: {{ matrix_mautrix_wsproxy_homeserver_domain }} - # Whether or not to verify the SSL certificate of the homeserver. - # Only applies if address starts with https:// - verify_ssl: true - -# Application service host/registration related details -# Changing these values requires regeneration of the registration. -appservice: - # The address that the homeserver can use to connect to this appservice. - address: {{ matrix_mautrix_wsproxy_appservice_address }} - # When using https:// the TLS certificate and key files for the address. - tls_cert: false - tls_key: false - - # The hostname and port where this appservice should listen. - hostname: 0.0.0.0 - port: 8080 - # The maximum body size of appservice API requests (from the homeserver) in mebibytes - # Usually 1 is enough, but on high-traffic bridges you might need to increase this to avoid 413s - max_body_size: 1 - - # Provisioning API part of the web server for automated portal creation and fetching information. - # Used by things like mautrix-manager (https://github.com/tulir/mautrix-manager). - provisioning: - # Whether or not the provisioning API should be enabled. - enabled: true - # The prefix to use in the provisioning API endpoints. - prefix: /_matrix/provision/v1 - # The shared secret to authorize users of the API. - # Set to "generate" to generate and save a new token. - shared_secret: generate - - # The unique ID of this appservice. - id: wsproxy - # Username of the appservice bot. - bot_username: {{ matrix_mautrix_wsproxy_appservice_bot_username|to_json }} - # Display name and avatar for bot. Set to "remove" to remove display name/avatar, leave empty - # to leave display name/avatar as-is. - bot_displayname: iMessage bridge bot - bot_avatar: mxc://maunium.net/VuvevQiMRlOxuBVMBNEZZrxi - - # Community ID for bridged users (changes registration file) and rooms. - # Must be created manually. - # - # Example: "+imessage:example.com". Set to false to disable. - community_id: false - - # Authentication tokens for AS <-> HS communication. Autogenerated; do not modify. - as_token: "{{ matrix_mautrix_wsproxy_appservice_token }}" - hs_token: "{{ matrix_mautrix_wsproxy_homeserver_token }}" - -# Prometheus telemetry config. Requires prometheus-client to be installed. -metrics: - enabled: false - listen_port: 8000 - -# Bridge config -bridge: - # Localpart template of MXIDs for remote users. - # {userid} is replaced with the user ID (phone or name converted into a mxid-friendly format). - username_template: "imessage_{userid}" - # Displayname template for remote users. - # {displayname} is replaced with the display name of the user. - # {phone} is replaced with the phone number or name of the user. - displayname_template: "{displayname} (iMessage)" - - # Maximum length of displayname - displayname_max_length: 100 - - # Number of conversations to sync (and create portals for) on login. - # Set 0 to disable automatic syncing. - initial_conversation_sync: 10 - # Shared secret for https://github.com/devture/matrix-synapse-shared-secret-auth - # - # If set, custom puppets will be enabled automatically for local users - # instead of users having to find an access token and run `login-matrix` - # manually. - login_shared_secret: null - # Whether or not created rooms should have federation enabled. - # If false, created portal rooms will never be federated. - federate_rooms: true - # Settings for backfilling messages from the Messages app. - backfill: - # If using double puppeting, should notifications be disabled - # while the initial backfill is in progress? - disable_notifications: false - # End-to-bridge encryption support options. These require matrix-nio to be installed with pip - # and login_shared_secret to be configured in order to get a device for the bridge bot. - # - # Additionally, https://github.com/matrix-org/synapse/pull/5758 is required if using a normal - # application service. - encryption: - # Allow encryption, work in group chat rooms with e2ee enabled - allow: false - # Default to encryption, force-enable encryption in all portals the bridge creates - # This will cause the bridge bot to be in private chats for the encryption to work properly. - default: false - # Options for automatic key sharing. - key_sharing: - # Enable key sharing? If enabled, key requests for rooms where users are in will be fulfilled. - # You must use a client that supports requesting keys from other users to use this feature. - allow: false - # Require the requesting device to have a valid cross-signing signature? - # This doesn't require that the bridge has verified the device, only that the user has verified it. - # Not yet implemented. - require_cross_signing: false - # Require devices to be verified by the bridge? - # Verification by the bridge is not yet implemented. - require_verification: true - # Whether or not to explicitly set the avatar and room name for private - # chat portal rooms. This will be implicitly enabled if encryption.default is true. - private_chat_portal_meta: false - # Whether or not the bridge should send a read receipt from the bridge bot when a message has - # been sent. - delivery_receipts: false - # Whether or not delivery errors should be reported as messages in the Matrix room. - delivery_error_reports: false - # Set this to true to tell the bridge to re-send m.bridge events to all rooms on the next run. - # This field will automatically be changed back to false after it, - # except if the config file is not writable. - resend_bridge_info: false - - # The prefix for commands. Only required in non-management rooms. - command_prefix: "!im" - -logging: - # The directory for log files. Will be created if not found. - directory: ./logs - # Available variables: .Date for the file date and .Index for different log files on the same day. - file_name_format: "{{ '{{.Date}}-{{.Index}}.log' }}" - # Date format for file names in the Go time format: https://golang.org/pkg/time/#pkg-constants - file_date_format: "2006-01-02" - # Log file permissions. - file_mode: 0600 - # Timestamp format for log entries in the Go time format. - timestamp_format: "Jan _2, 2006 15:04:05" - # Minimum severity for log messages. - # Options: debug, info, warn, error, fatal - print_level: debug \ No newline at end of file +listen_address: 0.0.0.0:8080 +appservices: + - id: iMessage + as: "{{ matrix_mautrix_wsproxy_appservice_token }}" + hs: "{{ matrix_mautrix_wsproxy_homeserver_token }}" \ No newline at end of file From d911f0aa2189d5d713e71fd2ebe8bcbac3d3690f Mon Sep 17 00:00:00 2001 From: hanthor Date: Thu, 13 May 2021 17:55:42 -0400 Subject: [PATCH 6/9] Got someting working, still rough --- ...guring-playbook-bridge-mautrix-imessage.md | 22 ++++ .../defaults/main.yml | 34 +++++- .../tasks/setup_install.yml | 10 +- .../templates/config-ios.yaml.j2 | 107 ++++++++++++++++++ .../templates/config.yaml.j2 | 4 +- .../systemd/matrix-mautrix-wsproxy.service.j2 | 3 +- 6 files changed, 169 insertions(+), 11 deletions(-) create mode 100644 docs/configuring-playbook-bridge-mautrix-imessage.md create mode 100644 roles/matrix-bridge-mautrix-wsproxy/templates/config-ios.yaml.j2 diff --git a/docs/configuring-playbook-bridge-mautrix-imessage.md b/docs/configuring-playbook-bridge-mautrix-imessage.md new file mode 100644 index 00000000..bc40aba1 --- /dev/null +++ b/docs/configuring-playbook-bridge-mautrix-imessage.md @@ -0,0 +1,22 @@ +# Setting up Mautrix Whatsapp (optional) + +The playbook can install and configure [mautrix-whatsapp](https://github.com/tulir/mautrix-whatsapp) for you. + +See the project's [documentation](https://github.com/tulir/mautrix-whatsapp/wiki) to learn what it does and why it might be useful to you. + +Use the following playbook configuration: + +```yaml +matrix_mautrix_wsproxy_enabled: true +matrix_mautrix_imessage_user: 'user@domain.com' +``` + + +## Set up Double Puppeting + + + + +## Usage + +You then need to start a chat with `@whatsappbot:YOUR_DOMAIN` (where `YOUR_DOMAIN` is your base domain, not the `matrix.` domain). diff --git a/roles/matrix-bridge-mautrix-wsproxy/defaults/main.yml b/roles/matrix-bridge-mautrix-wsproxy/defaults/main.yml index 1bddcae4..aeaa3baf 100644 --- a/roles/matrix-bridge-mautrix-wsproxy/defaults/main.yml +++ b/roles/matrix-bridge-mautrix-wsproxy/defaults/main.yml @@ -14,7 +14,7 @@ matrix_mautrix_wsproxy_data_path: "{{ matrix_mautrix_wsproxy_base_path }}/data" matrix_mautrix_wsproxy_homeserver_address: "{{ matrix_homeserver_container_url }}" matrix_mautrix_wsproxy_homeserver_domain: "{{ matrix_domain }}" -matrix_mautrix_wsproxy_appservice_address: "http://matrix-mautrix-wsproxy:8080" +matrix_mautrix_wsproxy_appservice_address: "http://matrix-mautrix-wsproxy:29331" # A list of extra arguments to pass to the container matrix_mautrix_wsproxy_container_extra_arguments: [] @@ -28,7 +28,7 @@ matrix_mautrix_wsproxy_systemd_wanted_services_list: [] matrix_mautrix_wsproxy_appservice_token: '' matrix_mautrix_wsproxy_homeserver_token: '' -matrix_mautrix_wsproxy_appservice_bot_username: wsproxy +matrix_mautrix_wsproxy_appservice_bot_username: imessagebot # Default mautrix-wsproxy configuration template which covers the generic use case. # You can customize it by controlling the various variables inside it. @@ -52,20 +52,42 @@ matrix_mautrix_wsproxy_configuration_extension: "{{ matrix_mautrix_wsproxy_confi # You most likely don't need to touch this variable. Instead, see `matrix_mautrix_wsproxy_configuration_yaml`. matrix_mautrix_wsproxy_configuration: "{{ matrix_mautrix_wsproxy_configuration_yaml|from_yaml|combine(matrix_mautrix_wsproxy_configuration_extension, recursive=True) }}" +# Default mautrix-wsproxy configuration template which covers the generic use case. +# You can customize it by controlling the various variables inside it. +# +# For a more advanced customization, you can extend the default (see `matrix_mautrix_wsproxy_configuration_extension_yaml`) +# or completely replace this variable with your own template. +matrix_mautrix_imessage_configuration_yaml: "{{ lookup('template', 'templates/config-ios.yaml.j2') }}" + +matrix_mautrix_imessage_configuration_extension_yaml: | + # Your custom YAML configuration goes here. + # This configuration extends the default starting configuration (`matrix_mautrix_wsproxy_configuration_yaml`). + # + # You can override individual variables from the default configuration, or introduce new ones. + # + # If you need something more special, you can take full control by + # completely redefining `matrix_mautrix_imessage_configuration_yaml`. + +matrix_mautrix_imessage_configuration_extension: "{{ matrix_mautrix_imessage_configuration_extension_yaml|from_yaml if matrix_mautrix_imessage_configuration_extension_yaml|from_yaml is mapping else {} }}" + +# Holds the final configuration (a combination of the default and its extension). +# You most likely don't need to touch this variable. Instead, see `matrix_mautrix_imessage_configuration_yaml`. +matrix_mautrix_imessage_configuration: "{{ matrix_mautrix_imessage_configuration_yaml|from_yaml|combine(matrix_mautrix_imessage_configuration_extension, recursive=True) }}" + +matrix_mautrix_imessage_user: '' + matrix_mautrix_wsproxy_registration_yaml: | - id: wsproxy + id: imessage url: {{ matrix_mautrix_wsproxy_appservice_address }} as_token: "{{ matrix_mautrix_wsproxy_appservice_token }}" hs_token: "{{ matrix_mautrix_wsproxy_homeserver_token }}" - # See https://github.com/tulir/mautrix-signal/issues/43 sender_localpart: _bot_{{ matrix_mautrix_wsproxy_appservice_bot_username }} rate_limited: false namespaces: users: - - regex: '^@wsproxy_[0-9]+:{{ matrix_mautrix_wsproxy_homeserver_domain|regex_escape }}$' + - regex: '@imessage_.+:{{ 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 }}$' - de.sorunome.msc2409.push_ephemeral: true matrix_mautrix_wsproxy_registration: "{{ matrix_mautrix_wsproxy_registration_yaml|from_yaml }}" diff --git a/roles/matrix-bridge-mautrix-wsproxy/tasks/setup_install.yml b/roles/matrix-bridge-mautrix-wsproxy/tasks/setup_install.yml index 3e4069ab..ca7df386 100644 --- a/roles/matrix-bridge-mautrix-wsproxy/tasks/setup_install.yml +++ b/roles/matrix-bridge-mautrix-wsproxy/tasks/setup_install.yml @@ -28,7 +28,7 @@ with_items: - "{{ matrix_mautrix_wsproxy_base_path }}" - "{{ matrix_mautrix_wsproxy_config_path }}" - - "{{ matrix_mautrix_wsproxy_data_path }}" + - "{{ matrix_mautrix_wsproxy_data_path }}" - name: Check if an old matrix state file exists stat: @@ -43,6 +43,14 @@ owner: "{{ matrix_user_username }}" group: "{{ matrix_user_groupname }}" +- name: Ensure mautrix-imessage config-ios.yaml installed + copy: + content: "{{ matrix_mautrix_imessage_configuration|to_nice_yaml }}" + dest: "{{ matrix_mautrix_wsproxy_config_path }}/config-ios.yaml" + mode: 0644 + owner: "{{ matrix_user_username }}" + group: "{{ matrix_user_groupname }}" + - name: Ensure mautrix-wsproxy registration.yaml installed copy: content: "{{ matrix_mautrix_wsproxy_registration|to_nice_yaml }}" diff --git a/roles/matrix-bridge-mautrix-wsproxy/templates/config-ios.yaml.j2 b/roles/matrix-bridge-mautrix-wsproxy/templates/config-ios.yaml.j2 new file mode 100644 index 00000000..dd87b2d2 --- /dev/null +++ b/roles/matrix-bridge-mautrix-wsproxy/templates/config-ios.yaml.j2 @@ -0,0 +1,107 @@ +#jinja2: lstrip_blocks: "True" +# Homeserver details. +homeserver: + # The address that this appservice can use to connect to the homeserver. + address: {{ matrix_mautrix_wsproxy_homeserver_domain }} + websocket_proxy: {{ matrix_mautrix_wsproxy_appservice_address }} + # The domain of the homeserver (for MXIDs, etc). + domain: {{ matrix_mautrix_wsproxy_homeserver_domain }} +# Application service host/registration related details. +# Changing these values requires regeneration of the registration. + +appservice: + # SQLite database path + database: mautrix-imessage.db + + # The unique ID of this appservice. + id: imessage + # Appservice bot details. + bot: + # Username of the appservice bot. + username: imessagebot + # Display name and avatar for bot. Set to "remove" to remove display name/avatar, leave empty + # to leave display name/avatar as-is. + displayname: iMessage bridge bot + avatar: mxc://maunium.net/tManJEpANASZvDVzvRvhILdX + + # Authentication tokens for AS <-> HS communication. Autogenerated; do not modify. + as_token: "This value is generated when generating the registration" + hs_token: "This value is generated when generating the registration" +imessage: + platform: ios + +# Bridge config +bridge: + # The user of the bridge. + user: "{{ matrix_mautrix_imessage_user }}" + + # Localpart template of MXIDs for iMessage users. + # {{ '{{.}}' }} is replaced with the phone number or email of the iMessage user. + username_template: "{{ 'imessage_{{.}}' }}" + # Displayname template for iMessage users. + # {{ '{{.}}' }} is replaced with the contact list name (if available) or username (phone number or email) of the iMessage user. + displayname_template: "{{ '{{.}} (iMessage)' }}" + + # Whether or not the bridge should send a read receipt from the bridge bot when a message has been + # sent to iMessage. If fetch_message_on_timeout is enabled, a successful post-timeout fetch will + # trigger a read receipt too. + delivery_receipts: false + + # Whether or not to update the m.direct account data event when double puppeting is enabled. + # Note that updating the m.direct event is not atomic (except with mautrix-asmux) + # and is therefore prone to race conditions. + sync_direct_chat_list: false + # Shared secret for https://github.com/devture/matrix-synapse-shared-secret-auth + # + # If set, custom puppets will be enabled automatically for local users + # instead of users having to find an access token and run `login-matrix` + # manually. + login_shared_secret: {{ matrix_mautrix_whatsapp_login_shared_secret|to_json }} + # Maximum age of chats to sync in days. + chat_sync_max_age: 0.5 + # Maximum number of messages to backfill for new portal rooms. + initial_backfill_limit: 100 + + # The prefix for commands. Only required in non-management rooms. + command_prefix: "!im" + + # End-to-bridge encryption support options. This requires login_shared_secret to be configured + # in order to get a device for the bridge bot. + # + # Additionally, https://github.com/matrix-org/synapse/pull/5758 is required if using a normal + # application service. + encryption: + # Allow encryption, work in group chat rooms with e2ee enabled + allow: false + # Default to encryption, force-enable encryption in all portals the bridge creates + # This will cause the bridge bot to be in private chats for the encryption to work properly. + # It is recommended to also set private_chat_portal_meta to true when using this. + default: false + # Options for automatic key sharing. + key_sharing: + # Enable key sharing? If enabled, key requests for rooms where users are in will be fulfilled. + # You must use a client that supports requesting keys from other users to use this feature. + allow: false + # Require the requesting device to have a valid cross-signing signature? + # This doesn't require that the bridge has verified the device, only that the user has verified it. + # Not yet implemented. + require_cross_signing: false + # Require devices to be verified by the bridge? + # Verification by the bridge is not yet implemented. + require_verification: true + +# Logging config. +logging: + # The directory for log files. Will be created if not found. + directory: ./logs + # Available variables: .Date for the file date and .Index for different log files on the same day. + file_name_format: "{{ '{{.Date}}-{{.Index}}.log' }}" + # Date format for file names in the Go time format: https://golang.org/pkg/time/#pkg-constants + file_date_format: "2006-01-02" + # Log file permissions. + file_mode: 0600 + # Timestamp format for log entries in the Go time format. + timestamp_format: "Jan _2, 2006 15:04:05" + # Minimum severity for log messages. + # Options: debug, info, warn, error, fatal + print_level: debug diff --git a/roles/matrix-bridge-mautrix-wsproxy/templates/config.yaml.j2 b/roles/matrix-bridge-mautrix-wsproxy/templates/config.yaml.j2 index c69c2d75..cfbe816f 100644 --- a/roles/matrix-bridge-mautrix-wsproxy/templates/config.yaml.j2 +++ b/roles/matrix-bridge-mautrix-wsproxy/templates/config.yaml.j2 @@ -1,5 +1,5 @@ -listen_address: 0.0.0.0:8080 +listen_address: 0.0.0.0:29331 appservices: - - id: iMessage + - id: imessage as: "{{ matrix_mautrix_wsproxy_appservice_token }}" hs: "{{ matrix_mautrix_wsproxy_homeserver_token }}" \ No newline at end of file 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 6c3aa5aa..807adea8 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 @@ -26,8 +26,7 @@ ExecStart={{ matrix_host_command_docker }} run --rm --name matrix-mautrix-wsprox --network={{ matrix_docker_network }} \ -v {{ matrix_mautrix_wsproxy_config_path }}:/config:z \ -v {{ matrix_mautrix_wsproxy_data_path }}:/data:z \ - -e AS_TOKEN='{{ matrix_mautrix_wsproxy_appservice_token }}' \ - -e HS_TOKEN='{{ matrix_mautrix_wsproxy_homeserver_token }}' \ + -p 29331:29331 \ {% for arg in matrix_mautrix_wsproxy_container_extra_arguments %} {{ arg }} \ {% endfor %} From 71cdb3fc6670127810f002a2ee256f03cfb2dcbf Mon Sep 17 00:00:00 2001 From: hanthor Date: Thu, 13 May 2021 20:11:59 -0400 Subject: [PATCH 7/9] Closer to working but still not working --- .../templates/config-ios.yaml.j2 | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/roles/matrix-bridge-mautrix-wsproxy/templates/config-ios.yaml.j2 b/roles/matrix-bridge-mautrix-wsproxy/templates/config-ios.yaml.j2 index dd87b2d2..9b108fa6 100644 --- a/roles/matrix-bridge-mautrix-wsproxy/templates/config-ios.yaml.j2 +++ b/roles/matrix-bridge-mautrix-wsproxy/templates/config-ios.yaml.j2 @@ -2,8 +2,8 @@ # Homeserver details. homeserver: # The address that this appservice can use to connect to the homeserver. - address: {{ matrix_mautrix_wsproxy_homeserver_domain }} - websocket_proxy: {{ matrix_mautrix_wsproxy_appservice_address }} + address: {{ matrix_mautrix_wsproxy_homeserver_address }} + websocket_proxy: {{ matrix_mautrix_wsproxy_appservice_websocket }} # The domain of the homeserver (for MXIDs, etc). domain: {{ matrix_mautrix_wsproxy_homeserver_domain }} # Application service host/registration related details. @@ -25,8 +25,8 @@ appservice: avatar: mxc://maunium.net/tManJEpANASZvDVzvRvhILdX # Authentication tokens for AS <-> HS communication. Autogenerated; do not modify. - as_token: "This value is generated when generating the registration" - hs_token: "This value is generated when generating the registration" + as_token: "{{ matrix_mautrix_wsproxy_appservice_token }}" + hs_token: "{{ matrix_mautrix_wsproxy_homeserver_token }}" imessage: platform: ios From f5d884edd87bca824cf8d3f371babfb0902bcb05 Mon Sep 17 00:00:00 2001 From: hanthor Date: Fri, 21 May 2021 16:06:29 -0400 Subject: [PATCH 8/9] reverting ports --- group_vars/matrix_servers | 4 ++++ roles/matrix-bridge-mautrix-wsproxy/defaults/main.yml | 3 ++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/group_vars/matrix_servers b/group_vars/matrix_servers index 53746c08..fc5e5b68 100755 --- a/group_vars/matrix_servers +++ b/group_vars/matrix_servers @@ -486,6 +486,10 @@ matrix_mautrix_wsproxy_homeserver_token: "{{ matrix_synapse_macaroon_secret_key matrix_mautrix_wsproxy_appservice_token: "{{ matrix_synapse_macaroon_secret_key | password_hash('sha512', 'wsproxy.as.token') | to_uuid }}" +matrix_mautrix_imessage_homeserver_token: "{{ matrix_synapse_macaroon_secret_key | password_hash('sha512', 'imessage.hs.token') | to_uuid }}" + +matrix_mautrix_imessage_appservice_token: "{{ matrix_synapse_macaroon_secret_key | password_hash('sha512', 'imessage.as.token') | to_uuid }}" + matrix_mautrix_wsproxy_login_shared_secret: "{{ matrix_synapse_ext_password_provider_shared_secret_auth_shared_secret if matrix_synapse_ext_password_provider_shared_secret_auth_enabled else '' }}" # Postgres is the default, except if not using `matrix_postgres` (internal postgres) diff --git a/roles/matrix-bridge-mautrix-wsproxy/defaults/main.yml b/roles/matrix-bridge-mautrix-wsproxy/defaults/main.yml index aeaa3baf..5fd9bb04 100644 --- a/roles/matrix-bridge-mautrix-wsproxy/defaults/main.yml +++ b/roles/matrix-bridge-mautrix-wsproxy/defaults/main.yml @@ -14,7 +14,8 @@ matrix_mautrix_wsproxy_data_path: "{{ matrix_mautrix_wsproxy_base_path }}/data" matrix_mautrix_wsproxy_homeserver_address: "{{ matrix_homeserver_container_url }}" matrix_mautrix_wsproxy_homeserver_domain: "{{ matrix_domain }}" -matrix_mautrix_wsproxy_appservice_address: "http://matrix-mautrix-wsproxy:29331" +matrix_mautrix_wsproxy_appservice_address: "http://localhost:29331" +matrix_mautrix_wsproxy_appservice_websocket: "ws://localhost:29331" # A list of extra arguments to pass to the container matrix_mautrix_wsproxy_container_extra_arguments: [] From 5c07382002d2800c5e781d8b3199770cbc51271f Mon Sep 17 00:00:00 2001 From: Miguel Alatzar Date: Sun, 17 Oct 2021 20:41:38 -0700 Subject: [PATCH 9/9] Add matrix-nginx-proxy config for mautrix-wsproxy --- ...guring-playbook-bridge-mautrix-imessage.md | 22 ---- ...iguring-playbook-bridge-mautrix-wsproxy.md | 27 +++++ group_vars/matrix_servers | 3 + roles/matrix-base/defaults/main.yml | 3 + .../defaults/main.yml | 4 +- roles/matrix-nginx-proxy/defaults/main.yml | 7 ++ .../tasks/setup_nginx_proxy.yml | 13 +++ .../conf.d/matrix-mautrix-wsproxy.conf.j2 | 110 ++++++++++++++++++ 8 files changed, 165 insertions(+), 24 deletions(-) delete mode 100644 docs/configuring-playbook-bridge-mautrix-imessage.md create mode 100644 docs/configuring-playbook-bridge-mautrix-wsproxy.md create mode 100644 roles/matrix-nginx-proxy/templates/nginx/conf.d/matrix-mautrix-wsproxy.conf.j2 diff --git a/docs/configuring-playbook-bridge-mautrix-imessage.md b/docs/configuring-playbook-bridge-mautrix-imessage.md deleted file mode 100644 index bc40aba1..00000000 --- a/docs/configuring-playbook-bridge-mautrix-imessage.md +++ /dev/null @@ -1,22 +0,0 @@ -# Setting up Mautrix Whatsapp (optional) - -The playbook can install and configure [mautrix-whatsapp](https://github.com/tulir/mautrix-whatsapp) for you. - -See the project's [documentation](https://github.com/tulir/mautrix-whatsapp/wiki) to learn what it does and why it might be useful to you. - -Use the following playbook configuration: - -```yaml -matrix_mautrix_wsproxy_enabled: true -matrix_mautrix_imessage_user: 'user@domain.com' -``` - - -## Set up Double Puppeting - - - - -## Usage - -You then need to start a chat with `@whatsappbot:YOUR_DOMAIN` (where `YOUR_DOMAIN` is your base domain, not the `matrix.` domain). diff --git a/docs/configuring-playbook-bridge-mautrix-wsproxy.md b/docs/configuring-playbook-bridge-mautrix-wsproxy.md new file mode 100644 index 00000000..ca598abb --- /dev/null +++ b/docs/configuring-playbook-bridge-mautrix-wsproxy.md @@ -0,0 +1,27 @@ +# Setting up Mautrix wsproxy (optional) + +The playbook can install and configure [mautrix-wsproxy](https://github.com/mautrix/wsproxy) for you. + +See the project's [documentation](https://github.com/mautrix/wsproxy#readme) to learn what it does and why it might be useful to you. + +Use the following playbook configuration: + +```yaml +matrix_mautrix_wsproxy_enabled: true +matrix_mautrix_imessage_user: 'user@domain.com' +``` + + +## Set up Double Puppeting + +If you'd like to use [Double Puppeting](https://github.com/tulir/mautrix-hangouts/wiki/Authentication#double-puppeting) (hint: you most likely do), you have 1 way of going about it. + +### Method 1: automatically, by enabling Shared Secret Auth + +The bridge will automatically perform Double Puppeting if you enable [Shared Secret Auth](configuring-playbook-shared-secret-auth.md) for this playbook. + +This is the recommended way of setting up Double Puppeting, as it's easier to accomplish, works for all your users automatically, and has less of a chance of breaking in the future. + +## Usage + +Follow the [matrix-imessage documenation](https://docs.mau.fi/bridges/go/imessage/index.html) for running `matrix-imessage` on your iOS device. diff --git a/group_vars/matrix_servers b/group_vars/matrix_servers index c18561ce..704fdcb4 100755 --- a/group_vars/matrix_servers +++ b/group_vars/matrix_servers @@ -1248,6 +1248,7 @@ matrix_nginx_proxy_proxy_bot_go_neb_enabled: "{{ matrix_bot_go_neb_enabled }}" matrix_nginx_proxy_proxy_jitsi_enabled: "{{ matrix_jitsi_enabled }}" matrix_nginx_proxy_proxy_grafana_enabled: "{{ matrix_grafana_enabled }}" matrix_nginx_proxy_proxy_sygnal_enabled: "{{ matrix_sygnal_enabled }}" +matrix_nginx_proxy_proxy_mautrix_wsproxy_enabled: "{{ matrix_mautrix_wsproxy_enabled }}" matrix_nginx_proxy_proxy_matrix_corporal_api_enabled: "{{ matrix_corporal_enabled and matrix_corporal_http_api_enabled }}" matrix_nginx_proxy_proxy_matrix_corporal_api_addr_with_container: "matrix-corporal:41081" @@ -1330,6 +1331,8 @@ matrix_ssl_domains_to_obtain_certificates_for: | + ([matrix_server_fqn_sygnal] if matrix_sygnal_enabled else []) + + ([matrix_server_fqn_mautrix_wsproxy] if matrix_mautrix_wsproxy_enabled else []) + + ([matrix_domain] if matrix_nginx_proxy_base_domain_serving_enabled else []) + matrix_ssl_additional_domains_to_obtain_certificates_for diff --git a/roles/matrix-base/defaults/main.yml b/roles/matrix-base/defaults/main.yml index ef30ad67..ab0b13f0 100644 --- a/roles/matrix-base/defaults/main.yml +++ b/roles/matrix-base/defaults/main.yml @@ -33,6 +33,9 @@ matrix_server_fqn_grafana: "stats.{{ matrix_domain }}" # This is where you access the Sygnal push gateway. matrix_server_fqn_sygnal: "sygnal.{{ matrix_domain }}" +# This is where you access the mautrix wsproxy push gateway. +matrix_server_fqn_mautrix_wsproxy: "mautrix-wsproxy.{{ matrix_domain }}" + matrix_federation_public_port: 8448 # The architecture that your server runs. diff --git a/roles/matrix-bridge-mautrix-wsproxy/defaults/main.yml b/roles/matrix-bridge-mautrix-wsproxy/defaults/main.yml index 5fd9bb04..18a01794 100644 --- a/roles/matrix-bridge-mautrix-wsproxy/defaults/main.yml +++ b/roles/matrix-bridge-mautrix-wsproxy/defaults/main.yml @@ -14,8 +14,8 @@ matrix_mautrix_wsproxy_data_path: "{{ matrix_mautrix_wsproxy_base_path }}/data" matrix_mautrix_wsproxy_homeserver_address: "{{ matrix_homeserver_container_url }}" matrix_mautrix_wsproxy_homeserver_domain: "{{ matrix_domain }}" -matrix_mautrix_wsproxy_appservice_address: "http://localhost:29331" -matrix_mautrix_wsproxy_appservice_websocket: "ws://localhost:29331" +matrix_mautrix_wsproxy_appservice_address: "http://matrix-mautrix-wsproxy:29331" +matrix_mautrix_wsproxy_appservice_websocket: "ws://matrix-mautrix-wsproxy:29331" # A list of extra arguments to pass to the container matrix_mautrix_wsproxy_container_extra_arguments: [] diff --git a/roles/matrix-nginx-proxy/defaults/main.yml b/roles/matrix-nginx-proxy/defaults/main.yml index e7ee00a5..11d46374 100644 --- a/roles/matrix-nginx-proxy/defaults/main.yml +++ b/roles/matrix-nginx-proxy/defaults/main.yml @@ -145,6 +145,10 @@ matrix_nginx_proxy_proxy_grafana_hostname: "{{ matrix_server_fqn_grafana }}" matrix_nginx_proxy_proxy_sygnal_enabled: false matrix_nginx_proxy_proxy_sygnal_hostname: "{{ matrix_server_fqn_sygnal }}" +# Controls whether proxying the mautrix wsproxy should be done. +matrix_nginx_proxy_proxy_mautrix_wsproxy_enabled: false +matrix_nginx_proxy_proxy_mautrix_wsproxy_hostname: "{{ matrix_server_fqn_mautrix_wsproxy }}" + # Controls whether proxying for the matrix-corporal API (`/_matrix/corporal`) should be done (on the matrix domain) matrix_nginx_proxy_proxy_matrix_corporal_api_enabled: false matrix_nginx_proxy_proxy_matrix_corporal_api_addr_with_container: "matrix-corporal:41081" @@ -274,6 +278,9 @@ matrix_nginx_proxy_proxy_grafana_additional_server_configuration_blocks: [] # A list of strings containing additional configuration blocks to add to Sygnal's server configuration (matrix-sygnal.conf). matrix_nginx_proxy_proxy_sygnal_additional_server_configuration_blocks: [] +# A list of strings containing additional configuration blocks to add to mautrix wsproxy server configuration (matrix-mautrix-wsproxy.conf). +matrix_nginx_proxy_proxy_mautrix_wsproxy_additional_server_configuration_blocks: [] + # A list of strings containing additional configuration blocks to add to the base domain server configuration (matrix-base-domain.conf). matrix_nginx_proxy_proxy_domain_additional_server_configuration_blocks: [] diff --git a/roles/matrix-nginx-proxy/tasks/setup_nginx_proxy.yml b/roles/matrix-nginx-proxy/tasks/setup_nginx_proxy.yml index 1d59f567..e2a6caf7 100644 --- a/roles/matrix-nginx-proxy/tasks/setup_nginx_proxy.yml +++ b/roles/matrix-nginx-proxy/tasks/setup_nginx_proxy.yml @@ -114,6 +114,13 @@ mode: 0644 when: matrix_nginx_proxy_proxy_sygnal_enabled|bool +- name: Ensure Matrix nginx-proxy configuration for mautrix wsproxy exists + template: + src: "{{ role_path }}/templates/nginx/conf.d/matrix-mautrix-wsproxy.conf.j2" + dest: "{{ matrix_nginx_proxy_confd_path }}/matrix-mautrix-wsproxy.conf" + mode: 0644 + when: matrix_nginx_proxy_proxy_mautrix_wsproxy_enabled|bool + - name: Ensure Matrix nginx-proxy configuration for Matrix domain exists template: src: "{{ role_path }}/templates/nginx/conf.d/matrix-domain.conf.j2" @@ -247,6 +254,12 @@ state: absent when: "not matrix_nginx_proxy_proxy_sygnal_enabled|bool" +- name: Ensure Matrix nginx-proxy configuration for mautrix wsproxy deleted + file: + path: "{{ matrix_nginx_proxy_confd_path }}/matrix-mautrix-wsproxy.conf" + state: absent + when: "not matrix_nginx_proxy_proxy_mautrix_wsproxy_enabled|bool" + - name: Ensure Matrix nginx-proxy homepage for base domain deleted file: path: "{{ matrix_nginx_proxy_data_path }}/matrix-domain/index.html" diff --git a/roles/matrix-nginx-proxy/templates/nginx/conf.d/matrix-mautrix-wsproxy.conf.j2 b/roles/matrix-nginx-proxy/templates/nginx/conf.d/matrix-mautrix-wsproxy.conf.j2 new file mode 100644 index 00000000..fd77bad2 --- /dev/null +++ b/roles/matrix-nginx-proxy/templates/nginx/conf.d/matrix-mautrix-wsproxy.conf.j2 @@ -0,0 +1,110 @@ +#jinja2: lstrip_blocks: "True" + +{% macro render_vhost_directives() %} + gzip on; + gzip_types text/plain application/json application/javascript text/css image/x-icon font/ttf image/gif; + + {% if matrix_nginx_proxy_hsts_preload_enabled %} + add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always; + {% else %} + add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always; + {% endif %} + add_header X-Content-Type-Options nosniff; + add_header X-XSS-Protection "{{ matrix_nginx_proxy_xss_protection }}"; + add_header X-Frame-Options SAMEORIGIN; + + {% if matrix_nginx_proxy_floc_optout_enabled %} + add_header Permissions-Policy interest-cohort=() always; + {% endif %} + + + {% for configuration_block in matrix_nginx_proxy_proxy_mautrix_wsproxy_additional_server_configuration_blocks %} + {{- configuration_block }} + {% endfor %} + + location / { + {% if matrix_nginx_proxy_enabled %} + {# Use the embedded DNS resolver in Docker containers to discover the service #} + resolver 127.0.0.11 valid=5s; + set $backend "matrix-mautrix-wsproxy:29331"; + proxy_pass http://$backend; + {% else %} + {# Generic configuration for use outside of our container setup #} + proxy_pass http://127.0.0.1:29331; + {% endif %} + + proxy_set_header Host $host; + proxy_set_header X-Forwarded-For $remote_addr; + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection "upgrade"; + proxy_http_version 1.1; + proxy_send_timeout 1d; + proxy_read_timeout 1d; + + tcp_nodelay on; + } +{% endmacro %} + +server { + listen {{ 8080 if matrix_nginx_proxy_enabled else 80 }}; + + server_name {{ matrix_nginx_proxy_proxy_mautrix_wsproxy_hostname }}; + + server_tokens off; + root /dev/null; + + {% if matrix_nginx_proxy_https_enabled %} + location /.well-known/acme-challenge { + {% if matrix_nginx_proxy_enabled %} + {# Use the embedded DNS resolver in Docker containers to discover the service #} + resolver 127.0.0.11 valid=5s; + set $backend "matrix-certbot:8080"; + proxy_pass http://$backend; + {% else %} + {# Generic configuration for use outside of our container setup #} + proxy_pass http://127.0.0.1:{{ matrix_ssl_lets_encrypt_certbot_standalone_http_port }}; + {% endif %} + } + + location / { + return 301 https://$http_host$request_uri; + } + {% else %} + {{ render_vhost_directives() }} + {% endif %} +} + +{% if matrix_nginx_proxy_https_enabled %} +server { + listen {{ 8443 if matrix_nginx_proxy_enabled else 443 }} ssl http2; + listen [::]:{{ 8443 if matrix_nginx_proxy_enabled else 443 }} ssl http2; + + server_name {{ matrix_nginx_proxy_proxy_mautrix_wsproxy_hostname }}; + + server_tokens off; + root /dev/null; + + ssl_certificate {{ matrix_ssl_config_dir_path }}/live/{{ matrix_nginx_proxy_proxy_mautrix_wsproxy_hostname }}/fullchain.pem; + ssl_certificate_key {{ matrix_ssl_config_dir_path }}/live/{{ matrix_nginx_proxy_proxy_mautrix_wsproxy_hostname }}/privkey.pem; + + ssl_protocols {{ matrix_nginx_proxy_ssl_protocols }}; + {% if matrix_nginx_proxy_ssl_ciphers != "" %} + ssl_ciphers {{ matrix_nginx_proxy_ssl_ciphers }}; + {% endif %} + ssl_prefer_server_ciphers {{ matrix_nginx_proxy_ssl_prefer_server_ciphers }}; + + {% if matrix_nginx_proxy_ocsp_stapling_enabled %} + ssl_stapling on; + ssl_stapling_verify on; + ssl_trusted_certificate {{ matrix_ssl_config_dir_path }}/live/{{ matrix_nginx_proxy_proxy_mautrix_wsproxy_hostname }}/chain.pem; + {% endif %} + + {% if matrix_nginx_proxy_ssl_session_tickets_off %} + ssl_session_tickets off; + {% endif %} + ssl_session_cache {{ matrix_nginx_proxy_ssl_session_cache }}; + ssl_session_timeout {{ matrix_nginx_proxy_ssl_session_timeout }}; + + {{ render_vhost_directives() }} +} +{% endif %}