Compare commits

...
240 Commits
Author SHA1 Message Date
Jason Volk 5110b9e47b Bump 1.5.0
Signed-off-by: Jason Volk <jason@zemos.net>
2026-02-01 07:42:17 +00:00
dasha_uwu 707dc74127 Fix panic with debug assertions turned on due to short argument clash 2026-02-01 06:54:25 +00:00
dasha_uwu 024c08f795 Fix allow_room_admins_to_request_unredacted_events applying to server admins instead of room admins. 2026-02-01 05:01:59 +00:00
Jason Volk fafdb5d7ea Merge remote-tracking branch 'xerusion/main' 2026-02-01 01:25:33 +00:00
Jason Volk 0b864801f5 Use map of identity_provider to accommodate env var enumerations.
Signed-off-by: Jason Volk <jason@zemos.net>
2026-02-01 01:22:13 +00:00
Xerusion 7e426e5fc8 clarify yourcertresolver 2026-01-31 20:06:58 +07:00
Xerusion c27fb45dc8 Update matrix_rtc.md 2026-01-31 20:01:03 +07:00
Xerusion a2e5d61745 fix formatting 2026-01-31 19:54:13 +07:00
Xerusion cdf188a776 Update matrix_rtc.md 2026-01-31 19:53:31 +07:00
Xerusion 0aa9160af7 Add Traefik setup for matrix-rtc docs 2026-01-31 19:51:27 +07:00
Xerusion 6e87758cf6 add traefik network example 2026-01-31 19:33:51 +07:00
Xerusion b94f5b5963 Update reverse-proxy-traefik.md
include adding to traefik network
2026-01-31 18:17:37 +07:00
Jason Volk 0474753333 Set display-sensitive directive for identity_provider client_secret.
Signed-off-by: Jason Volk <jason@zemos.net>
2026-01-31 09:40:50 +00:00
Jason Volk 1fc69efffe Simplify Figment init, toml config file folding.
Signed-off-by: Jason Volk <jason@zemos.net>
2026-01-31 09:40:50 +00:00
Jason Volk 2fb032fabb Reduce default redaction_retention_seconds to sixty days.
Signed-off-by: Jason Volk <jason@zemos.net>
2026-01-30 22:16:05 +00:00
Vladislav Grechannik f2a15fda34 Format all nix files with new nixfmt 2026-01-30 16:56:39 -05:00
Vladislav Grechannik dbc208d5d9 Add comments on link fixes 2026-01-30 16:56:39 -05:00
Vladislav Grechannik 5aea9d139b Fix nix linkage issues 2026-01-30 16:56:39 -05:00
June Strawberry 3398d1730e bump MSRV to 1.91.1
Signed-off-by: June Strawberry <june@vern.cc>
2026-01-30 16:56:39 -05:00
June Strawberry 895387e8c2 error on startup for unknown config file paths
Signed-off-by: June Strawberry <june@vern.cc>
2026-01-30 16:56:27 -05:00
June Strawberry fbeaed1c15 implement error_on_unknown_config_opts, slightly improve related code
Signed-off-by: June Strawberry <june@vern.cc>
2026-01-30 16:56:27 -05:00
Jason Volk 13ad2c7966 Fix m.tz identifier and bias to stable.
Bump Ruma; enable spec versions 1.12 and 1.15.

