Compare commits

...
142 Commits
Author SHA1 Message Date
Jason Volk af570c481e Bump 1.4.8
Signed-off-by: Jason Volk <jason@zemos.net>
2025-12-21 19:21:27 +00:00
Jason Volk dc9314de1f Fix missing validations for federation invites.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-12-21 12:34:58 +00:00
June Strawberry cf005ee537 bump ruma to fix join_rule being omitted on default value in /publicRooms
also updates complement results for now passing TestKnockRoomsInPublicRoomsDirectory

Signed-off-by: June Strawberry <june@vern.cc>
2025-12-20 22:22:40 -05:00
June Strawberry 6455ef72cd bump ldap3 fork, initialise aws_lc_rs with single function for ldap and direct tls
Signed-off-by: June Strawberry <june@vern.cc>
2025-12-20 22:16:27 -05:00
dasha_uwu 7115fb2796 Refactor join, alias services
Split knock, user register from api into services

Fix autojoin not working with v12 rooms

Fix 'm.login.registration_token/validity' for reloaded registration tokens

Change join servers order

Move autojoin for ldap
2025-12-18 18:41:23 +00:00
Jason Volk 959c559bd8 Log all ruma handler results at trace level.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-12-18 17:27:22 +00:00
Jason Volk a1bf188504 Update some links to project documentation.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-12-18 16:54:19 +00:00
Jason Volk 87770fefeb Bump dependencies.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-12-18 02:43:35 +00:00
dasha_uwu 0c7ba1dd5a Fix sync handling for appservices calling without device_id 2025-12-18 02:18:30 +00:00
dasha_uwu 7b2079f714 Check PDU formats 2025-12-18 01:53:09 +00:00
dasha_uwu c5508bba58 Fix appservices not receiving membership events to sender_localpart
Simplify sending PDUs to appservices
2025-12-18 01:49:54 +00:00
Jason Volk 88a6b72f0f Update config comment for default hydra backports.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-12-18 01:49:54 +00:00
Jason Volk 22c5f0207d Fix chat badge hyperlink too.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-12-18 01:49:45 +00:00
Jason Volk 3af78ac851 Fix readme documentation link; tweak wording. [ci skip]
Signed-off-by: Jason Volk <jason@zemos.net>
2025-12-17 14:34:14 +00:00
dasha_uwu b5f50c3fda Allow using OpenID userinfo endpoint with federation disabled, fixes #240
Even though it's in SS API this is not really related to federation, so
it makes sense to allow using this endpoint with allow_federation=false
2025-12-17 08:51:29 +00:00
Jason Volk c9a26a0280 Bump rust-rocksdb, jevmalloc. (fixes #238) (fixes #239)
Signed-off-by: Jason Volk <jason@zemos.net>
2025-12-17 08:51:26 +00:00
Jason Volk 417e9ba052 Update README; additional docs link. (closes #235)
Signed-off-by: Jason Volk <jason@zemos.net>
2025-12-17 06:53:06 +00:00
yefim 085cdb30f4 Fix newly created LDAP users not added to auto-join-rooms 2025-12-17 06:53:06 +00:00
Jason Volk 77474479b5 audit: Ignore unmaintained rustls-pemfile. (RUSTSEC-2025-0134)
Signed-off-by: Jason Volk <jason@zemos.net>
2025-12-17 06:53:06 +00:00
Alexandru Scvorțov d244e8027c Update README to say matrix-tuwunel is in stable NixOS [ci skip] 2025-12-17 04:39:38 +00:00
Jason Volk 4fea0abac4 Bump 1.4.7
Signed-off-by: Jason Volk <jason@zemos.net>
2025-12-03 16:40:08 +00:00
Jason Volk cab0b3fd9f Reduce join-server selection randomness with separated sets.
Keep alias server biased at the front unless configured deprioritized.

Signed-off-by: Jason Volk <jason@zemos.net>
2025-12-03 09:23:33 +00:00
Jason Volk f311332bad Add configuration for make_join and send_join attempt counts.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-12-03 09:23:33 +00:00
Jason Volk fb0c2a2832 Allow clippy::useless-let-if-seq.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-12-03 09:23:29 +00:00
yefim 3403943880 Fix LDAP unauthorized 2025-12-02 18:34:18 +02:00
Jason Volk 7e69e9b051 Bump jemalloc.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-12-01 08:39:44 +00:00
Jason Volk 27ed9b88f1 Bump Complement.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-12-01 08:21:53 +00:00
Jason Volk 10edc3bd5e Disconnect jemalloc stats and prof until after default feature-shifting.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-12-01 08:21:53 +00:00
Jason Volk 6553ba829f Fix room-leave conditions for rooms without joined users or remote servers.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-12-01 08:01:52 +00:00
Jason Volk 1ce3d2b01f Add ReadyBoolExt special case for ReadyEqExt.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-12-01 08:01:52 +00:00
Jason Volk 50bfb0fe5e Provide default for empty PathAndQuery (Sentry ID c09f58adf5...)
Signed-off-by: Jason Volk <jason@zemos.net>
2025-12-01 05:20:26 +00:00
Jason Volk 74f8cd3708 Stop panic propagation on malloc_stats_print() callback.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-12-01 05:20:26 +00:00
Jason Volk bcc2be7661 docs: Use a !tip for the generic deployment sub-header.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-11-30 00:59:06 +00:00
Jason Volk 4f9b1d6dbd Add config options to coarsely disable log/tracing without initialization.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-11-30 00:52:55 +00:00
Jason Volk c9362b8605 Optimize formatted event serializations.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-11-29 12:26:49 +00:00
Jason Volk e84d6666c0 Optimize separate constraint for each stream argument.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-11-27 11:13:27 +00:00
Jason Volk 7666bb63d8 Optimize syncv3 joined room path with additional conditional queries.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-11-27 07:22:55 +00:00
Jason Volk 3125b7e291 Actually re-establish and assert Syncness of Error. (98affbdeaf)
Signed-off-by: Jason Volk <jason@zemos.net>
2025-11-27 07:22:55 +00:00
Jason Volk aebe2d72de Add utils for asserting common marker traits.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-11-27 07:22:55 +00:00
Jason Volk 8f54d9dc09 Replace unnecessary exchange util.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-11-27 07:22:55 +00:00
Jason Volk 52b156e034 Implement MSC4289/MSC4291 (room version 12) upgrade support. (closes #141)
Signed-off-by: Jason Volk <jason@zemos.net>
2025-11-27 00:22:45 +00:00
Jason Volk d2d6a98180 Improve backfill server selection post-v12 power and creator changes.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-11-27 00:22:45 +00:00
Jason Volk 0bbc228f7a Bump Ruma for context events_before serialization.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-11-26 23:25:18 +00:00
Jason Volk 0d782095ad Refactor room upgrade endpoint; rollback on failure.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-11-26 08:16:01 +00:00
Jason Volk f6b95ff1c4 Cleanup and optimize several user_can conditions.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-11-26 08:16:01 +00:00
Jason Volk 347298d7d6 Additional json/jsint Error cases for ErrorKind and StatusCode.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-11-26 06:49:57 +00:00
Jason Volk 442a5aafeb Derive or impl fmt::Debug on several structs.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-11-26 06:49:57 +00:00
Jason Volk 8bb0d02619 Replace calls through sender request interface.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-11-26 06:49:57 +00:00
Jason Volk 71f3ccf140 Unbox and pin database streams.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-11-26 06:49:57 +00:00
Jason Volk 98affbdeaf Re-establish Syncness of Error.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-11-26 03:02:26 +00:00
Jason Volk e5073165f0 Cleanup TryIgnore/TryExpect stream extensions related.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-11-26 03:02:26 +00:00
Jason Volk 6705efc760 Fix lib search path for liburing.a on redhat/centos.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-11-23 21:50:14 +00:00
Jason Volk 61085f4707 Bump RocksDB to 10.7.5-tuwunel.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-11-23 03:36:17 +00:00
Jason Volk deb5c65885 docker: Add conditions to set assert_linkage for Dockerfile.install
ci: Check packages when using test branch.

Signed-off-by: Jason Volk <jason@zemos.net>
2025-11-23 03:36:17 +00:00
Jason Volk 11c4cbf54e docker: Build RocksDB with NUMA policy support.
docker: Build RocksDB with LTO.

Signed-off-by: Jason Volk <jason@zemos.net>
2025-11-23 03:30:19 +00:00
Jason Volk a748edd621 Reduce high volume of strings from FedDest and ActualDest in resolver.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-11-23 03:30:19 +00:00
Jason Volk 9e539d0a22 Activate recursive relations. Maximum fan-out.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-11-21 03:16:01 +00:00
Jason Volk 5260912c3b Add admin configurable Access-Control-Allow-Origin.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-11-20 08:34:31 +00:00
Jason Volk b924412efb Reduce tracing level of request handler return value.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-11-20 08:34:31 +00:00
Jason Volk 120ab1d068 Add config circuit-breaker for heroes calculations during sync.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-11-20 08:34:31 +00:00
Jason Volk e60e86e9ed Optimize heroes calculation using member state instead of timeline scan.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-11-19 02:14:35 +00:00
Jason Volk 5147b541b5 Fix relations pagination compliance.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-11-19 01:43:28 +00:00
Jason Volk f4eeaaf167 Remove unknown multilingual directive from book.toml.
Fix and improve mdbook related.

Signed-off-by: Jason Volk <jason@zemos.net>
2025-11-18 19:46:25 +00:00
Jason Volk 54fe4bdf56 docker: Move complement logs into tests directory.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-11-18 19:29:21 +00:00
Jason Volk fe12daead9 Fix short-circuit condition; propagate 403 in unlikely branch anyway. (fixes #219)
Signed-off-by: Jason Volk <jason@zemos.net>
2025-11-18 19:29:21 +00:00
Jason Volk 6f29a34ffb Fix existing event reinsertions during backfill handling.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-11-18 19:29:21 +00:00
Jason Volk dafbe59d00 Return whether event already existed from event_handler.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-11-18 19:29:21 +00:00
Jason Volk 0746f4b1ad Eliminate clone of Pdu to queue during relations recursions.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-11-18 10:47:59 +00:00
Jason Volk 90228e4865 Combine pdu_metadata Data into Service unit.
Move recursive component of get_relations from service to api crate.

Signed-off-by: Jason Volk <jason@zemos.net>
2025-11-18 10:47:59 +00:00
Jason Volk 53b5eb4ba6 ci: Re-enable lychee.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-11-18 10:47:59 +00:00
dasha_uwu 20f080fc49 Fix typo. 2025-11-18 10:36:02 +00:00
Jason Volk 424ed3d7ad Bump dependencies.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-11-18 03:56:37 +00:00
Tails32 728085bd1b Remove allow_check_for_updates from configs [ci skip]
to suppress a warning at startup due to removal of the update module in edb245a2ba
2025-11-16 01:05:08 +01:00
Jason Volk 64a029ee09 Bump 1.4.6
Signed-off-by: Jason Volk <jason@zemos.net>
2025-11-06 19:34:51 +00:00
Jason Volk a3f6971579 docker: Add rust-sdk memcheck target.
docker: Separate and tweak cargo cache paths.

ci: Improve memcheck job targets.

cargo: Add missing debug syms to bench profile.

docker: Optimize layer copying.

Signed-off-by: Jason Volk <jason@zemos.net>
2025-11-06 19:34:51 +00:00
Jason Volk 51681aec1b docker: Bump complement.
ci: Run complement with optimizations.

Signed-off-by: Jason Volk <jason@zemos.net>
2025-11-06 17:14:45 +00:00
Jason Volk 39c84fabb4 Optimize filter fetch in syncv3 prologue.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-11-06 17:14:45 +00:00
Jason Volk f77bd41837 Add empty config default for rtc_transports to not generate false.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-11-06 17:14:45 +00:00
Jason Volk 6f34b8e9ca Fix missing pushrules evaluation on federation invite.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-11-06 17:14:45 +00:00
Jason Volk 5051da493a Fix pushrule evals relying on non-option RoomPowerLevels.
Add option to bypass pushrules to send everything to pushers.

Improve robustness of pushrule eval loops.

Signed-off-by: Jason Volk <jason@zemos.net>
2025-11-06 17:14:45 +00:00
Jason Volk ff0e007c45 Fix readability of admin query account_data outputs.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-11-06 17:14:45 +00:00
Alexandru Scvorțov b85fb5ea6f Fix tests in nix and re-enable 2025-11-06 16:38:13 +00:00
dasha_uwu e905538269 Shuffle additional join servers. 2025-11-04 06:40:51 +00:00
Jason Volk 6f672b7304 Handle missing media dir as individual item errors w/o aborting check. (closes #213)
Signed-off-by: Jason Volk <jason@zemos.net>
2025-11-04 06:40:51 +00:00
Jason Volk 4363ed6ec3 Adapt MSC4143 rtc_foci support to Ruma types. (e1f89b69ea)
Signed-off-by: Jason Volk <jason@zemos.net>
2025-11-04 06:40:51 +00:00
Jason Volk dd50a4cb0b Fix ignored column descriptions; enhance preservation behavior. (7320d0a40b)
Signed-off-by: Jason Volk <jason@zemos.net>
2025-11-03 23:02:42 +00:00
tototomate123 e0a997c227 docs(reverse-proxy): restructure and add nginx guide
- Moved Caddy and Nginx configuration examples to separate files
- Added documentation for Nginx setup
- Updated generic reverse proxy section
2025-11-03 12:49:04 +00:00
tototomate123 e1f89b69ea feat: Add Element Call / MatrixRTC support 2025-11-03 11:36:19 +00:00
dasha_uwu 888f72d8d0 Generalize common_prefix for AsStr inputs.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-11-03 11:36:19 +00:00
Jason Volk 06618eadab Re-establish federating with several Conduit endpoints.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-11-03 11:36:19 +00:00
Benedikt Vollmerhaus 05390d6097 Fix shared TURN secret from turn_secret_file not being used 2025-11-03 12:30:00 +01:00
Jason Volk 1f803fe3a9 Fix verification loss; workaround Nheko-Reborn/nheko#1908 (closes #146)
Signed-off-by: Jason Volk <jason@zemos.net>
2025-11-03 04:45:31 +00:00
Jason Volk 1492d68e25 Additional tracing of request body.
Optimize router request parse types.

Signed-off-by: Jason Volk <jason@zemos.net>
2025-11-03 04:45:31 +00:00
Jason Volk c1aa94fb91 Describe roomsynctoken_shortstatehash for deletion.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-11-03 02:05:35 +00:00
Jason Volk 7320d0a40b Improve config options for missing and dropping db columns.
Implement actual drop functionality.

Signed-off-by: Jason Volk <jason@zemos.net>
2025-11-03 02:05:35 +00:00
Jason Volk abded2d442 Allow guests even when token required for normal registration. (closes #189)
Signed-off-by: Jason Volk <jason@zemos.net>
2025-11-02 18:19:11 +00:00
Jason Volk 4afd6f347b Implement GET /_matrix/client/v3/events live room previews.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-11-02 08:06:32 +00:00
Jason Volk 6b8d6956a3 Fix timeline events order in /initialSync.
Implement acccount_data for response.

Ensure consistency of revealed events.

Signed-off-by: Jason Volk <jason@zemos.net>
2025-11-02 08:06:32 +00:00
Jason Volk f59d62c01c Bump Ruma for CanonicalJson property name optimizations.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-11-02 03:57:11 +00:00
Jason Volk a14556da97 Implement encryption_enabled_by_default_for_room_type. (closes #29)
Signed-off-by: Jason Volk <jason@zemos.net>
2025-11-02 00:02:49 +00:00
Jason Volk 8b1de3d8db Add update for device last_seen_ts. (closes #40)
Piggyback on presence ping for controlled device seen updates.

Signed-off-by: Jason Volk <jason@zemos.net>
2025-11-02 00:02:49 +00:00
Jason Volk 240b498489 Add unwrap suite to future::OptionExt extensions.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-11-02 00:02:49 +00:00
Jason Volk d680a6ba53 Bump Ruma for device type optimizations.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-11-02 00:02:49 +00:00
Jason Volk aa3f14cd57 Optimize federation user devices response.
Modernize some error branches.

Signed-off-by: Jason Volk <jason@zemos.net>
2025-11-01 23:15:37 +00:00
Jason Volk 15627bc8d0 Simplify device metadata update interface.
Add broadcast argument; reorder notify to after write.

Signed-off-by: Jason Volk <jason@zemos.net>
2025-11-01 23:15:37 +00:00
Jason Volk 084facf474 Move user profile related functions to profile unit.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-11-01 23:15:37 +00:00
Jason Volk d24986edf1 sliding-sync: Stop propagation from room handlers.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-10-31 03:38:35 +00:00
Jason Volk ce1ac277a6 Refactor sync v3 leave handler.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-10-31 03:38:35 +00:00
Jason Volk 7aeed0a95a Fix next_batch pagination tokens in relations response.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-10-31 03:38:35 +00:00
Jason Volk 9265748a57 Remove legacy ruma return type on sync endpoint.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-10-30 16:28:27 +00:00
Jason Volk e85cfdf48a Remove errant inline. (48923b3657)
Signed-off-by: Jason Volk <jason@zemos.net>
2025-10-30 14:01:57 +00:00
Jason Volk 48923b3657 Implement notifications retrieval. (closes #201)
Signed-off-by: Jason Volk <jason@zemos.net>
2025-10-30 03:48:16 +00:00
Jason Volk aedaf3f0c1 Fix pushing useless notifications.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-10-30 03:48:16 +00:00
Jason Volk b24b59dc38 Add admin query raw command to clear table.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-10-30 03:48:16 +00:00
Jason Volk 738b5e3fa5 Add attempted newtype struct name to deserialization error message.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-10-30 03:48:16 +00:00
Jason Volk 1f0cfec5ca Use Result for last_notification_read as zero-default not appropriate.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-10-30 03:48:16 +00:00
Jason Volk 9c5caa3a5f Move appservice request unit from sender to appservice.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-10-30 03:48:16 +00:00
Jason Volk 1cf4a26ae9 docker: Use prefix match for nightly rust_toolchain settings.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-10-30 03:48:16 +00:00
Jason Volk 3694ffbab3 Bump Ruma w/ PushRule type optimizations.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-10-28 23:23:27 +00:00
Jason Volk af0e01e016 Add util to assist with Display/to_string for SmallString.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-10-28 23:23:27 +00:00
Jason Volk 5e89f0acae Split pusher service send/request into unit.
Refactor sender's push destination handler.

Combine remnants of service::rooms::user with pusher service.

Further split and reorg pusher service units.

Signed-off-by: Jason Volk <jason@zemos.net>
2025-10-28 23:23:19 +00:00
Jason Volk 563873af77 Use signed PduCount for better bumpstamp on create and backfill.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-10-27 15:24:50 +00:00
Jason Volk d2072080c9 Add admin query for remote alias resolve.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-10-27 15:24:17 +00:00
Jason Volk e191730950 Tracing span adjustments for ret verbosity.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-10-27 15:15:12 +00:00
Jason Volk f660e00bb5 Implement Dehydrated Devices MSC3814 (closes #200)
Signed-off-by: Jason Volk <jason@zemos.net>
2025-10-27 12:06:43 +00:00
Jason Volk f613d0c2ad Add admin command to delete a user's device.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-10-27 10:55:12 +00:00
Jason Volk 7596ad2019 Add rountripping test for database cbor serialization.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-10-27 10:41:49 +00:00
Jason Volk 384add9784 ci: Extract Nix artifacts.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-10-27 09:39:57 +00:00
Jason Volk a234f019b1 Advertise MSC2659 support; reorder features by MSC ascending.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-10-27 09:06:02 +00:00
Jason Volk e8a87bdfa3 Remove DynamicUser from systemd files (closes #207).
Signed-off-by: Jason Volk <jason@zemos.net>
2025-10-27 09:06:02 +00:00
Jason Volk 6a4aff424f Add count value to the to_device iter item.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-10-27 09:06:02 +00:00
Jason Volk 8959d9e2c1 Add bulk one_time_keys adder to interface.
Add device_exists to interface.

Signed-off-by: Jason Volk <jason@zemos.net>
2025-10-27 09:06:02 +00:00
Jason Volk a30c043386 Support creating devices without access_tokens.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-10-27 09:06:02 +00:00
Jason Volk a39ef994d2 Add is_err_or to Result utils.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-10-27 09:06:02 +00:00
Jason Volk b714f24029 sliding-sync: Filter redacted events from bump calculation.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-10-27 09:06:02 +00:00
Jason Volk 2263f2e874 docker: Add dirty-runs option passed to complement.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-10-27 09:06:02 +00:00
Jason Volk 9abe9becd6 Split pusher/push-rule from timeline append.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-10-27 09:06:02 +00:00
Jason Volk 4b74c01895 Split appservice dissemination from timeline::append().
Signed-off-by: Jason Volk <jason@zemos.net>
2025-10-27 09:06:02 +00:00
Jason Volk 1223763e2b Split sliding-sync into additional units.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-10-27 09:06:02 +00:00
Jason Volk 9b64c1f105 Bump Nix flake. 2025-10-27 09:06:02 +00:00
Jason Volk 212a8434a8 Bump Rust 1.89
We can't upgrade to 1.90 because NixOS breaks. 🙂‍↔️

Signed-off-by: Jason Volk <jason@zemos.net>
2025-10-27 09:06:02 +00:00
Jason Volk bfaac8b5a2 Bump dependencies.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-10-25 17:46:25 +00:00
235 changed files with 7999 additions and 5820 deletions

No files matched your search

+1 -1
View File
@@ -1,5 +1,5 @@
[advisories]
ignore = ["RUSTSEC-2024-0436", "RUSTSEC-2025-0014"] # advisory IDs to ignore e.g. ["RUSTSEC-2019-0001", ...]
ignore = ["RUSTSEC-2024-0436", "RUSTSEC-2025-0014", "RUSTSEC-2025-0134"] # advisory IDs to ignore e.g. ["RUSTSEC-2019-0001", ...]
informational_warnings = [] # warn for categories of informational advisories
severity_threshold = "none" # CVSS severity ("none", "low", "medium", "high", "critical")
-2
View File
@@ -106,7 +106,6 @@ jobs:
excludes: ${{inputs.excludes}}
includes: ${{inputs.includes}}
# disabled due to excessive build time issue installing cargo lychee
lychee:
if: >
!failure() && !cancelled()
@@ -114,7 +113,6 @@ jobs:
&& contains(fromJSON(inputs.feat_sets), fromJSON('["all"]')[0])
&& contains(fromJSON(inputs.rust_toolchains), fromJSON('["nightly"]')[0])
&& contains(fromJSON(inputs.sys_targets), fromJSON('["x86_64-v1-linux-gnu"]')[0])
&& false
name: Lychee
uses: ./.github/workflows/bake.yml
+12 -11
View File
@@ -20,13 +20,13 @@ jobs:
name: Init
runs-on: ${{matrix.runner}}
env:
default_cargo_profiles: '["test", "release"]'
default_cargo_profiles: '["test", "bench", "release"]'
default_feat_sets: '["none", "default", "all"]'
default_rust_toolchains: '["nightly", "stable"]'
default_sys_names: '["debian"]'
default_sys_versions: '["testing-slim"]'
default_rust_targets: '["x86_64-unknown-linux-gnu"]'
default_sys_targets: '["x86_64-v1-linux-gnu"]'
default_sys_targets: '["x86_64-v1-linux-gnu", "x86_64-v3-linux-gnu"]'
default_machines: '["X64"]'
outputs:
@@ -38,17 +38,17 @@ jobs:
sys_targets: ${{vars.SYS_TARGETS || env.default_sys_targets}}
sys_versions: ${{vars.SYS_VERSIONS || env.default_sys_versions}}
machines: ${{vars.MACHINES || env.default_machines}}
package: ${{vars.PACKAGE || !contains(github.ref, 'refs/pull/')}}
publish: ${{vars.PUBLISH || !contains(github.ref, 'refs/pull/')}}
build_nix: ${{vars.BUILD_NIX || !contains(github.ref, 'refs/pull/')}}
build_pkgs: ${{vars.BUILD_PKGS || github.ref == 'refs/heads/main' || contains(github.ref, 'tags/v')}}
check_pkgs: ${{vars.CHECK_PKGS || 'false'}}
complement: ${{vars.COMPLEMENT || 'true'}}
package: ${{vars.PACKAGE != 'false'}}
publish: ${{vars.PUBLISH != 'false'}}
build_nix: ${{vars.BUILD_NIX != 'false'}}
build_pkgs: ${{vars.BUILD_PKGS || github.ref == 'refs/heads/main' || github.ref == 'refs/heads/test' || contains(github.ref, 'tags/v')}}
check_pkgs: ${{vars.CHECK_PKGS || github.ref == 'refs/heads/test'}}
complement: ${{vars.COMPLEMENT != 'false'}}
complement_runner: 'het'
docker_repo: ${{vars.DOCKER_REPO}}
release_url: ${{steps.release.outputs.upload_url}}
pages_url: 'https://matrix-construct.github.io/tuwunel/'
rust_sdk_integration: ${{vars.RUST_SDK_INTEGRATION || 'true'}}
rust_sdk_integ: ${{vars.RUST_SDK_INTEGRATION != 'false'}}
head_msg: ${{github.event.head_commit.message || github.event.workflow_run.head_commit.message}}
strategy:
@@ -176,7 +176,7 @@ jobs:
with:
head_msg: ${{needs.init.outputs.head_msg}}
build_nix: ${{fromJSON(needs.init.outputs.build_nix)}}
rust_sdk_integration: ${{fromJSON(needs.init.outputs.rust_sdk_integration)}}
rust_sdk_integ: ${{fromJSON(needs.init.outputs.rust_sdk_integ)}}
complement: ${{fromJSON(needs.init.outputs.complement)}}
complement_runner: ${{needs.init.outputs.complement_runner}}
cargo_profiles: ${{needs.init.outputs.cargo_profiles}}
@@ -189,7 +189,6 @@ jobs:
machines: ${{needs.init.outputs.machines}}
excludes: >
[
{"feat_set": "logging"},
{"cargo_profile": "test", "rust_toolchain": "stable", "feat_set": "none"},
{"cargo_profile": "test", "rust_target": "aarch64-unknown-linux-gnu"},
{"cargo_profile": "release-debuginfo"},
@@ -223,6 +222,7 @@ jobs:
if: >
!failure() && !cancelled()
&& needs.init.outputs.package
&& !contains(github.ref, 'refs/pull')
&& !contains(needs.init.outputs.head_msg, '[ci only it]')
&& !contains(needs.init.outputs.head_msg, '[ci no build]')
&& !contains(needs.init.outputs.head_msg, '[ci no package]')
@@ -286,6 +286,7 @@ jobs:
if: >
!failure() && !cancelled()
&& needs.init.outputs.publish
&& !contains(github.ref, 'refs/pull')
&& !contains(needs.init.outputs.head_msg, '[ci only it]')
&& !contains(needs.init.outputs.head_msg, '[ci no build]')
&& !contains(needs.init.outputs.head_msg, '[ci no package]')
+3 -3
View File
@@ -181,10 +181,10 @@ jobs:
"mime": "application/x-rpm"
},
"nix": {
"dst": "tuwunel.drv.tar.zst",
"src": "/opt/tuwunel/tuwunel.drv",
"dst": "tuwunel.nix.tar.zst",
"src": "/opt/tuwunel.nix.tar",
"mime": "application/zstd",
"zstd": 6
"zstd": 11
}
}
+39 -31
View File
@@ -38,12 +38,9 @@ on:
complement:
type: boolean
default: true
complement_feat_sets:
type: string
default: '["all"]'
complement_runner:
type: string
rust_sdk_integration:
rust_sdk_integ:
type: boolean
default: true
build_nix:
@@ -84,10 +81,10 @@ jobs:
&& contains(fromJSON(inputs.rust_toolchains), fromJSON('["nightly"]')[0])
&& contains(fromJSON(inputs.sys_targets), fromJSON('["x86_64-v1-linux-gnu"]')[0])
name: Unit
name: Module
uses: ./.github/workflows/bake.yml
with:
bake_targets: '["unit", "integration"]'
bake_targets: '["unit", "integ"]'
cargo_profiles: '["test"]'
feat_sets: '["all"]'
rust_toolchains: ${{inputs.rust_toolchains}}
@@ -110,7 +107,7 @@ jobs:
name: Bench
uses: ./.github/workflows/bake.yml
with:
bake_targets: '["unit", "integration"]'
bake_targets: '["unit", "integ"]'
cargo_profiles: '["bench"]'
feat_sets: '["all"]'
rust_toolchains: '["nightly"]'
@@ -122,7 +119,7 @@ jobs:
excludes: ${{inputs.excludes}}
includes: ${{inputs.includes}}
valgrind:
memcheck:
if: >
!contains(inputs.head_msg, '[ci only it]')
&& contains(fromJSON(inputs.cargo_profiles), fromJSON('["bench"]')[0])
@@ -133,8 +130,9 @@ jobs:
name: Memcheck
uses: ./.github/workflows/bake.yml
with:
bake_targets: '["integration-valgrind"]'
cargo_profiles: '["bench"]'
#bake_targets: '["unit-valgrind", "integ-valgrind"]'
bake_targets: '["integ-valgrind"]'
cargo_profiles: '["bench"]' # use bench not release for debug syms
feat_sets: '["all"]'
rust_toolchains: '["nightly"]'
sys_names: ${{inputs.sys_names}}
@@ -142,8 +140,8 @@ jobs:
rust_targets: ${{inputs.rust_targets}}
sys_targets: '["x86_64-v3-linux-gnu"]'
machines: ${{inputs.machines}}
excludes: ${{inputs.excludes}}
includes: ${{inputs.includes}}
excludes: ${{inputs.excludes}}
smoke:
if: >
@@ -225,6 +223,7 @@ jobs:
!failure() && !cancelled()
&& !contains(inputs.head_msg, '[ci only it]')
&& !contains(inputs.head_msg, '[ci no build]')
&& !contains(github.ref, 'refs/pull')
&& contains(fromJSON(inputs.cargo_profiles), fromJSON('["release"]')[0])
&& contains(fromJSON(inputs.feat_sets), fromJSON('["all"]')[0])
&& contains(fromJSON(inputs.rust_toolchains), fromJSON('["stable"]')[0])
@@ -246,45 +245,56 @@ jobs:
excludes: ${{inputs.excludes}}
includes: ${{inputs.includes}}
matrix-rust-sdk-integration:
rust-sdk-integ:
if: >
!failure() && !cancelled()
&& !contains(inputs.head_msg, '[ci no build]')
&& inputs.rust_sdk_integration
&& inputs.rust_sdk_integ
&& contains(fromJSON(inputs.feat_sets), fromJSON('["all"]')[0])
&& contains(fromJSON(inputs.rust_toolchains), fromJSON('["nightly"]')[0])
&& contains(fromJSON(inputs.rust_targets), fromJSON('["x86_64-unknown-linux-gnu"]')[0])
&& contains(fromJSON(inputs.sys_targets), fromJSON('["x86_64-v1-linux-gnu"]')[0])
name: Matrix SDK Integration
needs: [smoke]
uses: ./.github/workflows/bake.yml
with:
bake_targets: '["matrix-rust-sdk-integration"]'
#bake_targets: '["rust-sdk-integ", "rust-sdk-valgrind"]'
bake_targets: '["rust-sdk-integ"]'
cargo_profiles: ${{inputs.cargo_profiles}}
feat_sets: '["all"]'
rust_toolchains: '["nightly"]'
sys_names: ${{inputs.sys_names}}
sys_versions: ${{inputs.sys_versions}}
rust_targets: '["x86_64-unknown-linux-gnu"]'
sys_targets: '["x86_64-v1-linux-gnu"]'
sys_targets: ${{inputs.sys_targets}}
machines: '["X64"]'
runner: ${{inputs.complement_runner}}
includes: ${{inputs.includes}}
artifact: >
{
"matrix-rust-sdk-integration": {
"rust-sdk-integ": {
"src": "/var/log/tuwunel.log",
"dst": "matrix-rust-sdk-integration.tuwunel.log",
"dst": "rust-sdk-integ.tuwunel.log",
},
"rust-sdk-valgrind": {
"src": "/var/log/tuwunel.log",
"dst": "rust-sdk-valgrind.tuwunel.log",
}
}
excludes: >
[
{"bake_target": "rust-sdk-valgrind", "cargo_profile": "test"},
{"feat_set": "none"},
{"feat_set": "logging"},
{"cargo_profile": "release"},
{"cargo_profile": "release-debuginfo"},
{"cargo_profile": "release-native"},
{"cargo_profile": "test", "sys_target": "x86_64-v2-linux-gnu"},
{"cargo_profile": "test", "sys_target": "x86_64-v3-linux-gnu"},
{"cargo_profile": "bench", "sys_target": "x86_64-v1-linux-gnu"},
{"cargo_profile": "bench", "sys_target": "x86_64-v2-linux-gnu"},
{"rust_target": "aarch64-unknown-linux-gnu"},
{"sys_target": "aarch64-v8-linux-gnu"},
]
complement:
@@ -292,22 +302,21 @@ jobs:
!failure() && !cancelled()
&& !contains(inputs.head_msg, '[ci no build]')
&& inputs.complement
&& contains(fromJSON(inputs.cargo_profiles), fromJSON('["test"]')[0])
&& contains(fromJSON(inputs.complement_feat_sets), fromJSON('["all"]')[0])
&& contains(fromJSON(inputs.cargo_profiles), fromJSON('["bench"]')[0])
&& contains(fromJSON(inputs.rust_toolchains), fromJSON('["nightly"]')[0])
&& contains(fromJSON(inputs.sys_targets), fromJSON('["x86_64-v1-linux-gnu"]')[0])
&& contains(fromJSON(inputs.sys_targets), fromJSON('["x86_64-v3-linux-gnu"]')[0])
name: Complement
uses: ./.github/workflows/bake.yml
with:
bake_targets: '["complement-tester", "complement-testee"]'
cargo_profiles: '["test"]'
feat_sets: ${{inputs.complement_feat_sets}}
cargo_profiles: '["bench"]'
feat_sets: '["logging"]'
rust_toolchains: '["nightly"]'
sys_names: ${{inputs.sys_names}}
sys_versions: ${{inputs.sys_versions}}
rust_targets: ${{inputs.rust_targets}}
sys_targets: '["x86_64-v1-linux-gnu"]'
sys_targets: '["x86_64-v3-linux-gnu"]'
machines: ${{inputs.machines}}
runner: ${{inputs.complement_runner}}
excludes: ${{inputs.excludes}}
@@ -319,10 +328,9 @@ jobs:
&& !contains(inputs.head_msg, '[ci no build]')
&& inputs.complement
&& inputs.machines
&& contains(fromJSON(inputs.cargo_profiles), fromJSON('["test"]')[0])
&& contains(fromJSON(inputs.feat_sets), fromJSON('["all"]')[0])
&& contains(fromJSON(inputs.cargo_profiles), fromJSON('["bench"]')[0])
&& contains(fromJSON(inputs.rust_toolchains), fromJSON('["nightly"]')[0])
&& contains(fromJSON(inputs.sys_targets), fromJSON('["x86_64-v1-linux-gnu"]')[0])
&& contains(fromJSON(inputs.sys_targets), fromJSON('["x86_64-v3-linux-gnu"]')[0])
name: Matrix Compliance
needs: [complement, smoke]
@@ -334,13 +342,13 @@ jobs:
strategy:
fail-fast: false
matrix:
cargo_profile: ${{fromJSON('["test"]')}}
feat_set: ${{fromJSON(inputs.complement_feat_sets)}}
cargo_profile: ${{fromJSON('["bench"]')}}
feat_set: ${{fromJSON('["logging"]')}}
rust_toolchain: ${{fromJSON('["nightly"]')}}
sys_name: ${{fromJSON(inputs.sys_names)}}
sys_version: ${{fromJSON(inputs.sys_versions)}}
rust_target: ${{fromJSON(inputs.rust_targets)}}
sys_target: ${{fromJSON('["x86_64-v1-linux-gnu"]')}}
sys_target: ${{fromJSON('["x86_64-v3-linux-gnu"]')}}
machine: ${{fromJSON(inputs.machines)}}
exclude: ${{fromJSON(inputs.excludes)}}
include: ${{fromJSON(inputs.includes)}}
@@ -387,7 +395,7 @@ jobs:
uses: actions/upload-artifact@v4
with:
name: complement_output-${{matrix.feat_set}}-${{matrix.sys_name}}-${{matrix.sys_target}}.jsonl
path: ./complement.jsonl
path: ./tests/complement/logs.jsonl
if-no-files-found: ignore
- name: Accept
+1 -1
View File
@@ -94,7 +94,7 @@ public/
rustc-ice-*
# complement test logs are huge
tests/test_results/complement/test_logs.jsonl
tests/complement/logs.jsonl
# cargo profiles from -Z self-profile
*.mm_profdata
Generated
+798 -823
View File
File diff suppressed because it is too large. Load diff
+28 -44
View File
@@ -28,8 +28,8 @@ keywords = [
license = "Apache-2.0"
readme = "README.md"
repository = "https://github.com/matrix-construct/tuwunel"
rust-version = "1.88.0"
version = "1.4.5"
rust-version = "1.89.0"
version = "1.4.8"
[workspace.metadata.crane]
name = "tuwunel"
@@ -104,7 +104,7 @@ features = [
version = "1.10"
[workspace.dependencies.bytesize]
version = "2.0"
version = "2.1"
[workspace.dependencies.cargo_toml]
version = "0.22"
@@ -192,7 +192,7 @@ version = "1.3"
version = "0.1"
[workspace.dependencies.hyper]
version = "1.6"
version = "1.7"
default-features = false
features = [
"server",
@@ -231,6 +231,10 @@ version = "0.1"
[workspace.dependencies.itertools]
version = "0.14"
[workspace.dependencies.jevmalloc]
git = "https://github.com/matrix-construct/jevmalloc"
rev = "93795449913f65ab533b7fa482333eef63fc3ae0"
[workspace.dependencies.jsonwebtoken]
version = "9.3"
default-features = false
@@ -238,7 +242,7 @@ features = ["use_pem"]
[workspace.dependencies.ldap3]
git = "https://github.com/matrix-construct/ldap3"
rev = "7d423314b9dbc66347284e38fc2b78c3d8f3d494"
rev = "fdfbba2bf916b53e5f73cdb1a495ebb649978079"
default-features = false
features = ["sync", "tls-rustls"]
@@ -262,7 +266,7 @@ version = "0.1"
version = "1.0"
[workspace.dependencies.minicbor]
version = "2.0"
version = "2.1"
features = ["std"]
[workspace.dependencies.minicbor-serde]
@@ -281,7 +285,7 @@ features = [
version = "0.2"
[workspace.dependencies.opentelemetry]
version = "0.30.0"
version = "0.31"
# Disabled until they move to opentelemetry 0.30
#[workspace.dependencies.opentelemetry-jaeger]
@@ -289,7 +293,7 @@ version = "0.30.0"
#features = ["rt-tokio"]
[workspace.dependencies.opentelemetry_sdk]
version = "0.30"
version = "0.31"
features = ["rt-tokio"]
[workspace.dependencies.proc-macro2]
@@ -302,7 +306,7 @@ version = "1.0"
version = "0.8"
[workspace.dependencies.regex]
version = "1.11"
version = "1.12"
[workspace.dependencies.reqwest]
version = "0.12"
@@ -320,7 +324,7 @@ default-features = false
[workspace.dependencies.ruma]
git = "https://github.com/matrix-construct/ruma"
rev = "b858d3478f2792a381463ef9b63f6c1469a5d81c"
rev = "214ab27fdc3d7004f6d46e0aa89fba573b59c66f"
features = [
"__compat",
"appservice-api-c",
@@ -339,6 +343,7 @@ features = [
"unstable-msc2870",
"unstable-msc3026",
"unstable-msc3061",
"unstable-msc3814",
"unstable-msc3245",
"unstable-msc3381", # polls
"unstable-msc3489", # beacon / live location
@@ -348,6 +353,7 @@ features = [
"unstable-msc4121",
"unstable-msc4125",
"unstable-msc4133",
"unstable-msc4143",
"unstable-msc4186",
"unstable-msc4203", # sending to-device events to appservices
"unstable-msc4310",
@@ -358,7 +364,7 @@ features = [
[workspace.dependencies.rustls]
version = "0.23"
default-features = false
features = ["aws_lc_rs"]
features = ["aws_lc_rs", "logging", "tls12", "prefer-post-quantum"]
[workspace.dependencies.rustyline-async]
version = "0.4.6"
@@ -366,14 +372,16 @@ default-features = false
[workspace.dependencies.rust-rocksdb]
git = "https://github.com/matrix-construct/rust-rocksdb"
rev = "225a42519276e502205bdc845cebdb22d70ee245"
rev = "c11395350bc1f2090a0152f2d15c8c5847821eba"
default-features = false
features = [
"bzip2",
"lto",
"lz4",
"multi-threaded-cf",
"mt_static",
"serde1",
"zstd",
"zstd-static-linking-only",
]
[workspace.dependencies.sanitize-filename]
@@ -384,7 +392,7 @@ version = "0.4"
default-features = false
[workspace.dependencies.sentry]
version = "0.42"
version = "0.45"
default-features = false
features = [
"backtrace",
@@ -400,10 +408,10 @@ features = [
]
[workspace.dependencies.sentry-tower]
version = "0.42"
version = "0.45"
[workspace.dependencies.sentry-tracing]
version = "0.42"
version = "0.45"
[workspace.dependencies.serde]
version = "1.0"
@@ -473,32 +481,8 @@ default-features = false
version = "2.0"
default-features = false
[workspace.dependencies.tikv-jemallocator]
git = "https://github.com/matrix-construct/jemallocator"
rev = "03bed96afbbc898bef4d4f7d335c0519e3d1afad"
default-features = false
features = [
"background_threads_runtime_support",
"unprefixed_malloc_on_supported_platforms",
]
[workspace.dependencies.tikv-jemalloc-ctl]
git = "https://github.com/matrix-construct/jemallocator"
rev = "03bed96afbbc898bef4d4f7d335c0519e3d1afad"
default-features = false
features = ["use_std"]
[workspace.dependencies.tikv-jemalloc-sys]
git = "https://github.com/matrix-construct/jemallocator"
rev = "03bed96afbbc898bef4d4f7d335c0519e3d1afad"
default-features = false
features = [
"background_threads_runtime_support",
"unprefixed_malloc_on_supported_platforms",
]
[workspace.dependencies.tokio]
version = "1.47"
version = "1.48"
default-features = false
features = [
"fs",
@@ -551,7 +535,7 @@ default-features = false
version = "0.2"
[workspace.dependencies.tracing-opentelemetry]
version = "0.31"
version = "0.32"
[workspace.dependencies.tracing-subscriber]
version = "0.3"
@@ -702,8 +686,8 @@ inherits = "release-native.build-override"
#]
[profile.bench]
debug = false
strip = false
debug = "limited"
strip = "none"
#rustflags = [
# "-Cremark=all",
# '-Ztime-passes',
+16 -15
View File
@@ -1,11 +1,11 @@
# Tuwunel<sup>💕</sup>
![GitHub License](https://img.shields.io/github/license/matrix-construct/tuwunel?style=flat&color=%238A2BE2)
![GitHub Created At](https://img.shields.io/github/created-at/matrix-construct/tuwunel?style=flat&color=%238A2BE2)
![GitHub Commit Activity](https://img.shields.io/github/commit-activity/m/matrix-construct/tuwunel?style=flat&link=https%3A%2F%2Fgithub.com%2Fmatrix-construct%2Ftuwunel%2Fpulse%2Fmonthly&color=%238A2BE2)
![Docker Pulls](https://img.shields.io/docker/pulls/jevolk/tuwunel?style=flat&color=8A2BE2)
![GitHub Repo Stars](https://img.shields.io/github/stars/matrix-construct/tuwunel?style=flat&link=https%3A%2F%2Fgithub.com%2Fmatrix-construct%2Ftuwunel&color=%238A2BE2)
[![CI/CD](https://github.com/matrix-construct/tuwunel/actions/workflows/main.yml/badge.svg?branch=main&style=flat)](https://github.com/matrix-construct/tuwunel/actions/workflows/main.yml)
![GitHub License](https://img.shields.io/github/license/matrix-construct/tuwunel?style=flat%2Dsquare&color=%238A2BE2)
![GitHub Created At](https://img.shields.io/github/created-at/matrix-construct/tuwunel?style=flat%2Dsquare&color=%238A2BE2)
![GitHub Commit Activity](https://img.shields.io/github/commit-activity/m/matrix-construct/tuwunel?style=flat%2Dsquare&link=https%3A%2F%2Fgithub.com%2Fmatrix-construct%2Ftuwunel%2Fpulse%2Fmonthly&color=%238A2BE2)
![Docker Pulls](https://img.shields.io/docker/pulls/jevolk/tuwunel?style=flat%2Dsquare&color=8A2BE2)
![GitHub Repo Stars](https://img.shields.io/github/stars/matrix-construct/tuwunel?style=flat%2Dsquare&link=https%3A%2F%2Fgithub.com%2Fmatrix-construct%2Ftuwunel&color=%238A2BE2)
[![CI/CD](https://github.com/matrix-construct/tuwunel/actions/workflows/main.yml/badge.svg?branch=main&style=flat%2Dsquare)](https://github.com/matrix-construct/tuwunel/actions/workflows/main.yml)
<!-- ANCHOR: catchphrase -->
@@ -15,19 +15,20 @@
<!-- ANCHOR: body -->
[![Documentation](https://img.shields.io/badge/documentation%2D_?color=%238A2BE2&style=for-the-badge&logo=mdBook&logoColor=white)](https://matrix-construct.github.io/tuwunel/)
[![Support Chat](https://img.shields.io/matrix/tuwunel%3Amatrix.org.svg?color=%238A2BE2&style=for-the-badge&label=Support%20Chat)](https://matrix.to/#/#tuwunel:grin.hu)
Tuwunel is a featureful [Matrix](https://matrix.org/) homeserver you can use instead of Synapse
with your favorite [client](https://matrix.org/ecosystem/clients/),
[bridge](https://matrix.org/ecosystem/bridges/) or
[bot](https://matrix.org/ecosystem/integrations/). It is written entirely in Rust to be a scalable,
lightweight, low-cost, community-driven alternative covering all but the most niche uses.
low-cost, enterprise-ready, community-driven alternative, fully implementing the
[Matrix Specification](https://spec.matrix.org/latest/) for all but the most niche uses.
This project is the official successor to conduwuit, which
was a featureful and high-performance fork of [Conduit](https://gitlab.com/famedly/conduit), all
community-lead homeservers implementing the compatible
[Matrix Specification](https://spec.matrix.org/latest/).
Tuwunel is operated by enterprise users with a vested interest in sponsoring its continued
development. It is now maintained by full-time staff.
This project is the official successor to [conduwuit](https://github.com/x86pup/conduwuit) after it
reached stability. Tuwunel is now used by many companies with a vested interest in its continued
development by full-time staff. It is primarily sponsored by the government of
Switzerland 🇨🇭 where it is currently deployed for citizens.
### Getting Started
@@ -38,7 +39,7 @@ development. It is now maintained by full-time staff.
- Static binaries available as [releases](https://github.com/matrix-construct/tuwunel/releases) or [build artifacts](https://github.com/matrix-construct/tuwunel/actions?query=branch%3Amain).
- Deb and RPM packages available as [releases](https://github.com/matrix-construct/tuwunel/releases) or [build artifacts](https://github.com/matrix-construct/tuwunel/actions?query=branch%3Amain).
- Arch package available as [tuwunel](https://aur.archlinux.org/packages/tuwunel) or [tuwunel-git](https://aur.archlinux.org/packages/tuwunel-git).
- Nix package still [needs some love](https://github.com/NixOS/nixpkgs/issues/415469).
- Nix package available as [`matrix-tuwunel`](https://search.nixos.org/packages?query=matrix-tuwunel) and NixOS module available as [`services.matrix-tuwunel`](https://search.nixos.org/options?query=services.matrix-tuwunel).
**1.** [Configure](https://matrix-construct.github.io/tuwunel/configuration.html) by
copying and editing the `tuwunel-example.toml`. The `server_name` and `database_path` must be
+5 -5
View File
@@ -1,9 +1,9 @@
# Tuwunel 1.4.5
# Tuwunel 1.4.8
October 24, 2025
December 21, 2025
All deployments serving ElementX, Element Pro, SchildiChat Next, or any client using sliding-sync should upgrade to this patch for an improved experience.
All federating deployments must upgrade to this patch for mitigations to severe vulnerabilities in Matrix protocol implementation logic. This is an off-schedule coordinated security release. Full release notes will be included with the next scheduled release.
### Bug Fixes
### Security Fixes
- Progress made for sliding-sync in 1.4.3 was effectively reverted by 1.4.4. This release puts sliding-sync back on track with further progress exceeding 1.4.3. This release is now shipping without any known issues. Please report all issues. **IMPORTANT:** Issues from prior releases (such as stuck or incorrect unread counts) benefit from clearing the app's cache to immediately resolve. Go to settings, tap the version number 7 times, then enter the menu to select `clear cache`.
- Requests to the [Federation Invite API](https://spec.matrix.org/v1.17/server-server-api/#put_matrixfederationv2inviteroomideventid) lacked sufficient validation on all input fields. An attacker can use this route to process other kinds of events: upon acceptance, they are signed by the victim's server as specified by the Matrix protocol. The attacker can therefore forge events on behalf of the victim's authority to gain control of a room. This vulnerability was present in all versions and derivatives of Conduit.
+3 -2
View File
@@ -5,7 +5,6 @@ authors = [
]
description = "Tuwunel, a high performance successor to Conduit and Conduwuit"
language = "en"
multilingual = false
src = "docs"
title = "Tuwunel One"
text-direction = "ltr"
@@ -15,7 +14,9 @@ build-dir = "public"
create-missing = true
extra-watch-dirs = [
"debian",
"docker",
"docs",
"rpm",
]
[rust]
@@ -24,7 +25,7 @@ edition = "2024"
[output.html]
git-repository-url = "https://github.com/matrix-construct/tuwunel"
edit-url-template = "https://github.com/matrix-construct/tuwunel/edit/main/{path}"
git-repository-icon = "fa-github-square"
git-repository-icon = "fab-github"
[output.html.search]
limit-results = 15
+6 -6
View File
@@ -1,11 +1,11 @@
stack-size-threshold = 393216
future-size-threshold = 24576
array-size-threshold = 4096
cognitive-complexity-threshold = 100 # TODO reduce me ALARA
excessive-nesting-threshold = 8
future-size-threshold = 8192
stack-size-threshold = 196608 # TODO reduce me ALARA
too-many-lines-threshold = 780 # TODO reduce me to <= 100
type-complexity-threshold = 250 # reduce me to ~200
large-error-threshold = 256 # TODO reduce me ALARA
too-many-lines-threshold = 780 # TODO reduce me to <= 100
excessive-nesting-threshold = 8
type-complexity-threshold = 250 # reduce me to ~200
cognitive-complexity-threshold = 100 # TODO reduce me ALARA
#disallowed-macros = [
# { path = "log::error", reason = "use tuwunel_core::error" },
-1
View File
@@ -5,7 +5,6 @@ After=network-online.target
Documentation=https://tuwunel.chat/
[Service]
DynamicUser=yes
User=tuwunel
Group=tuwunel
Type=notify
+11 -8
View File
@@ -12,6 +12,7 @@ ARG CARGO_TARGET_DIR
ARG cargo_target_profile
ARG cargo_target_artifact
ARG cargo_target_share
ARG cargo_share
ARG CARGO_TERM_VERBOSE=0
ARG RUST_BACKTRACE
ARG JEMALLOC_OVERRIDE
@@ -33,6 +34,8 @@ ARG color_args="--color always"
ARG recipe_args=""
ARG cargo_args=""
ARG git_checkout
ARG targ_dir="${CARGO_TARGET_DIR}/${cargo_target_profile}"
ARG targ_targ_dir="${CARGO_TARGET_DIR}/${rust_target}/${cargo_target_profile}"
WORKDIR /
COPY --link --from=input . .
@@ -43,6 +46,7 @@ WORKDIR /usr/lib/${sys_triple}
COPY --link --from=rocksdb . .
WORKDIR /usr/src/tuwunel
SHELL ["/bin/bash", "-c"]
ENV PATH="${CARGO_HOME}/bin:$PATH"
ENV CARGO_TERM_VERBOSE="${CARGO_TERM_VERBOSE}"
ENV RUST_BACKTRACE="${RUST_BACKTRACE}"
@@ -58,17 +62,15 @@ ENV CARGO_PROFILE_RELEASE_DEBUGINFO_DEBUG="${CARGO_PROFILE_RELEASE_DEBUGINFO_DEB
ENV CARGO_PROFILE_RELEASE_DEBUGINFO_LTO="${CARGO_PROFILE_RELEASE_DEBUGINFO_LTO}"
ENV CARGO_BUILD_RUSTFLAGS="${CARGO_BUILD_RUSTFLAGS}"
ENV CARGO_TARGET_DIR="${CARGO_TARGET_DIR}"
ENV targ_dir="${CARGO_TARGET_DIR}/${cargo_target_profile}"
ENV targ_targ_dir="${CARGO_TARGET_DIR}/${rust_target}/${cargo_target_profile}"
RUN \
--mount=type=cache,dst=${RUSTUP_HOME}/downloads,sharing=shared \
--mount=type=cache,dst=${CARGO_HOME}/registry,sharing=shared \
--mount=type=cache,dst=${CARGO_HOME}/git,sharing=shared \
--mount=type=cache,dst=${targ_dir}/deps,id=${cargo_target_share}/deps,sharing=locked \
--mount=type=cache,dst=${targ_dir}/build,id=${cargo_target_share}/build,sharing=locked \
--mount=type=cache,dst=${targ_dir}/examples,id=${cargo_target_share}/examples,sharing=locked \
--mount=type=cache,dst=${targ_dir}/incremental,id=${cargo_target_share}/incremental,sharing=locked \
--mount=type=cache,dst=${targ_dir}/.fingerprint,id=${cargo_target_share}/fingerprint,sharing=locked \
--mount=type=cache,dst=${targ_dir}/deps,id=${cargo_share}/deps,sharing=locked \
--mount=type=cache,dst=${targ_dir}/build,id=${cargo_share}/build,sharing=locked \
--mount=type=cache,dst=${targ_dir}/examples,id=${cargo_share}/examples,sharing=locked \
--mount=type=cache,dst=${targ_dir}/incremental,id=${cargo_share}/incremental,sharing=locked \
--mount=type=cache,dst=${targ_dir}/.fingerprint,id=${cargo_share}/fingerprint,sharing=locked \
--mount=type=cache,dst=${targ_targ_dir}/deps,id=${cargo_target_share}/deps,sharing=locked \
--mount=type=cache,dst=${targ_targ_dir}/build,id=${cargo_target_share}/build,sharing=locked \
--mount=type=cache,dst=${targ_targ_dir}/examples,id=${cargo_target_share}/examples,sharing=locked \
@@ -77,7 +79,8 @@ RUN \
<<EOF
set -eux
ulimit -n 65535
rustup run ${rust_toolchain} \
rustup run "${rust_toolchain}" \
cargo ${cargo_cmd} \
--verbose \
--locked \
+8 -7
View File
@@ -10,9 +10,12 @@ ARG CARGO_TARGET_DIR
ARG cargo_target_profile
ARG cargo_target_artifact
ARG cargo_target_share
ARG cargo_share
ARG cargo_profile
ARG cargo_features
ARG cargo_spec_features
ARG targ_dir="${CARGO_TARGET_DIR}/${cargo_target_profile}"
ARG targ_targ_dir="${CARGO_TARGET_DIR}/${rust_target}/${cargo_target_profile}"
ARG pkg_dir
ARG deb_args=""
@@ -20,17 +23,15 @@ WORKDIR /
COPY --link --from=input . .
WORKDIR /usr/src/tuwunel
ENV targ_dir="${CARGO_TARGET_DIR}/${cargo_target_profile}"
ENV targ_targ_dir="${CARGO_TARGET_DIR}/${rust_target}/${cargo_target_profile}"
RUN \
--mount=type=cache,dst=${RUSTUP_HOME}/downloads,sharing=shared \
--mount=type=cache,dst=${CARGO_HOME}/registry,sharing=shared \
--mount=type=cache,dst=${CARGO_HOME}/git,sharing=shared \
--mount=type=cache,dst=${targ_dir}/deps,id=${cargo_target_share}/deps,sharing=locked \
--mount=type=cache,dst=${targ_dir}/build,id=${cargo_target_share}/build,sharing=locked \
--mount=type=cache,dst=${targ_dir}/examples,id=${cargo_target_share}/examples,sharing=locked \
--mount=type=cache,dst=${targ_dir}/incremental,id=${cargo_target_share}/incremental,sharing=locked \
--mount=type=cache,dst=${targ_dir}/.fingerprint,id=${cargo_target_share}/fingerprint,sharing=locked \
--mount=type=cache,dst=${targ_dir}/deps,id=${cargo_share}/deps,sharing=locked \
--mount=type=cache,dst=${targ_dir}/build,id=${cargo_share}/build,sharing=locked \
--mount=type=cache,dst=${targ_dir}/examples,id=${cargo_share}/examples,sharing=locked \
--mount=type=cache,dst=${targ_dir}/incremental,id=${cargo_share}/incremental,sharing=locked \
--mount=type=cache,dst=${targ_dir}/.fingerprint,id=${cargo_share}/fingerprint,sharing=locked \
--mount=type=cache,dst=${targ_targ_dir}/deps,id=${cargo_target_share}/deps,sharing=locked \
--mount=type=cache,dst=${targ_targ_dir}/build,id=${cargo_target_share}/build,sharing=locked \
--mount=type=cache,dst=${targ_targ_dir}/examples,id=${cargo_target_share}/examples,sharing=locked \
+9 -5
View File
@@ -1,6 +1,7 @@
# syntax = docker/dockerfile:1.11-labs
FROM input AS build-rpm
ARG rust_target
ARG rust_toolchain
ARG RUSTUP_HOME
ARG CARGO_HOME
@@ -9,9 +10,12 @@ ARG CARGO_TARGET_DIR
ARG cargo_target_profile
ARG cargo_target_artifact
ARG cargo_target_share
ARG cargo_share
ARG cargo_profile
ARG cargo_features
ARG cargo_spec_features
ARG targ_dir="${CARGO_TARGET_DIR}/${cargo_target_profile}"
ARG targ_targ_dir="${CARGO_TARGET_DIR}/${rust_target}/${cargo_target_profile}"
ARG pkg_dir
ARG gen_rpm_args=""
@@ -23,11 +27,11 @@ RUN \
--mount=type=cache,dst=${RUSTUP_HOME}/downloads,sharing=shared \
--mount=type=cache,dst=${CARGO_HOME}/registry,sharing=shared \
--mount=type=cache,dst=${CARGO_HOME}/git,sharing=shared \
--mount=type=cache,dst=${targ_dir}/deps,id=${cargo_target_share}/deps,sharing=locked \
--mount=type=cache,dst=${targ_dir}/build,id=${cargo_target_share}/build,sharing=locked \
--mount=type=cache,dst=${targ_dir}/examples,id=${cargo_target_share}/examples,sharing=locked \
--mount=type=cache,dst=${targ_dir}/incremental,id=${cargo_target_share}/incremental,sharing=locked \
--mount=type=cache,dst=${targ_dir}/.fingerprint,id=${cargo_target_share}/fingerprint,sharing=locked \
--mount=type=cache,dst=${targ_dir}/deps,id=${cargo_share}/deps,sharing=locked \
--mount=type=cache,dst=${targ_dir}/build,id=${cargo_share}/build,sharing=locked \
--mount=type=cache,dst=${targ_dir}/examples,id=${cargo_share}/examples,sharing=locked \
--mount=type=cache,dst=${targ_dir}/incremental,id=${cargo_share}/incremental,sharing=locked \
--mount=type=cache,dst=${targ_dir}/.fingerprint,id=${cargo_share}/fingerprint,sharing=locked \
--mount=type=cache,dst=${targ_targ_dir}/deps,id=${cargo_target_share}/deps,sharing=locked \
--mount=type=cache,dst=${targ_targ_dir}/build,id=${cargo_target_share}/build,sharing=locked \
--mount=type=cache,dst=${targ_targ_dir}/examples,id=${cargo_target_share}/examples,sharing=locked \
+5 -11
View File
@@ -103,9 +103,6 @@ EOF
FROM input AS complement-testee
WORKDIR /
COPY --link --from=input . .
EXPOSE 8008 8448
RUN mkdir /database
COPY --from=complement-config * /complement/
@@ -115,9 +112,6 @@ ENTRYPOINT tuwunel -Oserver_name=\""$SERVER_NAME\""
FROM input AS complement-testee-valgrind
WORKDIR /
COPY --link --from=input . .
EXPOSE 8008 8448
RUN mkdir /database
COPY --from=complement-config * /complement/
@@ -134,12 +128,13 @@ ENTRYPOINT valgrind \
FROM input AS complement-base
ARG var_cache
ARG var_lib_apt
ARG complement_ref="4d3130f06d0dc3f794b5d48fbdba0b466792b52b"
ARG complement_tags="conduwuit_blacklist"
ARG complement_tests="./tests/..."
ARG complement_run=".*"
WORKDIR /usr/src
ADD https://github.com/matrix-construct/complement.git#403840348f6bcc9cc8ed1671dc2f638c2b1ce4ac complement
ADD https://github.com/matrix-construct/complement.git#${complement_ref} complement
WORKDIR /usr/src/complement
ENV COMPLEMENT_BASE_IMAGE="complement-testee"
@@ -155,8 +150,9 @@ EOF
FROM input AS complement-tester
ARG complement_verbose=0
ARG complement_debug=0
ARG complement_dirty=0
ARG complement_count=1
ARG complement_parallel=16
ARG complement_parallel=1
ARG complement_shuffle=1337
ARG complement_timeout="1h"
ARG complement_run=".*"
@@ -165,9 +161,6 @@ ARG complement_tags="conduwuit_blacklist"
ARG complement_tests="./tests/..."
ARG complement_base_image
WORKDIR /
COPY --link --from=input . .
WORKDIR /usr/src/complement
ENV COMPLEMENT_DEBUG=$complement_debug
ENV complement_parallel="$complement_parallel"
@@ -179,6 +172,7 @@ ENV complement_tests="$complement_tests"
ENV complement_skip="$complement_skip"
ENV complement_run="$complement_run"
ENV complement_tests="$complement_tests"
ENV COMPLEMENT_ENABLE_DIRTY_RUNS="$complement_dirty"
ENV COMPLEMENT_ALWAYS_PRINT_SERVER_LOGS="$complement_verbose"
ENV COMPLEMENT_HOSTNAME_RUNNING_COMPLEMENT="host.docker.internal"
ENV COMPLEMENT_HOST_MOUNTS="/var/run/docker.sock:/var/run/docker.sock"
+6 -4
View File
@@ -15,15 +15,17 @@ ENV src_path="${CARGO_TARGET_DIR}/${rust_target}/${cargo_target_profile}/tuwunel
ENV dst_path="${install_prefix}/bin/tuwunel"
COPY --from=bins $src_path $dst_path
RUN <<EOF
ret=$(ldd "${dst_path}")
ldd -v "${dst_path}"
ret=$?
if [ "$ret" = "0" ] && [ "$assert_linkage" = "static" ]; then
echo "($ret) expected a static binary"
exit 1
elif [ "$ret" != "0" ] && [ "$assert_linkage" = "dynamic" ]; then
echo "($ret) expected a dynamic binary"
exit 1
fi
set -eux
ldd -v ${dst_path} || true
du -h ${dst_path}
sha1sum ${dst_path}
du -h "${dst_path}"
sha1sum "${dst_path}"
EOF
+30 -37
View File
@@ -1,25 +1,28 @@
# syntax = docker/dockerfile:1.11-labs
FROM input AS matrix-rust-sdk-integration
FROM input AS rust-sdk-integration
ARG sys_name
ARG sys_version
ARG rust_toolchain
ARG feat_set
ARG rust_target
ARG rust_toolchain
ARG cargo_profile
ARG RUSTUP_HOME
ARG CARGO_HOME
ARG CARGO_TARGET
ARG MRSDK_TARGET_DIR="/usr/src/matrix-rust-sdk"
ARG mrsdk_profile="release"
ARG mrsdk_target_share="${MRSDK_TARGET_DIR}/${sys_name}/${sys_version}/${rust_target}/${rust_toolchain}/${mrsdk_profile}/_shared_cache"
ARG MRSDK_TARGET_DIR="/usr/src/matrix-rust-sdk/target"
ARG mrsdk_target_share
#ARG mrsdk_ref="integration"
ARG mrsdk_ref="tuwunel-changes"
ARG mrsdk_test_args=""
ARG mrsdk_test_opts=""
ARG mrsdk_skip_list=""
WORKDIR /
COPY --link --from=input . .
ARG mrsdk_parallel=2
ARG mrsdk_startup_delay="10s"
ARG mrsdk_testee="/usr/bin/tuwunel"
WORKDIR /usr/src
ADD --link https://github.com/matrix-construct/matrix-rust-sdk.git#integration matrix-rust-sdk
ADD --link https://github.com/matrix-construct/matrix-rust-sdk.git#${mrsdk_ref} matrix-rust-sdk
WORKDIR /etc
COPY <<EOF tuwunel.toml
@@ -59,49 +62,39 @@ WORKDIR /usr/bin
COPY --link --from=install /usr/bin/tuwunel .
WORKDIR /usr/src/matrix-rust-sdk
ENV targ_dir="${MRSDK_TARGET_DIR}/${mrsdk_profile}"
ENV targ_targ_dir="${MRSDK_TARGET_DIR}/${rust_target}/${mrsdk_profile}"
SHELL ["/bin/bash", "-c"]
ENV RUST_BACKTRACE="full"
ENV TUWUNEL_CONFIG="/etc/tuwunel.toml"
ENV TUWUNEL_DATABASE_PATH="/var/db/tuwunel"
ENV TUWUNEL_SERVER_NAME="localhost:6167"
ENV TUWUNEL_PORT="[6167]"
ENV HOMESERVER_URL="http://localhost:6167"
ENV TUWUNEL_SERVER_NAME="localhost"
ENV TUWUNEL_PORT="[8448]"
ENV HOMESERVER_URL="http://localhost:8448"
RUN \
--mount=type=cache,dst=${RUSTUP_HOME}/downloads,sharing=shared \
--mount=type=cache,dst=${CARGO_HOME}/registry,sharing=shared \
--mount=type=cache,dst=${CARGO_HOME}/git,sharing=shared \
--mount=type=cache,dst=${targ_dir}/deps,id=${mrsdk_target_share}/deps,sharing=locked \
--mount=type=cache,dst=${targ_dir}/build,id=${mrsdk_target_share}/build,sharing=locked \
--mount=type=cache,dst=${targ_dir}/examples,id=${mrsdk_target_share}/examples,sharing=locked \
--mount=type=cache,dst=${targ_dir}/incremental,id=${mrsdk_target_share}/incremental,sharing=locked \
--mount=type=cache,dst=${targ_dir}/.fingerprint,id=${mrsdk_target_share}/fingerprint,sharing=locked \
--mount=type=cache,dst=${targ_targ_dir}/deps,id=${mrsdk_target_share}/deps,sharing=locked \
--mount=type=cache,dst=${targ_targ_dir}/build,id=${mrsdk_target_share}/build,sharing=locked \
--mount=type=cache,dst=${targ_targ_dir}/examples,id=${mrsdk_target_share}/examples,sharing=locked \
--mount=type=cache,dst=${targ_targ_dir}/incremental,id=${mrsdk_target_share}/incremental,sharing=locked \
--mount=type=cache,dst=${targ_targ_dir}/.fingerprint,id=${mrsdk_target_share}/fingerprint,sharing=locked \
--mount=type=cache,dst=${MRSDK_TARGET_DIR},id=${mrsdk_target_share},sharing=locked \
<<EOF
set -eux
nohup /usr/bin/tuwunel 1> /var/log/tuwunel.log &
PID=$!; trap "sleep 10s; set +ex; kill -QUIT ${PID}; wait ${PID}" EXIT
sleep 3s
nohup ${mrsdk_testee[@]} 1> /var/log/tuwunel.log &
PID=$!; trap "sleep 10s; set +e; kill -QUIT ${PID}; wait ${PID}" EXIT
sleep "${mrsdk_startup_delay}"
rustup run ${rust_toolchain} \
cargo test \
--locked \
--release \
--color always \
--features default \
--target "${rust_target}" \
--target-dir "${MRSDK_TARGET_DIR}" \
--package matrix-sdk-integration-testing \
${mrsdk_test_args} \
"--color=always" \
"--features=default" \
"--target=${rust_target}" \
"--target-dir=${MRSDK_TARGET_DIR}" \
"--package=matrix-sdk-integration-testing" \
${mrsdk_test_args[@]} \
-- \
--test-threads 1 \
--color always \
${mrsdk_skip_list} \
${mrsdk_test_opts} \
"--color=always" \
"--test-threads=${mrsdk_parallel}" \
${mrsdk_skip_list[@]} \
${mrsdk_test_opts[@]} \
;
EOF
+26 -24
View File
@@ -1,12 +1,10 @@
# syntax = docker/dockerfile:1.11-labs
FROM input AS nix-base
ARG sys_name
ARG sys_version
ARG sys_target
WORKDIR /
COPY --link --from=input . .
RUN \
--mount=type=cache,dst=/nix,sharing=shared \
--mount=type=cache,dst=/root/.cache/nix,sharing=shared \
@@ -20,9 +18,6 @@ EOF
FROM nix-base AS build-nix
ARG sys_name
ARG sys_version
ARG sys_target
WORKDIR /usr/src/tuwunel
COPY --link --from=source /usr/src/tuwunel .
@@ -44,15 +39,13 @@ RUN \
EOF
FROM build-nix AS smoke-nix
ARG sys_name
ARG sys_version
ARG sys_target
FROM input AS smoke-nix
WORKDIR /
COPY --link --from=build-nix . .
COPY --link --from=nix-base . .
WORKDIR /opt/tuwunel
WORKDIR /usr/src/tuwunel
COPY --link --from=source /usr/src/tuwunel .
ENV TUWUNEL_DATABASE_PATH="/tmp/tuwunel/smoketest.db"
ENV TUWUNEL_LOG="info"
RUN \
@@ -61,29 +54,38 @@ RUN \
--mount=type=cache,dst=/root/.local/state/nix,sharing=shared \
<<EOF
set -eux
alias nix="nix --extra-experimental-features nix-command --extra-experimental-features flakes"
bin/tuwunel \
-Otest='["smoke", "fresh"]' \
-Oserver_name=\"localhost\" \
nix run \
--verbose \
--cores 0 \
--max-jobs $(nproc) \
--log-format raw \
.#all-features \
-- \
-Otest='["smoke", "fresh"]' \
-Oserver_name=\"localhost\" \
EOF
FROM build-nix AS nix-pkg
ARG sys_name
ARG sys_version
ARG sys_target
FROM input AS nix-pkg
WORKDIR /
COPY --link --from=build-nix . .
COPY --link --from=nix-base . .
WORKDIR /usr/src/tuwunel
COPY --link --from=source /usr/src/tuwunel .
RUN \
--mount=type=cache,dst=/nix,sharing=shared \
--mount=type=cache,dst=/root/.cache/nix,sharing=shared \
--mount=type=cache,dst=/root/.local/state/nix,sharing=shared \
<<EOF
set -eux
#TODO: extract derivation?
mkdir -p /opt/tuwunel
touch /opt/tuwunel/tuwunel.drv
set -eux
alias nix="nix --extra-experimental-features nix-command --extra-experimental-features flakes"
ID=$(nix-store --realise $(nix path-info --derivation))
mkdir -p tuwunel
nix-store --export $ID > tuwunel/tuwunel.drv
tar -cvf /opt/tuwunel.nix.tar tuwunel
EOF
+4 -1
View File
@@ -28,10 +28,12 @@ FROM input AS rocksdb-build
ARG rocksdb_shared=0
ARG rocksdb_portable="1"
ARG rocksdb_opt_level="3"
ARG rocksdb_lto="-flto -ffat-lto-objects"
ARG rocksdb_build_type="Release"
ARG rocksdb_cxx_flags="-ftls-model=initial-exec"
ARG rocksdb_make_verbose="ON"
ARG rocksdb_make_rule_messages="OFF"
ARG rocksdb_numa=0
ARG rocksdb_jemalloc=1
ARG rocksdb_iouring=1
ARG rocksdb_zstd=1
@@ -59,7 +61,7 @@ RUN <<EOF
"-DBUILD_SHARED_LIBS=${rocksdb_shared}" \
"-DROCKSDB_BUILD_SHARED=${rocksdb_shared}" \
"-DCMAKE_CXX_FLAGS:STRING=${rocksdb_cxx_flags}" \
"-DCMAKE_CXX_FLAGS_RELEASE:STRING=-g0 -O${rocksdb_opt_level} -DNDEBUG" \
"-DCMAKE_CXX_FLAGS_RELEASE:STRING=-g0 -O${rocksdb_opt_level} -DNDEBUG ${rocksdb_lto}" \
"-DPORTABLE=${rocksdb_portable}" \
"-DFAIL_ON_WARNINGS=0" \
"-DUSE_RTTI=0" \
@@ -73,6 +75,7 @@ RUN <<EOF
"-DWITH_TOOLS=0" \
"-DWITH_TESTS=0" \
"-DWITH_GFLAGS=0" \
"-DWITH_NUMA=${rocksdb_numa}" \
"-DWITH_LIBURING=${rocksdb_iouring}" \
"-DWITH_JEMALLOC=${rocksdb_jemalloc}" \
"-DWITH_ZSTD=${rocksdb_zstd}" \
-6
View File
@@ -6,9 +6,6 @@ ARG rust_target
ARG rustup_version="1.28.2"
ARG rustup_profile="minimal"
WORKDIR /
COPY --link --from=input . .
WORKDIR ${RUST_HOME}
RUN <<EOF
set -eux
@@ -32,9 +29,6 @@ ARG CARGO_TERM_VERBOSE
ARG rustup_components
ARG cargo_installs
WORKDIR /
COPY --link --from=input . .
WORKDIR ${RUST_HOME}
ENV CARGO_TARGET="${rust_target}"
ENV RUSTUP_HOME="${RUSTUP_HOME}"
-7
View File
@@ -3,9 +3,6 @@
FROM input AS source
ARG git_checkout
WORKDIR /
COPY --link --from=input . .
ADD --link --keep-git-dir . /usr/src/tuwunel
WORKDIR /usr/src/tuwunel
RUN <<EOF
@@ -30,7 +27,6 @@ ARG JEMALLOC_OVERRIDE
ARG ROCKSDB_LIB_DIR
WORKDIR /
COPY --link --from=input . .
COPY --link --from=rust ${RUST_HOME} ${RUST_HOME}
COPY --link --from=source /usr/src/tuwunel /usr/src/tuwunel
@@ -67,9 +63,6 @@ ARG RUSTUP_HOME
ARG CARGO_HOME
ARG CARGO_TARGET
WORKDIR /
COPY --link --from=input . .
WORKDIR /usr/src/tuwunel
RUN \
--mount=type=cache,dst=${RUSTUP_HOME}/downloads,sharing=locked \
-3
View File
@@ -12,9 +12,6 @@ ARG var_lib_apt
ARG packages
ARG DEBIAN_FRONTEND
WORKDIR /
COPY --link --from=input . .
ENV DEBIAN_FRONTEND="${DEBIAN_FRONTEND}"
RUN \
--mount=type=cache,dst=/var/cache,id=${var_cache},sharing=locked \
+91 -44
View File
@@ -1,4 +1,4 @@
variable "CI" {}
variable "GITHUB_ACTOR" {}
variable "GITHUB_REPOSITORY" {}
variable "GITHUB_REF" {}
@@ -84,6 +84,9 @@ variable "rocksdb_build_type" {
variable "rocksdb_make_verbose" {
default = "ON"
}
variable "rocksdb_numa" {
default = "0"
}
# Complement options
variable "complement_count" {
@@ -247,7 +250,7 @@ group "tests" {
group "matrix-compliance" {
targets = [
"complement",
"matrix-rust-sdk-integration",
"rust-sdk-integ",
]
}
@@ -440,61 +443,96 @@ target "complement-config" {
# Integration tests
#
group "integrations" {
group "integration" {
targets = [
"matrix-rust-sdk-integration",
"integration",
"integ",
"rust-sdk-integ",
]
}
target "matrix-rust-sdk-integration" {
name = elem("matrix-rust-sdk-integration", [cargo_profile, rust_toolchain, rust_target, feat_set, sys_name, sys_version, sys_target])
variable "valgrind_flags" {
default = "--error-exitcode=1 --exit-on-first-error=yes --undef-value-errors=no --leak-check=no"
}
target "rust-sdk-valgrind" {
name = elem("rust-sdk-valgrind", [cargo_profile, rust_toolchain, rust_target, feat_set, sys_name, sys_version, sys_target])
tags = [
elem_tag("matrix-rust-sdk-integration", [cargo_profile, rust_toolchain, rust_target, feat_set, sys_name, sys_version, sys_target], "latest"),
elem_tag("rust-sdk-valgrind", [cargo_profile, rust_toolchain, rust_target, feat_set, sys_name, sys_version, sys_target], "latest"),
]
output = ["type=docker,compression=zstd,mode=max,compression-level=${zstd_image_compress_level}"]
cache_to = ["type=local,compression=zstd,mode=max,compression-level=${cache_compress_level}"]
target = "matrix-rust-sdk-integration"
dockerfile = "${docker_dir}/Dockerfile.matrix-rust-sdk"
matrix = cargo_rust_feat_sys
inherits = [
elem("rust", [rust_toolchain, rust_target, sys_name, sys_version, sys_target])
elem("rust-sdk-integ", [cargo_profile, rust_toolchain, rust_target, feat_set, sys_name, sys_version, sys_target])
]
contexts = {
input = elem("target:rust", [rust_toolchain, rust_target, sys_name, sys_version, sys_target])
install = elem("target:install", [cargo_profile, rust_toolchain, rust_target, feat_set, sys_name, sys_version, sys_target])
}
args = {
mrsdk_test_args=""
mrsdk_test_opts=""
VALGRINDFLAGS = "${valgrind_flags}"
mrsdk_testee = "valgrind ${valgrind_flags} /usr/bin/tuwunel"
mrsdk_test_args = ""
mrsdk_startup_delay = "30s"
mrsdk_skip_list =<<EOF
--skip test_delayed_invite_response_and_sent_message_decryption
--skip test_history_share_on_invite_pin_violation
EOF
}
}
target "integration-valgrind" {
name = elem("integration-valgrind", [cargo_profile, rust_toolchain, rust_target, feat_set, sys_name, sys_version, sys_target])
target "rust-sdk-integ" {
name = elem("rust-sdk-integ", [cargo_profile, rust_toolchain, rust_target, feat_set, sys_name, sys_version, sys_target])
tags = [
elem_tag("integration-valgrind", [cargo_profile, rust_toolchain, rust_target, feat_set, sys_name, sys_version, sys_target], "latest"),
elem_tag("rust-sdk-integ", [cargo_profile, rust_toolchain, rust_target, feat_set, sys_name, sys_version, sys_target], "latest"),
]
output = ["type=docker,compression=zstd,mode=max,compression-level=${zstd_image_compress_level}"]
cache_to = ["type=local,compression=zstd,mode=max,compression-level=${cache_compress_level}"]
target = "rust-sdk-integration"
dockerfile = "${docker_dir}/Dockerfile.matrix-rust-sdk"
matrix = cargo_rust_feat_sys
inherits = [
elem("rust", [rust_toolchain, rust_target, sys_name, sys_version, sys_target]),
elem("integ", [cargo_profile, rust_toolchain, rust_target, feat_set, sys_name, sys_version, sys_target]),
]
contexts = {
input = elem("target:rust", [rust_toolchain, rust_target, sys_name, sys_version, sys_target])
install = elem("target:install", [cargo_profile, rust_toolchain, rust_target, feat_set, sys_name, sys_version, sys_target])
}
args = {
mrsdk_target_share = "/usr/src/matrix-rust-sdk/target/${sys_name}/${sys_version}/${rust_target}/${rust_toolchain}/_shared_cache"
mrsdk_testee = "/usr/bin/tuwunel"
mrsdk_test_args = "--no-fail-fast"
mrsdk_skip_list =<<EOF
--skip test_delayed_invite_response_and_sent_message_decryption
EOF
}
}
target "integ-valgrind" {
name = elem("integ-valgrind", [cargo_profile, rust_toolchain, rust_target, feat_set, sys_name, sys_version, sys_target])
tags = [
elem_tag("integ-valgrind", [cargo_profile, rust_toolchain, rust_target, feat_set, sys_name, sys_version, sys_target], "latest"),
]
matrix = cargo_rust_feat_sys
inherits = [
elem("integration", [cargo_profile, rust_toolchain, rust_target, feat_set, sys_name, sys_version, sys_target]),
elem("integ", [cargo_profile, rust_toolchain, rust_target, feat_set, sys_name, sys_version, sys_target]),
]
contexts = {
input = elem("target:integration", [cargo_profile, rust_toolchain, rust_target, feat_set, sys_name, sys_version, sys_target])
input = elem("target:build-tests", [cargo_profile, rust_toolchain, rust_target, feat_set, sys_name, sys_version, sys_target])
}
args = {
VALGRINDFLAGS = "--error-exitcode=1 --exit-on-first-error=yes --undef-value-errors=no --leak-check=no"
VALGRINDFLAGS = "${valgrind_flags}"
cargo_cmd = "valgrind test"
cargo_args = "--test=*"
}
}
target "integration" {
name = elem("integration", [cargo_profile, rust_toolchain, rust_target, feat_set, sys_name, sys_version, sys_target])
target "integ" {
name = elem("integ", [cargo_profile, rust_toolchain, rust_target, feat_set, sys_name, sys_version, sys_target])
tags = [
elem_tag("integration", [cargo_profile, rust_toolchain, rust_target, feat_set, sys_name, sys_version, sys_target], "latest"),
elem_tag("integ", [cargo_profile, rust_toolchain, rust_target, feat_set, sys_name, sys_version, sys_target], "latest"),
]
target = "cargo"
matrix = cargo_rust_feat_sys
inherits = [
elem("build-tests", [cargo_profile, rust_toolchain, rust_target, feat_set, sys_name, sys_version, sys_target]),
@@ -504,7 +542,6 @@ target "integration" {
}
args = {
TUWUNEL_DATABASE_PATH = "/tmp/integration.test.db"
cargo_cmd = (cargo_profile == "bench"? "bench": "test")
cargo_args = (cargo_profile == "bench"?
"--no-fail-fast --bench=*": "--no-fail-fast --test=*"
@@ -527,16 +564,16 @@ group "smoke" {
}
target "smoke-nix" {
name = elem("smoke-nix", [sys_name, sys_version, sys_target])
name = elem("smoke-nix", [cargo_profile, rust_toolchain, rust_target, feat_set, sys_name, sys_version, sys_target])
tags = [
elem_tag("smoke-nix", [sys_name, sys_version, sys_target], "latest"),
elem_tag("smoke-nix", [cargo_profile, rust_toolchain, rust_target, feat_set, sys_name, sys_version, sys_target], "latest"),
]
output = ["type=cacheonly,compression=zstd,mode=min,compression-level=${cache_compress_level}"]
dockerfile = "${docker_dir}/Dockerfile.nix"
target = "smoke-nix"
matrix = sys
matrix = cargo_rust_feat_sys
inherits = [
elem("build-nix", [sys_name, sys_version, sys_target]),
elem("build-nix", [cargo_profile, rust_toolchain, rust_target, feat_set, sys_name, sys_version, sys_target]),
]
}
@@ -630,7 +667,7 @@ target "unit-valgrind" {
input = elem("target:unit", [cargo_profile, rust_toolchain, rust_target, feat_set, sys_name, sys_version, sys_target])
}
args = {
VALGRINDFLAGS = "--error-exitcode=1 --exit-on-first-error=yes --undef-value-errors=no --leak-check=no"
VALGRINDFLAGS = "${valgrind_flags}"
cargo_cmd = "valgrind test"
cargo_args = "--lib --bins"
}
@@ -831,6 +868,12 @@ target "install" {
}
args = {
install_prefix = install_prefix
assert_linkage = (
substr(cargo_profile, 0, 5) == "bench"? "static":
substr(cargo_profile, 0, 7) == "release"? "static":
substr(rust_toolchain, 0, 6) == "stable"? "static":
""
)
}
}
@@ -961,20 +1004,20 @@ target "nix" {
target = "nix-pkg"
matrix = cargo_rust_feat_sys
inherits = [
elem("build-nix", [sys_name, sys_version, sys_target]),
elem("build-nix", [cargo_profile, rust_toolchain, rust_target, feat_set, sys_name, sys_version, sys_target]),
]
}
target "build-nix" {
name = elem("build-nix", [sys_name, sys_version, sys_target])
name = elem("build-nix", [cargo_profile, rust_toolchain, rust_target, feat_set, sys_name, sys_version, sys_target])
tags = [
elem_tag("build-nix", [sys_name, sys_version, sys_target], "latest"),
elem_tag("build-nix", [cargo_profile, rust_toolchain, rust_target, feat_set, sys_name, sys_version, sys_target], "latest"),
]
output = ["type=cacheonly,compression=zstd,mode=min,compression-level=${cache_compress_level}"]
cache_to = ["type=local,compression=zstd,mode=max,compression-level=${cache_compress_level}"]
dockerfile = "${docker_dir}/Dockerfile.nix"
target = "build-nix"
matrix = sys
matrix = cargo_rust_feat_sys
inherits = [
elem("builder", [sys_name, sys_version, sys_target]),
elem("source", [sys_name, sys_version, sys_target]),
@@ -1006,7 +1049,6 @@ target "book" {
}
dockerfile-inline =<<EOF
FROM input AS book
COPY --link --from=input . .
RUN ["mdbook", "build", "-d", "/book", "/usr/src/tuwunel"]
EOF
}
@@ -1352,6 +1394,8 @@ target "deps-base" {
# cache key for unique artifact area
cargo_target_artifact = "${cargo_tgt_dir_base}/${sys_name}/${sys_version}/${rust_target}/${rust_toolchain}/${cargo_profile}/${feat_set}/${git_ref_sha}"
# cache key for hashed subdirs
cargo_share = "${cargo_tgt_dir_base}/${sys_name}/${sys_version}/${rust_toolchain}/${cargo_profile}/_shared_cache"
# cache key for hashed subdirs
cargo_target_share = "${cargo_tgt_dir_base}/${sys_name}/${sys_version}/${rust_target}/${rust_toolchain}/${cargo_profile}/_shared_cache"
# cased name of profile subdir within target complex
cargo_target_profile = (
@@ -1376,7 +1420,7 @@ target "deps-base" {
join(" ", static_rustflags),
join(" ", static_nightly_rustflags),
join(" ", native_rustflags),
"-C link-arg=-L/usr/lib/gcc/${sys_target_triple(sys_target)}/14", #FIXME
"-C link-arg=-L/usr/lib/gcc/${sys_target_triple(sys_target)}/15", #FIXME
contains(split(",", cargo_feat_sets[feat_set]), "bzip2_compression")?
"-C link-arg=-l:libbz2.a": "",
contains(split(",", cargo_feat_sets[feat_set]), "lz4_compression")?
@@ -1390,7 +1434,7 @@ target "deps-base" {
"-C link-arg=-l:libgcc.a": "",
]):
(cargo_profile == "release" || cargo_profile == "bench") && rust_toolchain == "nightly"?
(cargo_profile == "release" || cargo_profile == "bench") && substr(rust_toolchain, 0, 7) == "nightly"?
join(" ", [
join(" ", rustflags),
join(" ", nightly_rustflags),
@@ -1398,7 +1442,7 @@ target "deps-base" {
join(" ", static_nightly_rustflags),
sys_target_triple(sys_target) == "x86_64-linux-gnu"?
"-C target-cpu=${sys_target_isa(sys_target)}": "",
"-C link-arg=-L/usr/lib/gcc/${sys_target_triple(sys_target)}/14", #FIXME
"-C link-arg=-L/usr/lib/gcc/${sys_target_triple(sys_target)}/15", #FIXME
contains(split(",", cargo_feat_sets[feat_set]), "bzip2_compression")?
"-C link-arg=-l:libbz2.a": "",
contains(split(",", cargo_feat_sets[feat_set]), "lz4_compression")?
@@ -1418,7 +1462,7 @@ target "deps-base" {
join(" ", static_rustflags),
sys_target_triple(sys_target) == "x86_64-linux-gnu"?
"-C target-cpu=${sys_target_isa(sys_target)}": "",
"-C link-arg=-L/usr/lib/gcc/${sys_target_triple(sys_target)}/14", #FIXME
"-C link-arg=-L/usr/lib/gcc/${sys_target_triple(sys_target)}/15", #FIXME
contains(split(",", cargo_feat_sets[feat_set]), "bzip2_compression")?
"-C link-arg=-l:libbz2.a": "",
contains(split(",", cargo_feat_sets[feat_set]), "lz4_compression")?
@@ -1432,13 +1476,13 @@ target "deps-base" {
"-C link-arg=-l:libgcc.a": "",
]):
rust_toolchain == "stable"?
substr(rust_toolchain, 0, 6) == "stable"?
join(" ", [
join(" ", rustflags),
join(" ", static_rustflags),
sys_target_triple(sys_target) == "x86_64-linux-gnu"?
"-C target-cpu=${sys_target_isa(sys_target)}": "",
"-C link-arg=-L/usr/lib/gcc/${sys_target_triple(sys_target)}/14", #FIXME
"-C link-arg=-L/usr/lib/gcc/${sys_target_triple(sys_target)}/15", #FIXME
contains(split(",", cargo_feat_sets[feat_set]), "bzip2_compression")?
"-C link-arg=-l:libbz2.a": "",
contains(split(",", cargo_feat_sets[feat_set]), "lz4_compression")?
@@ -1452,7 +1496,7 @@ target "deps-base" {
"-C link-arg=-l:libgcc.a": "",
]):
rust_toolchain == "nightly"?
substr(rust_toolchain, 0, 7) == "nightly"?
join(" ", [
join(" ", rustflags),
join(" ", nightly_rustflags),
@@ -1517,6 +1561,7 @@ target "rocksdb-build" {
rocksdb_zstd = contains(split(",", cargo_feat_sets[feat_set]), "zstd_compression")? 1: 0
rocksdb_jemalloc = contains(split(",", cargo_feat_sets[feat_set]), "jemalloc")? 1: 0
rocksdb_iouring = contains(split(",", cargo_feat_sets[feat_set]), "io_uring")? 1: 0
rocksdb_numa = rocksdb_numa
rocksdb_shared = 0
rocksdb_opt_level = rocksdb_opt_level
rocksdb_build_type = rocksdb_build_type
@@ -1681,7 +1726,7 @@ cargo_installs = [
"cargo-deb",
"cargo-generate-rpm",
"cargo-valgrind",
#"lychee",
"lychee",
"mdbook",
"typos-cli",
]
@@ -1770,6 +1815,8 @@ kitchen_packages = [
"gzip",
"jq",
"libc6-dev",
"libclang-dev",
"libnuma-dev",
"libssl-dev",
"libsqlite3-dev",
"make",
+4 -4
View File
@@ -16,6 +16,7 @@ default_sys_target="x86_64-v1-linux-gnu"
default_sys_version="testing-slim"
default_complement_verbose=0
default_complement_dirty=0
default_complement_count=1
default_complement_parallel=1
default_complement_shuffle=0
@@ -31,9 +32,7 @@ skip="${skip}|TestRoomCreate/Parallel/POST_/createRoom_makes_a_room_with_a_topic
skip="${skip}|TestLogin/parallel/POST_/"
skip="${skip}|TestUnbanViaInvite"
skip="${skip}|TestRoomState/Parallel/GET_/publicRooms_lists_newly-created_room"
# flakes due to timeout in debug-mode
skip="${skip}|TestMSC4297StateResolutionV2_1_starts_from_empty_set"
skip="${skip}|TestMSC4297StateResolutionV2_1_includes_conflicted_subgraph"
skip="${skip}|TestThreadReceiptsInSyncMSC4102"
set -a
cargo_profile="${cargo_profile:-$default_cargo_profile}"
@@ -53,6 +52,7 @@ set +a
envs=""
envs="$envs -e complement_verbose=${complement_verbose:-$default_complement_verbose}"
envs="$envs -e complement_count=${complement_count:-$default_complement_count}"
envs="$envs -e complement_dirty=${complement_dirty:-$default_complement_dirty}"
envs="$envs -e complement_parallel=${complement_parallel:-$default_complement_parallel}"
envs="$envs -e complement_shuffle=${complement_shuffle:-$default_complement_shuffle}"
envs="$envs -e complement_timeout=${complement_timeout:-$default_complement_timeout}"
@@ -82,7 +82,7 @@ if test "$CI" = "true"; then
fi
output_src="$cid:/usr/src/complement/full_output.jsonl"
output_dst="complement.jsonl"
output_dst="tests/complement/logs.jsonl"
extract_output() {
docker cp "$output_src" "$output_dst"
}
+2
View File
@@ -5,6 +5,8 @@
- [Examples](configuration/examples.md)
- [Deploying](deploying.md)
- [Generic](deploying/generic.md)
- [Reverse Proxy - Caddy](deploying/reverse-proxy-caddy.md)
- [Reverse Proxy - Nginx](deploying/reverse-proxy-nginx.md)
- [NixOS](deploying/nixos.md)
- [Docker](deploying/docker.md)
- [Kubernetes](deploying/kubernetes.md)
@@ -20,7 +20,6 @@ services:
TUWUNEL_REGISTRATION_TOKEN: 'YOUR_TOKEN' # A registration token is required when registration is allowed.
#TUWUNEL_YES_I_AM_VERY_VERY_SURE_I_WANT_AN_OPEN_REGISTRATION_SERVER_PRONE_TO_ABUSE: 'true'
TUWUNEL_ALLOW_FEDERATION: 'true'
TUWUNEL_ALLOW_CHECK_FOR_UPDATES: 'true'
TUWUNEL_TRUSTED_SERVERS: '["matrix.org"]'
#TUWUNEL_LOG: warn,state_res=warn
TUWUNEL_ADDRESS: 0.0.0.0
@@ -36,7 +36,6 @@ services:
TUWUNEL_REGISTRATION_TOKEN: 'YOUR_TOKEN' # A registration token is required when registration is allowed.
#TUWUNEL_YES_I_AM_VERY_VERY_SURE_I_WANT_AN_OPEN_REGISTRATION_SERVER_PRONE_TO_ABUSE: 'true'
TUWUNEL_ALLOW_FEDERATION: 'true'
TUWUNEL_ALLOW_CHECK_FOR_UPDATES: 'true'
TUWUNEL_TRUSTED_SERVERS: '["matrix.org"]'
#TUWUNEL_LOG: warn,state_res=warn
TUWUNEL_ADDRESS: 0.0.0.0
@@ -26,7 +26,6 @@ services:
# TUWUNEL_LOG: info # default is: "warn,state_res=warn"
# TUWUNEL_ALLOW_ENCRYPTION: 'true'
# TUWUNEL_ALLOW_FEDERATION: 'true'
# TUWUNEL_ALLOW_CHECK_FOR_UPDATES: 'true'
# TUWUNEL_ALLOW_INCOMING_PRESENCE: true
# TUWUNEL_ALLOW_OUTGOING_PRESENCE: true
# TUWUNEL_ALLOW_LOCAL_PRESENCE: true
-1
View File
@@ -20,7 +20,6 @@ services:
TUWUNEL_REGISTRATION_TOKEN: 'YOUR_TOKEN' # A registration token is required when registration is allowed.
#TUWUNEL_YES_I_AM_VERY_VERY_SURE_I_WANT_AN_OPEN_REGISTRATION_SERVER_PRONE_TO_ABUSE: 'true'
TUWUNEL_ALLOW_FEDERATION: 'true'
TUWUNEL_ALLOW_CHECK_FOR_UPDATES: 'true'
TUWUNEL_TRUSTED_SERVERS: '["matrix.org"]'
#TUWUNEL_LOG: warn,state_res=warn
TUWUNEL_ADDRESS: 0.0.0.0
+26 -47
View File
@@ -1,9 +1,9 @@
# Generic deployment documentation
> ### Getting help
> [!TIP]
>
> If you run into any problems while setting up Tuwunel [open an issue on
> GitHub](https://github.com/matrix-construct/tuwunel/issues/new).
> Getting help: If you run into any problems while setting up Tuwunel
> [open an issue on GitHub](https://github.com/matrix-construct/tuwunel/issues/new).
## Installing Tuwunel
@@ -141,46 +141,31 @@ sudo chmod 700 /var/lib/tuwunel/
## Setting up the Reverse Proxy
We recommend Caddy as a reverse proxy, as it is trivial to use, handling TLS certificates, reverse proxy headers, etc transparently with proper defaults.
For other software, please refer to their respective documentation or online guides.
We recommend Caddy as a reverse proxy, as it is trivial to use, handling TLS certificates, reverse proxy headers, etc. transparently with proper defaults. However, Nginx is also well-supported and widely used.
### Caddy
**Choose your reverse proxy:**
After installing Caddy via your preferred method, create `/etc/caddy/conf.d/tuwunel_caddyfile`
and enter this (substitute for your server name).
- **[Caddy Setup Guide](reverse-proxy-caddy.md)** - Recommended for ease of use and automatic TLS
- **[Nginx Setup Guide](reverse-proxy-nginx.md)** - Popular choice with extensive documentation
```caddyfile
your.server.name, your.server.name:8448 {
# TCP reverse_proxy
reverse_proxy localhost:8008
# UNIX socket
#reverse_proxy unix//run/tuwunel/tuwunel.sock
}
```
### Quick Overview
That's it! Just start and enable the service and you're set.
Regardless of which reverse proxy you choose, you will need to:
```bash
sudo systemctl enable --now caddy
```
1. **Reverse proxy the following routes:**
- `/_matrix/` - core Matrix C-S and S-S APIs
- `/_tuwunel/` - ad-hoc Tuwunel routes such as `/local_user_count` and `/server_version`
### Other Reverse Proxies
2. **Optionally reverse proxy (recommended):**
- `/.well-known/matrix/client` and `/.well-known/matrix/server` if using Tuwunel to perform delegation (see the `[global.well_known]` config section)
- `/.well-known/matrix/support` if using Tuwunel to send the homeserver admin contact and support page (formerly known as MSC1929)
- `/` if you would like to see `hewwo from tuwunel woof!` at the root
_Specific contributions for other proxies are welcome!_
3. **Handle ports:**
- Port 443 (HTTPS) for client-server API
- Port 8448 for federation (if federating with other homeservers)
You will need to reverse proxy everything under following routes:
- `/_matrix/` - core Matrix C-S and S-S APIs
- `/_tuwunel/` - ad-hoc Tuwunel routes such as `/local_user_count` and
`/server_version`
You can optionally reverse proxy the following individual routes:
- `/.well-known/matrix/client` and `/.well-known/matrix/server` if using
Tuwunel to perform delegation (see the `[global.well_known]` config section)
- `/.well-known/matrix/support` if using Tuwunel to send the homeserver admin
contact and support page (formerly known as MSC1929)
- `/` if you would like to see `hewwo from tuwunel woof!` at the root
See the following spec pages for more details on these files:
See the following spec pages for more details on well-known files:
- [`/.well-known/matrix/server`](https://spec.matrix.org/latest/client-server-api/#getwell-knownmatrixserver)
- [`/.well-known/matrix/client`](https://spec.matrix.org/latest/client-server-api/#getwell-knownmatrixclient)
- [`/.well-known/matrix/support`](https://spec.matrix.org/latest/client-server-api/#getwell-knownmatrixsupport)
@@ -189,21 +174,15 @@ Examples of delegation:
- <https://matrix.org/.well-known/matrix/server>
- <https://matrix.org/.well-known/matrix/client>
For Apache and Nginx there are many examples available online.
### Other Reverse Proxies
Lighttpd is not supported as it seems to mess with the `X-Matrix` Authorization
header, making federation non-functional. If a workaround is found, feel free to share to get it added to the documentation here.
_Specific contributions for other proxies are welcome!_
If using Apache, you need to use `nocanon` in your `ProxyPass` directive to prevent httpd from messing with the `X-Matrix` header (note that Apache isn't very good as a general reverse proxy and we discourage the usage of it if you can).
**Not Recommended:**
- **Apache**: While possible, Apache requires special configuration (`nocanon` in `ProxyPass`) to prevent corruption of the `X-Matrix` header.
- **Lighttpd**: Its proxy module alters the `X-Matrix` authorization header, breaking federation functionality.
If using Nginx, you need to give Tuwunel the request URI using `$request_uri`, or like so:
- `proxy_pass http://127.0.0.1:6167$request_uri;`
- `proxy_pass http://127.0.0.1:6167;`
Nginx users need to increase `client_max_body_size` (default is 1M) to match
`max_request_size` defined in tuwunel.toml.
## You're done
## You are done
Now you can start Tuwunel with:
+48
View File
@@ -0,0 +1,48 @@
# Reverse Proxy Setup - Caddy
[<= Back to Generic Deployment Guide](generic.md#setting-up-the-reverse-proxy)
We recommend Caddy as a reverse proxy, as it is trivial to use, handling TLS certificates, reverse proxy headers, etc. transparently with proper defaults.
## Installation
Install Caddy via your preferred method. Refer to the [official Caddy installation guide](https://caddyserver.com/docs/install) for your distribution.
## Configuration
After installing Caddy, create `/etc/caddy/conf.d/tuwunel_caddyfile` and enter this (substitute `your.server.name` with your actual server name):
```caddyfile
your.server.name, your.server.name:8448 {
# TCP reverse_proxy
reverse_proxy localhost:8008
# UNIX socket (alternative - comment out the line above and uncomment this)
#reverse_proxy unix//run/tuwunel/tuwunel.sock
}
```
### What this does
- Handles both port 443 (HTTPS) and port 8448 (Matrix federation) automatically
- Automatically provisions and renews TLS certificates via Let's Encrypt
- Sets all necessary reverse proxy headers correctly
- Routes all traffic to Tuwunel listening on `localhost:8008`
That's it! Just start and enable the service and you're set.
```bash
sudo systemctl enable --now caddy
```
## Verification
After starting Caddy, verify it's working by checking:
```bash
curl https://your.server.name/_tuwunel/server_version
curl https://your.server.name:8448/_tuwunel/server_version
```
---
[=> Continue with "You're Done"](generic.md#you-are-done)
+164
View File
@@ -0,0 +1,164 @@
# Reverse Proxy Setup - Nginx
[<= Back to Generic Deployment Guide](generic.md#setting-up-the-reverse-proxy)
This guide shows you how to configure Nginx as a reverse proxy for Tuwunel with TLS support.
## Installation
Install Nginx via your preferred method. Most distributions include Nginx in their package repositories:
```bash
# Debian/Ubuntu
sudo apt install nginx
# Red Hat/Fedora
sudo dnf install nginx
# Arch Linux
sudo pacman -S nginx
```
## Configuration
Create a new configuration file at `/etc/nginx/sites-available/tuwunel` (or `/etc/nginx/conf.d/tuwunel.conf` on some distributions):
```nginx
# Client-Server API over HTTPS (port 443)
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name matrix.example.com;
# Nginx standard body size is 1MB, which is quite small for media uploads
# Increase this to match the max_request_size in your tuwunel.toml
client_max_body_size 100M;
# Forward requests to Tuwunel (listening on 127.0.0.1:8008)
location / {
proxy_pass http://127.0.0.1:8008;
# Preserve host and scheme - critical for proper Matrix operation
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-Proto https;
}
# TLS configuration (Let's Encrypt example using certbot)
ssl_certificate /etc/letsencrypt/live/matrix.example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/matrix.example.com/privkey.pem;
}
# Matrix Federation over HTTPS (port 8448)
# Only needed if you want to federate with other homeservers
# Don't forget to open port 8448 in your firewall!
server {
listen 8448 ssl http2;
listen [::]:8448 ssl http2;
server_name matrix.example.com;
# Same body size increase for larger files
client_max_body_size 100M;
# Forward to the same local port as client-server API
location / {
proxy_pass http://127.0.0.1:8008;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-Proto https;
}
# TLS configuration (same certificates as above)
ssl_certificate /etc/letsencrypt/live/matrix.example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/matrix.example.com/privkey.pem;
}
```
### Important Notes
- **Replace `matrix.example.com`** with your actual server name
- **`client_max_body_size`**: Must match or exceed `max_request_size` in your `tuwunel.toml`
- **Do NOT use `$request_uri`** in `proxy_pass` - while some guides suggest this, it's not necessary for Tuwunel and can cause issues
- **IPv6**: The `listen [::]:443` and `listen [::]:8448` lines enable IPv6 support. Remove them if you don't need IPv6
### TLS Certificates
The example above uses Let's Encrypt certificates via certbot. To obtain certificates:
```bash
sudo certbot certonly --nginx -d matrix.example.com
```
Certbot will automatically handle renewal. Make sure to reload Nginx after certificate renewal:
```bash
sudo systemctl reload nginx
```
### Optional: Timeout Configuration
The default Nginx timeouts are usually sufficient for Matrix operations. Element's long-polling `/sync` requests typically run for 30 seconds, which is within Nginx's default timeouts.
However, if you experience federation retries or dropped long-poll connections, you can extend the timeouts by adding these lines inside your `location /` blocks:
```nginx
location / {
proxy_pass http://127.0.0.1:8008;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-Proto https;
# Optional: Extend timeouts if experiencing issues
proxy_read_timeout 300s;
proxy_send_timeout 300s;
}
```
## Enable the Configuration
If using sites-available/sites-enabled structure:
```bash
sudo ln -s /etc/nginx/sites-available/tuwunel /etc/nginx/sites-enabled/
```
Test the configuration:
```bash
sudo nginx -t
```
If the test passes, reload Nginx:
```bash
sudo systemctl reload nginx
```
Enable Nginx to start on boot:
```bash
sudo systemctl enable nginx
```
## Verification
After configuring Nginx, verify it's working by checking:
```bash
curl https://matrix.example.com/_tuwunel/server_version
curl https://matrix.example.com:8448/_tuwunel/server_version
```
## Troubleshooting
### Apache Compatibility Note
If you're considering Apache instead of Nginx: Apache is not well-suited as a reverse proxy for Matrix homeservers. If you must use Apache, you need to use `nocanon` in your `ProxyPass` directive to prevent httpd from corrupting the `X-Matrix` authorization header, which will break federation.
### Lighttpd is Not Supported
Lighttpd has known issues with the `X-Matrix` authorization header, making federation non-functional. We do not recommend using Lighttpd with Tuwunel.
---
[=> Continue with "You're Done"](generic.md#you-are-done)
+1 -1
View File
@@ -1,5 +1,5 @@
[Container]
Environment=TUWUNEL_SERVER_NAME=your.domain.here TUWUNEL_DATABASE_PATH=/var/lib/tuwunel TUWUNEL_PORT=6167 TUWUNEL_MAX_REQUEST_SIZE=20000000 TUWUNEL_ALLOW_REGISTRATION=true TUWUNEL_REGISTRATION_TOKEN=YOUR_TOKEN TUWUNEL_ALLOW_FEDERATION=true TUWUNEL_ALLOW_CHECK_FOR_UPDATES=true TUWUNEL_TRUSTED_SERVERS=["matrix.org"] TUWUNEL_ADDRESS=0.0.0.0 # Add TUWUNEL_CONFIG: '/etc/tuwunel.toml' if the config is mapped
Environment=TUWUNEL_SERVER_NAME=your.domain.here TUWUNEL_DATABASE_PATH=/var/lib/tuwunel TUWUNEL_PORT=6167 TUWUNEL_MAX_REQUEST_SIZE=20000000 TUWUNEL_ALLOW_REGISTRATION=true TUWUNEL_REGISTRATION_TOKEN=YOUR_TOKEN TUWUNEL_ALLOW_FEDERATION=true TUWUNEL_TRUSTED_SERVERS=["matrix.org"] TUWUNEL_ADDRESS=0.0.0.0 # Add TUWUNEL_CONFIG: '/etc/tuwunel.toml' if the config is mapped
Image=docker.io/jevolk/tuwunel:latest
PublishPort=8448:6167
Volume=/path/to/db:/var/lib/tuwunel
Generated
+52 -48
View File
@@ -32,11 +32,11 @@
"nixpkgs": "nixpkgs_2"
},
"locked": {
"lastModified": 1756385612,
"narHash": "sha256-+NU5MMhuPHHRyvZZWNFG7zt+leRSPsJu1MwhOUzkPUk=",
"lastModified": 1763236786,
"narHash": "sha256-JB19RGXDr6loKSdqwvA15jhRHwf6+9Crq2glqqVar84=",
"owner": "cachix",
"repo": "cachix",
"rev": "dc24688cd67518c3711d511fa369c0f5a131063a",
"rev": "938a275857047c300596092beaabaee6d892e243",
"type": "github"
},
"original": {
@@ -68,11 +68,11 @@
]
},
"locked": {
"lastModified": 1748883665,
"narHash": "sha256-R0W7uAg+BLoHjMRMQ8+oiSbTq8nkGz5RDpQ+ZfxxP3A=",
"lastModified": 1752264895,
"narHash": "sha256-1zBPE/PNAkPNUsOWFET4J0cjlvziH8DOekesDmjND+w=",
"owner": "cachix",
"repo": "cachix",
"rev": "f707778d902af4d62d8dd92c269f8e70de09acbe",
"rev": "47053aef762f452e816e44eb9a23fbc3827b241a",
"type": "github"
},
"original": {
@@ -85,11 +85,11 @@
"complement": {
"flake": false,
"locked": {
"lastModified": 1756358878,
"narHash": "sha256-H/4TkbFcGsMRDZ7gNaXZ7NxeO3b1YgkVt9zPb+3ATSg=",
"lastModified": 1761739261,
"narHash": "sha256-XdzSBbJIYG6thrHbo44/qBiMu5R4bayfy/dlWo9AXBA=",
"owner": "matrix-construct",
"repo": "complement",
"rev": "403840348f6bcc9cc8ed1671dc2f638c2b1ce4ac",
"rev": "350d7666cab14cb0051ef53da7a1b0b3216d7269",
"type": "github"
},
"original": {
@@ -116,11 +116,11 @@
},
"crane_2": {
"locked": {
"lastModified": 1758758545,
"narHash": "sha256-NU5WaEdfwF6i8faJ2Yh+jcK9vVFrofLcwlD/mP65JrI=",
"lastModified": 1763511871,
"narHash": "sha256-KKZWi+ij7oT0Ag8yC6MQkzfHGcytyjMJDD+47ZV1YNU=",
"owner": "ipetkov",
"repo": "crane",
"rev": "95d528a5f54eaba0d12102249ce42f4d01f4e364",
"rev": "099f9014bc8d0cd6e445470ea1df0fd691d5a548",
"type": "github"
},
"original": {
@@ -137,6 +137,7 @@
"cachix",
"flake-compat"
],
"flake-parts": "flake-parts_2",
"git-hooks": [
"cachix",
"git-hooks"
@@ -148,11 +149,11 @@
]
},
"locked": {
"lastModified": 1754404745,
"narHash": "sha256-BdbW/iTImczgcuATgQIa9sPGuYIBxVq2xqcvICsa2AQ=",
"lastModified": 1760560333,
"narHash": "sha256-goJQdVl9oDgCxF9CggPUw1DvB4gsot1jzMmz9px8Du8=",
"owner": "cachix",
"repo": "devenv",
"rev": "6563b21105168f90394dfaf58284b078af2d7275",
"rev": "0a4043938f540027e562c5a0feebbe6be872c3ea",
"type": "github"
},
"original": {
@@ -169,11 +170,11 @@
"rust-analyzer-src": "rust-analyzer-src"
},
"locked": {
"lastModified": 1759251341,
"narHash": "sha256-0vt4IQnTLyAhaeom3h9GOCpO2+av+wK4zP7O8BSKr6I=",
"lastModified": 1763707297,
"narHash": "sha256-Bd9VGavwFBLpyU4pjiWfv73gUibNj8dc3xmOW8ff3bI=",
"owner": "nix-community",
"repo": "fenix",
"rev": "af8a7505a1c62fab493157ea380a05c094bb63af",
"rev": "7c2d3a165a4a080fdcb6c191d8f9768281c99f75",
"type": "github"
},
"original": {
@@ -218,11 +219,11 @@
"flake-compat_3": {
"flake": false,
"locked": {
"lastModified": 1747046372,
"narHash": "sha256-CIVLLkVgvHYbgI2UpXvIIBJ12HWgX+fjA8Xf8PUmqCY=",
"lastModified": 1761588595,
"narHash": "sha256-XKUZz9zewJNUj46b4AJdiRZJAvSZ0Dqj2BNfXvFlJC4=",
"owner": "edolstra",
"repo": "flake-compat",
"rev": "9100a0f413b0c601e0533d1d94ffd501ce2e7885",
"rev": "f387cd2afec9419c8ee37694406ca490c3f34ee5",
"type": "github"
},
"original": {
@@ -258,16 +259,15 @@
"nixpkgs-lib": [
"cachix",
"devenv",
"nix",
"nixpkgs"
]
},
"locked": {
"lastModified": 1733312601,
"narHash": "sha256-4pDvzqnegAfRkPwO3wmwBhVi/Sye1mzps0zHWYnP88c=",
"lastModified": 1756770412,
"narHash": "sha256-+uWLQZccFHwqpGqr2Yt5VsW/PbeJVTn9Dk6SHWhNRPw=",
"owner": "hercules-ci",
"repo": "flake-parts",
"rev": "205b12d8b7cd4802fbcb8e8ef6a0f1408781a4f9",
"rev": "4524271976b625a4a605beefd893f270620fd751",
"type": "github"
},
"original": {
@@ -308,11 +308,11 @@
]
},
"locked": {
"lastModified": 1750779888,
"narHash": "sha256-wibppH3g/E2lxU43ZQHC5yA/7kIKLGxVEnsnVK1BtRg=",
"lastModified": 1760392170,
"narHash": "sha256-WftxJgr2MeDDFK47fQKywzC72L2jRc/PWcyGdjaDzkw=",
"owner": "cachix",
"repo": "git-hooks.nix",
"rev": "16ec914f6fb6f599ce988427d9d94efddf25fe6d",
"rev": "46d55f0aeb1d567a78223e69729734f3dca25a85",
"type": "github"
},
"original": {
@@ -346,11 +346,11 @@
"liburing": {
"flake": false,
"locked": {
"lastModified": 1759268303,
"narHash": "sha256-nT3JWey0P2lPWlnwN57XHmuzkeW8Mo795+Ahf8S5TMI=",
"lastModified": 1763758538,
"narHash": "sha256-cDsxLOqeC7imBcArolTHvejSnWoadgpvDy1DJ2/3MOw=",
"owner": "axboe",
"repo": "liburing",
"rev": "1671cb952a8f680214b04ae9546d4f649f1b367b",
"rev": "e1ef1e680ee38ed9116989155fca47921698c25f",
"type": "github"
},
"original": {
@@ -367,7 +367,11 @@
"devenv",
"flake-compat"
],
"flake-parts": "flake-parts_2",
"flake-parts": [
"cachix",
"devenv",
"flake-parts"
],
"git-hooks-nix": [
"cachix",
"devenv",
@@ -388,16 +392,16 @@
]
},
"locked": {
"lastModified": 1752773918,
"narHash": "sha256-dOi/M6yNeuJlj88exI+7k154z+hAhFcuB8tZktiW7rg=",
"lastModified": 1758763079,
"narHash": "sha256-Bx1A+lShhOWwMuy3uDzZQvYiBKBFcKwy6G6NEohhv6A=",
"owner": "cachix",
"repo": "nix",
"rev": "031c3cf42d2e9391eee373507d8c12e0f9606779",
"rev": "6f0140527c2b0346df4afad7497baa08decb929f",
"type": "github"
},
"original": {
"owner": "cachix",
"ref": "devenv-2.30",
"ref": "devenv-2.30.5",
"repo": "nix",
"type": "github"
}
@@ -473,11 +477,11 @@
},
"nixpkgs_2": {
"locked": {
"lastModified": 1754214453,
"narHash": "sha256-Q/I2xJn/j1wpkGhWkQnm20nShYnG7TI99foDBpXm1SY=",
"lastModified": 1760524057,
"narHash": "sha256-EVAqOteLBFmd7pKkb0+FIUyzTF61VKi7YmvP1tw4nEw=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "5b09dc45f24cf32316283e62aec81ffee3c3e376",
"rev": "544961dfcce86422ba200ed9a0b00dd4b1486ec5",
"type": "github"
},
"original": {
@@ -489,11 +493,11 @@
},
"nixpkgs_3": {
"locked": {
"lastModified": 1759070547,
"narHash": "sha256-JVZl8NaVRYb0+381nl7LvPE+A774/dRpif01FKLrYFQ=",
"lastModified": 1763618868,
"narHash": "sha256-v5afmLjn/uyD9EQuPBn7nZuaZVV9r+JerayK/4wvdWA=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "647e5c14cbd5067f44ac86b74f014962df460840",
"rev": "a8d610af3f1a5fb71e23e08434d8d61a466fc942",
"type": "github"
},
"original": {
@@ -506,11 +510,11 @@
"rocksdb": {
"flake": false,
"locked": {
"lastModified": 1749358049,
"narHash": "sha256-ZSjvAZBfZkJrBIpw8ANZMbJVb8AeuogvuAipGVE4Qe4=",
"lastModified": 1763593074,
"narHash": "sha256-aOV/jJjRjNJ3hrRqhCsXlIz05NvEhDF/j5Q5UOQuvp8=",
"owner": "matrix-construct",
"repo": "rocksdb",
"rev": "cf7f65d0b377af019661c240f9165b3ef60640c3",
"rev": "9a3a213b55df0b11408102c899a940675c0d90e4",
"type": "github"
},
"original": {
@@ -538,11 +542,11 @@
"rust-analyzer-src": {
"flake": false,
"locked": {
"lastModified": 1759134797,
"narHash": "sha256-YPi+jL3tx/yC5J5l7/OB7Lnlr9BMTzYnZtm7tRJzUNg=",
"lastModified": 1763648203,
"narHash": "sha256-/WJdebbRD+m5vr2xy/bJdCpqd7YHSMapjuXAM/0lvtA=",
"owner": "rust-lang",
"repo": "rust-analyzer",
"rev": "062ac7a5451e8e92a32e22a60d86882d6a034f3f",
"rev": "eaaa2da9fbbfd7a79ff501e0563351cb2004574a",
"type": "github"
},
"original": {
+1 -1
View File
@@ -26,7 +26,7 @@
file = ./rust-toolchain.toml;
# See also `rust-toolchain.toml`
sha256 = "sha256-Qxt8XAuaUR2OMdKbN4u8dBJOhSHxS+uS06Wl9+flVEk=";
sha256 = "sha256-+9FmLhAOezBZCOziO0Qct1NOrfpjNsXxc/8I0c7BdKE=";
};
mkScope = pkgs: pkgs.lib.makeScope pkgs.newScope (self: {
-1
View File
@@ -21,7 +21,6 @@ media_startup_check = true
prune_missing_media = true
log_colors = true
admin_room_notices = false
allow_check_for_updates = false
intentionally_unknown_config_option_for_testing = true
rocksdb_log_level = "info"
rocksdb_max_log_files = 1
+19 -2
View File
@@ -202,8 +202,25 @@ craneLib.buildPackage ( commonAttrs // {
env = buildDepsOnlyEnv;
});
# Disabled due to integration test failing to find /etc/resolv.conf
doCheck = false;
nativeCheckInputs = [
pkgsBuildHost.libredirect.hook
];
preCheck =
let
fakeResolvConf = pkgsBuildHost.writeTextFile {
name = "resolv.conf";
text = ''
nameserver 0.0.0.0
'';
};
in
''
export NIX_REDIRECTS="/etc/resolv.conf=${fakeResolvConf}"
export TUWUNEL_DATABASE_PATH="$(mktemp -d)/smoketest.db"
'';
doCheck = true;
doBenchmark = false;
cargoExtraArgs = "--no-default-features --locked "
-1
View File
@@ -5,7 +5,6 @@ After=network-online.target
Documentation=https://tuwunel.chat/
[Service]
DynamicUser=yes
User=tuwunel
Group=tuwunel
Type=notify
+1 -1
View File
@@ -9,7 +9,7 @@
# If you're having trouble making the relevant changes, bug a maintainer.
[toolchain]
channel = "1.88.0"
channel = "1.89.0"
profile = "minimal"
components = [
# For rust-analyzer
+6 -9
View File
@@ -236,8 +236,8 @@ pub(super) async fn get_remote_pdu(
match self
.services
.sending
.send_federation_request(&server, ruma::api::federation::event::get_event::v1::Request {
.federation
.execute(&server, ruma::api::federation::event::get_event::v1::Request {
event_id: event_id.clone(),
})
.await
@@ -327,11 +327,8 @@ pub(super) async fn ping(&self, server: OwnedServerName) -> Result {
match self
.services
.sending
.send_federation_request(
&server,
ruma::api::federation::discovery::get_server_version::v1::Request {},
)
.federation
.execute(&server, ruma::api::federation::discovery::get_server_version::v1::Request {})
.await
{
| Err(e) => {
@@ -571,8 +568,8 @@ pub(super) async fn force_set_room_state_from_server(
let remote_state_response = self
.services
.sending
.send_federation_request(&server_name, get_room_state::v1::Request {
.federation
.execute(&server_name, get_room_state::v1::Request {
room_id: room_id.clone(),
event_id: first_pdu.event_id().to_owned(),
})
+5 -3
View File
@@ -2,6 +2,7 @@ use clap::Subcommand;
use futures::StreamExt;
use ruma::{OwnedRoomId, OwnedUserId};
use tuwunel_core::Result;
use tuwunel_database::Deserialized;
use crate::{admin_command, admin_command_dispatch};
@@ -46,7 +47,7 @@ async fn changes_since(
.await;
let query_time = timer.elapsed();
self.write_str(&format!("Query completed in {query_time:?}:\n\n```rs\n{results:#?}\n```"))
self.write_str(&format!("Query completed in {query_time:?}:\n\n```rs\n{results:?}\n```"))
.await
}
@@ -58,11 +59,12 @@ async fn account_data_get(
room_id: Option<OwnedRoomId>,
) -> Result {
let timer = tokio::time::Instant::now();
let results = self
let results: serde_json::Value = self
.services
.account_data
.get_raw(room_id.as_deref(), &user_id, &kind)
.await;
.await
.deserialized()?;
let query_time = timer.elapsed();
self.write_str(&format!("Query completed in {query_time:?}:\n\n```rs\n{results:#?}\n```"))
+42 -10
View File
@@ -37,15 +37,6 @@ pub(crate) enum RawCommand {
base64: bool,
},
/// - Raw database delete (for string keys)
Del {
/// Map name
map: String,
/// Key
key: String,
},
/// - Raw database keys iteration
Keys {
/// Map name
@@ -135,7 +126,26 @@ pub(crate) enum RawCommand {
prefix: Option<String>,
},
/// - Compact database
/// - Raw database delete (for string keys) DANGER!!!
Del {
/// Map name
map: String,
/// Key
key: String,
},
/// - Clear database table DANGER!!!
Clear {
/// Map name
map: String,
/// Confirm
#[arg(long)]
confirm: bool,
},
/// - Compact database DANGER!!!
Compact {
#[arg(short, long, alias("column"))]
map: Option<Vec<String>>,
@@ -425,6 +435,28 @@ pub(super) async fn raw_del(&self, map: String, key: String) -> Result {
.await
}
#[admin_command]
pub(super) async fn raw_clear(&self, map: String, confirm: bool) -> Result {
let map = self.services.db.get(&map)?;
if !confirm {
return Err!("Are you really sure you want to clear all data? Add the --confirm option.");
}
let timer = Instant::now();
let cork = self.services.db.cork();
map.raw_keys()
.ignore_err()
.ready_for_each(|key| map.remove(&key))
.boxed()
.await;
drop(cork);
let query_time = timer.elapsed();
self.write_str(&format!("Operation completed in {query_time:?}"))
.await
}
#[admin_command]
pub(super) async fn raw_get(&self, map: String, key: String, base64: bool) -> Result {
let map = self.services.db.get(&map)?;
+14
View File
@@ -8,6 +8,13 @@ use crate::Context;
#[derive(Debug, Subcommand)]
/// All the getters and iterators from src/database/key_value/rooms/alias.rs
pub(crate) enum RoomAliasCommand {
/// - Resolve any local or remote alias.
ResolveAlias {
/// Full room alias
alias: OwnedRoomAliasId,
},
/// - Resolve an alias on this server.
ResolveLocalAlias {
/// Full room alias
alias: OwnedRoomAliasId,
@@ -28,6 +35,13 @@ pub(super) async fn process(subcommand: RoomAliasCommand, context: &Context<'_>)
let services = context.services;
match subcommand {
| RoomAliasCommand::ResolveAlias { alias } => {
let timer = tokio::time::Instant::now();
let results = services.alias.resolve_alias(&alias).await;
let query_time = timer.elapsed();
write!(context, "Query completed in {query_time:?}:\n\n```rs\n{results:#?}\n```")
},
| RoomAliasCommand::ResolveLocalAlias { alias } => {
let timer = tokio::time::Instant::now();
let results = services.alias.resolve_local_alias(&alias).await;
+2 -2
View File
@@ -15,7 +15,7 @@ pub(crate) enum SyncCommand {
/// Show details of sliding sync connection by ID.
ShowConnection {
user_id: OwnedUserId,
device_id: OwnedDeviceId,
device_id: Option<OwnedDeviceId>,
conn_id: Option<String>,
},
@@ -43,7 +43,7 @@ pub(super) async fn list_connections(&self) -> Result {
pub(super) async fn show_connection(
&self,
user_id: OwnedUserId,
device_id: OwnedDeviceId,
device_id: Option<OwnedDeviceId>,
conn_id: Option<String>,
) -> Result {
let key = into_connection_key(user_id, device_id, conn_id);
+35 -156
View File
@@ -1,8 +1,8 @@
use std::{collections::BTreeMap, fmt::Write as _};
use std::collections::BTreeMap;
use futures::{FutureExt, StreamExt};
use ruma::{
Int, OwnedEventId, OwnedRoomId, OwnedRoomOrAliasId, OwnedUserId, UserId,
Int, OwnedDeviceId, OwnedEventId, OwnedRoomId, OwnedRoomOrAliasId, OwnedUserId, UserId,
events::{
RoomAccountDataEventType, StateEventType,
room::{
@@ -13,10 +13,9 @@ use ruma::{
},
};
use tuwunel_core::{
Err, Result, debug, debug_warn, error, info, is_equal_to,
Err, Result, debug_warn, info,
matrix::{Event, pdu::PduBuilder},
utils::{self, ReadyExt},
warn,
};
use tuwunel_service::Services;
@@ -62,148 +61,11 @@ pub(super) async fn create_user(&self, username: String, password: Option<String
let password = password.unwrap_or_else(|| utils::random_string(AUTO_GEN_PASSWORD_LENGTH));
// Create user
self.services
.users
.create(&user_id, Some(password.as_str()), None)
.full_register(&user_id, Some(&password), None, None, false, true)
.await?;
// Default to pretty displayname
let mut displayname = user_id.localpart().to_owned();
// If `new_user_displayname_suffix` is set, registration will push whatever
// content is set to the user's display name with a space before it
if !self
.services
.server
.config
.new_user_displayname_suffix
.is_empty()
{
write!(
displayname,
" {}",
self.services
.server
.config
.new_user_displayname_suffix
)?;
}
self.services
.users
.set_displayname(&user_id, Some(displayname));
// Initial account data
self.services
.account_data
.update(
None,
&user_id,
ruma::events::GlobalAccountDataEventType::PushRules
.to_string()
.into(),
&serde_json::to_value(ruma::events::push_rules::PushRulesEvent {
content: ruma::events::push_rules::PushRulesEventContent {
global: ruma::push::Ruleset::server_default(&user_id),
},
})?,
)
.await?;
if !self
.services
.server
.config
.auto_join_rooms
.is_empty()
{
for room in &self.services.server.config.auto_join_rooms {
let Ok(room_id) = self.services.alias.maybe_resolve(room).await else {
error!(
%user_id,
"Failed to resolve room alias to room ID when attempting to auto join {room}, skipping"
);
continue;
};
if !self
.services
.state_cache
.server_in_room(self.services.globals.server_name(), &room_id)
.await
{
warn!(
"Skipping room {room} to automatically join as we have never joined before."
);
continue;
}
let state_lock = self.services.state.mutex.lock(&room_id).await;
if let Some(room_server_name) = room.server_name() {
match self
.services
.membership
.join(
&user_id,
&room_id,
Some("Automatically joining this room upon registration".to_owned()),
&[
self.services.globals.server_name().to_owned(),
room_server_name.to_owned(),
],
&None,
&state_lock,
)
.await
{
| Ok(_response) => {
info!("Automatically joined room {room} for user {user_id}");
},
| Err(e) => {
// don't return this error so we don't fail registrations
error!(
"Failed to automatically join room {room} for user {user_id}: {e}"
);
self.services
.admin
.send_text(&format!(
"Failed to automatically join room {room} for user {user_id}: \
{e}"
))
.await;
},
}
drop(state_lock);
}
}
}
// we dont add a device since we're not the user, just the creator
// if this account creation is from the CLI / --execute, invite the first user
// to admin room
if let Ok(admin_room) = self.services.admin.get_admin_room().await {
if self
.services
.state_cache
.room_joined_count(&admin_room)
.await
.is_ok_and(is_equal_to!(1))
{
self.services
.admin
.make_user_admin(&user_id)
.boxed()
.await?;
warn!("Granting {user_id} admin privileges as the first user");
}
} else {
debug!("create_user admin command called without an admin room being available");
}
self.write_str(&format!("Created user with user_id: {user_id} and password: `{password}`"))
.await
}
@@ -224,6 +86,25 @@ pub(super) async fn deactivate(&self, no_leave_rooms: bool, user_id: String) ->
.await
}
#[admin_command]
pub(super) async fn delete_device(
&self,
user_id: OwnedUserId,
device_id: OwnedDeviceId,
) -> Result {
if !self.services.globals.user_is_local(&user_id) {
return Err!("Cannot delete device of remote user");
}
self.services
.users
.remove_device(&user_id, &device_id)
.await;
self.write_str(&format!("User {user_id}'s device {device_id} removed."))
.await
}
#[admin_command]
pub(super) async fn reset_password(&self, username: String, password: Option<String>) -> Result {
let user_id = parse_local_user_id(self.services, &username)?;
@@ -384,7 +265,7 @@ pub(super) async fn list_joined_rooms(&self, user_id: String) -> Result {
#[admin_command]
pub(super) async fn force_join_list_of_local_users(
&self,
room_id: OwnedRoomOrAliasId,
room: OwnedRoomOrAliasId,
yes_i_want_to_do_this: bool,
) -> Result {
if self.body.len() < 2
@@ -396,7 +277,7 @@ pub(super) async fn force_join_list_of_local_users(
if !yes_i_want_to_do_this {
return Err!(
"You must pass the --yes-i-want-to-do-this-flag to ensure you really want to force \
"You must pass the --yes-i-want-to-do-this flag to ensure you really want to force \
bulk join all specified local users.",
);
}
@@ -408,7 +289,7 @@ pub(super) async fn force_join_list_of_local_users(
let (room_id, servers) = self
.services
.alias
.maybe_resolve_with_servers(&room_id, None)
.maybe_resolve_with_servers(&room, None)
.await?;
if !self
@@ -486,9 +367,10 @@ pub(super) async fn force_join_list_of_local_users(
.join(
&user_id,
&room_id,
Some(&room),
Some(String::from(BULK_JOIN_REASON)),
&servers,
&None,
false,
&state_lock,
)
.await
@@ -515,7 +397,7 @@ pub(super) async fn force_join_list_of_local_users(
#[admin_command]
pub(super) async fn force_join_all_local_users(
&self,
room_id: OwnedRoomOrAliasId,
room: OwnedRoomOrAliasId,
yes_i_want_to_do_this: bool,
) -> Result {
if !yes_i_want_to_do_this {
@@ -532,7 +414,7 @@ pub(super) async fn force_join_all_local_users(
let (room_id, servers) = self
.services
.alias
.maybe_resolve_with_servers(&room_id, None)
.maybe_resolve_with_servers(&room, None)
.await?;
if !self
@@ -581,9 +463,10 @@ pub(super) async fn force_join_all_local_users(
.join(
user_id,
&room_id,
Some(&room),
Some(String::from(BULK_JOIN_REASON)),
&servers,
&None,
false,
&state_lock,
)
.await
@@ -608,16 +491,12 @@ pub(super) async fn force_join_all_local_users(
}
#[admin_command]
pub(super) async fn force_join_room(
&self,
user_id: String,
room_id: OwnedRoomOrAliasId,
) -> Result {
pub(super) async fn force_join_room(&self, user_id: String, room: OwnedRoomOrAliasId) -> Result {
let user_id = parse_local_user_id(self.services, &user_id)?;
let (room_id, servers) = self
.services
.alias
.maybe_resolve_with_servers(&room_id, None)
.maybe_resolve_with_servers(&room, None)
.await?;
assert!(
@@ -629,7 +508,7 @@ pub(super) async fn force_join_room(
self.services
.membership
.join(&user_id, &room_id, None, &servers, &None, &state_lock)
.join(&user_id, &room_id, Some(&room), None, &servers, false, &state_lock)
.await?;
drop(state_lock);
+10 -4
View File
@@ -1,7 +1,7 @@
mod commands;
use clap::Subcommand;
use ruma::{OwnedEventId, OwnedRoomId, OwnedRoomOrAliasId};
use ruma::{OwnedDeviceId, OwnedEventId, OwnedRoomId, OwnedRoomOrAliasId, OwnedUserId};
use tuwunel_core::Result;
use crate::admin_command_dispatch;
@@ -59,6 +59,12 @@ pub(super) enum UserCommand {
force: bool,
},
/// - Deletes a user's device.
DeleteDevice {
user_id: OwnedUserId,
device_id: OwnedDeviceId,
},
/// - List local users in the database
#[clap(alias = "list")]
ListUsers,
@@ -72,7 +78,7 @@ pub(super) enum UserCommand {
/// - Manually join a local user to a room.
ForceJoinRoom {
user_id: String,
room_id: OwnedRoomOrAliasId,
room: OwnedRoomOrAliasId,
},
/// - Manually leave a local user from a room.
@@ -142,7 +148,7 @@ pub(super) enum UserCommand {
///
/// Requires the `--yes-i-want-to-do-this` flag.
ForceJoinListOfLocalUsers {
room_id: OwnedRoomOrAliasId,
room: OwnedRoomOrAliasId,
#[arg(long)]
yes_i_want_to_do_this: bool,
@@ -154,7 +160,7 @@ pub(super) enum UserCommand {
///
/// Requires the `--yes-i-want-to-do-this` flag.
ForceJoinAllLocalUsers {
room_id: OwnedRoomOrAliasId,
room: OwnedRoomOrAliasId,
#[arg(long)]
yes_i_want_to_do_this: bool,
+7 -5
View File
@@ -5,7 +5,7 @@ use ruma::api::client::account::{
ThirdPartyIdRemovalStatus, change_password, deactivate, get_3pids,
request_3pid_management_token_via_email, request_3pid_management_token_via_msisdn, whoami,
};
use tuwunel_core::{Err, Result, info, utils::ReadyExt};
use tuwunel_core::{Err, Result, err, info, utils::ReadyExt};
use crate::{Ruma, router::auth_uiaa};
@@ -73,10 +73,12 @@ pub(crate) async fn whoami_route(
Ok(whoami::v3::Response {
user_id: body.sender_user().to_owned(),
device_id: body.sender_device.clone(),
is_guest: services
.users
.is_deactivated(body.sender_user())
.await? && body.appservice_info.is_none(),
is_guest: body.appservice_info.is_none()
&& services
.users
.is_deactivated(body.sender_user())
.await
.map_err(|_| err!(Request(Forbidden("User does not exist."))))?,
})
}
+7 -17
View File
@@ -121,26 +121,16 @@ async fn room_available_servers(
// insert our server as the very first choice if in list, else check if we can
// prefer the room alias server first
match servers
if let Some(server_index) = servers
.iter()
.position(|server_name| services.globals.server_is_ours(server_name))
{
| Some(server_index) => {
servers.swap_remove(server_index);
servers.insert(0, services.globals.server_name().to_owned());
},
| _ => {
match servers
.iter()
.position(|server| server == room_alias.server_name())
{
| Some(alias_server_index) => {
servers.swap_remove(alias_server_index);
servers.insert(0, room_alias.server_name().into());
},
| _ => {},
}
},
servers.swap(0, server_index);
} else if let Some(alias_server_index) = servers
.iter()
.position(|server| server == room_alias.server_name())
{
servers.swap(0, alias_server_index);
}
servers
+4 -7
View File
@@ -37,13 +37,10 @@ pub(crate) async fn appservice_ping(
let timer = tokio::time::Instant::now();
let _response = services
.sending
.send_appservice_request(
appservice_info.registration.clone(),
ping::send_ping::v1::Request {
transaction_id: body.transaction_id.clone(),
},
)
.appservice
.send_request(appservice_info.registration.clone(), ping::send_ping::v1::Request {
transaction_id: body.transaction_id.clone(),
})
.await?
.expect("We already validated if an appservice URL exists above");
+3 -3
View File
@@ -32,8 +32,8 @@ pub(crate) async fn get_context_route(
State(services): State<crate::State>,
body: Ruma<get_context::v3::Request>,
) -> Result<get_context::v3::Response> {
let sender = body.sender();
let (sender_user, sender_device) = sender;
let sender_user = body.sender_user();
let sender_device = body.sender_device.as_deref();
let room_id = &body.room_id;
let event_id = &body.event_id;
let filter = &body.filter;
@@ -110,7 +110,7 @@ pub(crate) async fn get_context_route(
let lazy_loading_context = lazy_loading::Context {
user_id: sender_user,
device_id: Some(sender_device),
device_id: sender_device,
room_id,
token: Some(base_count.into_unsigned()),
options: Some(&filter.lazy_load_options),
+132
View File
@@ -0,0 +1,132 @@
use axum::extract::State;
use axum_client_ip::InsecureClientIp;
use futures::StreamExt;
use ruma::api::client::dehydrated_device::{
delete_dehydrated_device::unstable as delete_dehydrated_device,
get_dehydrated_device::unstable as get_dehydrated_device, get_events::unstable as get_events,
put_dehydrated_device::unstable as put_dehydrated_device,
};
use tuwunel_core::{Err, Result, at, utils::result::IsErrOr};
use crate::Ruma;
const MAX_BATCH_EVENTS: usize = 50;
/// # `PUT /_matrix/client/../dehydrated_device`
///
/// Creates or overwrites the user's dehydrated device.
#[tracing::instrument(skip_all, fields(%client))]
pub(crate) async fn put_dehydrated_device_route(
State(services): State<crate::State>,
InsecureClientIp(client): InsecureClientIp,
body: Ruma<put_dehydrated_device::Request>,
) -> Result<put_dehydrated_device::Response> {
let sender_user = body
.sender_user
.as_deref()
.expect("AccessToken authentication required");
let device_id = body.body.device_id.clone();
services
.users
.set_dehydrated_device(sender_user, body.body)
.await?;
Ok(put_dehydrated_device::Response { device_id })
}
/// # `DELETE /_matrix/client/../dehydrated_device`
///
/// Deletes the user's dehydrated device without replacement.
#[tracing::instrument(skip_all, fields(%client))]
pub(crate) async fn delete_dehydrated_device_route(
State(services): State<crate::State>,
InsecureClientIp(client): InsecureClientIp,
body: Ruma<delete_dehydrated_device::Request>,
) -> Result<delete_dehydrated_device::Response> {
let sender_user = body.sender_user();
let device_id = services
.users
.get_dehydrated_device_id(sender_user)
.await?;
services
.users
.remove_device(sender_user, &device_id)
.await;
Ok(delete_dehydrated_device::Response { device_id })
}
/// # `GET /_matrix/client/../dehydrated_device`
///
/// Gets the user's dehydrated device
#[tracing::instrument(skip_all, fields(%client))]
pub(crate) async fn get_dehydrated_device_route(
State(services): State<crate::State>,
InsecureClientIp(client): InsecureClientIp,
body: Ruma<get_dehydrated_device::Request>,
) -> Result<get_dehydrated_device::Response> {
let sender_user = body.sender_user();
let device = services
.users
.get_dehydrated_device(sender_user)
.await?;
Ok(get_dehydrated_device::Response {
device_id: device.device_id,
device_data: device.device_data,
})
}
/// # `GET /_matrix/client/../dehydrated_device/{device_id}/events`
///
/// Paginates the events of the dehydrated device.
#[tracing::instrument(skip_all, fields(%client))]
pub(crate) async fn get_dehydrated_events_route(
State(services): State<crate::State>,
InsecureClientIp(client): InsecureClientIp,
body: Ruma<get_events::Request>,
) -> Result<get_events::Response> {
let sender_user = body.sender_user();
let device_id = &body.body.device_id;
let existing_id = services
.users
.get_dehydrated_device_id(sender_user)
.await;
if existing_id
.as_ref()
.is_err_or(|existing_id| existing_id != device_id)
{
return Err!(Request(Forbidden("Not the dehydrated device_id.")));
}
let since: Option<u64> = body
.body
.next_batch
.as_deref()
.map(str::parse)
.transpose()?;
let mut next_batch: Option<u64> = None;
let events = services
.users
.get_to_device_events(sender_user, device_id, since, None)
.take(MAX_BATCH_EVENTS)
.inspect(|&(count, _)| {
next_batch.replace(count);
})
.map(at!(1))
.collect()
.await;
Ok(get_events::Response {
events,
next_batch: next_batch.as_ref().map(ToString::to_string),
})
}
+12 -10
View File
@@ -2,14 +2,14 @@ use axum::extract::State;
use axum_client_ip::InsecureClientIp;
use futures::StreamExt;
use ruma::{
MilliSecondsSinceUnixEpoch, OwnedDeviceId,
MilliSecondsSinceUnixEpoch,
api::client::device::{
self, delete_device, delete_devices, get_device, get_devices, update_device,
},
};
use tuwunel_core::{Err, Result, debug, err, utils};
use tuwunel_core::{Err, Result, debug, err, utils::string::to_small_string};
use crate::{Ruma, client::DEVICE_ID_LENGTH, router::auth_uiaa};
use crate::{Ruma, router::auth_uiaa};
/// # `GET /_matrix/client/r0/devices`
///
@@ -61,18 +61,21 @@ pub(crate) async fn update_device_route(
.await
{
| Ok(mut device) => {
let notify = device.display_name != body.display_name;
device.display_name.clone_from(&body.display_name);
device
.last_seen_ip
.clone_from(&Some(client.to_string()));
.clone_from(&Some(to_small_string(client)));
device
.last_seen_ts
.clone_from(&Some(MilliSecondsSinceUnixEpoch::now()));
assert_eq!(device.device_id, body.device_id, "device_id mismatch");
services
.users
.update_device_metadata(sender_user, &body.device_id, &device)
.await?;
.put_device_metadata(sender_user, notify, &device);
Ok(update_device::v3::Response {})
},
@@ -80,6 +83,7 @@ pub(crate) async fn update_device_route(
let Some(appservice) = appservice else {
return Err!(Request(NotFound("Device not found.")));
};
if !appservice.registration.device_management {
return Err!(Request(NotFound("Device not found.")));
}
@@ -90,14 +94,12 @@ pub(crate) async fn update_device_route(
appservice.registration.id
);
let device_id = OwnedDeviceId::from(utils::random_string(DEVICE_ID_LENGTH));
services
.users
.create_device(
sender_user,
&device_id,
(&appservice.registration.as_token, None),
None,
(Some(&appservice.registration.as_token), None),
None,
None,
Some(client.to_string()),
+2 -2
View File
@@ -220,8 +220,8 @@ pub(crate) async fn get_public_rooms_filtered_helper(
server.filter(|server_name| !services.globals.server_is_ours(server_name))
{
let response = services
.sending
.send_federation_request(
.federation
.execute(
other_server,
federation::directory::get_public_rooms_filtered::v1::Request {
limit,
+118
View File
@@ -0,0 +1,118 @@
use std::iter::once;
use axum::extract::State;
use futures::StreamExt;
use ruma::api::client::peeking::listen_to_new_events::v3::{Request, Response};
use tokio::time::{Duration, Instant, timeout_at};
use tuwunel_core::{
Err, Event, Result, at,
matrix::PduCount,
utils::{
BoolExt,
result::FlatOk,
stream::{IterStream, ReadyExt},
},
};
use crate::Ruma;
const EVENT_LIMIT: usize = 50;
/// GET `/_matrix/client/v3/events`
pub(crate) async fn events_route(
State(services): State<crate::State>,
body: Ruma<Request>,
) -> Result<Response> {
let sender_user = body.sender_user();
let from = body
.body
.from
.as_deref()
.map(str::parse)
.flat_ok()
.unwrap_or_default();
let timeout = body
.body
.timeout
.as_ref()
.map(Duration::as_millis)
.map(TryInto::try_into)
.flat_ok()
.unwrap_or(services.config.client_sync_timeout_default)
.max(services.config.client_sync_timeout_min)
.min(services.config.client_sync_timeout_max);
let Some(room_id) = body.room_id.as_deref() else {
//TODO: upgrade ruma
return Err!(Request(InvalidParam("Missing RoomId parameter.")));
};
if !services
.state_accessor
.user_can_see_state_events(sender_user, room_id)
.await
{
return Err!(Request(Forbidden("No room preview available.")));
}
let stop_at = Instant::now()
.checked_add(Duration::from_millis(timeout))
.expect("configuration must limit maximum timeout");
loop {
let watchers = services.sync.watch(
sender_user,
body.sender_device.as_deref(),
once(room_id).stream(),
);
let next_batch = services.globals.wait_pending().await?;
let events = services
.timeline
.pdus(Some(sender_user), room_id, Some(PduCount::Normal(from)))
.ready_filter_map(Result::ok)
.ready_take_while(|(count, _)| PduCount::Normal(next_batch).ge(count))
.take(EVENT_LIMIT)
.collect::<Vec<_>>()
.await;
if !events.is_empty() {
return Ok(Response {
start: events
.first()
.map(at!(0))
.as_ref()
.map(ToString::to_string),
end: events
.last()
.map(at!(0))
.as_ref()
.map(ToString::to_string),
chunk: events
.into_iter()
.map(at!(1))
.map(Event::into_format)
.collect(),
});
}
if timeout_at(stop_at, watchers).await.is_err() || services.server.is_stopping() {
return Ok(Response {
chunk: Default::default(),
start: body.body.from,
end: services
.server
.is_stopping()
.is_false()
.then_some(next_batch)
.as_ref()
.map(ToString::to_string),
});
}
}
}
+16 -29
View File
@@ -35,32 +35,19 @@ pub(crate) async fn upload_keys_route(
State(services): State<crate::State>,
body: Ruma<upload_keys::v3::Request>,
) -> Result<upload_keys::v3::Response> {
let (sender_user, sender_device) = body.sender();
let sender_user = body.sender_user();
let sender_device = body.sender_device()?;
for (key_id, one_time_key) in body
let one_time_keys = body
.one_time_keys
.iter()
.take(services.config.one_time_key_limit)
{
if one_time_key
.deserialize()
.inspect_err(|e| {
debug_warn!(
?key_id,
?one_time_key,
"Invalid one time key JSON submitted by client, skipping: {e}"
);
})
.is_err()
{
continue;
}
.map(|(id, val)| (id.as_ref(), val));
services
.users
.add_one_time_key(sender_user, sender_device, key_id, one_time_key)
.await?;
}
services
.users
.add_one_time_keys(sender_user, sender_device, one_time_keys)
.await?;
if let Some(device_keys) = &body.device_keys {
let deser_device_keys = device_keys.deserialize().map_err(|e| {
@@ -85,8 +72,11 @@ pub(crate) async fn upload_keys_route(
.users
.get_device_keys(sender_user, sender_device)
.await
.and_then(|keys| keys.deserialize().map_err(Into::into))
{
if existing_keys.json().get() == device_keys.json().get() {
// NOTE: also serves as a workaround for a nheko bug which omits cross-signing
// NOTE: signatures when re-uploading the same DeviceKeys.
if existing_keys.keys == deser_device_keys.keys {
debug!(
?sender_user,
?sender_device,
@@ -514,10 +504,7 @@ where
let request =
federation::keys::get_keys::v1::Request { device_keys: device_keys_input_fed };
let response = services
.sending
.send_federation_request(server, request)
.await;
let response = services.federation.execute(server, request).await;
(server, response)
})
@@ -585,7 +572,7 @@ fn add_unsigned_device_display_name(
.or_insert_with(|| CanonicalJsonObject::default().into())
{
let display_name = if include_display_names {
CanonicalJsonValue::String(display_name)
CanonicalJsonValue::String(display_name.to_string())
} else {
CanonicalJsonValue::String(metadata.device_id.into())
};
@@ -642,8 +629,8 @@ pub(crate) async fn claim_keys_helper(
(
server,
services
.sending
.send_federation_request(server, federation::keys::claim_keys::v1::Request {
.federation
.execute(server, federation::keys::claim_keys::v1::Request {
one_time_keys: one_time_keys_input_fed,
})
.await,
+1 -2
View File
@@ -22,8 +22,7 @@ pub(crate) async fn invite_user_route(
invite_check(&services, sender_user, room_id).await?;
banned_room_check(&services, sender_user, Some(room_id), room_id.server_name(), client)
.await?;
banned_room_check(&services, sender_user, room_id, None, client).await?;
let invite_user::v3::InvitationRecipient::UserId { user_id } = &body.recipient else {
return Err!(Request(ThreepidDenied("Third party identifiers are not implemented")));
+44 -22
View File
@@ -2,13 +2,13 @@ use axum::extract::State;
use axum_client_ip::InsecureClientIp;
use futures::FutureExt;
use ruma::{
RoomId, RoomOrAliasId,
RoomId,
api::client::membership::{join_room_by_id, join_room_by_id_or_alias},
};
use tuwunel_core::Result;
use tuwunel_core::{Result, warn};
use super::banned_room_check;
use crate::{Ruma, client::membership::get_join_params};
use crate::Ruma;
/// # `POST /_matrix/client/r0/rooms/{roomId}/join`
///
@@ -28,30 +28,38 @@ pub(crate) async fn join_room_by_id_route(
let room_id: &RoomId = &body.room_id;
banned_room_check(&services, sender_user, Some(room_id), room_id.server_name(), client)
.await?;
banned_room_check(&services, sender_user, room_id, None, client).await?;
let (room_id, servers) =
get_join_params(&services, sender_user, <&RoomOrAliasId>::from(room_id), &[]).await?;
let state_lock = services.state.mutex.lock(room_id).await;
let state_lock = services.state.mutex.lock(&room_id).await;
services
let mut errors = 0_usize;
while let Err(e) = services
.membership
.join(
sender_user,
&room_id,
room_id,
None,
body.reason.clone(),
&servers,
&body.appservice_info,
&[],
body.appservice_info.is_some(),
&state_lock,
)
.boxed()
.await?;
.await
{
errors = errors.saturating_add(1);
if errors >= services.config.max_join_attempts_per_join_request {
warn!(
"Several servers failed. Giving up for this request. Try again for different \
server selection."
);
return Err(e);
}
}
drop(state_lock);
Ok(join_room_by_id::v3::Response { room_id })
Ok(join_room_by_id::v3::Response { room_id: room_id.to_owned() })
}
/// # `POST /_matrix/client/r0/join/{roomIdOrAlias}`
@@ -72,28 +80,42 @@ pub(crate) async fn join_room_by_id_or_alias_route(
let sender_user = body.sender_user();
let appservice_info = &body.appservice_info;
let (room_id, servers) =
get_join_params(&services, sender_user, &body.room_id_or_alias, &body.via).await?;
let (room_id, servers) = services
.alias
.maybe_resolve_with_servers(&body.room_id_or_alias, Some(&body.via))
.await?;
banned_room_check(&services, sender_user, Some(&room_id), room_id.server_name(), client)
banned_room_check(&services, sender_user, &room_id, Some(&body.room_id_or_alias), client)
.await?;
let state_lock = services.state.mutex.lock(&room_id).await;
services
let mut errors = 0_usize;
while let Err(e) = services
.membership
.join(
sender_user,
&room_id,
Some(&body.room_id_or_alias),
body.reason.clone(),
&servers,
appservice_info,
appservice_info.is_some(),
&state_lock,
)
.boxed()
.await?;
.await
{
errors = errors.saturating_add(1);
if errors >= services.config.max_join_attempts_per_join_request {
warn!(
"Several servers failed. Giving up for this request. Try again for different \
server selection."
);
return Err(e);
}
}
drop(state_lock);
Ok(join_room_by_id_or_alias::v3::Response { room_id })
Ok(join_room_by_id_or_alias::v3::Response { room_id: room_id.clone() })
}
+16 -566
View File
@@ -1,45 +1,10 @@
use std::{borrow::Borrow, collections::HashMap, iter::once, sync::Arc};
use axum::extract::State;
use axum_client_ip::InsecureClientIp;
use futures::{FutureExt, StreamExt};
use ruma::{
CanonicalJsonObject, CanonicalJsonValue, OwnedEventId, OwnedServerName, RoomId,
RoomVersionId, UserId,
api::{
client::knock::knock_room,
federation::{
membership::RawStrippedState,
{self},
},
},
canonical_json::to_canonical_value,
events::{
StateEventType,
room::member::{MembershipState, RoomMemberEventContent},
},
};
use tuwunel_core::{
Err, Result, debug, debug_info, debug_warn, err, extract_variant, info,
matrix::{
PduCount,
event::{Event, gen_event_id},
pdu::{PduBuilder, PduEvent},
},
trace,
utils::{self},
warn,
};
use tuwunel_service::{
Services,
rooms::{
state::RoomMutexGuard,
state_compressor::{CompressedState, HashSetCompressStateEvent},
},
};
use ruma::api::client::knock::knock_room;
use tuwunel_core::Result;
use super::banned_room_check;
use crate::{Ruma, client::membership::get_join_params};
use crate::Ruma;
/// # `POST /_matrix/client/*/knock/{roomIdOrAlias}`
///
@@ -51,545 +16,30 @@ pub(crate) async fn knock_room_route(
body: Ruma<knock_room::v3::Request>,
) -> Result<knock_room::v3::Response> {
let sender_user = body.sender_user();
let body = &body.body;
let (room_id, servers) =
get_join_params(&services, sender_user, &body.room_id_or_alias, &body.via).await?;
banned_room_check(&services, sender_user, Some(&room_id), room_id.server_name(), client)
let (room_id, servers) = services
.alias
.maybe_resolve_with_servers(&body.room_id_or_alias, Some(&body.via))
.await?;
knock_room_by_id_helper(&services, sender_user, &room_id, body.reason.clone(), &servers).await
}
async fn knock_room_by_id_helper(
services: &Services,
sender_user: &UserId,
room_id: &RoomId,
reason: Option<String>,
servers: &[OwnedServerName],
) -> Result<knock_room::v3::Response> {
let state_lock = services.state.mutex.lock(room_id).await;
if services
.state_cache
.is_invited(sender_user, room_id)
.await
{
debug_warn!("{sender_user} is already invited in {room_id} but attempted to knock");
return Err!(Request(Forbidden(
"You cannot knock on a room you are already invited/accepted to."
)));
}
if services
.state_cache
.is_joined(sender_user, room_id)
.await
{
debug_warn!("{sender_user} is already joined in {room_id} but attempted to knock");
return Err!(Request(Forbidden("You cannot knock on a room you are already joined in.")));
}
if services
.state_cache
.is_knocked(sender_user, room_id)
.await
{
debug_warn!("{sender_user} is already knocked in {room_id}");
return Ok(knock_room::v3::Response { room_id: room_id.into() });
}
if let Ok(membership) = services
.state_accessor
.get_member(room_id, sender_user)
.await
{
if membership.membership == MembershipState::Ban {
debug_warn!("{sender_user} is banned from {room_id} but attempted to knock");
return Err!(Request(Forbidden("You cannot knock on a room you are banned from.")));
}
}
let server_in_room = services
.state_cache
.server_in_room(services.globals.server_name(), room_id)
.await;
let local_knock = server_in_room
|| servers.is_empty()
|| (servers.len() == 1 && services.globals.server_is_ours(&servers[0]));
if local_knock {
knock_room_helper_local(services, sender_user, room_id, reason, servers, state_lock)
.boxed()
.await?;
} else {
knock_room_helper_remote(services, sender_user, room_id, reason, servers, state_lock)
.boxed()
.await?;
}
Ok(knock_room::v3::Response::new(room_id.to_owned()))
}
async fn knock_room_helper_local(
services: &Services,
sender_user: &UserId,
room_id: &RoomId,
reason: Option<String>,
servers: &[OwnedServerName],
state_lock: RoomMutexGuard,
) -> Result {
debug_info!("We can knock locally");
let room_version_id = services.state.get_room_version(room_id).await?;
if matches!(
room_version_id,
RoomVersionId::V1
| RoomVersionId::V2
| RoomVersionId::V3
| RoomVersionId::V4
| RoomVersionId::V5
| RoomVersionId::V6
) {
return Err!(Request(Forbidden("This room does not support knocking.")));
}
let content = RoomMemberEventContent {
displayname: services.users.displayname(sender_user).await.ok(),
avatar_url: services.users.avatar_url(sender_user).await.ok(),
blurhash: services.users.blurhash(sender_user).await.ok(),
reason: reason.clone(),
..RoomMemberEventContent::new(MembershipState::Knock)
};
// Try normal knock first
let Err(error) = services
.timeline
.build_and_append_pdu(
PduBuilder::state(sender_user.to_string(), &content),
sender_user,
room_id,
&state_lock,
)
.await
else {
return Ok(());
};
if servers.is_empty() || (servers.len() == 1 && services.globals.server_is_ours(&servers[0]))
{
return Err(error);
}
warn!("We couldn't do the knock locally, maybe federation can help to satisfy the knock");
let (make_knock_response, remote_server) =
make_knock_request(services, sender_user, room_id, servers).await?;
info!("make_knock finished");
let room_version_id = make_knock_response.room_version;
if !services
.server
.supported_room_version(&room_version_id)
{
return Err!(BadServerResponse(
"Remote room version {room_version_id} is not supported by tuwunel"
));
}
let mut knock_event_stub = serde_json::from_str::<CanonicalJsonObject>(
make_knock_response.event.get(),
)
.map_err(|e| {
err!(BadServerResponse("Invalid make_knock event json received from server: {e:?}"))
})?;
knock_event_stub.insert(
"origin".to_owned(),
CanonicalJsonValue::String(services.globals.server_name().as_str().to_owned()),
);
knock_event_stub.insert(
"origin_server_ts".to_owned(),
CanonicalJsonValue::Integer(
utils::millis_since_unix_epoch()
.try_into()
.expect("Timestamp is valid js_int value"),
),
);
knock_event_stub.insert(
"content".to_owned(),
to_canonical_value(RoomMemberEventContent {
displayname: services.users.displayname(sender_user).await.ok(),
avatar_url: services.users.avatar_url(sender_user).await.ok(),
blurhash: services.users.blurhash(sender_user).await.ok(),
reason,
..RoomMemberEventContent::new(MembershipState::Knock)
})
.expect("event is valid, we just created it"),
);
// In order to create a compatible ref hash (EventID) the `hashes` field needs
// to be present
services
.server_keys
.hash_and_sign_event(&mut knock_event_stub, &room_version_id)?;
// Generate event id
let event_id = gen_event_id(&knock_event_stub, &room_version_id)?;
// Add event_id
knock_event_stub
.insert("event_id".to_owned(), CanonicalJsonValue::String(event_id.clone().into()));
// It has enough fields to be called a proper event now
let knock_event = knock_event_stub;
info!("Asking {remote_server} for send_knock in room {room_id}");
let send_knock_request = federation::membership::create_knock_event::v1::Request {
room_id: room_id.to_owned(),
event_id: event_id.clone(),
pdu: services
.federation
.format_pdu_into(knock_event.clone(), Some(&room_version_id))
.await,
};
let send_knock_response = services
.sending
.send_federation_request(&remote_server, send_knock_request)
banned_room_check(&services, sender_user, &room_id, Some(&body.room_id_or_alias), client)
.await?;
info!("send_knock finished");
let state_lock = services.state.mutex.lock(&room_id).await;
services
.short
.get_or_create_shortroomid(room_id)
.await;
info!("Parsing knock event");
let parsed_knock_pdu = PduEvent::from_id_val(&event_id, knock_event.clone())
.map_err(|e| err!(BadServerResponse("Invalid knock event PDU: {e:?}")))?;
info!("Updating membership locally to knock state with provided stripped state events");
let count = services.globals.next_count();
services
.state_cache
.update_membership(
room_id,
.membership
.knock(
sender_user,
parsed_knock_pdu
.get_content::<RoomMemberEventContent>()
.expect("we just created this"),
sender_user,
Some(
send_knock_response
.knock_room_state
.into_iter()
.filter_map(|s| extract_variant!(s, RawStrippedState::Stripped))
.collect(),
),
None,
false,
PduCount::Normal(*count),
)
.await?;
info!("Appending room knock event locally");
services
.timeline
.append_pdu(
&parsed_knock_pdu,
knock_event,
once(parsed_knock_pdu.event_id.borrow()),
&room_id,
Some(&body.room_id_or_alias),
body.reason.clone(),
&servers,
&state_lock,
)
.await?;
Ok(())
}
drop(state_lock);
async fn knock_room_helper_remote(
services: &Services,
sender_user: &UserId,
room_id: &RoomId,
reason: Option<String>,
servers: &[OwnedServerName],
state_lock: RoomMutexGuard,
) -> Result {
info!("Knocking {room_id} over federation.");
let (make_knock_response, remote_server) =
make_knock_request(services, sender_user, room_id, servers).await?;
info!("make_knock finished");
let room_version_id = make_knock_response.room_version;
if !services
.server
.supported_room_version(&room_version_id)
{
return Err!(BadServerResponse(
"Remote room version {room_version_id} is not supported by tuwunel"
));
}
let mut knock_event_stub: CanonicalJsonObject =
serde_json::from_str(make_knock_response.event.get()).map_err(|e| {
err!(BadServerResponse("Invalid make_knock event json received from server: {e:?}"))
})?;
knock_event_stub.insert(
"origin".to_owned(),
CanonicalJsonValue::String(services.globals.server_name().as_str().to_owned()),
);
knock_event_stub.insert(
"origin_server_ts".to_owned(),
CanonicalJsonValue::Integer(
utils::millis_since_unix_epoch()
.try_into()
.expect("Timestamp is valid js_int value"),
),
);
knock_event_stub.insert(
"content".to_owned(),
to_canonical_value(RoomMemberEventContent {
displayname: services.users.displayname(sender_user).await.ok(),
avatar_url: services.users.avatar_url(sender_user).await.ok(),
blurhash: services.users.blurhash(sender_user).await.ok(),
reason,
..RoomMemberEventContent::new(MembershipState::Knock)
})
.expect("event is valid, we just created it"),
);
// In order to create a compatible ref hash (EventID) the `hashes` field needs
// to be present
services
.server_keys
.hash_and_sign_event(&mut knock_event_stub, &room_version_id)?;
// Generate event id
let event_id = gen_event_id(&knock_event_stub, &room_version_id)?;
// Add event_id
knock_event_stub
.insert("event_id".to_owned(), CanonicalJsonValue::String(event_id.clone().into()));
// It has enough fields to be called a proper event now
let knock_event = knock_event_stub;
info!("Asking {remote_server} for send_knock in room {room_id}");
let send_knock_request = federation::membership::create_knock_event::v1::Request {
room_id: room_id.to_owned(),
event_id: event_id.clone(),
pdu: services
.federation
.format_pdu_into(knock_event.clone(), Some(&room_version_id))
.await,
};
let send_knock_response = services
.sending
.send_federation_request(&remote_server, send_knock_request)
.await?;
info!("send_knock finished");
services
.short
.get_or_create_shortroomid(room_id)
.await;
info!("Parsing knock event");
let parsed_knock_pdu = PduEvent::from_id_val(&event_id, knock_event.clone())
.map_err(|e| err!(BadServerResponse("Invalid knock event PDU: {e:?}")))?;
info!("Going through send_knock response knock state events");
let state = send_knock_response
.knock_room_state
.iter()
.map(|event| {
serde_json::from_str::<CanonicalJsonObject>(
extract_variant!(event.clone(), RawStrippedState::Stripped)
.expect("Raw<AnyStrippedStateEvent>")
.json()
.get(),
)
})
.filter_map(Result::ok);
let mut state_map: HashMap<u64, OwnedEventId> = HashMap::new();
for event in state {
let Some(state_key) = event.get("state_key") else {
debug_warn!("send_knock stripped state event missing state_key: {event:?}");
continue;
};
let Some(event_type) = event.get("type") else {
debug_warn!("send_knock stripped state event missing event type: {event:?}");
continue;
};
let Ok(state_key) = serde_json::from_value::<String>(state_key.clone().into()) else {
debug_warn!("send_knock stripped state event has invalid state_key: {event:?}");
continue;
};
let Ok(event_type) = serde_json::from_value::<StateEventType>(event_type.clone().into())
else {
debug_warn!("send_knock stripped state event has invalid event type: {event:?}");
continue;
};
let event_id = gen_event_id(&event, &room_version_id)?;
let shortstatekey = services
.short
.get_or_create_shortstatekey(&event_type, &state_key)
.await;
services
.timeline
.add_pdu_outlier(&event_id, &event);
state_map.insert(shortstatekey, event_id.clone());
}
info!("Compressing state from send_knock");
let compressed: CompressedState = services
.state_compressor
.compress_state_events(
state_map
.iter()
.map(|(ssk, eid)| (ssk, eid.borrow())),
)
.collect()
.await;
debug!("Saving compressed state");
let HashSetCompressStateEvent {
shortstatehash: statehash_before_knock,
added,
removed,
} = services
.state_compressor
.save_state(room_id, Arc::new(compressed))
.await?;
debug!("Forcing state for new room");
services
.state
.force_state(room_id, statehash_before_knock, added, removed, &state_lock)
.await?;
let statehash_after_knock = services
.state
.append_to_state(&parsed_knock_pdu)
.await?;
info!("Updating membership locally to knock state with provided stripped state events");
let count = services.globals.next_count();
services
.state_cache
.update_membership(
room_id,
sender_user,
parsed_knock_pdu
.get_content::<RoomMemberEventContent>()
.expect("we just created this"),
sender_user,
Some(
send_knock_response
.knock_room_state
.into_iter()
.filter_map(|s| extract_variant!(s, RawStrippedState::Stripped))
.collect(),
),
None,
false,
PduCount::Normal(*count),
)
.await?;
info!("Appending room knock event locally");
services
.timeline
.append_pdu(
&parsed_knock_pdu,
knock_event,
once(parsed_knock_pdu.event_id.borrow()),
&state_lock,
)
.await?;
info!("Setting final room state for new room");
// We set the room state after inserting the pdu, so that we never have a moment
// in time where events in the current room state do not exist
services
.state
.set_room_state(room_id, statehash_after_knock, &state_lock);
Ok(())
}
async fn make_knock_request(
services: &Services,
sender_user: &UserId,
room_id: &RoomId,
servers: &[OwnedServerName],
) -> Result<(federation::membership::prepare_knock_event::v1::Response, OwnedServerName)> {
let mut make_knock_response_and_server =
Err!(BadServerResponse("No server available to assist in knocking."));
let mut make_knock_counter: usize = 0;
for remote_server in servers {
if services.globals.server_is_ours(remote_server) {
continue;
}
info!("Asking {remote_server} for make_knock ({make_knock_counter})");
let make_knock_response = services
.sending
.send_federation_request(
remote_server,
federation::membership::prepare_knock_event::v1::Request {
room_id: room_id.to_owned(),
user_id: sender_user.to_owned(),
ver: services
.server
.supported_room_versions()
.collect(),
},
)
.await;
trace!("make_knock response: {make_knock_response:?}");
make_knock_counter = make_knock_counter.saturating_add(1);
make_knock_response_and_server = make_knock_response.map(|r| (r, remote_server.clone()));
if make_knock_response_and_server.is_ok() {
break;
}
if make_knock_counter > 40 {
warn!(
"50 servers failed to provide valid make_knock response, assuming no server can \
assist in knocking."
);
make_knock_response_and_server =
Err!(BadServerResponse("No server available to assist in knocking."));
return make_knock_response_and_server;
}
}
make_knock_response_and_server
Ok(knock_room::v3::Response::new(room_id.clone()))
}
+38 -142
View File
@@ -8,15 +8,12 @@ mod leave;
mod members;
mod unban;
use std::{cmp::Ordering, net::IpAddr};
use std::net::IpAddr;
use axum::extract::State;
use futures::{FutureExt, StreamExt};
use ruma::{
OwnedRoomId, OwnedServerName, RoomId, RoomOrAliasId, ServerName, UserId,
api::client::membership::joined_rooms,
};
use tuwunel_core::{Err, Result, result::LogErr, utils::shuffle, warn};
use ruma::{RoomId, RoomOrAliasId, UserId, api::client::membership::joined_rooms};
use tuwunel_core::{Err, Result, result::LogErr, warn};
use tuwunel_service::Services;
pub(crate) use self::{
@@ -58,57 +55,42 @@ pub(crate) async fn joined_rooms_route(
pub(crate) async fn banned_room_check(
services: &Services,
user_id: &UserId,
room_id: Option<&RoomId>,
server_name: Option<&ServerName>,
room_id: &RoomId,
orig_room_id: Option<&RoomOrAliasId>,
client_ip: IpAddr,
) -> Result {
if services.users.is_admin(user_id).await {
return Ok(());
}
// TODO: weird condition
if let Some(room_id) = room_id {
if services.metadata.is_banned(room_id).await
|| (room_id.server_name().is_some()
&& services
.config
.forbidden_remote_server_names
.is_match(
room_id
.server_name()
.expect("legacy room mxid")
.host(),
)) {
warn!(
"User {user_id} who is not an admin attempted to send an invite for or \
attempted to join a banned room or banned room server name: {room_id}"
);
// room id is banned ...
if services.metadata.is_banned(room_id).await
// ... or legacy room id server is banned ...
|| room_id.server_name().is_some_and(|server_name| {
services
.config
.forbidden_remote_server_names
.is_match(server_name.host())
})
// ... or alias server is banned
|| orig_room_id.is_some_and(|orig_room_id| {
orig_room_id.server_name().is_some_and(|orig_server_name|
services
.config
.forbidden_remote_server_names
.is_match(orig_server_name.host()))
}) {
warn!(
"User {user_id} who is not an admin attempted to send an invite for or attempted to \
join a banned room or banned room server name: {room_id}"
);
maybe_deactivate(services, user_id, client_ip)
.await
.log_err()
.ok();
maybe_deactivate(services, user_id, client_ip)
.await
.log_err()
.ok();
return Err!(Request(Forbidden("This room is banned on this homeserver.")));
}
} else if let Some(server_name) = server_name {
if services
.config
.forbidden_remote_server_names
.is_match(server_name.host())
{
warn!(
"User {user_id} who is not an admin tried joining a room which has the server \
name {server_name} that is globally forbidden. Rejecting.",
);
maybe_deactivate(services, user_id, client_ip)
.await
.log_err()
.ok();
return Err!(Request(Forbidden("This remote server is banned on this homeserver.")));
}
return Err!(Request(Forbidden("This room is banned on this homeserver.")));
}
Ok(())
@@ -120,16 +102,15 @@ async fn maybe_deactivate(services: &Services, user_id: &UserId, client_ip: IpAd
.config
.auto_deactivate_banned_room_attempts
{
warn!("Automatically deactivating user {user_id} due to attempted banned room join");
let notice = format!(
"Automatically deactivating user {user_id} due to attempted banned room join from \
IP {client_ip}"
);
warn!("{notice}");
if services.server.config.admin_room_notices {
services
.admin
.send_text(&format!(
"Automatically deactivating user {user_id} due to attempted banned room \
join from IP {client_ip}"
))
.await;
services.admin.send_text(&notice).await;
}
services
@@ -141,88 +122,3 @@ async fn maybe_deactivate(services: &Services, user_id: &UserId, client_ip: IpAd
Ok(())
}
// TODO: should this be in services? banned check would have to resolve again if
// room_id is not available at callsite
async fn get_join_params(
services: &Services,
user_id: &UserId,
room_id_or_alias: &RoomOrAliasId,
via: &[OwnedServerName],
) -> Result<(OwnedRoomId, Vec<OwnedServerName>)> {
// servers tried first, additional_servers shuffled then tried after
let (room_id, mut servers, mut additional_servers) =
match OwnedRoomId::try_from(room_id_or_alias.to_owned()) {
// if room id, shuffle via + room_id server_name ...
| Ok(room_id) => {
let mut additional_servers = via.to_vec();
if let Some(server) = room_id.server_name() {
additional_servers.push(server.to_owned());
}
(room_id, Vec::new(), additional_servers)
},
// ... if room alias, resolve and don't shuffle ...
| Err(room_alias) => {
let (room_id, servers) = services.alias.resolve_alias(&room_alias).await?;
(room_id, servers, Vec::new())
},
};
// either way, add invited vias
additional_servers.extend(
services
.state_cache
.servers_invite_via(&room_id)
.map(ToOwned::to_owned)
.collect::<Vec<_>>()
.await,
);
// either way, add invite senders' servers
additional_servers.extend(
services
.state_cache
.invite_state(user_id, &room_id)
.await
.unwrap_or_default()
.iter()
.filter_map(|event| event.get_field("sender").ok().flatten())
.filter_map(|sender: &str| UserId::parse(sender).ok())
.map(|user| user.server_name().to_owned()),
);
// shuffle additionals, append to base servers
additional_servers.sort_unstable();
additional_servers.dedup();
shuffle(&mut additional_servers);
servers.sort_unstable();
servers.dedup();
servers.append(&mut additional_servers);
// sort deprioritized servers last
servers.sort_by(|a, b| {
let a_matches = services
.server
.config
.deprioritize_joins_through_servers
.is_match(a.host());
let b_matches = services
.server
.config
.deprioritize_joins_through_servers
.is_match(b.host());
if a_matches && !b_matches {
Ordering::Greater
} else if !a_matches && b_matches {
Ordering::Less
} else {
Ordering::Equal
}
});
Ok((room_id, servers))
}
+17 -12
View File
@@ -1,5 +1,9 @@
use axum::extract::State;
use futures::{FutureExt, StreamExt, TryFutureExt, future::OptionFuture, pin_mut};
use futures::{
FutureExt, StreamExt, TryFutureExt,
future::{Either, OptionFuture},
pin_mut,
};
use ruma::{
RoomId, UserId,
api::{
@@ -105,17 +109,18 @@ pub(crate) async fn get_message_events_route(
}
let it = match body.dir {
| Direction::Forward => services
.timeline
.pdus(Some(sender_user), room_id, Some(from))
.ignore_err()
.boxed(),
| Direction::Backward => services
.timeline
.pdus_rev(Some(sender_user), room_id, Some(from))
.ignore_err()
.boxed(),
| Direction::Forward => Either::Left(
services
.timeline
.pdus(Some(sender_user), room_id, Some(from))
.ignore_err(),
),
| Direction::Backward => Either::Right(
services
.timeline
.pdus_rev(Some(sender_user), room_id, Some(from))
.ignore_err(),
),
};
let events: Vec<_> = it
+4 -3
View File
@@ -5,8 +5,10 @@ pub(super) mod appservice;
pub(super) mod backup;
pub(super) mod capabilities;
pub(super) mod context;
pub(super) mod dehydrated_device;
pub(super) mod device;
pub(super) mod directory;
pub(super) mod events;
pub(super) mod filter;
pub(super) mod keys;
pub(super) mod media;
@@ -49,8 +51,10 @@ pub(super) use appservice::*;
pub(super) use backup::*;
pub(super) use capabilities::*;
pub(super) use context::*;
pub(super) use dehydrated_device::*;
pub(super) use device::*;
pub(super) use directory::*;
pub(super) use events::*;
pub(super) use filter::*;
pub(super) use keys::*;
pub(super) use media::*;
@@ -84,9 +88,6 @@ pub(super) use user_directory::*;
pub(super) use voip::*;
pub(super) use well_known::*;
/// generated device ID length
const DEVICE_ID_LENGTH: usize = 10;
/// generated user access token length
const TOKEN_LENGTH: usize = tuwunel_service::users::device::TOKEN_LENGTH;
+8 -8
View File
@@ -49,7 +49,7 @@ pub(crate) async fn set_displayname_route(
// Presence update
services
.presence
.maybe_ping_presence(&body.user_id, &PresenceState::Online)
.maybe_ping_presence(&body.user_id, body.sender_device.as_deref(), &PresenceState::Online)
.await?;
Ok(set_display_name::v3::Response {})
@@ -68,8 +68,8 @@ pub(crate) async fn get_displayname_route(
if !services.globals.user_is_local(&body.user_id) {
// Create and update our local copy of the user
if let Ok(response) = services
.sending
.send_federation_request(
.federation
.execute(
body.user_id.server_name(),
federation::query::get_profile_information::v1::Request {
user_id: body.user_id.clone(),
@@ -149,7 +149,7 @@ pub(crate) async fn set_avatar_url_route(
// Presence update
services
.presence
.maybe_ping_presence(&body.user_id, &PresenceState::Online)
.maybe_ping_presence(&body.user_id, body.sender_device.as_deref(), &PresenceState::Online)
.await
.ok();
@@ -169,8 +169,8 @@ pub(crate) async fn get_avatar_url_route(
if !services.globals.user_is_local(&body.user_id) {
// Create and update our local copy of the user
if let Ok(response) = services
.sending
.send_federation_request(
.federation
.execute(
body.user_id.server_name(),
federation::query::get_profile_information::v1::Request {
user_id: body.user_id.clone(),
@@ -231,8 +231,8 @@ pub(crate) async fn get_profile_route(
if !services.globals.user_is_local(&body.user_id) {
// Create and update our local copy of the user
if let Ok(response) = services
.sending
.send_federation_request(
.federation
.execute(
body.user_id.server_name(),
federation::query::get_profile_information::v1::Request {
user_id: body.user_id.clone(),
+109 -6
View File
@@ -1,10 +1,11 @@
use axum::extract::State;
use futures::StreamExt;
use ruma::{
CanonicalJsonObject, CanonicalJsonValue,
CanonicalJsonObject, CanonicalJsonValue, MilliSecondsSinceUnixEpoch,
api::client::{
error::ErrorKind,
push::{
delete_pushrule, get_pushers, get_pushrule, get_pushrule_actions,
delete_pushrule, get_notifications, get_pushers, get_pushrule, get_pushrule_actions,
get_pushrule_enabled, get_pushrules_all, get_pushrules_global_scope, set_pusher,
set_pushrule, set_pushrule_actions, set_pushrule_enabled,
},
@@ -14,15 +15,117 @@ use ruma::{
push_rules::{PushRulesEvent, PushRulesEventContent},
},
push::{
InsertPushRuleError, PredefinedContentRuleId, PredefinedOverrideRuleId,
Action, InsertPushRuleError, PredefinedContentRuleId, PredefinedOverrideRuleId,
RemovePushRuleError, Ruleset,
},
};
use tuwunel_core::{Err, Error, Result, err};
use tuwunel_core::{
Err, Error, Result, at, err,
matrix::{Event, PduId},
utils::{
stream::{ReadyExt, WidebandExt},
string::to_small_string,
},
};
use tuwunel_service::Services;
use crate::Ruma;
/// # `GET /_matrix/client/r0/notifications/`
///
/// Paginate through the list of events the user has been, or would have been
/// notified about.
pub(crate) async fn get_notifications_route(
State(services): State<crate::State>,
body: Ruma<get_notifications::v3::Request>,
) -> Result<get_notifications::v3::Response> {
use get_notifications::v3::Notification;
let sender_user = body.sender_user();
let from = body
.body
.from
.as_deref()
.map(str::parse)
.transpose()
.map_err(|e| err!(Request(InvalidParam("Invalid `from' parameter: {e}"))))?;
let limit: usize = body
.body
.limit
.map(TryInto::try_into)
.transpose()?
.unwrap_or(50)
.clamp(1, 100);
let only_highlight = body
.body
.only
.as_deref()
.is_some_and(|only| only.contains("highlight"));
let mut next_token: Option<u64> = None;
let notifications = services
.pusher
.get_notifications(sender_user, from)
.ready_filter(|(_, notify)| {
if only_highlight && !notify.actions.iter().any(Action::is_highlight) {
return false;
}
true
})
.wide_filter_map(async |(count, notify)| {
let pdu_id = PduId {
shortroomid: notify.sroomid,
count: count.into(),
};
let event = services
.timeline
.get_pdu_from_id(&pdu_id.into())
.await
.ok()
.filter(|event| !event.is_redacted())?;
let read = services
.pusher
.last_notification_read(sender_user, event.room_id())
.await
.is_ok_and(|last_read| last_read.ge(&count));
let ts = notify
.ts
.try_into()
.map(MilliSecondsSinceUnixEpoch)
.ok()?;
let notification = Notification {
room_id: event.room_id().into(),
event: event.into_format(),
ts,
read,
profile_tag: notify.tag,
actions: notify.actions,
};
Some((count, notification))
})
.take(limit)
.inspect(|(count, _)| {
next_token.replace(*count);
})
.map(at!(1))
.collect::<Vec<_>>()
.await;
Ok(get_notifications::v3::Response {
next_token: next_token.map(to_small_string),
notifications,
})
}
/// # `GET /_matrix/client/r0/pushrules/`
///
/// Retrieves the push rules event for this user.
@@ -335,7 +438,7 @@ pub(crate) async fn set_pushrule_actions_route(
if account_data
.content
.global
.set_actions(body.kind.clone(), &body.rule_id, body.actions.clone())
.set_actions(body.kind.clone(), &body.rule_id, body.actions.clone().into())
.is_err()
{
return Err!(Request(NotFound("Push rule not found.")));
@@ -486,7 +589,7 @@ pub(crate) async fn set_pushers_route(
services
.pusher
.set_pusher(sender_user, body.sender_device(), &body.action)
.set_pusher(sender_user, body.sender_device()?, &body.action)
.await?;
Ok(set_pusher::v3::Response::new())
+12 -4
View File
@@ -28,7 +28,7 @@ pub(crate) async fn set_read_marker_route(
if body.private_read_receipt.is_some() || body.read_receipt.is_some() {
services
.user
.pusher
.reset_notification_counts(sender_user, &body.room_id);
}
@@ -93,7 +93,11 @@ pub(crate) async fn set_read_marker_route(
services
.presence
.maybe_ping_presence(sender_user, &ruma::presence::PresenceState::Online)
.maybe_ping_presence(
sender_user,
body.sender_device.as_deref(),
&ruma::presence::PresenceState::Online,
)
.await
.ok();
}
@@ -115,7 +119,7 @@ pub(crate) async fn create_receipt_route(
create_receipt::v3::ReceiptType::Read | create_receipt::v3::ReceiptType::ReadPrivate
) {
services
.user
.pusher
.reset_notification_counts(sender_user, &body.room_id);
}
@@ -165,7 +169,11 @@ pub(crate) async fn create_receipt_route(
services
.presence
.maybe_ping_presence(sender_user, &ruma::presence::PresenceState::Online)
.maybe_ping_presence(
sender_user,
body.sender_device.as_deref(),
&ruma::presence::PresenceState::Online,
)
.await
.ok();
},
+58 -240
View File
@@ -2,7 +2,6 @@ use std::fmt::Write;
use axum::extract::State;
use axum_client_ip::InsecureClientIp;
use futures::FutureExt;
use register::RegistrationKind;
use ruma::{
UserId,
@@ -13,13 +12,11 @@ use ruma::{
},
uiaa::{AuthFlow, AuthType, UiaaInfo},
},
events::GlobalAccountDataEventType,
push,
};
use tuwunel_core::{Err, Error, Result, debug_info, error, info, is_equal_to, utils, warn};
use tuwunel_core::{Err, Error, Result, debug_info, debug_warn, info, utils};
use tuwunel_service::users::device::generate_refresh_token;
use super::{DEVICE_ID_LENGTH, SESSION_ID_LENGTH};
use super::SESSION_ID_LENGTH;
use crate::Ruma;
const RANDOM_USER_ID_LENGTH: usize = 10;
@@ -46,15 +43,10 @@ pub(crate) async fn get_register_available_route(
.appservice_info
.as_ref()
.is_some_and(|appservice| {
appservice.registration.id == "irc"
|| appservice
.registration
.id
.contains("matrix-appservice-irc")
|| appservice
.registration
.id
.contains("matrix_appservice_irc")
let id = &appservice.registration.id;
id == "irc"
|| id.contains("matrix-appservice-irc")
|| id.contains("matrix_appservice_irc")
});
if services
@@ -146,67 +138,30 @@ pub(crate) async fn register_route(
let is_guest = body.kind == RegistrationKind::Guest;
let emergency_mode_enabled = services.config.emergency_password.is_some();
let user = body.username.as_deref().unwrap_or("");
let device_name = body
.initial_device_display_name
.as_deref()
.unwrap_or("");
if !services.config.allow_registration && body.appservice_info.is_none() {
match (body.username.as_ref(), body.initial_device_display_name.as_ref()) {
| (Some(username), Some(device_display_name)) => {
info!(
%is_guest,
user = %username,
device_name = %device_display_name,
"Rejecting registration attempt as registration is disabled"
);
},
| (Some(username), _) => {
info!(
%is_guest,
user = %username,
"Rejecting registration attempt as registration is disabled"
);
},
| (_, Some(device_display_name)) => {
info!(
%is_guest,
device_name = %device_display_name,
"Rejecting registration attempt as registration is disabled"
);
},
| (None, _) => {
info!(
%is_guest,
"Rejecting registration attempt as registration is disabled"
);
},
}
info!(
%is_guest,
%user,
%device_name,
"Rejecting registration attempt as registration is disabled"
);
return Err!(Request(Forbidden("Registration has been disabled.")));
}
if is_guest
&& (!services.config.allow_guest_registration
|| (services.config.allow_registration
&& services.globals.registration_token.is_some()))
{
info!(
"Guest registration disabled / registration enabled with token configured, \
rejecting guest registration attempt, initial device name: \"{}\"",
body.initial_device_display_name
.as_deref()
.unwrap_or("")
if is_guest && !services.config.allow_guest_registration {
debug_warn!(
%device_name,
"Guest registration disabled, rejecting guest registration attempt"
);
return Err!(Request(GuestAccessForbidden("Guest registration is disabled.")));
}
// forbid guests from registering if there is not a real admin user yet. give
// generic user error.
if is_guest && services.users.count().await < 2 {
warn!(
"Guest account attempted to register before a real admin user has been registered, \
rejecting registration. Guest's initial device name: \"{}\"",
body.initial_device_display_name
.as_deref()
.unwrap_or("")
);
return Err!(Request(Forbidden("Registration is temporarily disabled.")));
return Err!(Request(GuestAccessForbidden("Guest registration is disabled.")));
}
let user_id = match (body.username.as_ref(), is_guest) {
@@ -309,7 +264,13 @@ pub(crate) async fn register_route(
// UIAA
let mut uiaainfo;
let skip_auth = if services.globals.registration_token.is_some() {
let skip_auth = if !services
.globals
.get_registration_tokens()
.await
.is_empty()
&& !is_guest
{
// Registration token required
uiaainfo = UiaaInfo {
flows: vec![AuthFlow {
@@ -320,6 +281,7 @@ pub(crate) async fn register_route(
session: None,
auth_error: None,
};
body.appservice_info.is_some()
} else {
// No registration token necessary, but clients must still go through the flow
@@ -330,6 +292,7 @@ pub(crate) async fn register_route(
session: None,
auth_error: None,
};
body.appservice_info.is_some() || is_guest
};
@@ -372,45 +335,9 @@ pub(crate) async fn register_route(
let password = if is_guest { None } else { body.password.as_deref() };
// Create user
services
.users
.create(&user_id, password, None)
.await?;
// Default to pretty displayname
let mut displayname = user_id.localpart().to_owned();
// If `new_user_displayname_suffix` is set, registration will push whatever
// content is set to the user's display name with a space before it
if !services
.config
.new_user_displayname_suffix
.is_empty()
&& body.appservice_info.is_none()
{
write!(displayname, " {}", services.server.config.new_user_displayname_suffix)?;
}
services
.users
.set_displayname(&user_id, Some(displayname.clone()));
// Initial account data
services
.account_data
.update(
None,
&user_id,
GlobalAccountDataEventType::PushRules
.to_string()
.into(),
&serde_json::to_value(ruma::events::push_rules::PushRulesEvent {
content: ruma::events::push_rules::PushRulesEventContent {
global: push::Ruleset::server_default(&user_id),
},
})?,
)
.full_register(&user_id, password, None, body.appservice_info.as_ref(), is_guest, true)
.await?;
if (!is_guest && body.inhibit_login)
@@ -420,169 +347,56 @@ pub(crate) async fn register_route(
.is_some_and(|appservice| appservice.registration.device_management)
{
return Ok(register::v3::Response {
access_token: None,
user_id,
device_id: None,
access_token: None,
refresh_token: None,
expires_in: None,
});
}
// Generate new device id if the user didn't specify one
let device_id = if is_guest { None } else { body.device_id.clone() }
.unwrap_or_else(|| utils::random_string(DEVICE_ID_LENGTH).into());
let device_id = if is_guest { None } else { body.device_id.as_deref() };
// Generate new token for the device
let (access_token, expires_in) = services
.users
.generate_access_token(body.body.refresh_token);
.generate_access_token(body.refresh_token);
// Generate a new refresh_token if requested by client
let refresh_token = expires_in.is_some().then(generate_refresh_token);
// Create device for this account
services
let device_id = services
.users
.create_device(
&user_id,
&device_id,
(&access_token, expires_in),
device_id,
(Some(&access_token), expires_in),
refresh_token.as_deref(),
body.initial_device_display_name.clone(),
body.initial_device_display_name.as_deref(),
Some(client.to_string()),
)
.await?;
debug_info!(%user_id, %device_id, "User account was created");
let device_display_name = body
.initial_device_display_name
.as_deref()
.unwrap_or("");
if body.appservice_info.is_none() && (!is_guest || services.config.log_guest_registrations) {
let mut notice = String::from(if is_guest { "New guest user" } else { "New user" });
// log in conduit admin channel if a non-guest user registered
if body.appservice_info.is_none() && !is_guest {
if !device_display_name.is_empty() {
let notice = format!(
"New user \"{user_id}\" registered on this server from IP {client} and device \
display name \"{device_display_name}\""
);
write!(notice, " registered on this server from IP {client}")?;
info!("{notice}");
if services.server.config.admin_room_notices {
services.admin.notice(&notice).await;
}
} else {
let notice = format!("New user \"{user_id}\" registered on this server.");
info!("{notice}");
if services.server.config.admin_room_notices {
services.admin.notice(&notice).await;
}
if let Some(device_name) = body.initial_device_display_name.as_deref() {
write!(notice, " with device name {device_name}")?;
}
}
// log in conduit admin channel if a guest registered
if body.appservice_info.is_none() && is_guest && services.config.log_guest_registrations {
debug_info!("New guest user \"{user_id}\" registered on this server.");
if !device_display_name.is_empty() {
if services.server.config.admin_room_notices {
services
.admin
.notice(&format!(
"Guest user \"{user_id}\" with device display name \
\"{device_display_name}\" registered on this server from IP {client}"
))
.await;
}
if !is_guest {
info!("{notice}");
} else {
#[allow(clippy::collapsible_else_if)]
if services.server.config.admin_room_notices {
services
.admin
.notice(&format!(
"Guest user \"{user_id}\" with no device display name registered on \
this server from IP {client}",
))
.await;
}
debug_info!("{notice}");
}
}
// If this is the first real user, grant them admin privileges except for guest
// users
// Note: the server user is generated first
if !is_guest
&& services.config.grant_admin_to_first_user
&& let Ok(admin_room) = services.admin.get_admin_room().await
&& services
.state_cache
.room_joined_count(&admin_room)
.await
.is_ok_and(is_equal_to!(1))
{
services
.admin
.make_user_admin(&user_id)
.boxed()
.await?;
warn!("Granting {user_id} admin privileges as the first user");
}
if body.appservice_info.is_none()
&& !services.server.config.auto_join_rooms.is_empty()
&& (services.config.allow_guests_auto_join_rooms || !is_guest)
{
for room in &services.server.config.auto_join_rooms {
let Ok(room_id) = services.alias.maybe_resolve(room).await else {
error!(
"Failed to resolve room alias to room ID when attempting to auto join \
{room}, skipping"
);
continue;
};
if !services
.state_cache
.server_in_room(services.globals.server_name(), &room_id)
.await
{
warn!(
"Skipping room {room} to automatically join as we have never joined before."
);
continue;
}
if let Some(room_server_name) = room.server_name() {
let state_lock = services.state.mutex.lock(&room_id).await;
match services
.membership
.join(
&user_id,
&room_id,
Some("Automatically joining this room upon registration".to_owned()),
&[services.globals.server_name().to_owned(), room_server_name.to_owned()],
&body.appservice_info,
&state_lock,
)
.boxed()
.await
{
| Err(e) => {
// don't return this error so we don't fail registrations
error!(
"Failed to automatically join room {room} for user {user_id}: {e}"
);
},
| _ => {
info!("Automatically joined room {room} for user {user_id}");
},
}
drop(state_lock);
}
if services.server.config.admin_room_notices {
services.admin.notice(&notice).await;
}
}
@@ -605,9 +419,13 @@ pub(crate) async fn check_registration_token_validity(
State(services): State<crate::State>,
body: Ruma<check_registration_token_validity::v1::Request>,
) -> Result<check_registration_token_validity::v1::Response> {
let Some(reg_token) = services.globals.registration_token.clone() else {
return Err!(Request(Forbidden("Server does not allow token registration")));
};
let tokens = services.globals.get_registration_tokens().await;
Ok(check_registration_token_validity::v1::Response { valid: reg_token == body.token })
if tokens.is_empty() {
return Err!(Request(Forbidden("Server does not allow token registration")));
}
let valid = tokens.contains(&body.token);
Ok(check_registration_token_validity::v1::Response { valid })
}
+112 -59
View File
@@ -1,5 +1,11 @@
use std::iter::once;
use axum::extract::State;
use futures::StreamExt;
use futures::{
FutureExt, StreamExt, TryFutureExt,
future::try_join3,
stream::{select_all, unfold},
};
use ruma::{
EventId, RoomId, UInt, UserId,
api::{
@@ -12,12 +18,16 @@ use ruma::{
events::{TimelineEventType, relation::RelationType},
};
use tuwunel_core::{
Result, at,
Err, Error, Result, at, err,
matrix::{
event::{Event, RelationTypeEqual},
pdu::PduCount,
pdu::{PduCount, PduId},
},
utils::{
BoolExt,
result::FlatOk,
stream::{ReadyExt, WidebandExt},
},
utils::{IterStream, ReadyExt, result::FlatOk, stream::WidebandExt},
};
use tuwunel_service::Services;
@@ -99,6 +109,12 @@ pub(crate) async fn get_relating_events_route(
}
#[allow(clippy::too_many_arguments)]
#[tracing::instrument(
name = "relations",
level = "debug",
skip_all,
fields(room_id, target, from, to, dir, limit, recurse)
)]
async fn paginate_relations_with_filter(
services: &Services,
sender_user: &UserId,
@@ -112,78 +128,115 @@ async fn paginate_relations_with_filter(
recurse: bool,
dir: Direction,
) -> Result<get_relating_events::v1::Response> {
let start: PduCount = from
.map(str::parse)
.transpose()?
.unwrap_or_else(|| match dir {
| Direction::Forward => PduCount::min(),
| Direction::Backward => PduCount::max(),
});
let from: Option<PduCount> = from.map(str::parse).transpose()?;
let to: Option<PduCount> = to.map(str::parse).flat_ok();
// Use limit or else 30, with maximum 100
// Spec (v1.10) recommends depth of at least 3
let max_depth: usize = if recurse { 3 } else { 0 };
let limit: usize = limit
.map(TryInto::try_into)
.flat_ok()
.unwrap_or(30)
.min(100);
// Spec (v1.10) recommends depth of at least 3
let depth: u8 = if recurse { 3 } else { 1 };
let target = services
.timeline
.get_pdu_id(target)
.map_ok(PduId::from)
.map_ok(Ok::<_, Error>);
let events: Vec<_> = services
.pdu_metadata
.get_relations(sender_user, room_id, target, start, limit, depth, dir)
.await
.into_iter()
.filter(|(_, pdu)| {
filter_event_type
.as_ref()
.is_none_or(|kind| kind == pdu.kind())
})
.filter(|(_, pdu)| {
filter_rel_type
.as_ref()
.is_none_or(|rel_type| rel_type.relation_type_equal(pdu))
})
.stream()
.ready_take_while(|(count, _)| Some(*count) != to)
.wide_filter_map(|item| visibility_filter(services, sender_user, item))
.take(limit)
.collect()
.await;
let visible = services
.state_accessor
.user_can_see_state_events(sender_user, room_id)
.map(|visible| {
visible.ok_or_else(|| err!(Request(Forbidden("You cannot view this room."))))
});
let next_batch = match dir {
| Direction::Forward => events.last(),
| Direction::Backward => events.first(),
let shortroomid = services.short.get_shortroomid(room_id);
let (shortroomid, target, ()) = try_join3(shortroomid, target, visible).await?;
let Ok(target) = target else {
return Ok(get_relating_events::v1::Response::new(Vec::new()));
};
if shortroomid != target.shortroomid {
return Err!(Request(NotFound("Event not found in room.")));
}
.map(at!(0))
.as_ref()
.map(ToString::to_string);
if let PduCount::Backfilled(_) = target.count {
return Ok(get_relating_events::v1::Response::new(Vec::new()));
}
let fetch = |depth: usize, count: PduCount| {
services
.pdu_metadata
.get_relations(shortroomid, count, from, dir, Some(sender_user))
.map(move |(count, pdu)| (depth, count, pdu))
.ready_filter(|(_, count, _)| matches!(count, PduCount::Normal(_)))
.boxed()
};
let events = unfold(select_all(once(fetch(0, target.count))), async |mut relations| {
let (depth, count, pdu) = relations.next().await?;
if depth < max_depth {
relations.push(fetch(depth.saturating_add(1), count));
}
Some(((depth, count, pdu), relations))
})
.ready_take_while(|&(_, count, _)| Some(count) != to)
.ready_filter(|(_, _, pdu)| {
filter_event_type
.as_ref()
.is_none_or(|kind| kind == pdu.kind())
})
.ready_filter(|(_, _, pdu)| {
filter_rel_type
.as_ref()
.is_none_or(|rel_type| rel_type.relation_type_equal(pdu))
})
.wide_filter_map(async |(depth, count, pdu)| {
services
.state_accessor
.user_can_see_event(sender_user, pdu.room_id(), pdu.event_id())
.await
.then_some((depth, count, pdu))
})
.take(limit)
.collect::<Vec<_>>()
.await;
Ok(get_relating_events::v1::Response {
next_batch,
prev_batch: from.map(Into::into),
recursion_depth: recurse.then_some(depth.into()),
recursion_depth: max_depth
.gt(&0)
.then(|| events.iter().map(at!(0)))
.into_iter()
.flatten()
.max()
.map(TryInto::try_into)
.transpose()?,
next_batch: events
.last()
.map(at!(1))
.as_ref()
.map(ToString::to_string),
prev_batch: events
.first()
.map(at!(1))
.or(from)
.as_ref()
.map(ToString::to_string),
chunk: events
.into_iter()
.map(at!(1))
.map(at!(2))
.map(Event::into_format)
.collect(),
})
}
async fn visibility_filter<Pdu: Event>(
services: &Services,
sender_user: &UserId,
item: (PduCount, Pdu),
) -> Option<(PduCount, Pdu)> {
let (_, pdu) = &item;
services
.state_accessor
.user_can_see_event(sender_user, pdu.room_id(), pdu.event_id())
.await
.then_some(item)
}
+35 -1
View File
@@ -3,7 +3,8 @@ use std::collections::BTreeMap;
use axum::extract::State;
use futures::{FutureExt, future::OptionFuture};
use ruma::{
CanonicalJsonObject, Int, OwnedRoomAliasId, OwnedRoomId, OwnedUserId, RoomId, RoomVersionId,
CanonicalJsonObject, EventEncryptionAlgorithm, Int, OwnedRoomAliasId, OwnedRoomId,
OwnedUserId, RoomId, RoomVersionId,
api::client::room::{
self, create_room,
create_room::v3::{CreationContent, RoomPreset},
@@ -13,6 +14,7 @@ use ruma::{
room::{
canonical_alias::RoomCanonicalAliasEventContent,
create::RoomCreateEventContent,
encryption::RoomEncryptionEventContent,
guest_access::{GuestAccess, RoomGuestAccessEventContent},
history_visibility::{HistoryVisibility, RoomHistoryVisibilityEventContent},
join_rules::{JoinRule, RoomJoinRulesEventContent},
@@ -262,6 +264,7 @@ pub(crate) async fn create_room_route(
.await?;
// 6. Events listed in initial_state
let mut is_encrypted = false;
for event in &body.initial_state {
let mut pdu_builder = event
.deserialize_as_unchecked::<PduBuilder>()
@@ -292,6 +295,10 @@ pub(crate) async fn create_room_route(
continue;
}
if pdu_builder.event_type == TimelineEventType::RoomEncryption {
is_encrypted = true;
}
services
.timeline
.build_and_append_pdu(pdu_builder, sender_user, &room_id, &state_lock)
@@ -299,6 +306,33 @@ pub(crate) async fn create_room_route(
.await?;
}
if services.config.allow_encryption && !is_encrypted {
use RoomPreset::*;
let config = services
.config
.encryption_enabled_by_default_for_room_type
.as_deref()
.unwrap_or("off");
let invite = matches!(config, "invite");
let always = matches!(config, "all" | "invite");
if always || (invite && matches!(preset, PrivateChat | TrustedPrivateChat)) {
let algorithm = EventEncryptionAlgorithm::MegolmV1AesSha2;
let content = RoomEncryptionEventContent::new(algorithm);
services
.timeline
.build_and_append_pdu(
PduBuilder::state(String::new(), &content),
sender_user,
&room_id,
&state_lock,
)
.boxed()
.await?;
}
}
// 7. Events implied by name and topic
if let Some(name) = &body.name {
services
+53 -36
View File
@@ -1,15 +1,20 @@
use axum::extract::State;
use futures::{FutureExt, TryStreamExt, future::try_join4};
use ruma::api::client::room::initial_sync::v3::{PaginationChunk, Request, Response};
use futures::{FutureExt, StreamExt, TryFutureExt, TryStreamExt, future::try_join5};
use ruma::{
api::client::room::initial_sync::v3::{PaginationChunk, Request, Response},
events::AnyRawAccountDataEvent,
};
use tuwunel_core::{
Err, Event, Result, at,
utils::{BoolExt, stream::TryTools},
Err, Event, Result, at, extract_variant,
matrix::PduCount,
utils::stream::{ReadyExt, TryTools},
};
use crate::Ruma;
const LIMIT_MAX: usize = 100;
const LIMIT_MAX: usize = 50;
/// GET `/_matrix/client/v3/rooms/{roomId}/initialSync`
pub(crate) async fn room_initial_sync_route(
State(services): State<crate::State>,
body: Ruma<Request>,
@@ -24,13 +29,15 @@ pub(crate) async fn room_initial_sync_route(
return Err!(Request(Forbidden("No room preview available.")));
}
let next_batch = services.globals.current_count();
let visibility = services.directory.visibility(room_id).map(Ok);
let membership = services
.state_cache
.user_membership(body.sender_user(), room_id)
.map(Ok);
let visibility = services.directory.visibility(room_id).map(Ok);
let state = services
.state_accessor
.room_state_full_pdus(room_id)
@@ -40,42 +47,52 @@ pub(crate) async fn room_initial_sync_route(
let limit = LIMIT_MAX;
let events = services
.timeline
.pdus_rev(None, room_id, None)
.pdus_rev(None, room_id, Some(PduCount::Normal(next_batch).saturating_add(1)))
.try_take(limit)
.try_collect::<Vec<_>>();
.try_collect()
.map_ok(|mut vec: Vec<_>| {
vec.reverse();
vec
});
let (membership, visibility, state, events) =
try_join4(membership, visibility, state, events)
let account_data = services
.account_data
.changes_since(Some(room_id), body.sender_user(), 0, Some(next_batch))
.ready_filter_map(|e| extract_variant!(e, AnyRawAccountDataEvent::Room))
.collect::<Vec<_>>()
.map(Ok);
let (membership, visibility, state, events, account_data) =
try_join5(membership, visibility, state, events, account_data)
.boxed()
.await?;
let messages = PaginationChunk {
start: events
.last()
.map(at!(0))
.as_ref()
.map(ToString::to_string),
end: events
.first()
.map(at!(0))
.as_ref()
.map(ToString::to_string)
.unwrap_or_default(),
chunk: events
.into_iter()
.map(at!(1))
.map(Event::into_format)
.collect(),
};
Ok(Response {
room_id: room_id.to_owned(),
account_data: None,
state: state.into(),
messages: messages.chunk.is_empty().or_some(messages),
visibility: visibility.into(),
membership,
visibility: visibility.into(),
account_data: Some(account_data),
state: state.into(),
messages: PaginationChunk {
start: events
.first()
.map(at!(0))
.as_ref()
.map(ToString::to_string),
end: events
.last()
.map(at!(0))
.as_ref()
.map(ToString::to_string)
.unwrap_or_default(),
chunk: events
.into_iter()
.map(at!(1))
.map(Event::into_format)
.collect(),
}
.into(),
})
}
+3 -3
View File
@@ -55,7 +55,7 @@ pub(crate) async fn get_room_summary(
) -> Result<get_summary::v1::Response> {
let (room_id, servers) = services
.alias
.maybe_resolve_with_servers(&body.room_id_or_alias, Some(body.via.clone()))
.maybe_resolve_with_servers(&body.room_id_or_alias, Some(&body.via))
.await?;
if services.metadata.is_banned(&room_id).await {
@@ -232,8 +232,8 @@ async fn remote_room_summary_hierarchy_response(
.iter()
.map(|server| {
services
.sending
.send_federation_request(server, request.clone())
.federation
.execute(server, request.clone())
})
.collect();
+420 -185
View File
@@ -1,42 +1,63 @@
use std::cmp::max;
use axum::extract::State;
use futures::StreamExt;
use futures::{FutureExt, StreamExt, TryFutureExt, TryStreamExt};
use ruma::{
CanonicalJsonObject, RoomId, RoomVersionId,
api::client::room::upgrade_room,
CanonicalJsonObject, OwnedEventId, OwnedRoomId, OwnedUserId, RoomId, RoomVersionId, UserId,
api::client::room::upgrade_room::v3,
events::{
StateEventType, TimelineEventType,
room::{
create::PreviousRoom,
member::{MembershipState, RoomMemberEventContent},
power_levels::RoomPowerLevelsEventContent,
tombstone::RoomTombstoneEventContent,
},
},
int,
room_version_rules::RoomIdFormatVersion,
room_version_rules::{RoomIdFormatVersion, RoomVersionRules},
};
use serde_json::{
Value as JsonValue, json,
value::{to_raw_value, to_value},
};
use serde_json::{json, value::to_raw_value};
use tuwunel_core::{
Err, Result, err,
Err, Result, debug_info, err, error, implement, info, is_equal_to, is_less_than,
matrix::{Event, StateKey, pdu::PduBuilder, room_version},
utils::{
future::TryExtExt,
stream::{IterStream, ReadyExt, WidebandExt},
},
};
use tuwunel_service::{Services, rooms::timeline::RoomMutexGuard};
use crate::Ruma;
/// Recommended transferable state events list from the spec
const TRANSFERABLE_STATE_EVENTS: &[StateEventType; 9] = &[
StateEventType::RoomAvatar,
//TODO: Upgrade Ruma
const RECOMMENDED_TRANSFERABLE_STATE_EVENT_TYPES: &[StateEventType; 9] = &[
StateEventType::RoomServerAcl,
StateEventType::RoomEncryption,
StateEventType::RoomName,
StateEventType::RoomAvatar,
StateEventType::RoomTopic,
StateEventType::RoomGuestAccess,
StateEventType::RoomHistoryVisibility,
StateEventType::RoomJoinRules,
StateEventType::RoomName,
StateEventType::RoomPowerLevels,
StateEventType::RoomServerAcl,
StateEventType::RoomTopic,
];
#[derive(Debug)]
struct RoomUpgradeContext<'a> {
services: &'a Services,
sender_user: &'a UserId,
old_room_id: &'a RoomId,
old_state_lock: &'a RoomMutexGuard,
new_room_id: &'a RoomId,
new_state_lock: &'a RoomMutexGuard,
new_version_rules: &'a RoomVersionRules,
additional_creators: &'a [OwnedUserId],
}
/// # `POST /_matrix/client/r0/rooms/{roomId}/upgrade`
///
/// Upgrades the room.
@@ -47,117 +68,227 @@ const TRANSFERABLE_STATE_EVENTS: &[StateEventType; 9] = &[
/// - Transfers some state events
/// - Moves local aliases
/// - Modifies old room power levels to prevent users from speaking
#[tracing::instrument(level = "debug")]
pub(crate) async fn upgrade_room_route(
State(services): State<crate::State>,
body: Ruma<upgrade_room::v3::Request>,
) -> Result<upgrade_room::v3::Response> {
debug_assert!(
TRANSFERABLE_STATE_EVENTS.is_sorted(),
"TRANSFERABLE_STATE_EVENTS is not sorted"
);
body: Ruma<v3::Request>,
) -> Result<v3::Response> {
let sender_user = body.sender_user();
let new_version = &body.new_version;
let version_rules = room_version::rules(new_version)?;
if !services
.server
.supported_room_version(&body.new_version)
.supported_room_version(new_version)
{
return Err!(Request(UnsupportedRoomVersion(
"This server does not support that room version.",
)));
}
if matches!(body.new_version, RoomVersionId::V12) {
return Err!(Request(UnsupportedRoomVersion(
"Upgrading to version 12 is still under development.",
)));
let old_room_id = &body.room_id;
let old_state_lock = services.state.mutex.lock(old_room_id).await;
if !services
.state_accessor
.user_can_tombstone(old_room_id, sender_user, &old_state_lock)
.await
{
return Err!(Request(Forbidden("You are not permitted to upgrade the room.")));
}
let room_version_rules = room_version::rules(&body.new_version)?;
let room_id_format = &room_version_rules.room_id_format;
assert!(*room_id_format == RoomIdFormatVersion::V1, "TODO");
// Create a replacement room
let replacement_room = RoomId::new_v1(services.globals.server_name());
let _short_id = services
.short
.get_or_create_shortroomid(&replacement_room)
.await;
let state_lock = services.state.mutex.lock(&body.room_id).await;
// Send a m.room.tombstone event to the old room to indicate that it is not
// intended to be used any further Fail if the sender does not have the required
// permissions
let tombstone_event_id = services
let latest_event = services
.timeline
.build_and_append_pdu(
PduBuilder::state(StateKey::new(), &RoomTombstoneEventContent {
body: "This room has been replaced".to_owned(),
replacement_room: replacement_room.clone(),
}),
sender_user,
&body.room_id,
&state_lock,
)
.await?;
.latest_pdu_in_room(old_room_id)
.await
.ok();
// Change lock to replacement room
drop(state_lock);
let state_lock = services.state.mutex.lock(&replacement_room).await;
let predecessor = PreviousRoom {
room_id: old_room_id.to_owned(),
event_id: latest_event
.as_ref()
.map(Event::event_id)
.map(ToOwned::to_owned),
};
debug_info!(
%sender_user,
%old_room_id,
last_event = ?predecessor.event_id,
?new_version,
"Attempting upgrade of room..."
);
let id_format = version_rules.room_id_format;
let (replacement_room, state_lock) = match id_format {
| RoomIdFormatVersion::V2 =>
upgrade_room_create(
&services,
sender_user,
old_room_id,
new_version,
&version_rules,
predecessor,
body.additional_creators.clone(),
)
.await,
| RoomIdFormatVersion::V1 =>
upgrade_room_create_legacy(
&services,
sender_user,
old_room_id,
new_version,
&version_rules,
predecessor,
)
.await,
}
.inspect_err(|e| error!(?body, "Upgrade m.room.create event failed: {e}"))?;
let context = RoomUpgradeContext {
services: &services,
sender_user,
old_room_id: &body.room_id,
old_state_lock: &old_state_lock,
new_room_id: &replacement_room,
new_state_lock: &state_lock,
new_version_rules: &version_rules,
additional_creators: &body.additional_creators,
};
if let Err(e) = context.transfer_room().await {
error!(?e, ?context, "Room upgrade failed. Cleaning up incomplete room...");
if let Err(e) = services
.delete
.delete_room(&replacement_room, false, state_lock)
.await
{
error!("Additional errors while deleting incomplete room: {e}");
}
return Err(e);
}
info!(
old_room_id = %context.old_room_id,
new_room_id = %context.new_room_id,
upgraded_by = %sender_user,
"Room upgraded",
);
Ok(v3::Response { replacement_room })
}
#[tracing::instrument(level = "info")]
async fn upgrade_room_create(
services: &Services,
sender_user: &UserId,
old_room_id: &RoomId,
new_version: &RoomVersionId,
version_rules: &RoomVersionRules,
predecessor: PreviousRoom,
mut additional_creators: Vec<OwnedUserId>,
) -> Result<(OwnedRoomId, RoomMutexGuard)> {
// Get the old room creation event
let mut create_event_content: CanonicalJsonObject = services
let mut content: CanonicalJsonObject = services
.state_accessor
.room_state_get_content(&body.room_id, &StateEventType::RoomCreate, "")
.room_state_get_content(old_room_id, &StateEventType::RoomCreate, "")
.await
.map_err(|_| err!(Database("Found room without m.room.create event.")))?;
// Use the m.room.tombstone event as the predecessor
let predecessor = Some(ruma::events::room::create::PreviousRoom::new(
body.room_id.clone(),
Some(tombstone_event_id),
));
content.remove("creator");
content.insert("predecessor".into(), json!(predecessor).try_into()?);
content.insert("room_version".into(), json!(new_version).try_into()?);
// Send a m.room.create event containing a predecessor field and the applicable
// room_version
if version_rules
.authorization
.additional_room_creators
{
use RoomVersionId::*;
match body.new_version {
| V1 | V2 | V3 | V4 | V5 | V6 | V7 | V8 | V9 | V10 => {
create_event_content.insert(
"creator".into(),
json!(&sender_user).try_into().map_err(|e| {
err!(Request(BadJson(error!("Error forming creation event: {e}"))))
})?,
);
},
| _ => {
// "creator" key no longer exists in V11+ rooms
create_event_content.remove("creator");
},
additional_creators.sort();
additional_creators.dedup();
content.remove("additional_creators");
if !additional_creators.is_empty() {
content.insert("additional_creators".into(), json!(additional_creators).try_into()?);
}
}
create_event_content.insert(
"room_version".into(),
json!(&body.new_version)
.try_into()
.map_err(|_| err!(Request(BadJson("Error forming creation event"))))?,
);
create_event_content.insert(
"predecessor".into(),
json!(predecessor)
.try_into()
.map_err(|_| err!(Request(BadJson("Error forming creation event"))))?,
);
// Validate creation event content
let raw_content = to_raw_value(&content)?;
if let Err(e) = serde_json::from_str::<CanonicalJsonObject>(raw_content.get()) {
return Err!(Request(BadJson("Error forming creation event: {e}")));
}
let room_id = ruma::room_id!("!thiswillbereplaced").to_owned();
let state_lock = services.state.mutex.lock(&room_id).await;
let create_event_id = services
.timeline
.build_and_append_pdu(
PduBuilder {
event_type: TimelineEventType::RoomCreate,
content: to_raw_value(&content)?,
state_key: Some(StateKey::new()),
..Default::default()
},
sender_user,
&room_id,
&state_lock,
)
.boxed()
.await?;
drop(state_lock);
// The real room_id is now the event_id.
let room_id = OwnedRoomId::from_parts('!', create_event_id.localpart(), None)?;
let state_lock = services.state.mutex.lock(&room_id).await;
Ok((room_id, state_lock))
}
#[tracing::instrument(level = "info")]
async fn upgrade_room_create_legacy(
services: &Services,
sender_user: &UserId,
old_room_id: &RoomId,
new_version: &RoomVersionId,
version_rules: &RoomVersionRules,
predecessor: PreviousRoom,
) -> Result<(OwnedRoomId, RoomMutexGuard)> {
// Create a replacement room
let new_room_id = RoomId::new_v1(services.globals.server_name());
let state_lock = services.state.mutex.lock(&new_room_id).await;
let _short_id = services
.short
.get_or_create_shortroomid(&new_room_id)
.await;
// Get the old room creation event
let mut content: CanonicalJsonObject = services
.state_accessor
.room_state_get_content(old_room_id, &StateEventType::RoomCreate, "")
.await
.map_err(|_| err!(Database("Found room without m.room.create event.")))?;
// Send a m.room.create event containing a predecessor field and the applicable
// room_version. "creator" key no longer exists in V11+ rooms.
{
use RoomVersionId::*;
match new_version {
| V1 | V2 | V3 | V4 | V5 | V6 | V7 | V8 | V9 | V10 =>
content.insert("creator".into(), json!(&sender_user).try_into()?),
| _ => content.remove("creator"),
}
};
content.insert("predecessor".into(), json!(predecessor).try_into()?);
content.insert("room_version".into(), json!(new_version).try_into()?);
// Validate creation event content
if serde_json::from_str::<CanonicalJsonObject>(to_raw_value(&create_event_content)?.get())
.is_err()
{
return Err!(Request(BadJson("Error forming creation event")));
let raw_content = to_raw_value(&content)?;
if let Err(e) = serde_json::from_str::<CanonicalJsonObject>(raw_content.get()) {
return Err!(Request(BadJson("Error forming creation event: {e}")));
}
services
@@ -165,125 +296,229 @@ pub(crate) async fn upgrade_room_route(
.build_and_append_pdu(
PduBuilder {
event_type: TimelineEventType::RoomCreate,
content: to_raw_value(&create_event_content)?,
unsigned: None,
content: to_raw_value(&content)?,
state_key: Some(StateKey::new()),
redacts: None,
timestamp: None,
..Default::default()
},
sender_user,
&replacement_room,
&new_room_id,
&state_lock,
)
.await?;
// Join the new room
services
Ok((new_room_id, state_lock))
}
#[implement(RoomUpgradeContext, params = "<'_>")]
#[tracing::instrument(level = "debug")]
async fn transfer_room(&self) -> Result {
self.move_joined_member().await?;
self.move_state_events().await?;
self.move_local_aliases().await?;
self.tombstone_old_room().await?;
// After commitment to the tombstone above no more errors can propagate.
self.lockdown_old_room()
.await
.inspect_err(|e| error!(?self, "Failed to lockdown old room: {e}"))
.ok();
Ok(())
}
// Join the new room
#[implement(RoomUpgradeContext, params = "<'_>")]
#[tracing::instrument(level = "debug")]
async fn move_joined_member(&self) -> Result<OwnedEventId> {
let old_content: RoomMemberEventContent = self
.services
.state_accessor
.room_state_get_content(
self.old_room_id,
&StateEventType::RoomMember,
self.sender_user.as_str(),
)
.inspect_err(|e| error!(?self, "Missing room member event: {e}"))
.await?;
self.services
.timeline
.build_and_append_pdu(
PduBuilder {
event_type: TimelineEventType::RoomMember,
content: to_raw_value(&RoomMemberEventContent {
membership: MembershipState::Join,
displayname: services.users.displayname(sender_user).await.ok(),
avatar_url: services.users.avatar_url(sender_user).await.ok(),
is_direct: None,
third_party_invite: None,
blurhash: services.users.blurhash(sender_user).await.ok(),
reason: None,
join_authorized_via_users_server: None,
})?,
unsigned: None,
state_key: Some(sender_user.as_str().into()),
redacts: None,
timestamp: None,
},
sender_user,
&replacement_room,
&state_lock,
PduBuilder::state(self.sender_user.as_str(), &RoomMemberEventContent {
membership: MembershipState::Join,
..old_content
}),
self.sender_user,
self.new_room_id,
self.new_state_lock,
)
.await?;
.await
}
// Replicate transferable state events to the new room
for event_type in TRANSFERABLE_STATE_EVENTS {
let event_content = match services
.state_accessor
.room_state_get(&body.room_id, event_type, "")
.await
// Replicate transferable state events to the new room
#[implement(RoomUpgradeContext, params = "<'_>")]
#[tracing::instrument(level = "debug")]
async fn move_state_events(&self) -> Result {
RECOMMENDED_TRANSFERABLE_STATE_EVENT_TYPES
.iter()
.rev()
.stream()
.wide_filter_map(|event_type| {
self.services
.state_accessor
.room_state_get(self.old_room_id, event_type, "")
.ok()
})
.map(Ok)
.try_for_each(async |event| {
self.services
.timeline
.build_and_append_pdu(
self.rebuild_state_event(&event)?,
self.sender_user,
self.new_room_id,
self.new_state_lock,
)
.inspect_err(|e| {
error!(?event, ?self, "Failed to transfer state on upgrade: {e}");
})
.map_ok(|_| ())
.await
})
.await
}
#[implement(RoomUpgradeContext, params = "<'_>")]
#[tracing::instrument(level = "debug")]
fn rebuild_state_event<Pdu: Event>(&self, event: &Pdu) -> Result<PduBuilder> {
let content = match event.kind() {
| TimelineEventType::RoomPowerLevels
if self
.new_version_rules
.authorization
.explicitly_privilege_room_creators =>
{
| Ok(v) => v.content().to_owned(),
| Err(_) => continue, // Skipping missing events.
};
let mut content = event.get_content_as_value();
services
.timeline
.build_and_append_pdu(
PduBuilder {
event_type: event_type.to_string().into(),
content: event_content,
state_key: Some(StateKey::new()),
..Default::default()
},
sender_user,
&replacement_room,
&state_lock,
)
.await?;
}
if let Some(users) = content
.get_mut("users")
.and_then(JsonValue::as_object_mut)
{
users.retain(|user_id, _pl| {
!self
.additional_creators
.iter()
.map(AsRef::as_ref)
.map(UserId::as_str)
.any(is_equal_to!(user_id.as_str()))
&& self.sender_user.as_str() != user_id.as_str()
});
}
// Moves any local aliases to the new room
let mut local_aliases = services
if content["events"]["m.room.tombstone"]
.as_i64()
.is_none_or(is_less_than!(150))
{
content["events"]["m.room.tombstone"] = to_value(150)?;
}
to_raw_value(&content)?
},
| _ => to_raw_value(event.content())?,
};
Ok(PduBuilder {
content,
event_type: event.kind().clone(),
state_key: event.state_key().map(Into::into),
..Default::default()
})
}
// Moves any local aliases to the new room
#[implement(RoomUpgradeContext, params = "<'_>")]
#[tracing::instrument(level = "debug")]
async fn move_local_aliases(&self) -> Result {
self.services
.alias
.local_aliases_for_room(&body.room_id)
.boxed();
.local_aliases_for_room(self.old_room_id)
.filter_map(|alias| {
self.services
.alias
.remove_alias(alias, self.sender_user)
.inspect_err(move |e| error!(?alias, ?self, "Failed to remove alias: {e}"))
.map_ok(move |()| alias)
.ok()
})
.ready_for_each(|alias| {
self.services
.alias
.set_alias(alias, self.new_room_id, self.sender_user)
.inspect_err(|e| error!(?self, "Failed to add alias: {e}"))
.ok();
})
.map(Ok)
.await
}
while let Some(alias) = local_aliases.next().await {
services
.alias
.remove_alias(alias, sender_user)
.await?;
services
.alias
.set_alias(alias, &replacement_room, sender_user)?;
}
// Send a m.room.tombstone event to the old room to indicate that it is not
// intended to be used any further Fail if the sender does not have the required
// permissions.
#[implement(RoomUpgradeContext, params = "<'_>")]
#[tracing::instrument(level = "debug")]
async fn tombstone_old_room(&self) -> Result<OwnedEventId> {
self.services
.timeline
.build_and_append_pdu(
PduBuilder::state(StateKey::new(), &RoomTombstoneEventContent {
body: "This room has been upgraded.".to_owned(),
replacement_room: self.new_room_id.to_owned(),
}),
self.sender_user,
self.old_room_id,
self.old_state_lock,
)
.await
}
// Modify the power levels in the old room to prevent sending of events and
// inviting new users. Though a Result is returned, the callsite above treats it
// as infallible because the tombstone represents the commitment.
#[implement(RoomUpgradeContext, params = "<'_>")]
#[tracing::instrument(level = "debug")]
async fn lockdown_old_room(&self) -> Result<OwnedEventId> {
// Get the old room power levels
let power_levels_event_content: RoomPowerLevelsEventContent = services
let old_content: RoomPowerLevelsEventContent = self
.services
.state_accessor
.room_state_get_content(&body.room_id, &StateEventType::RoomPowerLevels, "")
.room_state_get_content(self.old_room_id, &StateEventType::RoomPowerLevels, "")
.await
.map_err(|_| err!(Database("Found room without m.room.power_levels event.")))?;
// Setting events_default and invite to the greater of 50 and users_default + 1
let new_level = max(
int!(50),
power_levels_event_content
.users_default
.checked_add(int!(1))
.ok_or_else(|| {
err!(Request(BadJson("users_default power levels event content is not valid")))
})?,
);
let old_users_default = old_content
.users_default
.checked_add(int!(1))
.ok_or_else(|| {
err!(Request(BadJson("users_default power levels event content is not valid")))
})?;
// Modify the power levels in the old room to prevent sending of events and
// inviting new users
services
// Setting events_default and invite to the greater of 50 and users_default + 1
let new_level = max(int!(50), old_users_default);
self.services
.timeline
.build_and_append_pdu(
PduBuilder::state(StateKey::new(), &RoomPowerLevelsEventContent {
events_default: new_level,
invite: new_level,
..power_levels_event_content
..old_content
}),
sender_user,
&body.room_id,
&state_lock,
self.sender_user,
self.old_room_id,
self.old_state_lock,
)
.await?;
drop(state_lock);
// Return the replacement room id
Ok(upgrade_room::v3::Response { replacement_room })
.await
}
+1 -1
View File
@@ -51,7 +51,7 @@ pub(super) async fn ldap_login(
if !services.users.exists(lowercased_user_id).await {
services
.users
.create(lowercased_user_id, Some("*"), Some("ldap"))
.full_register(lowercased_user_id, Some("*"), Some("ldap"), None, false, false)
.await?;
}
+1 -1
View File
@@ -23,7 +23,7 @@ pub(crate) async fn logout_route(
) -> Result<logout::v3::Response> {
services
.users
.remove_device(body.sender_user(), body.sender_device())
.remove_device(body.sender_user(), body.sender_device()?)
.await;
Ok(logout::v3::Response::new())
+24 -28
View File
@@ -21,7 +21,7 @@ use ruma::api::client::session::{
v3::{DiscoveryInfo, HomeserverInfo, LoginInfo},
},
};
use tuwunel_core::{Err, Result, info, utils, utils::stream::ReadyExt};
use tuwunel_core::{Err, Result, info, utils::stream::ReadyExt};
use tuwunel_service::users::device::generate_refresh_token;
use self::{ldap::ldap_login, password::password_login};
@@ -30,7 +30,7 @@ pub(crate) use self::{
refresh::refresh_token_route,
token::login_token_route,
};
use super::{DEVICE_ID_LENGTH, TOKEN_LENGTH};
use super::TOKEN_LENGTH;
use crate::Ruma;
/// # `GET /_matrix/client/v3/login`
@@ -97,43 +97,39 @@ pub(crate) async fn login_route(
// Generate a new refresh_token if requested by client
let refresh_token = expires_in.is_some().then(generate_refresh_token);
// Generate new device id if the user didn't specify one
let device_id = body
.device_id
.clone()
.unwrap_or_else(|| utils::random_string(DEVICE_ID_LENGTH).into());
// Determine if device_id was provided and exists in the db for this user
let device_exists = services
.users
.all_device_ids(&user_id)
.ready_any(|v| v == device_id)
.await;
if !device_exists {
services
let device_id = if let Some(device_id) = &body.device_id
&& services
.users
.create_device(
&user_id,
&device_id,
(&access_token, expires_in),
refresh_token.as_deref(),
body.initial_device_display_name.clone(),
Some(client.to_string()),
)
.await?;
} else {
.all_device_ids(&user_id)
.ready_any(|v| v == device_id)
.await
{
services
.users
.set_access_token(
&user_id,
&device_id,
device_id,
&access_token,
expires_in,
refresh_token.as_deref(),
)
.await?;
}
device_id.clone()
} else {
services
.users
.create_device(
&user_id,
body.device_id.as_deref(),
(Some(&access_token), expires_in),
refresh_token.as_deref(),
body.initial_device_display_name.as_deref(),
Some(client.to_string()),
)
.await?
};
info!("{user_id} logged in");
+2 -1
View File
@@ -50,7 +50,8 @@ pub(crate) async fn login_token_route(
// This route SHOULD have UIA
// TODO: How do we make only UIA sessions that have not been used before valid?
let (sender_user, sender_device) = body.sender();
let sender_user = body.sender_user();
let sender_device = body.sender_device()?;
let password_flow = uiaa::AuthFlow { stages: vec![uiaa::AuthType::Password] };
+344 -223
View File
@@ -10,7 +10,7 @@ use futures::{
pin_mut,
};
use ruma::{
DeviceId, EventId, OwnedEventId, OwnedRoomId, OwnedUserId, RoomId, UserId,
DeviceId, EventId, OwnedRoomId, OwnedUserId, RoomId, UserId,
api::client::{
filter::FilterDefinition,
sync::sync_events::{
@@ -21,7 +21,6 @@ use ruma::{
State as RoomState, StateEvents, Timeline, ToDevice,
},
},
uiaa::UiaaResponse,
},
events::{
AnyRawAccountDataEvent, AnySyncEphemeralRoomEvent, StateEventType,
@@ -36,11 +35,11 @@ use ruma::{
};
use tokio::time;
use tuwunel_core::{
Error, Result, at,
Result, at,
debug::INFO_SPAN_LEVEL,
err, error,
err,
error::{inspect_debug_log, inspect_log},
extract_variant, is_equal_to,
extract_variant, is_equal_to, is_false, is_true,
matrix::{
Event,
event::Matches,
@@ -51,11 +50,12 @@ use tuwunel_core::{
trace,
utils::{
self, BoolExt, FutureBoolExt, IterStream, ReadyExt, TryFutureExtExt,
future::{OptionStream, ReadyEqExt},
future::{OptionStream, ReadyBoolExt},
math::ruma_from_u64,
result::MapExpect,
stream::{BroadbandExt, Tools, TryExpect, WidebandExt},
string::to_small_string,
},
warn,
};
use tuwunel_service::{
Services,
@@ -67,7 +67,7 @@ use tuwunel_service::{
};
use super::{load_timeline, share_encrypted_room};
use crate::{Ruma, RumaResponse, client::ignored_filter};
use crate::{Ruma, client::ignored_filter};
#[derive(Default)]
struct StateChanges {
@@ -120,24 +120,43 @@ type PresenceUpdates = HashMap<OwnedUserId, PresenceEventContent>;
skip_all,
fields(
user_id = %body.sender_user(),
device_id = %body.sender_device.as_deref().map_or("<no device>", |x| x.as_str()),
)
)]
pub(crate) async fn sync_events_route(
State(services): State<crate::State>,
body: Ruma<sync_events::v3::Request>,
) -> Result<sync_events::v3::Response, RumaResponse<UiaaResponse>> {
let (sender_user, sender_device) = body.sender();
) -> Result<sync_events::v3::Response> {
let sender_user = body.sender_user();
let sender_device = body.sender_device.as_deref();
let filter: OptionFuture<_> = body
.body
.filter
.as_ref()
.map(async |filter| match filter {
| Filter::FilterDefinition(filter) => filter.clone(),
| Filter::FilterId(filter_id) => services
.users
.get_filter(sender_user, filter_id)
.await
.unwrap_or_default(),
})
.into();
let filter = filter.map(Option::unwrap_or_default);
let full_state = body.body.full_state;
let set_presence = &body.body.set_presence;
let ping_presence = services
.presence
.maybe_ping_presence(sender_user, &body.body.set_presence)
.maybe_ping_presence(sender_user, body.sender_device.as_deref(), set_presence)
.inspect_err(inspect_log)
.ok();
// Record user as actively syncing for push suppression heuristic.
let note_sync = services.presence.note_sync(sender_user);
join(ping_presence, note_sync).await;
let (filter, ..) = join3(filter, ping_presence, note_sync).await;
let mut since = body
.body
@@ -175,22 +194,33 @@ pub(crate) async fn sync_events_route(
let next_batch = services.globals.wait_pending().await?;
debug_assert!(since <= next_batch, "next_batch is monotonic");
if since < next_batch || body.body.full_state {
let response = build_sync_events(&services, &body, since, next_batch).await?;
if since < next_batch || full_state {
let response = build_sync_events(
&services,
sender_user,
sender_device,
since,
next_batch,
full_state,
&filter,
)
.await?;
let empty = response.rooms.is_empty()
&& response.presence.is_empty()
&& response.account_data.is_empty()
&& response.device_lists.is_empty()
&& response.to_device.is_empty();
if !empty || body.body.full_state {
if !empty || full_state {
return Ok(response);
}
}
// Wait for activity
if time::timeout_at(stop_at, watchers).await.is_err() || services.server.is_stopping() {
let response = build_empty_response(&services, &body, next_batch).await;
let response =
build_empty_response(&services, sender_user, sender_device, next_batch).await;
trace!(since, next_batch, "empty response");
return Ok(response);
}
@@ -209,23 +239,30 @@ pub(crate) async fn sync_events_route(
async fn build_empty_response(
services: &Services,
body: &Ruma<sync_events::v3::Request>,
sender_user: &UserId,
sender_device: Option<&DeviceId>,
next_batch: u64,
) -> sync_events::v3::Response {
sync_events::v3::Response {
device_one_time_keys_count: services
.users
.count_one_time_keys(body.sender_user(), body.sender_device())
.await,
let device_one_time_keys_count: OptionFuture<_> = sender_device
.map(|sender_device| {
services
.users
.count_one_time_keys(sender_user, sender_device)
})
.into();
..sync_events::v3::Response::new(next_batch.to_string())
sync_events::v3::Response {
device_one_time_keys_count: device_one_time_keys_count
.await
.unwrap_or_default(),
..sync_events::v3::Response::new(to_small_string(next_batch))
}
}
#[tracing::instrument(
name = "build",
level = INFO_SPAN_LEVEL,
ret(level = "trace"),
skip_all,
fields(
%since,
@@ -235,23 +272,13 @@ async fn build_empty_response(
)]
async fn build_sync_events(
services: &Services,
body: &Ruma<sync_events::v3::Request>,
sender_user: &UserId,
sender_device: Option<&DeviceId>,
since: u64,
next_batch: u64,
full_state: bool,
filter: &FilterDefinition,
) -> Result<sync_events::v3::Response> {
let (sender_user, sender_device) = body.sender();
let full_state = body.body.full_state;
let filter = match body.body.filter.as_ref() {
| None => FilterDefinition::default(),
| Some(Filter::FilterDefinition(filter)) => filter.clone(),
| Some(Filter::FilterId(filter_id)) => services
.users
.get_filter(sender_user, filter_id)
.await
.unwrap_or_default(),
};
let joined_rooms = services
.state_cache
.rooms_joined(sender_user)
@@ -266,7 +293,7 @@ async fn build_sync_events(
since,
next_batch,
full_state,
&filter,
filter,
)
.map_ok(move |(joined_room, dlu, jeu)| (room_id, joined_room, dlu, jeu))
.ok()
@@ -297,7 +324,7 @@ async fn build_sync_events(
sender_user,
next_batch,
full_state,
&filter,
filter,
)
.map_ok(move |left_room| (room_id, left_room))
.ok()
@@ -356,7 +383,7 @@ async fn build_sync_events(
let presence_updates: OptionFuture<_> = services
.config
.allow_local_presence
.then(|| process_presence_updates(services, since, next_batch, sender_user, &filter))
.then(|| process_presence_updates(services, since, next_batch, sender_user, filter))
.into();
let account_data = services
@@ -372,26 +399,38 @@ async fn build_sync_events(
.map(ToOwned::to_owned)
.collect::<HashSet<_>>();
let to_device_events = services
.users
.get_to_device_events(sender_user, sender_device, Some(since), Some(next_batch))
.collect::<Vec<_>>();
let to_device_events: OptionFuture<_> = sender_device
.map(|sender_device| {
services
.users
.get_to_device_events(sender_user, sender_device, Some(since), Some(next_batch))
.map(at!(1))
.collect::<Vec<_>>()
})
.into();
let device_one_time_keys_count = services
.users
.count_one_time_keys(sender_user, sender_device);
let device_one_time_keys_count: OptionFuture<_> = sender_device
.map(|sender_device| {
services
.users
.count_one_time_keys(sender_user, sender_device)
})
.into();
// Remove all to-device events the device received *last time*
let remove_to_device_events =
services
.users
.remove_to_device_events(sender_user, sender_device, since);
let remove_to_device_events: OptionFuture<_> = sender_device
.map(|sender_device| {
services
.users
.remove_to_device_events(sender_user, sender_device, since)
})
.into();
let (
account_data,
keys_changed,
device_one_time_keys_count,
((), to_device_events, presence_updates),
presence_updates,
(_, to_device_events, device_one_time_keys_count),
(
(joined_rooms, mut device_list_updates, left_encrypted_users),
left_rooms,
@@ -401,8 +440,8 @@ async fn build_sync_events(
) = join5(
account_data,
keys_changed,
device_one_time_keys_count,
join3(remove_to_device_events, to_device_events, presence_updates),
presence_updates,
join3(remove_to_device_events, to_device_events, device_one_time_keys_count),
join4(joined_rooms, left_rooms, invited_rooms, knocked_rooms),
)
.boxed()
@@ -438,10 +477,10 @@ async fn build_sync_events(
left: device_list_left,
changed: device_list_updates.into_iter().collect(),
},
device_one_time_keys_count,
device_one_time_keys_count: device_one_time_keys_count.unwrap_or_default(),
// Fallback keys are not yet supported
device_unused_fallback_key_types: None,
next_batch: next_batch.to_string(),
next_batch: to_small_string(next_batch),
presence: Presence { events: presence_events },
rooms: Rooms {
leave: left_rooms,
@@ -449,7 +488,9 @@ async fn build_sync_events(
invite: invited_rooms,
knock: knocked_rooms,
},
to_device: ToDevice { events: to_device_events },
to_device: ToDevice {
events: to_device_events.unwrap_or_default(),
},
})
}
@@ -506,32 +547,24 @@ async fn handle_left_room(
.state_cache
.get_left_count(room_id, sender_user)
.await
.ok();
.unwrap_or(0);
let filter_exclude = filter
.room
.not_rooms
.iter()
.any(is_equal_to!(room_id));
let filter_include = filter
.room
.rooms
.as_ref()
.is_some_and(|rooms| rooms.iter().any(is_equal_to!(room_id)));
let too_soon = Some(next_batch) < left_count;
let too_late = Some(since) >= left_count;
let initial_sync = since == 0;
let include_leave =
filter.room.include_leave && !filter_exclude && (filter_include || initial_sync);
// Left before last sync or after cutoff for next sync
if (too_late && !include_leave) || too_soon {
if left_count == 0 || left_count > next_batch {
return Ok(None);
}
let is_not_found = services.metadata.exists(room_id).eq(&false);
let include_leave = filter.room.include_leave;
if since == 0 && !include_leave {
return Ok(None);
}
// Cannot sync unless the event falls within the snapshot. The room is only
// sync'ed once to the client, after that it's too late.
if since != 0 && left_count <= since {
return Ok(None);
}
let is_not_found = services.metadata.exists(room_id).is_false();
let is_disabled = services.metadata.is_disabled(room_id);
@@ -539,125 +572,176 @@ async fn handle_left_room(
pin_mut!(is_not_found, is_disabled, is_banned);
if is_not_found.or(is_disabled).or(is_banned).await {
// This is just a rejected invite, not a room we know
// Insert a leave event anyways for the client
// For rejected invites, deleted, missing, or broken room state this is the last
// resort to convey a the minimum of information to the client.
let event = PduEvent {
event_id: EventId::new(services.globals.server_name()),
sender: sender_user.to_owned(),
origin: None,
origin_server_ts: utils::millis_since_unix_epoch().try_into()?,
kind: RoomMember,
content: serde_json::from_str(r#"{"membership":"leave"}"#)?,
state_key: Some(sender_user.as_str().into()),
unsigned: None,
sender: sender_user.to_owned(),
content: serde_json::from_str(r#"{"membership":"leave"}"#)?,
// The following keys are dropped on conversion
room_id: room_id.clone(),
prev_events: Default::default(),
auth_events: Default::default(),
depth: uint!(1),
origin: None,
unsigned: None,
redacts: None,
hashes: EventHash::default(),
auth_events: Default::default(),
prev_events: Default::default(),
signatures: None,
};
return Ok(Some(LeftRoom {
account_data: RoomAccountData { events: Vec::new() },
account_data: RoomAccountData::default(),
state: RoomState::Before(StateEvents { events: vec![event.into_format()] }),
timeline: Timeline {
limited: false,
prev_batch: Some(next_batch.to_string()),
events: Vec::new(),
events: Default::default(),
prev_batch: Some(left_count.to_string()),
},
state: RoomState::Before(StateEvents { events: vec![event.into_format()] }),
}));
}
let mut left_state_events = Vec::new();
load_left_room(services, sender_user, room_id, since, left_count, full_state, filter).await
}
#[tracing::instrument(name = "load", level = "debug", skip_all)]
async fn load_left_room(
services: &Services,
sender_user: &UserId,
room_id: &RoomId,
since: u64,
left_count: u64,
full_state: bool,
filter: &FilterDefinition,
) -> Result<Option<LeftRoom>> {
let initial = since == 0;
let timeline_limit: usize = filter
.room
.timeline
.limit
.map(TryInto::try_into)
.map_expect("UInt to usize")
.unwrap_or(10)
.min(100);
let (timeline_pdus, limited, _) = load_timeline(
services,
sender_user,
room_id,
PduCount::Normal(since),
Some(PduCount::Normal(left_count)),
timeline_limit.max(1),
)
.await
.unwrap_or_default();
let since_shortstatehash = services
.timeline
.prev_shortstatehash(room_id, PduCount::Normal(since).saturating_add(1))
.await
.ok();
let since_state_ids: HashMap<_, OwnedEventId> = since_shortstatehash
.map(|since_shortstatehash| {
let horizon_shortstatehash: OptionFuture<_> = timeline_pdus
.first()
.map(at!(0))
.map(|count| {
services
.state_accessor
.state_full_ids(since_shortstatehash)
.timeline
.get_shortstatehash(room_id, count)
.inspect_err(inspect_debug_log)
.ok()
})
.into();
let left_shortstatehash = services
.timeline
.get_shortstatehash(room_id, PduCount::Normal(left_count))
.inspect_err(inspect_debug_log)
.or_else(|_| services.state.get_room_shortstatehash(room_id))
.map_err(|_| err!(Database(error!("Room {room_id} has no state"))));
let (since_shortstatehash, horizon_shortstatehash, left_shortstatehash) =
join3(since_shortstatehash, horizon_shortstatehash, left_shortstatehash)
.boxed()
.await;
let StateChanges { state_events, .. } = calculate_state_changes(
services,
sender_user,
room_id,
full_state || initial,
since_shortstatehash,
horizon_shortstatehash.flatten(),
left_shortstatehash?,
false,
None,
)
.boxed()
.await?;
let is_sender_membership = |event: &PduEvent| {
*event.kind() == RoomMember && event.state_key() == Some(sender_user.as_str())
};
let timeline_sender_member = timeline_limit
.eq(&0)
.then(|| timeline_pdus.last().map(ref_at!(1)).cloned())
.into_iter()
.flat_map(Option::into_iter);
let state_events = state_events
.into_iter()
.filter(|pdu| filter.room.state.matches(pdu))
.filter(|pdu| timeline_limit > 0 || !is_sender_membership(pdu))
.chain(timeline_sender_member)
.map(Event::into_format)
.collect();
let left_prev_batch = timeline_limit
.eq(&0)
.then_some(left_count)
.map(PduCount::Normal);
let prev_batch = timeline_pdus
.first()
.filter(|_| timeline_limit > 0)
.map(at!(0))
.or(left_prev_batch)
.as_ref()
.map(ToString::to_string);
let timeline_events = timeline_pdus
.into_iter()
.stream()
.flatten()
.collect()
.wide_filter_map(|item| ignored_filter(services, item, sender_user))
.map(at!(1))
.ready_filter(|pdu| filter.room.timeline.matches(pdu))
.take(timeline_limit)
.collect::<Vec<_>>();
let account_data_events = services
.account_data
.changes_since(Some(room_id), sender_user, since, None)
.ready_filter_map(|e| extract_variant!(e, AnyRawAccountDataEvent::Room))
.collect();
let (account_data_events, timeline_events) = join(account_data_events, timeline_events)
.boxed()
.await;
let Ok(left_event_id): Result<OwnedEventId> = services
.state_accessor
.room_state_get_id(room_id, &StateEventType::RoomMember, sender_user.as_str())
.await
else {
warn!("Left {room_id} but no left state event");
return Ok(None);
};
let Ok(left_shortstatehash) = services
.state
.pdu_shortstatehash(&left_event_id)
.await
else {
warn!(event_id = %left_event_id, "Leave event has no state in {room_id}");
return Ok(None);
};
let mut left_state_ids: HashMap<_, _> = services
.state_accessor
.state_full_ids(left_shortstatehash)
.collect()
.await;
let leave_shortstatekey = services
.short
.get_or_create_shortstatekey(&StateEventType::RoomMember, sender_user.as_str())
.await;
left_state_ids.insert(leave_shortstatekey, left_event_id);
for (shortstatekey, event_id) in left_state_ids {
if full_state || since_state_ids.get(&shortstatekey) != Some(&event_id) {
let (event_type, state_key) = services
.short
.get_statekey_from_short(shortstatekey)
.await?;
if filter.room.state.lazy_load_options.is_enabled()
&& event_type == StateEventType::RoomMember
&& !full_state
&& state_key
.as_str()
.try_into()
.is_ok_and(|user_id: &UserId| sender_user != user_id)
{
continue;
}
let Ok(pdu) = services.timeline.get_pdu(&event_id).await else {
error!("Pdu in state not found: {event_id}");
continue;
};
left_state_events.push(pdu.into_format());
}
}
Ok(Some(LeftRoom {
account_data: RoomAccountData { events: Vec::new() },
account_data: RoomAccountData { events: account_data_events },
state: RoomState::Before(StateEvents { events: state_events }),
timeline: Timeline {
// TODO: support left timeline events so we dont need to set limited to true
limited: true,
prev_batch: Some(next_batch.to_string()),
events: Vec::new(), // and so we dont need to set this to empty vec
prev_batch,
limited: limited || timeline_limit == 0,
events: timeline_events
.into_iter()
.map(Event::into_format)
.collect(),
},
state: RoomState::Before(StateEvents { events: left_state_events }),
}))
}
@@ -673,7 +757,7 @@ async fn handle_left_room(
async fn load_joined_room(
services: &Services,
sender_user: &UserId,
sender_device: &DeviceId,
sender_device: Option<&DeviceId>,
ref room_id: OwnedRoomId,
since: u64,
next_batch: u64,
@@ -685,8 +769,10 @@ async fn load_joined_room(
.room
.timeline
.limit
.unwrap_or_else(|| uint!(10))
.try_into()?;
.map(TryInto::try_into)
.map_expect("UInt to usize")
.unwrap_or(10)
.min(100);
let (timeline_pdus, limited, last_timeline_count) = load_timeline(
services,
@@ -698,10 +784,20 @@ async fn load_joined_room(
)
.await?;
let since_shortstatehash = services
.timeline
.prev_shortstatehash(room_id, PduCount::Normal(since).saturating_add(1))
.ok();
let timeline_changed = last_timeline_count.into_unsigned() > since;
debug_assert!(
timeline_pdus.is_empty() || timeline_changed,
"if timeline events, last_timeline_count must be in the since window."
);
let since_shortstatehash: OptionFuture<_> = timeline_changed
.then(|| {
services
.timeline
.prev_shortstatehash(room_id, PduCount::Normal(since).saturating_add(1))
.ok()
})
.into();
let horizon_shortstatehash: OptionFuture<_> = timeline_pdus
.first()
@@ -714,12 +810,20 @@ async fn load_joined_room(
})
.into();
let current_shortstatehash = services
.timeline
.get_shortstatehash(room_id, last_timeline_count)
.inspect_err(inspect_debug_log)
.or_else(|_| services.state.get_room_shortstatehash(room_id))
.map_err(|_| err!(Database(error!("Room {room_id} has no state"))));
let current_shortstatehash: OptionFuture<_> = timeline_changed
.then(|| {
services
.timeline
.get_shortstatehash(room_id, last_timeline_count)
.inspect_err(inspect_debug_log)
.or_else(|_| services.state.get_room_shortstatehash(room_id))
.map_err(|_| err!(Database(error!("Room {room_id} has no state"))))
})
.into();
let encrypted_room: OptionFuture<_> = timeline_changed
.then(|| services.state_accessor.is_encrypted_room(room_id))
.into();
let receipt_events = services
.read_receipt
@@ -733,8 +837,6 @@ async fn load_joined_room(
})
.collect::<HashMap<OwnedUserId, Raw<AnySyncEphemeralRoomEvent>>>();
let encrypted_room = services.state_accessor.is_encrypted_room(room_id);
let (
(since_shortstatehash, horizon_shortstatehash, current_shortstatehash),
receipt_events,
@@ -744,8 +846,12 @@ async fn load_joined_room(
receipt_events,
encrypted_room,
)
.map(|((since, horizon, current), receipt, encrypted_room)| {
Ok::<_, Error>(((since, horizon.flat_ok(), current?), receipt, encrypted_room))
.map(|((since, horizon, current), receipt, encrypted_room)| -> Result<_> {
Ok((
(since.flatten(), horizon.flat_ok(), current.transpose()?),
receipt,
encrypted_room,
))
})
.boxed()
.await?;
@@ -753,14 +859,14 @@ async fn load_joined_room(
let lazy_load_options =
[&filter.room.state.lazy_load_options, &filter.room.timeline.lazy_load_options];
let lazy_loading_enabled = !encrypted_room
let lazy_loading_enabled = encrypted_room.is_some_and(is_false!())
&& lazy_load_options
.iter()
.any(|opts| opts.is_enabled());
let lazy_loading_context = &lazy_loading::Context {
user_id: sender_user,
device_id: Some(sender_device),
device_id: sender_device,
room_id,
token: Some(since),
options: Some(&filter.room.state.lazy_load_options),
@@ -788,9 +894,14 @@ async fn load_joined_room(
})
.into();
let sender_joined_count = services
.state_cache
.get_joined_count(room_id, sender_user);
let sender_joined_count: OptionFuture<_> = timeline_changed
.then(|| {
services
.state_cache
.get_joined_count(room_id, sender_user)
.unwrap_or(0)
})
.into();
let since_encryption: OptionFuture<_> = since_shortstatehash
.map(|shortstatehash| {
@@ -800,19 +911,20 @@ async fn load_joined_room(
})
.into();
let last_privateread_update = services
.read_receipt
.last_privateread_update(sender_user, room_id);
let last_notification_read: OptionFuture<_> = timeline_pdus
.is_empty()
.then(|| {
services
.user
.pusher
.last_notification_read(sender_user, room_id)
.ok()
})
.into();
let last_privateread_update = services
.read_receipt
.last_privateread_update(sender_user, room_id);
let (
(last_privateread_update, last_notification_read),
(sender_joined_count, since_encryption),
@@ -824,27 +936,36 @@ async fn load_joined_room(
)
.await;
let _encrypted_since_last_sync = !initial && encrypted_room && since_encryption.is_none();
let _encrypted_since_last_sync =
!initial && encrypted_room.is_some_and(is_true!()) && since_encryption.is_none();
let joined_since_last_sync = sender_joined_count.is_ok_and(|count| count > since);
let joined_since_last_sync = sender_joined_count.unwrap_or(0) > since;
let state_changes: OptionFuture<_> = current_shortstatehash
.map(|current_shortstatehash| {
calculate_state_changes(
services,
sender_user,
room_id,
full_state || initial,
since_shortstatehash,
horizon_shortstatehash,
current_shortstatehash,
joined_since_last_sync,
witness.as_ref(),
)
})
.into();
let StateChanges {
heroes,
joined_member_count,
invited_member_count,
mut state_events,
} = calculate_state_changes(
services,
sender_user,
room_id,
full_state || initial,
since_shortstatehash,
horizon_shortstatehash,
current_shortstatehash,
joined_since_last_sync,
witness.as_ref(),
)
.await?;
} = state_changes
.await
.transpose()?
.unwrap_or_default();
let is_sender_membership = |event: &PduEvent| {
*event.event_type() == StateEventType::RoomMember.into()
@@ -870,13 +991,14 @@ async fn load_joined_room(
.map(Into::into)
});
let send_notification_counts =
last_notification_read.is_none_or(|last_count| last_count.gt(&since));
let send_notification_counts = last_notification_read
.flatten()
.is_none_or(|last_count| last_count.gt(&since));
let notification_count: OptionFuture<_> = send_notification_counts
.then(|| {
services
.user
.pusher
.notification_count(sender_user, room_id)
.map(TryInto::try_into)
.unwrap_or(uint!(0))
@@ -886,7 +1008,7 @@ async fn load_joined_room(
let highlight_count: OptionFuture<_> = send_notification_counts
.then(|| {
services
.user
.pusher
.highlight_count(sender_user, room_id)
.map(TryInto::try_into)
.unwrap_or(uint!(0))
@@ -1121,6 +1243,7 @@ async fn calculate_state_changes<'a>(
.state_accessor
.state_full_shortids(horizon_shortstatehash)
.expect_ok()
.boxed()
.into_future()
})
.into();
@@ -1201,7 +1324,10 @@ async fn calculate_counts(
let small_room = joined_member_count.saturating_add(invited_member_count) <= 5;
let heroes: OptionFuture<_> = small_room
let heroes: OptionFuture<_> = services
.config
.calculate_heroes
.and_is(small_room)
.then(|| calculate_heroes(services, room_id, sender_user))
.into();
@@ -1214,28 +1340,23 @@ async fn calculate_heroes(
sender_user: &UserId,
) -> Vec<OwnedUserId> {
services
.timeline
.all_pdus(sender_user, room_id)
.ready_filter(|(_, pdu)| pdu.kind == RoomMember)
.fold_default(|heroes: Vec<_>, (_, pdu)| {
.state_accessor
.room_state_type_pdus(room_id, &StateEventType::RoomMember)
.ready_filter_map(Result::ok)
.fold_default(|heroes: Vec<_>, pdu| {
fold_hero(heroes, services, room_id, sender_user, pdu)
})
.await
}
async fn fold_hero(
async fn fold_hero<Pdu: Event>(
mut heroes: Vec<OwnedUserId>,
services: &Services,
room_id: &RoomId,
sender_user: &UserId,
pdu: PduEvent,
pdu: Pdu,
) -> Vec<OwnedUserId> {
let Some(user_id): Option<&UserId> = pdu
.state_key
.as_deref()
.map(TryInto::try_into)
.flat_ok()
else {
let Some(user_id): Option<&UserId> = pdu.state_key().map(TryInto::try_into).flat_ok() else {
return heroes;
};
+19 -188
View File
@@ -1,50 +1,35 @@
mod account_data;
mod e2ee;
mod extensions;
mod filter;
mod receipts;
mod room;
mod rooms;
mod selector;
mod to_device;
mod typing;
use std::{collections::BTreeMap, fmt::Debug, time::Duration};
use axum::extract::State;
use futures::{
FutureExt, TryFutureExt, TryStreamExt,
future::{OptionFuture, join, join5, try_join},
FutureExt, TryFutureExt,
future::{join, try_join},
};
use ruma::{
DeviceId, OwnedRoomId, RoomId, UserId,
api::client::sync::sync_events::v5::{
ListId, Request, Response, request::ExtensionRoomConfig, response,
},
DeviceId, OwnedRoomId, UserId,
api::client::sync::sync_events::v5::{ListId, Request, Response, response},
events::room::member::MembershipState,
};
use tokio::time::{Instant, timeout_at};
use tuwunel_core::{
Err, Result, apply, at, debug,
Err, Result, debug,
debug::INFO_SPAN_LEVEL,
debug_warn,
error::inspect_log,
extract_variant,
smallvec::SmallVec,
trace,
utils::{
BoolExt, IterStream, TryFutureExtExt,
result::FlatOk,
stream::{TryBroadbandExt, TryReadyExt},
},
utils::{TryFutureExtExt, result::FlatOk},
};
use tuwunel_service::{
Services,
sync::{Connection, into_connection_key},
};
use self::{
filter::{filter_room, filter_room_meta},
selector::selector,
};
use super::share_encrypted_room;
use crate::Ruma;
@@ -52,7 +37,7 @@ use crate::Ruma;
struct SyncInfo<'a> {
services: &'a Services,
sender_user: &'a UserId,
sender_device: &'a DeviceId,
sender_device: Option<&'a DeviceId>,
}
#[derive(Clone, Debug)]
@@ -84,7 +69,7 @@ type ListIds = SmallVec<[ListId; 1]>;
skip_all,
fields(
user_id = %body.sender_user().localpart(),
device_id = %body.sender_device(),
device_id = %body.sender_device.as_deref().map_or("<no device>", |x| x.as_str()),
conn_id = ?body.body.conn_id.clone().unwrap_or_default(),
since = ?body.body.pos.clone().unwrap_or_default(),
)
@@ -93,7 +78,8 @@ pub(crate) async fn sync_events_v5_route(
State(ref services): State<crate::State>,
body: Ruma<Request>,
) -> Result<Response> {
let (sender_user, sender_device) = body.sender();
let sender_user = body.sender_user();
let sender_device = body.sender_device.as_deref();
let request = &body.body;
let since = request
.pos
@@ -123,7 +109,7 @@ pub(crate) async fn sync_events_v5_route(
let conn = conn_val.lock();
let ping_presence = services
.presence
.maybe_ping_presence(sender_user, &request.set_presence)
.maybe_ping_presence(sender_user, sender_device, &request.set_presence)
.inspect_err(inspect_log)
.ok();
@@ -185,12 +171,15 @@ pub(crate) async fn sync_events_v5_route(
);
conn.next_batch = services.globals.wait_pending().await?;
(window, response.lists) = selector(&mut conn, sync_info).boxed().await;
(window, response.lists) = selector::selector(&mut conn, sync_info)
.boxed()
.await;
if conn.globalsince < conn.next_batch {
let rooms = handle_rooms(sync_info, &conn, &window)
let rooms = rooms::handle(sync_info, &conn, &window)
.map_ok(|response_rooms| response.rooms = response_rooms);
let extensions = handle_extensions(sync_info, &conn, &window)
let extensions = extensions::handle(sync_info, &conn, &window)
.map_ok(|response_extensions| response.extensions = response_extensions);
try_join(rooms, extensions).boxed().await?;
@@ -233,161 +222,3 @@ pub(crate) async fn sync_events_v5_route(
fn is_empty_response(response: &Response) -> bool {
response.extensions.is_empty() && response.rooms.is_empty()
}
#[tracing::instrument(
name = "rooms",
level = "debug",
skip_all,
fields(
next_batch = conn.next_batch,
window = window.len(),
)
)]
async fn handle_rooms(
sync_info: SyncInfo<'_>,
conn: &Connection,
window: &Window,
) -> Result<BTreeMap<OwnedRoomId, response::Room>> {
window
.iter()
.try_stream()
.broad_and_then(async |(room_id, room)| {
room::handle(sync_info, conn, room)
.map_ok(|room| (room_id, room))
.await
})
.ready_try_filter_map(|(room_id, room)| Ok(room.map(|room| (room_id, room))))
.map_ok(|(room_id, room)| (room_id.to_owned(), room))
.try_collect()
.await
}
#[tracing::instrument(
name = "extensions",
level = "debug",
skip_all,
fields(
next_batch = conn.next_batch,
window = window.len(),
rooms = conn.rooms.len(),
subs = conn.subscriptions.len(),
)
)]
async fn handle_extensions(
sync_info: SyncInfo<'_>,
conn: &Connection,
window: &Window,
) -> Result<response::Extensions> {
let SyncInfo { .. } = sync_info;
let account_data: OptionFuture<_> = conn
.extensions
.account_data
.enabled
.unwrap_or(false)
.then(|| account_data::collect(sync_info, conn, window))
.into();
let receipts: OptionFuture<_> = conn
.extensions
.receipts
.enabled
.unwrap_or(false)
.then(|| receipts::collect(sync_info, conn, window))
.into();
let typing: OptionFuture<_> = conn
.extensions
.typing
.enabled
.unwrap_or(false)
.then(|| typing::collect(sync_info, conn, window))
.into();
let to_device: OptionFuture<_> = conn
.extensions
.to_device
.enabled
.unwrap_or(false)
.then(|| to_device::collect(sync_info, conn))
.into();
let e2ee: OptionFuture<_> = conn
.extensions
.e2ee
.enabled
.unwrap_or(false)
.then(|| e2ee::collect(sync_info, conn))
.into();
let (account_data, receipts, typing, to_device, e2ee) =
join5(account_data, receipts, typing, to_device, e2ee)
.map(apply!(5, |t: Option<_>| t.unwrap_or(Ok(Default::default()))))
.await;
Ok(response::Extensions {
account_data: account_data?,
receipts: receipts?,
typing: typing?,
to_device: to_device?,
e2ee: e2ee?,
})
}
#[tracing::instrument(
name = "selector",
level = "trace",
skip_all,
fields(?implicit, ?explicit),
)]
fn extension_rooms_selector<'a, ListIter, SubsIter>(
SyncInfo { .. }: SyncInfo<'a>,
conn: &'a Connection,
window: &'a Window,
implicit: Option<ListIter>,
explicit: Option<SubsIter>,
) -> impl Iterator<Item = &'a RoomId> + Send + Sync + 'a
where
ListIter: Iterator<Item = &'a ListId> + Clone + Debug + Send + Sync + 'a,
SubsIter: Iterator<Item = &'a ExtensionRoomConfig> + Clone + Debug + Send + Sync + 'a,
{
let has_all_subscribed = explicit
.clone()
.into_iter()
.flatten()
.any(|erc| matches!(erc, ExtensionRoomConfig::AllSubscribed));
let all_subscribed = has_all_subscribed
.then(|| conn.subscriptions.keys())
.into_iter()
.flatten()
.map(AsRef::as_ref);
let rooms_explicit = has_all_subscribed
.is_false()
.then(move || {
explicit
.into_iter()
.flatten()
.filter_map(|erc| extract_variant!(erc, ExtensionRoomConfig::Room))
.map(AsRef::as_ref)
})
.into_iter()
.flatten();
let rooms_selected = window
.iter()
.filter(move |(_, room)| {
implicit.as_ref().is_none_or(|lists| {
lists
.clone()
.any(|list| room.lists.contains(list))
})
})
.map(at!(0))
.map(AsRef::as_ref);
all_subscribed
.chain(rooms_explicit)
.chain(rooms_selected)
}
+150
View File
@@ -0,0 +1,150 @@
mod account_data;
mod e2ee;
mod receipts;
mod to_device;
mod typing;
use std::fmt::Debug;
use futures::{
FutureExt,
future::{OptionFuture, join5},
};
use ruma::{
RoomId,
api::client::sync::sync_events::v5::{ListId, request::ExtensionRoomConfig, response},
};
use tuwunel_core::{Result, apply, at, extract_variant, utils::BoolExt};
use tuwunel_service::sync::Connection;
use super::{SyncInfo, Window, share_encrypted_room};
#[tracing::instrument(
name = "extensions",
level = "debug",
skip_all,
fields(
next_batch = conn.next_batch,
window = window.len(),
rooms = conn.rooms.len(),
subs = conn.subscriptions.len(),
)
)]
pub(super) async fn handle(
sync_info: SyncInfo<'_>,
conn: &Connection,
window: &Window,
) -> Result<response::Extensions> {
let SyncInfo { .. } = sync_info;
let account_data: OptionFuture<_> = conn
.extensions
.account_data
.enabled
.unwrap_or(false)
.then(|| account_data::collect(sync_info, conn, window))
.into();
let receipts: OptionFuture<_> = conn
.extensions
.receipts
.enabled
.unwrap_or(false)
.then(|| receipts::collect(sync_info, conn, window))
.into();
let typing: OptionFuture<_> = conn
.extensions
.typing
.enabled
.unwrap_or(false)
.then(|| typing::collect(sync_info, conn, window))
.into();
let to_device: OptionFuture<_> = conn
.extensions
.to_device
.enabled
.unwrap_or(false)
.then(|| to_device::collect(sync_info, conn))
.into();
let e2ee: OptionFuture<_> = conn
.extensions
.e2ee
.enabled
.unwrap_or(false)
.then(|| e2ee::collect(sync_info, conn))
.into();
let (account_data, receipts, typing, to_device, e2ee) =
join5(account_data, receipts, typing, to_device, e2ee)
.map(apply!(5, |t: Option<_>| t.unwrap_or(Ok(Default::default()))))
.await;
Ok(response::Extensions {
account_data: account_data?,
receipts: receipts?,
typing: typing?,
to_device: to_device?,
e2ee: e2ee?,
})
}
#[tracing::instrument(
name = "selector",
level = "trace",
skip_all,
fields(?implicit, ?explicit),
)]
fn selector<'a, ListIter, SubsIter>(
SyncInfo { .. }: SyncInfo<'a>,
conn: &'a Connection,
window: &'a Window,
implicit: Option<ListIter>,
explicit: Option<SubsIter>,
) -> impl Iterator<Item = &'a RoomId> + Send + Sync + 'a
where
ListIter: Iterator<Item = &'a ListId> + Clone + Debug + Send + Sync + 'a,
SubsIter: Iterator<Item = &'a ExtensionRoomConfig> + Clone + Debug + Send + Sync + 'a,
{
let has_all_subscribed = explicit
.clone()
.into_iter()
.flatten()
.any(|erc| matches!(erc, ExtensionRoomConfig::AllSubscribed));
let all_subscribed = has_all_subscribed
.then(|| conn.subscriptions.keys())
.into_iter()
.flatten()
.map(AsRef::as_ref);
let rooms_explicit = has_all_subscribed
.is_false()
.then(move || {
explicit
.into_iter()
.flatten()
.filter_map(|erc| extract_variant!(erc, ExtensionRoomConfig::Room))
.map(AsRef::as_ref)
})
.into_iter()
.flatten();
let rooms_selected = window
.iter()
.filter(move |(_, room)| {
implicit.as_ref().is_none_or(|lists| {
lists
.clone()
.any(|list| room.lists.contains(list))
})
})
.map(at!(0))
.map(AsRef::as_ref);
all_subscribed
.chain(rooms_explicit)
.chain(rooms_selected)
}
@@ -6,7 +6,7 @@ use tuwunel_core::{
};
use tuwunel_service::sync::Room;
use super::{Connection, SyncInfo, Window, extension_rooms_selector};
use super::{Connection, SyncInfo, Window, selector};
#[tracing::instrument(name = "account_data", level = "trace", skip_all)]
pub(super) async fn collect(
@@ -30,7 +30,7 @@ pub(super) async fn collect(
.as_deref()
.map(<[_]>::iter);
let rooms = extension_rooms_selector(sync_info, conn, window, implicit, explicit)
let rooms = selector(sync_info, conn, window, implicit, explicit)
.stream()
.broad_filter_map(async |room_id| {
let &Room { roomsince, .. } = conn.rooms.get(room_id)?;
@@ -32,6 +32,9 @@ pub(super) async fn collect(
conn: &Connection,
) -> Result<response::E2EE> {
let SyncInfo { services, sender_user, sender_device, .. } = sync_info;
let Some(sender_device) = sender_device else {
return Ok(response::E2EE::default());
};
let keys_changed = services
.users
@@ -170,6 +173,7 @@ async fn collect_room(
.ready_filter(|&user_id| user_id != sender_user)
.map(ToOwned::to_owned)
.map(|user_id| (MembershipState::Join, user_id))
.boxed()
.into_future()
})
.into();
@@ -11,7 +11,7 @@ use tuwunel_core::{
};
use tuwunel_service::{rooms::read_receipt::pack_receipts, sync::Room};
use super::{Connection, SyncInfo, Window, extension_rooms_selector};
use super::{Connection, SyncInfo, Window, selector};
#[tracing::instrument(name = "receipts", level = "trace", skip_all)]
pub(super) async fn collect(
@@ -35,7 +35,7 @@ pub(super) async fn collect(
.as_deref()
.map(<[_]>::iter);
let rooms = extension_rooms_selector(sync_info, conn, window, implicit, explicit)
let rooms = selector(sync_info, conn, window, implicit, explicit)
.stream()
.broad_filter_map(|room_id| collect_room(sync_info, conn, window, room_id))
.collect()
@@ -1,6 +1,6 @@
use futures::StreamExt;
use ruma::api::client::sync::sync_events::v5::response;
use tuwunel_core::{self, Result};
use tuwunel_core::{self, Result, at};
use super::{Connection, SyncInfo};
@@ -9,6 +9,10 @@ pub(super) async fn collect(
SyncInfo { services, sender_user, sender_device, .. }: SyncInfo<'_>,
conn: &Connection,
) -> Result<Option<response::ToDevice>> {
let Some(sender_device) = sender_device else {
return Ok(None);
};
services
.users
.remove_to_device_events(sender_user, sender_device, conn.globalsince)
@@ -17,6 +21,7 @@ pub(super) async fn collect(
let events: Vec<_> = services
.users
.get_to_device_events(sender_user, sender_device, None, Some(conn.next_batch))
.map(at!(1))
.collect()
.await;
@@ -11,7 +11,7 @@ use tuwunel_core::{
utils::{IterStream, ReadyExt},
};
use super::{Connection, SyncInfo, Window, extension_rooms_selector};
use super::{Connection, SyncInfo, Window, selector};
#[tracing::instrument(name = "typing", level = "trace", skip_all, ret)]
pub(super) async fn collect(
@@ -37,7 +37,7 @@ pub(super) async fn collect(
.as_deref()
.map(<[_]>::iter);
extension_rooms_selector(sync_info, conn, window, implicit, explicit)
selector(sync_info, conn, window, implicit, explicit)
.stream()
.filter_map(async |room_id| {
services
+4 -4
View File
@@ -7,7 +7,7 @@ use tuwunel_core::{
is_equal_to, is_true,
utils::{
BoolExt, FutureBoolExt, IterStream, ReadyExt,
future::{self, OptionExt, ReadyEqExt},
future::{self, OptionExt, ReadyBoolExt},
},
};
@@ -132,7 +132,7 @@ pub(super) async fn filter_room_meta(
SyncInfo { services, sender_user, .. }: SyncInfo<'_>,
room_id: &RoomId,
) -> bool {
let not_exists = services.metadata.exists(room_id).eq(&false);
let not_exists = services.metadata.exists(room_id).is_false();
let is_disabled = services.metadata.is_disabled(room_id);
@@ -141,13 +141,13 @@ pub(super) async fn filter_room_meta(
let not_visible = services
.state_accessor
.user_can_see_state_events(sender_user, room_id)
.eq(&false);
.is_false();
pin_mut!(not_visible, not_exists, is_disabled, is_banned);
not_visible
.or(not_exists)
.or(is_disabled)
.or(is_banned)
.is_false()
.await
.eq(&false)
}
@@ -1,11 +1,14 @@
use std::{cmp::Ordering, collections::HashSet};
use std::{
cmp::Ordering,
collections::{BTreeMap, HashSet},
};
use futures::{
FutureExt, StreamExt, TryFutureExt, TryStreamExt,
future::{OptionFuture, join, join3, join4},
};
use ruma::{
JsOption, MxcUri, OwnedMxcUri, RoomId, UserId,
JsOption, MxcUri, OwnedMxcUri, OwnedRoomId, RoomId, UserId,
api::client::sync::sync_events::{
UnreadNotificationsCount,
v5::{DisplayName, response, response::Heroes},
@@ -19,7 +22,7 @@ use ruma::{
},
};
use tuwunel_core::{
Result, at, err, is_equal_to,
Result, at, err, error, is_equal_to,
matrix::{Event, StateKey, pdu::PduCount},
ref_at,
utils::{
@@ -29,12 +32,41 @@ use tuwunel_core::{
};
use tuwunel_service::{Services, sync::Room};
use super::{super::load_timeline, Connection, SyncInfo, WindowRoom};
use super::{super::load_timeline, Connection, SyncInfo, Window, WindowRoom};
use crate::client::ignored_filter;
static DEFAULT_BUMP_TYPES: [TimelineEventType; 6] =
[CallInvite, PollStart, Beacon, RoomEncrypted, RoomMessage, Sticker];
#[tracing::instrument(
name = "rooms",
level = "debug",
skip_all,
fields(
next_batch = conn.next_batch,
window = window.len(),
)
)]
pub(super) async fn handle(
sync_info: SyncInfo<'_>,
conn: &Connection,
window: &Window,
) -> Result<BTreeMap<OwnedRoomId, response::Room>> {
window
.iter()
.stream()
.broad_filter_map(async |(room_id, room)| {
handle_room(sync_info, conn, room)
.map_ok(move |room| (room_id.clone(), room))
.inspect_err(|e| error!(?room_id, "sync handler: {e:?}"))
.await
.ok()
})
.collect()
.map(Ok)
.await
}
#[tracing::instrument(
name = "room",
level = "debug",
@@ -42,13 +74,13 @@ static DEFAULT_BUMP_TYPES: [TimelineEventType; 6] =
fields(room_id, roomsince)
)]
#[allow(clippy::too_many_arguments)]
pub(super) async fn handle(
async fn handle_room(
SyncInfo { services, sender_user, .. }: SyncInfo<'_>,
conn: &Connection,
WindowRoom {
lists, membership, room_id, last_count, ..
}: &WindowRoom,
) -> Result<Option<response::Room>> {
) -> Result<response::Room> {
debug_assert!(
DEFAULT_BUMP_TYPES.is_sorted(),
"DEFAULT_BUMP_TYPES must be sorted for binary search"
@@ -65,7 +97,7 @@ pub(super) async fn handle(
);
if *membership == Some(MembershipState::Leave) {
return Ok(Some(response::Room {
return Ok(response::Room {
initial: roomsince.eq(&0).then_some(true),
lists: lists.clone(),
membership: membership.clone(),
@@ -80,7 +112,7 @@ pub(super) async fn handle(
],
..Default::default()
}));
});
}
let is_invite = *membership == Some(MembershipState::Invite);
@@ -143,9 +175,9 @@ pub(super) async fn handle(
.binary_search(pdu.event_type())
.is_ok()
})
.filter(|(_, pdu)| !pdu.is_redacted())
.map(at!(0))
.filter(|count| matches!(count, PduCount::Normal(_)))
.map(PduCount::into_unsigned)
.map(PduCount::into_signed)
.max()
.map(TryInto::try_into)
.flat_ok();
@@ -240,13 +272,13 @@ pub(super) async fn handle(
.map(Option::flatten);
let highlight_count = services
.user
.pusher
.highlight_count(sender_user, room_id)
.map(TryInto::try_into)
.map(Result::ok);
let notification_count = services
.user
.pusher
.notification_count(sender_user, room_id)
.map(TryInto::try_into)
.map(Result::ok);
@@ -271,7 +303,7 @@ pub(super) async fn handle(
.map(|is_dm| is_dm.then_some(is_dm));
let last_read_count = services
.user
.pusher
.last_notification_read(sender_user, room_id);
let timeline = timeline_pdus
@@ -295,20 +327,27 @@ pub(super) async fn handle(
.boxed()
.await;
let (heroes, hero_name, heroes_avatar) = calculate_heroes(
services,
sender_user,
room_id,
room_name.as_ref(),
room_avatar.as_deref(),
)
.await?;
let heroes: OptionFuture<_> = services
.config
.calculate_heroes
.then(|| {
calculate_heroes(
services,
sender_user,
room_id,
room_name.as_ref(),
room_avatar.as_deref(),
)
})
.into();
Ok(Some(response::Room {
let (heroes, heroes_name, heroes_avatar) = heroes.await.unwrap_or_default();
Ok(response::Room {
initial: roomsince.eq(&0).then_some(true),
lists: lists.clone(),
membership: membership.clone(),
name: room_name.or(hero_name),
name: room_name.or(heroes_name),
avatar: JsOption::from_option(room_avatar.or(heroes_avatar)),
is_dm,
heroes,
@@ -322,7 +361,7 @@ pub(super) async fn handle(
joined_count,
invited_count,
unread_notifications: UnreadNotificationsCount { highlight_count, notification_count },
}))
})
}
#[tracing::instrument(name = "heroes", level = "trace", skip_all)]
@@ -333,8 +372,9 @@ async fn calculate_heroes(
room_id: &RoomId,
room_name: Option<&DisplayName>,
room_avatar: Option<&MxcUri>,
) -> Result<(Option<Heroes>, Option<DisplayName>, Option<OwnedMxcUri>)> {
) -> (Option<Heroes>, Option<DisplayName>, Option<OwnedMxcUri>) {
const MAX_HEROES: usize = 5;
let heroes: Heroes = services
.state_cache
.room_members(room_id)
@@ -411,5 +451,5 @@ async fn calculate_heroes(
})
.flatten();
Ok((Some(heroes), hero_name, heroes_avatar))
(Some(heroes), hero_name, heroes_avatar)
}
+111 -113
View File
@@ -10,8 +10,7 @@ use tuwunel_core::{
matrix::PduCount,
trace,
utils::{
BoolExt,
future::TryExtExt,
BoolExt, TryFutureExtExt,
math::usize_from_ruma,
stream::{BroadbandExt, IterStream},
},
@@ -19,7 +18,8 @@ use tuwunel_core::{
use tuwunel_service::sync::Connection;
use super::{
ListIds, ResponseLists, SyncInfo, Window, WindowRoom, filter_room, filter_room_meta,
ListIds, ResponseLists, SyncInfo, Window, WindowRoom,
filter::{filter_room, filter_room_meta},
};
#[tracing::instrument(level = "debug", skip_all)]
@@ -35,9 +35,7 @@ pub(super) async fn selector(
.state_cache
.user_memberships(sender_user, Some(&[Join, Invite, Knock]))
.map(|(membership, room_id)| (room_id.to_owned(), Some(membership)))
.broad_filter_map(|(room_id, membership)| {
match_lists_for_room(sync_info, conn, room_id, membership)
})
.broad_filter_map(|(room_id, membership)| matcher(sync_info, conn, room_id, membership))
.collect::<Vec<_>>()
.await;
@@ -57,19 +55,123 @@ pub(super) async fn selector(
let lists = response_lists(rooms.iter());
trace!(?lists);
let window = select_window(sync_info, conn, rooms.iter(), &lists).await;
let window = window(sync_info, conn, rooms.iter(), &lists).await;
trace!(?window);
(window, lists)
}
#[tracing::instrument(
name = "window",
name = "matcher",
level = "trace",
skip_all,
fields(?room_id, ?membership)
)]
async fn matcher(
sync_info: SyncInfo<'_>,
conn: &Connection,
room_id: OwnedRoomId,
membership: Option<MembershipState>,
) -> Option<WindowRoom> {
let SyncInfo { services, sender_user, .. } = sync_info;
let (matched, lists) = conn
.lists
.iter()
.stream()
.filter_map(async |(id, list)| {
let filter: OptionFuture<_> = list
.filters
.clone()
.map(async |filters| {
filter_room(sync_info, &filters, &room_id, membership.as_ref()).await
})
.into();
filter
.await
.is_none_or(is_true!())
.then(|| id.clone())
})
.collect::<ListIds>()
.map(|lists| (lists.is_empty().is_false(), lists))
.await;
let last_notification: OptionFuture<_> = matched
.then(|| {
services
.pusher
.last_notification_read(sender_user, &room_id)
.unwrap_or_default()
})
.into();
let last_privateread: OptionFuture<_> = matched
.then(|| {
services
.read_receipt
.last_privateread_update(sender_user, &room_id)
})
.into();
let last_receipt: OptionFuture<_> = matched
.then(|| {
services
.read_receipt
.last_receipt_count(&room_id, sender_user.into(), None)
.unwrap_or_default()
})
.into();
let last_account: OptionFuture<_> = matched
.then(|| {
services
.account_data
.last_count(Some(room_id.as_ref()), sender_user, Some(conn.next_batch))
.unwrap_or_default()
})
.into();
let last_timeline: OptionFuture<_> = matched
.then(|| {
services
.timeline
.last_timeline_count(None, &room_id, Some(conn.next_batch.into()))
.map_ok(PduCount::into_unsigned)
.unwrap_or_default()
})
.into();
let (last_timeline, last_notification, last_account, last_receipt, last_privateread) =
join5(last_timeline, last_notification, last_account, last_receipt, last_privateread)
.await;
Some(WindowRoom {
room_id: room_id.clone(),
membership,
lists,
ranked: 0,
last_count: [
last_timeline,
last_notification,
last_account,
last_receipt,
last_privateread,
]
.into_iter()
.map(Option::unwrap_or_default)
.filter(|count| conn.next_batch.ge(count))
.max()
.unwrap_or_default(),
})
}
#[tracing::instrument(
level = "debug",
skip_all,
fields(rooms = rooms.clone().count())
)]
async fn select_window<'a, Rooms>(
async fn window<'a, Rooms>(
sync_info: SyncInfo<'_>,
conn: &Connection,
rooms: Rooms,
@@ -142,110 +244,6 @@ where
subscriptions.chain(selections).collect().await
}
#[tracing::instrument(
name = "matcher",
level = "trace",
skip_all,
fields(?room_id, ?membership)
)]
async fn match_lists_for_room(
sync_info: SyncInfo<'_>,
conn: &Connection,
room_id: OwnedRoomId,
membership: Option<MembershipState>,
) -> Option<WindowRoom> {
let SyncInfo { services, sender_user, .. } = sync_info;
let (matched, lists) = conn
.lists
.iter()
.stream()
.filter_map(async |(id, list)| {
let filter: OptionFuture<_> = list
.filters
.clone()
.map(async |filters| {
filter_room(sync_info, &filters, &room_id, membership.as_ref()).await
})
.into();
filter
.await
.is_none_or(is_true!())
.then(|| id.clone())
})
.collect::<ListIds>()
.map(|lists| (lists.is_empty().is_false(), lists))
.await;
let last_notification: OptionFuture<_> = matched
.then(|| {
services
.user
.last_notification_read(sender_user, &room_id)
})
.into();
let last_privateread: OptionFuture<_> = matched
.then(|| {
services
.read_receipt
.last_privateread_update(sender_user, &room_id)
})
.into();
let last_receipt: OptionFuture<_> = matched
.then(|| {
services
.read_receipt
.last_receipt_count(&room_id, sender_user.into(), None)
.unwrap_or_default()
})
.into();
let last_account: OptionFuture<_> = matched
.then(|| {
services
.account_data
.last_count(Some(room_id.as_ref()), sender_user, Some(conn.next_batch))
.unwrap_or_default()
})
.into();
let last_timeline: OptionFuture<_> = matched
.then(|| {
services
.timeline
.last_timeline_count(None, &room_id, Some(conn.next_batch.into()))
.map_ok(PduCount::into_unsigned)
.unwrap_or_default()
})
.into();
let (last_timeline, last_notification, last_account, last_receipt, last_privateread) =
join5(last_timeline, last_notification, last_account, last_receipt, last_privateread)
.await;
Some(WindowRoom {
room_id: room_id.clone(),
membership,
lists,
ranked: 0,
last_count: [
last_timeline,
last_notification,
last_account,
last_receipt,
last_privateread,
]
.into_iter()
.map(Option::unwrap_or_default)
.filter(|count| conn.next_batch.ge(count))
.max()
.unwrap_or_default(),
})
}
fn response_lists<'a, Rooms>(rooms: Rooms) -> ResponseLists
where
Rooms: Iterator<Item = &'a WindowRoom>,
+5 -1
View File
@@ -66,7 +66,11 @@ pub(crate) async fn create_typing_event_route(
// ping presence
services
.presence
.maybe_ping_presence(&body.user_id, &ruma::presence::PresenceState::Online)
.maybe_ping_presence(
&body.user_id,
body.sender_device.as_deref(),
&ruma::presence::PresenceState::Online,
)
.await?;
Ok(create_typing_event::v3::Response {})
+8 -8
View File
@@ -76,7 +76,7 @@ pub(crate) async fn delete_timezone_key_route(
// Presence update
services
.presence
.maybe_ping_presence(&body.user_id, &PresenceState::Online)
.maybe_ping_presence(&body.user_id, body.sender_device.as_deref(), &PresenceState::Online)
.await?;
Ok(delete_timezone_key::unstable::Response {})
@@ -104,7 +104,7 @@ pub(crate) async fn set_timezone_key_route(
// Presence update
services
.presence
.maybe_ping_presence(&body.user_id, &PresenceState::Online)
.maybe_ping_presence(&body.user_id, body.sender_device.as_deref(), &PresenceState::Online)
.await?;
Ok(set_timezone_key::unstable::Response {})
@@ -170,7 +170,7 @@ pub(crate) async fn set_profile_field_route(
// Presence update
services
.presence
.maybe_ping_presence(&body.user_id, &PresenceState::Online)
.maybe_ping_presence(&body.user_id, body.sender_device.as_deref(), &PresenceState::Online)
.await?;
Ok(set_profile_field::v3::Response {})
@@ -224,7 +224,7 @@ pub(crate) async fn delete_profile_field_route(
// Presence update
services
.presence
.maybe_ping_presence(&body.user_id, &PresenceState::Online)
.maybe_ping_presence(&body.user_id, body.sender_device.as_deref(), &PresenceState::Online)
.await?;
Ok(delete_profile_field::v3::Response {})
@@ -243,8 +243,8 @@ pub(crate) async fn get_timezone_key_route(
if !services.globals.user_is_local(&body.user_id) {
// Create and update our local copy of the user
if let Ok(response) = services
.sending
.send_federation_request(
.federation
.execute(
body.user_id.server_name(),
federation::query::get_profile_information::v1::Request {
user_id: body.user_id.clone(),
@@ -304,8 +304,8 @@ pub(crate) async fn get_profile_field_route(
if !services.globals.user_is_local(&body.user_id) {
// Create and update our local copy of the user
if let Ok(response) = services
.sending
.send_federation_request(
.federation
.execute(
body.user_id.server_name(),
federation::query::get_profile_information::v1::Request {
user_id: body.user_id.clone(),
+7 -4
View File
@@ -37,22 +37,25 @@ pub(crate) async fn get_supported_versions_route(
"v1.3".to_owned(),
"v1.4".to_owned(),
"v1.5".to_owned(),
"v1.11".to_owned(),
"v1.10".to_owned(), // relations recursion
"v1.11".to_owned(), // authenticated media
],
unstable_features: BTreeMap::from_iter([
("org.matrix.e2e_cross_signing".to_owned(), true),
("org.matrix.msc2285.stable".to_owned(), true), /* private read receipts (https://github.com/matrix-org/matrix-spec-proposals/pull/2285) */
("fi.mau.msc2659.stable".to_owned(), true), /* appservice ping https://github.com/matrix-org/matrix-spec-proposals/pull/2659) */
("uk.half-shot.msc2666.query_mutual_rooms".to_owned(), true), /* query mutual rooms (https://github.com/matrix-org/matrix-spec-proposals/pull/2666) */
("org.matrix.msc2836".to_owned(), true), /* threading/threads (https://github.com/matrix-org/matrix-spec-proposals/pull/2836) */
("org.matrix.msc2946".to_owned(), true), /* spaces/hierarchy summaries (https://github.com/matrix-org/matrix-spec-proposals/pull/2946) */
("org.matrix.msc3026.busy_presence".to_owned(), true), /* busy presence status (https://github.com/matrix-org/matrix-spec-proposals/pull/3026) */
("org.matrix.msc3827".to_owned(), true), /* filtering of /publicRooms by room type (https://github.com/matrix-org/matrix-spec-proposals/pull/3827) */
("org.matrix.msc3952_intentional_mentions".to_owned(), true), /* intentional mentions (https://github.com/matrix-org/matrix-spec-proposals/pull/3952) */
("org.matrix.msc3575".to_owned(), true), /* sliding sync (https://github.com/matrix-org/matrix-spec-proposals/pull/3575/files#r1588877046) */
("org.matrix.msc3814".to_owned(), true), /* dehydrated devices */
("org.matrix.msc3827".to_owned(), true), /* filtering of /publicRooms by room type (https://github.com/matrix-org/matrix-spec-proposals/pull/3827) */
("org.matrix.msc3916.stable".to_owned(), true), /* authenticated media (https://github.com/matrix-org/matrix-spec-proposals/pull/3916) */
("org.matrix.msc4180".to_owned(), true), /* stable flag for 3916 (https://github.com/matrix-org/matrix-spec-proposals/pull/4180) */
("org.matrix.msc3952_intentional_mentions".to_owned(), true), /* intentional mentions (https://github.com/matrix-org/matrix-spec-proposals/pull/3952) */
("uk.tcpip.msc4133".to_owned(), true), /* Extending User Profile API with Key:Value Pairs (https://github.com/matrix-org/matrix-spec-proposals/pull/4133) */
("us.cloke.msc4175".to_owned(), true), /* Profile field for user time zone (https://github.com/matrix-org/matrix-spec-proposals/pull/4175) */
("org.matrix.msc4180".to_owned(), true), /* stable flag for 3916 (https://github.com/matrix-org/matrix-spec-proposals/pull/4180) */
("org.matrix.simplified_msc3575".to_owned(), true), /* Simplified Sliding sync (https://github.com/matrix-org/matrix-spec-proposals/pull/4186) */
]),
};
+2 -1
View File
@@ -34,7 +34,7 @@ pub(crate) async fn search_users_route(
.min(LIMIT_MAX);
let search_term = body.search_term.to_lowercase();
let mut users = services
let users = services
.users
.stream()
.ready_filter(|&user_id| user_id != sender_user)
@@ -83,6 +83,7 @@ pub(crate) async fn search_users_route(
})
});
pin_mut!(users);
let results = users.by_ref().take(limit).collect().await;
let limited = users.next().await.is_some();
+2 -2
View File
@@ -25,9 +25,9 @@ pub(crate) async fn turn_server_route(
return Err!(Request(NotFound("Not Found")));
}
let turn_secret = &services.config.turn_secret;
let turn_secret = &services.globals.turn_secret;
let (username, password) = if !turn_secret.is_empty() {
let (username, password) = if let Some(turn_secret) = turn_secret {
let expiry = SecondsSinceUnixEpoch::from_system_time(
SystemTime::now()
.checked_add(Duration::from_secs(services.config.turn_ttl))
+39 -8
View File
@@ -1,28 +1,59 @@
use axum::{Json, extract::State, response::IntoResponse};
use ruma::api::client::discovery::{
discover_homeserver::{self, HomeserverInfo},
discover_homeserver::{self, HomeserverInfo, RtcFocusInfo},
discover_support::{self, Contact},
};
use tuwunel_core::{Err, Result};
use serde_json::Value as JsonValue;
use tuwunel_core::{Err, Result, err, error::inspect_log};
use crate::Ruma;
/// # `GET /.well-known/matrix/client`
///
/// Returns the .well-known URL if it is configured, otherwise returns 404.
/// Also includes RTC transport configuration for Element Call (MSC4143).
pub(crate) async fn well_known_client(
State(services): State<crate::State>,
_body: Ruma<discover_homeserver::Request>,
) -> Result<discover_homeserver::Response> {
let client_url = match services.server.config.well_known.client.as_ref() {
| Some(url) => url.to_string(),
| None => return Err!(Request(NotFound("Not found."))),
let homeserver = HomeserverInfo {
base_url: match services.server.config.well_known.client.as_ref() {
| Some(url) => url.to_string(),
| None => return Err!(Request(NotFound("Not found."))),
},
};
// Add RTC transport configuration if available (MSC4143 / Element Call)
// Element Call has evolved through several versions with different field
// expectations
let rtc_foci = services
.server
.config
.well_known
.rtc_transports
.iter()
.map(|transport| {
let focus_type = transport
.get("type")
.and_then(JsonValue::as_str)
.ok_or_else(|| err!("`type` is not a valid string"))?;
let transport = transport
.as_object()
.cloned()
.ok_or_else(|| err!("`rtc_transport` is not a valid object"))?;
RtcFocusInfo::new(focus_type, transport).map_err(Into::into)
})
.collect::<Result<_>>()
.map_err(|e| {
err!(Config("global.well_known.rtc_transports", "Malformed value(s): {e:?}"))
})
.inspect_err(inspect_log)?;
Ok(discover_homeserver::Response {
homeserver: HomeserverInfo { base_url: client_url },
identity_server: None,
tile_server: None,
rtc_foci,
..discover_homeserver::Response::new(homeserver)
})
}
+9 -1
View File
@@ -47,6 +47,7 @@ pub fn build(router: Router<State>, server: &Server) -> Router<State> {
.ruma_route(&client::request_3pid_management_token_via_email_route)
.ruma_route(&client::request_3pid_management_token_via_msisdn_route)
.ruma_route(&client::check_registration_token_validity)
.ruma_route(&client::get_notifications_route)
.ruma_route(&client::get_capabilities_route)
.ruma_route(&client::get_pushrules_all_route)
.ruma_route(&client::get_pushrules_global_route)
@@ -145,6 +146,7 @@ pub fn build(router: Router<State>, server: &Server) -> Router<State> {
get(client::get_state_events_for_empty_key_route)
.put(client::send_state_event_for_empty_key_route),
)
.ruma_route(&client::events_route)
.ruma_route(&client::sync_events_route)
.ruma_route(&client::sync_events_v5_route)
.ruma_route(&client::get_context_route)
@@ -163,6 +165,10 @@ pub fn build(router: Router<State>, server: &Server) -> Router<State> {
.ruma_route(&client::update_device_route)
.ruma_route(&client::delete_device_route)
.ruma_route(&client::delete_devices_route)
.ruma_route(&client::put_dehydrated_device_route)
.ruma_route(&client::delete_dehydrated_device_route)
.ruma_route(&client::get_dehydrated_device_route)
.ruma_route(&client::get_dehydrated_events_route)
.ruma_route(&client::get_tags_route)
.ruma_route(&client::update_tag_route)
.ruma_route(&client::delete_tag_route)
@@ -189,6 +195,9 @@ pub fn build(router: Router<State>, server: &Server) -> Router<State> {
.ruma_route(&client::room_initial_sync_route)
.route("/client/server.json", get(client::syncv3_client_server_json));
// SS endpoint not related to federation
router = router.ruma_route(&server::get_openid_userinfo_route);
if config.allow_federation {
router = router
.ruma_route(&server::get_server_version_route)
@@ -220,7 +229,6 @@ pub fn build(router: Router<State>, server: &Server) -> Router<State> {
.ruma_route(&server::get_profile_information_route)
.ruma_route(&server::get_keys_route)
.ruma_route(&server::claim_keys_route)
.ruma_route(&server::get_openid_userinfo_route)
.ruma_route(&server::get_hierarchy_route)
.ruma_route(&server::well_known_server)
.ruma_route(&server::get_content_route)
+13 -24
View File
@@ -6,7 +6,7 @@ use ruma::{
CanonicalJsonObject, CanonicalJsonValue, DeviceId, OwnedDeviceId, OwnedServerName,
OwnedUserId, ServerName, UserId, api::IncomingRequest,
};
use tuwunel_core::{Error, Result, debug, debug_warn, err, trace, utils::string::EMPTY};
use tuwunel_core::{Error, Result, debug_warn, err, trace, utils::string::EMPTY};
use tuwunel_service::{Services, appservice::RegistrationInfo};
use super::{auth, auth::Auth, request, request::Request};
@@ -40,11 +40,6 @@ pub(crate) struct Args<T> {
}
impl<T> Args<T> {
#[inline]
pub(crate) fn sender(&self) -> (&UserId, &DeviceId) {
(self.sender_user(), self.sender_device())
}
#[inline]
pub(crate) fn sender_user(&self) -> &UserId {
self.sender_user
@@ -52,19 +47,19 @@ impl<T> Args<T> {
.expect("user must be authenticated for this handler")
}
#[inline]
pub(crate) fn sender_device(&self) -> &DeviceId {
self.sender_device
.as_deref()
.expect("user must be authenticated and device identified")
}
#[inline]
pub(crate) fn origin(&self) -> &ServerName {
self.origin
.as_deref()
.expect("server must be authenticated for this handler")
}
#[inline]
pub(crate) fn sender_device(&self) -> Result<&DeviceId> {
self.sender_device
.as_deref()
.ok_or(err!(Request(Forbidden("user must be authenticated and device identified"))))
}
}
impl<T> Deref for Args<T>
@@ -87,7 +82,7 @@ where
level = "debug",
skip(services),
err(level = "debug")
ret,
ret(level = "trace"),
)]
async fn from_request(
request: hyper::Request<Body>,
@@ -95,6 +90,7 @@ where
) -> Result<Self, Self::Rejection> {
let mut request = request::from(services, request).await?;
let mut json_body = serde_json::from_slice::<CanonicalJsonValue>(&request.body).ok();
trace!(?request);
// while very unusual and really shouldn't be recommended, Synapse accepts POST
// requests with a completely empty body. very old clients, libraries, and some
@@ -104,13 +100,13 @@ where
&& request.parts.method == http::Method::POST
&& !request.parts.uri.path().contains("/media/")
{
trace!("json_body from_request: {:?}", json_body.clone());
debug_warn!(
"received a POST request with an empty body, defaulting/assuming to {{}} like \
Synapse does"
);
json_body = Some(CanonicalJsonValue::Object(CanonicalJsonObject::new()));
}
let auth = auth::auth(services, &mut request, json_body.as_ref(), &T::METADATA).await?;
Ok(Self {
body: make_body::<T>(services, &mut request, json_body.as_mut(), &auth)?,
@@ -147,16 +143,9 @@ fn into_http_request(request: &Request, body: Bytes) -> hyper::Request<Bytes> {
.headers_mut()
.expect("mutable http headers") = request.parts.headers.clone();
let http_request = http_request
.body(body)
.expect("http request body");
let headers = http_request.headers();
let method = http_request.method();
let uri = http_request.uri();
debug!("{method:?} {uri:?} {headers:?}");
http_request
.body(body)
.expect("http request body")
}
#[allow(clippy::needless_pass_by_value)]
+1 -1
View File
@@ -19,7 +19,7 @@ pub(super) async fn auth_appservice(
let Ok(user_id) = request
.query
.user_id
.clone()
.as_deref()
.map_or_else(user_id_default, OwnedUserId::parse)
else {
return Err!(Request(InvalidUsername("Username is invalid.")));
+11 -7
View File
@@ -1,6 +1,10 @@
use axum::RequestPartsExt;
use axum_extra::{TypedHeader, headers::Authorization, typed_header::TypedHeaderRejectionReason};
use ruma::{CanonicalJsonObject, CanonicalJsonValue, api::federation::authentication::XMatrix};
use http::uri::PathAndQuery;
use ruma::{
CanonicalJsonName, CanonicalJsonObject, CanonicalJsonValue,
api::federation::authentication::XMatrix,
};
use tuwunel_core::{Err, Result, debug_error, err, warn};
use tuwunel_service::{
Services,
@@ -14,7 +18,7 @@ pub(super) async fn auth_server(
request: &mut Request,
body: Option<&CanonicalJsonValue>,
) -> Result<Auth> {
type Member = (String, CanonicalJsonValue);
type Member = (CanonicalJsonName, CanonicalJsonValue);
type Object = CanonicalJsonObject;
type Value = CanonicalJsonValue;
@@ -27,8 +31,8 @@ pub(super) async fn auth_server(
.parts
.uri
.path_and_query()
.expect("all requests have a path")
.to_string();
.map(PathAndQuery::as_str)
.unwrap_or("/");
let signature: [Member; 1] =
[(x_matrix.key.as_str().into(), Value::String(x_matrix.sig.to_string()))];
@@ -42,7 +46,7 @@ pub(super) async fn auth_server(
("method".into(), Value::String(request.parts.method.as_str().into())),
("origin".into(), Value::String(origin.as_str().into())),
("signatures".into(), Value::Object(signatures.into())),
("uri".into(), Value::String(signature_uri)),
("uri".into(), Value::String(signature_uri.into())),
];
authorization.into()
@@ -52,7 +56,7 @@ pub(super) async fn auth_server(
("method".into(), Value::String(request.parts.method.as_str().into())),
("origin".into(), Value::String(origin.as_str().into())),
("signatures".into(), Value::Object(signatures.into())),
("uri".into(), Value::String(signature_uri)),
("uri".into(), Value::String(signature_uri.into())),
];
authorization.into()
@@ -66,7 +70,7 @@ pub(super) async fn auth_server(
err!(Request(Forbidden(debug_warn!("Failed to fetch signing keys: {e}"))))
})?;
let keys: PubKeys = [(x_matrix.key.to_string(), key.key)].into();
let keys: PubKeys = [(x_matrix.key.as_str().into(), key.key)].into();
let keys: PubKeyMap = [(origin.as_str().into(), keys)].into();
if let Err(e) = ruma::signatures::verify_json(&keys, &authorization) {
debug_error!("Failed to verify federation request from {origin}: {e}");
+4 -2
View File
@@ -14,6 +14,8 @@ pub(crate) async fn auth_uiaa<T>(services: &Services, body: &Ruma<T>) -> Result<
where
T: IncomingRequest + Send + Sync,
{
let sender_device = body.sender_device()?;
let flows = [
AuthFlow::new([AuthType::Password].into()),
AuthFlow::new([AuthType::Jwt].into()),
@@ -51,7 +53,7 @@ where
let (worked, uiaainfo) = services
.uiaa
.try_auth(sender_user, body.sender_device(), auth, &uiaainfo)
.try_auth(sender_user, sender_device, auth, &uiaainfo)
.await?;
if !worked {
@@ -71,7 +73,7 @@ where
uiaainfo.session = Some(utils::random_string(SESSION_ID_LENGTH));
services
.uiaa
.create(sender_user, body.sender_device(), &uiaainfo, json);
.create(sender_user, sender_device, &uiaainfo, json);
Err(Error::Uiaa(uiaainfo))
},
+9 -4
View File
@@ -9,7 +9,7 @@ use axum::{
use futures::{Future, TryFutureExt};
use http::Method;
use ruma::api::IncomingRequest;
use tuwunel_core::Result;
use tuwunel_core::{Result, trace};
use super::{Ruma, RumaResponse, State};
@@ -41,8 +41,8 @@ macro_rules! ruma_handler {
Fun: Fn($($tx,)* Ruma<Req>,) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Result<Req::OutgoingResponse, Err>> + Send,
Req: IncomingRequest + Debug + Send + Sync + 'static,
Err: IntoResponse + Send,
<Req as IncomingRequest>::OutgoingResponse: Send,
Err: IntoResponse + Debug + Send,
<Req as IncomingRequest>::OutgoingResponse: Debug + Send,
$( $tx: FromRequestParts<State> + Send + Sync + 'static, )*
{
fn add_routes(&'static self, router: Router<State>) -> Router<State> {
@@ -53,8 +53,13 @@ macro_rules! ruma_handler {
}
fn add_route(&'static self, router: Router<State>, path: &str) -> Router<State> {
let action = |$($tx,)* req| self($($tx,)* req).map_ok(RumaResponse);
let method = method_to_filter(&Req::METADATA.method);
let action = |$($tx,)* req| {
self($($tx,)* req)
.inspect_ok(|result| trace!(?result))
.map_ok(RumaResponse)
};
router.route(path, on(method, action))
}
}
+11 -5
View File
@@ -4,22 +4,28 @@ use axum::{RequestExt, RequestPartsExt, extract::Path};
use bytes::Bytes;
use http::request::Parts;
use serde::Deserialize;
use tuwunel_core::{Result, err};
use tuwunel_core::{Result, err, smallstr::SmallString, smallvec::SmallVec};
use tuwunel_service::Services;
#[derive(Deserialize)]
#[derive(Debug, Deserialize)]
pub(super) struct QueryParams {
pub(super) access_token: Option<String>,
pub(super) user_id: Option<String>,
pub(super) user_id: Option<UserId>,
}
pub(super) type UserId = SmallString<[u8; 48]>;
#[derive(Debug)]
pub(super) struct Request {
pub(super) path: Path<Vec<String>>,
pub(super) path: Path<PathParams>,
pub(super) query: QueryParams,
pub(super) body: Bytes,
pub(super) parts: Parts,
}
pub(super) type PathParams = SmallVec<[PathParam; 8]>;
pub(super) type PathParam = SmallString<[u8; 32]>;
pub(super) async fn from(
services: &Services,
request: hyper::Request<axum::body::Body>,
@@ -27,7 +33,7 @@ pub(super) async fn from(
let limited = request.with_limited_body();
let (mut parts, body) = limited.into_parts();
let path: Path<Vec<String>> = parts.extract().await?;
let path: Path<PathParams> = parts.extract().await?;
let query = parts.uri.query().unwrap_or_default();
let query = serde_html_form::from_str(query)
.map_err(|e| err!(Request(Unknown("Failed to read query parameters: {e}"))))?;
Loaded 100 of 235 files, more files were not shown because too many files have changed in this diff. Show more