Compare commits

...
167 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
265 changed files with 10383 additions and 4237 deletions

No files matched your search

+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
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,6 +110,7 @@ jobs:
machines: ${{inputs.machines}}
excludes: ${{inputs.excludes}}
includes: ${{inputs.includes}}
checkout: ${{inputs.checkout}}
lychee:
if: >
@@ -128,6 +134,7 @@ jobs:
machines: ${{inputs.machines}}
excludes: ${{inputs.excludes}}
includes: ${{inputs.includes}}
checkout: ${{inputs.checkout}}
check:
if: >
@@ -151,6 +158,7 @@ jobs:
machines: ${{inputs.machines}}
excludes: ${{inputs.excludes}}
includes: ${{inputs.includes}}
checkout: ${{inputs.checkout}}
clippy:
if: >
@@ -171,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 || github.ref == 'refs/heads/test'}}
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
+64 -28
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,8 +425,8 @@ 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:
Generated
+268 -219
View File
File diff suppressed because it is too large. Load diff
+18 -10
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.8"
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",
]
@@ -312,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]
@@ -324,7 +327,7 @@ default-features = false
[workspace.dependencies.ruma]
git = "https://github.com/matrix-construct/ruma"
rev = "214ab27fdc3d7004f6d46e0aa89fba573b59c66f"
rev = "069b3f6ee7499cdac84175946aeb9443d2578363"
features = [
"__compat",
"appservice-api-c",
@@ -339,6 +342,7 @@ features = [
"ring-compat",
"unstable-msc2448",
"unstable-msc2666",
"unstable-msc2815",
"unstable-msc2867",
"unstable-msc2870",
"unstable-msc3026",
@@ -418,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"
@@ -524,7 +532,7 @@ features = [
]
[workspace.dependencies.tracing]
version = "0.1"
version = "=0.1.43"
default-features = false
[workspace.dependencies.tracing-core]
@@ -812,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"
@@ -822,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"
@@ -834,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
@@ -910,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 }
@@ -922,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 }
@@ -930,7 +939,6 @@ perf = { level = "warn", priority = -1 }
###################
#restriction = "warn"
#allow_attributes = "warn" # UNSTABLE
arithmetic_side_effects = "warn"
as_conversions = "warn"
as_underscore = "warn"
@@ -981,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 }
+4 -2
View File
@@ -15,8 +15,9 @@
<!-- ANCHOR: body -->
[![Documentation](https://img.shields.io/badge/documentation%2D_?color=%238A2BE2&style=for-the-badge&logo=mdBook&logoColor=white)](https://matrix-construct.github.io/tuwunel/)
[![Support Chat](https://img.shields.io/matrix/tuwunel%3Amatrix.org.svg?color=%238A2BE2&style=for-the-badge&label=Support%20Chat)](https://matrix.to/#/#tuwunel:grin.hu)
[![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/),
@@ -40,6 +41,7 @@ Switzerland 🇨🇭 where it is currently deployed for citizens.
- 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 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
+83 -5
View File
@@ -1,9 +1,87 @@
# Tuwunel 1.4.8
# Tuwunel 1.5.0
December 21, 2025
January 31, 2025
All federating deployments must upgrade to this patch for mitigations to severe vulnerabilities in Matrix protocol implementation logic. This is an off-schedule coordinated security release. Full release notes will be included with the next scheduled release.
### New Features & Enhancements
### Security Fixes
- 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.
- Requests to the [Federation Invite API](https://spec.matrix.org/v1.17/server-server-api/#put_matrixfederationv2inviteroomideventid) lacked sufficient validation on all input fields. An attacker can use this route to process other kinds of events: upon acceptance, they are signed by the victim's server as specified by the Matrix protocol. The attacker can therefore forge events on behalf of the victim's authority to gain control of a room. This vulnerability was present in all versions and derivatives of Conduit.
- [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.
- 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.
- 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.
- [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).
- @VlaDexa implemented reading the `client_secret` configuration for an SSO Identity Provider from a separate file; a recommended secure practice (#256).
- 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).
- Thank you @Xerusion for documenting Traefik for deploying Tuwunel in (#259). This will save a lot of time and headache for many new users!
- 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).
- 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
- 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.
- 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).
- @OptimoSupreme fixed issues with unread notification counting, including eliminating one of the last remaining non-async database calls in the codebase in (#253).
- @x86pup fixed linker issues for platforms without static builds of `io_uring`. Thanks @darix for reporting in (#238).
- @x86pup fixed compatibility for our optimized jemalloc build on macOS (#239).
- @dasha-uwu made Livekit operate properly even when federation is disabled (b5f50c3fda3). Thank you @apodavalov for reporting in (#240).
- Thank you @VlaDexa for updating the `Cache-Control` header to cache media as `private` which is more appropriate now in the Authenticated Media era.
- Appservices now receive events properly matching on the sender MXID's localpart thanks to @dasha-uwu (c5508bba58d0).
- Additional PDU format and compliance checks were added by @dasha-uwu (7b2079f71499).
- 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.
+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}"
+4 -2
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,7 +123,8 @@ 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
+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
+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
+10 -1
View File
@@ -450,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 = [
@@ -469,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
@@ -522,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=*"
+1
View File
@@ -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)
+1
View File
@@ -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)
+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).
+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
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 = []
+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,
+35 -9
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;
@@ -242,10 +248,11 @@ pub(super) async fn get_remote_pdu(
})
.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| {
@@ -383,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}");
@@ -406,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}");
},
}
}
@@ -1013,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, "%+");
-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,
}
+54 -11
View File
@@ -1,6 +1,6 @@
use std::collections::BTreeMap;
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,13 +11,14 @@ use ruma::{
},
tag::{TagEvent, TagEventContent, TagInfo},
},
uint,
};
use tuwunel_core::{
Err, Result, debug_warn, info,
matrix::{Event, pdu::PduBuilder},
utils::{self, ReadyExt},
utils::{self, ReadyExt, stream::IterStream},
};
use tuwunel_service::Services;
use tuwunel_service::{Services, users::Register};
use crate::{
admin_command, get_room_info,
@@ -49,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 {
@@ -63,7 +64,12 @@ pub(super) async fn create_user(&self, username: String, password: Option<String
self.services
.users
.full_register(&user_id, Some(&password), None, None, false, true)
.full_register(Register {
user_id: Some(&user_id),
password: Some(&password),
grant_first_user_admin: true,
..Default::default()
})
.await?;
self.write_str(&format!("Created user with user_id: {user_id} and password: `{password}`"))
@@ -125,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
}
@@ -879,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
}
+6
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,
+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
+4 -4
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},
},
};
@@ -116,7 +117,7 @@ pub(crate) async fn get_context_route(
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()
+34 -43
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},
},
};
@@ -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.")));
}
+4 -4
View File
@@ -484,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);
}
}
+1 -1
View File
@@ -1,4 +1,4 @@
#![allow(deprecated)]
#![expect(deprecated)]
use axum::extract::State;
use axum_client_ip::InsecureClientIp;
+2 -4
View File
@@ -49,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 {
+14 -22
View File
@@ -1,9 +1,5 @@
use axum::extract::State;
use futures::{
FutureExt, StreamExt, TryFutureExt,
future::{Either, OptionFuture},
pin_mut,
};
use futures::{FutureExt, StreamExt, TryFutureExt, future::Either, pin_mut};
use ruma::{
RoomId, UserId,
api::{
@@ -21,7 +17,7 @@ use tuwunel_core::{
},
ref_at,
utils::{
IterStream, ReadyExt,
BoolExt, IterStream, ReadyExt,
result::{FlatOk, LogErr},
stream::{BroadbandExt, TryIgnore, WidebandExt},
},
@@ -140,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)
@@ -262,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]
+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 {
+16 -16
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
@@ -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;
@@ -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,
@@ -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
+5 -5
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()
+22 -20
View File
@@ -2,19 +2,18 @@ use std::fmt::Write;
use axum::extract::State;
use axum_client_ip::InsecureClientIp;
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},
},
};
use tuwunel_core::{Err, Error, Result, debug_info, debug_warn, info, utils};
use tuwunel_service::users::device::generate_refresh_token;
use tuwunel_service::users::{Register, device::generate_refresh_token};
use super::SESSION_ID_LENGTH;
use crate::Ruma;
@@ -94,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
@@ -128,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>,
@@ -264,13 +263,7 @@ pub(crate) async fn register_route(
// UIAA
let mut uiaainfo;
let skip_auth = if !services
.globals
.get_registration_tokens()
.await
.is_empty()
&& !is_guest
{
let skip_auth = if services.registration_tokens.is_enabled().await && !is_guest {
// Registration token required
uiaainfo = UiaaInfo {
flows: vec![AuthFlow {
@@ -337,7 +330,14 @@ pub(crate) async fn register_route(
services
.users
.full_register(&user_id, password, None, body.appservice_info.as_ref(), is_guest, true)
.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)
@@ -419,13 +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 tokens = services.globals.get_registration_tokens().await;
if tokens.is_empty() {
if !services.registration_tokens.is_enabled().await {
return Err!(Request(Forbidden("Server does not allow token registration")));
}
let valid = tokens.contains(&body.token);
let valid = services
.registration_tokens
.is_token_valid(&body.token)
.await
.is_ok();
Ok(check_registration_token_validity::v1::Response { valid })
}
+1 -1
View File
@@ -108,7 +108,7 @@ 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",
+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.")));
}
+8 -14
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;
@@ -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,
+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
.full_register(lowercased_user_id, Some("*"), Some("ldap"), None, false, false)
.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)
+42 -10
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::{
@@ -28,6 +29,7 @@ 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::TOKEN_LENGTH;
@@ -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,
@@ -145,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 -47
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,52 +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 = body.sender_user();
let sender_device = body.sender_device()?;
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),
+26 -30
View File
@@ -1,13 +1,13 @@
use std::{collections::BTreeSet, iter::once, str::FromStr};
use axum::extract::State;
use futures::{FutureExt, StreamExt, TryFutureExt, future::OptionFuture, stream::FuturesOrdered};
use futures::{FutureExt, StreamExt, TryFutureExt, stream::FuturesOrdered};
use ruma::{
OwnedRoomId, OwnedServerName, RoomId, UInt, UserId, api::client::space::get_hierarchy,
};
use tuwunel_core::{
Err, Result, debug_error,
utils::{future::TryExtExt, stream::IterStream},
utils::{future::TryExtExt, option::OptionExt, stream::IterStream},
};
use tuwunel_service::{
Services,
@@ -45,12 +45,12 @@ pub(crate) async fn get_hierarchy_route(
.and_then(|s| PaginationToken::from_str(s).ok());
// Should prevent unexpected behaviour in (bad) clients
if let Some(ref token) = key {
if token.suggested_only != body.suggested_only || token.max_depth != max_depth {
return Err!(Request(InvalidParam(
"suggested_only and max_depth cannot change on paginated requests"
)));
}
if let Some(ref token) = key
&& (token.suggested_only != body.suggested_only || token.max_depth != max_depth)
{
return Err!(Request(InvalidParam(
"suggested_only and max_depth cannot change on paginated requests"
)));
}
get_client_hierarchy(
@@ -187,30 +187,26 @@ where
}
}
let next_batch: OptionFuture<_> = queue
.next()
.await
.map(async |(room, ..)| {
parents.insert(room);
let next_batch = queue.next().await.map_async(async |(room, ..)| {
parents.insert(room);
let next_short_room_ids: Vec<_> = parents
.iter()
.stream()
.filter_map(|room_id| services.short.get_shortroomid(room_id).ok())
.collect()
.await;
let next_short_room_ids: Vec<_> = parents
.iter()
.stream()
.filter_map(|room_id| services.short.get_shortroomid(room_id).ok())
.collect()
.await;
(next_short_room_ids.iter().ne(short_room_ids) && !next_short_room_ids.is_empty())
.then_some(PaginationToken {
short_room_ids: next_short_room_ids,
limit: limit.try_into().ok()?,
max_depth: max_depth.try_into().ok()?,
suggested_only,
})
.as_ref()
.map(PaginationToken::to_string)
})
.into();
(next_short_room_ids.iter().ne(short_room_ids) && !next_short_room_ids.is_empty())
.then_some(PaginationToken {
short_room_ids: next_short_room_ids,
limit: limit.try_into().ok()?,
max_depth: max_depth.try_into().ok()?,
suggested_only,
})
.as_ref()
.map(PaginationToken::to_string)
});
Ok(get_hierarchy::v1::Response {
next_batch: next_batch.await.flatten(),
+17 -16
View File
@@ -276,21 +276,21 @@ async fn allowed_to_send_state_event(
},
| StateEventType::RoomJoinRules => {
// admin room is a sensitive room, it should not ever be made public
if let Ok(admin_room_id) = services.admin.get_admin_room().await {
if admin_room_id == room_id {
match json.deserialize_as_unchecked::<RoomJoinRulesEventContent>() {
| Ok(join_rule) =>
if join_rule.join_rule == JoinRule::Public {
return Err!(Request(Forbidden(
"Admin room is a sensitive room, it cannot be made public"
)));
},
| Err(e) => {
return Err!(Request(BadJson(debug_warn!(
"Room join rules event is invalid: {e}"
))));
if let Ok(admin_room_id) = services.admin.get_admin_room().await
&& admin_room_id == room_id
{
match json.deserialize_as_unchecked::<RoomJoinRulesEventContent>() {
| Ok(join_rule) =>
if join_rule.join_rule == JoinRule::Public {
return Err!(Request(Forbidden(
"Admin room is a sensitive room, it cannot be made public"
)));
},
}
| Err(e) => {
return Err!(Request(BadJson(debug_warn!(
"Room join rules event is invalid: {e}"
))));
},
}
}
},
@@ -349,7 +349,7 @@ async fn allowed_to_send_state_event(
},
}
},
| StateEventType::RoomMember =>
| StateEventType::RoomMember => {
match json.deserialize_as_unchecked::<RoomMemberEventContent>() {
| Ok(membership_content) => {
let Ok(_state_key) = UserId::parse(state_key) else {
@@ -394,7 +394,8 @@ async fn allowed_to_send_state_event(
membership state: {e}"
)));
},
},
}
},
| _ => (),
}
+166 -195
View File
@@ -6,11 +6,11 @@ use std::{
use axum::extract::State;
use futures::{
FutureExt, StreamExt, TryFutureExt,
future::{OptionFuture, join, join3, join4, join5},
future::{join, join3, join4, join5},
pin_mut,
};
use ruma::{
DeviceId, EventId, OwnedRoomId, OwnedUserId, RoomId, UserId,
DeviceId, EventId, OwnedRoomId, OwnedUserId, RoomId, UInt, UserId,
api::client::{
filter::FilterDefinition,
sync::sync_events::{
@@ -37,7 +37,7 @@ use tokio::time;
use tuwunel_core::{
Result, at,
debug::INFO_SPAN_LEVEL,
err,
debug_error, err,
error::{inspect_debug_log, inspect_log},
extract_variant, is_equal_to, is_false, is_true,
matrix::{
@@ -52,6 +52,7 @@ use tuwunel_core::{
self, BoolExt, FutureBoolExt, IterStream, ReadyExt, TryFutureExtExt,
future::{OptionStream, ReadyBoolExt},
math::ruma_from_u64,
option::OptionExt,
result::MapExpect,
stream::{BroadbandExt, Tools, TryExpect, WidebandExt},
string::to_small_string,
@@ -130,19 +131,18 @@ pub(crate) async fn sync_events_route(
let sender_user = body.sender_user();
let sender_device = body.sender_device.as_deref();
let filter: OptionFuture<_> = body
let filter = body
.body
.filter
.as_ref()
.map(async |filter| match filter {
.map_async(async |filter| match filter {
| Filter::FilterDefinition(filter) => filter.clone(),
| Filter::FilterId(filter_id) => services
.users
.get_filter(sender_user, filter_id)
.await
.unwrap_or_default(),
})
.into();
});
let filter = filter.map(Option::unwrap_or_default);
let full_state = body.body.full_state;
@@ -192,7 +192,10 @@ pub(crate) async fn sync_events_route(
.watch(sender_user, sender_device, watch_rooms);
let next_batch = services.globals.wait_pending().await?;
debug_assert!(since <= next_batch, "next_batch is monotonic");
if since > next_batch {
debug_error!(since, next_batch, "received since > next_batch, clamping");
since = next_batch;
}
if since < next_batch || full_state {
let response = build_sync_events(
@@ -221,6 +224,7 @@ pub(crate) async fn sync_events_route(
if time::timeout_at(stop_at, watchers).await.is_err() || services.server.is_stopping() {
let response =
build_empty_response(&services, sender_user, sender_device, next_batch).await;
trace!(since, next_batch, "empty response");
return Ok(response);
}
@@ -243,16 +247,13 @@ async fn build_empty_response(
sender_device: Option<&DeviceId>,
next_batch: u64,
) -> sync_events::v3::Response {
let device_one_time_keys_count: OptionFuture<_> = sender_device
.map(|sender_device| {
services
.users
.count_one_time_keys(sender_user, sender_device)
})
.into();
sync_events::v3::Response {
device_one_time_keys_count: device_one_time_keys_count
device_one_time_keys_count: sender_device
.map_async(|sender_device| {
services
.users
.count_one_time_keys(sender_user, sender_device)
})
.await
.unwrap_or_default(),
@@ -380,11 +381,12 @@ async fn build_sync_events(
knocked_rooms
});
let presence_updates: OptionFuture<_> = services
let presence_updates = services
.config
.allow_local_presence
.then(|| process_presence_updates(services, since, next_batch, sender_user, filter))
.into();
.then_async(|| {
process_presence_updates(services, since, next_batch, sender_user, filter)
});
let account_data = services
.account_data
@@ -399,32 +401,26 @@ async fn build_sync_events(
.map(ToOwned::to_owned)
.collect::<HashSet<_>>();
let to_device_events: OptionFuture<_> = sender_device
.map(|sender_device| {
services
.users
.get_to_device_events(sender_user, sender_device, Some(since), Some(next_batch))
.map(at!(1))
.collect::<Vec<_>>()
})
.into();
let to_device_events = sender_device.map_async(|sender_device| {
services
.users
.get_to_device_events(sender_user, sender_device, Some(since), Some(next_batch))
.map(at!(1))
.collect::<Vec<_>>()
});
let device_one_time_keys_count: OptionFuture<_> = sender_device
.map(|sender_device| {
services
.users
.count_one_time_keys(sender_user, sender_device)
})
.into();
let device_one_time_keys_count = sender_device.map_async(|sender_device| {
services
.users
.count_one_time_keys(sender_user, sender_device)
});
// Remove all to-device events the device received *last time*
let remove_to_device_events: OptionFuture<_> = sender_device
.map(|sender_device| {
services
.users
.remove_to_device_events(sender_user, sender_device, since)
})
.into();
let remove_to_device_events = sender_device.map_async(|sender_device| {
services
.users
.remove_to_device_events(sender_user, sender_device, since)
});
let (
account_data,
@@ -533,7 +529,6 @@ async fn process_presence_updates(
full = %full_state,
),
)]
#[allow(clippy::too_many_arguments)]
async fn handle_left_room(
services: &Services,
since: u64,
@@ -643,17 +638,16 @@ async fn load_left_room(
.prev_shortstatehash(room_id, PduCount::Normal(since).saturating_add(1))
.ok();
let horizon_shortstatehash: OptionFuture<_> = timeline_pdus
let horizon_shortstatehash = timeline_pdus
.first()
.map(at!(0))
.map(|count| {
.map_async(|count| {
services
.timeline
.get_shortstatehash(room_id, count)
.inspect_err(inspect_debug_log)
.ok()
})
.into();
});
let left_shortstatehash = services
.timeline
@@ -753,7 +747,7 @@ async fn load_left_room(
room_id = ?room_id,
),
)]
#[allow(clippy::too_many_arguments)]
#[expect(clippy::too_many_arguments)]
async fn load_joined_room(
services: &Services,
sender_user: &UserId,
@@ -790,40 +784,34 @@ async fn load_joined_room(
"if timeline events, last_timeline_count must be in the since window."
);
let since_shortstatehash: OptionFuture<_> = timeline_changed
.then(|| {
services
.timeline
.prev_shortstatehash(room_id, PduCount::Normal(since).saturating_add(1))
.ok()
})
.into();
let since_shortstatehash = timeline_changed.then_async(|| {
services
.timeline
.prev_shortstatehash(room_id, PduCount::Normal(since).saturating_add(1))
.ok()
});
let horizon_shortstatehash: OptionFuture<_> = timeline_pdus
let horizon_shortstatehash = timeline_pdus
.first()
.map(at!(0))
.map(|count| {
.map_async(|count| {
services
.timeline
.get_shortstatehash(room_id, count)
.inspect_err(inspect_debug_log)
})
.into();
});
let current_shortstatehash: OptionFuture<_> = timeline_changed
.then(|| {
services
.timeline
.get_shortstatehash(room_id, last_timeline_count)
.inspect_err(inspect_debug_log)
.or_else(|_| services.state.get_room_shortstatehash(room_id))
.map_err(|_| err!(Database(error!("Room {room_id} has no state"))))
})
.into();
let current_shortstatehash = timeline_changed.then_async(|| {
services
.timeline
.get_shortstatehash(room_id, last_timeline_count)
.inspect_err(inspect_debug_log)
.or_else(|_| services.state.get_room_shortstatehash(room_id))
.map_err(|_| err!(Database(error!("Room {room_id} has no state"))))
});
let encrypted_room: OptionFuture<_> = timeline_changed
.then(|| services.state_accessor.is_encrypted_room(room_id))
.into();
let encrypted_room =
timeline_changed.then_async(|| services.state_accessor.is_encrypted_room(room_id));
let receipt_events = services
.read_receipt
@@ -873,53 +861,43 @@ async fn load_joined_room(
};
// Reset lazy loading because this is an initial sync
let lazy_load_reset: OptionFuture<_> = initial
.then(|| services.lazy_loading.reset(lazy_loading_context))
.into();
let lazy_load_reset =
initial.then_async(|| services.lazy_loading.reset(lazy_loading_context));
lazy_load_reset.await;
let witness: OptionFuture<_> = lazy_loading_enabled
.then(|| {
let witness: Witness = timeline_pdus
.iter()
.map(ref_at!(1))
.map(Event::sender)
.map(Into::into)
.chain(receipt_events.keys().map(Into::into))
.collect();
let witness = lazy_loading_enabled.then_async(|| {
let witness: Witness = timeline_pdus
.iter()
.map(ref_at!(1))
.map(Event::sender)
.map(Into::into)
.chain(receipt_events.keys().map(Into::into))
.collect();
services
.lazy_loading
.witness_retain(witness, lazy_loading_context)
})
.into();
services
.lazy_loading
.witness_retain(witness, lazy_loading_context)
});
let sender_joined_count: OptionFuture<_> = timeline_changed
.then(|| {
services
.state_cache
.get_joined_count(room_id, sender_user)
.unwrap_or(0)
})
.into();
let sender_joined_count = timeline_changed.then_async(|| {
services
.state_cache
.get_joined_count(room_id, sender_user)
.unwrap_or(0)
});
let since_encryption: OptionFuture<_> = since_shortstatehash
.map(|shortstatehash| {
services
.state_accessor
.state_get(shortstatehash, &StateEventType::RoomEncryption, "")
})
.into();
let since_encryption = since_shortstatehash.map_async(|shortstatehash| {
services
.state_accessor
.state_get(shortstatehash, &StateEventType::RoomEncryption, "")
});
let last_notification_read: OptionFuture<_> = timeline_pdus
.is_empty()
.then(|| {
services
.pusher
.last_notification_read(sender_user, room_id)
.ok()
})
.into();
let last_notification_read = timeline_pdus.is_empty().then_async(|| {
services
.pusher
.last_notification_read(sender_user, room_id)
.ok()
});
let last_privateread_update = services
.read_receipt
@@ -941,21 +919,19 @@ async fn load_joined_room(
let joined_since_last_sync = sender_joined_count.unwrap_or(0) > since;
let state_changes: OptionFuture<_> = current_shortstatehash
.map(|current_shortstatehash| {
calculate_state_changes(
services,
sender_user,
room_id,
full_state || initial,
since_shortstatehash,
horizon_shortstatehash,
current_shortstatehash,
joined_since_last_sync,
witness.as_ref(),
)
})
.into();
let state_changes = current_shortstatehash.map_async(|current_shortstatehash| {
calculate_state_changes(
services,
sender_user,
room_id,
full_state || initial,
since_shortstatehash,
horizon_shortstatehash,
current_shortstatehash,
joined_since_last_sync,
witness.as_ref(),
)
});
let StateChanges {
heroes,
@@ -993,37 +969,37 @@ async fn load_joined_room(
let send_notification_counts = last_notification_read
.flatten()
.is_none_or(|last_count| last_count.gt(&since));
.is_none_or(|last_count| last_count > since && last_count <= next_batch);
let notification_count: OptionFuture<_> = send_notification_counts
.then(|| {
services
.pusher
.notification_count(sender_user, room_id)
.map(TryInto::try_into)
.unwrap_or(uint!(0))
})
.into();
let send_notification_resets = last_notification_read
.flatten()
.is_some_and(|last_count| last_count > since);
let highlight_count: OptionFuture<_> = send_notification_counts
.then(|| {
services
.pusher
.highlight_count(sender_user, room_id)
.map(TryInto::try_into)
.unwrap_or(uint!(0))
})
.into();
let send_notification_count_filter =
|count: &UInt| *count != uint!(0) || send_notification_resets;
let private_read_event: OptionFuture<_> = last_privateread_update
.gt(&since)
.then(|| {
services
.read_receipt
.private_read_get(room_id, sender_user)
.map(Result::ok)
})
.into();
let notification_count = send_notification_counts.then_async(|| {
services
.pusher
.notification_count(sender_user, room_id)
.map(TryInto::try_into)
.unwrap_or(uint!(0))
});
let highlight_count = send_notification_counts.then_async(|| {
services
.pusher
.highlight_count(sender_user, room_id)
.map(TryInto::try_into)
.unwrap_or(uint!(0))
});
let private_read_event = last_privateread_update.gt(&since).then_async(|| {
services
.read_receipt
.private_read_get(room_id, sender_user)
.map(Result::ok)
});
let typing_events = services
.typing
@@ -1169,7 +1145,10 @@ async fn load_joined_room(
.map(Event::into_format)
.collect(),
},
unread_notifications: UnreadNotificationsCount { highlight_count, notification_count },
unread_notifications: UnreadNotificationsCount {
highlight_count: highlight_count.filter(send_notification_count_filter),
notification_count: notification_count.filter(send_notification_count_filter),
},
unread_thread_notifications: BTreeMap::new(),
};
@@ -1187,7 +1166,7 @@ async fn load_joined_room(
cs = %current_shortstatehash,
)
)]
#[allow(clippy::too_many_arguments)]
#[expect(clippy::too_many_arguments)]
async fn calculate_state_changes<'a>(
services: &Services,
sender_user: &UserId,
@@ -1216,37 +1195,31 @@ async fn calculate_state_changes<'a>(
.ok()
};
let lazy_state_ids: OptionFuture<_> = witness
.map(|witness| {
witness
.iter()
.stream()
.ready_filter(|&user_id| user_id != sender_user)
.broad_filter_map(|user_id| state_get_shorteventid(user_id))
.into_future()
})
.into();
let lazy_state_ids = witness.map_async(|witness| {
witness
.iter()
.stream()
.ready_filter(|&user_id| user_id != sender_user)
.broad_filter_map(|user_id| state_get_shorteventid(user_id))
.into_future()
});
let state_diff_ids: OptionFuture<_> = incremental
.then(|| {
services
.state_accessor
.state_added((since_shortstatehash, horizon_shortstatehash))
.boxed()
.into_future()
})
.into();
let state_diff_ids = incremental.then_async(|| {
services
.state_accessor
.state_added((since_shortstatehash, horizon_shortstatehash))
.boxed()
.into_future()
});
let current_state_ids: OptionFuture<_> = (!incremental)
.then(|| {
services
.state_accessor
.state_full_shortids(horizon_shortstatehash)
.expect_ok()
.boxed()
.into_future()
})
.into();
let current_state_ids = (!incremental).then_async(|| {
services
.state_accessor
.state_full_shortids(horizon_shortstatehash)
.expect_ok()
.boxed()
.into_future()
});
let state_events = current_state_ids
.stream()
@@ -1268,9 +1241,8 @@ async fn calculate_state_changes<'a>(
.iter()
.any(|event| *event.kind() == RoomMember);
let member_counts: OptionFuture<_> = send_member_counts
.then(|| calculate_counts(services, room_id, sender_user))
.into();
let member_counts =
send_member_counts.then_async(|| calculate_counts(services, room_id, sender_user));
let (joined_member_count, invited_member_count, heroes) =
member_counts.await.unwrap_or((None, None, None));
@@ -1324,12 +1296,11 @@ async fn calculate_counts(
let small_room = joined_member_count.saturating_add(invited_member_count) <= 5;
let heroes: OptionFuture<_> = services
let heroes = services
.config
.calculate_heroes
.and_is(small_room)
.then(|| calculate_heroes(services, room_id, sender_user))
.into();
.then_async(|| calculate_heroes(services, room_id, sender_user));
(Some(joined_member_count), Some(invited_member_count), heroes.await)
}
+11 -19
View File
@@ -6,10 +6,7 @@ mod typing;
use std::fmt::Debug;
use futures::{
FutureExt,
future::{OptionFuture, join5},
};
use futures::{FutureExt, future::join5};
use ruma::{
RoomId,
api::client::sync::sync_events::v5::{ListId, request::ExtensionRoomConfig, response},
@@ -37,45 +34,40 @@ pub(super) async fn handle(
) -> Result<response::Extensions> {
let SyncInfo { .. } = sync_info;
let account_data: OptionFuture<_> = conn
let account_data = conn
.extensions
.account_data
.enabled
.unwrap_or(false)
.then(|| account_data::collect(sync_info, conn, window))
.into();
.then_async(|| account_data::collect(sync_info, conn, window));
let receipts: OptionFuture<_> = conn
let receipts = conn
.extensions
.receipts
.enabled
.unwrap_or(false)
.then(|| receipts::collect(sync_info, conn, window))
.into();
.then_async(|| receipts::collect(sync_info, conn, window));
let typing: OptionFuture<_> = conn
let typing = conn
.extensions
.typing
.enabled
.unwrap_or(false)
.then(|| typing::collect(sync_info, conn, window))
.into();
.then_async(|| typing::collect(sync_info, conn, window));
let to_device: OptionFuture<_> = conn
let to_device = conn
.extensions
.to_device
.enabled
.unwrap_or(false)
.then(|| to_device::collect(sync_info, conn))
.into();
.then_async(|| to_device::collect(sync_info, conn));
let e2ee: OptionFuture<_> = conn
let e2ee = conn
.extensions
.e2ee
.enabled
.unwrap_or(false)
.then(|| e2ee::collect(sync_info, conn))
.into();
.then_async(|| e2ee::collect(sync_info, conn));
let (account_data, receipts, typing, to_device, e2ee) =
join5(account_data, receipts, typing, to_device, e2ee)
+10 -15
View File
@@ -2,7 +2,7 @@ use std::collections::HashSet;
use futures::{
FutureExt, StreamExt, TryFutureExt,
future::{OptionFuture, join, join3},
future::{join, join3},
stream::once,
};
use ruma::{
@@ -71,15 +71,12 @@ pub(super) async fn collect(
let device_one_time_keys_count = services
.users
.last_one_time_keys_update(sender_user)
.then(|since| -> OptionFuture<_> {
since
.gt(&conn.globalsince)
.then(|| {
services
.users
.count_one_time_keys(sender_user, sender_device)
})
.into()
.then(|since| {
since.gt(&conn.globalsince).then_async(|| {
services
.users
.count_one_time_keys(sender_user, sender_device)
})
})
.map(Option::unwrap_or_default);
@@ -164,9 +161,8 @@ async fn collect_room(
let encrypted_since_last_sync = !since_encryption;
let joined_since_last_sync = sender_joined_count.is_ok_and(|count| count > conn.globalsince);
let joined_members_burst: OptionFuture<_> = (joined_since_last_sync
|| encrypted_since_last_sync)
.then(|| {
let joined_members_burst =
(joined_since_last_sync || encrypted_since_last_sync).then_async(|| {
services
.state_cache
.room_members(room_id)
@@ -175,8 +171,7 @@ async fn collect_room(
.map(|user_id| (MembershipState::Join, user_id))
.boxed()
.into_future()
})
.into();
});
services
.state_accessor
+58 -63
View File
@@ -1,4 +1,4 @@
use futures::{StreamExt, future::OptionFuture, pin_mut};
use futures::{StreamExt, pin_mut};
use ruma::{
RoomId, api::client::sync::sync_events::v5::request::ListFilters, directory::RoomTypeFilter,
events::room::member::MembershipState,
@@ -7,7 +7,8 @@ use tuwunel_core::{
is_equal_to, is_true,
utils::{
BoolExt, FutureBoolExt, IterStream, ReadyExt,
future::{self, OptionExt, ReadyBoolExt},
future::{self, OptionFutureExt, ReadyBoolExt},
option::OptionExt,
},
};
@@ -20,9 +21,10 @@ pub(super) async fn filter_room(
room_id: &RoomId,
membership: Option<&MembershipState>,
) -> bool {
let match_invite: OptionFuture<_> = filter
#[expect(clippy::match_same_arms)] // helps readability
let match_invite = filter
.is_invite
.map(async |is_invite| match (membership, is_invite) {
.map_async(async |is_invite| match (membership, is_invite) {
| (Some(MembershipState::Invite), true) => true,
| (Some(MembershipState::Invite), false) => false,
| (Some(_), true) => false,
@@ -32,44 +34,36 @@ pub(super) async fn filter_room(
.state_cache
.is_invited(sender_user, room_id)
.await == is_invite,
})
.into();
});
let match_direct: OptionFuture<_> = filter
.is_dm
.map(async |is_dm| {
services
.account_data
.is_direct(sender_user, room_id)
.await == is_dm
})
.into();
let match_direct = filter.is_dm.map_async(async |is_dm| {
services
.account_data
.is_direct(sender_user, room_id)
.await == is_dm
});
let match_direct_member: OptionFuture<_> = filter
.is_dm
.map(async |is_dm| {
services
.state_accessor
.is_direct(room_id, sender_user)
.await == is_dm
})
.into();
let match_direct_member = filter.is_dm.map_async(async |is_dm| {
services
.state_accessor
.is_direct(room_id, sender_user)
.await == is_dm
});
let match_encrypted: OptionFuture<_> = filter
let match_encrypted = filter
.is_encrypted
.map(async |is_encrypted| {
.map_async(async |is_encrypted| {
services
.state_accessor
.is_encrypted_room(room_id)
.await == is_encrypted
})
.into();
});
let match_space_child: OptionFuture<_> = filter
let match_space_child = filter
.spaces
.is_empty()
.is_false()
.then(async || {
.then_async(async || {
filter
.spaces
.iter()
@@ -77,43 +71,38 @@ pub(super) async fn filter_room(
.flat_map(|room_id| services.spaces.get_space_children(room_id))
.ready_any(is_equal_to!(room_id))
.await
})
.into();
});
let fetch_tags = !filter.tags.is_empty() || !filter.not_tags.is_empty();
let match_room_tag: OptionFuture<_> = fetch_tags
.then(async || {
if let Some(tags) = services
.account_data
.get_room_tags(sender_user, room_id)
.await
.ok()
.filter(|tags| !tags.is_empty())
{
tags.keys().any(|tag| {
(filter.not_tags.is_empty() || !filter.not_tags.contains(tag))
|| (!filter.tags.is_empty() && filter.tags.contains(tag))
})
} else {
filter.tags.is_empty()
}
})
.into();
let match_room_tag = fetch_tags.then_async(async || {
if let Some(tags) = services
.account_data
.get_room_tags(sender_user, room_id)
.await
.ok()
.filter(|tags| !tags.is_empty())
{
tags.keys().any(|tag| {
(filter.not_tags.is_empty() || !filter.not_tags.contains(tag))
|| (!filter.tags.is_empty() && filter.tags.contains(tag))
})
} else {
filter.tags.is_empty()
}
});
let fetch_room_type = !filter.room_types.is_empty() || !filter.not_room_types.is_empty();
let match_room_type: OptionFuture<_> = fetch_room_type
.then(async || {
let room_type = services
.state_accessor
.get_room_type(room_id)
.await
.ok();
let match_room_type = fetch_room_type.then_async(async || {
let room_type = services
.state_accessor
.get_room_type(room_id)
.await
.ok();
let room_type = RoomTypeFilter::from(room_type);
(filter.not_room_types.is_empty() || !filter.not_room_types.contains(&room_type))
&& (filter.room_types.is_empty() || filter.room_types.contains(&room_type))
})
.into();
let room_type = RoomTypeFilter::from(room_type);
(filter.not_room_types.is_empty() || !filter.not_room_types.contains(&room_type))
&& (filter.room_types.is_empty() || filter.room_types.contains(&room_type))
});
future::and7(
match_invite.is_none_or(is_true!()),
@@ -143,8 +132,14 @@ pub(super) async fn filter_room_meta(
.user_can_see_state_events(sender_user, room_id)
.is_false();
pin_mut!(not_visible, not_exists, is_disabled, is_banned);
let not_invited = services
.state_cache
.is_invited(sender_user, room_id)
.is_false();
pin_mut!(not_visible, not_invited, not_exists, is_disabled, is_banned);
not_visible
.and(not_invited)
.or(not_exists)
.or(is_disabled)
.or(is_banned)
+29 -38
View File
@@ -5,7 +5,7 @@ use std::{
use futures::{
FutureExt, StreamExt, TryFutureExt, TryStreamExt,
future::{OptionFuture, join, join3, join4},
future::{join, join3, join4},
};
use ruma::{
JsOption, MxcUri, OwnedMxcUri, OwnedRoomId, RoomId, UserId,
@@ -73,7 +73,6 @@ pub(super) async fn handle(
skip_all,
fields(room_id, roomsince)
)]
#[allow(clippy::too_many_arguments)]
async fn handle_room(
SyncInfo { services, sender_user, .. }: SyncInfo<'_>,
conn: &Connection,
@@ -131,19 +130,16 @@ async fn handle_room(
(timeline_limit, required_state)
});
let timeline: OptionFuture<_> = is_invite
.is_false()
.then(|| {
load_timeline(
services,
sender_user,
room_id,
PduCount::Normal(roomsince),
Some(PduCount::from(conn.next_batch)),
timeline_limit,
)
})
.into();
let timeline = is_invite.is_false().then_async(|| {
load_timeline(
services,
sender_user,
room_id,
PduCount::Normal(roomsince),
Some(PduCount::from(conn.next_batch)),
timeline_limit,
)
});
let (timeline_pdus, limited, _lastcount) = timeline
.await
@@ -182,18 +178,17 @@ async fn handle_room(
.map(TryInto::try_into)
.flat_ok();
let num_live: OptionFuture<_> = roomsince
let num_live = roomsince
.ne(&0)
.and_is(limited || timeline_pdus.len() >= timeline_limit)
.then(|| {
.then_async(|| {
services
.timeline
.pdus(None, room_id, Some(roomsince.into()))
.count()
.map(TryInto::try_into)
.map(Result::ok)
})
.into();
});
let lazy = required_state
.iter()
@@ -249,14 +244,12 @@ async fn handle_room(
.collect();
// TODO: figure out a timestamp we can use for remote invites
let invite_state: OptionFuture<_> = is_invite
.then(|| {
services
.state_cache
.invite_state(sender_user, room_id)
.ok()
})
.into();
let invite_state = is_invite.then_async(|| {
services
.state_cache
.invite_state(sender_user, room_id)
.ok()
});
let room_name = services
.state_accessor
@@ -327,10 +320,10 @@ async fn handle_room(
.boxed()
.await;
let heroes: OptionFuture<_> = services
let heroes = services
.config
.calculate_heroes
.then(|| {
.then_async(|| {
calculate_heroes(
services,
sender_user,
@@ -339,9 +332,10 @@ async fn handle_room(
room_avatar.as_deref(),
)
})
.into();
.await
.unwrap_or_default();
let (heroes, heroes_name, heroes_avatar) = heroes.await.unwrap_or_default();
let (heroes, heroes_name, heroes_avatar) = heroes;
Ok(response::Room {
initial: roomsince.eq(&0).then_some(true),
@@ -365,7 +359,6 @@ async fn handle_room(
}
#[tracing::instrument(name = "heroes", level = "trace", skip_all)]
#[allow(clippy::type_complexity)]
async fn calculate_heroes(
services: &Services,
sender_user: &UserId,
@@ -388,17 +381,15 @@ async fn calculate_heroes(
.await
.ok()?;
let name: OptionFuture<_> = content
let name = content
.displayname
.is_none()
.then(|| services.users.displayname(&user_id).ok())
.into();
.then_async(|| services.users.displayname(&user_id).ok());
let avatar: OptionFuture<_> = content
let avatar = content
.avatar_url
.is_none()
.then(|| services.users.avatar_url(&user_id).ok())
.into();
.then_async(|| services.users.avatar_url(&user_id).ok());
let (name, avatar) = join(name, avatar).await;
let hero = response::Hero {
+34 -50
View File
@@ -1,9 +1,6 @@
use std::cmp::Ordering;
use futures::{
FutureExt, StreamExt, TryFutureExt,
future::{OptionFuture, join5},
};
use futures::{FutureExt, StreamExt, TryFutureExt, future::join5};
use ruma::{OwnedRoomId, UInt, events::room::member::MembershipState, uint};
use tuwunel_core::{
apply, is_true,
@@ -12,6 +9,7 @@ use tuwunel_core::{
utils::{
BoolExt, TryFutureExtExt,
math::usize_from_ruma,
option::OptionExt,
stream::{BroadbandExt, IterStream},
},
};
@@ -80,15 +78,11 @@ async fn matcher(
.iter()
.stream()
.filter_map(async |(id, list)| {
let filter: OptionFuture<_> = list
.filters
list.filters
.clone()
.map(async |filters| {
.map_async(async |filters| {
filter_room(sync_info, &filters, &room_id, membership.as_ref()).await
})
.into();
filter
.await
.is_none_or(is_true!())
.then(|| id.clone())
@@ -97,50 +91,40 @@ async fn matcher(
.map(|lists| (lists.is_empty().is_false(), lists))
.await;
let last_notification: OptionFuture<_> = matched
.then(|| {
services
.pusher
.last_notification_read(sender_user, &room_id)
.unwrap_or_default()
})
.into();
let last_notification = matched.then_async(|| {
services
.pusher
.last_notification_read(sender_user, &room_id)
.unwrap_or_default()
});
let last_privateread: OptionFuture<_> = matched
.then(|| {
services
.read_receipt
.last_privateread_update(sender_user, &room_id)
})
.into();
let last_privateread = matched.then_async(|| {
services
.read_receipt
.last_privateread_update(sender_user, &room_id)
});
let last_receipt: OptionFuture<_> = matched
.then(|| {
services
.read_receipt
.last_receipt_count(&room_id, sender_user.into(), None)
.unwrap_or_default()
})
.into();
let last_receipt = matched.then_async(|| {
services
.read_receipt
.last_receipt_count(&room_id, sender_user.into(), None)
.unwrap_or_default()
});
let last_account: OptionFuture<_> = matched
.then(|| {
services
.account_data
.last_count(Some(room_id.as_ref()), sender_user, Some(conn.next_batch))
.unwrap_or_default()
})
.into();
let last_account = matched.then_async(|| {
services
.account_data
.last_count(Some(room_id.as_ref()), sender_user, Some(conn.next_batch))
.unwrap_or_default()
});
let last_timeline: OptionFuture<_> = matched
.then(|| {
services
.timeline
.last_timeline_count(None, &room_id, Some(conn.next_batch.into()))
.map_ok(PduCount::into_unsigned)
.unwrap_or_default()
})
.into();
let last_timeline = matched.then_async(|| {
services
.timeline
.last_timeline_count(None, &room_id, Some(conn.next_batch.into()))
.map_ok(PduCount::into_unsigned)
.unwrap_or_default()
});
let (last_timeline, last_notification, last_account, last_receipt, last_privateread) =
join5(last_timeline, last_notification, last_account, last_receipt, last_privateread)
+11 -16
View File
@@ -1,7 +1,6 @@
use std::collections::BTreeMap;
use axum::extract::State;
use futures::StreamExt;
use ruma::{
api::{
client::{error::ErrorKind, to_device::send_event_to_device},
@@ -9,7 +8,7 @@ use ruma::{
},
to_device::DeviceIdOrAllDevices,
};
use tuwunel_core::{Error, Result};
use tuwunel_core::{Error, Result, utils::ReadyExt};
use tuwunel_service::sending::EduBuf;
use crate::Ruma;
@@ -69,31 +68,27 @@ pub(crate) async fn send_event_to_device_route(
match target_device_id_maybe {
| DeviceIdOrAllDevices::DeviceId(target_device_id) => {
services
.users
.add_to_device_event(
sender_user,
target_user_id,
target_device_id,
event_type,
event,
)
.await;
services.users.add_to_device_event(
sender_user,
target_user_id,
target_device_id,
event_type,
&event,
);
},
| DeviceIdOrAllDevices::AllDevices => {
let (event_type, event) = (&event_type, &event);
services
.users
.all_device_ids(target_user_id)
.for_each(|target_device_id| {
.ready_for_each(|target_device_id| {
services.users.add_to_device_event(
sender_user,
target_user_id,
target_device_id,
event_type,
event.clone(),
)
&event,
);
})
.await;
},
+85 -99
View File
@@ -16,7 +16,7 @@ use ruma::{
},
presence::PresenceState,
};
use tuwunel_core::{Err, Error, Result};
use tuwunel_core::{Err, Error, Result, err};
use crate::Ruma;
@@ -99,7 +99,7 @@ pub(crate) async fn set_timezone_key_route(
services
.users
.set_timezone(&body.user_id, body.tz.clone());
.set_timezone(&body.user_id, body.tz.as_deref());
// Presence update
services
@@ -129,42 +129,40 @@ pub(crate) async fn set_profile_field_route(
return Err!(Request(BadJson("Key names cannot be longer than 128 bytes")));
}
if body.value.field_name() == ProfileFieldName::DisplayName {
let all_joined_rooms: Vec<OwnedRoomId> = services
.state_cache
.rooms_joined(&body.user_id)
.map(Into::into)
.collect()
.await;
match &body.value {
| ProfileFieldValue::DisplayName(displayname) => {
let all_joined_rooms: Vec<OwnedRoomId> = services
.state_cache
.rooms_joined(&body.user_id)
.map(Into::into)
.collect()
.await;
services
.users
.update_displayname(
services
.users
.update_displayname(&body.user_id, Some(displayname), &all_joined_rooms)
.await;
},
| ProfileFieldValue::AvatarUrl(avatar_url) => {
let all_joined_rooms: Vec<OwnedRoomId> = services
.state_cache
.rooms_joined(&body.user_id)
.map(Into::into)
.collect()
.await;
services
.users
.update_avatar_url(&body.user_id, Some(avatar_url), None, &all_joined_rooms)
.await;
},
| _ => {
services.users.set_profile_key(
&body.user_id,
Some(body.value.value().to_string()),
&all_joined_rooms,
)
.await;
} else if body.value.field_name() == ProfileFieldName::AvatarUrl {
let mxc = ruma::OwnedMxcUri::from(body.value.value().to_string());
let all_joined_rooms: Vec<OwnedRoomId> = services
.state_cache
.rooms_joined(&body.user_id)
.map(Into::into)
.collect()
.await;
services
.users
.update_avatar_url(&body.user_id, Some(mxc), None, &all_joined_rooms)
.await;
} else {
services.users.set_profile_key(
&body.user_id,
body.value.field_name().as_str(),
Some(body.value.value().into_owned()),
);
body.value.field_name().as_str(),
Some(&body.value.value()),
);
},
}
// Presence update
@@ -191,34 +189,38 @@ pub(crate) async fn delete_profile_field_route(
return Err!(Request(Forbidden("You cannot update the profile of another user")));
}
if body.field == ProfileFieldName::DisplayName {
let all_joined_rooms: Vec<OwnedRoomId> = services
.state_cache
.rooms_joined(&body.user_id)
.map(Into::into)
.collect()
.await;
match body.field {
| ProfileFieldName::DisplayName => {
let all_joined_rooms: Vec<OwnedRoomId> = services
.state_cache
.rooms_joined(&body.user_id)
.map(Into::into)
.collect()
.await;
services
.users
.update_displayname(&body.user_id, None, &all_joined_rooms)
.await;
} else if body.field == ProfileFieldName::AvatarUrl {
let all_joined_rooms: Vec<OwnedRoomId> = services
.state_cache
.rooms_joined(&body.user_id)
.map(Into::into)
.collect()
.await;
services
.users
.update_displayname(&body.user_id, None, &all_joined_rooms)
.await;
},
| ProfileFieldName::AvatarUrl => {
let all_joined_rooms: Vec<OwnedRoomId> = services
.state_cache
.rooms_joined(&body.user_id)
.map(Into::into)
.collect()
.await;
services
.users
.update_avatar_url(&body.user_id, None, None, &all_joined_rooms)
.await;
} else {
services
.users
.set_profile_key(&body.user_id, body.field.as_str(), None);
services
.users
.update_avatar_url(&body.user_id, None, None, &all_joined_rooms)
.await;
},
| _ => {
services
.users
.set_profile_key(&body.user_id, body.field.as_str(), None);
},
}
// Presence update
@@ -262,19 +264,19 @@ pub(crate) async fn get_timezone_key_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());
return Ok(get_timezone_key::unstable::Response { tz: response.tz });
}
@@ -323,43 +325,34 @@ pub(crate) async fn get_profile_field_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());
let profile_key_value: Option<ProfileFieldValue> = match response
let value = response
.custom_profile_fields
.get(body.field.as_str())
{
| Some(value) => {
services.users.set_profile_key(
&body.user_id,
body.field.as_str(),
Some(value.clone()),
);
.ok_or_else(|| {
err!(Request(NotFound("The requested profile key does not exist.")))
})?;
Some(ProfileFieldValue::new(body.field.as_str(), value.clone())?)
},
| _ => {
return Err!(Request(NotFound("The requested profile key does not exist.")));
},
};
services
.users
.set_profile_key(&body.user_id, body.field.as_str(), Some(value));
if profile_key_value.is_none() {
return Err!(Request(NotFound("The requested profile key does not exist.")));
}
let profile_key_value = ProfileFieldValue::new(body.field.as_str(), value.clone())?;
return Ok(get_profile_field::v3::Response { value: profile_key_value });
return Ok(get_profile_field::v3::Response { value: Some(profile_key_value) });
}
}
@@ -369,20 +362,13 @@ pub(crate) async fn get_profile_field_route(
return Err!(Request(NotFound("Profile was not found.")));
}
let profile_key_value: Option<ProfileFieldValue> = match services
let value = services
.users
.profile_key(&body.user_id, body.field.as_str())
.await
{
| Ok(value) => Some(ProfileFieldValue::new(body.field.as_str(), value)?),
| _ => {
return Err!(Request(NotFound("The requested profile key does not exist.")));
},
};
.map_err(|_| err!(Request(NotFound("The requested profile key does not exist."))))?;
if profile_key_value.is_none() {
return Err!(Request(NotFound("The requested profile key does not exist.")));
}
let profile_key_value = ProfileFieldValue::new(body.field.as_str(), value)?;
Ok(get_profile_field::v3::Response { value: profile_key_value })
Ok(get_profile_field::v3::Response { value: Some(profile_key_value) })
}
+3
View File
@@ -39,6 +39,8 @@ pub(crate) async fn get_supported_versions_route(
"v1.5".to_owned(),
"v1.10".to_owned(), // relations recursion
"v1.11".to_owned(), // authenticated media
"v1.12".to_owned(), // m.tz
"v1.15".to_owned(), // custom profile fields
],
unstable_features: BTreeMap::from_iter([
("org.matrix.e2e_cross_signing".to_owned(), true),
@@ -57,6 +59,7 @@ pub(crate) async fn get_supported_versions_route(
("us.cloke.msc4175".to_owned(), true), /* Profile field for user time zone (https://github.com/matrix-org/matrix-spec-proposals/pull/4175) */
("org.matrix.msc4180".to_owned(), true), /* stable flag for 3916 (https://github.com/matrix-org/matrix-spec-proposals/pull/4180) */
("org.matrix.simplified_msc3575".to_owned(), true), /* Simplified Sliding sync (https://github.com/matrix-org/matrix-spec-proposals/pull/4186) */
("fi.mau.msc2815".to_owned(), true), /* Allow room moderators to view redacted event content (https://github.com/matrix-org/matrix-spec-proposals/pull/2815) */
]),
};
+66 -41
View File
@@ -1,16 +1,18 @@
use axum::extract::State;
use futures::{FutureExt, StreamExt, pin_mut};
use ruma::{
UserId,
api::client::user_directory::search_users::{self},
events::room::join_rules::JoinRule,
};
use tuwunel_core::{
Result,
utils::{
future::BoolExt,
BoolExt, FutureBoolExt,
stream::{BroadbandExt, ReadyExt},
},
};
use tuwunel_service::Services;
use crate::Ruma;
@@ -30,7 +32,7 @@ pub(crate) async fn search_users_route(
) -> Result<search_users::v3::Response> {
let sender_user = body.sender_user();
let limit = usize::try_from(body.limit)
.map_or(LIMIT_DEFAULT, usize::from)
.unwrap_or(LIMIT_DEFAULT)
.min(LIMIT_MAX);
let search_term = body.search_term.to_lowercase();
@@ -42,45 +44,20 @@ pub(crate) async fn search_users_route(
.broad_filter_map(async |user_id| {
let display_name = services.users.displayname(&user_id).await.ok();
let user_id_matches = user_id
.as_str()
.to_lowercase()
.contains(&search_term);
let display_name_matches = display_name
.as_deref()
.map(str::to_lowercase)
.is_some_and(|display_name| display_name.contains(&search_term));
if !user_id_matches && !display_name_matches {
return None;
}
let user_in_public_room = services
.state_cache
.rooms_joined(&user_id)
.map(ToOwned::to_owned)
.broad_any(async |room_id| {
services
.state_accessor
.get_join_rules(&room_id)
.map(|rule| matches!(rule, JoinRule::Public))
.await
});
let user_sees_user = services
.state_cache
.user_sees_user(sender_user, &user_id);
pin_mut!(user_in_public_room, user_sees_user);
user_in_public_room
.or(user_sees_user)
.await
.then_some(search_users::v3::User {
user_id: user_id.clone(),
display_name,
avatar_url: services.users.avatar_url(&user_id).await.ok(),
})
should_show_user(
&services,
sender_user,
&user_id,
display_name.as_deref(),
&search_term,
)
.await
.then_async(async || search_users::v3::User {
user_id: user_id.clone(),
display_name,
avatar_url: services.users.avatar_url(&user_id).await.ok(),
})
.await
});
pin_mut!(users);
@@ -89,3 +66,51 @@ pub(crate) async fn search_users_route(
Ok(search_users::v3::Response { results, limited })
}
async fn should_show_user(
services: &Services,
sender_user: &UserId,
target_user: &UserId,
target_display_name: Option<&str>,
search_term: &str,
) -> bool {
let user_id_matches = target_user
.as_str()
.to_lowercase()
.contains(search_term);
let display_name_matches = target_display_name
.map(str::to_lowercase)
.is_some_and(|display_name| display_name.contains(search_term));
if !user_id_matches && !display_name_matches {
return false;
}
if services
.server
.config
.show_all_local_users_in_user_directory
{
return true;
}
let user_in_public_room = services
.state_cache
.rooms_joined(target_user)
.map(ToOwned::to_owned)
.broad_any(async |room_id| {
services
.state_accessor
.get_join_rules(&room_id)
.map(|rule| matches!(rule, JoinRule::Public))
.await
});
let user_sees_user = services
.state_cache
.user_sees_user(sender_user, target_user);
pin_mut!(user_in_public_room, user_sees_user);
user_in_public_room.or(user_sees_user).await
}
+4 -1
View File
@@ -1,10 +1,13 @@
#![type_length_limit = "262144"] //TODO: REDUCE ME
#![allow(clippy::toplevel_ref_arg)]
#![expect(clippy::toplevel_ref_arg)]
#![expect(clippy::duration_suboptimal_units)] // remove after MSRV 1.91
pub mod client;
pub mod router;
pub mod server;
use log as _;
pub(crate) use self::router::{Ruma, RumaResponse, State};
tuwunel_core::mod_ctor! {}
+3
View File
@@ -38,6 +38,9 @@ pub fn build(router: Router<State>, server: &Server) -> Router<State> {
.ruma_route(&client::login_route)
.ruma_route(&client::login_token_route)
.ruma_route(&client::refresh_token_route)
.ruma_route(&client::sso_login_route)
.ruma_route(&client::sso_login_with_provider_route)
.ruma_route(&client::sso_callback_route)
.ruma_route(&client::whoami_route)
.ruma_route(&client::logout_route)
.ruma_route(&client::logout_all_route)
+6 -1
View File
@@ -1,6 +1,7 @@
use std::{fmt::Debug, mem, ops::Deref};
use axum::{body::Body, extract::FromRequest};
use axum_extra::extract::cookie::CookieJar;
use bytes::{BufMut, Bytes, BytesMut};
use ruma::{
CanonicalJsonObject, CanonicalJsonValue, DeviceId, OwnedDeviceId, OwnedServerName,
@@ -18,6 +19,9 @@ pub(crate) struct Args<T> {
/// Request struct body
pub(crate) body: T,
/// Cookies received from the useragent.
pub(crate) cookie: CookieJar,
/// Federation server authentication: X-Matrix origin
/// None when not a federation server.
pub(crate) origin: Option<OwnedServerName>,
@@ -110,6 +114,7 @@ where
let auth = auth::auth(services, &mut request, json_body.as_ref(), &T::METADATA).await?;
Ok(Self {
body: make_body::<T>(services, &mut request, json_body.as_mut(), &auth)?,
cookie: request.cookie,
origin: auth.origin,
sender_user: auth.sender_user,
sender_device: auth.sender_device,
@@ -148,7 +153,7 @@ fn into_http_request(request: &Request, body: Bytes) -> hyper::Request<Bytes> {
.expect("http request body")
}
#[allow(clippy::needless_pass_by_value)]
#[expect(clippy::needless_pass_by_value)]
fn take_body(
services: &Services,
request: &mut Request,
+12 -8
View File
@@ -122,11 +122,13 @@ pub(super) async fn auth(
Err(BadRequest(UnknownToken { soft_logout: true }, "Expired access token."))
},
| (AppserviceToken, User(_)) =>
Err!(Request(Unauthorized("Appservice tokens must be used on this endpoint."))),
| (AppserviceToken, User(_)) => {
Err!(Request(Unauthorized("Appservice tokens must be used on this endpoint.")))
},
| (ServerSignatures, Appservice(_) | User(_)) =>
Err!(Request(Unauthorized("Server signatures must be used on this endpoint."))),
| (ServerSignatures, Appservice(_) | User(_)) => {
Err!(Request(Unauthorized("Server signatures must be used on this endpoint.")))
},
| (ServerSignatures, Token::None) => Ok(auth_server(services, request, json_body).await?),
@@ -182,8 +184,9 @@ fn check_auth_still_required(services: &Services, metadata: &Metadata, token: &T
.require_auth_for_profile_requests =>
match token {
| Token::Appservice(_) | Token::User(_) => Ok(()),
| Token::None | Token::Expired(_) | Token::Invalid =>
Err!(Request(MissingToken("Missing or invalid access token."))),
| Token::None | Token::Expired(_) | Token::Invalid => {
Err!(Request(MissingToken("Missing or invalid access token.")))
},
},
| &get_public_rooms::v3::Request::METADATA
if !services
@@ -192,8 +195,9 @@ fn check_auth_still_required(services: &Services, metadata: &Metadata, token: &T
.allow_public_room_directory_without_auth =>
match token {
| Token::Appservice(_) | Token::User(_) => Ok(()),
| Token::None | Token::Expired(_) | Token::Invalid =>
Err!(Request(MissingToken("Missing or invalid access token."))),
| Token::None | Token::Expired(_) | Token::Invalid => {
Err!(Request(MissingToken("Missing or invalid access token.")))
},
},
| _ => Ok(()),
}
+12 -2
View File
@@ -5,7 +5,7 @@ use ruma::{
client::uiaa::{AuthData, AuthFlow, AuthType, Jwt, UiaaInfo},
},
};
use tuwunel_core::{Err, Error, Result, err, utils};
use tuwunel_core::{Err, Error, Result, err, is_equal_to, utils};
use tuwunel_service::{Services, uiaa::SESSION_ID_LENGTH};
use crate::{Ruma, client::jwt};
@@ -67,9 +67,19 @@ where
| Some(ref json) => {
let sender_user = body
.sender_user
.as_ref()
.as_deref()
.ok_or_else(|| err!(Request(MissingToken("Missing access token."))))?;
// Skip UIAA for SSO/OIDC users.
if services
.users
.origin(sender_user)
.await
.is_ok_and(is_equal_to!("sso"))
{
return Ok(sender_user.to_owned());
}
uiaainfo.session = Some(utils::random_string(SESSION_ID_LENGTH));
services
.uiaa
+1
View File
@@ -35,6 +35,7 @@ impl RouterExt for Router<State> {
macro_rules! ruma_handler {
( $($tx:ident),* $(,)? ) => {
#[allow(clippy::allow_attributes)]
#[allow(non_snake_case)]
impl<Err, Req, Fut, Fun, $($tx,)*> RumaHandler<($($tx,)* Ruma<Req>,)> for Fun
where
+4 -1
View File
@@ -1,6 +1,7 @@
use std::str;
use axum::{RequestExt, RequestPartsExt, extract::Path};
use axum_extra::extract::cookie::CookieJar;
use bytes::Bytes;
use http::request::Parts;
use serde::Deserialize;
@@ -17,6 +18,7 @@ pub(super) type UserId = SmallString<[u8; 48]>;
#[derive(Debug)]
pub(super) struct Request {
pub(super) cookie: CookieJar,
pub(super) path: Path<PathParams>,
pub(super) query: QueryParams,
pub(super) body: Bytes,
@@ -33,6 +35,7 @@ pub(super) async fn from(
let limited = request.with_limited_body();
let (mut parts, body) = limited.into_parts();
let cookie: CookieJar = parts.extract().await?;
let path: Path<PathParams> = parts.extract().await?;
let query = parts.uri.query().unwrap_or_default();
let query = serde_html_form::from_str(query)
@@ -44,5 +47,5 @@ pub(super) async fn from(
.await
.map_err(|e| err!(Request(TooLarge("Request body too large: {e}"))))?;
Ok(Request { path, query, body, parts })
Ok(Request { cookie, path, query, body, parts })
}
+3 -7
View File
@@ -46,22 +46,18 @@ pub(crate) async fn get_event_authorization_route(
let room_id = <&RoomId>::try_from(room_id_str)
.map_err(|_| Error::bad_database("Invalid room_id in event in database."))?;
let room_version = services
.state
.get_room_version(room_id)
.await
.ok();
let room_version = services.state.get_room_version(room_id).await?;
let auth_chain = services
.auth_chain
.event_ids_iter(room_id, once(body.event_id.borrow()))
.event_ids_iter(room_id, &room_version, once(body.event_id.borrow()))
.ready_filter_map(Result::ok)
.broad_filter_map(async |id| {
let pdu = services.timeline.get_pdu_json(&id).await.ok()?;
let pdu = services
.federation
.format_pdu_into(pdu, room_version.as_ref())
.format_pdu_into(pdu, Some(&room_version))
.await;
Some(pdu)
+2 -18
View File
@@ -21,7 +21,6 @@ use tuwunel_core::{
matrix::{Event, PduCount, PduEvent, event::gen_event_id},
utils,
utils::hash::sha256,
warn,
};
use crate::Ruma;
@@ -51,27 +50,12 @@ pub(crate) async fn create_invite_route(
));
}
if let Some(server) = body.room_id.server_name() {
if services
if let Some(server) = body.room_id.server_name()
&& services
.config
.forbidden_remote_server_names
.is_match(server.host())
{
return Err!(Request(Forbidden("Server is banned on this homeserver.")));
}
}
if services
.config
.forbidden_remote_server_names
.is_match(body.origin().host())
{
warn!(
"Received federated/remote invite from banned server {} for room ID {}. Rejecting.",
body.origin(),
body.room_id
);
return Err!(Request(Forbidden("Server is banned on this homeserver.")));
}
+7 -23
View File
@@ -12,7 +12,7 @@ use ruma::{
},
};
use tuwunel_core::{
Err, Error, Result, at, debug_info, matrix::pdu::PduBuilder, utils::IterStream, warn,
Err, Error, Result, at, debug_info, matrix::pdu::PduBuilder, utils::IterStream,
};
use tuwunel_service::Services;
@@ -39,31 +39,15 @@ pub(crate) async fn create_join_event_template_route(
.acl_check(body.origin(), &body.room_id)
.await?;
if services
.config
.forbidden_remote_server_names
.is_match(body.origin().host())
{
warn!(
"Server {} for remote user {} tried joining room ID {} which has a server name that \
is globally forbidden. Rejecting.",
body.origin(),
&body.user_id,
&body.room_id,
);
return Err!(Request(Forbidden("Server is banned on this homeserver.")));
}
if let Some(server) = body.room_id.server_name() {
if services
if let Some(server) = body.room_id.server_name()
&& services
.config
.forbidden_remote_server_names
.is_match(server.host())
{
return Err!(Request(Forbidden(warn!(
"Room ID server name {server} is banned on this homeserver."
))));
}
{
return Err!(Request(Forbidden(warn!(
"Room ID server name {server} is banned on this homeserver."
))));
}
let room_version_id = services
+12 -30
View File
@@ -6,7 +6,7 @@ use ruma::{
api::{client::error::ErrorKind, federation::membership::prepare_knock_event},
events::room::member::{MembershipState, RoomMemberEventContent},
};
use tuwunel_core::{Err, Error, Result, at, debug_warn, matrix::pdu::PduBuilder, warn};
use tuwunel_core::{Err, Error, Result, at, debug_warn, matrix::pdu::PduBuilder};
use crate::Ruma;
@@ -31,29 +31,13 @@ pub(crate) async fn create_knock_event_template_route(
.acl_check(body.origin(), &body.room_id)
.await?;
if services
.config
.forbidden_remote_server_names
.is_match(body.origin().host())
{
warn!(
"Server {} for remote user {} tried knocking room ID {} which has a server name \
that is globally forbidden. Rejecting.",
body.origin(),
&body.user_id,
&body.room_id,
);
return Err!(Request(Forbidden("Server is banned on this homeserver.")));
}
if let Some(server) = body.room_id.server_name() {
if services
if let Some(server) = body.room_id.server_name()
&& services
.config
.forbidden_remote_server_names
.is_match(server.host())
{
return Err!(Request(Forbidden("Server is banned on this homeserver.")));
}
{
return Err!(Request(Forbidden("Server is banned on this homeserver.")));
}
let room_version = services
@@ -80,17 +64,15 @@ pub(crate) async fn create_knock_event_template_route(
if let Ok(membership) = services
.state_accessor
.get_member(&body.room_id, &body.user_id)
.await
.await && membership.membership == MembershipState::Ban
{
if membership.membership == MembershipState::Ban {
debug_warn!(
"Remote user {} is banned from {} but attempted to knock",
&body.user_id,
&body.room_id
);
debug_warn!(
"Remote user {} is banned from {} but attempted to knock",
&body.user_id,
&body.room_id
);
return Err!(Request(Forbidden("You cannot knock on a room you are banned from.")));
}
return Err!(Request(Forbidden("You cannot knock on a room you are banned from.")));
}
let pdu_json = services
+54 -31
View File
@@ -3,7 +3,6 @@ use std::{collections::BTreeMap, net::IpAddr, time::Instant};
use axum::extract::State;
use axum_client_ip::InsecureClientIp;
use futures::{FutureExt, Stream, StreamExt, TryFutureExt, TryStreamExt};
use itertools::Itertools;
use ruma::{
CanonicalJsonObject, OwnedEventId, OwnedRoomId, OwnedUserId, RoomId, ServerName, UserId,
api::{
@@ -25,10 +24,13 @@ use tuwunel_core::{
Err, Error, Result, debug,
debug::INFO_SPAN_LEVEL,
debug_warn, err, error,
itertools::Itertools,
result::LogErr,
trace,
utils::{
IterStream, ReadyExt, millis_since_unix_epoch,
IterStream, ReadyExt,
debug::str_truncated,
millis_since_unix_epoch,
stream::{BroadbandExt, TryBroadbandExt, automatic_width},
},
warn,
@@ -52,7 +54,8 @@ type Pdu = (OwnedRoomId, OwnedEventId, CanonicalJsonObject);
skip_all,
fields(
%client,
origin = body.origin().as_str()
origin = body.origin().as_str(),
txn = str_truncated(body.transaction_id.as_str(), 20),
),
)]
pub(crate) async fn send_transaction_message_route(
@@ -83,8 +86,6 @@ pub(crate) async fn send_transaction_message_route(
pdus = body.pdus.len(),
edus = body.edus.len(),
elapsed = ?txn_start_time.elapsed(),
id = ?body.transaction_id,
origin =?body.origin(),
"Starting txn",
);
@@ -104,21 +105,25 @@ pub(crate) async fn send_transaction_message_route(
.filter_map(Result::ok)
.stream();
trace!(
elapsed = ?txn_start_time.elapsed(),
"Parsed txn",
);
let results = handle(&services, &client, body.origin(), txn_start_time, pdus, edus).await?;
debug!(
pdus = body.pdus.len(),
edus = body.edus.len(),
elapsed = ?txn_start_time.elapsed(),
id = ?body.transaction_id,
origin =?body.origin(),
"Finished txn",
);
for (id, result) in &results {
if let Err(e) = result {
if matches!(e, Error::BadRequest(ErrorKind::NotFound, _)) {
warn!("Incoming PDU failed {id}: {e:?}");
}
if let Err(e) = result
&& matches!(e, Error::BadRequest(ErrorKind::NotFound, _))
{
warn!("Incoming PDU failed {id}: {e:?}");
}
}
@@ -140,11 +145,12 @@ async fn handle(
) -> Result<ResolvedMap> {
// group pdus by room
let pdus = pdus
.enumerate()
.collect()
.map(|mut pdus: Vec<_>| {
pdus.sort_by(|(room_a, ..), (room_b, ..)| room_a.cmp(room_b));
pdus.sort_by(|(_, (room_a, ..)), (_, (room_b, ..))| room_a.cmp(room_b));
pdus.into_iter()
.into_grouping_map_by(|(room_id, ..)| room_id.clone())
.into_grouping_map_by(|(_, (room_id, ..))| room_id.clone())
.collect()
})
.await;
@@ -164,7 +170,10 @@ async fn handle(
.await?;
// evaluate edus after pdus, at least for now.
edus.for_each_concurrent(automatic_width(), |edu| handle_edu(services, client, origin, edu))
edus.enumerate()
.for_each_concurrent(automatic_width(), |(i, edu)| {
handle_edu(services, client, origin, i, edu)
})
.await;
Ok(results)
@@ -176,7 +185,7 @@ async fn handle_room(
origin: &ServerName,
txn_start_time: &Instant,
ref room_id: OwnedRoomId,
pdus: impl Iterator<Item = Pdu> + Send,
pdus: impl Iterator<Item = (usize, Pdu)> + Send,
) -> Result<ResolvedMap> {
let _room_lock = services
.event_handler
@@ -184,8 +193,9 @@ async fn handle_room(
.lock(room_id)
.await;
pdus.try_stream()
.and_then(async |(room_id, event_id, value)| {
pdus.enumerate()
.try_stream()
.and_then(async |(ri, (ti, (room_id, event_id, value)))| {
services.server.check_running()?;
let pdu_start_time = Instant::now();
let result = services
@@ -195,9 +205,13 @@ async fn handle_room(
.await;
debug!(
%event_id,
%room_id,
ri,
ti,
pdu_elapsed = ?pdu_start_time.elapsed(),
txn_elapsed = ?txn_start_time.elapsed(),
"Finished PDU {event_id}",
"Finished PDU",
);
Ok((event_id, result))
@@ -207,7 +221,13 @@ async fn handle_room(
.await
}
async fn handle_edu(services: &Services, client: &IpAddr, origin: &ServerName, edu: Edu) {
async fn handle_edu(
services: &Services,
client: &IpAddr,
origin: &ServerName,
i: usize,
edu: Edu,
) {
match edu {
| Edu::Presence(presence) if services.server.config.allow_incoming_presence =>
handle_edu_presence(services, client, origin, presence).await,
@@ -231,9 +251,9 @@ async fn handle_edu(services: &Services, client: &IpAddr, origin: &ServerName, e
| Edu::SigningKeyUpdate(content) =>
handle_edu_signing_key_update(services, client, origin, content).await,
| Edu::_Custom(ref _custom) => debug_warn!(?edu, "received custom/unknown EDU"),
| Edu::_Custom(ref _custom) => debug_warn!(?i, ?edu, "received custom/unknown EDU"),
| _ => trace!(?edu, "skipped"),
| _ => trace!(?i, ?edu, "skipped"),
}
}
@@ -268,11 +288,11 @@ async fn handle_edu_presence_update(
services
.presence
.set_presence(
.set_presence_from_federation(
&update.user_id,
&update.presence,
Some(update.currently_active),
Some(update.last_active_ago),
update.currently_active,
update.last_active_ago,
update.status_msg.clone(),
)
.await
@@ -538,24 +558,27 @@ async fn handle_edu_direct_to_device_event(
) {
match target_device_id_maybe {
| DeviceIdOrAllDevices::DeviceId(ref target_device_id) => {
services
.users
.add_to_device_event(sender, target_user_id, target_device_id, ev_type, event)
.await;
services.users.add_to_device_event(
sender,
target_user_id,
target_device_id,
ev_type,
&event,
);
},
| DeviceIdOrAllDevices::AllDevices => {
services
.users
.all_device_ids(target_user_id)
.for_each(|target_device_id| {
.ready_for_each(|target_device_id| {
services.users.add_to_device_event(
sender,
target_user_id,
target_device_id,
ev_type,
event.clone(),
)
&event,
);
})
.await;
},
Loaded 100 of 265 files, more files were not shown because too many files have changed in this diff. Show more