Signed-off-by: Jason Volk <jason@zemos.net>
2026-01-30 21:53:52 +00:00
Jason Volk 522f9181a5 ci: Upload complement logs artifact unconditionally.
Signed-off-by: Jason Volk <jason@zemos.net>
2026-01-30 21:53:52 +00:00
dasha_uwu 9920f8bab0 Fix formatting output of admin debug get-retained-pdu 2026-01-30 21:42:32 +00:00
dasha_uwu 32e3d38866 Remove admin check
This was non-functional for over a year
2026-01-30 21:42:32 +00:00
dasha_uwu 876c291c91 Remove redundant admin query users iter-users2 2026-01-30 21:42:32 +00:00
dasha_uwu 712c0c9487 Do not overwrite retained PDU 2026-01-30 21:42:32 +00:00
dasha_uwu 010c519677 Fix display name not being set on registration when no display name suffix is configured 2026-01-30 21:42:32 +00:00
dasha_uwu cc9e99f31d Include displayname on remote invites 2026-01-30 21:42:32 +00:00
dasha_uwu 73eee626df Take stuff by ref in users service profile 2026-01-30 21:42:32 +00:00
Vladislav Grechannik c76a66be28 Stop storing media in shared caches
Cache-Control=public leads to everyone being able to GET media from some shared cache (e.g. Cloudflare's)
2026-01-30 18:48:47 +01:00
Jason Volk 90d4bda70b Default redaction_retention_seconds to one year.
Signed-off-by: Jason Volk <jason@zemos.net>
2026-01-28 12:16:41 +00:00
Vladislav Grechannik ae35e5a5b2 Set save_unredacted_events option to true 2026-01-28 11:07:31 +00:00
Jason Volk 10060cef69 Merge remote-tracking branch 'bdfd9/patch-1' 2026-01-28 06:06:44 +00:00
Jason Volk 136c0f2682 Mitigate clippy::duration_suboptimal_units until 1.91 msrv upgrade.
Signed-off-by: Jason Volk <jason@zemos.net>
2026-01-28 05:58:05 +00:00
Jason Volk 7bc25ad1b1 Fix nightly clippy::unnecessary-sort-by.
Signed-off-by: Jason Volk <jason@zemos.net>
2026-01-28 04:50:31 +00:00
bdfd9 5112c55bf0 Don't log a password when a new user account is registered
Introduced in 4150b917d8
2026-01-27 17:25:53 +01:00
winyadepla 9411996813 Fix typo 2026-01-27 01:01:48 +00:00
winyadepla 8e29fbb3a4 Update matrix_rtc.md to use websocket for Livekit domain in compose. 2026-01-27 00:57:52 +00:00
winyadepla 23b532d0aa Update Matrix RTC docs to reflect Livkit now supporting secrets for TURN. 2026-01-27 00:53:32 +00:00
winyadepla afbf854cfd Fix livekit path in Matrix RTC docs. 2026-01-27 00:41:58 +00:00
winyadepla ae3f08eb9a Update matrix_rtc.md to fix typo. 2026-01-26 21:59:39 +00:00
winyadepla cb3dfbc437 Update matrix_rtc.md to fix instruction for serving .well-known from tuwunel. 2026-01-26 21:54:18 +00:00
Jason Volk 0b82fe27b5 Revert "Remove deprecated _matrix._tcp SRV query."
This reverts commit 258e2e3ce8.

This can be re-applied when Synapse decides to deprecate it.
2026-01-26 20:30:25 +00:00
dasha_uwu a1960553e9 Turn non-monotonic next_batch assert into error log and clamp. 2026-01-26 20:24:44 +00:00
Jason Volk daa869fced Additional missing tracing spans on state_res::resolve components.
Signed-off-by: Jason Volk <jason@zemos.net>
2026-01-26 20:24:44 +00:00
Jason Volk 23a3e87e93 Fix stale lint expectations.
Signed-off-by: Jason Volk <jason@zemos.net>
2026-01-26 20:24:44 +00:00
dasha_uwuandGinger 56f3f5ea15 Limited use registration token support
Co-authored-by: Ginger <ginger@gingershaped.computer>
Signed-off-by: Jason Volk <jason@zemos.net>
2026-01-26 20:24:15 +00:00
dasha_uwu 0dbe79df8e Refactor admin query raw 2026-01-26 19:28:56 +00:00
dasha_uwu 3caab50e0d Minor rewrites 2026-01-26 19:28:56 +00:00
dasha_uwu bb26b749ae Redacted event retention, implement MSC2815 2026-01-26 19:28:56 +00:00
dasha_uwu 8000fcce9e Non-reserializing redaction 2026-01-26 19:28:56 +00:00
dasha_uwu 0c9a3abb71 clippy allow -> expect 2026-01-26 19:28:56 +00:00
dasha_uwu fbedd713ca Fix lint 2026-01-26 19:28:56 +00:00
Jason Volk 6db87a4027 Implement associated multi-provider single-sign-on flow support. (#252)
Add experimental note for multi-provider flow. (#252)

Signed-off-by: Jason Volk <jason@zemos.net>
2026-01-26 19:28:50 +00:00
Jason Volk a3294fe1cf Add admin command to list last-active local users.
Signed-off-by: Jason Volk <jason@zemos.net>
2026-01-24 08:19:41 +00:00
Jason Volk 885e402e38 Lower generics on OptionExt to function prototypes.
Signed-off-by: Jason Volk <jason@zemos.net>
2026-01-24 08:19:41 +00:00
Jason Volk f048f87dac Split login_via_existing_session conf item to login_via_token.
Signed-off-by: Jason Volk <jason@zemos.net>
2026-01-24 08:19:41 +00:00
Jason Volk 7ff51a8eca Support serializing and deserializing arrays of separated strings in schema.
Signed-off-by: Jason Volk <jason@zemos.net>
2026-01-24 08:19:41 +00:00
Jason Volk be542da6e4 Re-export Either from tuwunel_core.
Signed-off-by: Jason Volk <jason@zemos.net>
2026-01-24 08:19:41 +00:00
Jason Volk f03321d33d ci: Build verbose-logging package for aarch64.
Signed-off-by: Jason Volk <jason@zemos.net>
2026-01-22 22:17:47 +00:00
Jason Volk 9d8525e7f7 Readjust auth_chain cache default size and scalars.
Signed-off-by: Jason Volk <jason@zemos.net>
2026-01-22 22:17:47 +00:00
Jason Volk c6c85374fa Flatten fork leaf state gather.
Signed-off-by: Jason Volk <jason@zemos.net>
2026-01-22 09:49:19 +00:00
Jason Volk 948e1681c2 Add map_stream(), trait constraints to OptionExt.
Signed-off-by: Jason Volk <jason@zemos.net>
2026-01-22 09:31:46 +00:00
Jason Volk 48aa6035f6 Hoist room_version query to callers of get_auth_chain.
Signed-off-by: Jason Volk <jason@zemos.net>
2026-01-22 09:31:46 +00:00
Jason Volk afcb2315ee Move auth_chain cache to db.
Signed-off-by: Jason Volk <jason@zemos.net>
2026-01-22 09:31:46 +00:00
Jason Volk 7b22e7930a Add descriptor type for larger persistent caches.
Signed-off-by: Jason Volk <jason@zemos.net>
2026-01-22 09:31:46 +00:00
Jason Volk 944f165202 Optimize get_auth_chain_inner concurrency.
Signed-off-by: Jason Volk <jason@zemos.net>
2026-01-22 09:31:46 +00:00
Jason Volk a0b98fa575 Misc debug and trace log tweaks.
Signed-off-by: Jason Volk <jason@zemos.net>
2026-01-22 09:31:46 +00:00
Jason Volk b7fe61b423 Add str_truncated() to debug suite.
Signed-off-by: Jason Volk <jason@zemos.net>
2026-01-22 03:41:39 +00:00
Jason Volk b849bcac19 Merge remote-tracking branch 'winyadepla/matrix-rtc-docs' 2026-01-22 03:41:11 +00:00
winyadepla dca100ff16 Update matrix_rtc.md to include TURN integration and be more in line with other tuwunel documentation 2026-01-22 00:36:15 +00:00
Jared L 7ec9d7f5aa fix: ensure aggregate presence change flushes queue 2026-01-21 16:50:23 +11:00
winyadepla 8510ca32c8 Update SUMMARY.md to include Matrix RTC 2026-01-20 23:11:03 +00:00
winyadepla 16acc3e1d0 Create matrix_rtc.md 2026-01-20 23:08:56 +00:00
Jared L 0f547115af chore: fmt 2026-01-21 07:28:12 +11:00
Jared L a91b01f9a2 chore: address clippy warnings 2026-01-21 06:32:10 +11:00
Jared L 5bc2863721 docs: add rustdocs for presence aggregation and suppression 2026-01-21 06:27:18 +11:00
Jared L aadc9ade3e presence: restore deferred push suppression 2026-01-21 06:23:12 +11:00
Jared L 22a7f6cb33 presence: add pipeline tests and move timer 2026-01-21 06:23:12 +11:00
Jared L a73b4eba8b presence: extract update pipeline 2026-01-21 06:23:12 +11:00
Jared L 50e8fcc403 presence: simplify update surface 2026-01-21 06:23:12 +11:00
Jared L 2b32769649 presence: aggregate device updates 2026-01-21 06:23:12 +11:00
Jason Volk 3e1246984c Adjust pool defaults for many core systems with undetected topologies.
Signed-off-by: Jason Volk <jason@zemos.net>
2026-01-20 00:18:00 +00:00
Jason Volk 04dc0fda60 ci: Fix condition for packaging and publishing workflows.
Signed-off-by: Jason Volk <jason@zemos.net>
2026-01-20 00:18:00 +00:00
Jason Volk 106bba761e Use bounded condition for syncv3 notification count; asymmetric reset condition. (#261)
Signed-off-by: Jason Volk <jason@zemos.net>
2026-01-19 08:26:23 +00:00
Jason Volk 6619171e13 Slightly increase max_request_size default.
Signed-off-by: Jason Volk <jason@zemos.net>
2026-01-19 08:26:23 +00:00
dasha_uwu 3a9446bb9c Remove dead code
These check the origin which is already checked in router auth
2026-01-19 00:53:33 +00:00
June Strawberry 2f4d3c2851 fix some unused_crate_dependencies lint warns
Signed-off-by: June Strawberry <june@vern.cc>
2026-01-18 13:18:10 -05:00
June Strawberry 1b68588b84 enable match_same_arms lint
Signed-off-by: June Strawberry <june@vern.cc>
2026-01-18 13:10:05 -05:00
June Strawberry f4ba3d4a29 enable some later rustc lints (1.90+)
Signed-off-by: June Strawberry <june@vern.cc>
2026-01-18 13:09:05 -05:00
June Strawberry 04e66a03d3 fix and enable collapsible_if lint
Signed-off-by: June Strawberry <june@vern.cc>
2026-01-18 13:09:03 -05:00
Jason Volk fb102f0e0a Add privileged support for SSO account associations. (#252)
Signed-off-by: Jason Volk <jason@zemos.net>
2026-01-18 15:59:02 +00:00
Jason Volk 99c84039da ci: Additional pipeline constrictions for dev branches.
Signed-off-by: Jason Volk <jason@zemos.net>
2026-01-18 15:59:02 +00:00
Jason Volk 2f2fc5c5f9 ci: Add full control panel for manual workflow dispatching.
Signed-off-by: Jason Volk <jason@zemos.net>
2026-01-18 13:29:10 +00:00
Jason Volk b3f8118f0e Re-export itertools from tuwunel_core crate.
Signed-off-by: Jason Volk <jason@zemos.net>
2026-01-18 13:19:59 +00:00
Jason Volk 6579df299a Find identity providers by brand name when unique.
Signed-off-by: Jason Volk <jason@zemos.net>
2026-01-18 06:11:40 +00:00
Jason Volk 3a613963f9 Improve some comments; fix unlinted typo.
Signed-off-by: Jason Volk <jason@zemos.net>
2026-01-18 06:10:55 +00:00
Jason Volk 5cb6d2058f Tweak some identity_provider example config doc comments.
Signed-off-by: Jason Volk <jason@zemos.net>
2026-01-18 06:04:15 +00:00
Jason Volk 64149f0cc4 Allow SSO users to set a password.
Signed-off-by: Jason Volk <jason@zemos.net>
2026-01-18 05:00:03 +00:00
Jason Volk 98f5ea203b Add simpler option to specify default identity_provider.
Signed-off-by: Jason Volk <jason@zemos.net>
2026-01-18 05:00:03 +00:00
Xerusion 85ac256786 add built in well-known file proxy 2026-01-17 09:11:39 +00:00
Jason Volk f9f00d83fc Use mtime for ts in delete_all_remote_media_at_after_time().
Signed-off-by: Jason Volk <jason@zemos.net>
2026-01-17 06:54:31 +00:00
dasha_uwu e78bf21085 Introduce OptionFuture helpers
Optimize user directory searches
2026-01-17 06:54:24 +00:00
dasha_uwu 95121ad905 Add a config option to show all local users in user directory, closes #260 2026-01-17 06:44:34 +00:00
dasha_uwu 62ec58b1ed Introduce extract!(), rewrite part of uiaa username handling 2026-01-17 06:44:34 +00:00
Jason Volk c0aaeefaf2 ci: Add manual workflow dispatch option for pipeline directives.
Signed-off-by: Jason Volk <jason@zemos.net>
2026-01-17 01:39:49 +00:00
Jason Volk 38ae93fe42 ci: Add options for complement verbosity and logging artifacts.
Signed-off-by: Jason Volk <jason@zemos.net>
2026-01-17 01:39:49 +00:00
Jason Volk 2880facc4c Replace hardcoded pool worker limit with configurable default.
Signed-off-by: Jason Volk <jason@zemos.net>
2026-01-17 01:39:49 +00:00
Jason Volk 4b9d4794fb Reapply "Support mdraid hierarchies for storage topology detection."
This reverts commit 121aa9e39d.

Fixes panics on systems with sparse core topologies.
2026-01-17 01:39:49 +00:00
Jason Volk 8a95390f1c Default affinity mask to cores available rather than nil.
Signed-off-by: Jason Volk <jason@zemos.net>
2026-01-17 01:39:49 +00:00
Jason Volk 9346e69b87 Add admin command to manually remove a pusher.
Signed-off-by: Jason Volk <jason@zemos.net>
2026-01-17 01:39:49 +00:00
Jason Volk d0b2181817 Split query pusher command w/ admin_command macros.
Signed-off-by: Jason Volk <jason@zemos.net>
2026-01-17 01:39:49 +00:00
Jason Volk febd4d1a23 Add string-consuming writer to admin context interface.
Signed-off-by: Jason Volk <jason@zemos.net>
2026-01-17 01:39:49 +00:00
Jason Volk 8be0a579db Add doc comments to SSO related interface routines.
Signed-off-by: Jason Volk <jason@zemos.net>
2026-01-17 01:39:49 +00:00
Jason Volk 1b5e11e943 Add cfgs for proving sendness and syncness of the OnceServices build optimization.
Signed-off-by: Jason Volk <jason@zemos.net>
2026-01-17 01:39:49 +00:00
Jason Volk dfb65d771f Add sso_default_provider_id option and defaulting behavior.
Signed-off-by: Jason Volk <jason@zemos.net>
2026-01-17 01:39:49 +00:00
Xerusion 86fc42bba0 Add federation port config 2026-01-17 08:29:58 +07:00
Xerusion 53d4495e21 Add TLS cert info
Added section for TLS certs
2026-01-17 01:38:00 +07:00
Xerusion fa7838ffb1 Adjust example for clarity
Updated the example configuration for clarity.
2026-01-17 01:12:48 +07:00
Xerusion ae8f266385 Add Traefik reverse proxy documentation
Updated the Traefik documentation with installation and configuration details
2026-01-17 00:36:24 +07:00
Jason Volk 3426b824a0 Add EdDSA support to JWT login (fixes #258).
Signed-off-by: Jason Volk <jason@zemos.net>
2026-01-13 08:47:34 +00:00
dasha_uwu d095a4fd3b Enable unused_async clippy lint 2026-01-13 07:42:43 +00:00
dasha_uwu fd519ff7f1 Fix sliding sync not returning rooms the user is invited to 2026-01-13 07:42:43 +00:00
KuhnChris 76c09851ea Guard admin assignment/removal against empty filter 2026-01-13 07:42:43 +00:00
June Strawberry 5e532593fa add autocopr action
Signed-off-by: June Strawberry <june@vern.cc>
2026-01-13 07:42:43 +00:00
Jason Volk fd8ee422dd Increment accumulators prior to release-action (fixes #253).
↳ userroomid_notificationcount and userroomid_highlightcount should be
incremented prior to touching useridcount_notification.

Signed-off-by: Jason Volk <jason@zemos.net>
2026-01-13 07:42:43 +00:00
Jason Volk cf8b57b751 Simplify MutexMap generic constraints.
Signed-off-by: Jason Volk <jason@zemos.net>
2026-01-13 07:42:43 +00:00
Justin 05898034e5 Fix growing unread notification counts by unifying key generation logic (#253)
* fix(pusher): use consistent tuple keys for notification counts

* fix(pusher): correct 'notifi' typo in send.rs
2026-01-13 07:42:43 +00:00
Jason Volk 257168946e Fix missing example config generation. (fc104d02a4)
Signed-off-by: Jason Volk <jason@zemos.net>
2026-01-13 07:42:43 +00:00
Vladislav Grechannik fc104d02a4 Add an option to read SSO client secret from a file 2026-01-10 04:38:29 +01:00
Jason Volk bd0a0acf4a Add conditional for login flows; filter out SSO when no providers configured.
Signed-off-by: Jason Volk <jason@zemos.net>
2026-01-03 05:22:55 +00:00
Jason Volk 11038b0ec7 Bump Cargo version to 1.4.9. (closes #250)
Signed-off-by: Jason Volk <jason@zemos.net>
2026-01-02 17:10:52 +00:00
Jason Volk 2a7455b5c9 Use form-urlencoded bodies for server-to-server oauth requests. (fixes #249)
Signed-off-by: Jason Volk <jason@zemos.net>
2026-01-02 05:52:37 +00:00
Jason Volk fbf66f565a Clarify misleading wording during keys acquire progress logging.
Signed-off-by: Jason Volk <jason@zemos.net>
2026-01-01 08:09:06 +00:00
Jason Volk 6303ae28e9 Add preliminary traefik proxy guide to docs.
Signed-off-by: Jason Volk <jason@zemos.net>
2026-01-01 06:49:15 +00:00
Jason Volk a434c4d452 ci: Use actions variable for dockerhub user rather than github.actor.
Signed-off-by: Jason Volk <jason@zemos.net>
2026-01-01 02:55:54 +00:00
dasha_uwu 121aa9e39d Revert "Support mdraid hierarchies for storage topology detection."
This reverts commit d665a34f30.

The commit was causing requests to panic on some systems
2026-01-01 01:10:59 +05:00
Jason Volk 6b4a09fc81 Bump 1.4.9
Signed-off-by: Jason Volk <jason@zemos.net>
2025-12-30 23:48:15 +00:00
Jason Volk 5a92a00939 Fix missing validations of federation member event stubs.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-12-30 18:42:46 +00:00
Jason Volk b412aafaf8 Add an infolog progress message during batch notary request.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-12-30 18:42:46 +00:00
Jason Volk 63bdeb79c9 Add newtype deserialization for Json(_) for parity with Cbor(_).
Signed-off-by: Jason Volk <jason@zemos.net>
2025-12-30 18:42:46 +00:00
Jason Volk 0bbe96f761 Bump Ruma for custom JoinRule fix.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-12-30 18:42:46 +00:00
Jason Volk b33e73672b Implement MSC3706 two-step join.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-12-29 21:55:15 +00:00
Jason Volk 59481ad28d Refactor/dedup join event preparation paths.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-12-29 08:08:05 +00:00
Jason Volk 977f9e2e2e Fix deactivated user check misapplied to appservice user. (11309062a2)
Signed-off-by: Jason Volk <jason@zemos.net>
2025-12-28 21:59:29 +00:00
Jason Volk 432e6a2663 Fix serializing zero notification count in never-read room case.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-12-28 05:17:29 +00:00
Jason Volk f4ee8bc433 Simplify spaces related syntax.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-12-28 00:49:45 +00:00
Jason Volk 8aeaaea271 Fix clippy::result-large-err.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-12-28 00:49:45 +00:00
divideableZero 5847d8135d docs(readme): add Alpine package 2025-12-27 18:22:48 +03:00
Jason Volk 14e7a001b9 Use abstracted UIAA call for login token route.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-12-26 14:20:07 +00:00
Jason Volk d56ee58a73 Fix SSO cookie deserialization for cases requiring allocated strings.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-12-26 14:20:07 +00:00
Jason Volk 806ecaec7e Add demoserver badge to readme documentation section.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-12-26 14:20:07 +00:00
Jason Volk 11309062a2 Implement SSO/OIDC support. (closes #7)
Signed-off-by: Jason Volk <jason@zemos.net>
2025-12-26 07:24:49 +00:00
Jason Volk d665a34f30 Support mdraid hierarchies for storage topology detection.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-12-26 00:51:02 +00:00
Jason Volk e019ed77d0 Add more linespace between example config sections.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-12-26 00:11:43 +00:00
Jason Volk 568a28220c Add stochastic string truncation utils.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-12-24 03:26:08 +00:00
Jason Volk 4229a1d630 Add type alias for Arc<OnceServices>
Signed-off-by: Jason Volk <jason@zemos.net>
2025-12-23 14:52:55 +00:00
Jason Volk 0cbdcf3b3d Add displayname argument to full_register.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-12-23 03:50:29 +00:00
Jason Volk fe57ea3354 Add functor for timepoint expiration to utils.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-12-23 02:55:32 +00:00
Jason Volk 422802a9cf Add serde_html_form to Error; use serde_core for greater abstraction.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-12-23 02:55:32 +00:00
Jason Volk e79b888367 Improve well-known resolver tracing span.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-12-23 02:55:32 +00:00
Jason Volk f91b3b6a6c Add string util for #[serde(deserialize_with = "deserialize::to_lowercase")]
Signed-off-by: Jason Volk <jason@zemos.net>
2025-12-23 02:55:32 +00:00
Jason Volk 4150b917d8 Restructure full_register arguments with names and defaults.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-12-23 02:55:32 +00:00
Jason Volk 5b0dfaeef6 Allow clippy::toplevel-ref-arg.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-12-23 02:55:32 +00:00
Jason Volk 7a4d07fd3b Lock tracing to 0.1.43 until 0.1.44 breaking change resolved.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-12-23 02:55:32 +00:00
dasha_uwu eadc9e782d Fetch tags in CI builds to provide correct version string 2025-12-21 22:38:50 +00:00
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
342 changed files with 14103 additions and 7290 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")
+27
View File
@@ -0,0 +1,27 @@
name: Update .rpm SPEC files
on:
# Allows for manually running an update from the actions tab
workflow_dispatch:
schedule:
# Runs every day at ~12am UTC - see https://crontab.guru/
- cron: '0 0 * * *'
# Revoke all default permissions
permissions: {}
jobs:
update:
name: update
runs-on: ubuntu-latest
permissions:
# needed to push commits to this repo
# see `check` mode if you don't want to give this permission
contents: write
steps:
- uses: actions/checkout@v5
- uses: aidandenlinger/autocopr@v1 # Or a specific release tag, or commit
with:
mode: "push"
+7
View File
@@ -42,6 +42,10 @@ on:
type: string
default: '[]'
description: Matrix inclusions
checkout:
type: string
default: 'HEAD'
description: Specific commit to checkout
max-parallel:
type: number
default: 128
@@ -111,6 +115,8 @@ jobs:
- uses: actions/checkout@v4
with:
persist-credentials: false
fetch-depth: 0
fetch-tags: true
# Call docker
- name: Bake ${{matrix.bake_target}}
@@ -129,6 +135,7 @@ jobs:
CI_VERBOSE_ENV: ${{inputs.verbose_env}}
CI_SILENT_BAKE: ${{inputs.silent_bake}}
CI_PRINT_BAKE: ${{inputs.print_bake}}
git_checkout: ${{inputs.checkout}}
run: |
docker/bake.sh "${{matrix.bake_target}}"
+9 -2
View File
@@ -35,6 +35,9 @@ on:
type: string
default: '[]'
description: Matrix inclusions
checkout:
type: string
default: 'HEAD'
jobs:
fmt:
@@ -59,6 +62,7 @@ jobs:
machines: ${{inputs.machines}}
excludes: ${{inputs.excludes}}
includes: ${{inputs.includes}}
checkout: ${{inputs.checkout}}
typos:
if: >
@@ -82,6 +86,7 @@ jobs:
machines: ${{inputs.machines}}
excludes: ${{inputs.excludes}}
includes: ${{inputs.includes}}
checkout: ${{inputs.checkout}}
audit:
if: >
@@ -105,8 +110,8 @@ jobs:
machines: ${{inputs.machines}}
excludes: ${{inputs.excludes}}
includes: ${{inputs.includes}}
checkout: ${{inputs.checkout}}
# disabled due to excessive build time issue installing cargo lychee
lychee:
if: >
!failure() && !cancelled()
@@ -114,7 +119,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
@@ -130,6 +134,7 @@ jobs:
machines: ${{inputs.machines}}
excludes: ${{inputs.excludes}}
includes: ${{inputs.includes}}
checkout: ${{inputs.checkout}}
check:
if: >
@@ -153,6 +158,7 @@ jobs:
machines: ${{inputs.machines}}
excludes: ${{inputs.excludes}}
includes: ${{inputs.includes}}
checkout: ${{inputs.checkout}}
clippy:
if: >
@@ -173,3 +179,4 @@ jobs:
machines: ${{inputs.machines}}
excludes: ${{inputs.excludes}}
includes: ${{inputs.includes}}
checkout: ${{inputs.checkout}}
+162 -45
View File
@@ -10,6 +10,73 @@ on:
branches:
- "**"
workflow_dispatch:
inputs:
pipeline:
type: string
description: "Directives e.g. '[ci only it]'"
checkout:
type: string
description: "Checkout commit instead of HEAD"
cargo_profiles:
type: string
default: '["test", "bench", "release", "release-debuginfo", "release-native"]'
description: "Matrix Selector: Cargo profiles"
feat_sets:
type: string
default: '["none", "default", "logging", "all"]'
description: "Matrix Selector: Cargo feature sets"
rust_toolchains:
type: string
default: '["nightly", "stable"]'
description: "Matrix Selector: Rust toolchains"
rust_targets:
type: string
default: '["x86_64-unknown-linux-gnu", "aarch64-unknown-linux-gnu"]'
description: "Matrix Selector: Rust targets"
sys_names:
type: string
default: '["debian"]'
description: "Matrix Selector: System names"
sys_targets:
type: string
default: '["x86_64-v1-linux-gnu", "x86_64-v2-linux-gnu", "x86_64-v3-linux-gnu", "aarch64-v8-linux-gnu"]'
description: "Matrix Selector: System targets"
sys_versions:
type: string
default: '["testing-slim"]'
description: "Matrix Selector: System versions"
machines:
type: string
default: '["X64", "ARM64"]'
description: "Matrix Selector: Machine architectures"
runners:
type: string
default: '["het", "aws"]'
description: "Matrix Selector: Runner labels"
package_containers:
type: string
default: '["docker", "oci"]'
description: "Bake Selector: Container packages"
package_distros:
type: string
default: '["deb", "rpm", "nix"]'
description: "Bake Selector: Distro packages"
enable_lint:
type: boolean
default: true
description: Enable linting stage
enable_test:
type: boolean
default: true
description: Enable testing stage
enable_package:
type: boolean
default: false
description: Enable packaging stage
enable_publish:
type: boolean
default: false
description: Enable publication stage
concurrency:
group: ${{github.workflow}}-${{github.ref}}
@@ -28,34 +95,64 @@ jobs:
default_rust_targets: '["x86_64-unknown-linux-gnu"]'
default_sys_targets: '["x86_64-v1-linux-gnu", "x86_64-v3-linux-gnu"]'
default_machines: '["X64"]'
default_package_containers: '["docker", "oci"]'
default_package_distros: '["deb", "rpm", "nix"]'
is_release: ${{contains(github.ref, 'tags/v')}}
is_branch: ${{contains(github.ref, 'refs/heads')}}
is_pull: ${{contains(github.ref, 'refs/pull')}}
is_main: ${{github.ref == 'refs/heads/main'}}
is_test: ${{github.ref == 'refs/heads/test'}}
is_dev: ${{contains(github.ref, 'refs/heads') && github.ref != 'refs/heads/main' && github.ref != 'refs/heads/test'}}
is_fat: ${{contains(github.ref, 'tags/v') || github.ref == 'refs/heads/main' || github.ref == 'refs/heads/test'}}
outputs:
cargo_profiles: ${{vars.CARGO_PROFILES || env.default_cargo_profiles}}
feat_sets: ${{vars.FEAT_SETS || env.default_feat_sets}}
rust_toolchains: ${{vars.RUST_TOOLCHAINS || env.default_rust_toolchains}}
rust_targets: ${{vars.RUST_TARGETS || env.default_rust_targets}}
sys_names: ${{vars.SYS_NAMES || env.default_sys_names}}
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 != '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 || 'false'}}
complement: ${{vars.COMPLEMENT != 'false'}}
cargo_profiles: ${{inputs.cargo_profiles || vars.CARGO_PROFILES || env.default_cargo_profiles}}
feat_sets: ${{inputs.feat_sets || vars.FEAT_SETS || env.default_feat_sets}}
rust_toolchains: ${{inputs.rust_toolchains || vars.RUST_TOOLCHAINS || env.default_rust_toolchains}}
rust_targets: ${{inputs.rust_targets || vars.RUST_TARGETS || env.default_rust_targets}}
sys_names: ${{inputs.sys_names || vars.SYS_NAMES || env.default_sys_names}}
sys_targets: ${{inputs.sys_targets || vars.SYS_TARGETS || env.default_sys_targets}}
sys_versions: ${{inputs.sys_versions || vars.SYS_VERSIONS || env.default_sys_versions}}
machines: ${{inputs.machines || (fromJSON(env.is_fat) && vars.MACHINES) || env.default_machines}}
pipeline: ${{inputs.pipeline || github.event.head_commit.message || github.event.workflow_run.head_commit.message}}
checkout: ${{inputs.checkout || github.sha}}
enable_lint: ${{fromJSON(inputs.enable_lint || 'true')}}
enable_test: ${{fromJSON(inputs.enable_test || 'true')}}
enable_test_unit: ${{fromJSON(inputs.enable_test_unit || 'true')}}
enable_test_docs: ${{fromJSON(inputs.enable_test_docs || 'true')}}
enable_test_bench: ${{fromJSON(inputs.enable_test_bench || 'true')}}
enable_test_memcheck: ${{fromJSON(inputs.enable_test_memcheck || 'true')}}
enable_test_smoke: ${{fromJSON(inputs.enable_test_smoke || 'true')}}
enable_test_nix: ${{fromJSON(inputs.enable_test_nix || 'true')}}
enable_test_rustsdk: ${{fromJSON(inputs.enable_test_rustsdk || 'true')}}
enable_test_complement: ${{fromJSON(inputs.enable_test_complement || 'true')}}
complement_verbose: ${{fromJSON(inputs.complement_verbose || 'false')}}
complement_runner: 'het'
docker_repo: ${{vars.DOCKER_REPO}}
release_url: ${{steps.release.outputs.upload_url}}
enable_package: ${{fromJSON(inputs.enable_package || 'true')}}
enable_package_book: ${{fromJSON(inputs.enable_package_book || 'true')}}
enable_package_binary: ${{fromJSON(inputs.enable_package_binary || 'true')}}
enable_package_container: ${{fromJSON(inputs.enable_package_container || env.is_fat || 'true')}}
package_containers: ${{inputs.package_containers || env.default_package_containers}}
enable_package_distro: ${{fromJSON(inputs.enable_package_distro || env.is_fat || 'false')}}
package_distros: ${{inputs.package_distros || env.default_package_distros}}
enable_package_checks: ${{fromJSON(inputs.enable_package_checks || env.is_test || 'false')}}
enable_publish: ${{fromJSON(inputs.enable_publish || 'true')}}
is_release: ${{fromJSON(env.is_release || 'false')}}
is_main: ${{fromJSON(env.is_main || 'false')}}
is_test: ${{fromJSON(env.is_test || 'false')}}
is_pull: ${{fromJSON(env.is_pull || 'false')}}
is_fat: ${{fromJSON(env.is_fat || 'false')}}
is_dev: ${{fromJSON(env.is_dev || 'true')}}
pages_url: 'https://matrix-construct.github.io/tuwunel/'
rust_sdk_integ: ${{vars.RUST_SDK_INTEGRATION != 'false'}}
head_msg: ${{github.event.head_commit.message || github.event.workflow_run.head_commit.message}}
release_url: ${{steps.release.outputs.upload_url}}
docker_repo: ${{vars.DOCKER_REPO}}
docker_acct: ${{vars.DOCKER_ACCT}}
strategy:
fail-fast: true
matrix:
runner: ${{fromJSON(vars.RUNNERS || '["het"]')}}
machine: ${{fromJSON(vars.MACHINES || '["X64"]')}}
runner: ${{fromJSON(inputs.runners || vars.RUNNERS || '["het"]')}}
machine: ${{fromJSON(inputs.machines || vars.MACHINES || '["X64"]')}}
exclude: ${{fromJSON(vars.RUNNERS_EXCLUDES || '[]')}}
steps:
@@ -64,6 +161,7 @@ jobs:
runner: ${{matrix.runner}}
reserved_space: '{"het": "192GB", "aws": "48GB", "gcp": "160GB"}'
max_used_space: '{"het": "384GB", "aws": "64GB", "gcp": "192GB"}'
min_free_space: '{"het": "512GB", "aws": "16GB", "gcp": "16GB"}'
run: |
set +e
docker buildx inspect "${GITHUB_ACTOR}"
@@ -74,6 +172,7 @@ jobs:
set -eux
reserved_space=$(echo -n "$reserved_space" | jq -r ".$runner")
max_used_space=$(echo -n "$max_used_space" | jq -r ".$runner")
min_free_space=$(echo -n "$min_free_space" | jq -r ".$runner")
cat <<EOF > ./buildkitd.toml
[system]
platformsCacheMaxAge = "504h"
@@ -83,11 +182,16 @@ jobs:
gc = true
reservedSpace = "${reserved_space}"
maxUsedSpace = "${max_used_space}"
minFreeSpace = "${min_free_space}"
[[worker.oci.gcpolicy]]
reservedSpace = "${reserved_space}"
maxUsedSpace = "${max_used_space}"
minFreeSpace = "${min_free_space}"
filters = ["label!=cache==pin"]
all = true
[[worker.oci.gcpolicy]]
minFreeSpace = "${min_free_space}"
all = true
EOF
docker buildx create \
@@ -117,14 +221,16 @@ jobs:
lint:
if: >
!failure() && !cancelled()
&& !contains(github.ref, 'refs/tags/v')
&& !contains(needs.init.outputs.head_msg, '[ci no lint]')
&& !contains(needs.init.outputs.head_msg, '[ci only it]')
&& fromJSON(needs.init.outputs.enable_lint)
&& !fromJSON(needs.init.outputs.is_release)
&& !contains(needs.init.outputs.pipeline, '[ci no lint]')
&& !contains(needs.init.outputs.pipeline, '[ci only it]')
name: Lint
needs: [init] #needs: [init, deps]
uses: ./.github/workflows/lint.yml
with:
checkout: ${{needs.init.outputs.checkout}}
cargo_profiles: ${{needs.init.outputs.cargo_profiles}}
feat_sets: ${{needs.init.outputs.feat_sets}}
rust_toolchains: ${{needs.init.outputs.rust_toolchains}}
@@ -167,18 +273,26 @@ jobs:
test:
if: >
!failure() && !cancelled()
&& !contains(github.ref, 'refs/tags/v')
&& !contains(needs.init.outputs.head_msg, '[ci no test]')
&& fromJSON(needs.init.outputs.enable_test)
&& !fromJSON(needs.init.outputs.is_release)
&& !contains(needs.init.outputs.pipeline, '[ci no test]')
name: Test
needs: [init, lint]
uses: ./.github/workflows/test.yml
with:
head_msg: ${{needs.init.outputs.head_msg}}
build_nix: ${{fromJSON(needs.init.outputs.build_nix)}}
rust_sdk_integ: ${{fromJSON(needs.init.outputs.rust_sdk_integ)}}
complement: ${{fromJSON(needs.init.outputs.complement)}}
pipeline: ${{needs.init.outputs.pipeline}}
checkout: ${{needs.init.outputs.checkout}}
enable_docs: ${{needs.init.outputs.enable_test_docs}}
enable_unit: ${{needs.init.outputs.enable_test_unit}}
enable_bench: ${{needs.init.outputs.enable_test_bench}}
enable_memcheck: ${{needs.init.outputs.enable_test_memcheck}}
enable_smoke: ${{needs.init.outputs.enable_test_smoke}}
enable_nix: ${{needs.init.outputs.enable_test_nix}}
enable_rustsdk: ${{needs.init.outputs.enable_test_rustsdk}}
enable_complement: ${{needs.init.outputs.enable_test_complement}}
complement_runner: ${{needs.init.outputs.complement_runner}}
complement_verbose: ${{needs.init.outputs.complement_verbose}}
cargo_profiles: ${{needs.init.outputs.cargo_profiles}}
feat_sets: ${{needs.init.outputs.feat_sets}}
rust_toolchains: ${{needs.init.outputs.rust_toolchains}}
@@ -221,20 +335,24 @@ jobs:
package:
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]')
&& fromJSON(needs.init.outputs.enable_package)
&& !contains(needs.init.outputs.pipeline, '[ci only it]')
&& !contains(needs.init.outputs.pipeline, '[ci no build]')
&& !contains(needs.init.outputs.pipeline, '[ci no package]')
name: Package
needs: [init, test]
uses: ./.github/workflows/package.yml
with:
enable_book: ${{needs.init.outputs.enable_package_book}}
enable_binary: ${{needs.init.outputs.enable_package_binary}}
enable_container: ${{needs.init.outputs.enable_package_container}}
enable_distro: ${{needs.init.outputs.enable_package_distro}}
enable_checks: ${{needs.init.outputs.enable_package_checks}}
distros: ${{needs.init.outputs.package_distros}}
containers: ${{needs.init.outputs.package_containers}}
checkout: ${{needs.init.outputs.checkout}}
release_url: ${{needs.init.outputs.release_url}}
check_pkgs: ${{needs.init.outputs.check_pkgs}}
build_pkgs: ${{needs.init.outputs.build_pkgs}}
build_nix: ${{fromJSON(needs.init.outputs.build_nix)}}
cargo_profiles: ${{needs.init.outputs.cargo_profiles}}
feat_sets: ${{needs.init.outputs.feat_sets}}
rust_toolchains: ${{needs.init.outputs.rust_toolchains}}
@@ -259,7 +377,6 @@ jobs:
{"cargo_profile": "release-debuginfo", "sys_target": "x86_64-v4-linux-gnu"},
{"cargo_profile": "release", "rust_toolchain": "nightly"},
{"rust_target": "aarch64-unknown-linux-gnu", "feat_set": "default"},
{"rust_target": "aarch64-unknown-linux-gnu", "feat_set": "logging"},
{"rust_target": "aarch64-unknown-linux-gnu", "bake_target": "nix"},
{"rust_target": "aarch64-unknown-linux-gnu", "sys_target": "x86_64-v1-linux-gnu"},
{"rust_target": "aarch64-unknown-linux-gnu", "sys_target": "x86_64-v2-linux-gnu"},
@@ -285,18 +402,19 @@ jobs:
publish:
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]')
&& !contains(needs.init.outputs.head_msg, '[ci no publish]')
&& fromJSON(needs.init.outputs.enable_publish)
&& !contains(needs.init.outputs.pipeline, '[ci only it]')
&& !contains(needs.init.outputs.pipeline, '[ci no build]')
&& !contains(needs.init.outputs.pipeline, '[ci no package]')
&& !contains(needs.init.outputs.pipeline, '[ci no publish]')
name: Publish
needs: [init, test, package]
uses: ./.github/workflows/publish.yml
with:
checkout: ${{needs.init.outputs.checkout}}
docker_repo: ${{needs.init.outputs.docker_repo}}
docker_acct: ${{needs.init.outputs.docker_acct}}
release_url: ${{needs.init.outputs.release_url}}
pages_url: ${{needs.init.outputs.pages_url}}
cargo_profiles: ${{needs.init.outputs.cargo_profiles}}
@@ -316,7 +434,6 @@ jobs:
{"cargo_profile": "release-debuginfo"},
{"cargo_profile": "release-native"},
{"rust_toolchain": "nightly"},
{"rust_target": "aarch64-unknown-linux-gnu", "feat_set": "logging"},
{"rust_target": "aarch64-unknown-linux-gnu", "sys_target": "x86_64-v1-linux-gnu"},
{"rust_target": "aarch64-unknown-linux-gnu", "sys_target": "x86_64-v2-linux-gnu"},
{"rust_target": "aarch64-unknown-linux-gnu", "sys_target": "x86_64-v3-linux-gnu"},
+35 -12
View File
@@ -38,20 +38,36 @@ on:
release_url:
type: string
description: For release assets
build_pkgs:
checkout:
type: string
default: 'HEAD'
enable_book:
type: string
default: 'true'
check_pkgs:
type: string
default: 'false'
build_nix:
enable_binary:
type: string
default: 'true'
enable_container:
type: string
default: 'true'
enable_distro:
type: string
default: 'true'
enable_checks:
type: string
default: 'true'
containers:
type: string
default: '["docker", "oci"]'
distros:
type: string
default: '["deb", "rpm", "nix"]'
jobs:
book:
if: >
!failure() && !cancelled()
&& fromJSON(inputs.enable_book)
&& contains(fromJSON(inputs.cargo_profiles), fromJSON('["release"]')[0])
&& contains(fromJSON(inputs.feat_sets), fromJSON('["default"]')[0])
&& contains(fromJSON(inputs.rust_toolchains), fromJSON('["stable"]')[0])
@@ -73,6 +89,7 @@ jobs:
excludes: ${{inputs.excludes}}
includes: ${{inputs.includes}}
release_url: ${{inputs.release_url}}
checkout: ${{inputs.checkout}}
artifact: >
{
"book": {
@@ -85,6 +102,7 @@ jobs:
binary:
if: >
!failure() && !cancelled()
&& fromJSON(inputs.enable_binary)
name: Binaries
uses: ./.github/workflows/bake.yml
@@ -101,6 +119,7 @@ jobs:
excludes: ${{inputs.excludes}}
includes: ${{inputs.includes}}
release_url: ${{inputs.release_url}}
checkout: ${{inputs.checkout}}
artifact: >
{
"static": {
@@ -115,11 +134,12 @@ jobs:
container:
if: >
!failure() && !cancelled()
&& fromJSON(inputs.enable_container)
name: Containers
uses: ./.github/workflows/bake.yml
with:
bake_targets: '["docker", "oci"]'
bake_targets: ${{inputs.containers}}
cargo_profiles: ${{inputs.cargo_profiles}}
feat_sets: ${{inputs.feat_sets}}
rust_toolchains: ${{inputs.rust_toolchains}}
@@ -131,6 +151,7 @@ jobs:
excludes: ${{inputs.excludes}}
includes: ${{inputs.includes}}
release_url: ${{inputs.release_url}}
checkout: ${{inputs.checkout}}
artifact: >
{
"docker": {
@@ -147,18 +168,18 @@ jobs:
}
}
pkgs:
distro:
if: >
!failure() && !cancelled()
&& fromJSON(inputs.enable_distro)
&& contains(fromJSON(inputs.feat_sets), fromJSON('["all"]')[0])
&& contains(fromJSON(inputs.cargo_profiles), fromJSON('["release"]')[0])
&& contains(fromJSON(inputs.rust_toolchains), fromJSON('["stable"]')[0])
&& fromJSON(inputs.build_pkgs)
name: Distro Packages
uses: ./.github/workflows/bake.yml
with:
bake_targets: '["deb", "rpm", "nix"]'
bake_targets: ${{inputs.distros}}
cargo_profiles: '["release"]'
feat_sets: '["all"]'
rust_toolchains: '["stable"]'
@@ -170,6 +191,7 @@ jobs:
excludes: ${{inputs.excludes}}
includes: ${{inputs.includes}}
release_url: ${{inputs.release_url}}
checkout: ${{inputs.checkout}}
artifact: >
{
"deb": {
@@ -188,16 +210,16 @@ jobs:
}
}
check_pkgs:
checks:
if: >
!failure() && !cancelled()
&& fromJSON(inputs.enable_checks)
&& contains(fromJSON(inputs.feat_sets), fromJSON('["all"]')[0])
&& contains(fromJSON(inputs.cargo_profiles), fromJSON('["release"]')[0])
&& contains(fromJSON(inputs.rust_toolchains), fromJSON('["stable"]')[0])
&& fromJSON(inputs.check_pkgs)
name: Check Packages
needs: [pkgs]
needs: [distro]
uses: ./.github/workflows/bake.yml
with:
bake_targets: '["deb-install", "rpm-install"]'
@@ -211,3 +233,4 @@ jobs:
machines: ${{inputs.machines}}
excludes: ${{inputs.excludes}}
includes: ${{inputs.includes}}
checkout: ${{inputs.checkout}}
+12 -3
View File
@@ -38,9 +38,15 @@ on:
type: string
default: '[]'
description: Matrix inclusions
checkout:
type: string
default: 'HEAD'
docker_repo:
type: string
description: DockerHub Repository
docker_acct:
type: string
description: DockerHub account name
release_url:
type: string
description: For release assets
@@ -100,6 +106,8 @@ jobs:
- uses: actions/checkout@v4
with:
persist-credentials: false
fetch-depth: 0
fetch-tags: true
- name: GitHub Login
uses: docker/login-action@v3
@@ -112,7 +120,7 @@ jobs:
uses: docker/login-action@v3
with:
registry: docker.io
username: ${{ github.actor }}
username: ${{ inputs.docker_acct }}
password: ${{ secrets.dockerhub_token }}
- name: Bake
@@ -126,6 +134,7 @@ jobs:
sys_version: ${{matrix.sys_version}}
sys_target: ${{matrix.sys_target}}
machine: ${{matrix.machine}}
git_checkout: ${{inputs.checkout}}
acct: ${{github.actor}}
repo: ${{github.repository}}
docker_repo: ${{inputs.docker_repo}}
@@ -174,7 +183,7 @@ jobs:
uses: docker/login-action@v3
with:
registry: docker.io
username: ${{ github.actor }}
username: ${{ inputs.docker_acct }}
password: ${{ secrets.dockerhub_token }}
- name: Release Branch
@@ -281,7 +290,7 @@ jobs:
uses: docker/login-action@v3
with:
registry: docker.io
username: ${{ github.actor }}
username: ${{ inputs.docker_acct }}
password: ${{ secrets.dockerhub_token }}
- name: Push Branch
+65 -29
View File
@@ -35,24 +35,48 @@ on:
type: string
default: '[]'
description: Matrix inclusions
complement:
type: boolean
default: true
pipeline:
type: string
default: ${{github.event.inputs.pipeline || github.event.workflow_run.head_commit.message}}
checkout:
type: string
default: 'HEAD'
enable_docs:
type: string
default: 'true'
enable_unit:
type: string
default: 'true'
enable_bench:
type: string
default: 'true'
enable_memcheck:
type: string
default: 'true'
enable_smoke:
type: string
default: 'true'
enable_nix:
type: string
default: 'true'
enable_rustsdk:
type: string
default: 'true'
enable_complement:
type: string
default: 'true'
complement_runner:
type: string
rust_sdk_integ:
type: boolean
default: true
build_nix:
type: boolean
default: true
head_msg:
default: 'het'
complement_verbose:
type: string
default: 'false'
jobs:
docs:
if: >
!contains(inputs.head_msg, '[ci only it]')
fromJSON(inputs.enable_docs)
&& !contains(inputs.pipeline, '[ci only it]')
&& contains(fromJSON(inputs.cargo_profiles), fromJSON('["test"]')[0])
&& contains(fromJSON(inputs.feat_sets), fromJSON('["all"]')[0])
&& contains(fromJSON(inputs.rust_toolchains), fromJSON('["nightly"]')[0])
@@ -72,10 +96,12 @@ jobs:
machines: ${{inputs.machines}}
excludes: ${{inputs.excludes}}
includes: ${{inputs.includes}}
checkout: ${{inputs.checkout}}
unit:
if: >
!contains(inputs.head_msg, '[ci only it]')
fromJSON(inputs.enable_unit)
&& !contains(inputs.pipeline, '[ci only it]')
&& contains(fromJSON(inputs.cargo_profiles), fromJSON('["test"]')[0])
&& contains(fromJSON(inputs.feat_sets), fromJSON('["all"]')[0])
&& contains(fromJSON(inputs.rust_toolchains), fromJSON('["nightly"]')[0])
@@ -95,10 +121,12 @@ jobs:
machines: ${{inputs.machines}}
excludes: ${{inputs.excludes}}
includes: ${{inputs.includes}}
checkout: ${{inputs.checkout}}
bench:
if: >
!contains(inputs.head_msg, '[ci only it]')
fromJSON(inputs.enable_bench)
&& !contains(inputs.pipeline, '[ci only it]')
&& contains(fromJSON(inputs.cargo_profiles), fromJSON('["bench"]')[0])
&& contains(fromJSON(inputs.feat_sets), fromJSON('["all"]')[0])
&& contains(fromJSON(inputs.rust_toolchains), fromJSON('["nightly"]')[0])
@@ -118,10 +146,12 @@ jobs:
machines: ${{inputs.machines}}
excludes: ${{inputs.excludes}}
includes: ${{inputs.includes}}
checkout: ${{inputs.checkout}}
memcheck:
if: >
!contains(inputs.head_msg, '[ci only it]')
fromJSON(inputs.enable_memcheck)
&& !contains(inputs.pipeline, '[ci only it]')
&& contains(fromJSON(inputs.cargo_profiles), fromJSON('["bench"]')[0])
&& contains(fromJSON(inputs.feat_sets), fromJSON('["all"]')[0])
&& contains(fromJSON(inputs.rust_toolchains), fromJSON('["nightly"]')[0])
@@ -142,12 +172,14 @@ jobs:
machines: ${{inputs.machines}}
includes: ${{inputs.includes}}
excludes: ${{inputs.excludes}}
checkout: ${{inputs.checkout}}
smoke:
if: >
!failure() && !cancelled()
&& !contains(inputs.head_msg, '[ci only it]')
&& !contains(inputs.head_msg, '[ci no build]')
&& fromJSON(inputs.enable_smoke)
&& !contains(inputs.pipeline, '[ci only it]')
&& !contains(inputs.pipeline, '[ci no build]')
&& inputs.cargo_profiles
&& inputs.machines
@@ -164,6 +196,7 @@ jobs:
sys_targets: ${{inputs.sys_targets}}
machines: ${{inputs.machines}}
includes: ${{inputs.includes}}
checkout: ${{inputs.checkout}}
excludes: >
[
{"cargo_profile": "test", "feat_set": "default"},
@@ -194,7 +227,6 @@ jobs:
{"cargo_profile": "release-native", "sys_target": "x86_64-v4-linux-gnu"},
{"rust_target": "aarch64-unknown-linux-gnu", "feat_set": "none"},
{"rust_target": "aarch64-unknown-linux-gnu", "feat_set": "default"},
{"rust_target": "aarch64-unknown-linux-gnu", "feat_set": "logging"},
{"rust_target": "aarch64-unknown-linux-gnu", "sys_target": "x86_64-v1-linux-gnu"},
{"rust_target": "aarch64-unknown-linux-gnu", "sys_target": "x86_64-v2-linux-gnu"},
{"rust_target": "aarch64-unknown-linux-gnu", "sys_target": "x86_64-v3-linux-gnu"},
@@ -221,14 +253,14 @@ jobs:
nix:
if: >
!failure() && !cancelled()
&& !contains(inputs.head_msg, '[ci only it]')
&& !contains(inputs.head_msg, '[ci no build]')
&& fromJSON(inputs.enable_nix)
&& !contains(inputs.pipeline, '[ci only it]')
&& !contains(inputs.pipeline, '[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])
&& contains(fromJSON(inputs.sys_targets), fromJSON('["x86_64-v1-linux-gnu"]')[0])
&& fromJSON(inputs.build_nix)
name: Smoke NixOS
uses: ./.github/workflows/bake.yml
@@ -244,12 +276,13 @@ jobs:
machines: ${{inputs.machines}}
excludes: ${{inputs.excludes}}
includes: ${{inputs.includes}}
checkout: ${{inputs.checkout}}
rust-sdk-integ:
if: >
!failure() && !cancelled()
&& !contains(inputs.head_msg, '[ci no build]')
&& inputs.rust_sdk_integ
&& fromJSON(inputs.enable_rustsdk)
&& !contains(inputs.pipeline, '[ci no build]')
&& 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])
@@ -269,6 +302,7 @@ jobs:
sys_targets: ${{inputs.sys_targets}}
machines: '["X64"]'
runner: ${{inputs.complement_runner}}
checkout: ${{inputs.checkout}}
includes: ${{inputs.includes}}
artifact: >
{
@@ -300,8 +334,8 @@ jobs:
complement:
if: >
!failure() && !cancelled()
&& !contains(inputs.head_msg, '[ci no build]')
&& inputs.complement
&& fromJSON(inputs.enable_complement)
&& !contains(inputs.pipeline, '[ci no build]')
&& contains(fromJSON(inputs.cargo_profiles), fromJSON('["bench"]')[0])
&& contains(fromJSON(inputs.rust_toolchains), fromJSON('["nightly"]')[0])
&& contains(fromJSON(inputs.sys_targets), fromJSON('["x86_64-v3-linux-gnu"]')[0])
@@ -321,12 +355,13 @@ jobs:
runner: ${{inputs.complement_runner}}
excludes: ${{inputs.excludes}}
includes: ${{inputs.includes}}
checkout: ${{inputs.checkout}}
compliance:
if: >
!failure() && !cancelled()
&& !contains(inputs.head_msg, '[ci no build]')
&& inputs.complement
&& fromJSON(inputs.enable_complement)
&& !contains(inputs.pipeline, '[ci no build]')
&& inputs.machines
&& contains(fromJSON(inputs.cargo_profiles), fromJSON('["bench"]')[0])
&& contains(fromJSON(inputs.rust_toolchains), fromJSON('["nightly"]')[0])
@@ -358,6 +393,7 @@ jobs:
- name: Execute
id: execute
env:
complement_verbose: ${{inputs.complement_verbose && 1 || 0}}
cargo_profile: ${{matrix.cargo_profile}}
rust_toolchain: ${{matrix.rust_toolchain}}
rust_target: ${{matrix.rust_target}}
@@ -389,13 +425,13 @@ jobs:
name: complement_results-${{matrix.feat_set}}-${{matrix.sys_name}}-${{matrix.sys_target}}.jsonl
path: ./tests/complement/results.jsonl
- if: failure() && steps.execute.outcome == 'failure'
name: Upload Failure Output
- if: success() || (failure() && steps.execute.outcome == 'failure')
name: Upload Log Output
id: upload-output
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
+725 -550
View File
File diff suppressed because it is too large. Load diff
+28 -38
View File
@@ -28,8 +28,8 @@ keywords = [
license = "Apache-2.0"
readme = "README.md"
repository = "https://github.com/matrix-construct/tuwunel"
rust-version = "1.89.0"
version = "1.4.6"
rust-version = "1.91.1"
version = "1.5.0"
[workspace.metadata.crane]
name = "tuwunel"
@@ -76,6 +76,7 @@ version = "0.7"
version = "0.10"
default-features = false
features = [
"cookie",
"typed-header",
"tracing",
]
@@ -231,6 +232,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 +243,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"]
@@ -308,10 +313,12 @@ version = "1.12"
version = "0.12"
default-features = false
features = [
"rustls-tls-native-roots",
"socks",
"charset",
"hickory-dns",
"http2",
"json",
"rustls-tls-native-roots",
"socks",
]
[workspace.dependencies.ring]
@@ -320,7 +327,7 @@ default-features = false
[workspace.dependencies.ruma]
git = "https://github.com/matrix-construct/ruma"
rev = "db4bf59eaa1757735f38489f8924348ae05a6a17"
rev = "069b3f6ee7499cdac84175946aeb9443d2578363"
features = [
"__compat",
"appservice-api-c",
@@ -335,6 +342,7 @@ features = [
"ring-compat",
"unstable-msc2448",
"unstable-msc2666",
"unstable-msc2815",
"unstable-msc2867",
"unstable-msc2870",
"unstable-msc3026",
@@ -360,7 +368,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"
@@ -368,14 +376,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]
@@ -412,6 +422,10 @@ version = "1.0"
default-features = false
features = ["rc"]
[workspace.dependencies.serde_core]
version = "1.0"
default-features = false
[workspace.dependencies.serde_html_form]
version = "0.2"
@@ -475,30 +489,6 @@ 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.48"
default-features = false
@@ -542,7 +532,7 @@ features = [
]
[workspace.dependencies.tracing]
version = "0.1"
version = "=0.1.43"
default-features = false
[workspace.dependencies.tracing-core]
@@ -830,6 +820,7 @@ elided-lifetimes-in-paths = "warn"
explicit-outlives-requirements = "warn"
ffi-unwind-calls = "warn"
keyword-idents = "warn"
macro-extended-temporary-scopes = "warn"
macro-use-extern-crate = "warn"
meta-variable-misuse = "warn"
missing-abi = "warn"
@@ -840,6 +831,7 @@ rust-2021-incompatible-closure-captures = "warn"
rust-2021-incompatible-or-patterns = "warn"
rust-2021-prefixes-incompatible-syntax = "warn"
rust-2021-prelude-collisions = "warn"
semicolon-in-expressions-from-macros = "warn"
single-use-lifetimes = "warn"
trivial-casts = "warn"
trivial-numeric-casts = "warn"
@@ -852,6 +844,7 @@ unused-extern-crates = "warn"
unused-import-braces = "warn"
unused-lifetimes = "warn"
unused-macro-rules = "warn"
unused-must-use = "warn"
unused-qualifications = "warn"
#unused-results = "warn" # TODO
@@ -928,7 +921,6 @@ if_then_some_else_none = { level = "allow", priority = 1 }
inline_always = { level = "allow", priority = 1 }
map_unwrap_or = { level = "allow", priority = 1 }
match_bool = { level = "allow", priority = 1 }
match_same_arms = { level = "allow", priority = 1 }
missing_docs_in_private_items = { level = "allow", priority = 1 }
missing_errors_doc = { level = "allow", priority = 1 }
missing_panics_doc = { level = "allow", priority = 1 }
@@ -940,7 +932,6 @@ single_match_else = { level = "allow", priority = 1 }
struct_excessive_bools = { level = "allow", priority = 1 }
struct_field_names = { level = "allow", priority = 1 }
unnecessary_wraps = { level = "allow", priority = 1 }
unused_async = { level = "allow", priority = 1 }
###################
perf = { level = "warn", priority = -1 }
@@ -948,7 +939,6 @@ perf = { level = "warn", priority = -1 }
###################
#restriction = "warn"
#allow_attributes = "warn" # UNSTABLE
arithmetic_side_effects = "warn"
as_conversions = "warn"
as_underscore = "warn"
@@ -999,7 +989,7 @@ style = { level = "warn", priority = -1 }
assertions_on_constants = { level = "allow", priority = 1 }
module_inception = { level = "allow", priority = 1 }
obfuscated_if_else = { level = "allow", priority = 1 }
collapsible_if = { level = "allow", priority = 1 } # TODO stable v. nightly 05-21-25
toplevel-ref-arg = { level = "allow", priority = 1 }
###################
suspicious = { level = "warn", priority = -1 }
+18 -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,21 @@
<!-- ANCHOR: body -->
[![Documentation](https://img.shields.io/badge/documentation%2D_?color=%238A2BE2&style=for-the-badge&logo=mdBook&logoColor=FFFFFF)](https://matrix-construct.github.io/tuwunel/)
[![Demo Server](https://img.shields.io/badge/demo%20server%2D_?color=%238A2BE2&style=for-the-badge&logo=Element&logoColor=FFFFFF)](https://try.tuwunel.chat)
[![Support Chat](https://img.shields.io/matrix/tuwunel%3Amatrix.org.svg?color=098A09&style=for-the-badge&label=Support%20Chat&labelColor=8A2BE2&logo=Matrix)](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 +40,8 @@ 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).
- Alpine package available as [tuwunel](https://pkgs.alpinelinux.org/package/edge/testing/x86_64/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
+63 -23
View File
@@ -1,47 +1,87 @@
# Tuwunel 1.4.6
# Tuwunel 1.5.0
November 6, 2025
January 31, 2025
### New Features
### New Features & Enhancements
- Element Call discovery support was implemented by @tototomate123 in (#209). Adding a `[[global.well_known.rtc_transports]]` section in your [config file](https://github.com/matrix-construct/tuwunel/blob/e1f89b69ea117f166be423f035a5a34f4c0e7366/tuwunel-example.toml#L1835-L1851) enables discovery. More information on setting up Element Call can be found at [Spaetzblog](https://sspaeth.de/2024/11/sfu/), skipping step one, and performing step three in your Tuwunel config.
- SSO/OIDC support. This feature allows users to register and login via authorizations from OIDC Identity Providers. For example, you can now use your GitHub account to register on the server. Tuwunel implements the OIDC client protocol directly. This is referred to as "legacy SSO" in the Matrix specification; Matrix client support is widespread. Credit to @samip5 for opening the feature-issue (#7), the most 👍 feature of the project.
- Dehydrated Device support (MSC3814) is now available (#200). This feature allows users to receive encrypted messages without being logged in. Supporting clients will setup the dehydrated device automatically and it will "just work" behind the scenes; in fact, these clients will also hide it from the sessions list to avoid confusion. Support is not widespread yet but it has been tested with matrix-js-sdk clients such as Element-web. This feature was commissioned and made public by an enterprise sponsor.
- [MSC2815](https://github.com/matrix-org/matrix-spec-proposals/pull/2815) has been implemented, allowing configurable redacted event retention and retrieval by room admins. The content of redacted events is persisted for sixty days by default. Redacted events can be viewed using Gomuks.
- Notification panel (the 🔔 button) has been implemented in (#201). Even though Element-web now requires enabling it in the Labs menu, the underlying support (`GET /_matrix/client/v3/notifications`) enhances the push-notification handling of other clients.
- Secure limited-use registration token support was implemented by @dasha-uwu building off earlier work by @gingershaped in (56f3f5ea154). Use this feature with the new `!admin token` set of commands.
- Live room previews are now available. This support (`GET /_matrix/client/v3/events`) allows users, including guests, to sync updates for a public room without joining (4afd6f347b1).
- An outstanding major rework of the presence system by @lhjt in (#264) coordinates conflicting updates from multiple devices and further builds on push suppression features first introduced by @tototomate123.
- Thanks to a suggestion by @cyberdoors in (#29), the configuration option `encryption_enabled_by_default_for_room_type` is now available. This feature can enable encryption for a room even when the client does not. The values `all` or `invite` are accepted, the latter roughly meaning DM's only. Neither are enabled by default.
- [MSC3706](https://github.com/matrix-org/matrix-spec-proposals/pull/3706) has been implemented, improving the performance and reliability of joining rooms over federation (b33e73672b).
### Enhancements
- @VlaDexa implemented reading the `client_secret` configuration for an SSO Identity Provider from a separate file; a recommended secure practice (#256).
- Thank you @tototomate123 for improving the reverse-proxy docs, adding dedicated pages for both [Caddy](https://github.com/matrix-construct/tuwunel/blob/e0a997c22784b453735b24907e607412b153ba56/docs/deploying/reverse-proxy-caddy.md) and [Nginx](https://github.com/matrix-construct/tuwunel/blob/e0a997c22784b453735b24907e607412b153ba56/docs/deploying/reverse-proxy-nginx.md) in (#209). Thanks to @tycode for pointing out the docs were missing for alternative proxies in (#197).
- Special thanks to @winyadepla for adding highly sought Matrix RTC (Element Call) documentation for Tuwunel in (#265) and for having a kind heart to follow up with maintenance in (#270).
- Thanks to an observation by @iwalkalone69 in (#40), the last-seen time for a device in the session list is now updated acceptably. This function piggybacks on the presence system to prevent writing too frequently; testing has never shown it more than a minute or few out of date.
- Thank you @Xerusion for documenting Traefik for deploying Tuwunel in (#259). This will save a lot of time and headache for many new users!
- Thanks to an inquiry by @EntityinArray in (#189) guest-accounts can now be enabled while registration tokens are also enabled to prevent fully open account registration. Note that registration tokens don't apply to guest-accounts and those are still fully open.
- At the request of @ChronosXYZ in (#260), @dasha-uwu implemented a configurable feature to include all local users in search results, rather than limiting to those in public or shared rooms (95121ad905fb).
- Courtesy of @dasha-uwu the list of servers attempted when joining a room is now properly shuffled to increase the odds of finding a viable server, especially if an additional join attempt is made.
- Thanks to a collaboration by @x86pup and @VlaDexa working through Nix maintenance we can now upgrade the MSRV to 1.91.1 (#275).
- Thank you @scvalex for updating the README indicating Tuwunel is in stable NixOS (#233).
- Thank you @divideableZero for updating the README with great news about an [Alpine Package](https://pkgs.alpinelinux.org/package/edge/testing/x86_64/tuwunel) (#248).
- Storage hardware characteristics for mdraid devices on Linux are now detected. On these systems we can now shape database requests to increase performance above generic defaults.
- EdDSA is now a supported algorithm for JWT logins. Thank you @vnhdx for the excellent report in (#258).
- Optimizations were made to maximize concurrency and cache performance when gathering the `auth_chain`.
- An admin command to manually remove a pusher is available (note: not intended for normal use).
- An admin command to list local users by recent activity was added.
### Bug Fixes
- Special thanks to @BVollmerhaus for finding the TURN secret file configured by `turn_secret_file` was broken in (#211), forcing users to configure `turn_secret` directly. Thank you for fixing this in (#212).
- LDAP users are now auto-joined to configured rooms upon creation. Thank you @yefimg for (#234), we especially appreciate help from domain-experts on these features.
- Thank you @scvalex for updating the nix build for Tuwunel's integration tests and re-enabling all checks. (#215)
- A surgical fix by @kuhnchris in (#254) addressed a pesky bug where LDAP logins would result in admin privileges being removed for the user. Thank you @foxing-quietly for reporting in (#236).
- Thanks to a report by @Anagastes in (#146) **Nheko and NeoChat users can now enjoy properly verified devices.** Special thanks for the assistance of @deepbluev7 with diagnosing the cross-signing signature issue.
- @OptimoSupreme fixed issues with unread notification counting, including eliminating one of the last remaining non-async database calls in the codebase in (#253).
- Database columns intended for deletion, notably `roomsynctoken_shortstatehash`, never had the deletion command actually invoked on them 😭 explaining the lack of enthusiasm after the 1.4.3 release introduced stateless sync. **Users will now see the free disk space they were promised.** This was uncovered during an unrelated issue investigation courtesy of @frebib.
- @x86pup fixed linker issues for platforms without static builds of `io_uring`. Thanks @darix for reporting in (#238).
- Thanks to investigation by @dasha-uwu the pagination tokens in the `/relations` endpoint were buggy and now operate correctly.
- @x86pup fixed compatibility for our optimized jemalloc build on macOS (#239).
- Thanks to @Polve for identifying the `DynamicUser=yes` directive in the systemd files was invalid and advising a replacement in (#207).
- @dasha-uwu made Livekit operate properly even when federation is disabled (b5f50c3fda3). Thank you @apodavalov for reporting in (#240).
- Thanks to @daudix for reporting an edge-case where the server will refuse to start rather than robustly reporting errors during startup checks and recreate a missing media directory (#213).
- Thank you @VlaDexa for updating the `Cache-Control` header to cache media as `private` which is more appropriate now in the Authenticated Media era.
- Push rule evaluation was never implemented for invites arriving over federation. Notifications are now properly sent in this case.
- Appservices now receive events properly matching on the sender MXID's localpart thanks to @dasha-uwu (c5508bba58d0).
- Sliding-sync handlers were susceptible to errors under rare circumstances escaping to cause an HTTP 500, which wreaks havoc on the rust-sdk. This has now been prevented.
- Additional PDU format and compliance checks were added by @dasha-uwu (7b2079f71499).
- Federating with Conduit over several non-essential endpoints was broken. It is unclear whether this affected an actual Conduit release version, but thanks to @kladki a fix is scheduled and we have included a workaround now on this end.
- Codepaths in sync systems which assumed `device_id` from appservices were fixed by @dasha-uwu.
- Auto-joining version 12 rooms was inhibited from a bug fixed by @dasha-uwu in (7115fb2796f).
- Thank you @x86pup for updating our ldap3 dependency with SSL/TLS enhancements in (#243) and fixing errors reported by @fruzitent in (#108).
- Thanks to @x86pup `join_rule` is now properly defaulted in `/publicRooms` responses in (#244); additional compliance tests now pass!
- Thank you @bdfd9 for reporting a regression where tracing spans around registrations did not filter out passwords from the list of fields.
- The timezone and extended profile features were not correctly stabilized last summer and the `m.tz` field was incorrectly labeled `tz`. Thank you @bunnyblack:matrix.org for reporting in #tuwunel:matrix.org.
- @dasha-uwu fixed git tags not being pulled and applied to CI builds (eadc9e782d8).
- @dasha-uwu fixed a bug in sliding-sync which may result in lost invites (fd519ff7f174).
- `since` tokens in legacy sync are now clamped to a maximum when the client sends a value greater than expected, preventing a possibility of missing events during the request.
- Media deletion commands which are time-based suffered a bug from incorrect creation timestamps on some filesystems. This was resolved by exclusively using the `mtime` attribute, which is acceptable because Matrix media is immutable.
- Queries for the deprecated `_matrix._tcp` SRV record have been reactivated due to an ineffective and unenforced sunset by the specification and other implementations.
- Thank you @x86pup and @dasha-uwu for various maintenance and linting efforts for the latest rustc versions and in general.
### Honorable Mentions
- Please take a moment to recognize how lucky we are to have @scvalex as our NixOS package maintainer. From having the wherewithal to rise above the noise and lend this project trust from the very first days, time and again this gentleman has gone above and beyond on our behalf. Thank you @symphorien at NixOS as well for the patch applied surgically in https://github.com/NixOS/nixpkgs/pull/462394.
+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" },
+11 -10
View File
@@ -1,10 +1,11 @@
(import
(
let lock = builtins.fromJSON (builtins.readFile ./flake.lock); in
fetchTarball {
url = lock.nodes.flake-compat.locked.url or "https://github.com/edolstra/flake-compat/archive/${lock.nodes.flake-compat.locked.rev}.tar.gz";
sha256 = lock.nodes.flake-compat.locked.narHash;
}
)
{ src = ./.; }
).defaultNix
(import (
let
lock = builtins.fromJSON (builtins.readFile ./flake.lock);
in
fetchTarball {
url =
lock.nodes.flake-compat.locked.url
or "https://github.com/edolstra/flake-compat/archive/${lock.nodes.flake-compat.locked.rev}.tar.gz";
sha256 = lock.nodes.flake-compat.locked.narHash;
}
) { src = ./.; }).defaultNix
+2
View File
@@ -18,6 +18,7 @@ ARG RUST_BACKTRACE
ARG JEMALLOC_OVERRIDE
ARG ROCKSDB_LIB_DIR
ARG VALGRINDFLAGS=""
ARG VALGRIND_MAX_WORKERS=128
ARG CARGO_BUILD_RUSTFLAGS
ARG CARGO_PROFILE_TEST_DEBUG
ARG CARGO_PROFILE_TEST_INCREMENTAL
@@ -53,6 +54,7 @@ ENV RUST_BACKTRACE="${RUST_BACKTRACE}"
ENV JEMALLOC_OVERRIDE="${JEMALLOC_OVERRIDE}"
ENV ROCKSDB_LIB_DIR="${ROCKSDB_LIB_DIR}"
ENV VALGRINDFLAGS="${VALGRINDFLAGS}"
ENV TUWUNEL_DB_POOL_MAX_WORKERS="${VALGRIND_MAX_WORKERS}"
ENV CARGO_PROFILE_TEST_DEBUG="${CARGO_PROFILE_TEST_DEBUG}"
ENV CARGO_PROFILE_TEST_INCREMENTAL="${CARGO_PROFILE_TEST_INCREMENTAL}"
ENV CARGO_PROFILE_BENCH_DEBUG="${CARGO_PROFILE_BENCH_DEBUG}"
+5 -3
View File
@@ -62,8 +62,8 @@ COPY <<EOF complement.toml
database_path = "/database"
dns_attempts = 20
dns_timeout = 60
error_on_unknown_config_opts = true
federation_idle_timeout = 300
intentionally_unknown_config_option_for_testing = true
ip_range_denylist = []
log = "debug,tuwunel=trace,h2=warn,hyper=warn"
log_colors = false
@@ -111,6 +111,7 @@ ENTRYPOINT tuwunel -Oserver_name=\""$SERVER_NAME\""
FROM input AS complement-testee-valgrind
ARG db_pool_max_workers=128
EXPOSE 8008 8448
RUN mkdir /database
@@ -122,13 +123,14 @@ ENTRYPOINT valgrind \
--exit-on-first-error=yes \
--error-exitcode=1 \
tuwunel \
-Oserver_name=\""$SERVER_NAME\""
-Oserver_name=\""$SERVER_NAME\"" \
-Odb_pool_max_workers=${db_pool_max_workers}
FROM input AS complement-base
ARG var_cache
ARG var_lib_apt
ARG complement_ref="426bc3458e5287398ec39ab7d2b8f2e3103dcf39"
ARG complement_ref="4d3130f06d0dc3f794b5d48fbdba0b466792b52b"
ARG complement_tags="conduwuit_blacklist"
ARG complement_tests="./tests/..."
ARG complement_run=".*"
+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
+1
View File
@@ -35,6 +35,7 @@ COPY <<EOF tuwunel.toml
allow_public_room_directory_without_auth = true
allow_registration = true
create_admin_room = false
error_on_unknown_config_opts = true
ip_range_denylist = []
log = "debug,tuwunel=trace,h2=warn,hyper=warn"
log_colors = false
+1
View File
@@ -65,6 +65,7 @@ RUN \
-- \
-Otest='["smoke", "fresh"]' \
-Oserver_name=\"localhost\" \
-Oerror_on_unknown_config_opts=true \
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}" \
+8 -3
View File
@@ -19,7 +19,8 @@ RUN <<EOF
tuwunel \
-Otest='["smoke"]' \
-Oserver_name=\"localhost\" \
-Odatabase_path=\"${TUWUNEL_DATABASE_PATH}\"
-Odatabase_path=\"${TUWUNEL_DATABASE_PATH}\" \
-Oerror_on_unknown_config_opts=true
rm -rf "${TUWUNEL_DATABASE_PATH}"
EOF
@@ -28,6 +29,7 @@ EOF
FROM input AS smoke-valgrind
ARG leak_check="no"
ARG undef_value_errors="no"
ARG db_pool_max_workers=128
COPY --link --from=input . .
ENV TUWUNEL_LOG="info"
ENV TUWUNEL_DATABASE_PATH="/tmp/smoketest.db"
@@ -42,7 +44,9 @@ RUN <<EOF
tuwunel \
-Otest='["smoke"]' \
-Oserver_name=\"localhost\" \
-Odatabase_path=\"${TUWUNEL_DATABASE_PATH}\"
-Odatabase_path=\"${TUWUNEL_DATABASE_PATH}\" \
-Odb_pool_max_workers=${db_pool_max_workers} \
-Oerror_on_unknown_config_opts=true
rm -rf "${TUWUNEL_DATABASE_PATH}"
EOF
@@ -60,7 +64,8 @@ RUN <<EOF
tuwunel \
-Otest='["smoke"]' \
-Oserver_name=\"localhost\" \
-Odatabase_path=\"${TUWUNEL_DATABASE_PATH}\"
-Odatabase_path=\"${TUWUNEL_DATABASE_PATH}\" \
-Oerror_on_unknown_config_opts=true
rm -rf "${TUWUNEL_DATABASE_PATH}"
EOF
+27 -6
View File
@@ -84,6 +84,9 @@ variable "rocksdb_build_type" {
variable "rocksdb_make_verbose" {
default = "ON"
}
variable "rocksdb_numa" {
default = "0"
}
# Complement options
variable "complement_count" {
@@ -447,10 +450,18 @@ group "integration" {
]
}
variable "valgrind_max_workers" {
default = 128
}
variable "valgrind_flags" {
default = "--error-exitcode=1 --exit-on-first-error=yes --undef-value-errors=no --leak-check=no"
}
variable "valgrind_testee_args" {
default = "-Odb_pool_max_workers=${valgrind_max_workers}"
}
target "rust-sdk-valgrind" {
name = elem("rust-sdk-valgrind", [cargo_profile, rust_toolchain, rust_target, feat_set, sys_name, sys_version, sys_target])
tags = [
@@ -466,7 +477,7 @@ target "rust-sdk-valgrind" {
}
args = {
VALGRINDFLAGS = "${valgrind_flags}"
mrsdk_testee = "valgrind ${valgrind_flags} /usr/bin/tuwunel"
mrsdk_testee = "valgrind ${valgrind_flags} /usr/bin/tuwunel ${valgrind_testee_args}"
mrsdk_test_args = ""
mrsdk_startup_delay = "30s"
mrsdk_skip_list =<<EOF
@@ -519,6 +530,7 @@ target "integ-valgrind" {
input = elem("target:build-tests", [cargo_profile, rust_toolchain, rust_target, feat_set, sys_name, sys_version, sys_target])
}
args = {
VALGRIND_MAX_WORKERS = "${valgrind_max_workers}"
VALGRINDFLAGS = "${valgrind_flags}"
cargo_cmd = "valgrind test"
cargo_args = "--test=*"
@@ -865,6 +877,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":
""
)
}
}
@@ -1411,7 +1429,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")?
@@ -1433,7 +1451,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")?
@@ -1453,7 +1471,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")?
@@ -1473,7 +1491,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")?
@@ -1552,6 +1570,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
@@ -1716,7 +1735,7 @@ cargo_installs = [
"cargo-deb",
"cargo-generate-rpm",
"cargo-valgrind",
#"lychee",
"lychee",
"mdbook",
"typos-cli",
]
@@ -1805,6 +1824,8 @@ kitchen_packages = [
"gzip",
"jq",
"libc6-dev",
"libclang-dev",
"libnuma-dev",
"libssl-dev",
"libsqlite3-dev",
"make",
+2 -1
View File
@@ -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"
}
@@ -99,6 +99,7 @@ docker logs -f "$cid"
docker wait "$cid" 2>/dev/null
extract_results
extract_output
git diff -U0 --color --shortstat "$result_dst" | (grep "$run" || true)
git diff --quiet --exit-code "$result_dst"
+2
View File
@@ -7,6 +7,7 @@
- [Generic](deploying/generic.md)
- [Reverse Proxy - Caddy](deploying/reverse-proxy-caddy.md)
- [Reverse Proxy - Nginx](deploying/reverse-proxy-nginx.md)
- [Reverse Proxy - Traefik](deploying/reverse-proxy-traefik.md)
- [NixOS](deploying/nixos.md)
- [Docker](deploying/docker.md)
- [Kubernetes](deploying/kubernetes.md)
@@ -15,6 +16,7 @@
- [Red Hat](deploying/redhat.md)
- [FreeBSD](deploying/freebsd.md)
- [TURN](turn.md)
- [Matrix RTC (Element Call)](matrix_rtc.md)
- [Appservices](appservices.md)
- [Maintenance](maintenance.md)
- [Troubleshooting](troubleshooting.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
+4 -3
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
@@ -147,6 +147,7 @@ We recommend Caddy as a reverse proxy, as it is trivial to use, handling TLS cer
- **[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
- **[Traefik Setup Guide](reverse-proxy-traefik.md)** - _Please help us document this choice_
### Quick Overview
+128
View File
@@ -0,0 +1,128 @@
# Reverse Proxy Setup - Traefik
[<= Back to Generic Deployment Guide](generic.md#setting-up-the-reverse-proxy)
## Installation
Install Traefik via your preferred method. You can read the official [docker quickstart guide](https://doc.traefik.io/traefik/getting-started/docker/) or the [in-depth walkthrough](https://doc.traefik.io/traefik/setup/docker/)
## Configuration
### TLS certificates
You can setup auto renewing certificates with different kinds of [acme challenges](https://doc.traefik.io/traefik/reference/install-configuration/tls/certificate-resolvers/acme/).
### Router configurations
Add tuwunel to your traefik's network.
```yaml
services:
tuwunel:
# ...
networks:
- proxy # your traefik network name
networks:
proxy: # your traefik network name
external: true
```
Be sure to change the `your.server.name` to your actual tuwunel domain. and the `yourcertresolver` should be changed to whatever you named it in your traefik config.
You only have to do any one of these methods below.
### Labels
To use labels with traefik you need to configure a [docker provider](https://doc.traefik.io/traefik/reference/install-configuration/providers/docker/).
Then add the labels in your tuwunel's docker compose file.
```yaml
services:
tuwunel:
# ...
labels:
- "traefik.enable=true"
- "traefik.http.routers.tuwunel.entrypoints=web"
- "traefik.http.routers.tuwunel.rule=Host(`your.server.name`)"
- "traefik.http.routers.tuwunel.middlewares=https-redirect@file"
- "traefik.http.routers.tuwunel-secure.entrypoints=websecure"
- "traefik.http.routers.tuwunel-secure.rule=Host(`your.server.name`)"
- "traefik.http.routers.tuwunel-secure.tls=true"
- "traefik.http.routers.tuwunel-secure.service=tuwunel"
- "traefik.http.services.tuwunel.loadbalancer.server.port=6167"
- "traefik.http.routers.tuwunel-secure.tls.certresolver=yourcertresolver"
- "traefik.docker.network=proxy"
```
### Config File
To use the config file you need to configure a [file provider](https://doc.traefik.io/traefik/reference/install-configuration/providers/others/file/).
Then add this into your config file.
```yaml
http:
routers:
tuwunel:
entryPoints:
- "web"
- "websecure"
rule: "Host(`your.server.name`)"
middlewares:
- https-redirect
tls:
certResolver: "yourcertresolver"
service: tuwunel
services:
tuwunel:
loadBalancer:
servers:
# this url should point to your tuwunel installation.
# this should work if your tuwunel container is named tuwunel and is in the same network as traefik.
- url: "http://tuwunel:6167"
passHostHeader: true
```
### Federation
If you will use a .well-known file you can use traefik to redirect .well-known/matrix to tuwunel built-in .well-known file.
replace the rule in either of the methods from
```
Host(`your.server.name`)
```
to
```
Host(`your.tuwunel.domain`) || Host(`your.server.name`) && PathPrefix(`/.well-known/matrix`)
```
If you are not using a .well-known file you will need to add and expose port 8448 to a [traefik entrypoint](https://doc.traefik.io/traefik/reference/install-configuration/entrypoints/).
You can then add these to your preferred traefik config method.
you should replace `matrixfederationentry` with what you named your entrypoint.
Labels:
```yaml
- "traefik.http.routers.matrix-federation.entrypoints=matrixfederationentry"
- "traefik.http.routers.matrix-federation.rule=Host(`your.server.name`)"
- "traefik.http.routers.matrix-federation.tls=true"
- "traefik.http.routers.matrix-federation.service=matrix-federation"
- "traefik.http.services.matrix-federation.loadbalancer.server.port=6167"
- "traefik.http.routers.matrix-federation.tls.certresolver=yourcertresolver"
```
Config file:
```yaml
entryPoints:
- "web"
- "websecure"
- "matrixfederationentry"
```
> [!IMPORTANT]
>
> [Encoded Character Filtering](https://doc.traefik.io/traefik/security/request-path/#encoded-character-filtering)
> options must be set to `true`. This only applies to traefik version 3.6.4 to 3.6.6 and 2.11.32 to 2.11.34
## Verification
After starting Traefik, 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)
+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
+299
View File
@@ -0,0 +1,299 @@
# Matrix RTC/Element Call Setup
## Notes
- `yourdomain.com` is whatever you have set as `server_name` in your tuwunel.toml. This needs to be replaced with the actual domain. It is assumed that you will be hosting MatrixRTC at `matrix-rtc.yourdomain.com`. If you wish to host this service at a different subdomain, this needs to be replaced as well.
- This guide provides example configuration for Caddy and Nginx reverse proxies. Others can be used, but the configuration will need to be adapted.
## Instructions
### 1. Set Up DNS
Create a DNS record for `matrix-rtc.yourdomain.com` pointing to your server.
### 2. Create Docker Containers
1. Create a directory for your MatrixRTC setup e.g. `mkdir /opt/matrix-rtc`.
2. Change directory to your new directory. e.g. `cd /opt/matrix-rtc`.
3. Create and open a compose.yaml file for MatrixRTC. e.g. `nano compose.yaml`.
4. Add the following. `mrtckey` and `mrtcsecret` should be random strings. It is suggested that `mrtckey` is 20 characters and `mrtcsecret` is 64 characters.
```yaml
services:
matrix-rtc-jwt:
image: ghcr.io/element-hq/lk-jwt-service:latest
container_name: matrix-rtc-jwt
environment:
- LIVEKIT_JWT_PORT=8081
- LIVEKIT_URL=wss://matrix-rtc.yourdomain.com
- LIVEKIT_KEY=mrtckey
- LIVEKIT_SECRET=mrtcsecret
- LIVEKIT_FULL_ACCESS_HOMESERVERS=yourdomain.com
restart: unless-stopped
ports:
- "8081:8081"
matrix-rtc-livekit:
image: livekit/livekit-server:latest
container_name: matrix-rtc-livekit
command: --config /etc/livekit.yaml
ports:
- 7880:7880/tcp
- 7881:7881/tcp
- 50100-50200:50100-50200/udp
restart: unless-stopped
volumes:
- ./livekit.yaml:/etc/livekit.yaml:ro
```
4. Close the file.
5. Create and open a livekit.yaml file. e.g. `nano livekit.yaml`.
6. Add the following. `mrtckey` and `mrtcsecret` should be the same as those from compose.yaml.
```yaml
port: 7880
bind_addresses:
- ""
rtc:
tcp_port: 7881
port_range_start: 50100
port_range_end: 50200
use_external_ip: true
enable_loopback_candidate: false
keys:
mrtckey: "mrtcsecret"
```
7. Close the file.
### 3. Configure .well-known
#### 3.1. .well-known served by Tuwunel
***Follow this step if your .well-known configuration is served by tuwunel. Otherwise follow Step 3.2***
1. Open your tuwunel.toml file. e.g. `nano /etc/tuwunel/tuwunel.toml`.
2. Find the line reading `#rtc_transports = []` and replace it with:
```toml
[[global.well_known.rtc_transports]]
type = "livekit"
livekit_service_url = "https://matrix-rtc.yourdomain.com"
```
3. Close the file.
#### 3.2. .well-known served independently
***Follow this step if you serve your .well-known/matrix files directly. Otherwise follow Step 3.1***
1. Open your `.well-known/matrix/client` file. e.g. `nano /var//www/.well-known/matrix/client`.
2. Add the following:
```json
"org.matrix.msc4143.rtc_foci": [
{
"type": "livekit",
"livekit_service_url": "https://matrix-rtc.yourdomain.com"
}
]
```
The final file should look something like this:
```json
{
"m.homeserver": {
"base_url":"https://matrix.yourdomain.com"
},
"org.matrix.msc4143.rtc_foci": [
{
"type": "livekit",
"livekit_service_url": "https://matrix-rtc.yourdomain.com"
}
]
}
```
3. Close the file.
### 4. Configure Firewall
You will need to allow ports `7881/tcp` and `50100:50200/udp` through your firewall. If you use UFW, the commands are: `ufw allow 7881/tcp` and `ufw allow 50100:50200/udp`.
### 5. Configure Reverse Proxy
As reverse proxies can be installed in different ways, step by step instructions are not given for this section.
If you use Caddy as your reverse proxy, follow step 5.1. If you use Nginx, follow step 5.2.
#### 5.1. Caddy
1. Add the following to your Caddyfile. If you are running Caddy in Docker, replace `localhost` with `matrix-rtc-jwt` in the first instance, and `matrix-rtc-livekit` in the second.
```
matrix-rtc.yourdomain.com {
# This is matrix-rtc-jwt
@jwt_service {
path /sfu/get* /healthz*
}
handle @jwt_service {
reverse_proxy localhost:8081 {
header_up Host {host}
header_up X-Forwarded-Server {host}
header_up X-Real-IP {remote}
header_up X-Forwarded-For {remote}
header_up X-Forwarded-Proto {scheme}
}
}
# This is livekit
handle {
reverse_proxy localhost:7880 {
header_up Connection "upgrade"
header_up Upgrade {http.request.header.Upgrade}
header_up Host {host}
header_up X-Forwarded-Server {host}
header_up X-Real-IP {remote}
header_up X-Forwarded-For {remote}
header_up X-Forwarded-Proto {scheme}
}
}
}
```
2. Restart Caddy.
#### 5.2. Nginx
1. Add the following to your Nginx configuration. If you are running Nginx in Docker, replace `localhost` with `matrix-rtc-jwt` in the first instance, and `matrix-rtc-livekit` in the second.
```
server {
listen 443 ssl;
listen [::]:443 ssl;
http2 on;
server_name matrix-rtc.yourdomain.com;
# Logging
access_log /var/log/nginx/matrix-rtc.yourdomain.com.log;
error_log /var/log/nginx/matrix-rtc.yourdomain.com.error;
# TLS example for certificate obtained from Let's Encrypt.
ssl_certificate /etc/letsencrypt/live/matrix-rtc.yourdomain.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/matrix-rtc.yourdomain.com/privkey.pem;
# lk-jwt-service
location ~ ^(/sfu/get|/healthz) {
proxy_pass http://localhost:8081;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
# livekit
location / {
proxy_pass http://localhost:7880;
proxy_http_version 1.1;
proxy_set_header Connection "upgrade";
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
# Optional timeouts per LiveKit
proxy_read_timeout 300s;
proxy_send_timeout 300s;
}
}
```
2. Restart Nginx.
#### 5.3. Traefik
1. Add your `matrix-rtc-jwt` `matrix-rtc-livekit` to your traefik's network
```yaml
services:
matrix-rtc-jwt:
# ...
networks:
- proxy # your traefik network name
matrix-rtc-livekit:
# ...
networks:
- proxy # your traefik network name
networks:
proxy: # your traefik network name
external: true
```
2. Configure with either one of the methods below
2.1 Labels
```yaml
services:
matrix-rtc-jwt:
# ...
labels:
- "traefik.enable=true"
- "traefik.http.routers.matrixrtcjwt.entrypoints=websecure"
- "traefik.http.routers.matrixrtcjwt.rule=Host(`matrix-rtc.yourdomain.com`) && PathPrefix(`/sfu/get`) || PathPrefix(`/healthz`)"
- "traefik.http.routers.matrixrtcjwt.tls=true"
- "traefik.http.routers.matrixrtcjwt.service=matrixrtcjwt"
- "traefik.http.services.matrixrtcjwt.loadbalancer.server.port=8081"
- "traefik.http.routers.matrixrtcjwt.tls.certresolver=yourcertresolver" # change to your cert resolver's name
- "traefik.docker.network=proxy" # your traefik network name
matrix-rtc-livekit:
# ...
labels:
- "traefik.enable=true"
- "traefik.http.routers.livekit.entrypoints=websecure"
- "traefik.http.routers.livekit.rule=Host(`matrix-rtc.yourdomain.com`)"
- "traefik.http.routers.livekit.tls=true"
- "traefik.http.routers.livekit.service=livekit"
- "traefik.http.services.livekit.loadbalancer.server.port=7880"
- "traefik.http.routers.livekit.tls.certresolver=yourcertresolver" # change to your cert resolver's name
- "traefik.docker.network=proxy" # your traefik network name
```
2.2 Config file
```yaml
http:
routers:
matrixrtcjwt:
entryPoints:
- "websecure"
rule: "Host(`matrix-rtc.yourdomain.com`) && PathPrefix(`/sfu/get`) || PathPrefix(`/healthz`)"
tls:
certResolver: "yourcertresolver" # change to your cert resolver's name
service: matrixrtcjwt
livekit:
entryPoints:
- "websecure"
rule: "Host(`matrix-rtc.yourdomain.com`)"
tls:
certResolver: "yourcertresolver" # change to your cert resolver's name
service: livekit
services:
matrixrtcjwt:
loadBalancer:
servers:
- url: "http://matrix-rtc-jwt:8081"
passHostHeader: true
livekit:
loadBalancer:
servers:
- url: "http://matrix-rtc-livekit:7880"
passHostHeader: true
```
### 6. Start Docker Containers
1. Ensure you are in your matrix-rtc directory. e.g. `cd /opt/matrix-rtc`.
2. Start containers: `docker compose up -d`.
Element Call should now be working.
## Additional Configuration
### TURN Integration
If you follow this guide, and also set up Coturn as per the tuwunel documentation, there will be a port clash between the two services. To avoid this, the following must be added to your `coturn.conf`:
```
min-port=50201
max-port=65535
```
If you have Coturn configured, you can use it as a TURN server for Livekit to improve call reliability. As Coturn allows multiple instances of `static-auth-secret`, it is suggested that the secret used for Livekit is different to that used for tuwunel.
1. Create a secret for Coturn. It is suggested that this should be a random 64 character alphanumeric string.
2. Add the following line to the end of your `turnserver.conf`. `AUTH_SECRET` is the secret created in Step 1.
```
static-auth-secret=AUTH_SECRET
```
3. Add the following to the end of the `rtc` block in your `livekit.yaml`. `AUTH_SECRET` is the same as above. `turn.yourdomain.com` should be replaced with your actual TURN domain.
```
turn_servers:
- host: turn.yourdomain.com
port: 5349
protocol: tls
secret: "AUTH_SECRET"
```
### Using the Livekit Built In TURN Server
It is also possible to use the built in Livekit TURN server. Getting this to work can be a somewhat involved process, and a TURN server is not usually required for Matrix RTC calls. Consequently, instructions are not provided here at this time. If you would like to configure this, more information can be found [here](https://docs.livekit.io/transport/self-hosting/deployment/#improving-connectivity-with-turn).
Generated
+27 -27
View File
@@ -32,11 +32,11 @@
"nixpkgs": "nixpkgs_2"
},
"locked": {
"lastModified": 1761150191,
"narHash": "sha256-fPQmV+K836jR/Tu6slfMdm9HL3p0IEPB5x5I7Zc6v5E=",
"lastModified": 1763236786,
"narHash": "sha256-JB19RGXDr6loKSdqwvA15jhRHwf6+9Crq2glqqVar84=",
"owner": "cachix",
"repo": "cachix",
"rev": "5629a7d84c91df9c06650b18eb428cce4166c4fc",
"rev": "938a275857047c300596092beaabaee6d892e243",
"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": 1760924934,
"narHash": "sha256-tuuqY5aU7cUkR71sO2TraVKK2boYrdW3gCSXUkF4i44=",
"lastModified": 1763511871,
"narHash": "sha256-KKZWi+ij7oT0Ag8yC6MQkzfHGcytyjMJDD+47ZV1YNU=",
"owner": "ipetkov",
"repo": "crane",
"rev": "c6b4d5308293d0d04fcfeee92705017537cad02f",
"rev": "099f9014bc8d0cd6e445470ea1df0fd691d5a548",
"type": "github"
},
"original": {
@@ -170,11 +170,11 @@
"rust-analyzer-src": "rust-analyzer-src"
},
"locked": {
"lastModified": 1761547629,
"narHash": "sha256-4OH1CVm2PdjKRqEJ3RLfkQMDSBdn7VId6iyYCwKOK+U=",
"lastModified": 1763707297,
"narHash": "sha256-Bd9VGavwFBLpyU4pjiWfv73gUibNj8dc3xmOW8ff3bI=",
"owner": "nix-community",
"repo": "fenix",
"rev": "d82a7c64ea441e397914577c9a18f2867e5b364b",
"rev": "7c2d3a165a4a080fdcb6c191d8f9768281c99f75",
"type": "github"
},
"original": {
@@ -219,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": {
@@ -346,11 +346,11 @@
"liburing": {
"flake": false,
"locked": {
"lastModified": 1761487364,
"narHash": "sha256-PmRdt9dQUFGcVrX7XCh7z2FpRm1SqKgiYbIwpkJPjRU=",
"lastModified": 1763758538,
"narHash": "sha256-cDsxLOqeC7imBcArolTHvejSnWoadgpvDy1DJ2/3MOw=",
"owner": "axboe",
"repo": "liburing",
"rev": "65ec4223032a38870dde2bce646ba4de13d2feff",
"rev": "e1ef1e680ee38ed9116989155fca47921698c25f",
"type": "github"
},
"original": {
@@ -493,11 +493,11 @@
},
"nixpkgs_3": {
"locked": {
"lastModified": 1761349956,
"narHash": "sha256-tH3wHnOJms+U4k/rK2Nn1RfBrhffX92jLP/2VndSn0w=",
"lastModified": 1763618868,
"narHash": "sha256-v5afmLjn/uyD9EQuPBn7nZuaZVV9r+JerayK/4wvdWA=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "02f2cb8e0feb4596d20cc52fda73ccee960e3538",
"rev": "a8d610af3f1a5fb71e23e08434d8d61a466fc942",
"type": "github"
},
"original": {
@@ -510,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": {
@@ -542,11 +542,11 @@
"rust-analyzer-src": {
"flake": false,
"locked": {
"lastModified": 1761500479,
"narHash": "sha256-syeBTCCU96qPJHcVpwHeCwmPCiLTDHHgYQYhpZ0iwLo=",
"lastModified": 1763648203,
"narHash": "sha256-/WJdebbRD+m5vr2xy/bJdCpqd7YHSMapjuXAM/0lvtA=",
"owner": "rust-lang",
"repo": "rust-analyzer",
"rev": "049767e6faa84b2d1a951d8f227e6ebd99d728a2",
"rev": "eaaa2da9fbbfd7a79ff501e0563351cb2004574a",
"type": "github"
},
"original": {
+486 -437
View File
@@ -2,265 +2,299 @@
inputs = {
attic.url = "github:zhaofengli/attic?ref=main";
cachix.url = "github:cachix/cachix?ref=master";
complement = { url = "github:matrix-construct/complement?ref=main"; flake = false; };
crane = { url = "github:ipetkov/crane?ref=master"; };
fenix = { url = "github:nix-community/fenix?ref=main"; inputs.nixpkgs.follows = "nixpkgs"; };
flake-compat = { url = "github:edolstra/flake-compat?ref=master"; flake = false; };
complement = {
url = "github:matrix-construct/complement?ref=main";
flake = false;
};
crane = {
url = "github:ipetkov/crane?ref=master";
};
fenix = {
url = "github:nix-community/fenix?ref=main";
inputs.nixpkgs.follows = "nixpkgs";
};
flake-compat = {
url = "github:edolstra/flake-compat?ref=master";
flake = false;
};
flake-utils.url = "github:numtide/flake-utils?ref=main";
nix-filter.url = "github:numtide/nix-filter?ref=main";
nixpkgs.url = "github:NixOS/nixpkgs?ref=nixpkgs-unstable";
rocksdb = { url = "github:matrix-construct/rocksdb?ref=tuwunel-changes"; flake = false; };
liburing = { url = "github:axboe/liburing?ref=master"; flake = false; };
rocksdb = {
url = "github:matrix-construct/rocksdb?ref=tuwunel-changes";
flake = false;
};
liburing = {
url = "github:axboe/liburing?ref=master";
flake = false;
};
};
outputs = inputs:
inputs.flake-utils.lib.eachDefaultSystem (system:
let
pkgsHost = import inputs.nixpkgs{
inherit system;
};
pkgsHostStatic = pkgsHost.pkgsStatic;
# The Rust toolchain to use
toolchain = inputs.fenix.packages.${system}.fromToolchainFile {
file = ./rust-toolchain.toml;
# See also `rust-toolchain.toml`
sha256 = "sha256-+9FmLhAOezBZCOziO0Qct1NOrfpjNsXxc/8I0c7BdKE=";
};
mkScope = pkgs: pkgs.lib.makeScope pkgs.newScope (self: {
inherit pkgs;
book = self.callPackage ./nix/pkgs/book {};
complement = self.callPackage ./nix/pkgs/complement {};
craneLib = ((inputs.crane.mkLib pkgs).overrideToolchain (_: toolchain));
inherit inputs;
main = self.callPackage ./nix/pkgs/main {};
oci-image = self.callPackage ./nix/pkgs/oci-image {};
tini = pkgs.tini.overrideAttrs {
# newer clang/gcc is unhappy with tini-static: <https://3.dog/~strawberry/pb/c8y4>
patches = [ (pkgs.fetchpatch {
url = "https://patch-diff.githubusercontent.com/raw/krallin/tini/pull/224.patch";
hash = "sha256-4bTfAhRyIT71VALhHY13hUgbjLEUyvgkIJMt3w9ag3k=";
})
];
};
liburing = pkgs.liburing.overrideAttrs {
# Tests weren't building
outputs = [ "out" "dev" "man" ];
buildFlags = [ "library" ];
src = inputs.liburing;
};
rocksdb = (pkgs.rocksdb.override {
liburing = self.liburing;
}).overrideAttrs (final: old: {
src = inputs.rocksdb;
version = pkgs.lib.removePrefix
"v"
(builtins.fromJSON (builtins.readFile ./flake.lock))
.nodes.rocksdb.original.ref;
# we have this already at https://github.com/girlbossceo/rocksdb/commit/a935c0273e1ba44eacf88ce3685a9b9831486155
# unsetting this so i don't have to revert it and make this nix exclusive
patches = [];
postPatch =
pkgs.lib.optionalString (pkgs.lib.versionOlder final.version "8") ''
# Fix gcc-13 build failures due to missing <cstdint> and
# <system_error> includes, fixed upstream since 8.x
sed -e '1i #include <cstdint>' -i db/compaction/compaction_iteration_stats.h
sed -e '1i #include <cstdint>' -i table/block_based/data_block_hash_index.h
sed -e '1i #include <cstdint>' -i util/string_util.h
sed -e '1i #include <cstdint>' -i include/rocksdb/utilities/checkpoint.h
'';
cmakeFlags = pkgs.lib.subtractLists
[
# no real reason to have snappy or zlib, no one uses this
"-DWITH_SNAPPY=1"
"-DZLIB=1"
"-DWITH_ZLIB=1"
# we dont need to use ldb or sst_dump (core_tools)
"-DWITH_CORE_TOOLS=1"
# we dont need to build rocksdb tests
"-DWITH_TESTS=1"
# we use rust-rocksdb via C interface and dont need C++ RTTI
"-DUSE_RTTI=1"
# this doesn't exist in RocksDB, and USE_SSE is deprecated for
# PORTABLE=$(march)
"-DFORCE_SSE42=1"
# PORTABLE will get set in main/default.nix
"-DPORTABLE=1"
]
old.cmakeFlags
++ [
# no real reason to have snappy, no one uses this
"-DWITH_SNAPPY=0"
"-DZLIB=0"
"-DWITH_ZLIB=0"
# we dont need to use ldb or sst_dump (core_tools)
"-DWITH_CORE_TOOLS=0"
# we dont need trace tools
"-DWITH_TRACE_TOOLS=0"
# we dont need to build rocksdb tests
"-DWITH_TESTS=0"
# we use rust-rocksdb via C interface and dont need C++ RTTI
"-DUSE_RTTI=0"
];
# outputs has "tools" which we dont need or use
outputs = [ "out" ];
# preInstall hooks has stuff for messing with ldb/sst_dump which we dont need or use
preInstall = "";
});
});
scopeHost = mkScope pkgsHost;
scopeHostStatic = mkScope pkgsHostStatic;
scopeCrossLinux = mkScope pkgsHost.pkgsLinux.pkgsStatic;
mkCrossScope = crossSystem:
let pkgsCrossStatic = (import inputs.nixpkgs {
outputs =
inputs:
inputs.flake-utils.lib.eachDefaultSystem (
system:
let
pkgsHost = import inputs.nixpkgs {
inherit system;
crossSystem = {
config = crossSystem;
};
pkgsHostStatic = pkgsHost.pkgsStatic;
# The Rust toolchain to use
toolchain = inputs.fenix.packages.${system}.fromToolchainFile {
file = ./rust-toolchain.toml;
# See also `rust-toolchain.toml`
sha256 = "sha256-SDu4snEWjuZU475PERvu+iO50Mi39KVjqCeJeNvpguU=";
};
mkScope =
pkgs:
pkgs.lib.makeScope pkgs.newScope (self: {
inherit pkgs;
book = self.callPackage ./nix/pkgs/book { };
complement = self.callPackage ./nix/pkgs/complement { };
craneLib = ((inputs.crane.mkLib pkgs).overrideToolchain (_: toolchain));
inherit inputs;
main = self.callPackage ./nix/pkgs/main { };
oci-image = self.callPackage ./nix/pkgs/oci-image { };
tini = pkgs.tini.overrideAttrs {
# newer clang/gcc is unhappy with tini-static: <https://3.dog/~strawberry/pb/c8y4>
patches = [
(pkgs.fetchpatch {
url = "https://patch-diff.githubusercontent.com/raw/krallin/tini/pull/224.patch";
hash = "sha256-4bTfAhRyIT71VALhHY13hUgbjLEUyvgkIJMt3w9ag3k=";
})
];
};
liburing = pkgs.liburing.overrideAttrs {
# Tests weren't building
outputs = [
"out"
"dev"
"man"
];
buildFlags = [ "library" ];
src = inputs.liburing;
};
rocksdb =
(pkgs.rocksdb.override {
liburing = self.liburing;
}).overrideAttrs
(
final: old: {
src = inputs.rocksdb;
version = pkgs.lib.removePrefix "v" (builtins.fromJSON (builtins.readFile ./flake.lock))
.nodes.rocksdb.original.ref;
# we have this already at https://github.com/girlbossceo/rocksdb/commit/a935c0273e1ba44eacf88ce3685a9b9831486155
# unsetting this so i don't have to revert it and make this nix exclusive
patches = [ ];
postPatch = pkgs.lib.optionalString (pkgs.lib.versionOlder final.version "8") ''
# Fix gcc-13 build failures due to missing <cstdint> and
# <system_error> includes, fixed upstream since 8.x
sed -e '1i #include <cstdint>' -i db/compaction/compaction_iteration_stats.h
sed -e '1i #include <cstdint>' -i table/block_based/data_block_hash_index.h
sed -e '1i #include <cstdint>' -i util/string_util.h
sed -e '1i #include <cstdint>' -i include/rocksdb/utilities/checkpoint.h
'';
cmakeFlags =
pkgs.lib.subtractLists [
# no real reason to have snappy or zlib, no one uses this
"-DWITH_SNAPPY=1"
"-DZLIB=1"
"-DWITH_ZLIB=1"
# we dont need to use ldb or sst_dump (core_tools)
"-DWITH_CORE_TOOLS=1"
# we dont need to build rocksdb tests
"-DWITH_TESTS=1"
# we use rust-rocksdb via C interface and dont need C++ RTTI
"-DUSE_RTTI=1"
# this doesn't exist in RocksDB, and USE_SSE is deprecated for
# PORTABLE=$(march)
"-DFORCE_SSE42=1"
# PORTABLE will get set in main/default.nix
"-DPORTABLE=1"
] old.cmakeFlags
++ [
# no real reason to have snappy, no one uses this
"-DWITH_SNAPPY=0"
"-DZLIB=0"
"-DWITH_ZLIB=0"
# we dont need to use ldb or sst_dump (core_tools)
"-DWITH_CORE_TOOLS=0"
# we dont need trace tools
"-DWITH_TRACE_TOOLS=0"
# we dont need to build rocksdb tests
"-DWITH_TESTS=0"
# we use rust-rocksdb via C interface and dont need C++ RTTI
"-DUSE_RTTI=0"
];
# outputs has "tools" which we dont need or use
outputs = [ "out" ];
# preInstall hooks has stuff for messing with ldb/sst_dump which we dont need or use
preInstall = "";
}
);
});
scopeHost = mkScope pkgsHost;
scopeHostStatic = mkScope pkgsHostStatic;
scopeCrossLinux = mkScope pkgsHost.pkgsLinux.pkgsStatic;
mkCrossScope =
crossSystem:
let
pkgsCrossStatic =
(import inputs.nixpkgs {
inherit system;
crossSystem = {
config = crossSystem;
};
}).pkgsStatic;
in
mkScope pkgsCrossStatic;
mkDevShell =
scope:
scope.pkgs.mkShell {
env = scope.main.env // {
# Rust Analyzer needs to be able to find the path to default crate
# sources, and it can read this environment variable to do so. The
# `rust-src` component is required in order for this to work.
RUST_SRC_PATH = "${toolchain}/lib/rustlib/src/rust/library";
# Convenient way to access a pinned version of Complement's source
# code.
COMPLEMENT_SRC = inputs.complement.outPath;
# Needed for Complement: <https://github.com/golang/go/issues/52690>
CGO_CFLAGS = "-Wl,--no-gc-sections";
CGO_LDFLAGS = "-Wl,--no-gc-sections";
};
# Development tools
packages = [
# Always use nightly rustfmt because most of its options are unstable
#
# This needs to come before `toolchain` in this list, otherwise
# `$PATH` will have stable rustfmt instead.
inputs.fenix.packages.${system}.latest.rustfmt
toolchain
]
++ (
with pkgsHost.pkgs;
[
# Required by hardened-malloc.rs dep
binutils
cargo-audit
cargo-auditable
# Needed for producing Debian packages
cargo-deb
# Needed for CI to check validity of produced Debian packages (dpkg-deb)
dpkg
engage
# Needed for Complement
go
# Needed for our script for Complement
jq
gotestfmt
# Needed for finding broken markdown links
lychee
# Needed for linting markdown files
markdownlint-cli
# Useful for editing the book locally
mdbook
# used for rust caching in CI to speed it up
sccache
]
# liburing is Linux-exclusive
++ lib.optional stdenv.hostPlatform.isLinux liburing
++ lib.optional stdenv.hostPlatform.isLinux numactl
)
++ scope.main.buildInputs
++ scope.main.propagatedBuildInputs
++ scope.main.nativeBuildInputs;
};
}).pkgsStatic;
in
mkScope pkgsCrossStatic;
mkDevShell = scope: scope.pkgs.mkShell {
env = scope.main.env // {
# Rust Analyzer needs to be able to find the path to default crate
# sources, and it can read this environment variable to do so. The
# `rust-src` component is required in order for this to work.
RUST_SRC_PATH = "${toolchain}/lib/rustlib/src/rust/library";
# Convenient way to access a pinned version of Complement's source
# code.
COMPLEMENT_SRC = inputs.complement.outPath;
# Needed for Complement: <https://github.com/golang/go/issues/52690>
CGO_CFLAGS = "-Wl,--no-gc-sections";
CGO_LDFLAGS = "-Wl,--no-gc-sections";
};
# Development tools
packages = [
# Always use nightly rustfmt because most of its options are unstable
#
# This needs to come before `toolchain` in this list, otherwise
# `$PATH` will have stable rustfmt instead.
inputs.fenix.packages.${system}.latest.rustfmt
toolchain
]
++ (with pkgsHost.pkgs; [
# Required by hardened-malloc.rs dep
binutils
cargo-audit
cargo-auditable
# Needed for producing Debian packages
cargo-deb
# Needed for CI to check validity of produced Debian packages (dpkg-deb)
dpkg
engage
# Needed for Complement
go
# Needed for our script for Complement
jq
gotestfmt
# Needed for finding broken markdown links
lychee
# Needed for linting markdown files
markdownlint-cli
# Useful for editing the book locally
mdbook
# used for rust caching in CI to speed it up
sccache
]
# liburing is Linux-exclusive
++ lib.optional stdenv.hostPlatform.isLinux liburing
++ lib.optional stdenv.hostPlatform.isLinux numactl)
++ scope.main.buildInputs
++ scope.main.propagatedBuildInputs
++ scope.main.nativeBuildInputs;
};
in
{
packages = {
default = scopeHost.main.override {
in
{
packages = {
default = scopeHost.main.override {
disable_features = [
# dont include experimental features
"experimental"
# jemalloc profiling/stats features are expensive and shouldn't
# be expected on non-debug builds.
"jemalloc_prof"
"jemalloc_stats"
# tuwunel_mods is a development-only hot reload feature
"tuwunel_mods"
# dont include experimental features
"experimental"
# jemalloc profiling/stats features are expensive and shouldn't
# be expected on non-debug builds.
"jemalloc_prof"
"jemalloc_stats"
# tuwunel_mods is a development-only hot reload feature
"tuwunel_mods"
];
};
default-debug = scopeHost.main.override {
};
default-debug = scopeHost.main.override {
profile = "dev";
# debug build users expect full logs
disable_release_max_log_level = true;
disable_features = [
# dont include experimental features
"experimental"
# this is non-functional on nix for some reason
"tuwunel_mods"
# dont include experimental features
"experimental"
# this is non-functional on nix for some reason
"tuwunel_mods"
];
};
# just a test profile used for things like CI and complement
default-test = scopeHost.main.override {
};
# just a test profile used for things like CI and complement
default-test = scopeHost.main.override {
profile = "test";
disable_release_max_log_level = true;
disable_features = [
# dont include experimental features
"experimental"
# this is non-functional on nix for some reason
"tuwunel_mods"
# dont include experimental features
"experimental"
# this is non-functional on nix for some reason
"tuwunel_mods"
];
};
all-features = scopeHost.main.override {
};
all-features = scopeHost.main.override {
all_features = true;
disable_features = [
# dont include experimental features
"experimental"
# jemalloc profiling/stats features are expensive and shouldn't
# be expected on non-debug builds.
"jemalloc_prof"
"jemalloc_stats"
# this is non-functional on nix for some reason
"tuwunel_mods"
# dont include experimental features
"experimental"
# jemalloc profiling/stats features are expensive and shouldn't
# be expected on non-debug builds.
"jemalloc_prof"
"jemalloc_stats"
# this is non-functional on nix for some reason
"tuwunel_mods"
];
};
all-features-debug = scopeHost.main.override {
};
all-features-debug = scopeHost.main.override {
profile = "dev";
all_features = true;
# debug build users expect full logs
disable_release_max_log_level = true;
disable_features = [
# dont include experimental features
"experimental"
# this is non-functional on nix for some reason
"tuwunel_mods"
# dont include experimental features
"experimental"
# this is non-functional on nix for some reason
"tuwunel_mods"
];
};
};
oci-image = scopeHost.oci-image;
oci-image-all-features = scopeHost.oci-image.override {
main = scopeHost.main.override {
all_features = true;
disable_features = [
oci-image = scopeHost.oci-image;
oci-image-all-features = scopeHost.oci-image.override {
main = scopeHost.main.override {
all_features = true;
disable_features = [
# dont include experimental features
"experimental"
# jemalloc profiling/stats features are expensive and shouldn't
@@ -269,171 +303,88 @@
"jemalloc_stats"
# tuwunel_mods is a development-only hot reload feature
"tuwunel_mods"
];
];
};
};
};
oci-image-all-features-debug = scopeHost.oci-image.override {
main = scopeHost.main.override {
profile = "dev";
all_features = true;
# debug build users expect full logs
disable_release_max_log_level = true;
disable_features = [
oci-image-all-features-debug = scopeHost.oci-image.override {
main = scopeHost.main.override {
profile = "dev";
all_features = true;
# debug build users expect full logs
disable_release_max_log_level = true;
disable_features = [
# dont include experimental features
"experimental"
# tuwunel_mods is a development-only hot reload feature
"tuwunel_mods"
];
];
};
};
};
book = scopeHost.book;
book = scopeHost.book;
complement = scopeHost.complement;
static-complement = scopeHostStatic.complement;
# macOS containers don't exist, so the complement images must be forced to linux
linux-complement = (mkCrossScope "${pkgsHost.hostPlatform.qemuArch}-linux-musl").complement;
}
//
builtins.listToAttrs
(builtins.concatLists
(builtins.map
(crossSystem:
let
binaryName = "static-${crossSystem}";
scopeCrossStatic = mkCrossScope crossSystem;
in
[
# An output for a statically-linked binary
{
name = binaryName;
value = scopeCrossStatic.main;
}
complement = scopeHost.complement;
static-complement = scopeHostStatic.complement;
# macOS containers don't exist, so the complement images must be forced to linux
linux-complement = (mkCrossScope "${pkgsHost.hostPlatform.qemuArch}-linux-musl").complement;
}
// builtins.listToAttrs (
builtins.concatLists (
builtins.map
(
crossSystem:
let
binaryName = "static-${crossSystem}";
scopeCrossStatic = mkCrossScope crossSystem;
in
[
# An output for a statically-linked binary
{
name = binaryName;
value = scopeCrossStatic.main;
}
# An output for a statically-linked binary with x86_64 haswell
# target optimisations
{
name = "${binaryName}-x86_64-haswell-optimised";
value = scopeCrossStatic.main.override {
x86_64_haswell_target_optimised = (if (crossSystem == "x86_64-linux-gnu" || crossSystem == "x86_64-linux-musl") then true else false);
};
}
# An output for a statically-linked unstripped debug ("dev") binary
{
name = "${binaryName}-debug";
value = scopeCrossStatic.main.override {
profile = "dev";
# debug build users expect full logs
disable_release_max_log_level = true;
};
}
# An output for a statically-linked unstripped debug binary with the
# "test" profile (for CI usage only)
{
name = "${binaryName}-test";
value = scopeCrossStatic.main.override {
profile = "test";
disable_release_max_log_level = true;
disable_features = [
# dont include experimental features
"experimental"
# tuwunel_mods is a development-only hot reload feature
"tuwunel_mods"
];
};
}
# An output for a statically-linked binary with `--all-features`
{
name = "${binaryName}-all-features";
value = scopeCrossStatic.main.override {
all_features = true;
disable_features = [
# dont include experimental features
"experimental"
# jemalloc profiling/stats features are expensive and shouldn't
# be expected on non-debug builds.
"jemalloc_prof"
"jemalloc_stats"
# tuwunel_mods is a development-only hot reload feature
"tuwunel_mods"
];
};
}
# An output for a statically-linked binary with `--all-features` and with x86_64 haswell
# target optimisations
{
name = "${binaryName}-all-features-x86_64-haswell-optimised";
value = scopeCrossStatic.main.override {
all_features = true;
disable_features = [
# dont include experimental features
"experimental"
# jemalloc profiling/stats features are expensive and shouldn't
# be expected on non-debug builds.
"jemalloc_prof"
"jemalloc_stats"
# tuwunel_mods is a development-only hot reload feature
"tuwunel_mods"
];
x86_64_haswell_target_optimised = (if (crossSystem == "x86_64-linux-gnu" || crossSystem == "x86_64-linux-musl") then true else false);
};
}
# An output for a statically-linked unstripped debug ("dev") binary with `--all-features`
{
name = "${binaryName}-all-features-debug";
value = scopeCrossStatic.main.override {
profile = "dev";
all_features = true;
# debug build users expect full logs
disable_release_max_log_level = true;
disable_features = [
# dont include experimental features
"experimental"
# tuwunel_mods is a development-only hot reload feature
"tuwunel_mods"
];
};
}
# An output for an OCI image based on that binary
{
name = "oci-image-${crossSystem}";
value = scopeCrossStatic.oci-image;
}
# An output for an OCI image based on that binary with x86_64 haswell
# target optimisations
{
name = "oci-image-${crossSystem}-x86_64-haswell-optimised";
value = scopeCrossStatic.oci-image.override {
main = scopeCrossStatic.main.override {
x86_64_haswell_target_optimised = (if (crossSystem == "x86_64-linux-gnu" || crossSystem == "x86_64-linux-musl") then true else false);
# An output for a statically-linked binary with x86_64 haswell
# target optimisations
{
name = "${binaryName}-x86_64-haswell-optimised";
value = scopeCrossStatic.main.override {
x86_64_haswell_target_optimised = (
if (crossSystem == "x86_64-linux-gnu" || crossSystem == "x86_64-linux-musl") then true else false
);
};
};
}
}
# An output for an OCI image based on that unstripped debug ("dev") binary
{
name = "oci-image-${crossSystem}-debug";
value = scopeCrossStatic.oci-image.override {
main = scopeCrossStatic.main.override {
profile = "dev";
# debug build users expect full logs
disable_release_max_log_level = true;
# An output for a statically-linked unstripped debug ("dev") binary
{
name = "${binaryName}-debug";
value = scopeCrossStatic.main.override {
profile = "dev";
# debug build users expect full logs
disable_release_max_log_level = true;
};
};
}
}
# An output for an OCI image based on that binary with `--all-features`
{
name = "oci-image-${crossSystem}-all-features";
value = scopeCrossStatic.oci-image.override {
main = scopeCrossStatic.main.override {
# An output for a statically-linked unstripped debug binary with the
# "test" profile (for CI usage only)
{
name = "${binaryName}-test";
value = scopeCrossStatic.main.override {
profile = "test";
disable_release_max_log_level = true;
disable_features = [
# dont include experimental features
"experimental"
# tuwunel_mods is a development-only hot reload feature
"tuwunel_mods"
];
};
}
# An output for a statically-linked binary with `--all-features`
{
name = "${binaryName}-all-features";
value = scopeCrossStatic.main.override {
all_features = true;
disable_features = [
# dont include experimental features
@@ -446,15 +397,13 @@
"tuwunel_mods"
];
};
};
}
}
# An output for an OCI image based on that binary with `--all-features` and with x86_64 haswell
# target optimisations
{
name = "oci-image-${crossSystem}-all-features-x86_64-haswell-optimised";
value = scopeCrossStatic.oci-image.override {
main = scopeCrossStatic.main.override {
# An output for a statically-linked binary with `--all-features` and with x86_64 haswell
# target optimisations
{
name = "${binaryName}-all-features-x86_64-haswell-optimised";
value = scopeCrossStatic.main.override {
all_features = true;
disable_features = [
# dont include experimental features
@@ -466,16 +415,16 @@
# tuwunel_mods is a development-only hot reload feature
"tuwunel_mods"
];
x86_64_haswell_target_optimised = (if (crossSystem == "x86_64-linux-gnu" || crossSystem == "x86_64-linux-musl") then true else false);
x86_64_haswell_target_optimised = (
if (crossSystem == "x86_64-linux-gnu" || crossSystem == "x86_64-linux-musl") then true else false
);
};
};
}
}
# An output for an OCI image based on that unstripped debug ("dev") binary with `--all-features`
{
name = "oci-image-${crossSystem}-all-features-debug";
value = scopeCrossStatic.oci-image.override {
main = scopeCrossStatic.main.override {
# An output for a statically-linked unstripped debug ("dev") binary with `--all-features`
{
name = "${binaryName}-all-features-debug";
value = scopeCrossStatic.main.override {
profile = "dev";
all_features = true;
# debug build users expect full logs
@@ -487,47 +436,147 @@
"tuwunel_mods"
];
};
};
}
}
# An output for a complement OCI image for the specified platform
{
name = "complement-${crossSystem}";
value = scopeCrossStatic.complement;
}
# An output for an OCI image based on that binary
{
name = "oci-image-${crossSystem}";
value = scopeCrossStatic.oci-image;
}
# An output for an OCI image based on that binary with x86_64 haswell
# target optimisations
{
name = "oci-image-${crossSystem}-x86_64-haswell-optimised";
value = scopeCrossStatic.oci-image.override {
main = scopeCrossStatic.main.override {
x86_64_haswell_target_optimised = (
if (crossSystem == "x86_64-linux-gnu" || crossSystem == "x86_64-linux-musl") then true else false
);
};
};
}
# An output for an OCI image based on that unstripped debug ("dev") binary
{
name = "oci-image-${crossSystem}-debug";
value = scopeCrossStatic.oci-image.override {
main = scopeCrossStatic.main.override {
profile = "dev";
# debug build users expect full logs
disable_release_max_log_level = true;
};
};
}
# An output for an OCI image based on that binary with `--all-features`
{
name = "oci-image-${crossSystem}-all-features";
value = scopeCrossStatic.oci-image.override {
main = scopeCrossStatic.main.override {
all_features = true;
disable_features = [
# dont include experimental features
"experimental"
# jemalloc profiling/stats features are expensive and shouldn't
# be expected on non-debug builds.
"jemalloc_prof"
"jemalloc_stats"
# tuwunel_mods is a development-only hot reload feature
"tuwunel_mods"
];
};
};
}
# An output for an OCI image based on that binary with `--all-features` and with x86_64 haswell
# target optimisations
{
name = "oci-image-${crossSystem}-all-features-x86_64-haswell-optimised";
value = scopeCrossStatic.oci-image.override {
main = scopeCrossStatic.main.override {
all_features = true;
disable_features = [
# dont include experimental features
"experimental"
# jemalloc profiling/stats features are expensive and shouldn't
# be expected on non-debug builds.
"jemalloc_prof"
"jemalloc_stats"
# tuwunel_mods is a development-only hot reload feature
"tuwunel_mods"
];
x86_64_haswell_target_optimised = (
if (crossSystem == "x86_64-linux-gnu" || crossSystem == "x86_64-linux-musl") then true else false
);
};
};
}
# An output for an OCI image based on that unstripped debug ("dev") binary with `--all-features`
{
name = "oci-image-${crossSystem}-all-features-debug";
value = scopeCrossStatic.oci-image.override {
main = scopeCrossStatic.main.override {
profile = "dev";
all_features = true;
# debug build users expect full logs
disable_release_max_log_level = true;
disable_features = [
# dont include experimental features
"experimental"
# tuwunel_mods is a development-only hot reload feature
"tuwunel_mods"
];
};
};
}
# An output for a complement OCI image for the specified platform
{
name = "complement-${crossSystem}";
value = scopeCrossStatic.complement;
}
]
)
[
#"x86_64-apple-darwin"
#"aarch64-apple-darwin"
"x86_64-linux-gnu"
"x86_64-linux-musl"
"aarch64-linux-musl"
]
)
[
#"x86_64-apple-darwin"
#"aarch64-apple-darwin"
"x86_64-linux-gnu"
"x86_64-linux-musl"
"aarch64-linux-musl"
]
)
);
devShells.default = mkDevShell scopeHostStatic;
devShells.all-features = mkDevShell
(scopeHostStatic.overrideScope (final: prev: {
main = prev.main.override {
all_features = true;
disable_features = [
# dont include experimental features
"experimental"
# jemalloc profiling/stats features are expensive and shouldn't
# be expected on non-debug builds.
"jemalloc_prof"
"jemalloc_stats"
# tuwunel_mods is a development-only hot reload feature
"tuwunel_mods"
];
};
}));
devShells.no-features = mkDevShell
(scopeHostStatic.overrideScope (final: prev: {
main = prev.main.override { default_features = false; };
}));
devShells.dynamic = mkDevShell scopeHost;
});
devShells.default = mkDevShell scopeHostStatic;
devShells.all-features = mkDevShell (
scopeHostStatic.overrideScope (
final: prev: {
main = prev.main.override {
all_features = true;
disable_features = [
# dont include experimental features
"experimental"
# jemalloc profiling/stats features are expensive and shouldn't
# be expected on non-debug builds.
"jemalloc_prof"
"jemalloc_stats"
# tuwunel_mods is a development-only hot reload feature
"tuwunel_mods"
];
};
}
)
);
devShells.no-features = mkDevShell (
scopeHostStatic.overrideScope (
final: prev: {
main = prev.main.override { default_features = false; };
}
)
);
devShells.dynamic = mkDevShell scopeHost;
}
);
}
+6 -5
View File
@@ -1,9 +1,10 @@
{ inputs
{
inputs,
# Dependencies
, main
, mdbook
, stdenv
# Dependencies
main,
mdbook,
stdenv,
}:
stdenv.mkDerivation {
+1 -1
View File
@@ -6,6 +6,7 @@ allow_public_room_directory_over_federation = true
allow_public_room_directory_without_auth = true
allow_registration = true
database_path = "/database"
error_on_unknown_config_opts = true
log = "trace,h2=debug,hyper=debug"
port = [8008, 8448]
trusted_servers = []
@@ -21,7 +22,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
+40 -32
View File
@@ -1,13 +1,14 @@
# Dependencies
{ bashInteractive
, buildEnv
, coreutils
, dockerTools
, lib
, main
, stdenv
, tini
, writeShellScriptBin
{
bashInteractive,
buildEnv,
coreutils,
dockerTools,
lib,
main,
stdenv,
tini,
writeShellScriptBin,
}:
let
@@ -16,24 +17,24 @@ let
all_features = true;
disable_release_max_log_level = true;
disable_features = [
# console/CLI stuff isn't used or relevant for complement
"console"
"tokio_console"
# sentry telemetry isn't useful for complement, disabled by default anyways
"sentry_telemetry"
"perf_measurements"
# this is non-functional on nix for some reason
"hardened_malloc"
# dont include experimental features
"experimental"
# compression isn't needed for complement
"brotli_compression"
"gzip_compression"
"zstd_compression"
# complement doesn't need hot reloading
"tuwunel_mods"
# complement doesn't have URL preview media tests
"url_preview"
# console/CLI stuff isn't used or relevant for complement
"console"
"tokio_console"
# sentry telemetry isn't useful for complement, disabled by default anyways
"sentry_telemetry"
"perf_measurements"
# this is non-functional on nix for some reason
"hardened_malloc"
# dont include experimental features
"experimental"
# compression isn't needed for complement
"brotli_compression"
"gzip_compression"
"zstd_compression"
# complement doesn't need hot reloading
"tuwunel_mods"
# complement doesn't have URL preview media tests
"url_preview"
];
};
@@ -68,11 +69,18 @@ dockerTools.buildImage {
"${lib.getExe start}"
];
Entrypoint = if !stdenv.hostPlatform.isDarwin
Entrypoint =
if
!stdenv.hostPlatform.isDarwin
# Use the `tini` init system so that signals (e.g. ctrl+c/SIGINT)
# are handled as expected
then [ "${lib.getExe' tini "tini"}" "--" ]
else [];
then
[
"${lib.getExe' tini "tini"}"
"--"
]
else
[ ];
Env = [
"TUWUNEL_TLS__KEY=${./private_key.key}"
@@ -82,8 +90,8 @@ dockerTools.buildImage {
];
ExposedPorts = {
"8008/tcp" = {};
"8448/tcp" = {};
"8008/tcp" = { };
"8448/tcp" = { };
};
};
}
+38 -47
View File
@@ -1,38 +1,35 @@
{ lib
, pkgsBuildHost
, rust
, stdenv
{
lib,
pkgsBuildHost,
rust,
stdenv,
}:
lib.optionalAttrs stdenv.hostPlatform.isStatic {
ROCKSDB_STATIC = "";
}
//
{
CARGO_BUILD_RUSTFLAGS =
lib.concatStringsSep
" "
([]
# This disables PIE for static builds, which isn't great in terms
# of security. Unfortunately, my hand is forced because nixpkgs'
# `libstdc++.a` is built without `-fPIE`, which precludes us from
# leaving PIE enabled.
++ lib.optionals
stdenv.hostPlatform.isStatic
[ "-C" "relocation-model=static" ]
++ lib.optionals
(stdenv.buildPlatform.config != stdenv.hostPlatform.config)
[
"-l"
"c"
// {
CARGO_BUILD_RUSTFLAGS = lib.concatStringsSep " " (
[ ]
# This disables PIE for static builds, which isn't great in terms
# of security. Unfortunately, my hand is forced because nixpkgs'
# `libstdc++.a` is built without `-fPIE`, which precludes us from
# leaving PIE enabled.
++ lib.optionals stdenv.hostPlatform.isStatic [
"-C"
"relocation-model=static"
]
++ lib.optionals (stdenv.buildPlatform.config != stdenv.hostPlatform.config) [
"-l"
"c"
"-l"
"stdc++"
"-l"
"stdc++"
"-L"
"${stdenv.cc.cc.lib}/${stdenv.hostPlatform.config}/lib"
]
);
"-L"
"${stdenv.cc.cc.lib}/${stdenv.hostPlatform.config}/lib"
]
);
}
# What follows is stolen from [here][0]. Its purpose is to properly
@@ -41,26 +38,21 @@ lib.optionalAttrs stdenv.hostPlatform.isStatic {
# run on the build platform (I think).
#
# [0]: https://github.com/NixOS/nixpkgs/blob/nixpkgs-unstable/pkgs/build-support/rust/lib/default.nix#L48-L68
//
(
// (
let
inherit (rust.lib) envVars;
in
lib.optionalAttrs
(stdenv.targetPlatform.rust.rustcTarget
!= stdenv.hostPlatform.rust.rustcTarget)
(
let
inherit (stdenv.targetPlatform.rust) cargoEnvVarTarget;
in
{
"CC_${cargoEnvVarTarget}" = envVars.ccForTarget;
"CXX_${cargoEnvVarTarget}" = envVars.cxxForTarget;
"CARGO_TARGET_${cargoEnvVarTarget}_LINKER" = envVars.ccForTarget;
}
)
//
(
lib.optionalAttrs (stdenv.targetPlatform.rust.rustcTarget != stdenv.hostPlatform.rust.rustcTarget) (
let
inherit (stdenv.targetPlatform.rust) cargoEnvVarTarget;
in
{
"CC_${cargoEnvVarTarget}" = envVars.ccForTarget;
"CXX_${cargoEnvVarTarget}" = envVars.cxxForTarget;
"CARGO_TARGET_${cargoEnvVarTarget}_LINKER" = envVars.ccForTarget;
}
)
// (
let
inherit (stdenv.hostPlatform.rust) cargoEnvVarTarget rustcTarget;
in
@@ -71,8 +63,7 @@ lib.optionalAttrs stdenv.hostPlatform.isStatic {
CARGO_BUILD_TARGET = rustcTarget;
}
)
//
(
// (
let
inherit (stdenv.buildPlatform.rust) cargoEnvVarTarget;
in
+202 -165
View File
@@ -1,85 +1,94 @@
# Dependencies (keep sorted)
{ craneLib
, inputs
, jq
, lib
, libiconv
, liburing
, pkgsBuildHost
, rocksdb
, removeReferencesTo
, rust
, rust-jemalloc-sys
, stdenv
{
craneLib,
inputs,
jq,
lib,
libiconv,
liburing,
pkgsBuildHost,
rocksdb,
removeReferencesTo,
rust,
autoPatchelfHook,
rust-jemalloc-sys-unprefixed,
stdenv,
# Options (keep sorted)
, all_features ? false
, default_features ? true
# default list of disabled features
, disable_features ? [
# dont include experimental features
"experimental"
# jemalloc profiling/stats features are expensive and shouldn't
# be expected on non-debug builds.
"jemalloc_prof"
"jemalloc_stats"
# tuwunel_mods is a development-only hot reload feature
"tuwunel_mods"
]
, disable_release_max_log_level ? false
, features ? []
, profile ? "release"
# rocksdb compiled with -march=haswell and target-cpu=haswell rustflag
# haswell is pretty much any x86 cpu made in the last 12 years, and
# supports modern CPU extensions that rocksdb can make use of.
# disable if trying to make a portable x86_64 build for very old hardware
, x86_64_haswell_target_optimised ? false
# Options (keep sorted)
all_features ? false,
default_features ? true,
# default list of disabled features
disable_features ? [
# dont include experimental features
"experimental"
# jemalloc profiling/stats features are expensive and shouldn't
# be expected on non-debug builds.
"jemalloc_prof"
"jemalloc_stats"
# tuwunel_mods is a development-only hot reload feature
"tuwunel_mods"
],
disable_release_max_log_level ? false,
features ? [ ],
profile ? "release",
# rocksdb compiled with -march=haswell and target-cpu=haswell rustflag
# haswell is pretty much any x86 cpu made in the last 12 years, and
# supports modern CPU extensions that rocksdb can make use of.
# disable if trying to make a portable x86_64 build for very old hardware
x86_64_haswell_target_optimised ? false,
}:
let
# We perform default-feature unification in nix, because some of the dependencies
# on the nix side depend on feature values.
crateFeatures = path:
let manifest = lib.importTOML "${path}/Cargo.toml"; in
lib.remove "default" (lib.attrNames manifest.features);
crateDefaultFeatures = path:
(lib.importTOML "${path}/Cargo.toml").features.default;
allDefaultFeatures = crateDefaultFeatures "${inputs.self}/src/main";
allFeatures = crateFeatures "${inputs.self}/src/main";
features' = lib.unique
(features ++
lib.optionals default_features allDefaultFeatures ++
lib.optionals all_features allFeatures);
disable_features' = disable_features ++ lib.optionals disable_release_max_log_level ["release_max_log_level"];
features'' = lib.subtractLists disable_features' features';
# We perform default-feature unification in nix, because some of the dependencies
# on the nix side depend on feature values.
crateFeatures =
path:
let
manifest = lib.importTOML "${path}/Cargo.toml";
in
lib.remove "default" (lib.attrNames manifest.features);
crateDefaultFeatures = path: (lib.importTOML "${path}/Cargo.toml").features.default;
allDefaultFeatures = crateDefaultFeatures "${inputs.self}/src/main";
allFeatures = crateFeatures "${inputs.self}/src/main";
features' = lib.unique (
features
++ lib.optionals default_features allDefaultFeatures
++ lib.optionals all_features allFeatures
);
disable_features' =
disable_features ++ lib.optionals disable_release_max_log_level [ "release_max_log_level" ];
features'' = lib.subtractLists disable_features' features';
featureEnabled = feature : builtins.elem feature features'';
featureEnabled = feature: builtins.elem feature features'';
enableLiburing = featureEnabled "io_uring" && !stdenv.hostPlatform.isDarwin;
enableLiburing = featureEnabled "io_uring" && !stdenv.hostPlatform.isDarwin;
# This derivation will set the JEMALLOC_OVERRIDE variable, causing the
# tikv-jemalloc-sys crate to use the nixpkgs jemalloc instead of building it's
# own. In order for this to work, we need to set flags on the build that match
# whatever flags tikv-jemalloc-sys was going to use. These are dependent on
# which features we enable in tikv-jemalloc-sys.
rust-jemalloc-sys' = (rust-jemalloc-sys.override {
# tikv-jemalloc-sys/unprefixed_malloc_on_supported_platforms feature
unprefixed = true;
}).overrideAttrs (old: {
configureFlags = old.configureFlags ++
# we dont need docs
[ "--disable-doc" ] ++
# we dont need cxx/C++ integration
[ "--disable-cxx" ] ++
# tikv-jemalloc-sys/profiling feature
lib.optional (featureEnabled "jemalloc_prof") "--enable-prof" ++
# tikv-jemalloc-sys/stats feature
(if (featureEnabled "jemalloc_stats") then [ "--enable-stats" ] else [ "--disable-stats" ]);
});
# This derivation will set the JEMALLOC_OVERRIDE variable, causing the
# tikv-jemalloc-sys crate to use the nixpkgs jemalloc instead of building it's
# own. In order for this to work, we need to set flags on the build that match
# whatever flags tikv-jemalloc-sys was going to use. These are dependent on
# which features we enable in tikv-jemalloc-sys.
rust-jemalloc-sys' =
# tikv-jemalloc-sys/unprefixed_malloc_on_supported_platforms feature
rust-jemalloc-sys-unprefixed.overrideAttrs (old: {
configureFlags =
old.configureFlags
++
# we dont need docs
[ "--disable-doc" ]
++
# we dont need cxx/C++ integration
[ "--disable-cxx" ]
++
# tikv-jemalloc-sys/profiling feature
lib.optional (featureEnabled "jemalloc_prof") "--enable-prof"
++
# tikv-jemalloc-sys/stats feature
(if (featureEnabled "jemalloc_stats") then [ "--enable-stats" ] else [ "--disable-stats" ]);
});
buildDepsOnlyEnv =
let
rocksdb' = (rocksdb.override {
rocksdb' =
(rocksdb.override {
jemalloc = lib.optional (featureEnabled "jemalloc") rust-jemalloc-sys';
# rocksdb fails to build with prefixed jemalloc, which is required on
# darwin due to [1]. In this case, fall back to building rocksdb with
@@ -92,24 +101,32 @@ buildDepsOnlyEnv =
# for some reason enableLiburing in nixpkgs rocksdb is default true
# which breaks Darwin entirely
enableLiburing = enableLiburing;
}).overrideAttrs (old: {
enableLiburing = enableLiburing;
cmakeFlags = (if x86_64_haswell_target_optimised then (lib.subtractLists [
# dont make a portable build if x86_64_haswell_target_optimised is enabled
"-DPORTABLE=1"
] old.cmakeFlags
++ [ "-DPORTABLE=haswell" ]) else ([ "-DPORTABLE=1" ])
)
++ old.cmakeFlags;
}).overrideAttrs
(old: {
enableLiburing = enableLiburing;
cmakeFlags =
(
if x86_64_haswell_target_optimised then
(
lib.subtractLists [
# dont make a portable build if x86_64_haswell_target_optimised is enabled
"-DPORTABLE=1"
] old.cmakeFlags
++ [ "-DPORTABLE=haswell" ]
)
else
([ "-DPORTABLE=1" ])
)
++ old.cmakeFlags;
# outputs has "tools" which we dont need or use
outputs = [ "out" ];
# outputs has "tools" which we dont need or use
outputs = [ "out" ];
# preInstall hooks has stuff for messing with ldb/sst_dump which we dont need or use
preInstall = "";
});
in
{
# preInstall hooks has stuff for messing with ldb/sst_dump which we dont need or use
preInstall = "";
});
buildDepsOnlyEnv = {
# https://crane.dev/faq/rebuilds-bindgen.html
NIX_OUTPATH_USED_AS_RANDOM_SEED = "aaaaaaaaaa";
@@ -117,69 +134,73 @@ buildDepsOnlyEnv =
ROCKSDB_INCLUDE_DIR = "${rocksdb'}/include";
ROCKSDB_LIB_DIR = "${rocksdb'}/lib";
}
//
(import ./cross-compilation-env.nix {
// (import ./cross-compilation-env.nix {
# Keep sorted
inherit
lib
pkgsBuildHost
rust
stdenv;
stdenv
;
});
buildPackageEnv = {
TUWUNEL_VERSION_EXTRA = inputs.self.shortRev or inputs.self.dirtyShortRev or "";
TUWUNEL_DATABASE_PATH = "/var/tmp/tuwunel.db";
} // buildDepsOnlyEnv // {
# Only needed in static stdenv because these are transitive dependencies of rocksdb
CARGO_BUILD_RUSTFLAGS = buildDepsOnlyEnv.CARGO_BUILD_RUSTFLAGS
+ lib.optionalString (enableLiburing && stdenv.hostPlatform.isStatic)
" -L${lib.getLib liburing}/lib -luring"
+ lib.optionalString x86_64_haswell_target_optimised
" -Ctarget-cpu=haswell";
};
buildPackageEnv = {
TUWUNEL_VERSION_EXTRA = inputs.self.shortRev or inputs.self.dirtyShortRev or "";
TUWUNEL_DATABASE_PATH = "/var/tmp/tuwunel.db";
}
// buildDepsOnlyEnv
// {
# Only needed in static stdenv because these are transitive dependencies of rocksdb
CARGO_BUILD_RUSTFLAGS =
buildDepsOnlyEnv.CARGO_BUILD_RUSTFLAGS
+ lib.optionalString (
enableLiburing && stdenv.hostPlatform.isStatic
) " -L${lib.getLib liburing}/lib -luring"
+ lib.optionalString x86_64_haswell_target_optimised " -Ctarget-cpu=haswell";
};
commonAttrs = {
inherit
(craneLib.crateNameFromCargoToml {
cargoToml = "${inputs.self}/Cargo.toml";
})
pname
version
;
src =
let
filter = inputs.nix-filter.lib;
in
filter {
root = inputs.self;
commonAttrs = {
inherit
(craneLib.crateNameFromCargoToml {
cargoToml = "${inputs.self}/Cargo.toml";
})
pname
version;
src = let filter = inputs.nix-filter.lib; in filter {
root = inputs.self;
# Keep sorted
include = [
".cargo"
"Cargo.lock"
"Cargo.toml"
"src"
];
};
# Keep sorted
include = [
".cargo"
"Cargo.lock"
"Cargo.toml"
"src"
];
};
doCheck = true;
cargoExtraArgs = "--no-default-features --locked "
+ lib.optionalString
(features'' != [])
"--features " + (builtins.concatStringsSep "," features'');
cargoExtraArgs =
"--no-default-features --locked "
+ lib.optionalString (features'' != [ ]) "--features "
+ (builtins.concatStringsSep "," features'');
dontStrip = profile == "dev" || profile == "test";
dontPatchELF = profile == "dev" || profile == "test";
buildInputs = lib.optional (featureEnabled "jemalloc") rust-jemalloc-sys'
# needed to build Rust applications on macOS
++ lib.optionals stdenv.hostPlatform.isDarwin [
buildInputs =
lib.optional (featureEnabled "jemalloc") rust-jemalloc-sys-unprefixed
# needed to build Rust applications on macOS
++ lib.optionals stdenv.hostPlatform.isDarwin [
# https://github.com/NixOS/nixpkgs/issues/206242
# ld: library not found for -liconv
libiconv
# https://stackoverflow.com/questions/69869574/properly-adding-darwin-apple-sdk-to-a-nix-shell
# https://discourse.nixos.org/t/compile-a-rust-binary-on-macos-dbcrossbar/8612
pkgsBuildHost.darwin.apple_sdk.frameworks.Security
];
nativeBuildInputs = [
@@ -193,46 +214,62 @@ commonAttrs = {
# differing values for `NIX_CFLAGS_COMPILE`, which contributes to spurious
# rebuilds of bindgen and its depedents.
jq
];
};
];
};
in
craneLib.buildPackage ( commonAttrs // {
cargoArtifacts = craneLib.buildDepsOnly (commonAttrs // {
env = buildDepsOnlyEnv;
});
craneLib.buildPackage (
commonAttrs
// rec {
cargoArtifacts = craneLib.buildDepsOnly (
commonAttrs
// {
env = buildDepsOnlyEnv;
}
);
nativeCheckInputs = [
pkgsBuildHost.libredirect.hook
];
# Adds runpath settings to the resulting binary
buildInputs = (commonAttrs.buildInputs or [ ]) ++ [
rocksdb'
];
nativeBuildInputs = (commonAttrs.nativeBuildInputs or [ ]) ++ [
autoPatchelfHook
];
# This is needed for tests to link
LD_LIBRARY_PATH = lib.makeLibraryPath buildInputs;
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;
nativeCheckInputs = [
pkgsBuildHost.libredirect.hook
];
doBenchmark = false;
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;
cargoExtraArgs = "--no-default-features --locked "
+ lib.optionalString
(features'' != [])
"--features " + (builtins.concatStringsSep "," features'');
doBenchmark = false;
env = buildPackageEnv;
cargoExtraArgs =
"--no-default-features --locked "
+ lib.optionalString (features'' != [ ]) "--features "
+ (builtins.concatStringsSep "," features'');
passthru = {
env = buildPackageEnv;
};
meta.mainProgram = commonAttrs.pname;
})
passthru = {
env = buildPackageEnv;
};
meta.mainProgram = commonAttrs.pname;
}
)
+21 -12
View File
@@ -1,11 +1,12 @@
{ inputs
{
inputs,
# Dependencies
, dockerTools
, lib
, main
, stdenv
, tini
# Dependencies
dockerTools,
lib,
main,
stdenv,
tini,
}:
dockerTools.buildLayeredImage {
@@ -17,11 +18,18 @@ dockerTools.buildLayeredImage {
main
];
config = {
Entrypoint = if !stdenv.hostPlatform.isDarwin
Entrypoint =
if
!stdenv.hostPlatform.isDarwin
# Use the `tini` init system so that signals (e.g. ctrl+c/SIGINT)
# are handled as expected
then [ "${lib.getExe' tini "tini"}" "--" ]
else [];
then
[
"${lib.getExe' tini "tini"}"
"--"
]
else
[ ];
Cmd = [
"${lib.getExe main}"
];
@@ -29,9 +37,10 @@ dockerTools.buildLayeredImage {
"RUST_BACKTRACE=full"
];
Labels = {
"org.opencontainers.image.authors" = "June Clementine Strawberry <june@girlboss.ceo> and Jason Volk
"org.opencontainers.image.authors" =
"June Clementine Strawberry <june@girlboss.ceo> and Jason Volk
<jason@zemos.net>";
"org.opencontainers.image.created" ="@${toString inputs.self.lastModified}";
"org.opencontainers.image.created" = "@${toString inputs.self.lastModified}";
"org.opencontainers.image.description" = "a very cool Matrix chat homeserver written in Rust";
"org.opencontainers.image.documentation" = "https://conduwuit.puppyirl.gay/";
"org.opencontainers.image.licenses" = "Apache-2.0";
+1 -1
View File
@@ -9,7 +9,7 @@
# If you're having trouble making the relevant changes, bug a maintainer.
[toolchain]
channel = "1.89.0"
channel = "1.91.1"
profile = "minimal"
components = [
# For rust-analyzer
+15 -9
View File
@@ -2,10 +2,16 @@ use clap::Parser;
use tuwunel_core::Result;
use crate::{
appservice, appservice::AppserviceCommand, check, check::CheckCommand, context::Context,
debug, debug::DebugCommand, federation, federation::FederationCommand, media,
media::MediaCommand, query, query::QueryCommand, room, room::RoomCommand, server,
server::ServerCommand, user, user::UserCommand,
appservice::{self, AppserviceCommand},
context::Context,
debug::{self, DebugCommand},
federation::{self, FederationCommand},
media::{self, MediaCommand},
query::{self, QueryCommand},
room::{self, RoomCommand},
server::{self, ServerCommand},
token::{self, TokenCommand},
user::{self, UserCommand},
};
#[derive(Debug, Parser)]
@@ -35,10 +41,6 @@ pub(super) enum AdminCommand {
/// - Commands for managing media
Media(MediaCommand),
#[command(subcommand)]
/// - Commands for checking integrity
Check(CheckCommand),
#[command(subcommand)]
/// - Commands for debugging things
Debug(DebugCommand),
@@ -46,6 +48,10 @@ pub(super) enum AdminCommand {
#[command(subcommand)]
/// - Low-level queries for database getters and iterators
Query(QueryCommand),
#[command(subcommand)]
/// - Commands for managing registration tokens
Token(TokenCommand),
}
#[tracing::instrument(skip_all, name = "command")]
@@ -61,6 +67,6 @@ pub(super) async fn process(command: AdminCommand, context: &Context<'_>) -> Res
| Server(command) => server::process(command, context).await,
| Debug(command) => debug::process(command, context).await,
| Query(command) => query::process(command, context).await,
| Check(command) => check::process(command, context).await,
| Token(command) => token::process(command, context).await,
}
}
-31
View File
@@ -1,31 +0,0 @@
use futures::StreamExt;
use tuwunel_core::Result;
use tuwunel_macros::implement;
use crate::Context;
/// Uses the iterator in `src/database/key_value/users.rs` to iterator over
/// every user in our database (remote and local). Reports total count, any
/// errors if there were any, etc
#[implement(Context, params = "<'_>")]
pub(super) async fn check_all_users(&self) -> Result {
let timer = tokio::time::Instant::now();
let users = self
.services
.users
.iter()
.collect::<Vec<_>>()
.await;
let query_time = timer.elapsed();
let total = users.len();
let err_count = users.iter().filter(|_user| false).count();
let ok_count = users.iter().filter(|_user| true).count();
self.write_str(&format!(
"Database query completed in {query_time:?}:\n\n```\nTotal entries: \
{total:?}\nFailure/Invalid user count: {err_count:?}\nSuccess/Valid user count: \
{ok_count:?}\n```"
))
.await
}
-12
View File
@@ -1,12 +0,0 @@
mod commands;
use clap::Subcommand;
use tuwunel_core::Result;
use crate::admin_command_dispatch;
#[admin_command_dispatch]
#[derive(Debug, Subcommand)]
pub(super) enum CheckCommand {
CheckAllUsers,
}
+4 -6
View File
@@ -23,14 +23,12 @@ impl Context<'_> {
arguments: fmt::Arguments<'_>,
) -> impl Future<Output = Result> + Send + '_ + use<'_> {
let buf = format!("{arguments}");
self.output.lock().then(async move |mut output| {
output
.write_all(buf.as_bytes())
.map_err(Into::into)
.await
})
self.write_string(buf)
}
#[inline]
pub(crate) async fn write_string(&self, s: String) -> Result { self.write_str(&s).await }
pub(crate) fn write_str<'a>(
&'a self,
s: &'a str,
+41 -18
View File
@@ -57,11 +57,17 @@ pub(super) async fn get_auth_chain(&self, event_id: OwnedEventId) -> Result {
let room_id = <&RoomId>::try_from(room_id_str)
.map_err(|_| err!(Database("Invalid room id field in event in database")))?;
let room_version = self
.services
.state
.get_room_version(room_id)
.await?;
let start = Instant::now();
let count = self
.services
.auth_chain
.event_ids_iter(room_id, once(event_id.as_ref()))
.event_ids_iter(room_id, &room_version, once(event_id.as_ref()))
.ready_filter_map(Result::ok)
.count()
.await;
@@ -236,16 +242,17 @@ 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
{
| Err(e) =>
| Err(e) => {
return Err!(
"Remote server did not have PDU or failed sending request to remote server: {e}"
),
);
},
| Ok(response) => {
let json: CanonicalJsonObject =
serde_json::from_str(response.pdu.get()).map_err(|e| {
@@ -327,11 +334,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) => {
@@ -386,8 +390,9 @@ pub(super) async fn change_log_level(&self, filter: Option<String>, reset: bool)
.reload
.reload(&old_filter_layer, Some(handles))
{
| Err(e) =>
return Err!("Failed to modify and reload the global tracing log level: {e}"),
| Err(e) => {
return Err!("Failed to modify and reload the global tracing log level: {e}");
},
| Ok(()) => {
let value = &self.services.server.config.log;
let out = format!("Successfully changed log level back to config value {value}");
@@ -409,12 +414,14 @@ pub(super) async fn change_log_level(&self, filter: Option<String>, reset: bool)
.reload
.reload(&new_filter_layer, Some(handles))
{
| Ok(()) =>
| Ok(()) => {
return self
.write_str("Successfully changed log level")
.await,
| Err(e) =>
return Err!("Failed to modify and reload the global tracing log level: {e}"),
.await;
},
| Err(e) => {
return Err!("Failed to modify and reload the global tracing log level: {e}");
},
}
}
@@ -571,8 +578,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(),
})
@@ -1016,3 +1023,19 @@ pub(super) async fn resync_database(&self) -> Result {
.update()
.map_err(|e| err!("Failed to update from primary: {e:?}"))
}
#[admin_command]
pub(super) async fn get_retained_pdu(&self, event_id: OwnedEventId) -> Result {
let pdu = self
.services
.retention
.get_original_pdu_json(&event_id)
.await?;
let text = serde_json::to_string_pretty(&pdu)?;
self.write_str(&format!("Original PDU:\n```json\n{text}\n```"))
.await?;
Ok(())
}
+5 -1
View File
@@ -250,9 +250,13 @@ pub(super) enum DebugCommand {
/// - Synchronize database with primary (secondary only)
ResyncDatabase,
/// - Retrieves the saved original PDU before it has been redacted
GetRetainedPdu {
event_id: OwnedEventId,
},
/// - Developer test stubs
#[command(subcommand)]
#[allow(non_snake_case)]
#[clap(hide(true))]
Tester(TesterCommand),
}
+1 -1
View File
@@ -46,7 +46,7 @@ async fn timer(&self) -> Result {
#[inline(never)]
#[rustfmt::skip]
#[allow(unused_variables)]
#[expect(unused_variables)]
fn timed(body: &[&str]) {
}
+3 -3
View File
@@ -1,4 +1,4 @@
#![allow(rustdoc::broken_intra_doc_links)]
#![expect(rustdoc::broken_intra_doc_links)]
mod commands;
use clap::Subcommand;
@@ -90,10 +90,10 @@ pub(super) enum MediaCommand {
#[arg(short, long, default_value("10000"))]
timeout: u32,
#[arg(short, long, default_value("800"))]
#[arg(long, default_value("800"))]
width: u32,
#[arg(short, long, default_value("800"))]
#[arg(long, default_value("800"))]
height: u32,
},
}
+4 -4
View File
@@ -1,6 +1,4 @@
#![allow(clippy::wildcard_imports)]
#![allow(clippy::enum_glob_use)]
#![allow(clippy::too_many_arguments)]
#![expect(clippy::too_many_arguments)]
pub(crate) mod admin;
pub(crate) mod context;
@@ -9,13 +7,13 @@ mod tests;
pub(crate) mod utils;
pub(crate) mod appservice;
pub(crate) mod check;
pub(crate) mod debug;
pub(crate) mod federation;
pub(crate) mod media;
pub(crate) mod query;
pub(crate) mod room;
pub(crate) mod server;
pub(crate) mod token;
pub(crate) mod user;
pub(crate) use tuwunel_macros::{admin_command, admin_command_dispatch};
@@ -24,6 +22,8 @@ pub(crate) use crate::{context::Context, utils::get_room_info};
pub(crate) const PAGE_SIZE: usize = 100;
use log as _;
tuwunel_core::mod_ctor! {}
tuwunel_core::mod_dtor! {}
tuwunel_core::rustc_flags_capture! {}
+7 -5
View File
@@ -54,7 +54,7 @@ async fn handle_command(services: Arc<Services>, command: CommandInput) -> Proce
async fn process_command(services: Arc<Services>, input: &CommandInput) -> ProcessorResult {
let (command, args, body) = match parse(&services, input) {
| Err(error) => return Err(error),
| Err(error) => return Err(Box::new(error)),
| Ok(parsed) => parsed,
};
@@ -90,12 +90,14 @@ async fn process_command(services: Arc<Services>, input: &CommandInput) -> Proce
write!(&mut logs, "Command failed with error:\n```\n{error:#?}\n```")
.expect("output buffer");
Err(reply(RoomMessageEventContent::notice_markdown(logs), context.reply_id))
Err(Box::new(reply(
RoomMessageEventContent::notice_markdown(logs),
context.reply_id,
)))
},
}
}
#[allow(clippy::result_large_err)]
fn handle_panic(error: &Error, command: &CommandInput) -> ProcessorResult {
let link =
"Please submit a [bug report](https://github.com/matrix-construct/tuwunel/issues/new). \
@@ -103,7 +105,7 @@ fn handle_panic(error: &Error, command: &CommandInput) -> ProcessorResult {
let msg = format!("Panic occurred while processing command:\n```\n{error:#?}\n```\n{link}");
let content = RoomMessageEventContent::notice_markdown(msg);
error!("Panic while processing command: {error:?}");
Err(reply(content, command.reply_id.as_deref()))
Err(Box::new(reply(content, command.reply_id.as_deref())))
}
/// Parse and process a message from the admin room
@@ -171,7 +173,7 @@ fn capture_create(context: &Context<'_>) -> (Arc<Capture>, Arc<Mutex<String>>) {
}
/// Parse chat messages from the admin room into an AdminCommand object
#[allow(clippy::result_large_err)]
#[expect(clippy::result_large_err)]
fn parse<'a>(
services: &Arc<Services>,
input: &'a CommandInput,
+9 -4
View File
@@ -1,6 +1,7 @@
mod account_data;
mod appservice;
mod globals;
mod oauth;
mod presence;
mod pusher;
mod raw;
@@ -18,10 +19,10 @@ use tuwunel_core::Result;
use self::{
account_data::AccountDataCommand, appservice::AppserviceCommand, globals::GlobalsCommand,
presence::PresenceCommand, pusher::PusherCommand, raw::RawCommand, resolver::ResolverCommand,
room_alias::RoomAliasCommand, room_state_cache::RoomStateCacheCommand,
room_timeline::RoomTimelineCommand, sending::SendingCommand, short::ShortCommand,
sync::SyncCommand, users::UsersCommand,
oauth::OauthCommand, presence::PresenceCommand, pusher::PusherCommand, raw::RawCommand,
resolver::ResolverCommand, room_alias::RoomAliasCommand,
room_state_cache::RoomStateCacheCommand, room_timeline::RoomTimelineCommand,
sending::SendingCommand, short::ShortCommand, sync::SyncCommand, users::UsersCommand,
};
use crate::admin_command_dispatch;
@@ -81,6 +82,10 @@ pub(super) enum QueryCommand {
#[command(subcommand)]
Sync(SyncCommand),
/// - oauth service
#[command(subcommand)]
Oauth(OauthCommand),
/// - raw service
#[command(subcommand)]
Raw(RawCommand),
+300
View File
@@ -0,0 +1,300 @@
use clap::Subcommand;
use futures::{StreamExt, TryStreamExt};
use ruma::OwnedUserId;
use tuwunel_core::{
Err, Result, apply,
either::{Either, Left, Right},
err,
itertools::Itertools,
utils::stream::{IterStream, ReadyExt},
};
use tuwunel_service::oauth::{Provider, ProviderId, SessionId};
use crate::{admin_command, admin_command_dispatch};
#[admin_command_dispatch(handler_prefix = "oauth")]
#[derive(Debug, Subcommand)]
/// Query OAuth service state
pub(crate) enum OauthCommand {
/// Associate existing user with future authorization claims.
Associate {
/// ID of configured provider to listen on.
provider: String,
/// MXID of local user to associate.
user_id: OwnedUserId,
/// List of claims to match in key=value format.
#[arg(long, required = true)]
claim: Vec<String>,
},
/// List configured OAuth providers.
ListProviders,
/// List users associated with any OAuth session
ListUsers,
/// List session ID's
ListSessions {
#[arg(long)]
user: Option<OwnedUserId>,
},
/// Show active configuration of a provider.
ShowProvider {
id: ProviderId,
#[arg(long)]
config: bool,
},
/// Show session state
ShowSession {
id: SessionId,
},
/// Show user sessions
ShowUser {
user_id: OwnedUserId,
},
/// Token introspection request to provider.
TokenInfo {
id: SessionId,
},
/// Revoke token for user_id or sess_id.
Revoke {
#[arg(value_parser = session_or_user_id)]
id: Either<SessionId, OwnedUserId>,
},
/// Remove oauth state (DANGER!)
Delete {
#[arg(value_parser = session_or_user_id)]
id: Either<SessionId, OwnedUserId>,
#[arg(long)]
force: bool,
},
}
type SessionOrUserId = Either<SessionId, OwnedUserId>;
fn session_or_user_id(input: &str) -> Result<SessionOrUserId> {
OwnedUserId::parse(input)
.map(Right)
.or_else(|_| Ok(Left(input.to_owned())))
}
#[admin_command]
pub(super) async fn oauth_associate(
&self,
provider: String,
user_id: OwnedUserId,
claim: Vec<String>,
) -> Result {
if !self.services.globals.user_is_local(&user_id) {
return Err!(Request(NotFound("User {user_id:?} does not belong to this server.")));
}
if !self.services.users.exists(&user_id).await {
return Err!(Request(NotFound("User {user_id:?} is not registered")));
}
let provider = self
.services
.oauth
.providers
.get(&provider)
.await?;
let claim = claim
.iter()
.map(|kv| {
let (key, val) = kv
.split_once('=')
.ok_or_else(|| err!("Missing '=' in --claim {kv}=???"))?;
if !key.is_empty() && !val.is_empty() {
Ok((key, val))
} else {
Err!("Missing key or value in --claim=key=value argument")
}
})
.map_ok(apply!(2, ToOwned::to_owned))
.collect::<Result<_>>()?;
let _replaced = self
.services
.oauth
.sessions
.set_user_association_pending(provider.id(), &user_id, claim);
Ok(())
}
#[admin_command]
pub(super) async fn oauth_list_providers(&self) -> Result {
self.services
.config
.identity_provider
.values()
.try_stream()
.map_ok(Provider::id)
.map_ok(|id| format!("{id}\n"))
.try_for_each(async |id| self.write_str(&id).await)
.await
}
#[admin_command]
pub(super) async fn oauth_list_users(&self) -> Result {
self.services
.oauth
.sessions
.users()
.map(|id| format!("{id}\n"))
.map(Ok)
.try_for_each(async |id: String| self.write_str(&id).await)
.await
}
#[admin_command]
pub(super) async fn oauth_list_sessions(&self, user_id: Option<OwnedUserId>) -> Result {
if let Some(user_id) = user_id.as_deref() {
return self
.services
.oauth
.sessions
.get_sess_id_by_user(user_id)
.map_ok(|id| format!("{id}\n"))
.try_for_each(async |id: String| self.write_str(&id).await)
.await;
}
self.services
.oauth
.sessions
.stream()
.ready_filter_map(|sess| sess.sess_id)
.map(|sess_id| format!("{sess_id:?}\n"))
.for_each(async |id: String| {
self.write_str(&id).await.ok();
})
.await;
Ok(())
}
#[admin_command]
pub(super) async fn oauth_show_provider(&self, id: ProviderId, config: bool) -> Result {
if config {
let config = self.services.oauth.providers.get_config(&id)?;
self.write_str(&format!("{config:#?}\n")).await?;
return Ok(());
}
let provider = self.services.oauth.providers.get(&id).await?;
self.write_str(&format!("{provider:#?}\n")).await
}
#[admin_command]
pub(super) async fn oauth_show_session(&self, id: SessionId) -> Result {
let session = self.services.oauth.sessions.get(&id).await?;
self.write_str(&format!("{session:#?}\n")).await
}
#[admin_command]
pub(super) async fn oauth_show_user(&self, user_id: OwnedUserId) -> Result {
self.services
.oauth
.sessions
.get_sess_id_by_user(&user_id)
.try_for_each(async |id| {
let session = self.services.oauth.sessions.get(&id).await?;
self.write_str(&format!("{session:#?}\n")).await
})
.await
}
#[admin_command]
pub(super) async fn oauth_token_info(&self, id: SessionId) -> Result {
let session = self.services.oauth.sessions.get(&id).await?;
let provider = self
.services
.oauth
.sessions
.provider(&session)
.await?;
let tokeninfo = self
.services
.oauth
.request_tokeninfo((&provider, &session))
.await?;
self.write_str(&format!("{tokeninfo:#?}\n")).await
}
#[admin_command]
pub(super) async fn oauth_revoke(&self, id: SessionOrUserId) -> Result {
match id {
| Left(sess_id) => {
let session = self.services.oauth.sessions.get(&sess_id).await?;
let provider = self
.services
.oauth
.sessions
.provider(&session)
.await?;
self.services
.oauth
.revoke_token((&provider, &session))
.await
.ok();
},
| Right(user_id) =>
self.services
.oauth
.revoke_user_tokens(&user_id)
.await,
}
self.write_str("revoked").await
}
#[admin_command]
pub(super) async fn oauth_delete(&self, id: SessionOrUserId, force: bool) -> Result {
if !force {
return Err!(
"Deleting these records can cause registration conflicts. Use --force to be sure."
);
}
match id {
| Left(sess_id) => {
self.services
.oauth
.sessions
.delete(&sess_id)
.await;
},
| Right(user_id) => {
self.services
.oauth
.delete_user_sessions(&user_id)
.await;
},
}
self.write_str("deleted any oauth state for {id}")
.await
}
+41 -14
View File
@@ -1,9 +1,9 @@
use clap::Subcommand;
use ruma::OwnedUserId;
use tuwunel_core::Result;
use tuwunel_macros::{admin_command, admin_command_dispatch};
use crate::Context;
#[admin_command_dispatch]
#[derive(Debug, Subcommand)]
pub(crate) enum PusherCommand {
/// - Returns all the pushers for the user.
@@ -11,19 +11,46 @@ pub(crate) enum PusherCommand {
/// Full user ID
user_id: OwnedUserId,
},
/// - Manually delete a pusher for a user.
RemovePusher {
/// Full user ID
user_id: OwnedUserId,
/// Pushkey
pushkey: String,
},
}
pub(super) async fn process(subcommand: PusherCommand, context: &Context<'_>) -> Result {
let services = context.services;
#[admin_command]
pub(super) async fn get_pushers(&self, user_id: OwnedUserId) -> Result {
let timer = tokio::time::Instant::now();
let results = self.services.pusher.get_pushers(&user_id).await;
let query_time = timer.elapsed();
match subcommand {
| PusherCommand::GetPushers { user_id } => {
let timer = tokio::time::Instant::now();
let results = services.pusher.get_pushers(&user_id).await;
let query_time = timer.elapsed();
write!(context, "Query completed in {query_time:?}:\n\n```rs\n{results:#?}\n```")
},
}
.await
self.write_string(format!("Query completed in {query_time:?}:\n\n```rs\n{results:#?}```"))
.await
}
#[admin_command]
pub(super) async fn remove_pusher(&self, user_id: OwnedUserId, pushkey: String) -> Result {
let exists = self
.services
.pusher
.get_pusher(&user_id, &pushkey)
.await
.is_ok();
self.services
.pusher
.delete_pusher(&user_id, &pushkey)
.await;
let message = if exists {
"Pusher deleted."
} else {
"Pusher was not found but deletion was still attempted."
};
self.write_str(message).await
}
+162 -136
View File
@@ -1,24 +1,26 @@
use std::{borrow::Cow, collections::BTreeMap, ops::Deref, sync::Arc};
use std::{collections::BTreeMap, fmt::Write, sync::Arc};
use base64::prelude::*;
use clap::Subcommand;
use futures::{FutureExt, Stream, StreamExt, TryStreamExt};
use futures::{FutureExt, StreamExt, TryStreamExt};
use tokio::time::Instant;
use tuwunel_core::{
Err, Result, apply, at, is_zero,
Err, Result, apply, at, err, is_zero,
itertools::Itertools,
utils::{
TryReadyExt,
math::Expected,
stream::{IterStream, ReadyExt, TryIgnore, TryParallelExt},
string::EMPTY,
},
};
use tuwunel_database::Map;
use tuwunel_database::{KeyVal, Map};
use tuwunel_service::Services;
use crate::{admin_command, admin_command_dispatch};
#[admin_command_dispatch(handler_prefix = "raw")]
#[derive(Debug, Subcommand)]
#[allow(clippy::enum_variant_names)]
/// Query tables from database
pub(crate) enum RawCommand {
/// - List database maps
@@ -44,6 +46,39 @@ pub(crate) enum RawCommand {
/// Key prefix
prefix: Option<String>,
/// Limit
#[arg(short, long)]
limit: Option<usize>,
/// Lower bound
#[arg(short, long)]
from: Option<String>,
/// Reverse iteration order
#[arg(short, long, default_value("false"))]
backwards: bool,
},
/// - Raw database items iteration
Iter {
/// Map name
map: String,
/// Key prefix
prefix: Option<String>,
/// Limit
#[arg(short, long)]
limit: Option<usize>,
/// Lower bound
#[arg(short, long)]
from: Option<String>,
/// Reverse iteration order
#[arg(short, long, default_value("false"))]
backwards: bool,
},
/// - Raw database key size breakdown
@@ -82,41 +117,6 @@ pub(crate) enum RawCommand {
prefix: Option<String>,
},
/// - Raw database items iteration
Iter {
/// Map name
map: String,
/// Key prefix
prefix: Option<String>,
},
/// - Raw database keys iteration
KeysFrom {
/// Map name
map: String,
/// Lower-bound
start: String,
/// Limit
#[arg(short, long)]
limit: Option<usize>,
},
/// - Raw database items iteration
IterFrom {
/// Map name
map: String,
/// Lower-bound
start: String,
/// Limit
#[arg(short, long)]
limit: Option<usize>,
},
/// - Raw database record count
Count {
/// Map name
@@ -148,7 +148,7 @@ pub(crate) enum RawCommand {
/// - Compact database DANGER!!!
Compact {
#[arg(short, long, alias("column"))]
map: Option<Vec<String>>,
maps: Option<Vec<String>>,
#[arg(long)]
start: Option<String>,
@@ -177,7 +177,7 @@ pub(crate) enum RawCommand {
#[admin_command]
pub(super) async fn raw_compact(
&self,
map: Option<Vec<String>>,
maps: Option<Vec<String>>,
start: Option<String>,
stop: Option<String>,
from: Option<usize>,
@@ -187,26 +187,7 @@ pub(super) async fn raw_compact(
) -> Result {
use tuwunel_database::compact::Options;
let default_all_maps: Option<_> = map.is_none().then(|| {
self.services
.db
.keys()
.map(Deref::deref)
.map(ToOwned::to_owned)
});
let maps: Vec<_> = map
.unwrap_or_default()
.into_iter()
.chain(default_all_maps.into_iter().flatten())
.map(|map| self.services.db.get(&map))
.filter_map(Result::ok)
.cloned()
.collect();
if maps.is_empty() {
return Err!("--map argument invalid. not found in database");
}
let maps = with_maps_or(maps.as_deref(), self.services)?;
let range = (
start
@@ -248,7 +229,9 @@ pub(super) async fn raw_count(&self, map: Option<String>, prefix: Option<String>
let prefix = prefix.as_deref().unwrap_or(EMPTY);
let timer = Instant::now();
let count = with_maps_or(map.as_deref(), self.services)
let count = with_map_or(map.as_deref(), self.services)?
.iter()
.stream()
.then(|map| map.raw_count_prefix(&prefix))
.ready_fold(0_usize, usize::saturating_add)
.await;
@@ -259,16 +242,45 @@ pub(super) async fn raw_count(&self, map: Option<String>, prefix: Option<String>
}
#[admin_command]
pub(super) async fn raw_keys(&self, map: String, prefix: Option<String>) -> Result {
pub(super) async fn raw_keys(
&self,
map: String,
prefix: Option<String>,
limit: Option<usize>,
from: Option<String>,
backwards: bool,
) -> Result {
writeln!(self, "```").boxed().await?;
let map = self.services.db.get(map.as_str())?;
let timer = Instant::now();
prefix
.as_deref()
.map_or_else(|| map.raw_keys().boxed(), |prefix| map.raw_keys_prefix(prefix).boxed())
.map_ok(String::from_utf8_lossy)
.try_for_each(|str| writeln!(self, "{str:?}"))
let stream = match from.as_ref().or(prefix.as_ref()) {
| Some(from) =>
if !backwards {
map.raw_keys_from(from).boxed()
} else {
map.rev_raw_keys_from(from).boxed()
},
| None =>
if !backwards {
map.raw_keys().boxed()
} else {
map.rev_raw_keys().boxed()
},
};
let prefix = prefix.as_ref().map(String::as_bytes);
stream
.ready_try_take_while(|k| {
Ok(prefix
.map(|prefix| k.starts_with(prefix))
.unwrap_or(true))
})
.take(limit.unwrap_or(usize::MAX))
.map_ok(encode)
.try_for_each(|str| writeln!(self, "{str}"))
.boxed()
.await?;
@@ -282,7 +294,9 @@ pub(super) async fn raw_keys_sizes(&self, map: Option<String>, prefix: Option<St
let prefix = prefix.as_deref().unwrap_or(EMPTY);
let timer = Instant::now();
let result = with_maps_or(map.as_deref(), self.services)
let result = with_map_or(map.as_deref(), self.services)?
.iter()
.stream()
.map(|map| map.raw_keys_prefix(&prefix))
.flatten()
.ignore_err()
@@ -304,7 +318,9 @@ pub(super) async fn raw_keys_total(&self, map: Option<String>, prefix: Option<St
let prefix = prefix.as_deref().unwrap_or(EMPTY);
let timer = Instant::now();
let result = with_maps_or(map.as_deref(), self.services)
let result = with_map_or(map.as_deref(), self.services)?
.iter()
.stream()
.map(|map| map.raw_keys_prefix(&prefix))
.flatten()
.ignore_err()
@@ -322,7 +338,9 @@ pub(super) async fn raw_vals_sizes(&self, map: Option<String>, prefix: Option<St
let prefix = prefix.as_deref().unwrap_or(EMPTY);
let timer = Instant::now();
let result = with_maps_or(map.as_deref(), self.services)
let result = with_map_or(map.as_deref(), self.services)?
.iter()
.stream()
.map(|map| map.raw_stream_prefix(&prefix))
.flatten()
.ignore_err()
@@ -345,7 +363,9 @@ pub(super) async fn raw_vals_total(&self, map: Option<String>, prefix: Option<St
let prefix = prefix.as_deref().unwrap_or(EMPTY);
let timer = Instant::now();
let result = with_maps_or(map.as_deref(), self.services)
let result = with_map_or(map.as_deref(), self.services)?
.iter()
.stream()
.map(|map| map.raw_stream_prefix(&prefix))
.flatten()
.ignore_err()
@@ -360,40 +380,44 @@ pub(super) async fn raw_vals_total(&self, map: Option<String>, prefix: Option<St
}
#[admin_command]
pub(super) async fn raw_iter(&self, map: String, prefix: Option<String>) -> Result {
writeln!(self, "```").await?;
let map = self.services.db.get(&map)?;
let timer = Instant::now();
prefix
.as_deref()
.map_or_else(|| map.raw_stream().boxed(), |prefix| map.raw_stream_prefix(prefix).boxed())
.map_ok(apply!(2, String::from_utf8_lossy))
.map_ok(apply!(2, Cow::into_owned))
.try_for_each(|keyval| writeln!(self, "{keyval:?}"))
.boxed()
.await?;
let query_time = timer.elapsed();
self.write_str(&format!("\n```\n\nQuery completed in {query_time:?}"))
.await
}
#[admin_command]
pub(super) async fn raw_keys_from(
pub(super) async fn raw_iter(
&self,
map: String,
start: String,
prefix: Option<String>,
limit: Option<usize>,
from: Option<String>,
backwards: bool,
) -> Result {
writeln!(self, "```").await?;
let map = self.services.db.get(&map)?;
let timer = Instant::now();
map.raw_keys_from(&start)
.map_ok(String::from_utf8_lossy)
let stream = match from.as_ref().or(prefix.as_ref()) {
| Some(from) =>
if !backwards {
map.raw_stream_from(from).boxed()
} else {
map.rev_raw_stream_from(from).boxed()
},
| None =>
if !backwards {
map.raw_stream().boxed()
} else {
map.rev_raw_stream().boxed()
},
};
let prefix = prefix.as_ref().map(String::as_bytes);
stream
.ready_try_take_while(|(k, _): &KeyVal<'_>| {
Ok(prefix
.map(|prefix| k.starts_with(prefix))
.unwrap_or(true))
})
.take(limit.unwrap_or(usize::MAX))
.try_for_each(|str| writeln!(self, "{str:?}"))
.map_ok(apply!(2, encode))
.try_for_each(|(key, val)| writeln!(self, "{{{key} => {val}}}"))
.boxed()
.await?;
@@ -402,28 +426,6 @@ pub(super) async fn raw_keys_from(
.await
}
#[admin_command]
pub(super) async fn raw_iter_from(
&self,
map: String,
start: String,
limit: Option<usize>,
) -> Result {
let map = self.services.db.get(&map)?;
let timer = Instant::now();
let result = map
.raw_stream_from(&start)
.map_ok(apply!(2, String::from_utf8_lossy))
.map_ok(apply!(2, Cow::into_owned))
.take(limit.unwrap_or(usize::MAX))
.try_collect::<Vec<(String, String)>>()
.await?;
let query_time = timer.elapsed();
self.write_str(&format!("Query completed in {query_time:?}:\n\n```rs\n{result:#?}\n```"))
.await
}
#[admin_command]
pub(super) async fn raw_del(&self, map: String, key: String) -> Result {
let map = self.services.db.get(&map)?;
@@ -468,7 +470,7 @@ pub(super) async fn raw_get(&self, map: String, key: String, base64: bool) -> Re
let result = if base64 {
BASE64_STANDARD.encode(&handle)
} else {
String::from_utf8_lossy(&handle).to_string()
encode(&handle)
};
self.write_str(&format!("Query completed in {query_time:?}:\n\n```rs\n{result:?}\n```"))
@@ -488,19 +490,43 @@ pub(super) async fn raw_maps(&self) -> Result {
self.write_str(&format!("{list:#?}")).await
}
fn with_maps_or<'a>(
map: Option<&'a str>,
services: &'a Services,
) -> impl Stream<Item = &'a Arc<Map>> + Send + 'a {
let default_all_maps = map
.is_none()
.then(|| services.db.keys().map(Deref::deref))
.into_iter()
.flatten();
map.into_iter()
.chain(default_all_maps)
.map(|map| services.db.get(map))
.filter_map(Result::ok)
.stream()
fn with_map_or(map: Option<&str>, services: &Services) -> Result<Vec<Arc<Map>>> {
with_maps_or(
map.map(|map| [map])
.as_ref()
.map(<[&str; 1]>::as_slice),
services,
)
}
fn with_maps_or<S: AsRef<str>>(maps: Option<&[S]>, services: &Services) -> Result<Vec<Arc<Map>>> {
Ok(if let Some(maps) = maps {
maps.iter()
.map(|map| {
let map = map.as_ref();
services
.db
.get(map)
.cloned()
.map_err(|_| err!("map {map} not found"))
})
.try_collect()?
} else {
services.db.iter().map(|x| x.1.clone()).collect()
})
}
#[expect(clippy::as_conversions)]
fn encode(data: &[u8]) -> String {
let mut res = String::with_capacity(data.len().expected_mul(4));
for byte in data {
if *byte < 0x20 || *byte > 0x7E {
let _ = write!(res, "\\x{byte:02x}");
} else {
res.push(*byte as char);
}
}
res
}
+8 -8
View File
@@ -35,10 +35,10 @@ async fn destinations_cache(&self, server_name: Option<OwnedServerName>) -> Resu
.boxed();
while let Some((name, CachedDest { dest, host, expire })) = destinations.next().await {
if let Some(server_name) = server_name.as_ref() {
if name != server_name {
continue;
}
if let Some(server_name) = server_name.as_ref()
&& name != server_name
{
continue;
}
let expire = time::format(expire, "%+");
@@ -61,10 +61,10 @@ async fn overrides_cache(&self, server_name: Option<String>) -> Result {
while let Some((name, CachedOverride { ips, port, expire, overriding })) =
overrides.next().await
{
if let Some(server_name) = server_name.as_ref() {
if name != server_name {
continue;
}
if let Some(server_name) = server_name.as_ref()
&& name != server_name
{
continue;
}
let expire = time::format(expire, "%+");
+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);
-18
View File
@@ -13,8 +13,6 @@ pub(crate) enum UsersCommand {
IterUsers,
IterUsers2,
PasswordHash {
user_id: OwnedUserId,
},
@@ -257,22 +255,6 @@ async fn iter_users(&self) -> Result {
.await
}
#[admin_command]
async fn iter_users2(&self) -> Result {
let timer = tokio::time::Instant::now();
let result: Vec<_> = self.services.users.stream().collect().await;
let result: Vec<_> = result
.into_iter()
.map(ruma::UserId::as_bytes)
.map(String::from_utf8_lossy)
.collect();
let query_time = timer.elapsed();
self.write_str(&format!("Query completed in {query_time:?}:\n\n```rs\n{result:?}\n```"))
.await
}
#[admin_command]
async fn count_users(&self) -> Result {
let timer = tokio::time::Instant::now();
+10 -10
View File
@@ -50,10 +50,10 @@ async fn ban_room(&self, room: OwnedRoomOrAliasId) -> Result {
let admin_room_alias = &self.services.admin.admin_alias;
if let Ok(admin_room_id) = self.services.admin.get_admin_room().await {
if room.to_string().eq(&admin_room_id) || room.to_string().eq(admin_room_alias) {
return Err!("Not allowed to ban the admin room.");
}
if let Ok(admin_room_id) = self.services.admin.get_admin_room().await
&& (room.to_string().eq(&admin_room_id) || room.to_string().eq(admin_room_alias))
{
return Err!("Not allowed to ban the admin room.");
}
let room_id = if room.is_room_id() {
@@ -217,12 +217,12 @@ async fn ban_list_of_rooms(&self) -> Result {
for &room in &rooms_s {
match <&RoomOrAliasId>::try_from(room) {
| Ok(room_alias_or_id) => {
if let Ok(admin_room_id) = self.services.admin.get_admin_room().await {
if room.to_owned().eq(&admin_room_id) || room.to_owned().eq(admin_room_alias)
{
warn!("User specified admin room in bulk ban list, ignoring");
continue;
}
if let Ok(admin_room_id) = self.services.admin.get_admin_room().await
&& (room.to_owned().eq(&admin_room_id)
|| room.to_owned().eq(admin_room_alias))
{
warn!("User specified admin room in bulk ban list, ignoring");
continue;
}
if room_alias_or_id.is_room_id() {
+61
View File
@@ -0,0 +1,61 @@
use futures::StreamExt;
use tuwunel_core::{Result, utils};
use tuwunel_macros::admin_command;
use tuwunel_service::registration_tokens::TokenExpires;
#[admin_command]
pub(super) async fn issue(
&self,
max_uses: Option<u64>,
max_age: Option<String>,
once: bool,
) -> Result {
let expires = TokenExpires {
max_uses: max_uses.or_else(|| once.then_some(1)),
max_age: max_age
.map(|max_age| {
let duration = utils::time::parse_duration(&max_age)?;
utils::time::timepoint_from_now(duration)
})
.transpose()?,
};
let (token, info) = self
.services
.registration_tokens
.issue_token(expires)
.await?;
self.write_str(&format!("New registration token issued: `{token}` - {info}",))
.await
}
#[admin_command]
pub(super) async fn revoke(&self, token: String) -> Result {
self.services
.registration_tokens
.revoke_token(&token)
.await?;
self.write_str("Token revoked successfully.")
.await
}
#[admin_command]
pub(super) async fn list(&self) -> Result {
let tokens: Vec<_> = self
.services
.registration_tokens
.iterate_tokens()
.collect()
.await;
self.write_str(&format!("Found {} registration tokens:\n", tokens.len()))
.await?;
for token in tokens {
self.write_str(&format!("- {token}\n")).await?;
}
Ok(())
}
+36
View File
@@ -0,0 +1,36 @@
mod commands;
use clap::Subcommand;
use tuwunel_core::Result;
use crate::admin_command_dispatch;
#[admin_command_dispatch]
#[derive(Debug, Subcommand)]
pub(crate) enum TokenCommand {
/// - Issue a new registration token
Issue {
/// The maximum number of times this token is allowed to be used before
/// it expires.
#[arg(long)]
max_uses: Option<u64>,
/// The maximum age of this token (e.g. 30s, 5m, 7d). It will expire
/// after this much time has passed.
#[arg(long)]
max_age: Option<String>,
/// A shortcut for `--max-uses 1`.
#[arg(long)]
once: bool,
},
/// - Revoke a registration token
Revoke {
/// The token to revoke.
token: String,
},
/// - List all registration tokens
List,
}
+67 -164
View File
@@ -1,6 +1,6 @@
use std::{collections::BTreeMap, fmt::Write as _};
use std::{cmp, collections::BTreeMap};
use futures::{FutureExt, StreamExt};
use futures::{FutureExt, StreamExt, TryStreamExt};
use ruma::{
Int, OwnedDeviceId, OwnedEventId, OwnedRoomId, OwnedRoomOrAliasId, OwnedUserId, UserId,
events::{
@@ -11,14 +11,14 @@ use ruma::{
},
tag::{TagEvent, TagEventContent, TagInfo},
},
uint,
};
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,
utils::{self, ReadyExt, stream::IterStream},
};
use tuwunel_service::Services;
use tuwunel_service::{Services, users::Register};
use crate::{
admin_command, get_room_info,
@@ -50,10 +50,10 @@ pub(super) async fn create_user(&self, username: String, password: Option<String
// Validate user id
let user_id = parse_local_user_id(self.services, &username)?;
if let Err(e) = user_id.validate_strict() {
if self.services.config.emergency_password.is_none() {
return Err!("Username {user_id} contains disallowed characters or spaces: {e}");
}
if let Err(e) = user_id.validate_strict()
&& self.services.config.emergency_password.is_none()
{
return Err!("Username {user_id} contains disallowed characters or spaces: {e}");
}
if self.services.users.exists(&user_id).await {
@@ -62,148 +62,16 @@ 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(Register {
user_id: Some(&user_id),
password: Some(&password),
grant_first_user_admin: true,
..Default::default()
})
.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
}
@@ -263,8 +131,9 @@ pub(super) async fn reset_password(&self, username: String, password: Option<Str
.await
{
| Err(e) => return Err!("Couldn't reset the password for user {user_id}: {e}"),
| Ok(()) =>
write!(self, "Successfully reset the password for user {user_id}: `{new_password}`"),
| Ok(()) => {
write!(self, "Successfully reset the password for user {user_id}: `{new_password}`")
},
}
.await
}
@@ -403,7 +272,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
@@ -415,7 +284,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.",
);
}
@@ -427,7 +296,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
@@ -505,9 +374,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
@@ -534,7 +404,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 {
@@ -551,7 +421,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
@@ -600,9 +470,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
@@ -627,16 +498,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!(
@@ -648,7 +515,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);
@@ -1019,3 +886,39 @@ pub(super) async fn redact_event(&self, event_id: OwnedEventId) -> Result {
))
.await
}
#[admin_command]
pub(super) async fn last_active(&self, limit: Option<usize>) -> Result {
self.services
.users
.list_local_users()
.map(ToOwned::to_owned)
.then(async |user_id| {
self.services
.users
.all_devices_metadata(&user_id)
.ready_filter_map(|device| device.last_seen_ts)
.ready_fold_default(cmp::max)
.map(|last_seen_ts| (last_seen_ts, user_id.clone()))
.await
})
.ready_filter(|(ts, _)| ts.get() > uint!(0))
.collect::<Vec<_>>()
.map(|mut vec| {
vec.sort_by_key(|k| cmp::Reverse(k.0));
vec
})
.map(Vec::into_iter)
.map(IterStream::try_stream)
.flatten_stream()
.take(limit.unwrap_or(48))
.try_for_each(async |(last_seen_ts, user_id)| {
let ago = last_seen_ts;
let user_id = user_id.localpart();
let line = format!("{ago:?} {user_id}\n");
self.write_str(&line).await
})
.boxed()
.await
}
+9 -3
View File
@@ -65,6 +65,12 @@ pub(super) enum UserCommand {
device_id: OwnedDeviceId,
},
/// - List local users by recent activity.
LastActive {
#[arg(short, long)]
limit: Option<usize>,
},
/// - List local users in the database
#[clap(alias = "list")]
ListUsers,
@@ -78,7 +84,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.
@@ -148,7 +154,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,
@@ -160,7 +166,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,
+1 -1
View File
@@ -1,4 +1,4 @@
#![allow(dead_code)]
#![expect(dead_code)]
use ruma::{OwnedRoomId, OwnedUserId, RoomId, UserId};
use tuwunel_core::{Err, Result, err};
+1 -2
View File
@@ -74,7 +74,6 @@ zstd_compression = [
]
[dependencies]
async-trait.workspace = true
axum-client-ip.workspace = true
axum-extra.workspace = true
axum.workspace = true
@@ -87,7 +86,6 @@ hmac.workspace = true
http.workspace = true
http-body-util.workspace = true
hyper.workspace = true
ipaddress.workspace = true
itertools.workspace = true
log.workspace = true
rand.workspace = true
@@ -101,6 +99,7 @@ tokio.workspace = true
tracing.workspace = true
tuwunel-core.workspace = true
tuwunel-service.workspace = true
url.workspace = true
[lints]
workspace = true
+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");
+7 -7
View File
@@ -1,7 +1,7 @@
use axum::extract::State;
use futures::{
FutureExt, StreamExt, TryFutureExt, TryStreamExt,
future::{OptionFuture, join, join3, try_join3},
future::{join, join3, try_join3},
};
use ruma::{OwnedEventId, UserId, api::client::context::get_context, events::StateEventType};
use tuwunel_core::{
@@ -9,6 +9,7 @@ use tuwunel_core::{
utils::{
IterStream,
future::TryExtExt,
option::OptionExt,
stream::{BroadbandExt, ReadyExt, TryIgnore, WidebandExt},
},
};
@@ -32,8 +33,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,13 +111,13 @@ 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),
};
let lazy_loading_witnessed: OptionFuture<_> = filter
let lazy_loading_witnessed = filter
.lazy_load_options
.is_enabled()
.then_some(
@@ -125,8 +126,7 @@ pub(crate) async fn get_context_route(
.chain(events_before.iter())
.chain(events_after.iter()),
)
.map(|witnessed| lazy_loading_witness(&services, &lazy_loading_context, witnessed))
.into();
.map_async(|witnessed| lazy_loading_witness(&services, &lazy_loading_context, witnessed));
let state_at = events_after
.last()
+4 -6
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, utils::string::to_small_string};
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`
///
@@ -94,13 +94,11 @@ 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,
None,
(Some(&appservice.registration.as_token), None),
None,
None,
+36 -45
View File
@@ -1,3 +1,5 @@
use std::cmp;
use axum::extract::State;
use axum_client_ip::InsecureClientIp;
use futures::{
@@ -17,19 +19,15 @@ use ruma::{
federation,
},
directory::{Filter, PublicRoomsChunk, RoomNetwork, RoomTypeFilter},
events::{
StateEventType,
room::join_rules::{JoinRule, RoomJoinRulesEventContent},
},
events::StateEventType,
uint,
};
use tuwunel_core::{
Err, Result, err, info, is_true,
Err, Result, err, info,
matrix::Event,
utils::{
TryFutureExtExt,
math::Expected,
result::FlatOk,
stream::{IterStream, ReadyExt, WidebandExt},
},
};
@@ -220,8 +218,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,
@@ -301,30 +299,26 @@ pub(crate) async fn get_public_rooms_filtered_helper(
return None;
}
if let Some(query) = search_room_id {
if chunk.room_id.as_str().contains(query) {
if let Some(query) = search_room_id
&& chunk.room_id.as_str().contains(query) {
return Some(chunk);
}
}
if let Some(query) = search_term.as_deref() {
if let Some(name) = &chunk.name {
if name.as_str().to_lowercase().contains(query) {
if let Some(name) = &chunk.name
&& name.as_str().to_lowercase().contains(query) {
return Some(chunk);
}
}
if let Some(topic) = &chunk.topic {
if topic.to_lowercase().contains(query) {
if let Some(topic) = &chunk.topic
&& topic.to_lowercase().contains(query) {
return Some(chunk);
}
}
if let Some(canonical_alias) = &chunk.canonical_alias {
if canonical_alias.as_str().to_lowercase().contains(query) {
if let Some(canonical_alias) = &chunk.canonical_alias
&& canonical_alias.as_str().to_lowercase().contains(query) {
return Some(chunk);
}
}
return None;
}
@@ -336,7 +330,7 @@ pub(crate) async fn get_public_rooms_filtered_helper(
.collect()
.await;
all_rooms.sort_by(|l, r| r.num_joined_members.cmp(&l.num_joined_members));
all_rooms.sort_by_key(|r| cmp::Reverse(r.num_joined_members));
let total_room_count_estimate = UInt::try_from(all_rooms.len())
.unwrap_or_else(|_| uint!(0))
@@ -433,17 +427,19 @@ async fn public_rooms_chunk(services: &Services, room_id: OwnedRoomId) -> Public
let join_rule = services
.state_accessor
.room_state_get_content(&room_id, &StateEventType::RoomJoinRules, "")
.map_ok(|c: RoomJoinRulesEventContent| match c.join_rule {
| JoinRule::Public => "public".into(),
| JoinRule::Knock => "knock".into(),
| JoinRule::KnockRestricted(_) => "knock_restricted".into(),
| _ => "invite".into(),
});
.get_join_rules(&room_id)
.map(|join_rule| join_rule.kind());
let guest_can_join = services.state_accessor.guest_can_join(&room_id);
let num_joined_members = services.state_cache.room_joined_count(&room_id);
let num_joined_members = services
.state_cache
.room_joined_count(&room_id)
.map(|x| {
x.ok()
.and_then(|x| x.try_into().ok())
.unwrap_or_else(|| uint!(0))
});
let (
(avatar_url, canonical_alias, guest_can_join, join_rule, name),
@@ -459,13 +455,9 @@ async fn public_rooms_chunk(services: &Services, room_id: OwnedRoomId) -> Public
avatar_url: avatar_url.flatten(),
canonical_alias,
guest_can_join,
join_rule: join_rule.unwrap_or_default(),
join_rule,
name,
num_joined_members: num_joined_members
.map(TryInto::try_into)
.map(Result::ok)
.flat_ok()
.unwrap_or_else(|| uint!(0)),
num_joined_members,
room_id,
room_type,
topic,
@@ -478,18 +470,17 @@ fn check_server_banned(services: &Services, server: Option<&ServerName>) -> Resu
return Ok(());
};
let conditions = [
services
.config
.forbidden_remote_room_directory_server_names
.is_match(server.host()),
services
let host = server.host();
if services
.config
.forbidden_remote_room_directory_server_names
.is_match(host)
|| services
.config
.forbidden_remote_server_names
.is_match(server.host()),
];
if conditions.iter().any(is_true!()) {
.is_match(host)
{
return Err!(Request(Forbidden("Server is banned on this homeserver.")));
}
+6 -4
View File
@@ -23,7 +23,7 @@ pub(crate) async fn events_route(
State(services): State<crate::State>,
body: Ruma<Request>,
) -> Result<Response> {
let (sender_user, sender_device) = body.sender();
let sender_user = body.sender_user();
let from = body
.body
@@ -62,9 +62,11 @@ pub(crate) async fn events_route(
.expect("configuration must limit maximum timeout");
loop {
let watchers = services
.sync
.watch(sender_user, sender_device, once(room_id).stream());
let watchers = services.sync.watch(
sender_user,
body.sender_device.as_deref(),
once(room_id).stream(),
);
let next_batch = services.globals.wait_pending().await?;
+9 -11
View File
@@ -35,7 +35,8 @@ 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()?;
let one_time_keys = body
.one_time_keys
@@ -483,10 +484,10 @@ where
{
self_signing_keys.insert(user_id.to_owned(), self_signing_key);
}
if Some(user_id) == sender_user {
if let Ok(user_signing_key) = services.users.get_user_signing_key(user_id).await {
user_signing_keys.insert(user_id.to_owned(), user_signing_key);
}
if Some(user_id) == sender_user
&& let Ok(user_signing_key) = services.users.get_user_signing_key(user_id).await
{
user_signing_keys.insert(user_id.to_owned(), user_signing_key);
}
}
@@ -503,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)
})
@@ -631,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 -1
View File
@@ -1,4 +1,4 @@
#![allow(deprecated)]
#![expect(deprecated)]
use axum::extract::State;
use axum_client_ip::InsecureClientIp;
+3 -6
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")));
@@ -50,11 +49,9 @@ pub(crate) async fn invite_user_route(
if let Ok(target_user_membership) = services
.state_accessor
.get_member(room_id, user_id)
.await
.await && target_user_membership.membership == MembershipState::Ban
{
if target_user_membership.membership == MembershipState::Ban {
return Err!(Request(Forbidden("User is banned from this room.")));
}
return Err!(Request(Forbidden("User is banned from this room.")));
}
if recipient_ignored_by_sender {
+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".into(),
CanonicalJsonValue::String(services.globals.server_name().as_str().to_owned()),
);
knock_event_stub.insert(
"origin_server_ts".into(),
CanonicalJsonValue::Integer(
utils::millis_since_unix_epoch()
.try_into()
.expect("Timestamp is valid js_int value"),
),
);
knock_event_stub.insert(
"content".into(),
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".into(), 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".into(),
CanonicalJsonValue::String(services.globals.server_name().as_str().to_owned()),
);
knock_event_stub.insert(
"origin_server_ts".into(),
CanonicalJsonValue::Integer(
utils::millis_since_unix_epoch()
.try_into()
.expect("Timestamp is valid js_int value"),
),
);
knock_event_stub.insert(
"content".into(),
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".into(), 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 -140
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,86 +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
servers.append(&mut additional_servers);
servers.sort_unstable();
servers.dedup();
shuffle(&mut 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))
}
+26 -29
View File
@@ -1,5 +1,5 @@
use axum::extract::State;
use futures::{FutureExt, StreamExt, TryFutureExt, future::OptionFuture, pin_mut};
use futures::{FutureExt, StreamExt, TryFutureExt, future::Either, pin_mut};
use ruma::{
RoomId, UserId,
api::{
@@ -17,7 +17,7 @@ use tuwunel_core::{
},
ref_at,
utils::{
IterStream, ReadyExt,
BoolExt, IterStream, ReadyExt,
result::{FlatOk, LogErr},
stream::{BroadbandExt, TryIgnore, WidebandExt},
},
@@ -105,17 +105,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
@@ -135,11 +136,10 @@ pub(crate) async fn get_message_events_route(
options: Some(&filter.lazy_load_options),
};
let witness: OptionFuture<_> = filter
let witness = filter
.lazy_load_options
.is_enabled()
.then(|| lazy_loading_witness(&services, &lazy_loading_context, events.iter()))
.into();
.then_async(|| lazy_loading_witness(&services, &lazy_loading_context, events.iter()));
let state = witness
.map(Option::into_iter)
@@ -257,26 +257,23 @@ where
return true;
}
let ignored_type = IGNORED_MESSAGE_TYPES
if IGNORED_MESSAGE_TYPES
.binary_search(event.kind())
.is_ok();
.is_err()
{
return false;
}
let ignored_server = services
.config
.forbidden_remote_server_names
.is_match(event.sender().server_name().host());
if ignored_type
&& (ignored_server
|| services
.users
.user_is_ignored(event.sender(), user_id)
.await)
{
return true;
}
false
ignored_server
|| services
.users
.user_is_ignored(event.sender(), user_id)
.await
}
#[inline]
-3
View File
@@ -88,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;
+10 -6
View File
@@ -23,7 +23,12 @@ pub(crate) async fn set_presence_route(
services
.presence
.set_presence(body.sender_user(), &body.presence, None, None, body.status_msg.clone())
.set_presence_for_device(
body.sender_user(),
body.sender_device.as_deref(),
&body.presence,
body.status_msg.clone(),
)
.await?;
Ok(set_presence::v3::Response {})
@@ -48,14 +53,13 @@ pub(crate) async fn get_presence_route(
.user_sees_user(body.sender_user(), &body.user_id)
.await;
if has_shared_rooms {
if let Ok(presence) = services
if has_shared_rooms
&& let Ok(presence) = services
.presence
.get_presence(&body.user_id)
.await
{
presence_event = Some(presence);
}
{
presence_event = Some(presence);
}
match presence_event {
+22 -22
View File
@@ -43,7 +43,7 @@ pub(crate) async fn set_displayname_route(
services
.users
.update_displayname(&body.user_id, body.displayname.clone(), &all_joined_rooms)
.update_displayname(&body.user_id, body.displayname.as_deref(), &all_joined_rooms)
.await;
// Presence update
@@ -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(),
@@ -87,13 +87,13 @@ pub(crate) async fn get_displayname_route(
services
.users
.set_displayname(&body.user_id, response.displayname.clone());
.set_displayname(&body.user_id, response.displayname.as_deref());
services
.users
.set_avatar_url(&body.user_id, response.avatar_url.clone());
.set_avatar_url(&body.user_id, response.avatar_url.as_deref());
services
.users
.set_blurhash(&body.user_id, response.blurhash.clone());
.set_blurhash(&body.user_id, response.blurhash.as_deref());
return Ok(get_display_name::v3::Response { displayname: response.displayname });
}
@@ -140,8 +140,8 @@ pub(crate) async fn set_avatar_url_route(
.users
.update_avatar_url(
&body.user_id,
body.avatar_url.clone(),
body.blurhash.clone(),
body.avatar_url.as_deref(),
body.blurhash.as_deref(),
&all_joined_rooms,
)
.await;
@@ -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(),
@@ -188,13 +188,13 @@ pub(crate) async fn get_avatar_url_route(
services
.users
.set_displayname(&body.user_id, response.displayname.clone());
.set_displayname(&body.user_id, response.displayname.as_deref());
services
.users
.set_avatar_url(&body.user_id, response.avatar_url.clone());
.set_avatar_url(&body.user_id, response.avatar_url.as_deref());
services
.users
.set_blurhash(&body.user_id, response.blurhash.clone());
.set_blurhash(&body.user_id, response.blurhash.as_deref());
return Ok(get_avatar_url::v3::Response {
avatar_url: response.avatar_url,
@@ -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(),
@@ -250,22 +250,22 @@ pub(crate) async fn get_profile_route(
services
.users
.set_displayname(&body.user_id, response.displayname.clone());
.set_displayname(&body.user_id, response.displayname.as_deref());
services
.users
.set_avatar_url(&body.user_id, response.avatar_url.clone());
.set_avatar_url(&body.user_id, response.avatar_url.as_deref());
services
.users
.set_blurhash(&body.user_id, response.blurhash.clone());
.set_blurhash(&body.user_id, response.blurhash.as_deref());
services
.users
.set_timezone(&body.user_id, response.tz.clone());
.set_timezone(&body.user_id, response.tz.as_deref());
for (profile_key, profile_key_value) in &response.custom_profile_fields {
services.users.set_profile_key(
&body.user_id,
profile_key,
Some(profile_key_value.clone()),
Some(profile_key_value),
);
}
@@ -273,7 +273,7 @@ pub(crate) async fn get_profile_route(
("avatar_url", response.avatar_url.map(Into::into)),
("blurhash", response.blurhash),
("displayname", response.displayname),
("tz", response.tz),
("m.tz", response.tz),
];
let response = canonical_fields
@@ -314,7 +314,7 @@ pub(crate) async fn get_profile_route(
("avatar_url", avatar_url.map(Into::into)),
("blurhash", blurhash),
("displayname", displayname),
("tz", tz),
("m.tz", tz),
];
let response = canonical_fields
+6 -6
View File
@@ -157,7 +157,7 @@ pub(crate) async fn get_pushrules_all_route(
// remove old deprecated mentions push rules as per MSC4210
// and update the stored server default push rules
#[allow(deprecated)]
#[expect(deprecated)]
{
use ruma::push::RuleKind::*;
if global_ruleset
@@ -245,7 +245,7 @@ pub(crate) async fn get_pushrules_global_route(
// remove old deprecated mentions push rules as per MSC4210
// and update the stored server default push rules
#[allow(deprecated)]
#[expect(deprecated)]
{
use ruma::push::RuleKind::*;
if global_ruleset
@@ -303,7 +303,7 @@ pub(crate) async fn get_pushrule_route(
.expect("user is authenticated");
// remove old deprecated mentions push rules as per MSC4210
#[allow(deprecated)]
#[expect(deprecated)]
if body.rule_id.as_str() == PredefinedContentRuleId::ContainsUserName.as_str()
|| body.rule_id.as_str() == PredefinedOverrideRuleId::ContainsDisplayName.as_str()
|| body.rule_id.as_str() == PredefinedOverrideRuleId::RoomNotif.as_str()
@@ -396,7 +396,7 @@ pub(crate) async fn get_pushrule_actions_route(
let sender_user = body.sender_user();
// remove old deprecated mentions push rules as per MSC4210
#[allow(deprecated)]
#[expect(deprecated)]
if body.rule_id.as_str() == PredefinedContentRuleId::ContainsUserName.as_str()
|| body.rule_id.as_str() == PredefinedOverrideRuleId::ContainsDisplayName.as_str()
|| body.rule_id.as_str() == PredefinedOverrideRuleId::RoomNotif.as_str()
@@ -463,7 +463,7 @@ pub(crate) async fn get_pushrule_enabled_route(
let sender_user = body.sender_user();
// remove old deprecated mentions push rules as per MSC4210
#[allow(deprecated)]
#[expect(deprecated)]
if body.rule_id.as_str() == PredefinedContentRuleId::ContainsUserName.as_str()
|| body.rule_id.as_str() == PredefinedOverrideRuleId::ContainsDisplayName.as_str()
|| body.rule_id.as_str() == PredefinedOverrideRuleId::RoomNotif.as_str()
@@ -589,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())
+61 -247
View File
@@ -2,26 +2,20 @@ use std::fmt::Write;
use axum::extract::State;
use axum_client_ip::InsecureClientIp;
use futures::FutureExt;
use register::RegistrationKind;
use ruma::{
UserId,
api::client::{
account::{
check_registration_token_validity, get_username_availability,
register::{self, LoginType},
register::{self, LoginType, RegistrationKind},
},
uiaa::{AuthFlow, AuthType, UiaaInfo},
},
events::GlobalAccountDataEventType,
push,
};
use tuwunel_core::{
Err, Error, Result, debug_info, debug_warn, error, info, is_equal_to, utils, warn,
};
use tuwunel_service::users::device::generate_refresh_token;
use tuwunel_core::{Err, Error, Result, debug_info, debug_warn, info, utils};
use tuwunel_service::users::{Register, 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;
@@ -48,15 +42,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
@@ -104,10 +93,10 @@ pub(crate) async fn get_register_available_route(
return Err!(Request(UserInUse("User ID is not available.")));
}
if let Some(ref info) = body.appservice_info {
if !info.is_user_match(&user_id) {
return Err!(Request(Exclusive("Username is not in an appservice namespace.")));
}
if let Some(ref info) = body.appservice_info
&& !info.is_user_match(&user_id)
{
return Err!(Request(Exclusive("Username is not in an appservice namespace.")));
}
if services
@@ -138,7 +127,7 @@ pub(crate) async fn get_register_available_route(
/// - Creates a new account and populates it with default account data
/// - If `inhibit_login` is false: Creates a device and returns device id and
/// access_token
#[allow(clippy::doc_markdown)]
#[expect(clippy::doc_markdown)]
#[tracing::instrument(skip_all, fields(%client), name = "register")]
pub(crate) async fn register_route(
State(services): State<crate::State>,
@@ -148,71 +137,32 @@ 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 {
let display_name = body
.initial_device_display_name
.as_deref()
.unwrap_or("");
debug_warn!(
"Guest registration disabled / registration enabled with token configured, \
rejecting guest registration attempt, initial device name: \"{display_name}\""
%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 {
let display_name = body
.initial_device_display_name
.as_deref()
.unwrap_or("");
warn!(
"Guest account attempted to register before a real admin user has been registered, \
rejecting registration. Guest's initial device name: \"{display_name}\""
);
return Err!(Request(Forbidden("Registration is temporarily disabled.")));
}
let user_id = match (body.username.as_ref(), is_guest) {
| (Some(username), false) => {
// workaround for https://github.com/matrix-org/matrix-appservice-irc/issues/1780 due to inactivity of fixing the issue
@@ -313,7 +263,7 @@ pub(crate) async fn register_route(
// UIAA
let mut uiaainfo;
let skip_auth = if services.globals.registration_token.is_some() && !is_guest {
let skip_auth = if services.registration_tokens.is_enabled().await && !is_guest {
// Registration token required
uiaainfo = UiaaInfo {
flows: vec![AuthFlow {
@@ -378,45 +328,16 @@ 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(Register {
user_id: Some(&user_id),
password,
appservice_info: body.appservice_info.as_ref(),
is_guest,
grant_first_user_admin: true,
..Default::default()
})
.await?;
if (!is_guest && body.inhibit_login)
@@ -426,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,
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;
}
}
@@ -611,9 +419,15 @@ 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 {
if !services.registration_tokens.is_enabled().await {
return Err!(Request(Forbidden("Server does not allow token registration")));
};
}
Ok(check_registration_token_validity::v1::Response { valid: reg_token == body.token })
let valid = services
.registration_tokens
.is_token_valid(&body.token)
.await
.is_ok();
Ok(check_registration_token_validity::v1::Response { valid })
}
+114 -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;
@@ -98,7 +108,13 @@ pub(crate) async fn get_relating_events_route(
.await
}
#[allow(clippy::too_many_arguments)]
#[expect(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,76 +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 = events
.last()
.map(at!(0))
.as_ref()
.map(ToString::to_string);
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.")));
}
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)
}
+12 -14
View File
@@ -1,7 +1,7 @@
use std::collections::BTreeMap;
use axum::extract::State;
use futures::{FutureExt, future::OptionFuture};
use futures::FutureExt;
use ruma::{
CanonicalJsonObject, EventEncryptionAlgorithm, Int, OwnedRoomAliasId, OwnedRoomId,
OwnedUserId, RoomId, RoomVersionId,
@@ -32,7 +32,7 @@ use serde_json::{json, value::to_raw_value};
use tuwunel_core::{
Err, Result, debug_info, debug_warn, err, info,
matrix::{StateKey, pdu::PduBuilder, room_version},
utils::BoolExt,
utils::{BoolExt, option::OptionExt},
warn,
};
use tuwunel_service::{Services, appservice::RegistrationInfo, rooms::state::RoomMutexGuard};
@@ -55,7 +55,6 @@ use crate::{Ruma, client::utils::invite_check};
/// - Send events listed in initial state
/// - Send events implied by `name` and `topic`
/// - Send invite events
#[allow(clippy::large_stack_frames)]
pub(crate) async fn create_room_route(
State(services): State<crate::State>,
body: Ruma<create_room::v3::Request>,
@@ -72,11 +71,10 @@ pub(crate) async fn create_room_route(
| _ => RoomPreset::PrivateChat, // Room visibility should not be custom
});
let alias: OptionFuture<_> = body
let alias = body
.room_alias_name
.as_ref()
.map(|alias| room_alias_check(&services, alias, body.appservice_info.as_ref()))
.into();
.map_async(|alias| room_alias_check(&services, alias, body.appservice_info.as_ref()));
// Determine room version
let (room_version, version_rules) = body
@@ -447,10 +445,10 @@ async fn create_create_event(
))))
})?;
if !services.config.federate_created_rooms {
if !services.config.allow_federation || !content.contains_key("m.federate") {
content.insert("m.federate".into(), json!(false).try_into()?);
}
if !services.config.federate_created_rooms
&& (!services.config.allow_federation || !content.contains_key("m.federate"))
{
content.insert("m.federate".into(), json!(false).try_into()?);
}
content.insert(
@@ -579,10 +577,10 @@ async fn create_create_event_legacy(
},
}
if !services.config.federate_created_rooms {
if !services.config.allow_federation || !content.contains_key("m.federate") {
content.insert("m.federate".into(), json!(false).try_into()?);
}
if !services.config.federate_created_rooms
&& (!services.config.allow_federation || !content.contains_key("m.federate"))
{
content.insert("m.federate".into(), json!(false).try_into()?);
}
content.insert(
+52 -8
View File
@@ -1,7 +1,11 @@
use axum::extract::State;
use futures::{FutureExt, TryFutureExt, future::try_join};
use futures::{TryFutureExt, future::join3, pin_mut};
use ruma::api::client::room::get_room_event;
use tuwunel_core::{Err, Event, Result, err};
use tuwunel_core::{
Err, Event, Pdu, Result, err,
result::IsErrOr,
utils::{BoolExt, FutureBoolExt, TryFutureExtExt, future::OptionFutureExt},
};
use crate::{Ruma, client::is_ignored_pdu};
@@ -9,9 +13,10 @@ use crate::{Ruma, client::is_ignored_pdu};
///
/// Gets a single event.
pub(crate) async fn get_room_event_route(
State(ref services): State<crate::State>,
ref body: Ruma<get_room_event::v3::Request>,
State(services): State<crate::State>,
body: Ruma<get_room_event::v3::Request>,
) -> Result<get_room_event::v3::Response> {
let sender_user = body.sender_user();
let event_id = &body.event_id;
let room_id = &body.room_id;
@@ -20,14 +25,53 @@ pub(crate) async fn get_room_event_route(
.get_pdu(event_id)
.map_err(|_| err!(Request(NotFound("Event {} not found.", event_id))));
let retained_event = body
.include_unredacted_content
.then_async(async || {
let is_admin = services.admin.user_is_admin(sender_user);
let can_redact = services
.config
.allow_room_admins_to_request_unredacted_events
.then_async(|| {
services
.state_accessor
.get_power_levels(room_id)
.map_ok_or(false, |power_levels| {
power_levels.for_user(sender_user) >= power_levels.redact
})
})
.unwrap_or(false);
pin_mut!(is_admin, can_redact);
if is_admin.or(can_redact).await {
services
.retention
.get_original_pdu(event_id)
.await
.map_err(|_| err!(Request(NotFound("Event {} not found.", event_id))))
} else {
Err!(Request(Forbidden("You are not allowed to see the original event")))
}
});
let visible = services
.state_accessor
.user_can_see_event(body.sender_user(), room_id, event_id)
.map(Ok);
.user_can_see_event(sender_user, room_id, event_id);
let (mut event, visible) = try_join(event, visible).await?;
let (mut event, retained_event, visible): (Result<Pdu>, Option<Result<Pdu>>, _) =
join3(event, retained_event, visible).await;
if !visible || is_ignored_pdu(services, &event, body.sender_user()).await {
if event.as_ref().is_err_or(Event::is_redacted)
&& let Some(retained_event) = retained_event
{
event = retained_event;
}
let mut event = event?;
if !visible || is_ignored_pdu(&services, &event, body.sender_user()).await {
return Err!(Request(Forbidden("You don't have permission to view this event.")));
}
+11 -17
View File
@@ -1,10 +1,6 @@
use axum::extract::State;
use axum_client_ip::InsecureClientIp;
use futures::{
FutureExt, StreamExt, TryFutureExt,
future::{OptionFuture, join3},
stream::FuturesUnordered,
};
use futures::{FutureExt, StreamExt, TryFutureExt, future::join3, stream::FuturesUnordered};
use ruma::{
OwnedServerName, RoomId, UserId,
api::{
@@ -16,7 +12,7 @@ use ruma::{
};
use tuwunel_core::{
Err, Result, debug_warn, trace,
utils::{IterStream, future::TryExtExt},
utils::{IterStream, future::TryExtExt, option::OptionExt},
};
use tuwunel_service::Services;
@@ -55,7 +51,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 {
@@ -158,14 +154,12 @@ async fn local_room_summary_response(
.room_joined_count(room_id)
.unwrap_or(0);
let membership: OptionFuture<_> = sender_user
.map(|sender_user| {
services
.state_accessor
.get_member(room_id, sender_user)
.map_ok_or(MembershipState::Leave, |content| content.membership)
})
.into();
let membership = sender_user.map_async(|sender_user| {
services
.state_accessor
.get_member(room_id, sender_user)
.map_ok_or(MembershipState::Leave, |content| content.membership)
});
let (
canonical_alias,
@@ -232,8 +226,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
}
+8 -9
View File
@@ -1,7 +1,7 @@
use std::collections::BTreeMap;
use axum::extract::State;
use futures::{FutureExt, StreamExt, TryFutureExt, TryStreamExt, future::OptionFuture};
use futures::{FutureExt, StreamExt, TryFutureExt, TryStreamExt};
use ruma::{
OwnedRoomId, RoomId, UInt, UserId,
api::client::search::search_events::{
@@ -16,7 +16,7 @@ use tuwunel_core::{
Err, Result, at, is_true,
matrix::Event,
result::FlatOk,
utils::{IterStream, stream::ReadyExt},
utils::{IterStream, option::OptionExt, stream::ReadyExt},
};
use tuwunel_service::{Services, rooms::search::RoomQuery};
@@ -41,23 +41,22 @@ pub(crate) async fn search_events_route(
) -> Result<Response> {
let sender_user = body.sender_user();
let next_batch = body.next_batch.as_deref();
let room_events_result: OptionFuture<_> = body
let room_events = body
.search_categories
.room_events
.as_ref()
.map(|criteria| category_room_events(&services, sender_user, next_batch, criteria))
.into();
.map_async(|criteria| category_room_events(&services, sender_user, next_batch, criteria))
.await
.transpose()?;
Ok(Response {
search_categories: ResultCategories {
room_events: room_events_result
.await
.unwrap_or_else(|| Ok(ResultRoomEvents::default()))?,
room_events: room_events.unwrap_or_default(),
},
})
}
#[allow(clippy::map_unwrap_or)]
#[expect(clippy::map_unwrap_or)]
async fn category_room_events(
services: &Services,
sender_user: &UserId,
+2 -2
View File
@@ -10,12 +10,12 @@ use tuwunel_service::Services;
use crate::Ruma;
pub(super) async fn handle_login(
pub(super) fn handle_login(
services: &Services,
body: &Ruma<Request>,
info: &ApplicationService,
) -> Result<OwnedUserId> {
#[allow(deprecated)]
#[expect(deprecated)]
let ApplicationService { identifier, user } = info;
let Some(ref info) = body.appservice_info else {
+6 -3
View File
@@ -66,16 +66,19 @@ fn validate(config: &JwtConfig, token: &str) -> Result<Claim> {
fn init_verifier(config: &JwtConfig) -> Result<DecodingKey> {
let key = &config.key;
let format = config.format.as_str();
let format = config.format.to_uppercase();
Ok(match format {
Ok(match format.as_str() {
| "HMAC" => DecodingKey::from_secret(key.as_bytes()),
| "HMACB64" => DecodingKey::from_base64_secret(key.as_str())
.map_err(|e| err!(Config("jwt.key", "JWT key is not valid base64: {e}")))?,
| "ECDSA" => DecodingKey::from_ec_pem(key.as_bytes())
.map_err(|e| err!(Config("jwt.key", "JWT key is not valid PEM: {e}")))?,
.map_err(|e| err!(Config("jwt.key", "JWT key is not valid ECDSA PEM: {e}")))?,
| "EDDSA" => DecodingKey::from_ed_pem(key.as_bytes())
.map_err(|e| err!(Config("jwt.key", "JWT key is not valid EDDSA PEM: {e}")))?,
| _ => return Err!(Config("jwt.format", "Key format {format:?} is not supported.")),
})
+25 -17
View File
@@ -1,7 +1,7 @@
use futures::FutureExt;
use ruma::{OwnedUserId, UserId};
use tuwunel_core::{Err, Result, debug};
use tuwunel_service::Services;
use tuwunel_service::{Services, users::Register};
use super::password_login;
@@ -51,26 +51,34 @@ 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(Register {
user_id: Some(lowercased_user_id),
password: Some("*"),
origin: Some("ldap"),
..Default::default()
})
.await?;
}
let is_tuwunel_admin = services
.admin
.user_is_admin(lowercased_user_id)
.await;
// only perform admin add/remove check if admin_filter is set
if !services.config.ldap.admin_filter.is_empty() {
let is_tuwunel_admin = services
.admin
.user_is_admin(lowercased_user_id)
.await;
if is_ldap_admin && !is_tuwunel_admin {
services
.admin
.make_user_admin(lowercased_user_id)
.boxed()
.await?;
} else if !is_ldap_admin && is_tuwunel_admin {
services
.admin
.revoke_admin(lowercased_user_id)
.await?;
if is_ldap_admin && !is_tuwunel_admin {
services
.admin
.make_user_admin(lowercased_user_id)
.boxed()
.await?;
} else if !is_ldap_admin && is_tuwunel_admin {
services
.admin
.revoke_admin(lowercased_user_id)
.await?;
}
}
Ok(user_id)
+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())
+66 -38
View File
@@ -4,6 +4,7 @@ mod ldap;
mod logout;
mod password;
mod refresh;
mod sso;
mod token;
use axum::extract::State;
@@ -12,8 +13,8 @@ use ruma::api::client::session::{
get_login_types::{
self,
v3::{
ApplicationServiceLoginType, JwtLoginType, LoginType, PasswordLoginType,
TokenLoginType,
ApplicationServiceLoginType, IdentityProvider, JwtLoginType, LoginType,
PasswordLoginType, SsoLoginType, TokenLoginType,
},
},
login::{
@@ -21,16 +22,17 @@ 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};
pub(crate) use self::{
logout::{logout_all_route, logout_route},
refresh::refresh_token_route,
sso::{sso_callback_route, sso_login_route, sso_login_with_provider_route},
token::login_token_route,
};
use super::{DEVICE_ID_LENGTH, TOKEN_LENGTH};
use super::TOKEN_LENGTH;
use crate::Ruma;
/// # `GET /_matrix/client/v3/login`
@@ -43,14 +45,44 @@ pub(crate) async fn get_login_types_route(
InsecureClientIp(client): InsecureClientIp,
_body: Ruma<get_login_types::v3::Request>,
) -> Result<get_login_types::v3::Response> {
Ok(get_login_types::v3::Response::new(vec![
LoginType::Password(PasswordLoginType::default()),
let get_login_token = services.config.login_via_existing_session;
let list_idps = !services.config.sso_custom_providers_page && !services.config.single_sso;
let identity_providers: Vec<_> = services
.config
.identity_provider
.values()
.filter(|_| list_idps)
.cloned()
.map(|config| IdentityProvider {
id: config.id().to_owned(),
brand: Some(config.brand.clone().into()),
icon: config.icon,
name: config.name.unwrap_or(config.brand),
})
.collect();
let flows = [
LoginType::ApplicationService(ApplicationServiceLoginType::default()),
LoginType::Jwt(JwtLoginType::default()),
LoginType::Token(TokenLoginType {
get_login_token: services.config.login_via_existing_session,
}),
]))
LoginType::Password(PasswordLoginType::default()),
LoginType::Token(TokenLoginType { get_login_token }),
LoginType::Sso(SsoLoginType { identity_providers }),
];
Ok(get_login_types::v3::Response {
flows: flows
.into_iter()
.filter(|login_type| match login_type {
| LoginType::Sso(SsoLoginType { identity_providers })
if list_idps && identity_providers.is_empty() =>
false,
| _ => true,
})
.collect(),
})
}
/// # `POST /_matrix/client/v3/login`
@@ -79,7 +111,7 @@ pub(crate) async fn login_route(
| LoginInfo::Token(info) => token::handle_login(&services, &body, info).await?,
| LoginInfo::Jwt(info) => jwt::handle_login(&services, &body, info).await?,
| LoginInfo::ApplicationService(info) =>
appservice::handle_login(&services, &body, info).await?,
appservice::handle_login(&services, &body, info)?,
| _ => {
return Err!(Request(Unknown(debug_warn!(
?body.login_info,
@@ -97,43 +129,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,
(Some(&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");
@@ -149,7 +177,7 @@ pub(crate) async fn login_route(
.map(HomeserverInfo::new)
.map(DiscoveryInfo::new);
#[allow(deprecated)]
#[expect(deprecated)]
Ok(login::v3::Response {
user_id,
access_token,
+1 -1
View File
@@ -17,7 +17,7 @@ pub(super) async fn handle_login(
body: &Ruma<Request>,
info: &Password,
) -> Result<OwnedUserId> {
#[allow(deprecated)]
#[expect(deprecated)]
let Password { identifier, password, user, .. } = info;
let user_id = if let Some(uiaa::UserIdentifier::UserIdOrLocalpart(user_id)) = identifier {
+693
View File
@@ -0,0 +1,693 @@
use std::{borrow::Cow, net::IpAddr, time::Duration};
use axum::extract::State;
use axum_client_ip::InsecureClientIp;
use axum_extra::extract::cookie::{Cookie, SameSite};
use base64::{Engine as _, engine::general_purpose::URL_SAFE_NO_PAD as b64};
use futures::{FutureExt, StreamExt, TryFutureExt, future::try_join};
use reqwest::header::{CONTENT_TYPE, HeaderValue};
use ruma::{
Mxc, OwnedMxcUri, OwnedRoomId, OwnedUserId, ServerName, UserId,
api::client::session::{sso_callback, sso_login, sso_login_with_provider},
};
use serde::{Deserialize, Serialize};
use tuwunel_core::{
Err, Result, at,
config::IdentityProvider,
debug::INFO_SPAN_LEVEL,
debug_info, debug_warn, err, info,
itertools::Itertools,
utils,
utils::{
OptionExt,
content_disposition::make_content_disposition,
hash::sha256,
result::{FlatOk, LogErr},
string::{EMPTY, truncate_deterministic},
timepoint_from_now, timepoint_has_passed,
},
warn,
};
use tuwunel_service::{
Services,
media::MXC_LENGTH,
oauth::{
CODE_VERIFIER_LENGTH, Provider, SESSION_ID_LENGTH, Session, UserInfo, unique_id_sub,
},
users::Register,
};
use url::Url;
use super::TOKEN_LENGTH;
use crate::Ruma;
/// Grant phase query string.
#[derive(Debug, Serialize)]
struct GrantQuery<'a> {
client_id: &'a str,
state: &'a str,
nonce: &'a str,
scope: &'a str,
response_type: &'a str,
access_type: &'a str,
code_challenge_method: &'a str,
code_challenge: &'a str,
redirect_uri: Option<&'a str>,
}
#[derive(Debug, Deserialize, Serialize)]
struct GrantCookie<'a> {
client_id: Cow<'a, str>,
state: Cow<'a, str>,
nonce: Cow<'a, str>,
redirect_uri: Cow<'a, str>,
}
static GRANT_SESSION_COOKIE: &str = "tuwunel_grant_session";
/// # `GET /_matrix/client/v3/login/sso/redirect`
///
/// A web-based Matrix client should instruct the user’s browser to navigate to
/// this endpoint in order to log in via SSO.
#[tracing::instrument(
name = "sso_login",
level = "debug",
skip_all,
fields(%client),
)]
pub(crate) async fn sso_login_route(
State(services): State<crate::State>,
InsecureClientIp(client): InsecureClientIp,
body: Ruma<sso_login::v3::Request>,
) -> Result<sso_login::v3::Response> {
if services.config.sso_custom_providers_page {
return Err!(Request(NotImplemented(
"sso_custom_providers_page has been enabled but this URL has not been overridden \
with any custom page listing the available providers..."
)));
}
let default_idp_id = services
.config
.identity_provider
.values()
.find(|idp| idp.default)
.or_else(|| services.config.identity_provider.values().next())
.map(IdentityProvider::id)
.map(ToOwned::to_owned)
.unwrap_or_default();
let redirect_url = body.body.redirect_url;
handle_sso_login(&services, &client, default_idp_id, redirect_url, None)
.map_ok(|response| sso_login::v3::Response {
location: response.location,
cookie: response.cookie,
})
.await
}
/// # `GET /_matrix/client/v3/login/sso/redirect/{idpId}`
///
/// This endpoint is the same as /login/sso/redirect, though with an IdP ID from
/// the original identity_providers array to inform the server of which IdP the
/// client/user would like to continue with.
#[tracing::instrument(
name = "sso_login_with_provider",
level = "info",
skip_all,
ret(level = "debug")
fields(
%client,
idp_id = body.body.idp_id,
),
)]
pub(crate) async fn sso_login_with_provider_route(
State(services): State<crate::State>,
InsecureClientIp(client): InsecureClientIp,
body: Ruma<sso_login_with_provider::v3::Request>,
) -> Result<sso_login_with_provider::v3::Response> {
let idp_id = body.body.idp_id;
let redirect_url = body.body.redirect_url;
let login_token = body.body.login_token;
handle_sso_login(&services, &client, idp_id, redirect_url, login_token).await
}
async fn handle_sso_login(
services: &Services,
_client: &IpAddr,
idp_id: String,
redirect_url: String,
login_token: Option<String>,
) -> Result<sso_login_with_provider::v3::Response> {
let Ok(redirect_url) = redirect_url.parse::<Url>() else {
return Err!(Request(InvalidParam("Invalid redirect_url")));
};
let provider = services.oauth.providers.get(&idp_id).await?;
let sess_id = utils::random_string(SESSION_ID_LENGTH);
let query_nonce = utils::random_string(CODE_VERIFIER_LENGTH);
let cookie_nonce = utils::random_string(CODE_VERIFIER_LENGTH);
let code_verifier = utils::random_string(CODE_VERIFIER_LENGTH);
let code_challenge = b64.encode(sha256::hash(code_verifier.as_bytes()));
let callback_uri = provider.callback_url.as_ref().map(Url::as_str);
let scope = provider.scope.iter().join(" ");
let query = GrantQuery {
client_id: &provider.client_id,
state: &sess_id,
nonce: &query_nonce,
access_type: "online",
response_type: "code",
code_challenge_method: "S256",
code_challenge: &code_challenge,
redirect_uri: callback_uri,
scope: scope
.is_empty()
.then_some("openid email profile")
.unwrap_or(scope.as_str()),
};
let location = provider
.authorization_url
.clone()
.map(|mut location| {
let query = serde_html_form::to_string(&query).ok();
location.set_query(query.as_deref());
location
})
.ok_or_else(|| {
err!(Config("authorization_url", "Missing required IdentityProvider config"))
})?;
let cookie_val = GrantCookie {
client_id: query.client_id.into(),
state: query.state.into(),
nonce: cookie_nonce.as_str().into(),
redirect_uri: redirect_url.as_str().into(),
};
let cookie_path = provider
.callback_url
.as_ref()
.map(Url::path)
.unwrap_or("/");
let cookie_max_age = provider
.grant_session_duration
.map(Duration::from_secs)
.expect("Defaulted to Some value during configure_idp()")
.try_into()
.expect("std::time::Duration to time::Duration conversion failure");
let cookie = Cookie::build((GRANT_SESSION_COOKIE, serde_html_form::to_string(&cookie_val)?))
.path(cookie_path)
.max_age(cookie_max_age)
.same_site(SameSite::None)
.secure(true)
.http_only(true)
.build()
.to_string()
.into();
let session = Session {
idp_id: Some(idp_id),
sess_id: Some(sess_id.clone()),
redirect_url: Some(redirect_url),
code_verifier: Some(code_verifier),
query_nonce: Some(query_nonce),
cookie_nonce: Some(cookie_nonce),
authorize_expires_at: provider
.grant_session_duration
.map(Duration::from_secs)
.map(timepoint_from_now)
.transpose()?,
user_id: login_token
.as_deref()
.map_async(|token| services.users.find_from_login_token(token))
.map(FlatOk::flat_ok)
.await,
..Default::default()
};
services.oauth.sessions.put(&session).await;
Ok(sso_login_with_provider::v3::Response {
location: location.into(),
cookie: Some(cookie),
})
}
#[tracing::instrument(
name = "sso_callback"
level = "debug",
skip_all,
fields(
%client,
cookie = ?body.cookie,
body = ?body.body,
),
)]
pub(crate) async fn sso_callback_route(
State(services): State<crate::State>,
InsecureClientIp(client): InsecureClientIp,
body: Ruma<sso_callback::unstable::Request>,
) -> Result<sso_callback::unstable::Response> {
let sess_id = body
.body
.state
.as_deref()
.ok_or_else(|| err!(Request(Forbidden("Missing sess_id in callback."))))?;
let code = body
.body
.code
.as_deref()
.ok_or_else(|| err!(Request(Forbidden("Missing code in callback."))))?;
let session = services
.oauth
.sessions
.get(sess_id)
.map_err(|_| err!(Request(Forbidden("Invalid state in callback"))));
let provider = services
.oauth
.providers
.get(body.body.idp_id.as_str());
let (provider, session) = try_join(provider, session).await.log_err()?;
let client_id = &provider.client_id;
let idp_id = provider.id();
if session.sess_id.as_deref() != Some(sess_id) {
return Err!(Request(Unauthorized("Session ID {sess_id:?} not recognized.")));
}
if session.idp_id.as_deref() != Some(idp_id) {
return Err!(Request(Unauthorized(
"Identity Provider {idp_id:?} session not recognized."
)));
}
if session
.authorize_expires_at
.map(timepoint_has_passed)
.unwrap_or(false)
{
return Err!(Request(Unauthorized("Authorization grant session has expired.")));
}
let cookie = body
.cookie
.get(GRANT_SESSION_COOKIE)
.map(Cookie::value)
.map(serde_html_form::from_str::<GrantCookie<'_>>)
.transpose()?
.ok_or_else(|| err!(Request(Unauthorized("Missing cookie {GRANT_SESSION_COOKIE:?}"))))?;
if cookie.client_id.as_ref() != client_id.as_str() {
return Err!(Request(Unauthorized("Client ID {client_id:?} cookie mismatch.")));
}
if Some(cookie.nonce.as_ref()) != session.cookie_nonce.as_deref() {
return Err!(Request(Unauthorized("Cookie nonce does not match session state.")));
}
if cookie.state.as_ref() != sess_id {
return Err!(Request(Unauthorized("Session ID {sess_id:?} cookie mismatch.")));
}
// Request access token.
let token_response = services
.oauth
.request_token((&provider, &session), code)
.await?;
let token_expires_at = token_response
.expires_in
.map(Duration::from_secs)
.map(timepoint_from_now)
.transpose()?;
let refresh_token_expires_at = token_response
.refresh_token_expires_in
.map(Duration::from_secs)
.map(timepoint_from_now)
.transpose()?;
// Update the session with access token results
let session = Session {
scope: token_response.scope,
token_type: token_response.token_type,
access_token: token_response.access_token,
expires_at: token_expires_at,
refresh_token: token_response.refresh_token,
refresh_token_expires_at,
..session
};
// Request userinfo claims.
let userinfo = services
.oauth
.request_userinfo((&provider, &session))
.await?;
let unique_id = unique_id_sub((&provider, &userinfo.sub))?;
// Check for an existing session from this identity. We want to maintain one
// session for each identity and keep the newer one which has up-to-date state
// and access.
let (old_user_id, old_sess_id) = match services
.oauth
.sessions
.get_by_unique_id(&unique_id)
.await
{
| Ok(session) => (session.user_id, session.sess_id),
| Err(error) if !error.is_not_found() => return Err(error),
| Err(_) => (None, None),
};
// Update the session with userinfo
let session = Session {
user_info: Some(userinfo.clone()),
..session
};
// Keep the user_id from the old session as best as possible.
let user_id = match (session.user_id, old_user_id) {
| (Some(user_id), ..) | (None, Some(user_id)) => user_id,
| (None, None) => decide_user_id(&services, &provider, &userinfo, &unique_id).await?,
};
// Update the session with user_id
let session = Session {
user_id: Some(user_id.clone()),
..session
};
// Attempt to register a non-existing user.
if !services.users.exists(&user_id).await {
register_user(&services, &provider, &session, &userinfo, &user_id).await?;
}
// Commit the updated session.
services.oauth.sessions.put(&session).await;
// Delete any old session.
if let Some(old_sess_id) = old_sess_id
&& sess_id != old_sess_id
{
services.oauth.sessions.delete(&old_sess_id).await;
}
if !services.users.is_active_local(&user_id).await {
return Err!(Request(UserDeactivated("This user has been deactivated.")));
}
let cookie = Cookie::build((GRANT_SESSION_COOKIE, EMPTY))
.removal()
.build()
.to_string()
.into();
// Determine the next provider to chain after this one.
let next_idp_url = services
.config
.identity_provider
.values()
.filter(|idp| idp.default || services.config.single_sso)
.skip_while(|idp| idp.id() != idp_id)
.nth(1)
.map(IdentityProvider::id)
.and_then(|next_idp| {
provider.callback_url.clone().map(|mut url| {
let path = format!("/_matrix/client/v3/login/sso/redirect/{next_idp}");
url.set_path(&path);
if let Some(redirect_url) = session.redirect_url.as_ref() {
url.query_pairs_mut()
.append_pair("redirectUrl", redirect_url.as_str());
}
url
})
});
// Allow the user to login to Matrix.
let login_token = utils::random_string(TOKEN_LENGTH);
let _login_token_expires_in = services
.users
.create_login_token(&user_id, &login_token);
let location = next_idp_url
.or(session.redirect_url)
.as_ref()
.ok_or_else(|| err!(Request(InvalidParam("Missing redirect URL in session data"))))?
.clone()
.query_pairs_mut()
.append_pair("loginToken", &login_token)
.finish()
.to_string();
Ok(sso_callback::unstable::Response { location, cookie: Some(cookie) })
}
#[tracing::instrument(
name = "register",
level = INFO_SPAN_LEVEL,
skip_all,
fields(user_id, userinfo)
)]
async fn register_user(
services: &Services,
provider: &Provider,
session: &Session,
userinfo: &UserInfo,
user_id: &UserId,
) -> Result {
debug_info!(%user_id, "Creating new user account...");
services
.users
.full_register(Register {
user_id: Some(user_id),
password: Some("*"),
origin: Some("sso"),
displayname: userinfo.name.as_deref(),
..Default::default()
})
.await?;
if let Some(avatar_url) = userinfo
.avatar_url
.as_deref()
.or(userinfo.picture.as_deref())
{
set_avatar(services, provider, session, userinfo, user_id, avatar_url)
.await
.ok();
}
let idp_id = provider.id();
let idp_name = provider
.name
.as_deref()
.unwrap_or(provider.brand.as_str());
// log in conduit admin channel if a non-guest user registered
let notice =
format!("New user \"{user_id}\" registered on this server via {idp_name} ({idp_id})",);
info!("{notice}");
if services.server.config.admin_room_notices {
services.admin.notice(&notice).await;
}
Ok(())
}
#[tracing::instrument(level = "debug", skip_all, fields(user_id, avatar_url))]
async fn set_avatar(
services: &Services,
_provider: &Provider,
_session: &Session,
_userinfo: &UserInfo,
user_id: &UserId,
avatar_url: &str,
) -> Result {
use reqwest::Response;
let response = services
.client
.default
.get(avatar_url)
.send()
.await
.and_then(Response::error_for_status)?;
let content_type = response
.headers()
.get(CONTENT_TYPE)
.map(HeaderValue::to_str)
.flat_ok()
.map(ToOwned::to_owned);
let mxc = Mxc {
server_name: services.globals.server_name(),
media_id: &utils::random_string(MXC_LENGTH),
};
let content_disposition = make_content_disposition(None, content_type.as_deref(), None);
let bytes = response.bytes().await?;
services
.media
.create(&mxc, Some(user_id), Some(&content_disposition), content_type.as_deref(), &bytes)
.await?;
let all_joined_rooms: Vec<OwnedRoomId> = services
.state_cache
.rooms_joined(user_id)
.map(ToOwned::to_owned)
.collect()
.await;
let mxc_uri: OwnedMxcUri = mxc.to_string().into();
services
.users
.update_avatar_url(user_id, Some(&mxc_uri), None, &all_joined_rooms)
.await;
Ok(())
}
#[tracing::instrument(
level = "debug",
ret(level = "debug")
skip_all,
fields(user),
)]
async fn decide_user_id(
services: &Services,
provider: &Provider,
userinfo: &UserInfo,
unique_id: &str,
) -> Result<OwnedUserId> {
if let Some(user_id) = services
.oauth
.sessions
.find_user_association_pending(provider.id(), userinfo)
{
debug_info!(
provider = ?provider.id(),
?user_id,
?userinfo,
"Matched pending association"
);
return Ok(user_id);
}
let allowed =
|claim: &str| provider.userid_claims.is_empty() || provider.userid_claims.contains(claim);
let choices = [
userinfo
.preferred_username
.as_deref()
.map(str::to_lowercase)
.filter(|_| allowed("preferred_username")),
userinfo
.nickname
.as_deref()
.map(str::to_lowercase)
.filter(|_| allowed("nickname")),
provider
.brand
.eq(&"github")
.then_some(userinfo.sub.as_str())
.map(str::to_lowercase)
.filter(|_| allowed("login")),
userinfo
.email
.as_deref()
.and_then(|email| email.split_once('@'))
.map(at!(0))
.map(str::to_lowercase)
.filter(|_| allowed("email")),
];
for choice in choices.into_iter().flatten() {
if let Some(user_id) = try_user_id(services, &choice, false).await {
return Ok(user_id);
}
}
let length = Some(15..23);
let unique_id = truncate_deterministic(unique_id, length).to_lowercase();
if let Some(user_id) = try_user_id(services, &unique_id, true).await {
return Ok(user_id);
}
Err!(Request(UserInUse("User ID is not available.")))
}
#[tracing::instrument(level = "debug", skip_all, fields(username))]
async fn try_user_id(
services: &Services,
username: &str,
may_exist: bool,
) -> Option<OwnedUserId> {
let server_name = services.globals.server_name();
let user_id = parse_user_id(server_name, username)
.inspect_err(|e| warn!(?username, "Username invalid: {e}"))
.ok()?;
if services
.config
.forbidden_usernames
.is_match(username)
{
warn!(?username, "Username forbidden.");
return None;
}
if services.users.exists(&user_id).await {
debug_warn!(?username, "Username exists.");
if services
.users
.origin(&user_id)
.await
.ok()
.is_none_or(|origin| origin != "sso")
{
debug_warn!(?username, "Username has non-sso origin.");
return None;
}
if !may_exist {
return None;
}
}
Some(user_id)
}
fn parse_user_id(server_name: &ServerName, username: &str) -> Result<OwnedUserId> {
match UserId::parse_with_server_name(username, server_name) {
| Err(e) => {
Err!(Request(InvalidUsername(debug_error!("Username {username} is not valid: {e}"))))
},
| Ok(user_id) => match user_id.validate_strict() {
| Ok(()) => Ok(user_id),
| Err(e) => Err!(Request(InvalidUsername(debug_error!(
"Username {username} contains disallowed characters or spaces: {e}"
)))),
},
}
}
+11 -46
View File
@@ -4,19 +4,16 @@ use axum::extract::State;
use axum_client_ip::InsecureClientIp;
use ruma::{
OwnedUserId,
api::client::{
session::{
get_login_token,
login::v3::{Request, Token},
},
uiaa,
api::client::session::{
get_login_token,
login::v3::{Request, Token},
},
};
use tuwunel_core::{Err, Result, utils::random_string};
use tuwunel_service::{Services, uiaa::SESSION_ID_LENGTH};
use tuwunel_service::Services;
use super::TOKEN_LENGTH;
use crate::Ruma;
use crate::{Ruma, router::auth_uiaa};
pub(super) async fn handle_login(
services: &Services,
@@ -25,7 +22,7 @@ pub(super) async fn handle_login(
) -> Result<OwnedUserId> {
let Token { token } = info;
if !services.config.login_via_existing_session {
if !services.config.login_via_token {
return Err!(Request(Unknown("Token login is not enabled.")));
}
@@ -44,51 +41,19 @@ pub(crate) async fn login_token_route(
InsecureClientIp(client): InsecureClientIp,
body: Ruma<get_login_token::v1::Request>,
) -> Result<get_login_token::v1::Response> {
if !services.config.login_via_existing_session {
if !services.config.login_via_existing_session || !services.config.login_via_token {
return Err!(Request(Forbidden("Login via an existing session is not enabled")));
}
// 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 password_flow = uiaa::AuthFlow { stages: vec![uiaa::AuthType::Password] };
let mut uiaainfo = uiaa::UiaaInfo {
flows: vec![password_flow],
..Default::default()
};
match &body.auth {
| Some(auth) => {
let (worked, uiaainfo) = services
.uiaa
.try_auth(sender_user, sender_device, auth, &uiaainfo)
.await?;
if !worked {
return Err!(Uiaa(uiaainfo));
}
// Success!
},
| _ => match body.json_body.as_ref() {
| Some(json) => {
uiaainfo.session = Some(random_string(SESSION_ID_LENGTH));
services
.uiaa
.create(sender_user, sender_device, &uiaainfo, json);
return Err!(Uiaa(uiaainfo));
},
| _ => return Err!(Request(NotJson("No JSON body was sent when required."))),
},
let sender_user = auth_uiaa(&services, &body).await?;
if !services.users.is_active_local(&sender_user).await {
return Err!(Request(UserDeactivated("This user has been deactivated.")));
}
let login_token = random_string(TOKEN_LENGTH);
let expires_in = services
.users
.create_login_token(sender_user, &login_token);
.create_login_token(&sender_user, &login_token);
Ok(get_login_token::v1::Response {
expires_in: Duration::from_millis(expires_in),
Loaded 100 of 342 files, more files were not shown because too many files have changed in this diff. Show more