From 3d7dd5413bb74ba17a2cd2d22c981cefbcba2a09 Mon Sep 17 00:00:00 2001 From: loki5512344 Date: Sat, 24 Oct 2026 16:51:15 +0200 Subject: [PATCH] feat(client): protocol v2 wire layer, screen sharing, channel webhooks and secure voice Verified against the real gateway via tests/gateway_interop.rs. --- Cargo.lock | 234 ++++++++ Cargo.toml | 9 +- build.rs | 6 + docs/04-clients/screen-capture.md | 45 +- docs/maintenance/protocol-interop.md | 19 + protocol/lnex.proto | 461 +++++++++++++++ scripts/sync-protocol.py | 14 + src/audio/mod.rs | 2 +- src/audio/pipeline/capture.rs | 2 + src/audio/pipeline/media.rs | 148 +++++ src/audio/pipeline/mod.rs | 24 + src/audio/pipeline/playback.rs | 20 + src/bin/e2e_voice.rs | 221 ++++--- src/lib.rs | 2 + src/main.rs | 43 +- src/media/broadcast.rs | 575 ++++++++++++++++++ src/media/capture.rs | 52 +- src/media/gateway_tests.rs | 245 ++++++++ src/media/mod.rs | 10 +- src/media/peer.rs | 646 +++++++++++++++++++++ src/media/policy.rs | 25 + src/net/crypto/cipher.rs | 26 +- src/net/crypto/mod.rs | 37 +- src/net/framing/io.rs | 151 ++++- src/net/framing/mod.rs | 28 +- src/net/handshake.rs | 68 ++- src/net/internal/dispatch/channel.rs | 41 +- src/net/internal/dispatch/dm.rs | 38 +- src/net/internal/dispatch/mod.rs | 38 +- src/net/internal/dispatch/social.rs | 123 ++-- src/net/mod.rs | 7 +- src/net/payloads/channel.rs | 180 +----- src/net/payloads/dm.rs | 185 +----- src/net/payloads/guild.rs | 201 +------ src/net/payloads/mod.rs | 17 +- src/net/session/handlers/core.rs | 90 ++- src/net/session/handlers/social/friends.rs | 32 +- src/net/session/handlers/social/guild.rs | 63 +- src/net/session/handlers/social/mod.rs | 59 +- src/net/session/handlers/voice.rs | 180 ++++-- src/net/session/task.rs | 47 +- src/net/transport.rs | 112 ++++ src/net/types/command.rs | 24 + src/net/types/event.rs | 23 + src/net/types/mod.rs | 4 +- src/net/voice/mod.rs | 273 ++++++--- src/net/voice/receive.rs | 80 --- src/net/wire/mod.rs | 9 + src/ui/media.rs | 455 +++++++++++++-- src/ui/mod.rs | 2 + src/ui/slint/chat.slint | 2 + src/ui/slint/main.slint | 5 +- src/ui/slint/screen-share.slint | 61 +- src/ui/slint/settings/appearance.slint | 58 +- src/ui/slint/webhooks.slint | 126 ++++ src/ui/update.rs | 41 ++ src/ui/webhooks.rs | 287 +++++++++ src/ui_smoke.rs | 2 + src/webhooks/controller.rs | 409 +++++++++++++ src/webhooks/mod.rs | 5 + src/webhooks/tests.rs | 172 ++++++ tests/gateway_interop.rs | 306 ++++++++++ translations/en/LC_MESSAGES/vnox-client.po | 134 ++++- translations/ru.json | 65 ++- translations/ru/LC_MESSAGES/vnox-client.po | 135 ++++- 65 files changed, 6048 insertions(+), 1156 deletions(-) create mode 100644 docs/maintenance/protocol-interop.md create mode 100644 protocol/lnex.proto create mode 100644 scripts/sync-protocol.py create mode 100644 src/audio/pipeline/media.rs create mode 100644 src/media/broadcast.rs create mode 100644 src/media/gateway_tests.rs create mode 100644 src/media/peer.rs create mode 100644 src/net/transport.rs delete mode 100644 src/net/voice/receive.rs create mode 100644 src/net/wire/mod.rs create mode 100644 src/ui/slint/webhooks.slint create mode 100644 src/ui/webhooks.rs create mode 100644 src/webhooks/controller.rs create mode 100644 src/webhooks/mod.rs create mode 100644 src/webhooks/tests.rs create mode 100644 tests/gateway_interop.rs diff --git a/Cargo.lock b/Cargo.lock index e0ed5a1..677b341 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -389,6 +389,29 @@ version = "1.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f2032f911046de80f0a198e0901378627c33f59ea0ac00e363d481118bd70a53" +[[package]] +name = "aws-lc-rs" +version = "1.18.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "faac5829c2b74c28f830747e7818ccfb684261b5f48a1118b1e2a13d36dfab13" +dependencies = [ + "aws-lc-sys", + "zeroize", +] + +[[package]] +name = "aws-lc-sys" +version = "0.46.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1622d8446a2d4b2ce0c7eefc73dd43a99779028d5ee5c2dd8073a658ba8a2bc" +dependencies = [ + "cc", + "cmake", + "dunce", + "fs_extra", + "pkg-config", +] + [[package]] name = "base64" version = "0.23.1" @@ -1252,6 +1275,12 @@ version = "0.8.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "edf234dd1594d6dd434a8fb8cada51ddbbc593e40e4a01556a0b31c62da2775b" +[[package]] +name = "dunce" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "92773504d58c093f6de2459af4af33faa518c13451eb8f2b5698ed3d36e7c813" + [[package]] name = "dwrote" version = "0.11.5" @@ -1605,6 +1634,12 @@ dependencies = [ "pkg-config", ] +[[package]] +name = "fs_extra" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "42703706b716c37f96a77aea830392ad231f44c9e9a67872fa5548707e11b11c" + [[package]] name = "futures" version = "0.3.34" @@ -1748,6 +1783,17 @@ dependencies = [ "windows-link", ] +[[package]] +name = "getrandom" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ff2abc00be7fca6ebc474524697ae276ad847ad0a6b3faa4bcb027e9a4614ad0" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + [[package]] name = "getrandom" version = "0.3.4" @@ -2044,6 +2090,29 @@ dependencies = [ "system-deps", ] +[[package]] +name = "gstreamer-sdp" +version = "0.25.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a7859290ba340f1bd45d4bc0ada958700e9d3b1a522b456fd7ee88da468cd13" +dependencies = [ + "glib", + "gstreamer", + "gstreamer-sdp-sys", +] + +[[package]] +name = "gstreamer-sdp-sys" +version = "0.25.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7e5afafebe4a135392ec2455144cec78821681e007cb9b19c071a4c86b218f64" +dependencies = [ + "glib-sys", + "gstreamer-sys", + "libc", + "system-deps", +] + [[package]] name = "gstreamer-sys" version = "0.25.4" @@ -2087,6 +2156,32 @@ dependencies = [ "system-deps", ] +[[package]] +name = "gstreamer-webrtc" +version = "0.25.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "de524e63b32f3f478b9f718ef6895bd50a1bb1d1d4d7c2134f0a30f36d046157" +dependencies = [ + "glib", + "gstreamer", + "gstreamer-sdp", + "gstreamer-webrtc-sys", + "libc", +] + +[[package]] +name = "gstreamer-webrtc-sys" +version = "0.25.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf77b07fd279bf060d938c7024c8a152a566b4b5e9ff401c7ba890d278a47d1b" +dependencies = [ + "glib-sys", + "gstreamer-sdp-sys", + "gstreamer-sys", + "libc", + "system-deps", +] + [[package]] name = "half" version = "2.7.1" @@ -3143,6 +3238,12 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "956787520e75e9bd233246045d19f42fb73242759cc57fba9611d940ae96d4b0" +[[package]] +name = "multimap" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d87ecb2933e8aeadb3e3a02b828fed80a7528047e68b4f424523a0981a3a084" + [[package]] name = "naga" version = "30.0.1" @@ -4133,6 +4234,57 @@ version = "1.0.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3d595e54a326bc53c1c197b32d295e14b169e3cfeaa8dc82b529f947fba6bcf5" +[[package]] +name = "prost" +version = "0.14.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "528ac67416ff8646872a3c02cad9cc4ee5dc9f9540c9b10771855c95cb2e5ae1" +dependencies = [ + "bytes", + "prost-derive", +] + +[[package]] +name = "prost-build" +version = "0.14.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "03da047801ff44bb6a4d407d4860c05fd70bb81714e6b2f3812603d5b145b042" +dependencies = [ + "heck", + "itertools 0.13.0", + "log", + "multimap", + "petgraph", + "prettyplease", + "prost", + "prost-types", + "regex", + "syn 2.0.119", + "tempfile", +] + +[[package]] +name = "prost-derive" +version = "0.14.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b570b25f7617e43d59005d0990ccb79e950a423952cea19671b7a876da390adf" +dependencies = [ + "anyhow", + "itertools 0.13.0", + "proc-macro2", + "quote", + "syn 2.0.119", +] + +[[package]] +name = "prost-types" +version = "0.14.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f94967dc7688f3054c7fac87473ffae4cc4c3904800e2d9f5b857246d8963b0a" +dependencies = [ + "prost", +] + [[package]] name = "pulldown-cmark" version = "0.13.5" @@ -4393,6 +4545,20 @@ dependencies = [ "bytemuck", ] +[[package]] +name = "ring" +version = "0.17.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4689e6c2294d81e88dc6261c768b63bc4fcdb852be6d1352498b114f61383b7" +dependencies = [ + "cc", + "cfg-if", + "getrandom 0.2.17", + "libc", + "untrusted", + "windows-sys 0.52.0", +] + [[package]] name = "rowan" version = "0.17.0" @@ -4489,6 +4655,42 @@ dependencies = [ "windows-sys 0.61.2", ] +[[package]] +name = "rustls" +version = "0.23.46" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "48e13bd8c0e9365c43cfa5c9e8f9ad49d3c8444926c9aac819e0e4dc503c8fdf" +dependencies = [ + "aws-lc-rs", + "log", + "once_cell", + "rustls-pki-types", + "rustls-webpki", + "subtle", + "zeroize", +] + +[[package]] +name = "rustls-pki-types" +version = "1.15.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2f4925028c7eb5d1fcdaf196971378ed9d2c1c4efc7dc5d011256f76c99c0a96" +dependencies = [ + "zeroize", +] + +[[package]] +name = "rustls-webpki" +version = "0.103.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4443b1b6d39d7c663139942cc4558262562d2c84123bb6282ef177009ca8b281" +dependencies = [ + "aws-lc-rs", + "ring", + "rustls-pki-types", + "untrusted", +] + [[package]] name = "rustversion" version = "1.0.23" @@ -5268,6 +5470,16 @@ dependencies = [ "syn 3.0.9", ] +[[package]] +name = "tokio-rustls" +version = "0.26.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c9cc2678c2cdd569ef8215e2afd7954ada2ae20b4fdd2c5fe6139a3b02d105db" +dependencies = [ + "rustls", + "tokio", +] + [[package]] name = "toml" version = "1.1.8+spec-1.1.0" @@ -5493,6 +5705,12 @@ dependencies = [ "ctutils", ] +[[package]] +name = "untrusted" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ecb6da28b8a351d773b68d5825ac39017e680750f980f3a1a85cd8dd28a47c1" + [[package]] name = "url" version = "2.5.8" @@ -5581,14 +5799,19 @@ dependencies = [ "ed25519-dalek", "gstreamer", "gstreamer-app", + "gstreamer-sdp", "gstreamer-video", + "gstreamer-webrtc", "hex", "hkdf", "image", "nnnoiseless", "opus", + "prost", + "prost-build", "rand", "rfd", + "rustls", "serde", "serde_bytes", "serde_json", @@ -5597,10 +5820,12 @@ dependencies = [ "slint-build", "sys-locale", "tokio", + "tokio-rustls", "tracing", "tracing-subscriber", "typed-index-collections", "uuid", + "webpki-roots", "x25519-dalek", "zbus", ] @@ -5854,6 +6079,15 @@ dependencies = [ "web-sys", ] +[[package]] +name = "webpki-roots" +version = "1.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f5f280bbb2b287bc8e7de7e3852945a0ff50d1bbcb5ed42b6c7a568c546df171" +dependencies = [ + "rustls-pki-types", +] + [[package]] name = "weezl" version = "0.1.12" diff --git a/Cargo.toml b/Cargo.toml index 8b6b5c7..404f652 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,11 +26,16 @@ name = "vnox-e2e-voice" path = "src/bin/e2e_voice.rs" [build-dependencies] +prost-build = "0.14" slint-build = "=1.18.1" # Compatible compiler utility before its unused bincode integration was added. typed-index-collections = "=3.2.3" [dependencies] +prost = "0.14" +tokio-rustls = "0.26" +rustls = "0.23" +webpki-roots = "1" tokio = { version = "1.54", features = ["full"] } serde = { version = "1", features = ["derive"] } serde_json = "1" @@ -61,13 +66,15 @@ rfd = { version = "0.17.2", default-features = false, features = default = ["screen-capture"] rnnoise = ["dep:nnnoiseless"] -screen-capture = ["dep:zbus", "dep:gstreamer", "dep:gstreamer-app", "dep:gstreamer-video"] +screen-capture = ["dep:zbus", "dep:gstreamer", "dep:gstreamer-app", "dep:gstreamer-video", "dep:gstreamer-webrtc", "dep:gstreamer-sdp"] [target.'cfg(target_os = "linux")'.dependencies] zbus = { version = "6.1", optional = true } gstreamer = { version = "0.25.2", optional = true } gstreamer-app = { version = "0.25.2", optional = true } gstreamer-video = { version = "0.25.2", optional = true } +gstreamer-webrtc = { version = "0.25.4", optional = true } +gstreamer-sdp = { version = "0.25.3", optional = true } # Compatibility backports to maintained zbus 6; see patches/README.md. [patch.crates-io] diff --git a/build.rs b/build.rs index 9b913e1..c77ab99 100644 --- a/build.rs +++ b/build.rs @@ -1,4 +1,10 @@ fn main() { + // Use the checked-in schema so this repository builds independently of the server checkout. + prost_build::Config::new() + .type_attribute(".", "#[derive(serde::Serialize, serde::Deserialize)]") + .compile_protos(&["protocol/lnex.proto"], &["protocol"]) + .expect("compile canonical LNEx protocol"); + println!("cargo:rerun-if-changed=protocol/lnex.proto"); let config = slint_build::CompilerConfiguration::new() .with_style("fluent".into()) .with_default_translation_context(slint_build::DefaultTranslationContext::None) diff --git a/docs/04-clients/screen-capture.md b/docs/04-clients/screen-capture.md index 3fc7154..399a0d9 100644 --- a/docs/04-clients/screen-capture.md +++ b/docs/04-clients/screen-capture.md @@ -24,26 +24,45 @@ before capture. `target-object`, `node.dont-fallback`, `node.dont-reconnect`, stream, microphone or default output. The UI displays actual captured audio level. Session teardown stops both pipelines before closing the portal FD. -**This is local preview, not broadcasting.** The existing client/server media -protocol does not yet negotiate separate video/application-audio tracks, codec -capabilities or recipient subscriptions. No frames are sent to other members. -The next integration must use established WebRTC transport with authenticated -signalling, independent tracks, backpressure and participant authorization in the -combined server daemon. Do not send video through custom UDP fragments or reuse -the microphone encryption/packet format without a documented protocol change. -Windows process-loopback and macOS adapters are not implemented; unsupported -builds report that capability rather than recording a different source. +## Broadcast and watch + +After the local preview is visible, **Start broadcast** explicitly announces it +in the authenticated voice channel. The server checks current voice/guild +membership and voice-speaking permissions. Other members choose **Watch +broadcast**; receiving never starts automatically. A separate native viewer +renders actual decoded frames and can mute only the broadcast. Closing the +viewer revokes its subscription and clears queued samples. Broadcast audio uses +the same selected CPAL output device and deafen/volume controls as voice; it does +not silently open the default output. Stereo application audio stays stereo. + +GStreamer `webrtcbin` negotiates ICE + DTLS-SRTP, VP8 video and independent Opus +tracks. Authenticated LNEx protobuf carries only bounded SDP/ICE signalling. +One publisher has at most four viewers; packets never use custom crypto or video +fragments. Existing voice remains an independent microphone lane by default, +respecting mute/PTT/VAD; broadcasting does not duplicate it. The transport also +supports an explicitly enabled separate microphone track through the gated, +bounded existing-capture tap for integrations that need it. + +The default uses host ICE candidates, suitable for reachable LAN/public-address +peers. Administrators may explicitly set `VNOX_STUN_SERVER=stun://host:port` and +`VNOX_TURN_SERVER=turns://user:password@host:port` in the client environment. +Some NATs require TURN; no public STUN server, third-party relay or system-audio +fallback is silently selected. ICE failure times out visibly. TURN credentials +are configuration secrets and must not be committed. Windows process-loopback +and macOS capture adapters remain unsupported; those builds report capability +rather than recording a different source. ## Dependencies and verification `screen-capture` is enabled by default. A build without it remains available with `cargo run --no-default-features`. Linux needs GStreamer core/base development -packages and the PipeWire GStreamer plugin (`gst-plugin-pipewire` on Arch; +packages the GStreamer WebRTC, libnice, VP8/Opus plugins and the PipeWire GStreamer plugin (`gst-plugin-pipewire` on Arch; `gstreamer1.0-pipewire` on Debian/Ubuntu), a ScreenCast-capable portal backend, PipeWire and `pw-dump`. Installation must not rewrite user audio configuration. ```sh -cargo test --lib media:: # Synthetic video/audio; no desktop recording +cargo test --lib media:: # Includes actual two-peer VP8 + separate Opus delivery +VNOX_GATEWAY_BIN=/path/to/vnox-serverd cargo test --lib media::gateway_tests -- --ignored python3 scripts/check-selected-audio.py # Temporary null sink + synthetic app, checks disconnect cargo run --bin screen_probe -- --list-audio # Metadata only cargo run --bin screen_probe -- --choose # Explicit portal consent, 15s local capture, no files @@ -52,7 +71,9 @@ cargo run --bin screen_probe -- --choose # Explicit portal consent, 15s loc The Python integration check requires `pactl`, `gst-launch-1.0`, the GStreamer `level` element and a running session manager. It creates and removes only its own temporary null sink; it does not change the default device. Manual consent -and real compositor preview still require an interactive desktop check. +and real compositor preview still require an interactive desktop check. The +two-peer transport fixture uses only synthetic video and distinct 440/880 Hz +audio, decodes both independently, and never opens microphone/output hardware. Primary contracts: [ScreenCast portal](https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.ScreenCast.html), [PipeWire properties](https://docs.pipewire.org/group__pw__keys.html), diff --git a/docs/maintenance/protocol-interop.md b/docs/maintenance/protocol-interop.md new file mode 100644 index 0000000..442ca77 --- /dev/null +++ b/docs/maintenance/protocol-interop.md @@ -0,0 +1,19 @@ +# Native client and gateway interoperability + +The client uses LNEx v2 protobuf payloads generated from `protocol/lnex.proto`. This is a verbatim snapshot of the gateway's canonical schema; run `python3 scripts/sync-protocol.py --check` with the adjacent server checkout, or `--server /path/to/lnex.proto`. The client builds independently using its checked-in snapshot. Internal payload aliases exist for application compatibility, but no JSON is sent on LNEx. + +Remote addresses use verified TLS 1.3, including certificate chain and hostname checks. Use `lnexs://host:7600` explicitly; bare remote hosts also use TLS. Development plaintext is restricted to actual connected loopback peers (`127.0.0.1:7600`, `[::1]:7600`, or explicit `lnex://localhost:7600`). Certificate failures never trigger a plaintext fallback. Self-signed remote certificates need a separately designed explicit trust workflow; verification is never bypassed. + +The v2 handshake verifies the server's signed HELLO and binds the client's identity signature to the complete HELLO and AUTH transcript, including both ephemeral X25519 keys. Control encryption retains ChaCha20-Poly1305 and HKDF-SHA256 with the v2 domain; it authenticates the entire 12-byte header. Sequence numbers are checked in order and never wrap. A dedicated bounded reader keeps partial frames intact while other commands and timers run and is cancelled when its session closes. + +Voice receives independent random credentials through encrypted control (`VoiceSession`, `0x0012`). UDP authenticates its full 28-byte header, explicit 64-bit sequence and protobuf frame, uses direction-specific keys, and permits bounded reordering without accepting duplicates. The receiver checks the selected relay endpoint and authenticated user/channel attribution. Empty encrypted Opus frames register the source and maintain it every five seconds. Leaving or disconnecting drops credentials and stops the receiver task. This replaces the incompatible legacy encrypted-guess-sequence client/plaintext relay combination. + +Run focused verification: + +```sh +cargo test --lib --no-default-features +VNOX_GATEWAY_BIN=/path/to/current/vnox-gateway cargo test --test gateway_interop --no-default-features -- --ignored +cargo run --bin vnox-e2e-voice --no-default-features -- 127.0.0.1:7600 +``` + +The ignored interoperability test launches its own loopback gateway and temporary database using an in-memory identity, without reading or writing the user's profile. The voice smoke command requires the combined `vnox-serverd` daemon so gateway credentials reach the UDP relay through the in-process bridge. diff --git a/protocol/lnex.proto b/protocol/lnex.proto new file mode 100644 index 0000000..3b18f62 --- /dev/null +++ b/protocol/lnex.proto @@ -0,0 +1,461 @@ +syntax = "proto3"; +package lnex; + +// ─── HANDSHAKE ────────────────────────────────────────────────────────────── + +message Hello { + string lnex_version = 1; + bytes server_pubkey = 2; + bytes challenge_nonce = 3; + string node_name = 4; + bytes server_eph_pubkey = 5; + bool private_mode = 6; + bytes server_signature = 7; +} + +message Auth { + bytes client_pubkey = 1; + string nickname = 2; + string lnex_version = 3; + bytes signature = 4; + bytes client_eph_pubkey = 5; +} + +message Session { + string session_id = 1; + string token = 2; + int64 expires_at = 3; +} + +// ─── KEEPALIVE ────────────────────────────────────────────────────────────── + +message Ping { int64 timestamp = 1; } +message Pong { int64 timestamp = 1; } + +// ─── ERROR / DISCONNECT ───────────────────────────────────────────────────── + +message Error { uint32 code = 1; string message = 2; } +message Disconnect { string reason = 1; } + +// ─── CHANNELS ─────────────────────────────────────────────────────────────── + +message JoinChannel { string channel_id = 1; } +message LeaveChannel { string channel_id = 1; } + +message ChannelState { + string channel_id = 1; + string channel_name = 2; + string kind = 3; + repeated MemberInfo members = 4; + string voice_endpoint = 5; + optional string guild_id = 6; +} + +message MemberInfo { + string user_id = 1; + string nickname = 2; + bool in_voice = 3; +} + +message ChannelCreate { + string channel_id = 1; + string channel_name = 2; + string kind = 3; + optional string guild_id = 4; +} + +message ChannelDelete { string channel_id = 1; } + +message ChannelEdit { + string channel_id = 1; + string channel_name = 2; +} + +message ChannelList { + repeated ChannelListItem channels = 1; + optional string guild_id = 2; +} + +message ChannelListItem { + string channel_id = 1; + string channel_name = 2; + string kind = 3; + optional string guild_id = 4; +} + +message UserJoin { string channel_id = 1; string user_id = 2; string nickname = 3; } +message UserLeave { string channel_id = 1; string user_id = 2; } + +// ─── CHAT ──────────────────────────────────────────────────────────────────── + +message ChatMessage { + string message_id = 1; + string channel_id = 2; + string sender_id = 3; + string content = 4; + int64 timestamp = 5; + bool edited = 6; + optional string reply_to = 7; +} + +message ChatHistory { + string channel_id = 1; + repeated ChatMessage messages = 2; +} + +message ReactionPayload { + string message_id = 1; + string channel_id = 2; + string emoji = 3; + string user_id = 4; +} + +message MessageEdit { + string message_id = 1; + string channel_id = 2; + string content = 3; +} + +message MessageDelete { + string message_id = 1; + string channel_id = 2; +} + +// ─── VOICE ────────────────────────────────────────────────────────────────── + +message VoiceState { + string user_id = 1; + string channel_id = 2; + string state = 3; +} + +// ─── GUILDS ───────────────────────────────────────────────────────────────── + +message GuildCreate { string name = 1; } + +message GuildInfo { + string id = 1; + string owner_id = 2; + string name = 3; + int64 member_count = 4; + int64 created_at = 5; +} + +message GuildDelete { string guild_id = 1; } +message GuildList { repeated GuildInfo guilds = 1; } + +message GuildMemberJoin { string guild_id = 1; } +message GuildMemberLeave { string guild_id = 1; string user_id = 2; } +message GuildMemberKick { string guild_id = 1; string user_id = 2; } + +message RoleCreate { + string guild_id = 1; + string name = 2; + optional string color = 3; + optional uint64 permissions = 4; + string id = 5; +} + +message RoleDelete { + string guild_id = 1; + string role_id = 2; +} + +message InviteCreate { + string guild_id = 1; + optional int64 max_uses = 2; + optional int64 expires_in_seconds = 3; +} + +message InviteInfo { + string id = 1; + string guild_id = 2; + string guild_name = 3; + string code = 4; + string creator_id = 5; + optional int64 max_uses = 6; + int64 uses = 7; + optional int64 expires_at = 8; + int64 created_at = 9; +} + +message InviteAccept { + string code = 1; + string guild_id = 2; + string guild_name = 3; +} + +message InviteDelete { + string guild_id = 1; + string invite_id = 2; +} + +message GuildAuditLogFetch { + string guild_id = 1; + int64 limit = 2; +} + +message AuditLogEntry { + string id = 1; + string guild_id = 2; + string actor_id = 3; + string action = 4; + optional string target_id = 5; + optional string target_type = 6; + optional string reason = 7; + int64 created_at = 8; +} + +message GuildAuditLog { + string guild_id = 1; + repeated AuditLogEntry entries = 2; +} + +message GuildMemberListFetch { string guild_id = 1; } + +message GuildMemberInfo { + string user_id = 1; + string nickname = 2; + int64 joined_at = 3; + string role_color = 4; + string role_name = 5; + bool is_owner = 6; +} + +message GuildMemberList { + string guild_id = 1; + repeated GuildMemberInfo members = 2; +} + +message RoleAssign { + string guild_id = 1; + string user_id = 2; + string role_id = 3; +} + +message GuildRoleListFetch { string guild_id = 1; } + +message GuildRoleInfo { + string id = 1; + string guild_id = 2; + string name = 3; + string color = 4; + uint64 permissions = 5; + int32 position = 6; +} + +message GuildRoleList { + string guild_id = 1; + repeated GuildRoleInfo roles = 2; +} + +// ─── DIRECT MESSAGES ──────────────────────────────────────────────────────── + +message DmStart { + string target_user_id = 1; +} + +message DmStartResponse { + string dm_id = 1; + string other_user_id = 2; + string other_nickname = 3; + repeated DmMessage messages = 4; + uint32 unread_count = 5; +} + +message DmMessage { + string dm_id = 1; + string sender_id = 2; + string content = 3; + int64 timestamp = 4; +} + +message DmHistory { + string dm_id = 1; + repeated DmMessage messages = 2; + optional string search_query = 3; + optional int64 limit = 4; +} + +message DmReadAck { + string dm_id = 1; +} + +// ─── FRIENDS ──────────────────────────────────────────────────────────────── + +message FriendRequest { string to_user_id = 1; } +message FriendAccept { string from_user_id = 1; } +message FriendDecline { string from_user_id = 1; } +message FriendRemove { string user_id = 1; } + +message FriendInfo { + string user_id = 1; + string nickname = 2; + string status = 3; + int64 since = 4; +} + +message FriendList { repeated FriendInfo friends = 1; } + +message BlockUser { string user_id = 1; } +message UnblockUser { string user_id = 1; } +message BlockList { repeated string blocked = 1; } + +message FriendEvent { + string event = 1; + string user_id = 2; + optional string nickname = 3; +} + +// ─── PRESENCE ────────────────────────────────────────────────────────────── + +message PresenceUpdate { + string status = 1; + optional string activity_type = 2; + optional string activity_text = 3; +} + +message PresenceInfo { + string user_id = 1; + string nickname = 2; + string status = 3; + optional string activity_type = 4; + optional string activity_text = 5; +} + +message PresenceSync { repeated PresenceInfo presences = 1; } + +message PresenceEvent { + string user_id = 1; + string nickname = 2; + string status = 3; + optional string activity_type = 4; + optional string activity_text = 5; +} + +// ─── READ RECEIPT & TYPING ───────────────────────────────────────────────── + +message ReadReceipt { + string channel_id = 1; + string last_read_message_id = 2; + string user_id = 3; +} + +message TypingStart { + string channel_id = 1; +} + +// ─── AD-HOC RESPONSE / BROADCAST TYPES ────────────────────────────────────── + +message ReadReceiptBroadcast { + string channel_id = 1; + string user_id = 2; + string last_read_message_id = 3; +} + +message UserRoleUpdate { + string user_id = 1; + string guild_id = 2; + string color = 3; +} + +message TypingBroadcast { + string user_id = 1; + string nickname = 2; + string channel_id = 3; +} + +message SimpleResponse { + bool ok = 1; + optional string user_id = 2; + optional string role_id = 3; +} + +// ─── E2EE for Direct Messages ──────────────────────────────────────────────── + +message E2eeDmKeyExchange { + string dm_id = 1; + bytes e2ee_public_key = 2; +} + +message E2eeDmKeyExchangeAck { + string dm_id = 1; +} + +message E2eeDmMessage { + string dm_id = 1; + string sender_id = 2; + bytes ciphertext = 3; + int64 timestamp = 4; +} + +message E2eeDmHistory { + string dm_id = 1; + repeated E2eeDmMessage messages = 2; + optional int64 limit = 3; +} + +// Channel-scoped webhook management: authenticated LNEx only. Secrets returned once. +message WebhookRequest { + string id = 1; + string channel_id = 2; + string name = 3; + string direction = 4; // incoming | outgoing + string provider = 5; // generic | github (incoming only) + string endpoint = 6; // outgoing HTTPS URL + string request_id = 7; +} +message WebhookInfo { + string id = 1; + string channel_id = 2; + string name = 3; + string direction = 4; + string provider = 5; + string endpoint = 6; + int64 created_at = 7; + bool revoked = 8; + string suspended_reason = 9; +} +message WebhookResult { WebhookInfo hook = 1; string secret = 2; string request_id = 3; } +message WebhookListResult { repeated WebhookInfo hooks = 1; string request_id = 2; } +message WebhookErrorResult { string request_id = 1; uint32 code = 2; string message = 3; } + +// Authenticated signalling only; media uses DTLS/SRTP peer connections. +message MediaControl { + uint32 action = 1; // START1 STOP2 WATCH3 UNWATCH4 OFFER5 ANSWER6 ICE7 + string channel_id = 2; + string share_id = 3; + string target_session_id = 4; + string body = 5; + uint32 mline_index = 6; + bool application_audio = 7; + bool microphone = 8; +} +message MediaEvent { + uint32 action = 1; + string channel_id = 2; + string share_id = 3; + string target_session_id = 4; + string body = 5; + uint32 mline_index = 6; + bool application_audio = 7; + bool microphone = 8; + string sender_session_id = 9; + string publisher_user_id = 10; +} + +// Fresh credentials delivered only over the authenticated encrypted control session. +message VoiceSession { + bytes route_id = 1; + bytes c2s_key = 2; + bytes s2c_key = 3; + string channel_id = 4; +} +message VoiceFrame { + uint32 voice_seq = 1; + uint32 timestamp = 2; + string channel_id = 3; + string sender_session_id = 4; + bytes opus = 5; + string sender_user_id = 6; +} diff --git a/scripts/sync-protocol.py b/scripts/sync-protocol.py new file mode 100644 index 0000000..368c598 --- /dev/null +++ b/scripts/sync-protocol.py @@ -0,0 +1,14 @@ +#!/usr/bin/env python3 +"""Synchronize the canonical gateway protobuf schema; --check rejects drift.""" +import argparse +from pathlib import Path +parser=argparse.ArgumentParser(description=__doc__) +parser.add_argument('--check',action='store_true') +parser.add_argument('--server',type=Path,default=Path(__file__).resolve().parents[2]/'VNOX-Server/protocol/lnex.proto') +args=parser.parse_args() +target=Path(__file__).resolve().parents[1]/'protocol/lnex.proto' +source=args.server.read_bytes() +if args.check: + if target.read_bytes()!=source: parser.exit(1,'Client protocol differs from the authoritative gateway schema. Run scripts/sync-protocol.py.\n') +else: target.write_bytes(source) +print('LNEx protobuf schema matches the gateway') diff --git a/src/audio/mod.rs b/src/audio/mod.rs index 6fbd2f8..249cced 100644 --- a/src/audio/mod.rs +++ b/src/audio/mod.rs @@ -4,8 +4,8 @@ mod pipeline; mod processing; pub use devices::{DeviceLabel, DeviceLists}; -pub use pipeline::AudioPipeline; pub use pipeline::JitterConfig; +pub use pipeline::{AudioPipeline, MediaMixer}; pub use processing::{DenoiseState, TransmitGate}; #[derive(Debug, Clone, Copy)] diff --git a/src/audio/pipeline/capture.rs b/src/audio/pipeline/capture.rs index 0a4f59c..a64615c 100644 --- a/src/audio/pipeline/capture.rs +++ b/src/audio/pipeline/capture.rs @@ -23,6 +23,7 @@ pub fn run( bitrate_bps: Arc, local_speaking: Arc, stopped: Arc, + microphone_tap: super::media::MicrophoneTap, ) -> Result<()> { let mut encoder = opus::Encoder::new(OPUS_RATE, opus::Channels::Mono, opus::Application::Voip)?; let mut last_bitrate_bps: u32 = 0; @@ -91,6 +92,7 @@ pub fn run( continue; } + microphone_tap.push(&pcm); apply_bitrate(&mut encoder, &bitrate_bps, &mut last_bitrate_bps)?; let mut out = vec![0u8; MAX_OPUS]; diff --git a/src/audio/pipeline/media.rs b/src/audio/pipeline/media.rs new file mode 100644 index 0000000..0fbd919 --- /dev/null +++ b/src/audio/pipeline/media.rs @@ -0,0 +1,148 @@ +//! Bounded broadcast PCM taps. No extra microphone or output device is opened. +use crate::media::AudioFrame; +use std::{ + collections::{HashMap, VecDeque}, + sync::{Arc, Mutex}, +}; + +#[derive(Clone)] +pub struct MediaMixer { + pub(super) queues: Arc>>>, + rate: u32, +} +impl MediaMixer { + pub(super) fn new(rate: u32) -> Self { + Self { + queues: Arc::default(), + rate, + } + } + pub fn push(&self, key: &str, frame: AudioFrame) -> Result<(), String> { + frame.validate()?; + if key.len() > 128 { + return Err("Invalid broadcast mixer source".into()); + } + let channels = usize::from(frame.channels); + let left: Vec = frame + .samples + .chunks(channels) + .map(|frame| frame[0]) + .collect(); + let right: Vec = frame + .samples + .chunks(channels) + .map(|frame| frame[channels - 1]) + .collect(); + let (left, right) = if self.rate == frame.rate { + (left, right) + } else { + ( + crate::audio::config::resample_rate(&left, frame.rate, self.rate), + crate::audio::config::resample_rate(&right, frame.rate, self.rate), + ) + }; + let samples = left + .into_iter() + .zip(right) + .map(|(left, right)| [left, right]); + let mut queues = self + .queues + .lock() + .map_err(|_| "Broadcast mixer unavailable")?; + if queues.len() >= 8 && !queues.contains_key(key) { + return Err("Too many broadcast audio sources".into()); + } + let queue = queues.entry(key.into()).or_default(); + let limit = self.rate as usize / 2; + queue.extend(samples); + if queue.len() > limit { + let excess = queue.len() - limit; + queue.drain(..excess); + } + Ok(()) + } + pub fn clear(&self) { + if let Ok(mut queues) = self.queues.lock() { + queues.clear(); + } + } + pub fn remove(&self, key: &str) { + if let Ok(mut queues) = self.queues.lock() { + queues.remove(key); + } + } +} + +#[derive(Clone, Default)] +pub(super) struct MicrophoneTap(Arc>); +#[derive(Default)] +struct TapState { + enabled: bool, + queue: VecDeque, +} +impl MicrophoneTap { + pub fn enable(&self, enabled: bool) { + if let Ok(mut state) = self.0.lock() { + if state.enabled != enabled { + state.queue.clear(); + } + state.enabled = enabled; + } + } + pub fn push(&self, samples: &[f32]) { + if let Ok(mut state) = self.0.lock() + && state.enabled + { + if state.queue.len() == 8 { + state.queue.pop_front(); + } + state.queue.push_back(AudioFrame { + samples: samples.to_vec(), + rate: 48_000, + channels: 1, + }); + } + } + pub fn pop(&self) -> Option { + let mut state = self.0.lock().ok()?; + if state.enabled { + state.queue.pop_front() + } else { + state.queue.clear(); + None + } + } +} +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn mute_clears_microphone_and_mixer_queues_remain_bounded() { + let tap = MicrophoneTap::default(); + tap.push(&[0.5; 960]); + assert!(tap.pop().is_none()); + tap.enable(true); + tap.push(&[0.5; 960]); + assert!(tap.pop().is_some()); + tap.push(&[0.5; 960]); + tap.enable(false); + tap.enable(true); + assert!(tap.pop().is_none()); + let mixer = MediaMixer::new(48_000); + for _ in 0..100 { + mixer + .push( + "fixture", + AudioFrame { + samples: vec![0.1; 1920], + rate: 48_000, + channels: 2, + }, + ) + .expect("valid PCM"); + } + assert_eq!(mixer.queues.lock().expect("queue")["fixture"].len(), 24_000); + mixer.clear(); + assert!(mixer.queues.lock().expect("queue").is_empty()); + } +} diff --git a/src/audio/pipeline/mod.rs b/src/audio/pipeline/mod.rs index d02fe92..1bcdf57 100644 --- a/src/audio/pipeline/mod.rs +++ b/src/audio/pipeline/mod.rs @@ -1,4 +1,6 @@ mod capture; +mod media; +pub use media::MediaMixer; mod playback; use cpal::traits::DeviceTrait; @@ -19,6 +21,8 @@ pub use playback::JitterConfig; pub struct AudioPipeline { stopped: Arc, encoded_rx: Receiver, + microphone_tap: media::MicrophoneTap, + media_mixer: MediaMixer, voice_tx: Sender, _encoded_tx: Sender, gate: Arc>, @@ -35,10 +39,22 @@ impl Drop for AudioPipeline { fn drop(&mut self) { // Releasing a voice session must release its microphone even while muted. self.stopped.store(true, Ordering::Release); + self.microphone_tap.enable(false); + self.media_mixer.clear(); } } impl AudioPipeline { + pub fn enable_media_microphone(&self, enabled: bool) { + self.microphone_tap.enable(enabled); + } + pub fn poll_media_microphone(&self) -> Option { + self.microphone_tap.pop() + } + pub fn media_mixer(&self) -> MediaMixer { + self.media_mixer.clone() + } + pub fn poll_encoded(&self) -> Option { self.encoded_rx.try_recv().ok() } @@ -111,6 +127,10 @@ pub fn start(cfg: PipelineConfig) -> anyhow::Result { let in_cfg = config::AudioConfig::find(&input, true)?; let out_cfg = config::AudioConfig::find(&output, false)?; let frame_ms = config::frame_ms_from_interval(cfg.packet_interval); + let microphone_tap = media::MicrophoneTap::default(); + let tap_capture = microphone_tap.clone(); + let media_mixer = MediaMixer::new(out_cfg.sample_rate); + let mixer_playback = media_mixer.clone(); let (encoded_tx, encoded_rx) = mpsc::channel::(); let (voice_tx, voice_rx) = mpsc::channel::(); @@ -148,6 +168,7 @@ pub fn start(cfg: PipelineConfig) -> anyhow::Result { bitrate_cap, local_speaking_cap, capture_stopped, + tap_capture, ) { tracing::error!("capture: {e}"); } @@ -160,6 +181,7 @@ pub fn start(cfg: PipelineConfig) -> anyhow::Result { vol_play, jitter_play, last_remote_voice_play, + mixer_playback, ) { tracing::error!("playback: {e}"); } @@ -168,6 +190,8 @@ pub fn start(cfg: PipelineConfig) -> anyhow::Result { Ok(AudioPipeline { stopped, encoded_rx, + microphone_tap, + media_mixer, voice_tx, _encoded_tx: encoded_tx, gate, diff --git a/src/audio/pipeline/playback.rs b/src/audio/pipeline/playback.rs index 0c25320..2d9291a 100644 --- a/src/audio/pipeline/playback.rs +++ b/src/audio/pipeline/playback.rs @@ -18,6 +18,7 @@ pub struct JitterConfig { pub adaptive: bool, } +#[allow(clippy::too_many_arguments)] pub fn run( device: cpal::Device, cfg: AudioConfig, @@ -25,6 +26,7 @@ pub fn run( output_volume: Arc>, jitter_cfg: Arc>, last_remote_voice_ms: Arc, + media_mixer: super::media::MediaMixer, ) -> Result<()> { let mut decoder = opus::Decoder::new(OPUS_RATE, opus::Channels::Mono)?; let pb: Arc>> = Arc::new(Mutex::new(Vec::new())); @@ -34,6 +36,7 @@ pub fn run( let needs_resample = cfg.sample_rate != OPUS_RATE; let opus_frame = config::frame_samples(OPUS_RATE); + let media_queues = media_mixer.queues.clone(); let stream = device.build_output_stream( cfg.stream, move |out: &mut [f32], _| { @@ -57,6 +60,23 @@ pub fn run( let drain_n = frames.min(buf.len()); buf.drain(..drain_n); } + if let Ok(mut queues) = media_queues.lock() { + for frame in out.chunks_mut(out_channels) { + let mut mixed = [0.0f32; 2]; + for samples in queues.values_mut().filter_map(|queue| queue.pop_front()) { + mixed[0] += samples[0]; + mixed[1] += samples[1]; + } + for (channel, sample) in frame.iter_mut().enumerate() { + let value = if out_channels == 1 || channel > 1 { + (mixed[0] + mixed[1]) * 0.5 + } else { + mixed[channel] + }; + *sample = (*sample + value * gain).clamp(-1.0, 1.0); + } + } + } }, |e| tracing::error!("output: {e}"), None, diff --git a/src/bin/e2e_voice.rs b/src/bin/e2e_voice.rs index 5973ed8..40af5bd 100644 --- a/src/bin/e2e_voice.rs +++ b/src/bin/e2e_voice.rs @@ -1,131 +1,118 @@ -//! Headless voice path smoke test: two TCP sessions, UDP relay between them. -//! -//! Requires gateway and voice-node running: -//! cargo run -p vnox-gateway -- --config dev/config.toml -//! cargo run -p vnox-voice-node -- --config dev/config.toml - -use anyhow::{Context, Result, anyhow}; -use tokio::net::{TcpStream, UdpSocket}; +//! Headless authenticated voice smoke test. Run vnox-serverd first (combined gateway+voice relay). +use anyhow::{Context, Result}; use tokio::time::{Duration, timeout}; -use tracing::info; use vnox_client::{ identity, - net::{crypto::SessionCrypto, framing, handshake, payloads::*, voice}, + net::{self, NetCommand, NetEvent, NetHandle, voice}, }; - -const GATEWAY: &str = "127.0.0.1:7600"; -const VOICE_CHANNEL: &str = "voice"; - -struct Session { - _stream: TcpStream, - _crypto: SessionCrypto, - udp: UdpSocket, - voice_endpoint: std::net::SocketAddr, +async fn event(handle: &NetHandle, predicate: impl Fn(&NetEvent) -> bool) -> Result { + timeout(Duration::from_secs(8), async { + loop { + if let Some(event) = handle.try_recv() { + if let NetEvent::Disconnected { ref reason, .. } = event { + anyhow::bail!("connection ended: {reason}"); + } + if let NetEvent::Error { code, ref message } = event { + anyhow::bail!("gateway error {code}: {message}"); + } + if predicate(&event) { + return Ok(event); + } + } + tokio::time::sleep(Duration::from_millis(5)).await; + } + }) + .await + .context("timed out waiting for network event")? } - -async fn connect_client(nickname: &str) -> Result { - let identity = identity::generate(nickname); - let mut stream = TcpStream::connect(GATEWAY).await.context("TCP connect")?; - let mut seq = 0u32; - let (_session_id, _node_name, _private_mode, crypto) = - handshake::run(&mut stream, &identity, &mut seq).await?; - - framing::io::write_encrypted( - &mut stream, - PID_JOIN_CHANNEL, - &mut seq, - &serde_json::to_vec(&JoinChannelPayload { - channel_id: VOICE_CHANNEL.into(), - })?, - &crypto, +async fn client(name: &str, address: &str) -> Result { + let handle = net::spawn(identity::generate(name)); + handle + .send(NetCommand::Connect { + address: address.into(), + auto_reconnect: false, + }) + .await; + event( + &handle, + |event| matches!(event,NetEvent::ChannelState {channel_id,..} if channel_id=="general"), ) .await?; - - let pkt = framing::io::read_encrypted(&mut stream, &crypto).await?; - if pkt.packet_id != PID_CHANNEL_STATE { - return Err(anyhow!( - "expected CHANNEL_STATE, got 0x{:04X}", - pkt.packet_id - )); - } - let state: ChannelStatePayload = serde_json::from_slice(&pkt.payload)?; - let voice_endpoint: std::net::SocketAddr = state - .voice_endpoint - .parse() - .context("parse voice_endpoint")?; - - let udp = UdpSocket::bind("0.0.0.0:0").await?; - info!( - "{nickname}: voice_endpoint={voice_endpoint} udp={}", - udp.local_addr()? - ); - - Ok(Session { - _stream: stream, - _crypto: crypto, - udp, - voice_endpoint, - }) + handle + .send(NetCommand::JoinChannel { + channel_id: "voice".into(), + }) + .await; + event( + &handle, + |event| matches!(event,NetEvent::ChannelState {channel_id,..} if channel_id=="voice"), + ) + .await?; + Ok(handle) } - #[tokio::main] async fn main() -> Result<()> { - tracing_subscriber::fmt() - .with_env_filter(std::env::var("VNOX_LOG").unwrap_or_else(|_| "info".into())) - .init(); - - let channel_key = voice::channel_key(VOICE_CHANNEL); - let marker = b"vnox-e2e-test-payload"; - - // Dummy crypto for the test packet - let dummy_shared = [0u8; 32]; - let dummy_crypto = SessionCrypto::derive(&dummy_shared, "test"); - let sender_pubkey = [0u8; 32]; - let packet = voice::build_packet(channel_key, 1, 0, marker, &dummy_crypto, 0, &sender_pubkey); - - let alice = connect_client("alice").await?; - let bob = connect_client("bob").await?; - - // Register both clients on the voice node. + let address = std::env::args() + .nth(1) + .unwrap_or_else(|| "127.0.0.1:7600".into()); + let alice = client("voice-fixture-alice", &address).await?; + let bob = client("voice-fixture-bob", &address).await?; + // The real network layer registers the current credentials with encrypted empty-Opus packets. + tokio::time::sleep(Duration::from_millis(250)).await; + let marker = b"authenticated-vnox-voice-fixture"; alice - .udp - .send_to(&packet, alice.voice_endpoint) - .await - .context("alice register")?; - bob.udp - .send_to(&packet, alice.voice_endpoint) - .await - .context("bob register")?; - tokio::time::sleep(Duration::from_millis(50)).await; - - let recv = tokio::spawn(async move { - let mut buf = vec![0u8; 1472]; - let (len, from) = bob.udp.recv_from(&mut buf).await.context("bob recv")?; - if len >= 20 && &buf[20..len] == marker { - info!("PASS: bob received relayed packet from {from} ({len} bytes)"); - Ok(()) - } else { - Err(anyhow!("unexpected UDP payload (len={len})")) - } - }); - - // Alice speaks; voice node should relay to bob. + .send(NetCommand::SendVoice { + channel_id: voice::channel_key("voice"), + voice_seq: 1, + timestamp: 48000, + opus_data: marker.to_vec(), + }) + .await; + event(&bob,|event|matches!(event,NetEvent::VoicePacket {opus_data,sender_id,..} if opus_data==marker && !sender_id.is_empty())).await?; alice - .udp - .send_to(&packet, alice.voice_endpoint) - .await - .context("alice send")?; - - info!( - "sent voice packet to {} (channel_key={channel_key})", - alice.voice_endpoint - ); - - timeout(Duration::from_secs(5), recv) - .await - .context("timed out waiting for voice relay")? - .context("voice recv task failed")??; - - info!("e2e voice test passed"); + .send(NetCommand::JoinChannel { + channel_id: "general".into(), + }) + .await; + alice + .send(NetCommand::LeaveChannel { + channel_id: "general".into(), + }) + .await; + // A control response synchronizes both commands before testing the retained voice route. + alice.send(NetCommand::ChannelList).await; + event(&alice, |event| { + matches!(event, NetEvent::ChannelListEvent { .. }) + }) + .await?; + let second = b"voice-retained-after-text-navigation"; + bob.send(NetCommand::SendVoice { + channel_id: voice::channel_key("voice"), + voice_seq: 2, + timestamp: 96000, + opus_data: second.to_vec(), + }) + .await; + event( + &alice, + |event| matches!(event,NetEvent::VoicePacket {opus_data,..} if opus_data==second), + ) + .await?; + alice + .send(NetCommand::SendVoice { + channel_id: voice::channel_key("voice"), + voice_seq: 3, + timestamp: 144000, + opus_data: second.to_vec(), + }) + .await; + event( + &bob, + |event| matches!(event,NetEvent::VoicePacket {opus_data,..} if opus_data==second), + ) + .await?; + alice.send(NetCommand::Disconnect).await; + bob.send(NetCommand::Disconnect).await; + println!("Authenticated v2 UDP voice relay passed"); Ok(()) } diff --git a/src/lib.rs b/src/lib.rs index a390782..4c45138 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,3 +6,5 @@ pub mod jitter; pub mod net; pub mod media; + +pub mod webhooks; diff --git a/src/main.rs b/src/main.rs index ef4004c..335b435 100644 --- a/src/main.rs +++ b/src/main.rs @@ -19,6 +19,8 @@ use vnox_client::net::NetHandle; use crate::ui::state::{ConnState, UiState}; pub struct AppState { + pub(crate) webhooks: Option>, + pub(crate) connection_generation: u64, pub(crate) identity: identity::Identity, pub(crate) net: NetHandle, pub(crate) ui: UiState, @@ -46,6 +48,8 @@ fn main() -> Result<()> { let net = net::spawn(identity.clone()); let state = Rc::new(RefCell::new(AppState { + webhooks: None, + connection_generation: 0, identity, net, ui: UiState::default(), @@ -67,8 +71,17 @@ fn main() -> Result<()> { window.set_address_input(state.borrow().ui.connect_input.as_str().into()); refresh_devices(&window); + let webhook_net = state.borrow().net.clone(); + state.borrow_mut().webhooks = Some(Rc::new(ui::webhooks::WebhookUi::new(Rc::new( + move |command| { + let net = webhook_net.clone(); + tokio::spawn(async move { + net.send(command).await; + }); + }, + ))?)); wire_signals(&window, &state); - ui::media::wire(&window)?; + ui::media::wire(&window, &state)?; let timer = slint::Timer::default(); { @@ -150,6 +163,34 @@ fn refresh_devices(window: &MainWindow) { } fn wire_signals(window: &MainWindow, state: &Rc>) { + { + let state = state.clone(); + let weak = window.as_weak(); + window.on_open_webhooks(move || { + let s = state.borrow(); + let Some(window) = weak.upgrade() else { return }; + if let Some(webhooks) = &s.webhooks { + let channel = + s.ui.active_channel + .as_deref() + .and_then(|id| s.ui.channels.iter().find(|c| c.id == id)); + let scope = vnox_client::webhooks::Scope { + connection: s.connection_generation, + channel_id: channel.map(|c| c.id.clone()).unwrap_or_default(), + channel_name: channel.map(|c| c.name.clone()).unwrap_or_default(), + guild_id: channel.and_then(|c| c.guild_id.clone()).unwrap_or_default(), + }; + if let Err(error) = webhooks.open( + &window, + scope, + matches!(s.ui.conn, ConnState::Connected { .. }), + ) { + tracing::warn!(%error,"Cannot open webhooks"); + } + } + }); + } + crate::ui::appearance::wire(window, state, !cfg!(test)); ui::i18n::wire(window, !cfg!(test)); crate::ui::avatar::wire(window, state, !cfg!(test)); diff --git a/src/media/broadcast.rs b/src/media/broadcast.rs new file mode 100644 index 0000000..6cbbeec --- /dev/null +++ b/src/media/broadcast.rs @@ -0,0 +1,575 @@ +//! UI-independent broadcast controller. User actions enqueue authenticated LNEx controls. +use super::{ + AudioFrame, CaptureFeed, VideoFrame, + peer::{AudioTrack, IceServers, Peer, RemoteFeed, Signal, Tracks}, +}; +use crate::net::wire::{MediaControl, MediaEvent}; +use std::{ + collections::{HashMap, VecDeque}, + sync::Arc, +}; +use tokio::sync::broadcast; + +const START: u32 = 1; +const STOP: u32 = 2; +const WATCH: u32 = 3; +const UNWATCH: u32 = 4; +const OFFER: u32 = 5; +const ANSWER: u32 = 6; +const ICE: u32 = 7; +#[derive(Clone, Debug)] +pub struct AvailableShare { + pub id: String, + pub publisher_session: String, + pub publisher_user: String, + pub application_audio: bool, + pub microphone: bool, +} +struct Publisher { + id: String, + tracks: Tracks, + acknowledged: bool, + started: std::time::Instant, + video: broadcast::Receiver>, + audio: broadcast::Receiver>, + latest: Option>, + peers: HashMap, +} +struct Viewer { + share: AvailableShare, + peer: Option, + pending_ice: Vec<(u32, String)>, + started: std::time::Instant, +} +#[derive(Default)] +pub struct BroadcastManager { + session: String, + channel: Option, + available: HashMap, + publisher: Option, + viewers: HashMap, + outgoing: VecDeque, + errors: VecDeque, + ice: IceServers, +} +impl BroadcastManager { + pub fn configure_ice(&mut self, ice: IceServers) -> Result<(), String> { + ice.validate()?; + if self.publisher.is_some() || !self.viewers.is_empty() { + return Err("Stop broadcasts before changing ICE services".into()); + } + self.ice = ice; + Ok(()) + } + /// Called after authenticated session/voice membership changes; revokes local peers first. + pub fn set_scope(&mut self, session: &str, channel: Option<&str>) { + if self.session == session && self.channel.as_deref() == channel { + return; + } + self.stop(); + self.outgoing.clear(); + self.available.clear(); + self.session = session.into(); + self.channel = channel.map(str::to_owned); + if self.channel.is_some() { + self.enqueue(WATCH, "", "", "", 0, Tracks::default()); + } + } + /// Explicit user publication only. A portal preview alone never calls this method. + pub fn publish(&mut self, feed: &CaptureFeed, tracks: Tracks) -> Result { + if self.channel.is_none() || self.session.is_empty() { + return Err("Join a voice channel before broadcasting".into()); + } + if self.publisher.is_some() { + return Err("A broadcast is already active".into()); + } + if self.outgoing.len() >= 120 { + return Err( + "Media signalling congested; wait before starting another broadcast".into(), + ); + } + super::peer::check_backend()?; + let id = uuid::Uuid::new_v4().to_string(); + self.publisher = Some(Publisher { + id: id.clone(), + tracks, + acknowledged: false, + started: std::time::Instant::now(), + video: feed.subscribe_video(), + audio: feed.subscribe_audio(), + latest: None, + peers: HashMap::new(), + }); + self.enqueue(START, &id, "", "", 0, tracks); + Ok(id) + } + pub fn stop_publishing(&mut self) { + if let Some(publisher) = self.publisher.take() { + self.enqueue(STOP, &publisher.id, "", "", 0, publisher.tracks); + } + } + /// Explicit receiver consent; no automatic incoming peer or speaker playback. + pub fn watch(&mut self, id: &str) -> Result<(), String> { + if self.viewers.contains_key(id) { + return Ok(()); + } + if self.outgoing.len() >= 120 { + return Err( + "Media signalling congested; wait before starting another broadcast".into(), + ); + } + if self.viewers.len() >= 4 { + return Err("At most four broadcasts may be viewed at once".into()); + } + let share = self + .available + .get(id) + .ok_or("Broadcast is no longer available")? + .clone(); + if share.publisher_session == self.session { + return Err("Cannot watch your own broadcast".into()); + } + self.viewers.insert( + id.into(), + Viewer { + share, + peer: None, + pending_ice: Vec::new(), + started: std::time::Instant::now(), + }, + ); + self.enqueue(WATCH, id, "", "", 0, Tracks::default()); + Ok(()) + } + pub fn unwatch(&mut self, id: &str) { + if self.viewers.remove(id).is_some() { + self.enqueue(UNWATCH, id, "", "", 0, Tracks::default()); + } + } + pub fn stop(&mut self) { + self.stop_publishing(); + let ids: Vec<_> = self.viewers.keys().cloned().collect(); + for id in ids { + self.unwatch(&id); + } + } + pub fn shares(&self) -> Vec { + let mut shares: Vec<_> = self.available.values().cloned().collect(); + shares.sort_by(|a, b| a.id.cmp(&b.id)); + shares + } + pub fn is_watching(&self, id: &str) -> bool { + self.viewers.contains_key(id) + } + pub fn publication_pending(&self) -> bool { + self.publisher.is_some() + } + pub fn publishing(&self) -> bool { + self.publisher + .as_ref() + .is_some_and(|publisher| publisher.acknowledged) + } + pub fn viewer_count(&self) -> usize { + self.publisher + .as_ref() + .map(|p| p.peers.values().filter(|peer| peer.connected()).count()) + .unwrap_or(0) + } + pub fn remote_feed(&self, id: &str) -> Option { + Some(self.viewers.get(id)?.peer.as_ref()?.remote.clone()) + } + pub fn take_control(&mut self) -> Option { + self.outgoing.pop_front() + } + pub fn take_error(&mut self) -> Option { + self.errors.pop_front() + } + /// Existing voice capture supplies this only when explicitly enabled and unmuted. + pub fn push_microphone(&self, frame: &AudioFrame) -> Result<(), String> { + if let Some(publisher) = &self.publisher { + if !publisher.tracks.microphone { + return Ok(()); + } + for peer in publisher.peers.values() { + peer.push_audio(AudioTrack::Microphone, frame)?; + } + } + Ok(()) + } + pub fn on_event(&mut self, event: MediaEvent) -> Result<(), String> { + if Some(event.channel_id.as_str()) != self.channel.as_deref() + || event.share_id.len() > 64 + || event.sender_session_id.len() > 64 + { + return Err("Media event is outside the active voice scope".into()); + } + match event.action { + START => { + if self.available.len() >= 128 && !self.available.contains_key(&event.share_id) { + return Err("Too many broadcast announcements".into()); + } + let share = AvailableShare { + id: event.share_id.clone(), + publisher_session: event.sender_session_id.clone(), + publisher_user: event.publisher_user_id, + application_audio: event.application_audio, + microphone: event.microphone, + }; + if let Some(publisher) = &mut self.publisher + && publisher.id == event.share_id + && event.sender_session_id == self.session + { + publisher.acknowledged = true; + } + self.available.insert(event.share_id, share); + } + STOP => { + if let Some(share) = self.available.get(&event.share_id) + && share.publisher_session != event.sender_session_id + { + return Err("Stop sender is not the publisher".into()); + } + self.available.remove(&event.share_id); + self.viewers.remove(&event.share_id); + if self + .publisher + .as_ref() + .is_some_and(|p| p.id == event.share_id) + { + self.publisher.take(); + } + } + WATCH => { + if let Some(publisher) = &mut self.publisher + && publisher.id == event.share_id + && publisher.acknowledged + && event.sender_session_id != self.session + { + if publisher.peers.len() >= 4 { + return Err("Viewer limit exceeded".into()); + } + if !publisher.peers.contains_key(&event.sender_session_id) { + let peer = Peer::publisher_with_ice(publisher.tracks, &self.ice)?; + if let Some(frame) = &publisher.latest { + peer.push_video(frame)?; + } + peer.offer(); + publisher.peers.insert(event.sender_session_id, peer); + } + } + } + UNWATCH => { + if let Some(publisher) = &mut self.publisher + && publisher.id == event.share_id + { + publisher.peers.remove(&event.sender_session_id); + } + if self + .viewers + .get(&event.share_id) + .is_some_and(|v| v.share.publisher_session == event.sender_session_id) + { + self.viewers.remove(&event.share_id); + } + } + OFFER => { + let viewer = self + .viewers + .get_mut(&event.share_id) + .ok_or("Unrequested broadcast offer rejected")?; + if viewer.share.publisher_session != event.sender_session_id + || viewer.peer.is_some() + { + return Err("Unexpected publisher or duplicate offer".into()); + } + let mut peer = Peer::viewer_with_ice( + Tracks { + application_audio: viewer.share.application_audio, + microphone: viewer.share.microphone, + }, + &self.ice, + )?; + peer.apply_offer(&event.body)?; + for (mline, candidate) in viewer.pending_ice.drain(..) { + peer.add_ice(mline, &candidate)?; + } + viewer.peer = Some(peer); + } + ANSWER => { + let publisher = self + .publisher + .as_mut() + .filter(|p| p.id == event.share_id) + .ok_or("No active publication for answer")?; + publisher + .peers + .get_mut(&event.sender_session_id) + .ok_or("Unsubscribed answer rejected")? + .apply_answer(&event.body)?; + } + ICE => { + if let Some(publisher) = self.publisher.as_mut().filter(|p| p.id == event.share_id) + { + publisher + .peers + .get_mut(&event.sender_session_id) + .ok_or("Unsubscribed ICE rejected")? + .add_ice(event.mline_index, &event.body)?; + } else { + let viewer = self + .viewers + .get_mut(&event.share_id) + .filter(|v| v.share.publisher_session == event.sender_session_id) + .ok_or("Unrequested ICE rejected")?; + if let Some(peer) = &mut viewer.peer { + peer.add_ice(event.mline_index, &event.body)?; + } else if viewer.pending_ice.len() < 64 + && event.body.len() <= 2048 + && event.mline_index <= 2 + { + viewer.pending_ice.push((event.mline_index, event.body)); + } else { + return Err("Too many early ICE candidates".into()); + } + } + } + _ => return Err("Unknown media event".into()), + } + Ok(()) + } + /// Call frequently on the owning UI thread; samples remain bounded and nonblocking. + pub fn tick(&mut self) { + if self.publisher.as_ref().is_some_and(|p| { + !p.acknowledged && p.started.elapsed() > std::time::Duration::from_secs(10) + }) { + self.stop_publishing(); + self.errors + .push_back("Broadcast authorization timed out".into()); + } + let expired: Vec<_> = self + .viewers + .iter() + .filter(|(_, v)| { + v.peer.is_none() && v.started.elapsed() > std::time::Duration::from_secs(20) + }) + .map(|(id, _)| id.clone()) + .collect(); + for id in expired { + self.unwatch(&id); + if self.errors.len() < 8 { + self.errors + .push_back("Broadcast connection timed out".into()); + } + } + + let mut controls = Vec::new(); + let mut failures = Vec::new(); + if let Some(publisher) = &mut self.publisher { + let mut new_video = false; + loop { + match publisher.video.try_recv() { + Ok(frame) => { + publisher.latest = Some(frame); + new_video = true; + } + Err(broadcast::error::TryRecvError::Lagged(_)) => continue, + _ => break, + } + } + let mut audio = Vec::new(); + loop { + match publisher.audio.try_recv() { + Ok(frame) => audio.push(frame), + Err(broadcast::error::TryRecvError::Lagged(_)) => continue, + _ => break, + } + } + for (viewer, peer) in &publisher.peers { + let result = (|| { + if new_video && let Some(frame) = &publisher.latest { + peer.push_video(frame)?; + } + for frame in &audio { + if publisher.tracks.application_audio { + peer.push_audio(AudioTrack::Application, frame)?; + } + } + while let Some(signal) = peer.poll()? { + controls.push((publisher.id.clone(), viewer.clone(), signal)); + } + Ok::<_, String>(()) + })(); + if let Err(error) = result { + failures.push(error); + } + } + } + for (id, viewer) in &self.viewers { + if let Some(peer) = &viewer.peer { + loop { + match peer.poll() { + Ok(Some(signal)) => controls.push(( + id.clone(), + viewer.share.publisher_session.clone(), + signal, + )), + Ok(None) => break, + Err(error) => { + failures.push(error); + break; + } + } + } + } + } + for (id, target, signal) in controls { + if !self + .publisher + .as_ref() + .is_some_and(|publisher| publisher.id == id) + && !self.viewers.contains_key(&id) + { + continue; + } + match signal { + Signal::Offer(body) => { + self.enqueue(OFFER, &id, &target, &body, 0, Tracks::default()) + } + Signal::Answer(body) => { + self.enqueue(ANSWER, &id, &target, &body, 0, Tracks::default()) + } + Signal::Ice { mline, candidate } => { + self.enqueue(ICE, &id, &target, &candidate, mline, Tracks::default()) + } + Signal::Error(error) => failures.push(error), + } + } + if !failures.is_empty() { + self.stop(); + for error in failures { + if self.errors.len() < 8 { + self.errors.push_back(error); + } + } + } + } + fn enqueue( + &mut self, + action: u32, + id: &str, + target: &str, + body: &str, + mline_index: u32, + tracks: Tracks, + ) { + if let Some(channel) = &self.channel { + if self.outgoing.len() >= 128 { + if self.errors.len() < 8 { + self.errors + .push_back("Media signalling queue overflow; broadcast stopped".into()); + } + let mut cleanup: Vec<_> = self + .outgoing + .iter() + .filter(|control| matches!(control.action, STOP | UNWATCH)) + .map(|control| (control.action, control.share_id.clone())) + .collect(); + if let Some(publisher) = self.publisher.take() { + cleanup.push((STOP, publisher.id)); + } + cleanup.extend(self.viewers.drain().map(|(id, _)| (UNWATCH, id))); + if matches!(action, STOP | UNWATCH) + && !cleanup + .iter() + .any(|(kind, share)| *kind == action && share == id) + { + cleanup.push((action, id.into())); + } + cleanup.sort(); + cleanup.dedup(); + self.outgoing.clear(); + for (action, share_id) in cleanup { + self.outgoing.push_back(MediaControl { + action, + channel_id: channel.clone(), + share_id, + ..Default::default() + }); + } + return; + } + self.outgoing.push_back(MediaControl { + action, + channel_id: channel.clone(), + share_id: id.into(), + target_session_id: target.into(), + body: body.into(), + mline_index, + application_audio: tracks.application_audio, + microphone: tracks.microphone, + }); + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn signalling_overflow_discards_stale_offers_and_retains_stop() { + let mut manager = BroadcastManager::default(); + manager.set_scope("publisher", Some("voice")); + let feed = CaptureFeed::default(); + let id = manager + .publish(&feed, Tracks::default()) + .expect("explicit publish"); + manager.outgoing.clear(); // START already handed to authenticated networking. + for _ in 0..129 { + manager.enqueue( + ICE, + &id, + "viewer", + "candidate:fixture", + 0, + Tracks::default(), + ); + } + assert!(!manager.publication_pending()); + let controls: Vec<_> = manager.outgoing.drain(..).collect(); + assert_eq!(controls.len(), 1); + assert_eq!(controls[0].action, STOP); + assert_eq!(controls[0].share_id, id); + } + #[test] + fn overflow_preserves_queued_cleanup_for_previous_lifecycle() { + let mut manager = BroadcastManager::default(); + manager.set_scope("publisher", Some("voice")); + let feed = CaptureFeed::default(); + let old = manager + .publish(&feed, Tracks::default()) + .expect("first publish"); + manager.outgoing.clear(); + manager.stop_publishing(); + let new = manager + .publish(&feed, Tracks::default()) + .expect("next publish"); + for _ in 0..127 { + manager.enqueue( + ICE, + &new, + "viewer", + "candidate:fixture", + 0, + Tracks::default(), + ); + } + let stops: std::collections::HashSet<_> = manager + .outgoing + .iter() + .filter(|control| control.action == STOP) + .map(|control| control.share_id.clone()) + .collect(); + assert!(stops.contains(&old) && stops.contains(&new)); + assert!(!manager.publication_pending()); + } +} diff --git a/src/media/capture.rs b/src/media/capture.rs index bdab97a..60be6fe 100644 --- a/src/media/capture.rs +++ b/src/media/capture.rs @@ -12,14 +12,41 @@ use std::{ }; /// Latest owned preview frame and selected audio level; queues never grow. -#[derive(Clone, Default)] +#[derive(Clone)] pub struct CaptureFeed { - video: Arc>>, + video: Arc>>>, + video_tx: Arc>>, + audio_tx: Arc>>, audio: Arc>, received_audio: Arc, } +impl Default for CaptureFeed { + fn default() -> Self { + Self { + video: Arc::default(), + audio: Arc::default(), + received_audio: Arc::default(), + video_tx: Arc::new(tokio::sync::broadcast::channel(2).0), + audio_tx: Arc::new(tokio::sync::broadcast::channel(8).0), + } + } +} impl CaptureFeed { - pub fn take_frame(&self) -> Option { + #[cfg(test)] + pub(super) fn fixture_video(&self, frame: VideoFrame) { + let _ = self.video_tx.send(Arc::new(frame)); + } + #[cfg(test)] + pub(super) fn fixture_audio(&self, frame: super::AudioFrame) { + let _ = self.audio_tx.send(Arc::new(frame)); + } + pub fn subscribe_video(&self) -> tokio::sync::broadcast::Receiver> { + self.video_tx.subscribe() + } + pub fn subscribe_audio(&self) -> tokio::sync::broadcast::Receiver> { + self.audio_tx.subscribe() + } + pub fn take_frame(&self) -> Option> { self.video.lock().ok()?.take() } pub fn audio_level(&self) -> f32 { @@ -87,6 +114,7 @@ impl CaptureSession { source.set_property("path", portal.node.to_string()); } strict_source(&source); + source.set_property("keepalive-time", 500i32); let pipeline = video_pipeline(source, &session.feed, options)?; session.pipelines.push(pipeline); if let AudioChoice::Application(serial) | AudioChoice::System(serial) = options.audio { @@ -177,11 +205,13 @@ fn video_pipeline( .sync(false) .build(); let latest = feed.video.clone(); + let tx = feed.video_tx.clone(); sink.set_callbacks( gstreamer_app::AppSinkCallbacks::builder() .new_sample(move |sink| { let sample = sink.pull_sample().map_err(|_| gst::FlowError::Eos)?; - let frame = copy_video(&sample).map_err(|_| gst::FlowError::Error)?; + let frame = Arc::new(copy_video(&sample).map_err(|_| gst::FlowError::Error)?); + let _ = tx.send(frame.clone()); *latest.lock().map_err(|_| gst::FlowError::Error)? = Some(frame); Ok(gst::FlowSuccess::Ok) }) @@ -214,7 +244,7 @@ fn video_pipeline( Ok(pipeline) } -fn copy_video(sample: &gst::Sample) -> Result { +pub(super) fn copy_video(sample: &gst::Sample) -> Result { let caps = sample.caps().ok_or("Missing video caps")?; let info = gstreamer_video::VideoInfo::from_caps(caps).map_err(error)?; if info.format() != gstreamer_video::VideoFormat::Rgba @@ -261,6 +291,7 @@ fn audio_pipeline(source: gst::Element, feed: &CaptureFeed) -> Result Result 48_000 * 8 { return Err(gst::FlowError::Error); } + let mut samples = Vec::with_capacity(map.len() / 4); let mut sum = 0.0f64; let mut count = 0u32; for bytes in map.as_slice().as_chunks::<4>().0 { let value = f32::from_le_bytes([bytes[0], bytes[1], bytes[2], bytes[3]]); if value.is_finite() { + samples.push(value); sum += f64::from(value) * f64::from(value); count += 1; } } + let _ = tx.send(Arc::new(super::AudioFrame { + samples, + rate: 48_000, + channels: 2, + })); received.store(true, Ordering::Relaxed); *level.lock().map_err(|_| gst::FlowError::Error)? = if count == 0 { 0.0 @@ -324,12 +362,12 @@ mod tests { #[test] fn silent_unavailable_source_times_out_and_drop_clears_preview() { let feed = CaptureFeed::default(); - *feed.video.lock().expect("frame lock") = Some(VideoFrame { + *feed.video.lock().expect("frame lock") = Some(Arc::new(VideoFrame { width: 1, height: 1, rgba: vec![1, 2, 3, 255], timestamp_ns: None, - }); + })); *feed.audio.lock().expect("level lock") = 0.5; let session = CaptureSession { pipelines: Vec::new(), diff --git a/src/media/gateway_tests.rs b/src/media/gateway_tests.rs new file mode 100644 index 0000000..f265617 --- /dev/null +++ b/src/media/gateway_tests.rs @@ -0,0 +1,245 @@ +//! Synthetic full native-client → authenticated gateway → peer media fixture. +use super::{ + AudioFrame, CaptureFeed, VideoFrame, + broadcast::BroadcastManager, + peer::{AudioTrack, Tracks}, +}; +use crate::{ + identity, + net::{self, NetCommand, NetEvent, NetHandle}, +}; +use std::{ + path::PathBuf, + process::{Child, Command, Stdio}, + time::{Duration, Instant}, +}; +struct Gateway { + child: Child, + directory: PathBuf, +} +impl Drop for Gateway { + fn drop(&mut self) { + let _ = self.child.kill(); + let _ = self.child.wait(); + let _ = std::fs::remove_dir_all(&self.directory); + } +} +async fn wait(handle: &NetHandle, test: impl Fn(&NetEvent) -> bool) -> NetEvent { + tokio::time::timeout(Duration::from_secs(8), async { + loop { + if let Some(event) = handle.try_recv() { + if let NetEvent::Disconnected { reason, .. } = &event { + panic!("fixture disconnected: {reason}"); + } + if let NetEvent::Error { message, .. } = &event { + panic!("gateway rejected fixture: {message}"); + } + if test(&event) { + return event; + } + } + tokio::time::sleep(Duration::from_millis(5)).await; + } + }) + .await + .expect("authenticated gateway fixture timeout") +} +async fn connect(handle: &NetHandle, address: &str) -> String { + handle + .send(NetCommand::Connect { + address: address.into(), + auto_reconnect: false, + }) + .await; + let NetEvent::Connected { session_id, .. } = + wait(handle, |event| matches!(event, NetEvent::Connected { .. })).await + else { + panic!("connected event") + }; + handle + .send(NetCommand::JoinChannel { + channel_id: "voice".into(), + }) + .await; + wait( + handle, + |event| matches!(event, NetEvent::ChannelState { kind, .. } if kind=="voice"), + ) + .await; + // Browsing text must preserve the independently authorized voice membership. + handle + .send(NetCommand::JoinChannel { + channel_id: "general".into(), + }) + .await; + tokio::time::sleep(Duration::from_millis(20)).await; + session_id +} +async fn pump(handle: &NetHandle, manager: &mut BroadcastManager) { + while let Some(event) = handle.try_recv() { + match event { + NetEvent::MediaEvent(event) => manager.on_event(event).expect("authorized media event"), + NetEvent::Error { message, .. } => panic!("gateway rejected media fixture: {message}"), + NetEvent::Disconnected { reason, .. } => { + panic!("gateway fixture disconnected: {reason}") + } + _ => {} + } + } + manager.tick(); + if let Some(error) = manager.take_error() { + panic!("media fixture failed: {error}"); + } + while let Some(control) = manager.take_control() { + handle.send(NetCommand::MediaControl(control)).await; + } +} +#[tokio::test] +#[ignore = "requires freshly built current VNOX_GATEWAY_BIN; uses synthetic media only"] +async fn gateway_authorizes_real_two_peer_video_and_application_audio() { + let binary = std::env::var_os("VNOX_GATEWAY_BIN").expect("set VNOX_GATEWAY_BIN"); + let directory = + std::env::temp_dir().join(format!("vnox-media-gateway-{}", uuid::Uuid::new_v4())); + std::fs::create_dir_all(&directory).expect("fixture dir"); + let port = std::net::TcpListener::bind("127.0.0.1:0") + .expect("port") + .local_addr() + .expect("address") + .port(); + let voice_port = std::net::UdpSocket::bind("127.0.0.1:0") + .expect("UDP port") + .local_addr() + .expect("UDP address") + .port(); + let config = format!( + r#" +[node] +name="media-fixture" +address="127.0.0.1" +[gateway] +bind="127.0.0.1:{port}" +admin_bind="127.0.0.1:0" +message_rate_per_sec=0 +[voice] +bind="127.0.0.1:{voice_port}" +[storage] +data_dir="{}" +backend="sqlite" +sqlite_path="{}/fixture.db" +[server] +mode="private" +"#, + directory.display(), + directory.display() + ); + let config_path = directory.join("config.toml"); + std::fs::write(&config_path, config).expect("config"); + let child = Command::new(binary) + .arg("--config") + .arg(config_path) + .stdout(Stdio::null()) + .stderr(Stdio::null()) + .spawn() + .expect("start actual gateway"); + let _gateway = Gateway { child, directory }; + tokio::time::sleep(Duration::from_millis(700)).await; + let publisher_net = net::spawn(identity::generate("Synthetic publisher")); + let viewer_net = net::spawn(identity::generate("Synthetic viewer")); + let address = format!("127.0.0.1:{port}"); + let publisher_session = connect(&publisher_net, &address).await; + let viewer_session = connect(&viewer_net, &address).await; + let mut publisher = BroadcastManager::default(); + publisher.set_scope(&publisher_session, Some("voice")); + let mut viewer = BroadcastManager::default(); + viewer.set_scope(&viewer_session, Some("voice")); + let feed = CaptureFeed::default(); + let share = publisher + .publish( + &feed, + Tracks { + application_audio: true, + microphone: false, + }, + ) + .expect("explicit publish"); + let start = Instant::now(); + let mut samples = 0usize; + let mut requested = false; + let mut received_pcm = Vec::new(); + let mut received_video = false; + while start.elapsed() < Duration::from_secs(15) { + feed.fixture_video(VideoFrame { + width: 64, + height: 48, + rgba: [200, 50, 70, 255].repeat(64 * 48), + timestamp_ns: None, + }); + let pcm = (samples..samples + 960) + .flat_map(|n| { + let sample = (n as f32 * 440.0 * std::f32::consts::TAU / 48_000.0).sin() * 0.3; + [sample, sample] + }) + .collect(); + samples += 960; + feed.fixture_audio(AudioFrame { + samples: pcm, + rate: 48_000, + channels: 2, + }); + pump(&publisher_net, &mut publisher).await; + pump(&viewer_net, &mut viewer).await; + if !requested && viewer.shares().iter().any(|item| item.id == share) { + viewer.watch(&share).expect("explicit watch"); + requested = true; + } + if let Some(remote) = viewer.remote_feed(&share) { + if let Some(frame) = remote.take_video() { + assert_eq!((frame.width, frame.height), (64, 48)); + received_video = true; + } + while let Some(pcm) = remote.take_audio(AudioTrack::Application) { + received_pcm.extend(pcm.samples.into_iter().step_by(2)); + } + assert_eq!( + remote + .microphone_samples + .load(std::sync::atomic::Ordering::Relaxed), + 0, + "unrequested microphone not delivered" + ); + } + if received_video && received_pcm.len() > 24_000 { + break; + } + tokio::time::sleep(Duration::from_millis(20)).await; + } + assert!( + publisher.publishing() && publisher.viewer_count() == 1, + "gateway authorized connected viewer" + ); + assert!( + received_video && received_pcm.len() > 24_000, + "actual decoded video and Opus reached consenting viewer" + ); + let frequency = received_pcm + .windows(2) + .filter(|p| p[0] < 0.0 && p[1] >= 0.0) + .count() as f32 + * 48_000.0 + / received_pcm.len() as f32; + assert!( + (frequency - 440.0).abs() < 60.0, + "actual selected fixture audio: {frequency} Hz" + ); + publisher.stop_publishing(); + pump(&publisher_net, &mut publisher).await; + let stop = Instant::now(); + while viewer.is_watching(&share) && stop.elapsed() < Duration::from_secs(3) { + pump(&viewer_net, &mut viewer).await; + tokio::time::sleep(Duration::from_millis(10)).await; + } + assert!( + !viewer.is_watching(&share), + "authenticated STOP revoked receiver transport" + ); +} diff --git a/src/media/mod.rs b/src/media/mod.rs index d31231d..bbfa643 100644 --- a/src/media/mod.rs +++ b/src/media/mod.rs @@ -5,7 +5,7 @@ mod policy; mod sources; -pub use policy::{AudioChoice, AudioKind, AudioSource, CaptureOptions, VideoFrame}; +pub use policy::{AudioChoice, AudioFrame, AudioKind, AudioSource, CaptureOptions, VideoFrame}; pub use sources::{audio_sources, parse_audio_sources, validate_audio_choice}; #[cfg(all(target_os = "linux", feature = "screen-capture"))] @@ -29,3 +29,11 @@ pub fn unavailable_reason() -> Option<&'static str> { ) } } + +#[cfg(all(target_os = "linux", feature = "screen-capture"))] +pub mod broadcast; +#[cfg(all(target_os = "linux", feature = "screen-capture"))] +pub mod peer; + +#[cfg(all(test, target_os = "linux", feature = "screen-capture"))] +mod gateway_tests; diff --git a/src/media/peer.rs b/src/media/peer.rs new file mode 100644 index 0000000..87361df --- /dev/null +++ b/src/media/peer.rs @@ -0,0 +1,646 @@ +//! Established ICE/DTLS-SRTP transport via GStreamer webrtcbin, with independent tracks. +use super::{AudioFrame, VideoFrame}; +use gst::prelude::*; +use gstreamer as gst; +use gstreamer_webrtc as rtc; +use std::sync::{ + Arc, Mutex, + atomic::{AtomicU64, Ordering}, + mpsc, +}; + +#[derive(Clone, Copy, Debug, Default)] +pub struct Tracks { + pub application_audio: bool, + pub microphone: bool, +} +#[derive(Debug)] +pub enum Signal { + Offer(String), + Answer(String), + Ice { mline: u32, candidate: String }, + Error(String), +} +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum AudioTrack { + Application, + Microphone, +} +#[derive(Clone, Default)] +pub struct RemoteFeed { + video: Arc>>>, + app: Arc>>, + mic: Arc>>, + pub video_frames: Arc, + pub application_samples: Arc, + pub microphone_samples: Arc, +} +impl RemoteFeed { + pub fn take_video(&self) -> Option> { + self.video.lock().ok()?.take() + } + pub fn take_audio(&self, track: AudioTrack) -> Option { + match track { + AudioTrack::Application => self.app.lock().ok()?.pop_front(), + AudioTrack::Microphone => self.mic.lock().ok()?.pop_front(), + } + } +} + +/// Explicit administrator-configured ICE services. Empty means host candidates only. +#[derive(Clone, Debug, Default)] +pub struct IceServers { + pub stun: Option, + pub turn: Option, +} +impl IceServers { + pub fn validate(&self) -> Result<(), String> { + if self.stun.as_ref().is_some_and(|uri| { + !uri.starts_with("stun://") + || uri.len() > 2048 + || uri.len() <= 7 + || uri.chars().any(char::is_whitespace) + }) || self.turn.as_ref().is_some_and(|uri| { + !(uri.starts_with("turn://") || uri.starts_with("turns://")) + || uri.len() > 2048 + || uri.len() <= 7 + || uri.chars().any(char::is_whitespace) + }) { + return Err("Invalid explicitly configured ICE server URI".into()); + } + Ok(()) + } +} + +/// One directed publisher/viewer connection. The authenticated gateway routes its signals. +pub struct Peer { + pipeline: gst::Pipeline, + rtc: gst::Element, + video: Option, + app: Option, + mic: Option, + events: mpsc::Receiver, + tx: mpsc::SyncSender, + pub remote: RemoteFeed, + remote_set: bool, + started: std::time::Instant, + pending_ice: Vec<(u32, String)>, + app_time: Mutex>, + mic_time: Mutex>, +} + +pub fn check_backend() -> Result<(), String> { + gst::init().map_err(error)?; + for plugin in [ + "webrtcbin", + "nicesrc", + "nicesink", + "dtlssrtpenc", + "dtlssrtpdec", + "vp8enc", + "vp8dec", + "rtpvp8pay", + "rtpvp8depay", + "opusenc", + "opusdec", + "rtpopuspay", + "rtpopusdepay", + ] { + if gst::ElementFactory::find(plugin).is_none() { + return Err(format!("Missing WebRTC plugin: {plugin}")); + } + } + Ok(()) +} +impl Peer { + pub fn publisher(tracks: Tracks) -> Result { + Self::new(true, tracks, &IceServers::default()) + } + pub fn viewer(tracks: Tracks) -> Result { + Self::new(false, tracks, &IceServers::default()) + } + pub fn publisher_with_ice(tracks: Tracks, ice: &IceServers) -> Result { + Self::new(true, tracks, ice) + } + pub fn viewer_with_ice(tracks: Tracks, ice: &IceServers) -> Result { + Self::new(false, tracks, ice) + } + fn new(publisher: bool, tracks: Tracks, ice: &IceServers) -> Result { + ice.validate()?; + check_backend()?; + let pipeline = gst::Pipeline::new(); + let rtc = gst::ElementFactory::make("webrtcbin") + .name("transport") + .build() + .map_err(error)?; + rtc.set_property_from_str("bundle-policy", "max-bundle"); + rtc.set_property("latency", 100u32); + if let Some(uri) = &ice.stun { + rtc.set_property("stun-server", uri); + } + if let Some(uri) = &ice.turn { + rtc.set_property("turn-server", uri); + } + pipeline.add(&rtc).map_err(error)?; + let (tx, events) = mpsc::sync_channel(64); + let ice_tx = tx.clone(); + rtc.connect("on-ice-candidate", false, move |values| { + if let (Ok(mline), Ok(candidate)) = (values[1].get::(), values[2].get::()) + { + let _ = ice_tx.try_send(Signal::Ice { mline, candidate }); + } + None + }); + let remote = RemoteFeed::default(); + if !publisher { + let weak_pipeline = pipeline.downgrade(); + let feed = remote.clone(); + let receive_tx = tx.clone(); + rtc.connect_pad_added(move |_, pad| { + if pad.direction() != gst::PadDirection::Src { + return; + } + if let Some(pipeline) = weak_pipeline.upgrade() + && let Err(error) = receive(&pipeline, pad, &feed, tracks) + { + let _ = receive_tx.try_send(Signal::Error(error)); + } + }); + } + let video = if publisher { + Some(add_sender(&pipeline, &rtc, 96, "screen-video", true)?) + } else { + None + }; + let app = if publisher && tracks.application_audio { + Some(add_sender(&pipeline, &rtc, 97, "application-audio", false)?) + } else { + None + }; + let mic = if publisher && tracks.microphone { + Some(add_sender(&pipeline, &rtc, 98, "microphone", false)?) + } else { + None + }; + pipeline.set_state(gst::State::Playing).map_err(error)?; + Ok(Self { + pipeline, + rtc, + video, + app, + mic, + events, + tx, + remote, + remote_set: false, + started: std::time::Instant::now(), + pending_ice: Vec::new(), + app_time: Mutex::new(None), + mic_time: Mutex::new(None), + }) + } + /// Create an SDP offer only after the gateway authorizes a viewer subscription. + pub fn offer(&self) { + create_description(&self.rtc, true, self.signal_sender()); + } + fn signal_sender(&self) -> mpsc::SyncSender { + self.tx.clone() + } + pub fn apply_offer(&mut self, sdp: &str) -> Result<(), String> { + self.set_description(sdp, true)?; + create_description(&self.rtc, false, self.signal_sender()); + Ok(()) + } + pub fn apply_answer(&mut self, sdp: &str) -> Result<(), String> { + self.set_description(sdp, false) + } + fn set_description(&mut self, text: &str, offer: bool) -> Result<(), String> { + if text.len() > 65_536 { + return Err("SDP exceeds limit".into()); + } + let sdp = gstreamer_sdp::SDPMessage::parse_buffer(text.as_bytes()).map_err(error)?; + let description = rtc::WebRTCSessionDescription::new( + if offer { + rtc::WebRTCSDPType::Offer + } else { + rtc::WebRTCSDPType::Answer + }, + sdp, + ); + let promise = gst::Promise::new(); + self.rtc + .emit_by_name::<()>("set-remote-description", &[&description, &promise]); + if promise.wait() != gst::PromiseResult::Replied { + return Err("Remote SDP was rejected".into()); + } + if let Some(reply) = promise.get_reply() + && reply.has_field("error") + { + return Err("Remote SDP was rejected".into()); + } + self.remote_set = true; + for (mline, candidate) in self.pending_ice.drain(..) { + self.rtc + .emit_by_name::<()>("add-ice-candidate", &[&mline, &candidate]); + } + Ok(()) + } + pub fn add_ice(&mut self, mline: u32, candidate: &str) -> Result<(), String> { + if mline > 2 || candidate.len() > 2048 || self.pending_ice.len() >= 64 { + return Err("ICE candidate exceeds limit".into()); + } + if self.remote_set { + self.rtc + .emit_by_name::<()>("add-ice-candidate", &[&mline, &candidate]); + } else { + self.pending_ice.push((mline, candidate.into())); + } + Ok(()) + } + pub fn poll(&self) -> Result, String> { + if let Some(bus) = self.pipeline.bus() { + while let Some(message) = bus.pop() { + match message.view() { + gst::MessageView::Error(e) => return Err(format!("WebRTC: {}", e.error())), + gst::MessageView::Eos(..) => return Err("WebRTC stream closed".into()), + _ => {} + } + } + } + let state = self + .rtc + .property::("connection-state"); + if self.started.elapsed() > std::time::Duration::from_secs(20) + && state != rtc::WebRTCPeerConnectionState::Connected + { + return Err("WebRTC connection timed out".into()); + } + if matches!( + state, + rtc::WebRTCPeerConnectionState::Failed | rtc::WebRTCPeerConnectionState::Closed + ) { + return Err("WebRTC peer disconnected".into()); + } + Ok(self.events.try_recv().ok()) + } + pub fn connected(&self) -> bool { + self.rtc + .property::("connection-state") + == rtc::WebRTCPeerConnectionState::Connected + } + pub fn push_video(&self, frame: &VideoFrame) -> Result<(), String> { + frame.validate()?; + let source = self.video.as_ref().ok_or("Peer does not publish video")?; + source.set_caps(Some( + &gst::Caps::builder("video/x-raw") + .field("format", "RGBA") + .field("width", frame.width as i32) + .field("height", frame.height as i32) + .field("framerate", gst::Fraction::new(30, 1)) + .build(), + )); + source + .push_buffer(gst::Buffer::from_mut_slice(frame.rgba.clone())) + .map_err(error)?; + Ok(()) + } + pub fn push_audio(&self, track: AudioTrack, frame: &AudioFrame) -> Result<(), String> { + frame.validate()?; + let source = match track { + AudioTrack::Application => &self.app, + AudioTrack::Microphone => &self.mic, + } + .as_ref() + .ok_or("Audio track was not explicitly enabled")?; + source.set_caps(Some( + &gst::Caps::builder("audio/x-raw") + .field("format", "F32LE") + .field("layout", "interleaved") + .field("rate", frame.rate as i32) + .field("channels", i32::from(frame.channels)) + .build(), + )); + let bytes: Vec = frame + .samples + .iter() + .flat_map(|sample| sample.to_le_bytes()) + .collect(); + let clock = match track { + AudioTrack::Application => &self.app_time, + AudioTrack::Microphone => &self.mic_time, + }; + let mut next = clock + .lock() + .map_err(|_| "Audio timestamp clock unavailable")?; + let pts = next.unwrap_or_else(|| { + self.pipeline + .current_running_time() + .map(|time| time.nseconds()) + .unwrap_or(0) + }); + let duration = (frame.samples.len() as u64 / u64::from(frame.channels)) * 1_000_000_000 + / u64::from(frame.rate); + let mut buffer = gst::Buffer::from_mut_slice(bytes); + if let Some(buffer) = buffer.get_mut() { + buffer.set_pts(gst::ClockTime::from_nseconds(pts)); + buffer.set_duration(gst::ClockTime::from_nseconds(duration)); + } + *next = Some(pts.saturating_add(duration)); + source.push_buffer(buffer).map_err(error)?; + Ok(()) + } +} +impl Drop for Peer { + fn drop(&mut self) { + let _ = self.pipeline.set_state(gst::State::Null); + } +} + +fn add_sender( + pipeline: &gst::Pipeline, + rtc: &gst::Element, + payload: i32, + msid: &str, + video: bool, +) -> Result { + let description = if video { + format!( + "appsrc name=source is-live=true format=time do-timestamp=true block=false max-bytes=8388608 leaky-type=downstream ! queue max-size-buffers=2 leaky=downstream ! videoconvert ! vp8enc deadline=1 cpu-used=8 target-bitrate=1500000 keyframe-max-dist=60 ! rtpvp8pay pt={payload} ! capsfilter caps=\"application/x-rtp,media=video,encoding-name=VP8,payload={payload},clock-rate=90000\"" + ) + } else { + format!( + "appsrc name=source is-live=true format=time do-timestamp=true block=false max-bytes=384000 leaky-type=downstream ! queue max-size-buffers=8 leaky=downstream ! audioconvert ! audioresample ! opusenc bitrate=64000 ! rtpopuspay pt={payload} ! capsfilter caps=\"application/x-rtp,media=audio,encoding-name=OPUS,payload={payload},clock-rate=48000\"" + ) + }; + let bin = gst::parse::bin_from_description(&description, true).map_err(error)?; + let source = bin + .by_name("source") + .ok_or("Missing media appsrc")? + .downcast::() + .map_err(|_| "Wrong media source type")?; + pipeline.add(&bin).map_err(error)?; + let src = bin.static_pad("src").ok_or("Missing RTP source pad")?; + let sink = rtc + .request_pad_simple("sink_%u") + .ok_or("WebRTC refused a media track")?; + sink.set_property("msid", msid); + let transceiver = sink.property::("transceiver"); + transceiver.set_property("direction", rtc::WebRTCRTPTransceiverDirection::Sendonly); + src.link(&sink).map_err(error)?; + Ok(source) +} + +fn create_description(webrtc: &gst::Element, offer: bool, tx: mpsc::SyncSender) { + let weak = webrtc.downgrade(); + let promise = gst::Promise::with_change_func(move |reply| { + let result = (|| { + let reply = reply + .map_err(|e| format!("SDP promise failed: {e:?}"))? + .ok_or("No SDP reply")?; + let description = reply + .get::(if offer { "offer" } else { "answer" }) + .map_err(error)?; + let webrtc = weak.upgrade().ok_or("WebRTC peer closed")?; + webrtc.emit_by_name::<()>( + "set-local-description", + &[&description, &None::], + ); + let text = description.sdp().as_text().map_err(error)?; + Ok::<_, String>(if offer { + Signal::Offer(text) + } else { + Signal::Answer(text) + }) + })(); + let _ = tx.try_send(result.unwrap_or_else(Signal::Error)); + }); + webrtc.emit_by_name::<()>( + if offer { + "create-offer" + } else { + "create-answer" + }, + &[&None::, &promise], + ); +} + +fn receive( + pipeline: &gst::Pipeline, + pad: &gst::Pad, + feed: &RemoteFeed, + tracks: Tracks, +) -> Result<(), String> { + let caps = pad.current_caps().ok_or("Incoming track has no caps")?; + let payload = caps + .structure(0) + .ok_or("Incoming track has no format")? + .get::("payload") + .map_err(error)?; + let video = payload == 96; + if !(video || payload == 97 && tracks.application_audio || payload == 98 && tracks.microphone) { + return Err("Unannounced remote track rejected".into()); + } + let description = if video { + "queue max-size-buffers=2 leaky=downstream ! rtpvp8depay ! vp8dec ! videoconvert ! video/x-raw,format=RGBA,width=[1,1920],height=[1,1080] ! appsink name=sink sync=false max-buffers=1 drop=true" + } else { + "queue max-size-buffers=8 leaky=downstream ! rtpopusdepay ! opusdec ! audioconvert ! audioresample ! audio/x-raw,format=F32LE,layout=interleaved,rate=48000,channels=2 ! appsink name=sink sync=false max-buffers=4 drop=true" + }; + let bin = gst::parse::bin_from_description(description, true).map_err(error)?; + let sink = bin + .by_name("sink") + .ok_or("Remote sink missing")? + .downcast::() + .map_err(|_| "Wrong remote sink")?; + let feed = feed.clone(); + sink.set_callbacks( + gstreamer_app::AppSinkCallbacks::builder() + .new_sample(move |sink| { + let sample = sink.pull_sample().map_err(|_| gst::FlowError::Eos)?; + if video { + let frame = + super::capture::copy_video(&sample).map_err(|_| gst::FlowError::Error)?; + *feed.video.lock().map_err(|_| gst::FlowError::Error)? = Some(Arc::new(frame)); + feed.video_frames.fetch_add(1, Ordering::Relaxed); + } else { + let map = sample + .buffer() + .ok_or(gst::FlowError::Error)? + .map_readable() + .map_err(|_| gst::FlowError::Error)?; + if map.len() > 384000 { + return Err(gst::FlowError::Error); + } + let samples: Vec = map + .as_slice() + .as_chunks::<4>() + .0 + .iter() + .map(|bytes| f32::from_le_bytes(*bytes)) + .collect(); + let count = samples.len() as u64; + let frame = AudioFrame { + samples, + rate: 48000, + channels: 2, + }; + frame.validate().map_err(|_| gst::FlowError::Error)?; + let (queue, received) = if payload == 97 { + (&feed.app, &feed.application_samples) + } else { + (&feed.mic, &feed.microphone_samples) + }; + let mut queue = queue.lock().map_err(|_| gst::FlowError::Error)?; + if queue.len() == 8 { + queue.pop_front(); + } + queue.push_back(frame); + received.fetch_add(count, Ordering::Relaxed); + } + Ok(gst::FlowSuccess::Ok) + }) + .build(), + ); + pipeline.add(&bin).map_err(error)?; + bin.sync_state_with_parent().map_err(error)?; + pad.link(&bin.static_pad("sink").ok_or("Missing remote RTP sink")?) + .map_err(error)?; + Ok(()) +} +fn error(error: impl std::fmt::Display) -> String { + error.to_string() +} + +#[cfg(test)] +mod tests { + use super::*; + use std::time::{Duration, Instant}; + fn forward(from: &Peer, to: &mut Peer) { + while let Some(signal) = from.poll().expect("healthy peer") { + match signal { + Signal::Offer(sdp) => to.apply_offer(&sdp).expect("offer accepted"), + Signal::Answer(sdp) => to.apply_answer(&sdp).expect("answer accepted"), + Signal::Ice { mline, candidate } => { + to.add_ice(mline, &candidate).expect("candidate accepted") + } + Signal::Error(error) => panic!("signalling failed: {error}"), + } + } + } + #[test] + fn two_peers_deliver_video_and_separate_application_and_microphone_tracks() { + let tracks = Tracks { + application_audio: true, + microphone: true, + }; + let mut publisher = Peer::publisher(tracks).expect("publisher"); + let mut viewer = Peer::viewer(tracks).expect("viewer"); + let frame = VideoFrame { + width: 64, + height: 48, + rgba: [200, 50, 70, 255].repeat(64 * 48), + timestamp_ns: None, + }; + publisher.push_video(&frame).expect("fixture video"); + publisher.offer(); + let start = Instant::now(); + let mut index = 0usize; + let mut app_pcm = Vec::new(); + let mut mic_pcm = Vec::new(); + while start.elapsed() < Duration::from_secs(12) { + forward(&publisher, &mut viewer); + forward(&viewer, &mut publisher); + publisher.push_video(&frame).expect("video push"); + for (track, frequency) in [ + (AudioTrack::Application, 440.0f32), + (AudioTrack::Microphone, 880.0f32), + ] { + let samples = (index..index + 960) + .flat_map(|sample| { + let value = (sample as f32 * frequency * std::f32::consts::TAU / 48_000.0) + .sin() + * 0.3; + [value, value] + }) + .collect(); + publisher + .push_audio( + track, + &AudioFrame { + samples, + rate: 48_000, + channels: 2, + }, + ) + .expect("independent audio push"); + } + index += 960; + while let Some(frame) = viewer.remote.take_audio(AudioTrack::Application) { + app_pcm.extend(frame.samples.into_iter().step_by(2)); + } + while let Some(frame) = viewer.remote.take_audio(AudioTrack::Microphone) { + mic_pcm.extend(frame.samples.into_iter().step_by(2)); + } + if viewer.remote.video_frames.load(Ordering::Relaxed) >= 3 + && app_pcm.len() > 24_000 + && mic_pcm.len() > 24_000 + { + break; + } + std::thread::sleep(Duration::from_millis(20)); + } + assert!( + publisher.connected() && viewer.connected(), + "DTLS/ICE connection established" + ); + let received = viewer + .remote + .take_video() + .expect("actual decoded remote video"); + assert_eq!((received.width, received.height), (64, 48)); + assert!((i32::from(received.rgba[0]) - 200).abs() < 20); + assert!( + app_pcm.len() > 24_000 && mic_pcm.len() > 24_000, + "both distinct Opus tracks delivered" + ); + let frequency = |pcm: &[f32]| { + pcm.windows(2) + .filter(|pair| pair[0] < 0.0 && pair[1] >= 0.0) + .count() as f32 + * 48_000.0 + / pcm.len() as f32 + }; + let app_frequency = frequency(&app_pcm); + let mic_frequency = frequency(&mic_pcm); + assert!( + (app_frequency - 440.0).abs() < 60.0, + "app frequency {app_frequency}" + ); + assert!( + (mic_frequency - 880.0).abs() < 100.0, + "microphone frequency {mic_frequency}" + ); + assert!( + publisher + .push_audio( + AudioTrack::Application, + &AudioFrame { + samples: vec![f32::NAN], + rate: 48_000, + channels: 1 + } + ) + .is_err() + ); + } + #[test] + fn unrequested_audio_track_cannot_be_enabled_by_push() { + let publisher = Peer::publisher(Tracks::default()).expect("publisher"); + let pcm = AudioFrame { + samples: vec![0.0; 960], + rate: 48_000, + channels: 1, + }; + assert!(publisher.push_audio(AudioTrack::Microphone, &pcm).is_err()); + assert!(publisher.push_audio(AudioTrack::Application, &pcm).is_err()); + } +} diff --git a/src/media/policy.rs b/src/media/policy.rs index 3b4c58c..451caf1 100644 --- a/src/media/policy.rs +++ b/src/media/policy.rs @@ -107,3 +107,28 @@ mod tests { ); } } + +/// One bounded independent PCM source, ready for an Opus track. +#[derive(Clone, Debug)] +pub struct AudioFrame { + pub samples: Vec, + pub rate: u32, + pub channels: u16, +} +impl AudioFrame { + pub fn validate(&self) -> Result<(), String> { + if self.rate != 48_000 + || !(1..=2).contains(&self.channels) + || self.samples.len() > 48_000 * 2 + || self.samples.is_empty() + || !self + .samples + .len() + .is_multiple_of(usize::from(self.channels)) + || self.samples.iter().any(|sample| !sample.is_finite()) + { + return Err("Invalid or unbounded PCM frame".into()); + } + Ok(()) + } +} diff --git a/src/net/crypto/cipher.rs b/src/net/crypto/cipher.rs index 71091cf..2930348 100644 --- a/src/net/crypto/cipher.rs +++ b/src/net/crypto/cipher.rs @@ -10,7 +10,18 @@ fn make_nonce(cid: &[u8; 8], seq: u64) -> [u8; 12] { nonce } +#[cfg(test)] pub fn encrypt_with_key(key: &[u8; 32], cid: &[u8; 8], seq: u64, plaintext: &[u8]) -> Vec { + encrypt_with_aad(key, cid, seq, plaintext, &[]) +} + +pub fn encrypt_with_aad( + key: &[u8; 32], + cid: &[u8; 8], + seq: u64, + plaintext: &[u8], + aad: &[u8], +) -> Vec { let cipher = ChaCha20Poly1305::new(&Key::from(*key)); let nonce_bytes = make_nonce(cid, seq); let nonce = Nonce::from(nonce_bytes); @@ -19,17 +30,28 @@ pub fn encrypt_with_key(key: &[u8; 32], cid: &[u8; 8], seq: u64, plaintext: &[u8 &nonce, Payload { msg: plaintext, - aad: &[], + aad, }, ) .expect("ChaCha20-Poly1305 encryption infallible") } +#[cfg(test)] pub fn decrypt_with_key( key: &[u8; 32], cid: &[u8; 8], seq: u64, ciphertext: &[u8], +) -> anyhow::Result> { + decrypt_with_aad(key, cid, seq, ciphertext, &[]) +} + +pub fn decrypt_with_aad( + key: &[u8; 32], + cid: &[u8; 8], + seq: u64, + ciphertext: &[u8], + aad: &[u8], ) -> anyhow::Result> { let cipher = ChaCha20Poly1305::new(&Key::from(*key)); let nonce_bytes = make_nonce(cid, seq); @@ -39,7 +61,7 @@ pub fn decrypt_with_key( &nonce, Payload { msg: ciphertext, - aad: &[], + aad, }, ) .map_err(|e| anyhow::anyhow!("decryption failed: {e:?}")) diff --git a/src/net/crypto/mod.rs b/src/net/crypto/mod.rs index 26901ea..d7415ce 100644 --- a/src/net/crypto/mod.rs +++ b/src/net/crypto/mod.rs @@ -20,7 +20,7 @@ pub struct SessionCrypto { impl SessionCrypto { pub fn derive(shared_secret: &[u8; 32], session_id: &str) -> Self { - let salt = b"VNOX-LNEx-KDF-v1"; + let salt = b"VNOX-LNEx-KDF-v2"; let hk = Hkdf::::new(Some(salt), shared_secret); let mut keys = [0u8; 64]; @@ -40,24 +40,16 @@ impl SessionCrypto { } } - pub fn encrypt_c2s(&self, seq: u64, plaintext: &[u8]) -> Vec { - cipher::encrypt_with_key(&self.c2s_key, &self.cid, seq, plaintext) + pub fn encrypt_c2s_aad(&self, seq: u64, plaintext: &[u8], aad: &[u8]) -> Vec { + cipher::encrypt_with_aad(&self.c2s_key, &self.cid, seq, plaintext, aad) } - - pub fn decrypt_s2c(&self, seq: u64, ciphertext: &[u8]) -> anyhow::Result> { - cipher::decrypt_with_key(&self.s2c_key, &self.cid, seq, ciphertext) - } - - pub fn decrypt_c2s(&self, seq: u64, ciphertext: &[u8]) -> anyhow::Result> { - cipher::decrypt_with_key(&self.c2s_key, &self.cid, seq, ciphertext) - } - - pub fn c2s_key(&self) -> &[u8; 32] { - &self.c2s_key - } - - pub fn cid(&self) -> &[u8; 8] { - &self.cid + pub fn decrypt_s2c_aad( + &self, + seq: u64, + ciphertext: &[u8], + aad: &[u8], + ) -> anyhow::Result> { + cipher::decrypt_with_aad(&self.s2c_key, &self.cid, seq, ciphertext, aad) } pub fn new_ephemeral() -> (EphemeralSecret, PublicKey) { @@ -82,8 +74,9 @@ mod tests { let crypto = SessionCrypto::derive(&shared_secret, "test-session"); let plaintext = b"hello encrypted world"; - let ct = crypto.encrypt_c2s(42, plaintext); - let decrypted = crypto.decrypt_c2s(42, &ct).unwrap(); + let ct = crypto.encrypt_c2s_aad(42, plaintext, b"header"); + let decrypted = + cipher::decrypt_with_aad(&crypto.c2s_key, &crypto.cid, 42, &ct, b"header").unwrap(); assert_eq!(decrypted, plaintext); } @@ -92,8 +85,8 @@ mod tests { let shared_secret = [0xABu8; 32]; let crypto = SessionCrypto::derive(&shared_secret, "test-session"); - let mut ct = crypto.encrypt_c2s(0, b"data"); + let mut ct = crypto.encrypt_c2s_aad(0, b"data", b"header"); ct[5] ^= 0xFF; - assert!(crypto.decrypt_c2s(0, &ct).is_err()); + assert!(cipher::decrypt_with_aad(&crypto.c2s_key, &crypto.cid, 0, &ct, b"header").is_err()); } } diff --git a/src/net/framing/io.rs b/src/net/framing/io.rs index fea1cd6..8223169 100644 --- a/src/net/framing/io.rs +++ b/src/net/framing/io.rs @@ -1,7 +1,6 @@ use anyhow::{Result, anyhow}; use tokio::io::AsyncReadExt; use tokio::io::AsyncWriteExt; -use tokio::net::TcpStream; use super::super::crypto::SessionCrypto; use super::super::payloads::HDR_SIZE; @@ -11,7 +10,10 @@ use super::parse_header; const ENCRYPTED_FLAG: u16 = 1 << 1; const KNOWN_FLAGS: u16 = ENCRYPTED_FLAG; -pub async fn read_encrypted(stream: &mut TcpStream, crypto: &SessionCrypto) -> Result { +pub async fn read_encrypted( + stream: &mut (impl tokio::io::AsyncRead + Unpin), + crypto: &SessionCrypto, +) -> Result { let mut buf = [0u8; HDR_SIZE]; stream.read_exact(&mut buf).await?; let (packet_id, flags, sequence, payload_length) = parse_header(&buf)?; @@ -25,27 +27,160 @@ pub async fn read_encrypted(stream: &mut TcpStream, crypto: &SessionCrypto) -> R if !encrypted.is_empty() { stream.read_exact(&mut encrypted).await?; } - let payload = crypto.decrypt_s2c(sequence as u64, &encrypted)?; - Ok(RawPacket { packet_id, payload }) + let payload = crypto.decrypt_s2c_aad(sequence as u64, &encrypted, &buf)?; + Ok(RawPacket { + packet_id, + sequence, + payload, + }) } pub async fn write_encrypted( - stream: &mut TcpStream, + stream: &mut (impl tokio::io::AsyncWrite + Unpin), id: u16, seq: &mut u32, payload: &[u8], crypto: &SessionCrypto, ) -> Result<()> { - let encrypted = crypto.encrypt_c2s(*seq as u64, payload); + if *seq == u32::MAX { + return Err(anyhow!("session sequence exhausted; reconnect required")); + } + if payload.len() > super::super::payloads::MAX_PAYLOAD as usize - 16 { + return Err(anyhow!("payload too large")); + } let mut hdr = [0u8; HDR_SIZE]; hdr[0..2].copy_from_slice(&id.to_be_bytes()); hdr[2..4].copy_from_slice(&ENCRYPTED_FLAG.to_be_bytes()); hdr[4..8].copy_from_slice(&seq.to_be_bytes()); - hdr[8..12].copy_from_slice(&(encrypted.len() as u32).to_be_bytes()); - *seq = seq.wrapping_add(1); + hdr[8..12].copy_from_slice(&((payload.len() + 16) as u32).to_be_bytes()); + let encrypted = crypto.encrypt_c2s_aad(*seq as u64, payload, &hdr); + *seq += 1; let mut frame = Vec::with_capacity(HDR_SIZE + encrypted.len()); frame.extend_from_slice(&hdr); frame.extend_from_slice(&encrypted); stream.write_all(&frame).await?; Ok(()) } + +/// A dedicated reader retains partial frames across command/timer selection. +/// Dropping a session cancels its reader and releases the socket half. +pub struct Reader { + receiver: tokio::sync::mpsc::Receiver>, + task: tokio::task::JoinHandle<()>, +} +impl Reader { + pub fn new( + mut stream: impl tokio::io::AsyncRead + Unpin + Send + 'static, + crypto: SessionCrypto, + ) -> Self { + let (sender, receiver) = tokio::sync::mpsc::channel(32); + let task = tokio::spawn(async move { + let mut expected = 2u32; + loop { + let result = read_encrypted(&mut stream, &crypto) + .await + .and_then(|packet| { + if packet.sequence != expected { + return Err(anyhow!("replayed or reordered control packet")); + } + expected = expected + .checked_add(1) + .ok_or_else(|| anyhow!("session sequence exhausted"))?; + Ok(packet) + }); + let failed = result.is_err(); + if sender.send(result).await.is_err() || failed { + break; + } + } + }); + Self { receiver, task } + } + pub async fn next(&mut self) -> Result { + self.receiver + .recv() + .await + .ok_or_else(|| anyhow!("gateway reader stopped"))? + } +} +impl Drop for Reader { + fn drop(&mut self) { + self.task.abort(); + } +} + +#[cfg(test)] +mod tests { + use super::*; + use hkdf::Hkdf; + use sha2::{Digest, Sha256}; + use tokio::time::{Duration, timeout}; + + fn server_frame(pid: u16, seq: u32, payload: &[u8]) -> Vec { + let mut keys = [0u8; 64]; + Hkdf::::new(Some(b"VNOX-LNEx-KDF-v2"), &[7; 32]) + .expand(b"session-keys", &mut keys) + .unwrap(); + let key: [u8; 32] = keys[32..].try_into().unwrap(); + let cid: [u8; 8] = Sha256::digest(b"fixture-session")[..8].try_into().unwrap(); + let mut header = [0u8; 12]; + header[..2].copy_from_slice(&pid.to_be_bytes()); + header[2..4].copy_from_slice(&ENCRYPTED_FLAG.to_be_bytes()); + header[4..8].copy_from_slice(&seq.to_be_bytes()); + header[8..].copy_from_slice(&((payload.len() + 16) as u32).to_be_bytes()); + let body = + crate::net::crypto::cipher::encrypt_with_aad(&key, &cid, seq as u64, payload, &header); + [header.as_slice(), body.as_slice()].concat() + } + fn crypto() -> SessionCrypto { + SessionCrypto::derive(&[7; 32], "fixture-session") + } + + #[tokio::test] + async fn cancelling_wait_retains_partial_control_frame() { + let (reader, mut writer) = tokio::io::duplex(256); + let mut reader = Reader::new(reader, crypto()); + let frame = server_frame(5, 2, b"protobuf"); + writer.write_all(&frame[..5]).await.unwrap(); + assert!( + timeout(Duration::from_millis(5), reader.next()) + .await + .is_err() + ); + writer.write_all(&frame[5..]).await.unwrap(); + let packet = timeout(Duration::from_secs(1), reader.next()) + .await + .unwrap() + .unwrap(); + assert_eq!(packet.payload, b"protobuf"); + } + #[tokio::test] + async fn replayed_control_sequence_is_rejected() { + let (reader, mut writer) = tokio::io::duplex(256); + let mut reader = Reader::new(reader, crypto()); + let frame = server_frame(5, 2, b""); + writer.write_all(&frame).await.unwrap(); + reader.next().await.unwrap(); + writer.write_all(&frame).await.unwrap(); + assert!(reader.next().await.is_err()); + } + #[tokio::test] + async fn changing_packet_id_invalidates_authentication() { + let (mut reader, mut writer) = tokio::io::duplex(256); + let mut frame = server_frame(5, 2, b"same body"); + frame[1] ^= 1; + writer.write_all(&frame).await.unwrap(); + assert!(read_encrypted(&mut reader, &crypto()).await.is_err()); + } + #[tokio::test] + async fn sequence_exhaustion_never_wraps_nonce() { + let (_, mut writer) = tokio::io::duplex(256); + let mut seq = u32::MAX; + assert!( + write_encrypted(&mut writer, 4, &mut seq, b"", &crypto()) + .await + .is_err() + ); + assert_eq!(seq, u32::MAX); + } +} diff --git a/src/net/framing/mod.rs b/src/net/framing/mod.rs index aa2ffcd..8616fbe 100644 --- a/src/net/framing/mod.rs +++ b/src/net/framing/mod.rs @@ -1,11 +1,11 @@ use anyhow::{Result, anyhow}; use tokio::io::{AsyncReadExt, AsyncWriteExt}; -use tokio::net::TcpStream; use super::payloads::{HDR_SIZE, MAX_PAYLOAD}; pub struct RawPacket { pub packet_id: u16, + pub sequence: u32, pub payload: Vec, } @@ -37,29 +37,45 @@ pub fn decode_frame(bytes: &[u8]) -> Result { return Err(anyhow!("frame too short")); } let hdr: [u8; HDR_SIZE] = bytes[..HDR_SIZE].try_into().expect("header length"); - let (packet_id, _, _, payload_length) = parse_header(&hdr)?; + let (packet_id, flags, sequence, payload_length) = parse_header(&hdr)?; + if flags != 0 { + return Err(anyhow!("unexpected handshake flags")); + } let end = HDR_SIZE + payload_length as usize; if bytes.len() < end { return Err(anyhow!("truncated frame")); } Ok(RawPacket { packet_id, + sequence, payload: bytes[HDR_SIZE..end].to_vec(), }) } -pub async fn read(stream: &mut TcpStream) -> Result { +pub async fn read(stream: &mut (impl tokio::io::AsyncRead + Unpin)) -> Result { let mut buf = [0u8; HDR_SIZE]; stream.read_exact(&mut buf).await?; - let (packet_id, _, _, payload_length) = parse_header(&buf)?; + let (packet_id, flags, sequence, payload_length) = parse_header(&buf)?; + if flags != 0 { + return Err(anyhow!("unexpected handshake flags")); + } let mut payload = vec![0u8; payload_length as usize]; if !payload.is_empty() { stream.read_exact(&mut payload).await?; } - Ok(RawPacket { packet_id, payload }) + Ok(RawPacket { + packet_id, + sequence, + payload, + }) } -pub async fn write(stream: &mut TcpStream, id: u16, seq: &mut u32, payload: &[u8]) -> Result<()> { +pub async fn write( + stream: &mut (impl tokio::io::AsyncWrite + Unpin), + id: u16, + seq: &mut u32, + payload: &[u8], +) -> Result<()> { let frame = encode_frame(id, seq, payload); stream.write_all(&frame).await?; Ok(()) diff --git a/src/net/handshake.rs b/src/net/handshake.rs index fb00081..536ebd4 100644 --- a/src/net/handshake.rs +++ b/src/net/handshake.rs @@ -1,5 +1,6 @@ +use crate::net::wire; use anyhow::{Result, anyhow}; -use tokio::net::TcpStream; + use tracing::{debug, info}; use super::crypto::SessionCrypto; @@ -12,7 +13,7 @@ use crate::identity::Identity; /// Returns (session_id, node_name, private_mode, crypto). /// All subsequent packets must be encrypted with `crypto`. pub async fn run( - stream: &mut TcpStream, + stream: &mut (impl tokio::io::AsyncRead + tokio::io::AsyncWrite + Unpin), identity: &Identity, seq: &mut u32, ) -> Result<(String, String, bool, SessionCrypto)> { @@ -21,57 +22,94 @@ pub async fn run( if pkt.packet_id != PID_HELLO { return Err(anyhow!("expected HELLO, got 0x{:04X}", pkt.packet_id)); } - let hello: HelloPayload = serde_json::from_slice(&pkt.payload)?; + let hello: HelloPayload = wire::decode(&pkt.payload)?; debug!("← HELLO node={}", hello.node_name); + if pkt.sequence != 0 { + return Err(anyhow!("invalid HELLO sequence")); + } + let server_public: [u8; 32] = hello + .server_pubkey + .as_slice() + .try_into() + .map_err(|_| anyhow!("invalid server identity key"))?; + let server_key = ed25519_dalek::VerifyingKey::from_bytes(&server_public)?; + let signature = ed25519_dalek::Signature::from_slice(&hello.server_signature)?; + let mut unsigned_hello = hello.clone(); + unsigned_hello.server_signature.clear(); + let mut hello_transcript = b"VNOX-LNEx-v2-server-hello\0".to_vec(); + hello_transcript.extend_from_slice(&wire::encode(&unsigned_hello)?); + server_key + .verify_strict(&hello_transcript, &signature) + .map_err(|_| anyhow!("server handshake signature verification failed"))?; + // Keep full signed hello for the client-auth transcript before moving fields. + let signed_hello = wire::encode(&hello)?; + // Generate our own ephemeral X25519 keypair let (eph_sk, eph_pk) = SessionCrypto::new_ephemeral(); - let eph_pk_hex = hex::encode(eph_pk.as_bytes()); + let eph_pk_hex = eph_pk.as_bytes().to_vec(); // Parse server's ephemeral key from HELLO - let server_eph_raw = - hex::decode(&hello.server_eph_pubkey).map_err(|_| anyhow!("bad server_eph_pubkey hex"))?; + let server_eph_raw = hello.server_eph_pubkey; let server_eph_key: [u8; 32] = server_eph_raw .try_into() .map_err(|_| anyhow!("bad server_eph_pubkey length"))?; let server_eph_pk = x25519_dalek::PublicKey::from(server_eph_key); // AUTH — include our ephemeral public key - let nonce = hex::decode(&hello.challenge_nonce)?; + if hello.lnex_version != LNEX_VERSION { + return Err(anyhow!("unsupported LNEx version")); + } + if hello.server_pubkey.len() != 32 || hello.challenge_nonce.len() != 32 { + return Err(anyhow!("invalid server handshake key/challenge")); + } let sk = identity .signing_key() .ok_or_else(|| anyhow!("no signing key"))?; use ed25519_dalek::Signer; - let sig = sk.sign(&nonce); - let auth = AuthPayload { - client_pubkey: identity.pubkey_hex.clone(), + let mut auth = AuthPayload { + client_pubkey: sk.verifying_key().to_bytes().to_vec(), nickname: identity.nickname.clone(), lnex_version: LNEX_VERSION.into(), - signature: hex::encode(sig.to_bytes()), + signature: Vec::new(), client_eph_pubkey: eph_pk_hex, }; - framing::write(stream, PID_AUTH, seq, &serde_json::to_vec(&auth)?).await?; + let mut auth_transcript = b"VNOX-LNEx-v2-client-auth\0".to_vec(); + auth_transcript.extend_from_slice(&(signed_hello.len() as u32).to_be_bytes()); + auth_transcript.extend_from_slice(&signed_hello); + auth_transcript.extend_from_slice(&wire::encode(&auth)?); + auth.signature = sk.sign(&auth_transcript).to_bytes().to_vec(); + framing::write(stream, PID_AUTH, seq, &wire::encode(&auth)?).await?; debug!("→ AUTH nick={}", identity.nickname); // SESSION let pkt = framing::read(stream).await?; if pkt.packet_id == PID_ERROR { - let e: ErrorPayload = serde_json::from_slice(&pkt.payload)?; + let e: ErrorPayload = wire::decode(&pkt.payload)?; return Err(anyhow!("auth rejected: {}", e.message)); } if pkt.packet_id != PID_SESSION { return Err(anyhow!("expected SESSION, got 0x{:04X}", pkt.packet_id)); } - let sess: SessionPayload = serde_json::from_slice(&pkt.payload)?; + if pkt.sequence != 1 { + return Err(anyhow!("invalid SESSION sequence")); + } + let sess: SessionPayload = wire::decode(&pkt.payload)?; // Derive encryption keys from ECDH shared secret let shared_secret = SessionCrypto::ecdh(eph_sk, &server_eph_pk); + if !shared_secret.was_contributory() { + return Err(anyhow!("invalid server ECDH key")); + } + if sess.session_id.is_empty() || sess.token.is_empty() { + return Err(anyhow!("invalid session response")); + } let crypto = SessionCrypto::derive(shared_secret.as_bytes(), &sess.session_id); info!( "authenticated — session {} encrypted", - &sess.session_id[..8] + sess.session_id.chars().take(8).collect::() ); Ok((sess.session_id, hello.node_name, hello.private_mode, crypto)) diff --git a/src/net/internal/dispatch/channel.rs b/src/net/internal/dispatch/channel.rs index cb914e6..dfe8c2b 100644 --- a/src/net/internal/dispatch/channel.rs +++ b/src/net/internal/dispatch/channel.rs @@ -1,5 +1,5 @@ +use crate::net::wire; use anyhow::{Result, anyhow}; -use serde_json; use tokio::sync::mpsc; use super::super::super::payloads::*; @@ -9,7 +9,7 @@ use super::super::super::types::{ChannelListItem as InfoChannelListItem, ChatMsg pub async fn handle(pid: u16, payload: &[u8], tx: &mpsc::Sender) -> Result<()> { match pid { PID_CHANNEL_STATE => { - let p: ChannelStatePayload = serde_json::from_slice(payload)?; + let p: ChannelStatePayload = wire::decode(payload)?; let _ = tx .send(NetEvent::ChannelState { channel_id: p.channel_id, @@ -30,7 +30,7 @@ pub async fn handle(pid: u16, payload: &[u8], tx: &mpsc::Sender) -> Re .await; } PID_CHANNEL_CREATE => { - let p: ChannelCreatePayload = serde_json::from_slice(payload)?; + let p: ChannelCreatePayload = wire::decode(payload)?; let _ = tx .send(NetEvent::ChannelCreated { channel_id: p.channel_id, @@ -41,7 +41,7 @@ pub async fn handle(pid: u16, payload: &[u8], tx: &mpsc::Sender) -> Re .await; } PID_CHANNEL_DELETE => { - let p: ChannelDeletePayload = serde_json::from_slice(payload)?; + let p: ChannelDeletePayload = wire::decode(payload)?; let _ = tx .send(NetEvent::ChannelDeleted { channel_id: p.channel_id, @@ -49,7 +49,7 @@ pub async fn handle(pid: u16, payload: &[u8], tx: &mpsc::Sender) -> Re .await; } PID_CHANNEL_LIST => { - let p: ChannelListPayload = serde_json::from_slice(payload)?; + let p: ChannelListPayload = wire::decode(payload)?; let _ = tx .send(NetEvent::ChannelListEvent { channels: p @@ -66,7 +66,7 @@ pub async fn handle(pid: u16, payload: &[u8], tx: &mpsc::Sender) -> Re .await; } PID_CHAT_MESSAGE => { - let p: ChatMessagePayload = serde_json::from_slice(payload)?; + let p: ChatMessagePayload = wire::decode(payload)?; let _ = tx .send(NetEvent::ChatMessage { message_id: p.message_id, @@ -80,7 +80,7 @@ pub async fn handle(pid: u16, payload: &[u8], tx: &mpsc::Sender) -> Re .await; } PID_CHAT_HISTORY => { - let p: ChatHistoryPayload = serde_json::from_slice(payload)?; + let p: ChatHistoryPayload = wire::decode(payload)?; let _ = tx .send(NetEvent::ChatHistory { channel_id: p.channel_id, @@ -99,7 +99,7 @@ pub async fn handle(pid: u16, payload: &[u8], tx: &mpsc::Sender) -> Re .await; } PID_USER_JOIN => { - let p: UserJoinPayload = serde_json::from_slice(payload)?; + let p: UserJoinPayload = wire::decode(payload)?; let _ = tx .send(NetEvent::UserJoin { channel_id: p.channel_id, @@ -109,7 +109,7 @@ pub async fn handle(pid: u16, payload: &[u8], tx: &mpsc::Sender) -> Re .await; } PID_USER_LEAVE => { - let p: UserLeavePayload = serde_json::from_slice(payload)?; + let p: UserLeavePayload = wire::decode(payload)?; let _ = tx .send(NetEvent::UserLeave { channel_id: p.channel_id, @@ -118,7 +118,7 @@ pub async fn handle(pid: u16, payload: &[u8], tx: &mpsc::Sender) -> Re .await; } PID_PONG => { - let p: PongPayload = serde_json::from_slice(payload)?; + let p: PongPayload = wire::decode(payload)?; let now = std::time::SystemTime::now() .duration_since(std::time::UNIX_EPOCH) .unwrap_or_default() @@ -127,20 +127,17 @@ pub async fn handle(pid: u16, payload: &[u8], tx: &mpsc::Sender) -> Re let _ = tx.send(NetEvent::LatencyUpdate { rtt_ms }).await; } PID_READ_RECEIPT_BROADCAST => { - let p: serde_json::Value = serde_json::from_slice(payload)?; + let p: ReadReceiptBroadcast = wire::decode(payload)?; let _ = tx .send(NetEvent::ReadReceiptBroadcast { - channel_id: p["channel_id"].as_str().unwrap_or_default().into(), - user_id: p["user_id"].as_str().unwrap_or_default().into(), - last_read_message_id: p["last_read_message_id"] - .as_str() - .unwrap_or_default() - .into(), + channel_id: p.channel_id, + user_id: p.user_id, + last_read_message_id: p.last_read_message_id, }) .await; } PID_ERROR => { - let p: ErrorPayload = serde_json::from_slice(payload)?; + let p: ErrorPayload = wire::decode(payload)?; let _ = tx .send(NetEvent::Error { code: p.code, @@ -150,12 +147,12 @@ pub async fn handle(pid: u16, payload: &[u8], tx: &mpsc::Sender) -> Re } PID_DISCONNECT => return Err(anyhow!("server disconnected")), PID_TYPING_START => { - let p: serde_json::Value = serde_json::from_slice(payload)?; + let p: TypingBroadcast = wire::decode(payload)?; let _ = tx .send(NetEvent::TypingStart { - user_id: p["user_id"].as_str().unwrap_or_default().into(), - nickname: p["nickname"].as_str().unwrap_or_default().into(), - channel_id: p["channel_id"].as_str().unwrap_or_default().into(), + user_id: p.user_id, + nickname: p.nickname, + channel_id: p.channel_id, }) .await; } diff --git a/src/net/internal/dispatch/dm.rs b/src/net/internal/dispatch/dm.rs index c4e5be7..c625346 100644 --- a/src/net/internal/dispatch/dm.rs +++ b/src/net/internal/dispatch/dm.rs @@ -1,5 +1,5 @@ +use crate::net::wire; use anyhow::Result; -use serde_json; use tokio::sync::mpsc; use tracing::debug; @@ -9,7 +9,7 @@ use super::super::super::types::*; pub async fn handle(pid: u16, payload: &[u8], tx: &mpsc::Sender) -> Result<()> { match pid { PID_DM_START => { - let p: DmStartResponsePayload = serde_json::from_slice(payload)?; + let p: DmStartResponsePayload = wire::decode(payload)?; let _ = tx .send(NetEvent::DmStart { dm_id: p.dm_id, @@ -31,7 +31,7 @@ pub async fn handle(pid: u16, payload: &[u8], tx: &mpsc::Sender) -> Re .await; } PID_DM_MESSAGE => { - let p: DmMessagePayload = serde_json::from_slice(payload)?; + let p: DmMessagePayload = wire::decode(payload)?; let _ = tx .send(NetEvent::DmMessage { dm_id: p.dm_id, @@ -42,7 +42,7 @@ pub async fn handle(pid: u16, payload: &[u8], tx: &mpsc::Sender) -> Re .await; } PID_DM_HISTORY => { - let p: DmHistoryPayload = serde_json::from_slice(payload)?; + let p: DmHistoryPayload = wire::decode(payload)?; let _ = tx .send(NetEvent::DmHistory { dm_id: p.dm_id, @@ -61,7 +61,7 @@ pub async fn handle(pid: u16, payload: &[u8], tx: &mpsc::Sender) -> Re .await; } PID_FRIEND_LIST => { - let p: FriendListPayload = serde_json::from_slice(payload)?; + let p: FriendListPayload = wire::decode(payload)?; let _ = tx .send(NetEvent::FriendList { friends: p @@ -78,7 +78,7 @@ pub async fn handle(pid: u16, payload: &[u8], tx: &mpsc::Sender) -> Re .await; } PID_MESSAGE_REACTION_ADD => { - let p: ReactionPayload = serde_json::from_slice(payload)?; + let p: ReactionPayload = wire::decode(payload)?; let _ = tx .send(NetEvent::ReactionAdded { channel_id: p.channel_id, @@ -89,7 +89,7 @@ pub async fn handle(pid: u16, payload: &[u8], tx: &mpsc::Sender) -> Re .await; } PID_MESSAGE_REACTION_REMOVE => { - let p: ReactionPayload = serde_json::from_slice(payload)?; + let p: ReactionPayload = wire::decode(payload)?; let _ = tx .send(NetEvent::ReactionRemoved { channel_id: p.channel_id, @@ -100,7 +100,7 @@ pub async fn handle(pid: u16, payload: &[u8], tx: &mpsc::Sender) -> Re .await; } PID_MESSAGE_EDIT => { - let p: ChatMessagePayload = serde_json::from_slice(payload)?; + let p: ChatMessagePayload = wire::decode(payload)?; let _ = tx .send(NetEvent::MessageEdited { channel_id: p.channel_id, @@ -111,7 +111,7 @@ pub async fn handle(pid: u16, payload: &[u8], tx: &mpsc::Sender) -> Re .await; } PID_MESSAGE_DELETE => { - let p: MessageDeletePayload = serde_json::from_slice(payload)?; + let p: MessageDeletePayload = wire::decode(payload)?; let _ = tx .send(NetEvent::MessageDeleted { channel_id: p.channel_id, @@ -120,9 +120,9 @@ pub async fn handle(pid: u16, payload: &[u8], tx: &mpsc::Sender) -> Re .await; } PID_FRIEND_EVENT => { - let p: FriendEventPayload = serde_json::from_slice(payload)?; - match p.event_type.as_str() { - "request" => { + let p: FriendEventPayload = wire::decode(payload)?; + match p.event.as_str() { + "request" | "REQUEST_RECEIVED" => { let _ = tx .send(NetEvent::FriendRequested { user_id: p.user_id, @@ -130,7 +130,7 @@ pub async fn handle(pid: u16, payload: &[u8], tx: &mpsc::Sender) -> Re }) .await; } - "accepted" => { + "accepted" | "REQUEST_ACCEPTED" => { let _ = tx .send(NetEvent::FriendAccepted { user_id: p.user_id, @@ -138,16 +138,16 @@ pub async fn handle(pid: u16, payload: &[u8], tx: &mpsc::Sender) -> Re }) .await; } - "removed" => { + "removed" | "FRIEND_REMOVED" => { let _ = tx .send(NetEvent::FriendRemoved { user_id: p.user_id }) .await; } - _ => debug!("unhandled friend event: {}", p.event_type), + _ => debug!("unhandled friend event: {}", p.event), } } PID_E2EE_DM_KEY_EXCHANGE => { - let p: E2eeDmKeyExchangePayload = serde_json::from_slice(payload)?; + let p: E2eeDmKeyExchangePayload = wire::decode(payload)?; let _ = tx .send(NetEvent::E2eeDmKeyExchange { dm_id: p.dm_id, @@ -156,13 +156,13 @@ pub async fn handle(pid: u16, payload: &[u8], tx: &mpsc::Sender) -> Re .await; } PID_E2EE_DM_KEY_EXCHANGE_ACK => { - let p: E2eeDmKeyExchangeAckPayload = serde_json::from_slice(payload)?; + let p: E2eeDmKeyExchangeAckPayload = wire::decode(payload)?; let _ = tx .send(NetEvent::E2eeDmKeyExchangeAck { dm_id: p.dm_id }) .await; } PID_E2EE_DM_MESSAGE => { - let p: E2eeDmMessagePayload = serde_json::from_slice(payload)?; + let p: E2eeDmMessagePayload = wire::decode(payload)?; let _ = tx .send(NetEvent::E2eeDmMessage { dm_id: p.dm_id, @@ -173,7 +173,7 @@ pub async fn handle(pid: u16, payload: &[u8], tx: &mpsc::Sender) -> Re .await; } PID_E2EE_DM_HISTORY => { - let p: E2eeDmHistoryPayload = serde_json::from_slice(payload)?; + let p: E2eeDmHistoryPayload = wire::decode(payload)?; let _ = tx .send(NetEvent::E2eeDmHistory { dm_id: p.dm_id, diff --git a/src/net/internal/dispatch/mod.rs b/src/net/internal/dispatch/mod.rs index 4435209..6e96216 100644 --- a/src/net/internal/dispatch/mod.rs +++ b/src/net/internal/dispatch/mod.rs @@ -11,7 +11,35 @@ mod social; pub async fn incoming(pid: u16, payload: &[u8], tx: &mpsc::Sender) -> Result<()> { match pid { - PID_CHANNEL_STATE + PID_MEDIA_EVENT => { + let event = crate::net::wire::decode(payload)?; + let _ = tx.send(NetEvent::MediaEvent(event)).await; + } + PID_WEBHOOK_RESULT => { + let result: crate::net::wire::WebhookResult = crate::net::wire::decode(payload)?; + let _ = tx + .send(NetEvent::WebhookResult(WebhookManagementResult { + hook: result.hook, + secret: result.secret, + request_id: result.request_id, + })) + .await; + } + PID_WEBHOOK_ERROR_RESULT => { + let _ = tx + .send(NetEvent::WebhookError(crate::net::wire::decode(payload)?)) + .await; + } + PID_WEBHOOK_LIST_RESULT => { + let _ = tx + .send(NetEvent::WebhookList(crate::net::wire::decode(payload)?)) + .await; + } + + PID_CHANNEL_CREATE + | PID_CHANNEL_DELETE + | PID_CHANNEL_LIST + | PID_CHANNEL_STATE | PID_CHAT_MESSAGE | PID_CHAT_HISTORY | PID_USER_JOIN @@ -36,7 +64,13 @@ pub async fn incoming(pid: u16, payload: &[u8], tx: &mpsc::Sender) -> | PID_E2EE_DM_MESSAGE | PID_E2EE_DM_HISTORY => dm::handle(pid, payload, tx).await?, - PID_GUILD_LIST + PID_BLOCK_LIST + | PID_BLOCK_USER + | PID_UNBLOCK_USER + | PID_GUILD_AUDIT_LOG + | PID_GUILD_MEMBER_LIST + | PID_GUILD_ROLE_LIST + | PID_GUILD_LIST | PID_GUILD_CREATE | PID_GUILD_DELETE | PID_GUILD_MEMBER_JOIN diff --git a/src/net/internal/dispatch/social.rs b/src/net/internal/dispatch/social.rs index 0af184e..0b04eac 100644 --- a/src/net/internal/dispatch/social.rs +++ b/src/net/internal/dispatch/social.rs @@ -1,5 +1,5 @@ +use crate::net::wire; use anyhow::Result; -use serde_json; use tokio::sync::mpsc; use super::super::super::payloads::*; @@ -8,35 +8,35 @@ use super::super::super::types::*; pub async fn handle(pid: u16, payload: &[u8], tx: &mpsc::Sender) -> Result<()> { match pid { PID_GUILD_LIST => { - let p: GuildListPayload = serde_json::from_slice(payload)?; + let p: GuildListPayload = wire::decode(payload)?; let _ = tx .send(NetEvent::GuildList { guilds: p .guilds .into_iter() .map(|g| GuildInfo { - guild_id: g.guild_id, + guild_id: g.id, name: g.name, owner_id: g.owner_id, - icon_url: g.icon_url, - member_count: g.member_count, + icon_url: None, + member_count: g.member_count.clamp(0, u32::MAX as i64) as u32, }) .collect(), }) .await; } PID_GUILD_CREATE => { - let p: GuildInfoPayload = serde_json::from_slice(payload)?; + let p: GuildInfoPayload = wire::decode(payload)?; let _ = tx .send(NetEvent::GuildCreated { - guild_id: p.guild_id, + guild_id: p.id, name: p.name, owner_id: p.owner_id, }) .await; } PID_GUILD_DELETE => { - let p: GuildDeletePayload = serde_json::from_slice(payload)?; + let p: GuildDeletePayload = wire::decode(payload)?; let _ = tx .send(NetEvent::GuildDeleted { guild_id: p.guild_id, @@ -44,26 +44,26 @@ pub async fn handle(pid: u16, payload: &[u8], tx: &mpsc::Sender) -> Re .await; } PID_GUILD_MEMBER_JOIN => { - let p: serde_json::Value = serde_json::from_slice(payload)?; + let p: crate::net::wire::GuildMemberJoin = wire::decode(payload)?; let _ = tx .send(NetEvent::GuildMemberJoined { - guild_id: p["guild_id"].as_str().unwrap_or_default().into(), - user_id: p["user_id"].as_str().unwrap_or_default().into(), - nickname: p["nickname"].as_str().unwrap_or_default().into(), + guild_id: p.guild_id, + user_id: String::new(), + nickname: String::new(), }) .await; } PID_GUILD_MEMBER_LEAVE => { - let p: serde_json::Value = serde_json::from_slice(payload)?; + let p: crate::net::wire::GuildMemberLeave = wire::decode(payload)?; let _ = tx .send(NetEvent::GuildMemberLeft { - guild_id: p["guild_id"].as_str().unwrap_or_default().into(), - user_id: p["user_id"].as_str().unwrap_or_default().into(), + guild_id: p.guild_id, + user_id: p.user_id, }) .await; } PID_USER_ROLE_UPDATE => { - let p: UserRoleUpdatePayload = serde_json::from_slice(payload)?; + let p: UserRoleUpdatePayload = wire::decode(payload)?; let _ = tx .send(NetEvent::UserColor { user_id: p.user_id, @@ -72,7 +72,7 @@ pub async fn handle(pid: u16, payload: &[u8], tx: &mpsc::Sender) -> Re .await; } PID_PRESENCE_SYNC => { - let p: PresenceSyncPayload = serde_json::from_slice(payload)?; + let p: PresenceSyncPayload = wire::decode(payload)?; let _ = tx .send(NetEvent::PresenceSync { presences: p @@ -81,8 +81,8 @@ pub async fn handle(pid: u16, payload: &[u8], tx: &mpsc::Sender) -> Re .map(|p| PresenceInfo { user_id: p.user_id, status: p.status, - custom_status: p.custom_status, - activity: p.activity, + custom_status: None, + activity: p.activity_type, activity_text: p.activity_text, }) .collect(), @@ -90,27 +90,27 @@ pub async fn handle(pid: u16, payload: &[u8], tx: &mpsc::Sender) -> Re .await; } PID_PRESENCE_EVENT => { - let p: PresenceEventPayload = serde_json::from_slice(payload)?; + let p: PresenceEventPayload = wire::decode(payload)?; let _ = tx .send(NetEvent::PresenceUpdated { user_id: p.user_id, status: p.status, - custom_status: p.custom_status, - activity: p.activity, + custom_status: None, + activity: p.activity_type, }) .await; } PID_GUILD_MEMBER_KICK => { - let p: serde_json::Value = serde_json::from_slice(payload)?; + let p: crate::net::wire::GuildMemberKick = wire::decode(payload)?; let _ = tx .send(NetEvent::GuildMemberKicked { - guild_id: p["guild_id"].as_str().unwrap_or_default().into(), - user_id: p["user_id"].as_str().unwrap_or_default().into(), + guild_id: p.guild_id, + user_id: p.user_id, }) .await; } PID_INVITE_CREATE => { - let p: InviteInfoPayload = serde_json::from_slice(payload)?; + let p: InviteInfoPayload = wire::decode(payload)?; let _ = tx .send(NetEvent::InviteCreated { id: p.id, @@ -126,16 +126,16 @@ pub async fn handle(pid: u16, payload: &[u8], tx: &mpsc::Sender) -> Re .await; } PID_INVITE_ACCEPT => { - let p: serde_json::Value = serde_json::from_slice(payload)?; + let p: crate::net::wire::InviteAccept = wire::decode(payload)?; let _ = tx .send(NetEvent::InviteAccepted { - guild_id: p["guild_id"].as_str().unwrap_or_default().into(), - guild_name: p["guild_name"].as_str().unwrap_or_default().into(), + guild_id: p.guild_id, + guild_name: p.guild_name, }) .await; } PID_INVITE_DELETE => { - let p: InviteDeletePayload = serde_json::from_slice(payload)?; + let p: InviteDeletePayload = wire::decode(payload)?; let _ = tx .send(NetEvent::InviteDeleted { guild_id: p.guild_id, @@ -144,17 +144,17 @@ pub async fn handle(pid: u16, payload: &[u8], tx: &mpsc::Sender) -> Re .await; } PID_ROLE_CREATE => { - let p: serde_json::Value = serde_json::from_slice(payload)?; + let p: crate::net::wire::RoleCreate = wire::decode(payload)?; let _ = tx .send(NetEvent::RoleCreated { - role_id: p["id"].as_str().unwrap_or_default().into(), - guild_id: p["guild_id"].as_str().unwrap_or_default().into(), - name: p["name"].as_str().unwrap_or_default().into(), + role_id: p.id, + guild_id: p.guild_id, + name: p.name, }) .await; } PID_ROLE_DELETE => { - let p: RoleDeletePayload = serde_json::from_slice(payload)?; + let p: RoleDeletePayload = wire::decode(payload)?; let _ = tx .send(NetEvent::RoleDeleted { role_id: p.role_id, @@ -163,7 +163,7 @@ pub async fn handle(pid: u16, payload: &[u8], tx: &mpsc::Sender) -> Re .await; } PID_GUILD_AUDIT_LOG => { - let p: GuildAuditLogPayload = serde_json::from_slice(payload)?; + let p: GuildAuditLogPayload = wire::decode(payload)?; let _ = tx .send(NetEvent::GuildAuditLog { guild_id: p.guild_id, @@ -172,7 +172,7 @@ pub async fn handle(pid: u16, payload: &[u8], tx: &mpsc::Sender) -> Re .await; } PID_GUILD_MEMBER_LIST => { - let p: GuildMemberListPayload = serde_json::from_slice(payload)?; + let p: GuildMemberListPayload = wire::decode(payload)?; let _ = tx .send(NetEvent::GuildMemberList { guild_id: p.guild_id, @@ -181,7 +181,7 @@ pub async fn handle(pid: u16, payload: &[u8], tx: &mpsc::Sender) -> Re .await; } PID_GUILD_ROLE_LIST => { - let p: GuildRoleListPayload = serde_json::from_slice(payload)?; + let p: GuildRoleListPayload = wire::decode(payload)?; let _ = tx .send(NetEvent::GuildRoleList { guild_id: p.guild_id, @@ -190,41 +190,20 @@ pub async fn handle(pid: u16, payload: &[u8], tx: &mpsc::Sender) -> Re .await; } PID_BLOCK_LIST => { - // Server may send {"blocked": [...]} — try to parse properly first, - // fall back to JSON value extraction. - let blocked: Vec = match serde_json::from_slice::(payload) { - Ok(p) => p.blocked, - Err(_) => { - let v: serde_json::Value = serde_json::from_slice(payload)?; - v["blocked"] - .as_array() - .map(|arr| { - arr.iter() - .filter_map(|x| x.as_str().map(String::from)) - .collect() - }) - .unwrap_or_default() - } - }; - let _ = tx.send(NetEvent::BlockList { blocked }).await; + let p: BlockListPayload = wire::decode(payload)?; + let _ = tx.send(NetEvent::BlockList { blocked: p.blocked }).await; } - PID_BLOCK_USER => { - let v: serde_json::Value = serde_json::from_slice(payload).unwrap_or_default(); - let user_id = v["user_id"] - .as_str() - .or_else(|| v["blocked"].as_str()) - .unwrap_or_default() - .to_string(); - let _ = tx.send(NetEvent::BlockedUser { user_id }).await; - } - PID_UNBLOCK_USER => { - let v: serde_json::Value = serde_json::from_slice(payload).unwrap_or_default(); - let user_id = v["user_id"] - .as_str() - .or_else(|| v["unblocked"].as_str()) - .unwrap_or_default() - .to_string(); - let _ = tx.send(NetEvent::UnblockedUser { user_id }).await; + PID_BLOCK_USER | PID_UNBLOCK_USER => { + let p: crate::net::wire::SimpleResponse = wire::decode(payload)?; + if p.ok { + let user_id = p.user_id.unwrap_or_default(); + let event = if pid == PID_BLOCK_USER { + NetEvent::BlockedUser { user_id } + } else { + NetEvent::UnblockedUser { user_id } + }; + let _ = tx.send(event).await; + } } _ => {} } diff --git a/src/net/mod.rs b/src/net/mod.rs index 45c4858..c7862ec 100644 --- a/src/net/mod.rs +++ b/src/net/mod.rs @@ -4,10 +4,15 @@ pub mod handshake; mod internal; pub mod payloads; mod session; +pub mod transport; mod types; pub mod voice; +pub mod wire; -pub use types::{ChatMsg, FriendInfo, GuildInfo, MemberInfo, NetCommand, NetEvent, PresenceInfo}; +pub use types::{ + ChatMsg, FriendInfo, GuildInfo, MemberInfo, NetCommand, NetEvent, PresenceInfo, + WebhookManagementResult, WebhookOperation, +}; use std::sync::Arc; use std::sync::Mutex; diff --git a/src/net/payloads/channel.rs b/src/net/payloads/channel.rs index 6b3927c..fb312fd 100644 --- a/src/net/payloads/channel.rs +++ b/src/net/payloads/channel.rs @@ -1,156 +1,26 @@ -use serde::{Deserialize, Serialize}; +// LNEx protobuf payload aliases. Schema: protocol/lnex.proto. +pub type HelloPayload = crate::net::wire::Hello; +pub type AuthPayload = crate::net::wire::Auth; +pub type SessionPayload = crate::net::wire::Session; +pub type PingPayload = crate::net::wire::Ping; +pub type PongPayload = crate::net::wire::Pong; +pub type JoinChannelPayload = crate::net::wire::JoinChannel; +pub type LeaveChannelPayload = crate::net::wire::LeaveChannel; +pub type ChannelStatePayload = crate::net::wire::ChannelState; +pub type ChannelCreatePayload = crate::net::wire::ChannelCreate; +pub type ChannelDeletePayload = crate::net::wire::ChannelDelete; +pub type ChannelListPayload = crate::net::wire::ChannelList; +pub type ChannelListItem = crate::net::wire::ChannelListItem; +pub type WireMember = crate::net::wire::MemberInfo; +pub type ChatMessagePayload = crate::net::wire::ChatMessage; +pub type ChatHistoryPayload = crate::net::wire::ChatHistory; +pub type ReactionPayload = crate::net::wire::ReactionPayload; +pub type MessageEditPayload = crate::net::wire::MessageEdit; +pub type MessageDeletePayload = crate::net::wire::MessageDelete; +pub type UserJoinPayload = crate::net::wire::UserJoin; +pub type UserLeavePayload = crate::net::wire::UserLeave; +pub type ErrorPayload = crate::net::wire::Error; -#[derive(Serialize, Deserialize, Debug)] -pub struct HelloPayload { - pub lnex_version: String, - pub server_pubkey: String, - pub challenge_nonce: String, - pub node_name: String, - pub server_eph_pubkey: String, - #[serde(default)] - pub private_mode: bool, -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct AuthPayload { - pub client_pubkey: String, - pub nickname: String, - pub lnex_version: String, - pub signature: String, - pub client_eph_pubkey: String, -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct SessionPayload { - pub session_id: String, - pub token: String, - pub expires_at: i64, -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct PingPayload { - pub timestamp: i64, -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct PongPayload { - pub timestamp: i64, -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct JoinChannelPayload { - pub channel_id: String, -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct LeaveChannelPayload { - pub channel_id: String, -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct ChannelStatePayload { - pub channel_id: String, - pub channel_name: String, - pub kind: String, - pub members: Vec, - pub voice_endpoint: String, - #[serde(default)] - pub guild_id: Option, -} - -#[derive(Serialize, Deserialize, Debug, Clone)] -pub struct ChannelCreatePayload { - pub channel_id: String, - pub channel_name: String, - /// "text" or "voice". - pub kind: String, - #[serde(default)] - pub guild_id: Option, -} - -#[derive(Serialize, Deserialize, Debug, Clone)] -pub struct ChannelDeletePayload { - pub channel_id: String, -} - -#[derive(Serialize, Deserialize, Debug, Clone)] -pub struct ChannelListPayload { - pub channels: Vec, -} - -#[derive(Serialize, Deserialize, Debug, Clone)] -pub struct ChannelListItem { - pub channel_id: String, - pub channel_name: String, - pub kind: String, - #[serde(default)] - pub guild_id: Option, -} - -#[derive(Serialize, Deserialize, Debug, Clone)] -pub struct WireMember { - pub user_id: String, - pub nickname: String, - pub in_voice: bool, -} - -#[derive(Serialize, Deserialize, Debug, Clone)] -pub struct ChatMessagePayload { - pub message_id: String, - pub channel_id: String, - pub sender_id: String, - pub content: String, - pub timestamp: i64, - #[serde(default)] - pub edited: bool, - /// Optional message_id this message is replying to. - #[serde(default, skip_serializing_if = "Option::is_none")] - pub reply_to: Option, -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct ChatHistoryPayload { - pub channel_id: String, - pub messages: Vec, -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct ReactionPayload { - pub message_id: String, - pub channel_id: String, - pub emoji: String, - #[serde(default)] - pub user_id: String, -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct MessageEditPayload { - pub message_id: String, - pub channel_id: String, - pub content: String, -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct MessageDeletePayload { - pub message_id: String, - pub channel_id: String, -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct UserJoinPayload { - pub channel_id: String, - pub user_id: String, - pub nickname: String, -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct UserLeavePayload { - pub channel_id: String, - pub user_id: String, -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct ErrorPayload { - pub code: u32, - pub message: String, -} +pub use crate::net::wire::{ + Disconnect, ReadReceipt, ReadReceiptBroadcast, TypingBroadcast, TypingStart, +}; diff --git a/src/net/payloads/dm.rs b/src/net/payloads/dm.rs index fec8cc8..3c0edf3 100644 --- a/src/net/payloads/dm.rs +++ b/src/net/payloads/dm.rs @@ -1,162 +1,23 @@ -use serde::{Deserialize, Serialize}; - -#[derive(Serialize, Deserialize, Debug)] -pub struct E2eeDmKeyExchangePayload { - pub dm_id: String, - pub e2ee_public_key: Vec, -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct E2eeDmKeyExchangeAckPayload { - pub dm_id: String, -} - -#[derive(Serialize, Deserialize, Debug, Clone)] -pub struct E2eeDmMessagePayload { - pub dm_id: String, - pub sender_id: String, - #[serde(with = "serde_bytes")] - pub ciphertext: Vec, - pub timestamp: i64, -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct E2eeDmHistoryPayload { - pub dm_id: String, - #[serde(default)] - pub messages: Vec, - #[serde(default)] - pub limit: Option, -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct DmStartPayload { - pub target_user_id: String, -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct DmStartResponsePayload { - pub dm_id: String, - pub other_user_id: String, - pub other_nickname: String, - pub messages: Vec, - #[serde(default)] - pub unread_count: u32, -} - -#[derive(Serialize, Deserialize, Debug, Clone)] -pub struct DmMessagePayload { - pub dm_id: String, - pub sender_id: String, - pub content: String, - pub timestamp: i64, -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct DmHistoryPayload { - pub dm_id: String, - #[serde(default)] - pub messages: Vec, - #[serde(default)] - pub search_query: Option, - #[serde(default)] - pub limit: Option, -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct PresenceUpdatePayload { - pub status: String, - /// Free-form activity type ("playing", "listening", "watching", "streaming"). - #[serde(default, skip_serializing_if = "Option::is_none")] - pub activity_type: Option, - /// Free-form activity text (e.g. "CS2 ranked"). - #[serde(default, skip_serializing_if = "Option::is_none")] - pub activity_text: Option, - /// Free-form custom status text (Discord-style "what's on your mind?"). - #[serde(default, skip_serializing_if = "Option::is_none")] - pub custom_status: Option, -} - -#[derive(Serialize, Deserialize, Debug, Clone)] -pub struct PresenceInfoPayload { - pub user_id: String, - pub status: String, - #[serde(default, skip_serializing_if = "Option::is_none")] - pub custom_status: Option, - #[serde(default, skip_serializing_if = "Option::is_none")] - pub activity: Option, - #[serde(default, skip_serializing_if = "Option::is_none")] - pub activity_text: Option, -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct PresenceSyncPayload { - pub presences: Vec, -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct PresenceEventPayload { - pub user_id: String, - pub status: String, - #[serde(default, skip_serializing_if = "Option::is_none")] - pub custom_status: Option, - #[serde(default, skip_serializing_if = "Option::is_none")] - pub activity: Option, - #[serde(default, skip_serializing_if = "Option::is_none")] - pub activity_text: Option, -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct FriendRequestPayload { - pub target_user_id: String, -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct FriendAcceptPayload { - pub user_id: String, -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct FriendDeclinePayload { - pub user_id: String, -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct FriendRemovePayload { - pub user_id: String, -} - -#[derive(Serialize, Deserialize, Debug, Clone)] -pub struct FriendInfoPayload { - pub user_id: String, - pub nickname: String, - pub status: String, - pub since: i64, -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct FriendListPayload { - pub friends: Vec, -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct BlockUserPayload { - pub user_id: String, -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct UnblockUserPayload { - pub user_id: String, -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct BlockListPayload { - pub blocked: Vec, -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct FriendEventPayload { - pub event_type: String, - pub user_id: String, - pub nickname: Option, -} +// LNEx protobuf payload aliases. Schema: protocol/lnex.proto. +pub type E2eeDmKeyExchangePayload = crate::net::wire::E2eeDmKeyExchange; +pub type E2eeDmKeyExchangeAckPayload = crate::net::wire::E2eeDmKeyExchangeAck; +pub type E2eeDmMessagePayload = crate::net::wire::E2eeDmMessage; +pub type E2eeDmHistoryPayload = crate::net::wire::E2eeDmHistory; +pub type DmStartPayload = crate::net::wire::DmStart; +pub type DmStartResponsePayload = crate::net::wire::DmStartResponse; +pub type DmMessagePayload = crate::net::wire::DmMessage; +pub type DmHistoryPayload = crate::net::wire::DmHistory; +pub type PresenceUpdatePayload = crate::net::wire::PresenceUpdate; +pub type PresenceInfoPayload = crate::net::wire::PresenceInfo; +pub type PresenceSyncPayload = crate::net::wire::PresenceSync; +pub type PresenceEventPayload = crate::net::wire::PresenceEvent; +pub type FriendRequestPayload = crate::net::wire::FriendRequest; +pub type FriendAcceptPayload = crate::net::wire::FriendAccept; +pub type FriendDeclinePayload = crate::net::wire::FriendDecline; +pub type FriendRemovePayload = crate::net::wire::FriendRemove; +pub type FriendInfoPayload = crate::net::wire::FriendInfo; +pub type FriendListPayload = crate::net::wire::FriendList; +pub type BlockUserPayload = crate::net::wire::BlockUser; +pub type UnblockUserPayload = crate::net::wire::UnblockUser; +pub type BlockListPayload = crate::net::wire::BlockList; +pub type FriendEventPayload = crate::net::wire::FriendEvent; diff --git a/src/net/payloads/guild.rs b/src/net/payloads/guild.rs index d59aee4..0bf4c87 100644 --- a/src/net/payloads/guild.rs +++ b/src/net/payloads/guild.rs @@ -1,176 +1,25 @@ -use serde::{Deserialize, Serialize}; - -#[derive(Serialize, Deserialize, Debug)] -pub struct GuildCreatePayload { - pub name: String, - pub icon_url: Option, -} - -#[derive(Serialize, Deserialize, Debug, Clone)] -pub struct GuildInfoPayload { - pub guild_id: String, - pub name: String, - pub owner_id: String, - pub icon_url: Option, - pub member_count: u32, -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct GuildListPayload { - pub guilds: Vec, -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct GuildDeletePayload { - pub guild_id: String, -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct GuildJoinPayload { - pub guild_id: String, - pub invite_code: Option, -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct GuildLeavePayload { - pub guild_id: String, -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct UserRoleUpdatePayload { - pub user_id: String, - pub guild_id: String, - pub color: String, -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct InviteCreatePayload { - pub guild_id: String, - pub max_uses: Option, - pub expires_in_seconds: Option, -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct InviteAcceptPayload { - pub code: String, -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct InviteDeletePayload { - pub guild_id: String, - pub invite_id: String, -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct RoleCreatePayload { - pub guild_id: String, - pub name: String, - pub color: Option, - pub permissions: Option, -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct RoleDeletePayload { - pub guild_id: String, - pub role_id: String, -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct GuildMemberKickPayload { - pub guild_id: String, - pub user_id: String, -} - -#[derive(Serialize, Deserialize, Debug, Clone)] -pub struct InviteInfoPayload { - pub id: String, - pub guild_id: String, - pub guild_name: String, - pub code: String, - pub creator_id: String, - pub max_uses: Option, - pub uses: i64, - pub expires_at: Option, - pub created_at: i64, -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct GuildAuditLogFetchPayload { - pub guild_id: String, - #[serde(default = "default_audit_limit")] - pub limit: i64, -} - -fn default_audit_limit() -> i64 { - 50 -} - -#[derive(Serialize, Deserialize, Debug, Clone)] -pub struct AuditLogEntryPayload { - pub id: String, - pub guild_id: String, - pub actor_id: String, - pub action: String, - #[serde(default)] - pub target_id: Option, - #[serde(default)] - pub target_type: Option, - #[serde(default)] - pub reason: Option, - pub created_at: i64, -} - -#[derive(Serialize, Deserialize, Debug, Clone)] -pub struct GuildAuditLogPayload { - pub guild_id: String, - pub entries: Vec, -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct GuildMemberListFetchPayload { - pub guild_id: String, -} - -#[derive(Serialize, Deserialize, Debug, Clone)] -pub struct GuildMemberInfoPayload { - pub user_id: String, - pub nickname: String, - pub joined_at: i64, - pub role_color: String, - pub role_name: String, - pub is_owner: bool, -} - -#[derive(Serialize, Deserialize, Debug, Clone)] -pub struct GuildMemberListPayload { - pub guild_id: String, - pub members: Vec, -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct RoleAssignPayload { - pub guild_id: String, - pub user_id: String, - pub role_id: String, -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct GuildRoleListFetchPayload { - pub guild_id: String, -} - -#[derive(Serialize, Deserialize, Debug, Clone)] -pub struct GuildRoleInfoPayload { - pub id: String, - pub guild_id: String, - pub name: String, - pub color: String, - pub permissions: u64, - pub position: i32, -} - -#[derive(Serialize, Deserialize, Debug, Clone)] -pub struct GuildRoleListPayload { - pub guild_id: String, - pub roles: Vec, -} +// LNEx protobuf payload aliases. Schema: protocol/lnex.proto. +pub type GuildCreatePayload = crate::net::wire::GuildCreate; +pub type GuildInfoPayload = crate::net::wire::GuildInfo; +pub type GuildListPayload = crate::net::wire::GuildList; +pub type GuildDeletePayload = crate::net::wire::GuildDelete; +pub type GuildJoinPayload = crate::net::wire::GuildMemberJoin; +pub type GuildLeavePayload = crate::net::wire::GuildMemberLeave; +pub type UserRoleUpdatePayload = crate::net::wire::UserRoleUpdate; +pub type InviteCreatePayload = crate::net::wire::InviteCreate; +pub type InviteAcceptPayload = crate::net::wire::InviteAccept; +pub type InviteDeletePayload = crate::net::wire::InviteDelete; +pub type RoleCreatePayload = crate::net::wire::RoleCreate; +pub type RoleDeletePayload = crate::net::wire::RoleDelete; +pub type GuildMemberKickPayload = crate::net::wire::GuildMemberKick; +pub type InviteInfoPayload = crate::net::wire::InviteInfo; +pub type GuildAuditLogFetchPayload = crate::net::wire::GuildAuditLogFetch; +pub type AuditLogEntryPayload = crate::net::wire::AuditLogEntry; +pub type GuildAuditLogPayload = crate::net::wire::GuildAuditLog; +pub type GuildMemberListFetchPayload = crate::net::wire::GuildMemberListFetch; +pub type GuildMemberInfoPayload = crate::net::wire::GuildMemberInfo; +pub type GuildMemberListPayload = crate::net::wire::GuildMemberList; +pub type RoleAssignPayload = crate::net::wire::RoleAssign; +pub type GuildRoleListFetchPayload = crate::net::wire::GuildRoleListFetch; +pub type GuildRoleInfoPayload = crate::net::wire::GuildRoleInfo; +pub type GuildRoleListPayload = crate::net::wire::GuildRoleList; diff --git a/src/net/payloads/mod.rs b/src/net/payloads/mod.rs index 62a3644..1c16b1a 100644 --- a/src/net/payloads/mod.rs +++ b/src/net/payloads/mod.rs @@ -67,7 +67,7 @@ pub const PID_FRIEND_EVENT: u16 = 0x0158; pub const PID_ERROR: u16 = 0x00F0; pub const PID_DISCONNECT: u16 = 0x00FF; -pub const LNEX_VERSION: &str = "v1"; +pub const LNEX_VERSION: &str = "v2"; pub const HDR_SIZE: usize = 12; pub const MAX_PAYLOAD: u32 = 4 * 1024 * 1024; @@ -78,3 +78,18 @@ mod guild; pub use channel::*; pub use dm::*; pub use guild::*; + +pub const PID_MEDIA_CONTROL: u16 = 0x0180; +pub const PID_MEDIA_EVENT: u16 = 0x0181; +pub const PID_WEBHOOK_CREATE: u16 = 0x0200; +pub const PID_WEBHOOK_LIST: u16 = 0x0201; +pub const PID_WEBHOOK_ROTATE: u16 = 0x0202; +pub const PID_WEBHOOK_REVOKE: u16 = 0x0203; +pub const PID_WEBHOOK_RESULT: u16 = 0x0204; +pub const PID_WEBHOOK_LIST_RESULT: u16 = 0x0205; + +pub const PID_WEBHOOK_ERROR_RESULT: u16 = 0x0206; + +pub const PID_VOICE_SESSION: u16 = 0x0012; + +pub const PID_VOICE_STATE: u16 = 0x0011; diff --git a/src/net/session/handlers/core.rs b/src/net/session/handlers/core.rs index fdab8e3..27124ee 100644 --- a/src/net/session/handlers/core.rs +++ b/src/net/session/handlers/core.rs @@ -1,18 +1,18 @@ use std::time::Duration; -use anyhow::Result; -use tokio::net::TcpStream; +use anyhow::{Context, Result}; + use tokio::sync::mpsc; use tokio::time::{MissedTickBehavior, interval}; use tracing::warn; use crate::identity::Identity; +use crate::net::wire; use crate::net::{ framing, handshake, internal, payloads::*, types::{NetCommand, NetEvent}, }; -use serde_json; use super::super::switch::ReconnectRequested; use super::{VoiceState, handle_cmd}; @@ -25,11 +25,22 @@ pub async fn session_loop( cmd_rx: &mut mpsc::Receiver, event_tx: &mpsc::Sender, ) -> Result<()> { - let mut stream = TcpStream::connect(address).await?; + let connection = tokio::time::timeout( + Duration::from_secs(10), + crate::net::transport::connect_with_peer(address), + ) + .await + .context("gateway connection timed out")??; + let peer = connection.peer; + let mut stream = connection.stream; let mut seq = 0u32; - let (session_id, node_name, private_mode, crypto) = - handshake::run(&mut stream, identity, &mut seq).await?; + let (session_id, node_name, private_mode, crypto) = tokio::time::timeout( + Duration::from_secs(10), + handshake::run(&mut stream, identity, &mut seq), + ) + .await + .context("gateway handshake timed out")??; let _ = event_tx .send(NetEvent::Connected { session_id: session_id.clone(), @@ -44,31 +55,58 @@ pub async fn session_loop( &mut stream, PID_JOIN_CHANNEL, &mut seq, - &serde_json::to_vec(&JoinChannelPayload { channel_id })?, + &wire::encode(&JoinChannelPayload { channel_id })?, &crypto, ) .await?; } - let mut voice_state = VoiceState::new(&crypto, event_tx).await?; + let (reader, mut stream) = tokio::io::split(stream); + let mut incoming = framing::io::Reader::new(reader, crypto.clone()); + let mut voice_state: Option = None; + let mut used_voice_routes = std::collections::HashSet::new(); let mut voice_endpoint: Option = None; + let mut voice_tick = interval(Duration::from_secs(5)); + voice_tick.set_missed_tick_behavior(MissedTickBehavior::Delay); let mut ping_tick = interval(Duration::from_secs(2)); ping_tick.set_missed_tick_behavior(MissedTickBehavior::Delay); loop { tokio::select! { + _ = voice_tick.tick() => { if let Some(voice)=voice_state.as_mut() {voice.keep_alive().await?;} } _ = ping_tick.tick() => { let ts = now_ms(); - let payload = serde_json::to_vec(&PingPayload { timestamp: ts })?; + let payload = wire::encode(&PingPayload { timestamp: ts })?; framing::io::write_encrypted(&mut stream, PID_PING, &mut seq, &payload, &crypto).await?; } - result = framing::io::read_encrypted(&mut stream, &crypto) => { + result = incoming.next() => { let pkt = result?; + if pkt.packet_id == PID_VOICE_SESSION { + let session:wire::VoiceSession=wire::decode(&pkt.payload)?; + anyhow::ensure!(session.route_id.len()==16 && session.c2s_key.len()==32 && session.s2c_key.len()==32,"invalid voice credentials"); + anyhow::ensure!(used_voice_routes.len()<1024 && used_voice_routes.insert(session.route_id.clone()),"voice credentials must be fresh; reconnect required"); + let mut voice=VoiceState::new(event_tx,peer.is_ipv6()).await?; + if let Some(endpoint)=voice_endpoint {voice.set_endpoint(endpoint).await?;} + voice.configure(session).await?; + voice_state=Some(voice);continue; + } + if pkt.packet_id == PID_VOICE_STATE { + let state:wire::VoiceState=wire::decode(&pkt.payload)?; + if state.state=="disconnected" && state.user_id==identity.pubkey_hex { + if voice_state.as_ref().and_then(|value|value.channel_id())==Some(state.channel_id.as_str()) {voice_state=None;voice_endpoint=None;} + channels.retain(|id|id!=&state.channel_id); + let _=event_tx.send(NetEvent::VoiceRevoked {channel_id:state.channel_id}).await; + } + continue; + } if pkt.packet_id == PID_CHANNEL_STATE - && let Ok(p) = serde_json::from_slice::(&pkt.payload) - && !p.voice_endpoint.is_empty() + && let Ok(p) = wire::decode::(&pkt.payload) + && p.kind == "voice" && !p.voice_endpoint.is_empty() { - voice_endpoint = p.voice_endpoint.parse().ok(); + let mut endpoint:std::net::SocketAddr=p.voice_endpoint.parse()?; + if endpoint.ip().is_unspecified() {endpoint.set_ip(peer.ip());} + voice_endpoint=Some(endpoint); + if let Some(voice)=voice_state.as_mut(){voice.set_endpoint(endpoint).await?;} } internal::dispatch::incoming(pkt.packet_id, &pkt.payload, event_tx).await?; } @@ -76,7 +114,7 @@ pub async fn session_loop( match cmd { None | Some(NetCommand::Disconnect) => { let _ = framing::io::write_encrypted(&mut stream, PID_DISCONNECT, &mut seq, - &serde_json::to_vec(&serde_json::json!({"reason":"bye"})).unwrap(), &crypto).await; + &wire::encode(&Disconnect {reason: "bye".into()})?, &crypto).await; return Ok(()); } Some(NetCommand::Connect { address, auto_reconnect }) => { @@ -92,15 +130,16 @@ pub async fn session_loop( Some(NetCommand::JoinChannel { channel_id }) => { track_channel(channels, &channel_id); framing::io::write_encrypted(&mut stream, PID_JOIN_CHANNEL, &mut seq, - &serde_json::to_vec(&JoinChannelPayload { channel_id })?, &crypto).await?; + &wire::encode(&JoinChannelPayload { channel_id })?, &crypto).await?; } Some(NetCommand::LeaveChannel { channel_id }) => { channels.retain(|c| c != &channel_id); + if voice_state.as_ref().and_then(|value|value.channel_id())==Some(channel_id.as_str()) {voice_state=None;voice_endpoint=None;} framing::io::write_encrypted(&mut stream, PID_LEAVE_CHANNEL, &mut seq, - &serde_json::to_vec(&LeaveChannelPayload { channel_id })?, &crypto).await?; + &wire::encode(&LeaveChannelPayload { channel_id })?, &crypto).await?; } Some(NetCommand::ChannelCreate { channel_id, channel_name, kind, guild_id }) => { - let payload = serde_json::to_vec(&ChannelCreatePayload { + let payload = wire::encode(&ChannelCreatePayload { channel_id, channel_name, kind, @@ -109,11 +148,11 @@ pub async fn session_loop( framing::io::write_encrypted(&mut stream, PID_CHANNEL_CREATE, &mut seq, &payload, &crypto).await?; } Some(NetCommand::ChannelDelete { channel_id }) => { - let payload = serde_json::to_vec(&ChannelDeletePayload { channel_id })?; + let payload = wire::encode(&ChannelDeletePayload { channel_id })?; framing::io::write_encrypted(&mut stream, PID_CHANNEL_DELETE, &mut seq, &payload, &crypto).await?; } Some(NetCommand::ChannelList) => { - framing::io::write_encrypted(&mut stream, PID_CHANNEL_LIST, &mut seq, b"{}", &crypto).await?; + framing::io::write_encrypted(&mut stream, PID_CHANNEL_LIST, &mut seq, b"", &crypto).await?; } Some(NetCommand::SendChat { channel_id, content, reply_to }) => { let msg = ChatMessagePayload { @@ -123,20 +162,13 @@ pub async fn session_loop( edited: false, reply_to, }; - framing::io::write_encrypted(&mut stream, PID_CHAT_MESSAGE, &mut seq, &serde_json::to_vec(&msg)?, &crypto).await?; + framing::io::write_encrypted(&mut stream, PID_CHAT_MESSAGE, &mut seq, &wire::encode(&msg)?, &crypto).await?; } Some(NetCommand::SendVoice { channel_id, voice_seq: voice_pkt_seq, timestamp, opus_data }) => { - let sender_pubkey = identity.verifying_key() - .map(|k| k.to_bytes()) - .unwrap_or([0u8; 32]); - voice_state.send_voice(channel_id, voice_pkt_seq, timestamp, &opus_data, &crypto, voice_endpoint, &sender_pubkey).await?; + if let Some(voice)=voice_state.as_mut(){voice.send(Some(channel_id), voice_pkt_seq, timestamp, &opus_data).await?;} } Some(NetCommand::ReadReceipt { channel_id, last_read_message_id }) => { - let payload = serde_json::to_vec(&serde_json::json!({ - "channel_id": channel_id, - "last_read_message_id": last_read_message_id, - "user_id": identity.pubkey_hex.clone(), - }))?; + let payload = wire::encode(&ReadReceipt {channel_id,last_read_message_id,user_id: identity.pubkey_hex.clone()})?; framing::io::write_encrypted(&mut stream, PID_READ_RECEIPT, &mut seq, &payload, &crypto).await?; } Some(other) => handle_cmd(other, &mut stream, &mut seq, &crypto, identity).await?, diff --git a/src/net/session/handlers/social/friends.rs b/src/net/session/handlers/social/friends.rs index 480c8fc..6804a0a 100644 --- a/src/net/session/handlers/social/friends.rs +++ b/src/net/session/handlers/social/friends.rs @@ -1,60 +1,64 @@ +use crate::net::wire; use anyhow::Result; -use serde_json; -use tokio::net::TcpStream; use crate::net::{crypto::SessionCrypto, framing, payloads::*, types::NetCommand}; pub async fn handle_cmd( cmd: NetCommand, - stream: &mut TcpStream, + stream: &mut (impl tokio::io::AsyncWrite + Unpin), seq: &mut u32, crypto: &SessionCrypto, ) -> Result<()> { match cmd { NetCommand::PresenceUpdate { status, - custom_status, + custom_status: _, activity, activity_text, } => { - let payload = serde_json::to_vec(&PresenceUpdatePayload { + let payload = wire::encode(&PresenceUpdatePayload { status, activity_type: activity, activity_text, - custom_status, })?; framing::io::write_encrypted(stream, PID_PRESENCE_UPDATE, seq, &payload, crypto) .await?; } NetCommand::FriendRequest { target_user_id } => { - let payload = serde_json::to_vec(&FriendRequestPayload { target_user_id })?; + let payload = wire::encode(&FriendRequestPayload { + to_user_id: target_user_id, + })?; framing::io::write_encrypted(stream, PID_FRIEND_REQUEST, seq, &payload, crypto).await?; } NetCommand::FriendAccept { user_id } => { - let payload = serde_json::to_vec(&FriendAcceptPayload { user_id })?; + let payload = wire::encode(&FriendAcceptPayload { + from_user_id: user_id, + })?; framing::io::write_encrypted(stream, PID_FRIEND_ACCEPT, seq, &payload, crypto).await?; } NetCommand::FriendDecline { user_id } => { - let payload = serde_json::to_vec(&FriendDeclinePayload { user_id })?; + let payload = wire::encode(&FriendDeclinePayload { + from_user_id: user_id, + })?; framing::io::write_encrypted(stream, PID_FRIEND_DECLINE, seq, &payload, crypto).await?; } NetCommand::FriendRemove { user_id } => { - let payload = serde_json::to_vec(&FriendRemovePayload { user_id })?; + let payload = wire::encode(&FriendRemovePayload { user_id })?; framing::io::write_encrypted(stream, PID_FRIEND_REMOVE, seq, &payload, crypto).await?; } NetCommand::FriendList => { - framing::io::write_encrypted(stream, PID_FRIEND_LIST, seq, b"{}", crypto).await?; + framing::io::write_encrypted(stream, PID_FRIEND_LIST, seq, b"", crypto).await?; } NetCommand::BlockUser { user_id } => { - let payload = serde_json::to_vec(&BlockUserPayload { user_id })?; + let payload = wire::encode(&BlockUserPayload { user_id })?; framing::io::write_encrypted(stream, PID_BLOCK_USER, seq, &payload, crypto).await?; } NetCommand::UnblockUser { user_id } => { - let payload = serde_json::to_vec(&UnblockUserPayload { user_id })?; + let payload = wire::encode(&UnblockUserPayload { user_id })?; framing::io::write_encrypted(stream, PID_UNBLOCK_USER, seq, &payload, crypto).await?; } NetCommand::BlockList => { - framing::io::write_encrypted(stream, PID_BLOCK_LIST, seq, b"{}", crypto).await?; + framing::io::write_encrypted(stream, PID_BLOCK_LIST, seq, b"", crypto).await?; } _ => {} } diff --git a/src/net/session/handlers/social/guild.rs b/src/net/session/handlers/social/guild.rs index eb0b84e..a4d38b7 100644 --- a/src/net/session/handlers/social/guild.rs +++ b/src/net/session/handlers/social/guild.rs @@ -1,43 +1,48 @@ +use crate::net::wire; use anyhow::Result; -use serde_json; -use tokio::net::TcpStream; use crate::net::{crypto::SessionCrypto, framing, payloads::*, types::NetCommand}; pub async fn handle_cmd( cmd: NetCommand, - stream: &mut TcpStream, + stream: &mut (impl tokio::io::AsyncWrite + Unpin), seq: &mut u32, crypto: &SessionCrypto, ) -> Result<()> { match cmd { NetCommand::GuildCreate { name } => { - let payload = serde_json::to_vec(&GuildCreatePayload { - name, - icon_url: None, - })?; + let payload = wire::encode(&GuildCreatePayload { name })?; framing::io::write_encrypted(stream, PID_GUILD_CREATE, seq, &payload, crypto).await?; } NetCommand::GuildDelete { guild_id } => { - let payload = serde_json::to_vec(&GuildDeletePayload { guild_id })?; + let payload = wire::encode(&GuildDeletePayload { guild_id })?; framing::io::write_encrypted(stream, PID_GUILD_DELETE, seq, &payload, crypto).await?; } NetCommand::GuildList => { - framing::io::write_encrypted(stream, PID_GUILD_LIST, seq, b"{}", crypto).await?; + framing::io::write_encrypted(stream, PID_GUILD_LIST, seq, b"", crypto).await?; } NetCommand::GuildJoin { guild_id, invite_code, } => { - let payload = serde_json::to_vec(&GuildJoinPayload { - guild_id, - invite_code, - })?; - framing::io::write_encrypted(stream, PID_GUILD_MEMBER_JOIN, seq, &payload, crypto) - .await?; + if let Some(code) = invite_code { + let payload = wire::encode(&InviteAcceptPayload { + code, + ..Default::default() + })?; + framing::io::write_encrypted(stream, PID_INVITE_ACCEPT, seq, &payload, crypto) + .await?; + } else { + let payload = wire::encode(&GuildJoinPayload { guild_id })?; + framing::io::write_encrypted(stream, PID_GUILD_MEMBER_JOIN, seq, &payload, crypto) + .await?; + } } NetCommand::GuildLeave { guild_id } => { - let payload = serde_json::to_vec(&GuildLeavePayload { guild_id })?; + let payload = wire::encode(&GuildLeavePayload { + guild_id, + user_id: String::new(), + })?; framing::io::write_encrypted(stream, PID_GUILD_MEMBER_LEAVE, seq, &payload, crypto) .await?; } @@ -46,7 +51,7 @@ pub async fn handle_cmd( max_uses, expires_in_seconds, } => { - let payload = serde_json::to_vec(&InviteCreatePayload { + let payload = wire::encode(&InviteCreatePayload { guild_id, max_uses, expires_in_seconds, @@ -54,14 +59,17 @@ pub async fn handle_cmd( framing::io::write_encrypted(stream, PID_INVITE_CREATE, seq, &payload, crypto).await?; } NetCommand::InviteAccept { code } => { - let payload = serde_json::to_vec(&InviteAcceptPayload { code })?; + let payload = wire::encode(&InviteAcceptPayload { + code, + ..Default::default() + })?; framing::io::write_encrypted(stream, PID_INVITE_ACCEPT, seq, &payload, crypto).await?; } NetCommand::InviteDelete { guild_id, invite_id, } => { - let payload = serde_json::to_vec(&InviteDeletePayload { + let payload = wire::encode(&InviteDeletePayload { guild_id, invite_id, })?; @@ -73,30 +81,31 @@ pub async fn handle_cmd( color, permissions, } => { - let payload = serde_json::to_vec(&RoleCreatePayload { + let payload = wire::encode(&RoleCreatePayload { guild_id, name, color, permissions, + id: String::new(), })?; framing::io::write_encrypted(stream, PID_ROLE_CREATE, seq, &payload, crypto).await?; } NetCommand::RoleDelete { guild_id, role_id } => { - let payload = serde_json::to_vec(&RoleDeletePayload { guild_id, role_id })?; + let payload = wire::encode(&RoleDeletePayload { guild_id, role_id })?; framing::io::write_encrypted(stream, PID_ROLE_DELETE, seq, &payload, crypto).await?; } NetCommand::GuildMemberKick { guild_id, user_id } => { - let payload = serde_json::to_vec(&GuildMemberKickPayload { guild_id, user_id })?; + let payload = wire::encode(&GuildMemberKickPayload { guild_id, user_id })?; framing::io::write_encrypted(stream, PID_GUILD_MEMBER_KICK, seq, &payload, crypto) .await?; } NetCommand::GuildAuditLogFetch { guild_id, limit } => { - let payload = serde_json::to_vec(&GuildAuditLogFetchPayload { guild_id, limit })?; + let payload = wire::encode(&GuildAuditLogFetchPayload { guild_id, limit })?; framing::io::write_encrypted(stream, PID_GUILD_AUDIT_LOG_FETCH, seq, &payload, crypto) .await?; } NetCommand::GuildMemberListFetch { guild_id } => { - let payload = serde_json::to_vec(&GuildMemberListFetchPayload { guild_id })?; + let payload = wire::encode(&GuildMemberListFetchPayload { guild_id })?; framing::io::write_encrypted( stream, PID_GUILD_MEMBER_LIST_FETCH, @@ -111,7 +120,7 @@ pub async fn handle_cmd( user_id, role_id, } => { - let payload = serde_json::to_vec(&RoleAssignPayload { + let payload = wire::encode(&RoleAssignPayload { guild_id, user_id, role_id, @@ -124,7 +133,7 @@ pub async fn handle_cmd( user_id, role_id, } => { - let payload = serde_json::to_vec(&RoleAssignPayload { + let payload = wire::encode(&RoleAssignPayload { guild_id, user_id, role_id, @@ -133,7 +142,7 @@ pub async fn handle_cmd( .await?; } NetCommand::GuildRoleListFetch { guild_id } => { - let payload = serde_json::to_vec(&GuildRoleListFetchPayload { guild_id })?; + let payload = wire::encode(&GuildRoleListFetchPayload { guild_id })?; framing::io::write_encrypted(stream, PID_GUILD_ROLE_LIST_FETCH, seq, &payload, crypto) .await?; } diff --git a/src/net/session/handlers/social/mod.rs b/src/net/session/handlers/social/mod.rs index 7d5037f..25f8a1e 100644 --- a/src/net/session/handlers/social/mod.rs +++ b/src/net/session/handlers/social/mod.rs @@ -1,9 +1,8 @@ mod friends; mod guild; +use crate::net::wire; use anyhow::Result; -use serde_json; -use tokio::net::TcpStream; use crate::identity::Identity; use crate::net::{crypto::SessionCrypto, framing, payloads::*, types::NetCommand}; @@ -12,12 +11,32 @@ use super::core::now_ms; pub async fn handle_cmd( cmd: NetCommand, - stream: &mut TcpStream, + stream: &mut (impl tokio::io::AsyncWrite + Unpin), seq: &mut u32, crypto: &SessionCrypto, identity: &Identity, ) -> Result<()> { match cmd { + NetCommand::MediaControl(message) => { + framing::io::write_encrypted( + stream, + PID_MEDIA_CONTROL, + seq, + &wire::encode(&message)?, + crypto, + ) + .await?; + } + NetCommand::Webhook { operation, request } => { + framing::io::write_encrypted( + stream, + operation.packet_id(), + seq, + &wire::encode(&request)?, + crypto, + ) + .await?; + } cmd @ (NetCommand::GuildCreate { .. } | NetCommand::GuildDelete { .. } | NetCommand::GuildList @@ -52,7 +71,7 @@ pub async fn handle_cmd( message_id, emoji, } => { - let payload = serde_json::to_vec(&ReactionPayload { + let payload = wire::encode(&ReactionPayload { message_id, channel_id, emoji, @@ -66,7 +85,7 @@ pub async fn handle_cmd( message_id, emoji, } => { - let payload = serde_json::to_vec(&ReactionPayload { + let payload = wire::encode(&ReactionPayload { message_id, channel_id, emoji, @@ -86,7 +105,7 @@ pub async fn handle_cmd( message_id, content, } => { - let payload = serde_json::to_vec(&MessageEditPayload { + let payload = wire::encode(&MessageEditPayload { message_id, channel_id, content, @@ -97,14 +116,14 @@ pub async fn handle_cmd( channel_id, message_id, } => { - let payload = serde_json::to_vec(&MessageDeletePayload { + let payload = wire::encode(&MessageDeletePayload { message_id, channel_id, })?; framing::io::write_encrypted(stream, PID_MESSAGE_DELETE, seq, &payload, crypto).await?; } NetCommand::DmStart { target_user_id } => { - let payload = serde_json::to_vec(&DmStartPayload { target_user_id })?; + let payload = wire::encode(&DmStartPayload { target_user_id })?; framing::io::write_encrypted(stream, PID_DM_START, seq, &payload, crypto).await?; } NetCommand::DmSend { dm_id, content } => { @@ -114,25 +133,19 @@ pub async fn handle_cmd( content, timestamp: now_ms(), }; - framing::io::write_encrypted( - stream, - PID_DM_MESSAGE, - seq, - &serde_json::to_vec(&msg)?, - crypto, - ) - .await?; + framing::io::write_encrypted(stream, PID_DM_MESSAGE, seq, &wire::encode(&msg)?, crypto) + .await?; } NetCommand::TypingStart { channel_id } => { - let payload = serde_json::to_vec(&serde_json::json!({"channel_id": channel_id}))?; + let payload = wire::encode(&TypingStart { channel_id })?; framing::io::write_encrypted(stream, PID_TYPING_START, seq, &payload, crypto).await?; } NetCommand::DmReadAck { dm_id } => { - let payload = serde_json::to_vec(&serde_json::json!({"dm_id": dm_id}))?; + let payload = wire::encode(&crate::net::wire::DmReadAck { dm_id })?; framing::io::write_encrypted(stream, PID_DM_READ_ACK, seq, &payload, crypto).await?; } NetCommand::DmSearch { dm_id, query } => { - let payload = serde_json::to_vec(&DmHistoryPayload { + let payload = wire::encode(&DmHistoryPayload { dm_id, messages: vec![], search_query: if query.is_empty() { None } else { Some(query) }, @@ -144,7 +157,7 @@ pub async fn handle_cmd( dm_id, e2ee_public_key, } => { - let payload = serde_json::to_vec(&E2eeDmKeyExchangePayload { + let payload = wire::encode(&E2eeDmKeyExchangePayload { dm_id, e2ee_public_key, })?; @@ -152,7 +165,7 @@ pub async fn handle_cmd( .await?; } NetCommand::E2eeDmKeyExchangeAck { dm_id } => { - let payload = serde_json::to_vec(&E2eeDmKeyExchangeAckPayload { dm_id })?; + let payload = wire::encode(&E2eeDmKeyExchangeAckPayload { dm_id })?; framing::io::write_encrypted( stream, PID_E2EE_DM_KEY_EXCHANGE_ACK, @@ -173,13 +186,13 @@ pub async fn handle_cmd( stream, PID_E2EE_DM_MESSAGE, seq, - &serde_json::to_vec(&msg)?, + &wire::encode(&msg)?, crypto, ) .await?; } NetCommand::E2eeDmHistory { dm_id, limit } => { - let payload = serde_json::to_vec(&E2eeDmHistoryPayload { + let payload = wire::encode(&E2eeDmHistoryPayload { dm_id, messages: vec![], limit, diff --git a/src/net/session/handlers/voice.rs b/src/net/session/handlers/voice.rs index 444897a..3da58ff 100644 --- a/src/net/session/handlers/voice.rs +++ b/src/net/session/handlers/voice.rs @@ -1,47 +1,151 @@ -use std::sync::Arc; - +use crate::net::{ + types::NetEvent, + voice::{self, Credentials, ReplayWindow}, +}; use anyhow::Result; -use tokio::net::UdpSocket; -use tokio::sync::mpsc; +use std::{ + net::SocketAddr, + sync::{Arc, Mutex}, +}; +use tokio::{net::UdpSocket, sync::mpsc}; -use crate::net::{crypto::SessionCrypto, types::NetEvent}; - -pub struct State { - pub udp: Arc, - pub seq: u64, +struct ReceiveState { + credentials: Credentials, + endpoint: SocketAddr, + replay: ReplayWindow, +} +pub struct State { + udp: Arc, + credentials: Option, + endpoint: Option, + receive: Arc>>, + receiver: tokio::task::JoinHandle<()>, + sequence: u64, } - impl State { - pub async fn new(crypto: &SessionCrypto, event_tx: &mpsc::Sender) -> Result { - let udp = Arc::new(UdpSocket::bind("0.0.0.0:0").await?); - crate::net::voice::spawn_recv(udp.clone(), (*crypto).clone(), event_tx.clone()); - Ok(Self { udp, seq: 0 }) + pub async fn new(event_tx: &mpsc::Sender, ipv6: bool) -> Result { + let udp = Arc::new(UdpSocket::bind(if ipv6 { "[::]:0" } else { "0.0.0.0:0" }).await?); + let receive = Arc::new(Mutex::new(None::)); + let worker_udp = udp.clone(); + let worker_state = receive.clone(); + let events = event_tx.clone(); + let receiver = tokio::spawn(async move { + let mut bytes = vec![0u8; voice::MAX_DATAGRAM + 1]; + while let Ok((length, source)) = worker_udp.recv_from(&mut bytes).await { + let event = { + let Ok(mut state) = worker_state.lock() else { + break; + }; + let Some(state) = state.as_mut() else { + continue; + }; + if source != state.endpoint { + continue; + } + let Ok((sequence, frame)) = voice::decode(&state.credentials, &bytes[..length]) + else { + continue; + }; + if !state.replay.accept(sequence) || frame.opus.is_empty() { + continue; + } + NetEvent::VoicePacket { + channel_id: voice::channel_key(&frame.channel_id), + voice_seq: frame.voice_seq, + timestamp: frame.timestamp, + sender: source, + sender_id: frame.sender_user_id, + opus_data: frame.opus, + } + }; + if events.send(event).await.is_err() { + break; + } + } + }); + Ok(Self { + udp, + credentials: None, + endpoint: None, + receive, + receiver, + sequence: 0, + }) } - - #[allow(clippy::too_many_arguments)] - pub async fn send_voice( - &mut self, - channel_id: u64, - voice_pkt_seq: u32, - timestamp: u32, - opus_data: &[u8], - crypto: &SessionCrypto, - endpoint: Option, - sender_pubkey: &[u8; 32], - ) -> Result<()> { - if let Some(ep) = endpoint { - let pkt = crate::net::voice::build_packet( - channel_id, - voice_pkt_seq, - timestamp, - opus_data, - crypto, - self.seq, - sender_pubkey, - ); - let _ = self.udp.send_to(&pkt, ep).await; - self.seq += 1; + pub async fn configure(&mut self, session: crate::net::wire::VoiceSession) -> Result<()> { + let credentials = Credentials::try_from(session)?; + if self + .credentials + .as_ref() + .is_some_and(|old| old.route_id == credentials.route_id) + { + anyhow::bail!("voice credentials must be fresh on each join"); } + self.credentials = Some(credentials); + self.sequence = 0; + self.sync_receive(); + self.keep_alive().await + } + pub async fn set_endpoint(&mut self, endpoint: SocketAddr) -> Result<()> { + if self.endpoint == Some(endpoint) { + return Ok(()); + } + self.endpoint = Some(endpoint); + self.sync_receive(); + self.keep_alive().await + } + fn sync_receive(&self) { + if let Ok(mut state) = self.receive.lock() { + if let Some((credentials, endpoint)) = self.credentials.as_ref().zip(self.endpoint) { + if let Some(existing) = state + .as_mut() + .filter(|existing| existing.credentials.route_id == credentials.route_id) + { + existing.endpoint = endpoint; + } else { + *state = Some(ReceiveState { + credentials: credentials.clone(), + endpoint, + replay: ReplayWindow::default(), + }); + } + } else { + *state = None; + } + } + } + pub fn channel_id(&self) -> Option<&str> { + self.credentials + .as_ref() + .map(|value| value.channel_id.as_str()) + } + pub async fn keep_alive(&mut self) -> Result<()> { + self.send(None, 0, 0, b"").await + } + pub async fn send( + &mut self, + channel: Option, + voice_seq: u32, + timestamp: u32, + opus: &[u8], + ) -> Result<()> { + let Some((credentials, endpoint)) = self.credentials.as_ref().zip(self.endpoint) else { + return Ok(()); + }; + if channel.is_some_and(|key| key != voice::channel_key(&credentials.channel_id)) { + return Ok(()); + } + let packet = voice::encode(credentials, self.sequence, voice_seq, timestamp, opus)?; + self.sequence = self + .sequence + .checked_add(1) + .ok_or_else(|| anyhow::anyhow!("voice sequence exhausted"))?; + self.udp.send_to(&packet, endpoint).await?; Ok(()) } } +impl Drop for State { + fn drop(&mut self) { + self.receiver.abort(); + } +} diff --git a/src/net/session/task.rs b/src/net/session/task.rs index 9b05a4c..ecb70b4 100644 --- a/src/net/session/task.rs +++ b/src/net/session/task.rs @@ -19,7 +19,7 @@ pub async fn net_task( let mut target: Option = None; let mut failures: u32 = 0; - loop { + 'session: loop { let Some(mut session) = target.take() else { match wait_for_connect(&mut cmd_rx).await { Some(t) => { @@ -39,7 +39,26 @@ pub async fn net_task( delay_secs: delay.as_secs(), }) .await; - sleep(delay).await; + let delay = sleep(delay); + tokio::pin!(delay); + loop { + tokio::select! { + _ = &mut delay => break, + command = cmd_rx.recv() => match command { + Some(NetCommand::Connect {address,auto_reconnect}) => { + session.address=address;session.auto_reconnect=auto_reconnect; + failures=0;target=Some(session);continue 'session; + } + Some(NetCommand::Disconnect) => { + failures=0; + let _=event_tx.send(NetEvent::Disconnected {reason:"disconnected".into(),will_retry:false}).await; + continue 'session; + } + None => return, + _ => {} + } + } + } } info!( @@ -109,8 +128,30 @@ async fn wait_for_connect(cmd_rx: &mut mpsc::Receiver) -> Option return None, + None => return None, + Some(NetCommand::Disconnect) => continue, _ => {} } } } + +#[cfg(test)] +mod tests { + use super::*; + #[tokio::test] + async fn disconnected_cancel_does_not_disable_later_connections() { + let (sender, mut receiver) = mpsc::channel(4); + sender.send(NetCommand::Disconnect).await.unwrap(); + sender + .send(NetCommand::Connect { + address: "127.0.0.1:7600".into(), + auto_reconnect: false, + }) + .await + .unwrap(); + assert_eq!( + wait_for_connect(&mut receiver).await.unwrap().address, + "127.0.0.1:7600" + ); + } +} diff --git a/src/net/transport.rs b/src/net/transport.rs new file mode 100644 index 0000000..6e4f246 --- /dev/null +++ b/src/net/transport.rs @@ -0,0 +1,112 @@ +//! TCP/TLS transport selection. TLS never falls back to plaintext after an error. +use anyhow::{Context, Result, bail}; +use tokio::io::{AsyncRead, AsyncWrite}; +use tokio::net::TcpStream; + +pub trait AsyncStream: AsyncRead + AsyncWrite + Unpin + Send {} +impl AsyncStream for T {} +pub type Stream = Box; + +pub struct Connected { + pub stream: Stream, + pub peer: std::net::SocketAddr, +} +pub async fn connect(address: &str) -> Result { + Ok(connect_with_peer(address).await?.stream) +} +pub async fn connect_with_peer(address: &str) -> Result { + let explicit_plain = address.starts_with("lnex://"); + let authority = address + .strip_prefix("lnexs://") + .or_else(|| address.strip_prefix("lnex://")) + .unwrap_or(address); + if authority.contains('/') + || authority.contains('@') + || authority.contains('?') + || authority.contains('#') + { + bail!("expected a server host and port"); + } + let (host, port) = if let Some(value) = authority.strip_prefix('[') { + let (host, tail) = value.split_once(']').context("invalid IPv6 address")?; + let port = if tail.is_empty() { + 7600 + } else { + tail.strip_prefix(':') + .context("invalid server address")? + .parse()? + }; + (host, port) + } else if let Some((host, port)) = authority.rsplit_once(':') { + (host, port.parse()?) + } else { + (authority, 7600) + }; + if host.is_empty() { + bail!("server host is empty"); + } + let tcp = TcpStream::connect((host, port)) + .await + .context("connect to gateway")?; + tcp.set_nodelay(true)?; + let peer = tcp.peer_addr()?; + let loopback_host = host.eq_ignore_ascii_case("localhost") + || host + .parse::() + .is_ok_and(|ip| ip.is_loopback()); + let plain = explicit_plain || (!address.starts_with("lnexs://") && loopback_host); + if plain { + if !tcp.peer_addr()?.ip().is_loopback() { + bail!("plaintext LNEx is restricted to loopback development; use lnexs://"); + } + return Ok(Connected { + stream: Box::new(tcp), + peer, + }); + } + let roots = rustls::RootCertStore::from_iter(webpki_roots::TLS_SERVER_ROOTS.iter().cloned()); + let config = rustls::ClientConfig::builder_with_provider( + rustls::crypto::aws_lc_rs::default_provider().into(), + ) + .with_protocol_versions(&[&rustls::version::TLS13])? + .with_root_certificates(roots) + .with_no_client_auth(); + let server_name = rustls::pki_types::ServerName::try_from(host.to_owned()) + .context("invalid TLS server name")?; + let tls = tokio_rustls::TlsConnector::from(std::sync::Arc::new(config)) + .connect(server_name, tcp) + .await + .context("TLS certificate/connection verification failed")?; + Ok(Connected { + stream: Box::new(tls), + peer, + }) +} + +#[cfg(test)] +mod tests { + use super::*; + use tokio::io::AsyncReadExt; + #[tokio::test] + async fn tls_failure_never_retries_plaintext() { + let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); + let address = listener.local_addr().unwrap(); + let task = tokio::spawn(async move { + let (mut stream, _) = listener.accept().await.unwrap(); + let first = stream.read_u8().await.unwrap(); + assert_eq!( + first, 22, + "first record must be a TLS handshake, never LNEx plaintext" + ); + drop(stream); + assert!( + tokio::time::timeout(std::time::Duration::from_millis(30), listener.accept()) + .await + .is_err(), + "certificate/handshake errors must not cause a downgrade reconnect" + ); + }); + assert!(connect(&format!("lnexs://{address}")).await.is_err()); + task.await.unwrap(); + } +} diff --git a/src/net/types/command.rs b/src/net/types/command.rs index a23b504..91c89c2 100644 --- a/src/net/types/command.rs +++ b/src/net/types/command.rs @@ -1,5 +1,10 @@ #[derive(Debug)] pub enum NetCommand { + MediaControl(crate::net::wire::MediaControl), + Webhook { + operation: WebhookOperation, + request: crate::net::wire::WebhookRequest, + }, Connect { address: String, auto_reconnect: bool, @@ -176,3 +181,22 @@ pub enum NetCommand { }, Disconnect, } + +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum WebhookOperation { + Create, + List, + Rotate, + Revoke, +} +impl WebhookOperation { + pub fn packet_id(self) -> u16 { + use crate::net::payloads::*; + match self { + Self::Create => PID_WEBHOOK_CREATE, + Self::List => PID_WEBHOOK_LIST, + Self::Rotate => PID_WEBHOOK_ROTATE, + Self::Revoke => PID_WEBHOOK_REVOKE, + } + } +} diff --git a/src/net/types/event.rs b/src/net/types/event.rs index cd8d359..806035d 100644 --- a/src/net/types/event.rs +++ b/src/net/types/event.rs @@ -3,6 +3,13 @@ use std::net::SocketAddr; #[derive(Debug, Clone)] pub enum NetEvent { + VoiceRevoked { + channel_id: String, + }, + MediaEvent(crate::net::wire::MediaEvent), + WebhookResult(WebhookManagementResult), + WebhookList(crate::net::wire::WebhookListResult), + WebhookError(crate::net::wire::WebhookErrorResult), Connected { session_id: String, node_name: String, @@ -244,3 +251,19 @@ pub enum NetEvent { messages: Vec, }, } + +/// One-time management secret. Debug deliberately omits it. +#[derive(Clone)] +pub struct WebhookManagementResult { + pub request_id: String, + pub hook: Option, + pub secret: String, +} +impl std::fmt::Debug for WebhookManagementResult { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("WebhookManagementResult") + .field("hook", &self.hook) + .field("secret", &"[REDACTED]") + .finish() + } +} diff --git a/src/net/types/mod.rs b/src/net/types/mod.rs index 6bbdc1e..da0dade 100644 --- a/src/net/types/mod.rs +++ b/src/net/types/mod.rs @@ -3,6 +3,6 @@ mod command; mod event; mod info; -pub use command::NetCommand; -pub use event::NetEvent; +pub use command::{NetCommand, WebhookOperation}; +pub use event::{NetEvent, WebhookManagementResult}; pub use info::{ChannelListItem, ChatMsg, FriendInfo, GuildInfo, MemberInfo, PresenceInfo}; diff --git a/src/net/voice/mod.rs b/src/net/voice/mod.rs index 949eda0..7023094 100644 --- a/src/net/voice/mod.rs +++ b/src/net/voice/mod.rs @@ -1,84 +1,221 @@ -use super::crypto::SessionCrypto; -use super::payloads::PID_VOICE_PACKET; +//! LNEx v2 authenticated UDP voice. Credentials arrive only over encrypted control. +use crate::net::wire::VoiceFrame; +use anyhow::{Result, bail}; +use chacha20poly1305::{ + ChaCha20Poly1305, KeyInit, + aead::{Aead, Payload}, +}; +use prost::Message; -mod receive; -pub use receive::spawn_recv; +pub const HEADER_SIZE: usize = 28; +pub const MAX_DATAGRAM: usize = 1472; -/// Stable string channel id to u64 key for UDP voice packets. +/// Stable UI channel key; the wire carries the original string channel identifier. pub fn channel_key(id: &str) -> u64 { - let mut h: u64 = 5381; - for b in id.bytes() { - h = h.wrapping_mul(33).wrapping_add(u64::from(b)); - } - h + id.bytes().fold(5381u64, |value, byte| { + value.wrapping_mul(33).wrapping_add(u64::from(byte)) + }) } -/// Build a UDP voice packet with encryption. -/// -/// Plaintext layout (Phase 1.2+): -/// `[voice_seq:4][timestamp:4][channel_id:8][sender_id:32][opus_data:N]` -/// -/// `sender_id` is the raw Ed25519 public key of the sender, so receivers can -/// attribute the voice activity to a specific user (Phase 1.3 speaking indicators). -pub fn build_packet( - channel_id: u64, +#[derive(Clone)] +pub struct Credentials { + pub route_id: [u8; 16], + pub c2s_key: [u8; 32], + pub s2c_key: [u8; 32], + pub channel_id: String, +} +impl TryFrom for Credentials { + type Error = anyhow::Error; + fn try_from(value: crate::net::wire::VoiceSession) -> Result { + if value.channel_id.is_empty() { + bail!("empty voice channel"); + } + Ok(Self { + route_id: value + .route_id + .try_into() + .map_err(|_| anyhow::anyhow!("invalid voice route"))?, + c2s_key: value + .c2s_key + .try_into() + .map_err(|_| anyhow::anyhow!("invalid voice key"))?, + s2c_key: value + .s2c_key + .try_into() + .map_err(|_| anyhow::anyhow!("invalid voice key"))?, + channel_id: value.channel_id, + }) + } +} +fn nonce(sequence: u64) -> [u8; 12] { + let mut nonce = [0; 12]; + nonce[4..].copy_from_slice(&sequence.to_be_bytes()); + nonce +} +pub fn encode( + credentials: &Credentials, + sequence: u64, voice_seq: u32, timestamp: u32, opus: &[u8], - crypto: &SessionCrypto, - packet_seq: u64, - sender_pubkey: &[u8; 32], -) -> Vec { - let mut plaintext = Vec::with_capacity(16 + 32 + opus.len()); - plaintext.extend_from_slice(&voice_seq.to_be_bytes()); - plaintext.extend_from_slice(×tamp.to_be_bytes()); - plaintext.extend_from_slice(&channel_id.to_be_bytes()); - plaintext.extend_from_slice(sender_pubkey); - plaintext.extend_from_slice(opus); - - let encrypted = crypto.encrypt_c2s(packet_seq, &plaintext); - - let mut buf = Vec::with_capacity(4 + encrypted.len()); - buf.extend_from_slice(&PID_VOICE_PACKET.to_be_bytes()); - buf.extend_from_slice(&0u16.to_be_bytes()); - buf.extend_from_slice(&encrypted); - buf +) -> Result> { + if sequence == u64::MAX { + bail!("voice sequence exhausted; rejoin required"); + } + let frame = VoiceFrame { + voice_seq, + timestamp, + channel_id: credentials.channel_id.clone(), + sender_session_id: String::new(), + sender_user_id: String::new(), + opus: opus.to_vec(), + }; + let plain = frame.encode_to_vec(); + if HEADER_SIZE + plain.len() + 16 > MAX_DATAGRAM { + bail!("voice datagram too large"); + } + let mut header = [0u8; HEADER_SIZE]; + header[..2].copy_from_slice(&0x0010u16.to_be_bytes()); + header[2..4].copy_from_slice(&1u16.to_be_bytes()); + header[4..20].copy_from_slice(&credentials.route_id); + header[20..].copy_from_slice(&sequence.to_be_bytes()); + let cipher = ChaCha20Poly1305::new((&credentials.c2s_key).into()); + let nonce = nonce(sequence); + let encrypted = cipher + .encrypt( + (&nonce).into(), + Payload { + msg: &plain, + aad: &header, + }, + ) + .map_err(|_| anyhow::anyhow!("voice encryption failed"))?; + Ok([header.as_slice(), encrypted.as_slice()].concat()) +} +pub fn decode(credentials: &Credentials, data: &[u8]) -> Result<(u64, VoiceFrame)> { + if data.len() < HEADER_SIZE + 16 || data.len() > MAX_DATAGRAM { + bail!("invalid voice datagram size"); + } + if data[..2] != 0x0010u16.to_be_bytes() + || data[2..4] != 1u16.to_be_bytes() + || data[4..20] != credentials.route_id + { + bail!("invalid voice datagram header"); + } + let sequence = u64::from_be_bytes(data[20..28].try_into()?); + let nonce = nonce(sequence); + let cipher = ChaCha20Poly1305::new((&credentials.s2c_key).into()); + let plain = cipher + .decrypt( + (&nonce).into(), + Payload { + msg: &data[HEADER_SIZE..], + aad: &data[..HEADER_SIZE], + }, + ) + .map_err(|_| anyhow::anyhow!("voice authentication failed"))?; + let frame = VoiceFrame::decode(plain.as_slice())?; + if frame.channel_id != credentials.channel_id + || (!frame.opus.is_empty() + && (frame.sender_session_id.is_empty() || frame.sender_user_id.is_empty())) + { + bail!("invalid authenticated voice attribution"); + } + Ok((sequence, frame)) +} +#[derive(Default)] +pub struct ReplayWindow { + highest: Option, + bitmap: u128, +} +impl ReplayWindow { + /// Call only after authentication and channel validation. + pub fn accept(&mut self, sequence: u64) -> bool { + let Some(highest) = self.highest else { + self.highest = Some(sequence); + self.bitmap = 1; + return true; + }; + if sequence > highest { + let distance = sequence - highest; + self.bitmap = if distance >= 128 { + 1 + } else { + (self.bitmap << distance) | 1 + }; + self.highest = Some(sequence); + true + } else { + let distance = highest - sequence; + if distance >= 128 || self.bitmap & (1u128 << distance) != 0 { + return false; + } + self.bitmap |= 1u128 << distance; + true + } + } } - #[cfg(test)] mod tests { use super::*; - - #[test] - fn channel_key_is_stable() { - assert_eq!(channel_key("voice"), channel_key("voice")); - assert_ne!(channel_key("voice"), channel_key("general")); + fn credentials() -> Credentials { + Credentials { + route_id: [1; 16], + c2s_key: [2; 32], + s2c_key: [3; 32], + channel_id: "voice".into(), + } } - #[test] - fn build_packet_header_layout() { - let key = channel_key("voice"); - let shared = [0u8; 32]; - let crypto = SessionCrypto::derive(&shared, "test"); - let sender = [0xABu8; 32]; - let pkt = build_packet(key, 42, 1000, b"opus", &crypto, 0, &sender); - - let packet_id = u16::from_be_bytes([pkt[0], pkt[1]]); - assert_eq!(packet_id, PID_VOICE_PACKET); - - let encrypted_payload = &pkt[4..]; - let plaintext = crypto.decrypt_c2s(0, encrypted_payload).unwrap(); - assert_eq!(plaintext.len(), 16 + 32 + 4); - - let pkt_voice_seq = u32::from_be_bytes(plaintext[0..4].try_into().unwrap()); - let timestamp = u32::from_be_bytes(plaintext[4..8].try_into().unwrap()); - let channel_id = u64::from_be_bytes(plaintext[8..16].try_into().unwrap()); - let sender_id: [u8; 32] = plaintext[16..48].try_into().unwrap(); - - assert_eq!(pkt_voice_seq, 42); - assert_eq!(timestamp, 1000); - assert_eq!(channel_id, key); - assert_eq!(sender_id, sender); - assert_eq!(&plaintext[48..], b"opus"); + fn replay_window_allows_reordering_but_never_duplicates_or_wrap() { + let mut window = ReplayWindow::default(); + for value in [3, 1, 2, 5, 4] { + assert!(window.accept(value)); + } + for value in [1, 2, 3, 4, 5] { + assert!(!window.accept(value)); + } + assert!(window.accept(200)); + assert!(!window.accept(0)); + assert!(window.accept(u64::MAX)); + assert!(!window.accept(0)); + } + #[test] + fn c2s_datagram_has_explicit_authenticated_sequence_and_no_claimed_sender() { + let credentials = credentials(); + let data = encode(&credentials, 42, 7, 48000, b"opus").unwrap(); + let nonce = nonce(42); + let cipher = ChaCha20Poly1305::new((&credentials.c2s_key).into()); + let plain = cipher + .decrypt( + (&nonce).into(), + Payload { + msg: &data[28..], + aad: &data[..28], + }, + ) + .unwrap(); + let frame = VoiceFrame::decode(plain.as_slice()).unwrap(); + assert!(frame.sender_user_id.is_empty()); + assert!(frame.sender_session_id.is_empty()); + assert_eq!(frame.opus, b"opus"); + let mut tampered = data.clone(); + tampered[27] ^= 1; + assert!( + cipher + .decrypt( + (&nonce).into(), + Payload { + msg: &tampered[28..], + aad: &tampered[..28] + } + ) + .is_err() + ); + } + #[test] + fn nonce_exhaustion_and_oversized_opus_fail_closed() { + assert!(encode(&credentials(), u64::MAX, 0, 0, b"").is_err()); + assert!(encode(&credentials(), 0, 0, 0, &vec![0; 2000]).is_err()); } } diff --git a/src/net/voice/receive.rs b/src/net/voice/receive.rs deleted file mode 100644 index 92c1a8a..0000000 --- a/src/net/voice/receive.rs +++ /dev/null @@ -1,80 +0,0 @@ -use std::sync::Arc; -use tokio::net::UdpSocket; -use tokio::sync::mpsc; -use tracing::warn; - -use super::super::crypto::SessionCrypto; -use super::super::payloads::PID_VOICE_PACKET; -use super::super::types::NetEvent; - -/// Spawn a task that reads UDP datagrams, decrypts them, and forwards as VoicePacket events. -pub fn spawn_recv(udp: Arc, crypto: SessionCrypto, tx: mpsc::Sender) { - tokio::spawn(async move { - let mut buf = vec![0u8; 1472]; - let mut voice_seq: u64 = 0; - - loop { - match udp.recv_from(&mut buf).await { - Ok((len, src)) => { - if len < 4 { - continue; - } - - let packet_id = u16::from_be_bytes([buf[0], buf[1]]); - if packet_id != PID_VOICE_PACKET { - continue; - } - - let encrypted_payload = &buf[4..len]; - let plaintext = match crypto.decrypt_s2c(voice_seq, encrypted_payload) { - Ok(p) => p, - Err(e) => { - warn!("voice packet decryption failed from {src}: {e}"); - voice_seq += 1; - continue; - } - }; - - // Layout: [voice_seq:4][timestamp:4][channel_id:8][sender_id:32][opus] - // Legacy format (no sender_id) is 16+ bytes — gracefully handle. - if plaintext.len() < 16 { - warn!("voice packet payload too short from {src}"); - voice_seq += 1; - continue; - } - - let pkt_voice_seq = u32::from_be_bytes(plaintext[0..4].try_into().unwrap()); - let timestamp = u32::from_be_bytes(plaintext[4..8].try_into().unwrap()); - let channel_id = u64::from_be_bytes(plaintext[8..16].try_into().unwrap()); - - // sender_id: 32 raw bytes if present, hex-encoded. - // Empty string when packet is from legacy sender without sender_id. - let (sender_id_hex, opus_data) = if plaintext.len() >= 48 { - let sender_bytes: [u8; 32] = plaintext[16..48].try_into().unwrap(); - (hex::encode(sender_bytes), plaintext[48..].to_vec()) - } else { - // Legacy: no sender_id in payload. - (String::new(), plaintext[16..].to_vec()) - }; - - let _ = tx - .send(NetEvent::VoicePacket { - channel_id, - voice_seq: pkt_voice_seq, - timestamp, - sender: src, - sender_id: sender_id_hex, - opus_data, - }) - .await; - - voice_seq += 1; - } - Err(e) => { - warn!("UDP recv: {e}"); - break; - } - } - } - }); -} diff --git a/src/net/wire/mod.rs b/src/net/wire/mod.rs new file mode 100644 index 0000000..e6167ca --- /dev/null +++ b/src/net/wire/mod.rs @@ -0,0 +1,9 @@ +//! Generated LNEx protobuf messages, shared verbatim with the gateway schema. +include!(concat!(env!("OUT_DIR"), "/lnex.rs")); + +pub fn encode(message: &impl prost::Message) -> anyhow::Result> { + Ok(message.encode_to_vec()) +} +pub fn decode(bytes: &[u8]) -> anyhow::Result { + Ok(T::decode(bytes)?) +} diff --git a/src/ui/media.rs b/src/ui/media.rs index ca5ad5b..ef2649d 100644 --- a/src/ui/media.rs +++ b/src/ui/media.rs @@ -3,10 +3,15 @@ use crate::ui::slint::{MainWindow, ScreenShareWindow}; use slint::ComponentHandle; /// Register the screen preview window; unsupported builds explain their capability. -pub fn wire(main: &MainWindow) -> Result<(), slint::PlatformError> { +pub fn wire( + main: &MainWindow, + state: &std::rc::Rc>, +) -> Result<(), slint::PlatformError> { + #[cfg(not(all(target_os = "linux", feature = "screen-capture")))] + let _ = state; let dialog = ScreenShareWindow::new()?; #[cfg(all(target_os = "linux", feature = "screen-capture"))] - let controller = native::Controller::new(dialog.clone_strong()); + let controller = native::Controller::new(dialog.clone_strong(), state)?; let main_weak = main.as_weak(); main.on_open_screen_share(move || { if let Some(main) = main_weak.upgrade() { @@ -26,6 +31,11 @@ pub fn wire(main: &MainWindow) -> Result<(), slint::PlatformError> { fn media_message(error: &str) -> crate::ui::i18n::Message { use crate::ui::i18n::Message; match error { + "Broadcast authorization timed out" => { + Message::with("Broadcast authorization timed out", vec![]) + } + "Broadcast connection timed out" => Message::with("Broadcast connection timed out", vec![]), + "WebRTC connection timed out" => Message::with("WebRTC connection timed out", vec![]), "Audio source inventory exceeds the supported size." => { Message::with("Audio source inventory exceeds the supported size.", vec![]) } @@ -128,22 +138,50 @@ fn media_message(error: &str) -> crate::ui::i18n::Message { #[cfg(all(target_os = "linux", feature = "screen-capture"))] mod native { use super::*; + use crate::ui::{slint::ScreenViewerWindow, state::ConnState}; use std::{ cell::{Cell, RefCell}, - rc::Rc, + rc::{Rc, Weak}, sync::mpsc, time::{Duration, Instant}, }; - use vnox_client::media::{ - self, AudioChoice, AudioKind, AudioSource, CaptureOptions, CaptureSession, + use vnox_client::{ + media::{ + self, AudioChoice, AudioKind, AudioSource, CaptureOptions, CaptureSession, + broadcast::{AvailableShare, BroadcastManager}, + peer::{AudioTrack, Tracks}, + }, + net::{NetCommand, wire::MediaEvent}, }; - + thread_local! { static ACTIVE: RefCell> = const { RefCell::new(Weak::new()) }; } + pub fn route(event: MediaEvent) { + ACTIVE.with(|active| { + if let Some(controller) = active.borrow().upgrade() + && let Err(error) = controller.manager.borrow_mut().on_event(event) + { + controller.set_error(&error); + } + }); + } + pub fn reject(error: &str) { + ACTIVE.with(|active| { + if let Some(controller) = active.borrow().upgrade() { + controller.manager.borrow_mut().stop(); + controller.set_error(error); + } + }); + } enum Event { Sources(Result, String>), Capture(u64, Result), } pub struct Controller { window: ScreenShareWindow, + viewer: ScreenViewerWindow, + state: Weak>, + manager: RefCell, + shares: RefCell>, + viewing: RefCell>, sources: RefCell>, selected: RefCell>, session: RefCell>, @@ -156,11 +194,26 @@ mod native { error: RefCell>, } impl Controller { - pub fn new(window: ScreenShareWindow) -> Rc { + pub fn new( + window: ScreenShareWindow, + state: &Rc>, + ) -> Result, slint::PlatformError> { window.set_capable(true); let (tx, rx) = mpsc::channel(); + let mut manager = BroadcastManager::default(); + let ice_error = manager + .configure_ice(media::peer::IceServers { + stun: std::env::var("VNOX_STUN_SERVER").ok(), + turn: std::env::var("VNOX_TURN_SERVER").ok(), + }) + .err(); let this = Rc::new(Self { window, + viewer: ScreenViewerWindow::new()?, + state: Rc::downgrade(state), + manager: RefCell::new(manager), + shares: RefCell::new(Vec::new()), + viewing: RefCell::new(None), sources: RefCell::new(Vec::new()), selected: RefCell::new(Vec::new()), session: RefCell::new(None), @@ -170,12 +223,13 @@ mod native { timer: slint::Timer::default(), started: RefCell::new(None), generation: Cell::new(0), - error: RefCell::new(None), + error: RefCell::new(ice_error.as_deref().map(super::media_message)), }); + ACTIVE.with(|active| *active.borrow_mut() = Rc::downgrade(&this)); let weak = Rc::downgrade(&this); this.timer.start( slint::TimerMode::Repeated, - Duration::from_millis(50), + Duration::from_millis(33), move || { if let Some(this) = weak.upgrade() { this.tick(); @@ -207,13 +261,44 @@ mod native { } }); let weak = Rc::downgrade(&this); + this.window.on_toggle_broadcast(move || { + if let Some(this) = weak.upgrade() { + this.toggle_broadcast(); + } + }); + let weak = Rc::downgrade(&this); + this.window.on_watch_broadcast(move || { + if let Some(this) = weak.upgrade() { + this.watch(); + } + }); + let weak = Rc::downgrade(&this); + this.viewer.on_stop_viewing(move || { + if let Some(this) = weak.upgrade() { + this.stop_viewing(); + } + }); + let weak = Rc::downgrade(&this); + this.viewer.on_audio_changed(move || { + if let Some(this) = weak.upgrade() { + this.clear_mixer(); + } + }); + let weak = Rc::downgrade(&this); + this.viewer.window().on_close_requested(move || { + if let Some(this) = weak.upgrade() { + this.stop_viewing(); + } + slint::CloseRequestResponse::HideWindow + }); + let weak = Rc::downgrade(&this); this.window.window().on_close_requested(move || { if let Some(this) = weak.upgrade() { this.stop(); } slint::CloseRequestResponse::HideWindow }); - this + Ok(this) } pub fn refresh(&self) { let tx = self.tx.clone(); @@ -221,6 +306,13 @@ mod native { let _ = tx.send(Event::Sources(media::audio_sources().await)); }); } + fn set_error(&self, error: &str) { + *self.error.borrow_mut() = Some(super::media_message(error)); + } + fn clear_error(&self) { + self.error.borrow_mut().take(); + self.window.set_error("".into()); + } fn filter_sources(&self) { let kind = match self.window.get_audio_mode() { 1 => Some(AudioKind::Application), @@ -234,27 +326,24 @@ mod native { .filter(|source| Some(source.kind) == kind) .cloned() .collect(); - let labels: Vec = - selected.iter().map(|s| s.name.as_str().into()).collect(); + let labels: Vec = selected + .iter() + .map(|source| source.name.as_str().into()) + .collect(); self.window .set_audio_sources(Rc::new(slint::VecModel::from(labels)).into()); self.window.set_source_index(0); *self.selected.borrow_mut() = selected; } - fn report_error(&self, error: &str) { - let message = media_message(error); - self.window.set_error(message.to_string().into()); - *self.error.borrow_mut() = Some(message); - } fn start(&self) { self.stop(); - self.window.set_error("".into()); + self.clear_error(); let audio = match self.window.get_audio_mode() { 1 | 2 => { let index = usize::try_from(self.window.get_source_index()).ok(); let selected = self.selected.borrow(); let Some(source) = index.and_then(|index| selected.get(index)) else { - self.report_error("Select an available audio source first."); + self.set_error("Select an available audio source first."); return; }; match source.kind { @@ -268,39 +357,112 @@ mod native { let tx = self.tx.clone(); let generation = self.generation.get(); *self.pending.borrow_mut() = Some(tokio::spawn(async move { - let result = CaptureSession::choose(CaptureOptions { - audio, - ..Default::default() - }) - .await; - let _ = tx.send(Event::Capture(generation, result)); + let _ = tx.send(Event::Capture( + generation, + CaptureSession::choose(CaptureOptions { + audio, + ..Default::default() + }) + .await, + )); })); } + fn toggle_broadcast(&self) { + let mut manager = self.manager.borrow_mut(); + if manager.publication_pending() { + manager.stop_publishing(); + self.window.set_publishing(false); + self.window.set_publication_starting(false); + return; + } + let session = self.session.borrow(); + let Some(session) = session.as_ref() else { + return; + }; + let tracks = Tracks { + application_audio: self.window.get_audio_mode() > 0, + microphone: false, + }; + if let Err(error) = manager.publish(&session.feed, tracks) { + self.set_error(&error); + } + } + fn watch(&self) { + self.stop_viewing(); + let index = usize::try_from(self.window.get_broadcast_index()).ok(); + let share = index.and_then(|index| self.shares.borrow().get(index).cloned()); + if let Some(share) = share { + if let Err(error) = self.manager.borrow_mut().watch(&share.id) { + self.set_error(&error); + return; + } + super::sync_viewer_palette(&self.window, &self.viewer); + self.viewer + .set_publisher(self.publisher_name(&share.publisher_user).into()); + self.viewer.set_has_frame(false); + self.viewer.set_frame(slint::Image::default()); + self.viewer.set_audio_enabled(true); + *self.viewing.borrow_mut() = Some(share.id); + let _ = self.viewer.show(); + } + } + fn publisher_name(&self, user: &str) -> String { + self.state + .upgrade() + .and_then(|state| { + state + .try_borrow() + .ok() + .map(|state| state.ui.nick_for(user).to_owned()) + }) + .unwrap_or_else(|| user.into()) + } + fn clear_mixer(&self) { + if let Some(state) = self.state.upgrade() + && let Ok(state) = state.try_borrow() + && let Some(audio) = &state.audio + { + audio.media_mixer().clear(); + } + } + fn stop_viewing(&self) { + if let Some(id) = self.viewing.borrow_mut().take() { + self.manager.borrow_mut().unwatch(&id); + } + self.clear_mixer(); + self.viewer.set_frame(slint::Image::default()); + self.viewer.set_has_frame(false); + let _ = self.viewer.hide(); + } fn stop(&self) { + self.manager.borrow_mut().stop_publishing(); self.generation.set(self.generation.get().wrapping_add(1)); if let Some(task) = self.pending.borrow_mut().take() { task.abort(); } self.session.borrow_mut().take(); - // Discard late completion before another selection can start. while self.rx.borrow_mut().try_recv().is_ok() {} *self.started.borrow_mut() = None; - self.error.borrow_mut().take(); - self.window.set_error("".into()); self.window.set_running(false); self.window.set_busy(false); + self.window.set_publishing(false); + self.window.set_publication_starting(false); self.window.set_has_frame(false); self.window.set_preview(slint::Image::default()); self.window.set_audio_level(0.0); } fn tick(&self) { - while let Ok(event) = self.rx.borrow_mut().try_recv() { + loop { + let event = self.rx.borrow_mut().try_recv(); + let Ok(event) = event else { + break; + }; match event { Event::Sources(Ok(sources)) => { *self.sources.borrow_mut() = sources; self.filter_sources(); } - Event::Sources(Err(error)) => self.report_error(&error), + Event::Sources(Err(error)) => self.set_error(&error), Event::Capture(generation, result) => { if generation != self.generation.get() { continue; @@ -313,25 +475,20 @@ mod native { *self.started.borrow_mut() = Some(Instant::now()); self.window.set_running(true); } - Err(error) => self.report_error(&error), + Err(error) => self.set_error(&error), } } } } - let mut error = None; + let mut failure = None; if let Some(session) = self.session.borrow().as_ref() { - if let Err(message) = session.check() { - error = Some(message); + if let Err(error) = session.check() { + failure = Some(error); } if let Some(frame) = session.feed.take_frame() { - let buffer = slint::SharedPixelBuffer::::clone_from_slice( - &frame.rgba, - frame.width, - frame.height, - ); - self.window.set_preview(slint::Image::from_rgba8(buffer)); + self.window.set_preview(image(&frame)); self.window.set_has_frame(true); - *self.started.borrow_mut() = None; + self.started.borrow_mut().take(); } self.window.set_audio_level(session.feed.audio_level()); if self @@ -339,25 +496,124 @@ mod native { .borrow() .is_some_and(|time| time.elapsed() > Duration::from_secs(10)) { - error = Some( + failure = Some( "No frames received. Capture stopped; select the source again.".into(), ); } } - if let Some(error) = error { + if let Some(error) = failure { self.stop(); - self.report_error(&error); + self.set_error(&error); } - if let Some(message) = self.error.borrow().as_ref() { - self.window.set_error(message.to_string().into()); + if let Some(state) = self.state.upgrade() + && let Ok(state) = state.try_borrow() + { + let session = match &state.ui.conn { + ConnState::Connected { session_id, .. } => session_id.as_str(), + _ => "", + }; + let mut manager = self.manager.borrow_mut(); + let channel = if session.is_empty() { + None + } else { + state.voice_channel.as_deref() + }; + manager.set_scope(session, channel); + self.window.set_can_broadcast(channel.is_some()); + // Existing voice owns microphone authorization. No duplicate mic track + // is requested by this UI; the tap remains disabled by default. + if let Some(audio) = &state.audio { + audio.enable_media_microphone(false); + } + manager.tick(); + self.window.set_publishing(manager.publishing()); + self.window.set_publication_starting( + manager.publication_pending() && !manager.publishing(), + ); + self.window.set_viewers(manager.viewer_count() as i32); + let shares: Vec<_> = manager + .shares() + .into_iter() + .filter(|share| share.publisher_session != session) + .collect(); + let labels: Vec = shares + .iter() + .map(|share| state.ui.nick_for(&share.publisher_user).into()) + .collect(); + self.window + .set_broadcasts(Rc::new(slint::VecModel::from(labels)).into()); + *self.shares.borrow_mut() = shares; + let mut commands = Vec::new(); + while let Some(control) = manager.take_control() { + commands.push(NetCommand::MediaControl(control)); + } + let net = state.net.clone(); + if !commands.is_empty() { + tokio::spawn(async move { + for command in commands { + net.send(command).await; + } + }); + } + while let Some(error) = manager.take_error() { + self.set_error(&error); + } + let viewing = self.viewing.borrow().clone(); + if let Some(id) = viewing { + if manager.is_watching(&id) { + if let Some(feed) = manager.remote_feed(&id) { + if let Some(frame) = feed.take_video() { + self.viewer.set_frame(image(&frame)); + self.viewer.set_has_frame(true); + } + for (track, suffix) in [ + (AudioTrack::Application, "app"), + (AudioTrack::Microphone, "mic"), + ] { + while let Some(frame) = feed.take_audio(track) { + if self.viewer.get_audio_enabled() + && !state.ui.deafen + && let Some(audio) = &state.audio + && let Err(error) = audio + .media_mixer() + .push(&format!("{id}:{suffix}"), frame) + { + self.set_error(&error); + } + } + } + } + } else { + self.viewing.borrow_mut().take(); + self.viewer.set_frame(slint::Image::default()); + self.viewer.set_has_frame(false); + let _ = self.viewer.hide(); + if let Some(audio) = &state.audio { + audio.media_mixer().clear(); + } + } + } + } + if let Some(error) = self.error.borrow().as_ref() { + self.window.set_error(error.to_string().into()); } } } impl Drop for Controller { fn drop(&mut self) { self.stop(); + self.stop_viewing(); } } + fn image(frame: &media::VideoFrame) -> slint::Image { + slint::Image::from_rgba8( + slint::SharedPixelBuffer::::clone_from_slice( + &frame.rgba, + frame.width, + frame.height, + ), + ) + } } fn sync_palette(main: &MainWindow, dialog: &ScreenShareWindow) { @@ -383,6 +639,30 @@ fn sync_palette(main: &MainWindow, dialog: &ScreenShareWindow) { target.set_on_accent(source.get_on_accent()); } +#[cfg(all(target_os = "linux", feature = "screen-capture"))] +fn sync_viewer_palette(main: &ScreenShareWindow, dialog: &crate::ui::slint::ScreenViewerWindow) { + use crate::ui::slint::Theme; + let source = main.global::(); + let target = dialog.global::(); + target.set_bg_base(source.get_bg_base()); + target.set_bg_surface(source.get_bg_surface()); + target.set_bg_rail(source.get_bg_rail()); + target.set_bg_hover(source.get_bg_hover()); + target.set_bg_interactive(source.get_bg_interactive()); + target.set_bg_elevated(source.get_bg_elevated()); + target.set_selected(source.get_selected()); + target.set_border(source.get_border()); + target.set_accent(source.get_accent()); + target.set_text_primary(source.get_text_primary()); + target.set_text_secondary(source.get_text_secondary()); + target.set_text_muted(source.get_text_muted()); + target.set_text_disabled(source.get_text_disabled()); + target.set_danger(source.get_danger()); + target.set_success(source.get_success()); + target.set_font_size(source.get_font_size()); + target.set_on_accent(source.get_on_accent()); +} + #[cfg(test)] mod message_tests { use super::*; @@ -420,3 +700,86 @@ mod message_tests { ); } } + +/// Forward authenticated media events from the existing network/UI event pump. +pub fn on_event(event: vnox_client::net::wire::MediaEvent) { + #[cfg(all(target_os = "linux", feature = "screen-capture"))] + native::route(event); + #[cfg(not(all(target_os = "linux", feature = "screen-capture")))] + let _ = event; +} + +#[cfg(test)] +mod window_tests { + use super::*; + use slint::platform::{ + Platform, PlatformError, WindowAdapter, + software_renderer::{MinimalSoftwareWindow, RepaintBufferType}, + }; + use slint::{PhysicalSize, Rgb8Pixel, SharedPixelBuffer}; + use std::{cell::RefCell, rc::Rc}; + struct PlatformAdapter(Rc>>>); + impl Platform for PlatformAdapter { + fn create_window_adapter(&self) -> Result, PlatformError> { + let surface = MinimalSoftwareWindow::new(RepaintBufferType::NewBuffer); + self.0.borrow_mut().push(surface.clone()); + Ok(surface) + } + } + fn render( + window: &impl ComponentHandle, + surface: &MinimalSoftwareWindow, + name: &str, + width: u32, + height: u32, + ) { + surface.set_size(PhysicalSize::new(width, height)); + window.window().request_redraw(); + let mut pixels = SharedPixelBuffer::::new(width, height); + assert!(surface.draw_if_needed(|renderer| { + renderer.render(pixels.make_mut_slice(), width as usize); + })); + assert!( + pixels.as_bytes().windows(3).any(|pair| pair[0] != pair[1]), + "rendered content" + ); + if let Some(path) = std::env::var_os("VNOX_UI_SNAPSHOT_DIR") { + std::fs::create_dir_all(&path).expect("snapshot dir"); + image::save_buffer( + std::path::PathBuf::from(path).join(format!("{name}.png")), + pixels.as_bytes(), + width, + height, + image::ColorType::Rgb8, + ) + .expect("rendered PNG"); + } + } + #[test] + fn broadcast_windows_render_without_opening_capture_or_audio_devices() { + let surfaces = Rc::new(RefCell::new(Vec::new())); + slint::platform::set_platform(Box::new(PlatformAdapter(surfaces.clone()))) + .expect("software platform"); + let preview = ScreenShareWindow::new().expect("preview window"); + preview.set_capable(true); + preview.set_can_broadcast(true); + preview.show().expect("show preview"); + assert!(!preview.get_running() && !preview.get_publishing()); + render(&preview, &surfaces.borrow()[0], "screen-preview", 820, 810); + preview.hide().expect("hide preview"); + let viewer = crate::ui::slint::ScreenViewerWindow::new().expect("viewer window"); + viewer.set_publisher("Synthetic test publisher".into()); + viewer.show().expect("show viewer"); + assert!(!viewer.get_has_frame()); + render(&viewer, &surfaces.borrow()[1], "screen-viewer", 1100, 720); + viewer.hide().expect("hide viewer"); + } +} + +/// Fail pending/active media explicitly when the authenticated gateway rejects control. +pub fn on_error(error: &str) { + #[cfg(all(target_os = "linux", feature = "screen-capture"))] + native::reject(error); + #[cfg(not(all(target_os = "linux", feature = "screen-capture")))] + let _ = error; +} diff --git a/src/ui/mod.rs b/src/ui/mod.rs index fdfca63..40761a6 100644 --- a/src/ui/mod.rs +++ b/src/ui/mod.rs @@ -7,3 +7,5 @@ pub mod preferences; pub mod slint; pub mod state; pub mod update; + +pub mod webhooks; diff --git a/src/ui/slint/chat.slint b/src/ui/slint/chat.slint index d79c08d..d45455f 100644 --- a/src/ui/slint/chat.slint +++ b/src/ui/slint/chat.slint @@ -81,6 +81,7 @@ export component ChatPanel inherits Rectangle { in-out property members-visible: true; callback input-edited(string); callback toggle-members(); + callback open-webhooks(); callback send-message(string, string); callback load-more(string); callback react(string, string); @@ -99,6 +100,7 @@ export component ChatPanel inherits Rectangle { Text { text: root.is-dm ? root.dm-partner-name : root.active-channel-name; color: Theme.text-primary; font-size: Math.max(11px, Theme.font-size * 1.0); font-weight: 600; overflow: elide; vertical-alignment: center; } if root.e2ee-enabled : Text { text: @tr("End-to-end encrypted"); color: Theme.success; font-size: Math.max(11px, Theme.font-size * 0.6875); } Rectangle { horizontal-stretch: 1; } + if !root.is-dm : ActionButton { text: @tr("Webhooks"); quiet: true; clicked => { root.open-webhooks(); } } if !root.is-dm : ActionButton { text: @tr("Members"); active: root.members-visible; clicked => { root.toggle-members(); } } } } diff --git a/src/ui/slint/main.slint b/src/ui/slint/main.slint index 9eb12bc..f6a4ac4 100644 --- a/src/ui/slint/main.slint +++ b/src/ui/slint/main.slint @@ -1,4 +1,5 @@ -export { ScreenShareWindow } from "screen-share.slint"; +export { WebhookWindow, WebhookRow } from "webhooks.slint"; +export { ScreenShareWindow, ScreenViewerWindow } from "screen-share.slint"; export { UnlockWindow } from "unlock.slint"; export { Theme } from "theme.slint"; import { Theme, ActionButton } from "theme.slint"; @@ -128,6 +129,7 @@ export component MainWindow inherits Window { callback friend-input-edited(string); callback open-settings(); callback open-screen-share(); + callback open-webhooks(); callback show-voice-view(); callback show-friends-view(); callback audio-settings-changed(int, int, float, int, float); @@ -231,6 +233,7 @@ export component MainWindow inherits Window { members: root.members; input-text <=> root.input-text; members-visible: root.members-visible && root.width > 1100px; + open-webhooks => { root.open-webhooks(); } toggle-members => { root.members-visible = !root.members-visible; } input-edited(text) => { root.input-edited(text); } is-dm: root.is-dm; diff --git a/src/ui/slint/screen-share.slint b/src/ui/slint/screen-share.slint index 2509fc4..2941623 100644 --- a/src/ui/slint/screen-share.slint +++ b/src/ui/slint/screen-share.slint @@ -3,7 +3,7 @@ import { Theme, ActionButton } from "theme.slint"; export component ScreenShareWindow inherits Window { title: @tr("Screen preview"); - preferred-width: 820px; preferred-height: 740px; min-width: 640px; min-height: 650px; + preferred-width: 820px; preferred-height: 810px; min-width: 640px; min-height: 720px; background: Theme.bg-base; in-out property preview; in-out property has-frame; @@ -15,21 +15,33 @@ export component ScreenShareWindow inherits Window { in-out property source-index; in-out property audio-mode; in-out property audio-level; + in-out property can-broadcast; + in-out property publishing; + in-out property publication-starting; + in-out property viewers; + in-out property <[string]> broadcasts; + in-out property broadcast-index; + callback toggle-broadcast(); + callback watch-broadcast(); callback refresh-audio(); callback choose-source(); callback stop-capture(); callback audio-mode-changed(); VerticalLayout { - padding: 28px; spacing: 14px; + spacing: 0; + Flickable { + content-width: self.width; + VerticalLayout { + padding: 24px; spacing: 12px; Text { text: @tr("Choose what you share"); color: Theme.text-primary; font-size: 24px; font-weight: 600; } Text { text: @tr("Video, application audio and your microphone stay separate."); color: Theme.text-secondary; wrap: word-wrap; } Rectangle { - min-height: 270px; vertical-stretch: 1; background: Theme.bg-rail; border-radius: 10px; + min-height: 230px; vertical-stretch: 1; background: Theme.bg-rail; border-radius: 10px; Image { visible: root.has-frame; source: root.preview; image-fit: contain; } if !root.has-frame : Text { text: root.busy ? @tr("Choose a screen or window in the system dialog…") : @tr("Your preview appears here after you choose a source."); color: Theme.text-muted; wrap: word-wrap; horizontal-alignment: center; vertical-alignment: center; } if root.running : Rectangle { x: 12px; y: 12px; width: 132px; height: 28px; border-radius: 5px; background: Theme.bg-surface; - Text { text: @tr("LOCAL PREVIEW"); color: Theme.accent; font-size: 11px; horizontal-alignment: center; vertical-alignment: center; } + Text { text: root.publishing ? @tr("LIVE") : @tr("LOCAL PREVIEW"); color: Theme.accent; font-size: 11px; horizontal-alignment: center; vertical-alignment: center; } } } Text { text: @tr("Share audio"); color: Theme.text-primary; font-weight: 600; } @@ -47,12 +59,49 @@ export component ScreenShareWindow inherits Window { Text { text: @tr("Microphone uses your existing voice controls. This preview never enables or mixes it."); color: Theme.text-secondary; wrap: word-wrap; } Rectangle { height: note.preferred-height + 24px; background: Theme.bg-surface; border-radius: 8px; - note := Text { x: 12px; y: 12px; width: parent.width - 24px; text: root.capable ? @tr("Local capture is available. Broadcasting to other members is not available until the media transport is connected.") : @tr("Screen capture is not available in this build."); color: Theme.text-muted; wrap: word-wrap; } + note := Text { x: 12px; y: 12px; width: parent.width - 24px; text: root.capable ? @tr("Preview stays local until you start broadcasting. Only members of your voice channel can watch.") : @tr("Screen capture is not available in this build."); color: Theme.text-muted; wrap: word-wrap; } + } + if root.broadcasts.length > 0 : HorizontalLayout { + DeviceSelect { model: root.broadcasts; current-index <=> root.broadcast-index; horizontal-stretch: 1; } + ActionButton { text: @tr("Watch broadcast"); clicked => { root.watch-broadcast(); } } } if root.error != "" : Text { text: root.error; color: Theme.danger; wrap: word-wrap; } + } + } + Rectangle { + height: 72px; background: Theme.bg-surface; HorizontalLayout { - Text { text: root.running && root.has-frame ? @tr("Preview active · not broadcasting") : @tr("Nothing is being broadcast"); color: Theme.text-muted; vertical-alignment: center; horizontal-stretch: 1; } + padding: 20px; spacing: 12px; + Text { text: root.publication-starting ? @tr("Starting broadcast…") : root.publishing ? @tr("Broadcasting · {n} viewer" | "Broadcasting · {n} viewers" % root.viewers) : root.running && root.has-frame ? @tr("Preview active · not broadcasting") : @tr("Nothing is being broadcast"); color: Theme.text-muted; vertical-alignment: center; horizontal-stretch: 1; } + if root.running && root.has-frame && root.can-broadcast : ActionButton { text: root.publication-starting ? @tr("Cancel broadcast") : root.publishing ? @tr("Stop broadcast") : @tr("Start broadcast"); active: root.publishing; clicked => { root.toggle-broadcast(); } } if root.capable : ActionButton { text: root.running || root.busy ? @tr("Stop") : @tr("Choose screen or window"); clicked => { if root.running || root.busy { root.stop-capture(); } else { root.choose-source(); } } } } + } + } +} + + +export component ScreenViewerWindow inherits Window { + title: @tr("Broadcast"); + preferred-width: 1100px; preferred-height: 720px; min-width: 640px; min-height: 420px; + background: Theme.bg-rail; + in-out property frame; + in-out property has-frame; + in-out property audio-enabled: true; + in-out property publisher; + callback stop-viewing(); + callback audio-changed(); + VerticalLayout { + padding: 16px; spacing: 12px; + HorizontalLayout { + Text { text: root.publisher; color: Theme.text-primary; font-size: 16px; vertical-alignment: center; horizontal-stretch: 1; } + ActionButton { text: root.audio-enabled ? @tr("Mute broadcast") : @tr("Unmute broadcast"); active: !root.audio-enabled; clicked => { root.audio-enabled = !root.audio-enabled; root.audio-changed(); } } + ActionButton { text: @tr("Stop watching"); clicked => { root.stop-viewing(); } } + } + Rectangle { + background: #000000; vertical-stretch: 1; border-radius: 8px; + Image { source: root.frame; visible: root.has-frame; image-fit: contain; } + if !root.has-frame : Text { text: @tr("Connecting to broadcast…"); color: Theme.text-muted; horizontal-alignment: center; vertical-alignment: center; } + } } } diff --git a/src/ui/slint/settings/appearance.slint b/src/ui/slint/settings/appearance.slint index 9d2522c..d897277 100644 --- a/src/ui/slint/settings/appearance.slint +++ b/src/ui/slint/settings/appearance.slint @@ -7,8 +7,9 @@ component ThemeChoice inherits Rectangle { in property preview-background; in property preview-sidebar; in property preview-ink; + in property preview-accent: #bba583; callback clicked(); - height: 142px; + height: 160px; border-radius: 9px; border-width: keyboard.has-focus || root.selected ? 2px : 1px; border-color: root.selected || keyboard.has-focus ? Theme.accent : Theme.border; @@ -17,13 +18,52 @@ component ThemeChoice inherits Rectangle { accessible-label: root.label; VerticalLayout { padding: 8px; spacing: 10px; - Rectangle { - height: 86px; background: root.preview-background; border-radius: 5px; clip: true; - Rectangle { width: 23%; height: 100%; background: root.preview-sidebar; } - Rectangle { x: parent.width * 0.29; y: 15px; width: 41%; height: 6px; background: root.preview-ink; opacity: 0.8; border-radius: 2px; } - Rectangle { x: parent.width * 0.29; y: 29px; width: 59%; height: 4px; background: root.preview-ink; opacity: 0.28; border-radius: 2px; } - Rectangle { x: parent.width * 0.29; y: 40px; width: 45%; height: 4px; background: root.preview-ink; opacity: 0.28; border-radius: 2px; } - Rectangle { x: parent.width * 0.29; y: 61px; width: 60%; height: 13px; background: root.preview-sidebar; border-radius: 3px; } + mini := Rectangle { + height: 108px; + background: root.preview-background; + border-radius: 5px; + border-width: 1px; + border-color: root.preview-ink.with-alpha(0.1); + clip: true; + // A miniature client: server rail, channel list, conversation and composer. + rail := Rectangle { + x: 0px; y: 0px; + width: 17px; height: parent.height; background: root.preview-sidebar.darker(8%); + Rectangle { x: 4px; y: 8px; width: 9px; height: 9px; border-radius: 3px; background: root.preview-accent; + Image { source: @image-url("../assets/vnox-mark.svg"); width: 9px; height: 9px; image-fit: contain; } + } + Rectangle { x: 4px; y: 24px; width: 9px; height: 9px; border-radius: 4px; background: root.preview-ink.with-alpha(0.14); } + Rectangle { x: 4px; y: 38px; width: 9px; height: 9px; border-radius: 4px; background: root.preview-ink.with-alpha(0.08); } + Rectangle { x: 4px; y: parent.height - 13px; width: 8px; height: 8px; border-radius: 4px; background: root.preview-ink.with-alpha(0.3); } + } + side := Rectangle { + x: 17px; y: 0px; width: parent.width * 0.27; height: parent.height; background: root.preview-sidebar; + Text { x: 5px; y: 7px; text: "Vnox"; color: root.preview-ink; font-size: 7px; font-weight: 700; } + Rectangle { x: 3px; y: 24px; width: parent.width - 6px; height: 13px; border-radius: 2px; background: root.preview-accent.with-alpha(0.18); + Text { x: 4px; text: "#"; color: root.preview-accent; font-size: 9px; vertical-alignment: center; } + Rectangle { x: 14px; y: 5px; width: Math.max(parent.width - 21px, 3px); height: 3px; border-radius: 1px; background: root.preview-ink.with-alpha(0.65); } + } + Text { x: 7px; y: 42px; text: "#"; color: root.preview-ink.with-alpha(0.4); font-size: 8px; } + Text { x: 7px; y: 59px; text: "♪"; color: root.preview-ink.with-alpha(0.4); font-size: 8px; } + Rectangle { x: 7px; y: 80px; width: 4px; height: 4px; border-radius: 2px; background: root.preview-accent; } + Rectangle { x: 15px; y: 81px; width: Math.max(parent.width - 24px, 3px); height: 2px; background: root.preview-ink.with-alpha(0.3); } + } + thread := Rectangle { + x: side.x + side.width; y: 0px; width: parent.width - self.x; height: parent.height; background: transparent; + Text { x: 7px; y: 5px; text: "#"; color: root.preview-ink.with-alpha(0.65); font-size: 10px; } + Rectangle { x: 21px; y: 10px; width: parent.width * 0.29; height: 3px; border-radius: 1px; background: root.preview-ink.with-alpha(0.65); } + Rectangle { y: 23px; width: parent.width; height: 1px; background: root.preview-ink.with-alpha(0.08); } + Rectangle { x: 7px; y: 33px; width: 10px; height: 10px; border-radius: 4px; background: root.preview-accent; } + Rectangle { x: 23px; y: 34px; width: parent.width * 0.35; height: 3px; border-radius: 1px; background: root.preview-ink.with-alpha(0.75); } + Rectangle { x: 23px; y: 42px; width: parent.width - 32px; height: 3px; border-radius: 1px; background: root.preview-ink.with-alpha(0.25); } + Rectangle { x: 7px; y: 56px; width: 10px; height: 10px; border-radius: 4px; background: root.preview-ink.with-alpha(0.22); } + Rectangle { x: 23px; y: 57px; width: parent.width * 0.25; height: 3px; border-radius: 1px; background: root.preview-ink.with-alpha(0.65); } + Rectangle { x: 23px; y: 65px; width: parent.width * 0.4; height: 3px; border-radius: 1px; background: root.preview-ink.with-alpha(0.25); } + Rectangle { x: 6px; y: parent.height - 23px; width: parent.width - 12px; height: 16px; border-radius: 4px; background: root.preview-sidebar; + Text { x: 4px; text: "+"; color: root.preview-ink.with-alpha(0.45); font-size: 10px; vertical-alignment: center; } + Rectangle { x: parent.width - 12px; y: 5px; width: 6px; height: 6px; border-radius: 2px; background: root.preview-accent; } + } + } } HorizontalLayout { Text { text: root.label; color: Theme.text-primary; font-size: Math.max(Theme.font-size - 3px, 11px); font-weight: 500; overflow: elide; horizontal-stretch: 1; } @@ -72,7 +112,7 @@ export component AppearanceSettings inherits Rectangle { ThemeChoice { label: @tr("Light"); selected: root.theme-mode == "light"; preview-background: #f5f4f2; preview-sidebar: #e6e4e0; preview-ink: #353330; clicked => { root.theme-selected("light"); } } ThemeChoice { label: @tr("Dark"); selected: root.theme-mode == "dark"; preview-background: #272729; preview-sidebar: #202022; preview-ink: #e9e7e4; clicked => { root.theme-selected("dark"); } } ThemeChoice { label: @tr("Extra dark"); selected: root.theme-mode == "black"; preview-background: #080808; preview-sidebar: #131313; preview-ink: #dedbd5; clicked => { root.theme-selected("black"); } } - ThemeChoice { label: @tr("Custom"); selected: root.theme-mode == "custom"; preview-background: Theme.bg-base; preview-sidebar: Theme.bg-surface; preview-ink: Theme.accent; clicked => { root.theme-selected("custom"); } } + ThemeChoice { label: @tr("Custom"); selected: root.theme-mode == "custom"; preview-background: Theme.bg-base; preview-sidebar: Theme.bg-surface; preview-ink: Theme.text-primary; preview-accent: Theme.accent; clicked => { root.theme-selected("custom"); } } } Rectangle { height: 29px; } Rectangle { height: 1px; background: Theme.border; } diff --git a/src/ui/slint/webhooks.slint b/src/ui/slint/webhooks.slint new file mode 100644 index 0000000..a45fd18 --- /dev/null +++ b/src/ui/slint/webhooks.slint @@ -0,0 +1,126 @@ +import { Theme, ActionButton } from "theme.slint"; +import { ThemeInput, DeviceSelect } from "controls.slint"; + +export struct WebhookRow { id: string, name: string, detail: string, paused: bool } + +export component WebhookWindow inherits Window { + title: @tr("Channel webhooks"); + preferred-width: 820px; preferred-height: 700px; min-width: 680px; min-height: 620px; + background: Theme.bg-base; + in property channel-name; + in property <[WebhookRow]> hooks; + in property allowed; + in property busy; + in property error; + in property secret; + in property secret-name; + in property secret-path; + in property secret-outgoing; + in property confirmation-name; + in property confirming; + in property confirming-revoke; + in-out property new-name; + in-out property new-endpoint; + in-out property new-kind; + callback refresh(); + callback create(); + callback rotate(string); + callback revoke(string); + callback confirm(); + callback cancel-confirmation(); + callback hide-secret(); + callback dismiss(); + VerticalLayout { + padding: 28px; spacing: 12px; + HorizontalLayout { + VerticalLayout { + spacing: 4px; + Text { text: @tr("Channel webhooks"); color: Theme.text-primary; font-size: Theme.font-size + 8px; font-weight: 600; } + Text { text: root.channel-name; color: Theme.text-muted; font-size: Theme.font-size; overflow: elide; } + } + if !root.busy && root.secret == "" && !root.confirming : ActionButton { text: @tr("Refresh"); clicked => { root.refresh(); } } + } + Text { text: @tr("Receive updates here, or send signed channel events to your own service."); color: Theme.text-secondary; wrap: word-wrap; } + Rectangle { height: 1px; background: Theme.border; } + if root.error != "" : Text { text: root.error; color: Theme.danger; wrap: word-wrap; } + if root.busy : Text { text: @tr("Waiting for the server…"); color: Theme.text-muted; } + if root.secret != "" : Rectangle { + height: secret-content.preferred-height + 28px; + background: Theme.bg-surface; border-radius: Theme.radius; + border-width: 1px; border-color: Theme.accent; + secret-content := VerticalLayout { + padding: 14px; spacing: 10px; + Text { text: @tr("Save this secret now"); color: Theme.text-primary; font-weight: 600; } + Text { text: root.secret-name; color: Theme.text-secondary; overflow: elide; } + Text { text: @tr("It is shown once. Closing this window or hiding it removes it from the client."); color: Theme.text-muted; wrap: word-wrap; } + credential := TextInput { + text: root.secret; read-only: true; wrap: word-wrap; height: 44px; + color: Theme.text-primary; font-family: "monospace"; font-size: Theme.font-size - 2px; + selection-background-color: Theme.selected; + accessible-label: @tr("Webhook secret"); + } + if !root.secret-outgoing : Text { text: root.secret-path; color: Theme.text-secondary; wrap: word-wrap; } + Text { text: root.secret-outgoing ? @tr("Use this key to verify X-Vnox-Signature over the timestamp and raw request body.") : @tr("Use your server's HTTPS webhook endpoint. Generic integrations send this secret in Authorization: Bearer; GitHub uses it as the webhook secret."); color: Theme.text-muted; wrap: word-wrap; } + HorizontalLayout { + ActionButton { text: @tr("Copy and hide"); active: true; clicked => { credential.select-all(); credential.copy(); root.hide-secret(); } } + ActionButton { text: @tr("Hide secret"); quiet: true; clicked => { root.hide-secret(); } } + Rectangle { horizontal-stretch: 1; } + } + } + } + if root.confirming : Rectangle { + height: confirm-content.preferred-height + 28px; background: Theme.bg-surface; border-radius: Theme.radius; + confirm-content := VerticalLayout { + padding: 14px; spacing: 10px; + Text { text: root.confirming-revoke ? @tr("Revoke this webhook?") : @tr("Rotate this secret?"); color: Theme.text-primary; font-weight: 600; } + Text { text: root.confirmation-name; color: Theme.text-secondary; overflow: elide; } + Text { text: root.confirming-revoke ? @tr("New deliveries stop. Existing messages stay in the channel.") : @tr("The old secret stops working. Update the external integration with the new secret."); color: Theme.text-muted; wrap: word-wrap; } + HorizontalLayout { + ActionButton { text: root.confirming-revoke ? @tr("Revoke webhook") : @tr("Rotate secret"); danger: root.confirming-revoke; clicked => { root.confirm(); } } + ActionButton { text: @tr("Cancel"); quiet: true; clicked => { root.cancel-confirmation(); } } + Rectangle { horizontal-stretch: 1; } + } + } + } + list := Flickable { + min-height: 80px; vertical-stretch: 1; content-width: self.width; + VerticalLayout { + spacing: 0; alignment: start; + if root.allowed && !root.busy && root.hooks.length == 0 : Text { text: @tr("No webhooks yet. Add an integration below."); color: Theme.text-muted; wrap: word-wrap; } + for hook in root.hooks : Rectangle { + height: 76px; background: transparent; + HorizontalLayout { + padding-top: 10px; padding-bottom: 10px; spacing: 10px; + VerticalLayout { + spacing: 5px; horizontal-stretch: 1; + Text { text: hook.name; color: Theme.text-primary; font-weight: 600; overflow: elide; } + Text { text: hook.detail; color: hook.paused ? Theme.danger : Theme.text-muted; font-size: Theme.font-size - 3px; overflow: elide; } + } + if root.allowed && !root.busy && root.secret == "" && !root.confirming : ActionButton { text: @tr("Rotate"); clicked => { root.rotate(hook.id); } } + if root.allowed && !root.busy && root.secret == "" && !root.confirming : ActionButton { text: @tr("Revoke"); quiet: true; danger: true; clicked => { root.revoke(hook.id); } } + } + Rectangle { y: parent.height - 1px; height: 1px; background: Theme.border; } + } + } + } + if root.allowed && !root.busy && root.secret == "" && !root.confirming : VerticalLayout { + spacing: 10px; + Text { text: @tr("Add an integration"); color: Theme.text-primary; font-weight: 600; } + HorizontalLayout { + spacing: 10px; + ThemeInput { text <=> root.new-name; placeholder-text: @tr("Integration name"); horizontal-stretch: 1; } + DeviceSelect { width: 210px; model: [@tr("Incoming message"), @tr("GitHub updates"), @tr("Outgoing events")]; current-index <=> root.new-kind; } + } + if root.new-kind == 2 : ThemeInput { text <=> root.new-endpoint; placeholder-text: "https://example.org/vnox-events"; } + HorizontalLayout { + Text { text: root.new-kind == 2 ? @tr("Only public HTTPS destinations. Events: message.created.") : @tr("An integration can post only in this channel, using its own identity."); color: Theme.text-muted; wrap: word-wrap; horizontal-stretch: 1; } + ActionButton { text: @tr("Create webhook"); active: true; clicked => { root.create(); } } + } + } + if !root.allowed && !root.busy : Text { text: @tr("Only the server owner or members with Manage Webhooks can change integrations."); color: Theme.text-muted; wrap: word-wrap; } + HorizontalLayout { + Rectangle { horizontal-stretch: 1; } + ActionButton { text: @tr("Close"); quiet: true; clicked => { root.dismiss(); } } + } + } +} diff --git a/src/ui/update.rs b/src/ui/update.rs index 034dbb5..b1ec3dc 100644 --- a/src/ui/update.rs +++ b/src/ui/update.rs @@ -10,12 +10,42 @@ use crate::AppState; pub fn process_net_events(state: &mut AppState) { while let Some(event) = state.net.try_recv() { match event { + NetEvent::VoiceRevoked { channel_id } => { + if state.voice_channel.as_deref() == Some(channel_id.as_str()) + || state.ui.voice_target.as_deref() == Some(channel_id.as_str()) + { + state.ui.voice = VoiceUiState::Off; + state.ui.voice_target = None; + state.voice_channel = None; + state.audio = None; + state.last_pipeline_config = None; + } + } + + NetEvent::MediaEvent(event) => crate::ui::media::on_event(event), + NetEvent::WebhookResult(result) => { + if let Some(ui) = &state.webhooks { + ui.receive_result(&result.request_id, result.hook, result.secret); + } + } + NetEvent::WebhookList(result) => { + if let Some(ui) = &state.webhooks { + ui.receive_list(&result.request_id, result.hooks); + } + } + NetEvent::WebhookError(result) => { + if let Some(ui) = &state.webhooks { + ui.receive_error(&result.request_id, result.code); + } + } + NetEvent::Connected { session_id, node_name, restored, private_mode, } => { + state.connection_generation = state.connection_generation.wrapping_add(1); state.ui.conn = ConnState::Connected { session_id, node_name: node_name.clone(), @@ -46,6 +76,9 @@ pub fn process_net_events(state: &mut AppState) { } } NetEvent::Disconnected { reason, will_retry } => { + if let Some(ui) = &state.webhooks { + ui.disconnected(); + } state.ui.voice = VoiceUiState::Off; state.audio = None; state.voice_channel = None; @@ -308,6 +341,11 @@ fn process_net_event(ui: &mut UiState, net: &NetHandle, event: NetEvent) { | NetEvent::PresenceSync { .. } | NetEvent::PresenceUpdated { .. } => status_handle(ui, net, event), + NetEvent::VoiceRevoked { .. } + | NetEvent::MediaEvent(_) + | NetEvent::WebhookResult(_) + | NetEvent::WebhookList(_) + | NetEvent::WebhookError(_) => unreachable!("handled before generic UI dispatch"), NetEvent::Connected { .. } | NetEvent::Disconnected { .. } | NetEvent::Reconnecting { .. } @@ -532,6 +570,9 @@ fn chat_handle(ui: &mut UiState, net: &NetHandle, event: NetEvent) { ui.scroll_bottom = true; } NetEvent::Error { message, .. } => { + if message.starts_with("media:") { + crate::ui::media::on_error(&message); + } ui.status_msg = Some(crate::ui::i18n::Message::with( "Server error: {message}", vec![("message", message.to_string())], diff --git a/src/ui/webhooks.rs b/src/ui/webhooks.rs new file mode 100644 index 0000000..b4f4902 --- /dev/null +++ b/src/ui/webhooks.rs @@ -0,0 +1,287 @@ +//! Native, authenticated channel integration management. Credentials are never persisted. +use crate::ui::{ + i18n, + slint::{MainWindow, Theme, WebhookRow, WebhookWindow}, +}; +use slint::{ComponentHandle, ModelRc, VecModel}; +use std::{ + cell::RefCell, + rc::Rc, + time::{Duration, Instant}, +}; +use vnox_client::{ + net::{NetCommand, wire::WebhookInfo}, + webhooks::{Controller, Error, Form, Kind, Scope}, +}; + +type Send = Rc; +/// Keep this bridge alive for the lifetime of the application. The caller feeds correlated network responses. +pub struct WebhookUi { + window: WebhookWindow, + controller: Rc>, + send: Send, + _timer: slint::Timer, +} +impl WebhookUi { + pub fn new(send: Send) -> Result { + let window = WebhookWindow::new()?; + let controller = Rc::new(RefCell::new(Controller::default())); + for action in [0, 1, 2] { + let weak = window.as_weak(); + let model = controller.clone(); + let send = send.clone(); + let callback = move || { + if let Some(window) = weak.upgrade() { + let result = match action { + 0 => model.borrow_mut().refresh(), + 1 => { + let kind = Kind::from_index(window.get_new_kind()); + if let Some(kind) = kind { + model.borrow_mut().create(Form { + name: window.get_new_name().to_string(), + kind, + endpoint: window.get_new_endpoint().to_string(), + }) + } else { + model.borrow_mut().show_error(Error::RequestFailed); + Err(Error::RequestFailed) + } + } + _ => model.borrow_mut().confirm(), + }; + if let Ok(command) = result { + send(command) + } + render(&window, &model.borrow()); + } + }; + match action { + 0 => window.on_refresh(callback), + 1 => window.on_create(callback), + _ => window.on_confirm(callback), + } + } + for revoke in [false, true] { + let weak = window.as_weak(); + let model = controller.clone(); + let callback = move |id: slint::SharedString| { + if let Some(window) = weak.upgrade() { + let _ = if revoke { + model.borrow_mut().ask_revoke(&id) + } else { + model.borrow_mut().ask_rotate(&id) + }; + render(&window, &model.borrow()); + } + }; + if revoke { + window.on_revoke(callback) + } else { + window.on_rotate(callback) + } + } + { + let weak = window.as_weak(); + let model = controller.clone(); + window.on_hide_secret(move || { + model.borrow_mut().hide_secret(); + if let Some(window) = weak.upgrade() { + render(&window, &model.borrow()) + } + }); + } + { + let weak = window.as_weak(); + let model = controller.clone(); + window.on_cancel_confirmation(move || { + model.borrow_mut().cancel_confirmation(); + if let Some(window) = weak.upgrade() { + render(&window, &model.borrow()) + } + }); + } + { + let weak = window.as_weak(); + let model = controller.clone(); + window.on_dismiss(move || { + model.borrow_mut().close(); + if let Some(window) = weak.upgrade() { + render(&window, &model.borrow()); + let _ = window.hide(); + } + }); + } + { + let weak = window.as_weak(); + let model = controller.clone(); + window.window().on_close_requested(move || { + model.borrow_mut().close(); + if let Some(window) = weak.upgrade() { + render(&window, &model.borrow()) + } + slint::CloseRequestResponse::HideWindow + }); + } + let timer = slint::Timer::default(); + { + let weak = window.as_weak(); + let model = controller.clone(); + timer.start( + slint::TimerMode::Repeated, + Duration::from_millis(250), + move || { + if model.borrow_mut().expire(Instant::now()) + && let Some(window) = weak.upgrade() + { + render(&window, &model.borrow()); + } + }, + ); + } + Ok(Self { + window, + controller, + send, + _timer: timer, + }) + } + pub fn open( + &self, + main: &MainWindow, + scope: Scope, + connected: bool, + ) -> Result<(), slint::PlatformError> { + sync_palette(main, &self.window); + self.window.set_new_name("".into()); + self.window.set_new_endpoint("".into()); + self.window.set_new_kind(0); + if let Ok(command) = self.controller.borrow_mut().open(scope, connected) { + (self.send)(command) + } + render(&self.window, &self.controller.borrow()); + self.window.show() + } + pub fn receive_list(&self, request_id: &str, hooks: Vec) { + if self.controller.borrow_mut().receive_list(request_id, hooks) { + render(&self.window, &self.controller.borrow()) + } + } + pub fn receive_result(&self, request_id: &str, hook: Option, secret: String) { + if self + .controller + .borrow_mut() + .receive_result(request_id, hook, secret) + { + self.window.set_new_name("".into()); + self.window.set_new_endpoint("".into()); + render(&self.window, &self.controller.borrow()) + } + } + pub fn receive_error(&self, request_id: &str, code: u32) { + if self.controller.borrow_mut().receive_error(request_id, code) { + render(&self.window, &self.controller.borrow()) + } + } + pub fn disconnected(&self) { + self.controller.borrow_mut().disconnected(); + render(&self.window, &self.controller.borrow()) + } +} +fn render(window: &WebhookWindow, controller: &Controller) { + window.set_channel_name( + controller + .scope() + .map(|s| format!("# {}", s.channel_name)) + .unwrap_or_default() + .into(), + ); + window.set_allowed(controller.allowed()); + window.set_busy(controller.busy()); + window.set_error( + controller + .error() + .map(|e| i18n::text(e.message())) + .unwrap_or_default() + .into(), + ); + window.set_secret(controller.secret().into()); + window.set_secret_name( + controller + .secret_hook() + .map(|h| h.name.as_str()) + .unwrap_or_default() + .into(), + ); + window.set_secret_outgoing( + controller + .secret_hook() + .is_some_and(|h| h.direction == "outgoing"), + ); + window.set_secret_path( + controller + .secret_hook() + .filter(|h| h.direction == "incoming") + .map(|h| format!("POST /v1/webhooks/{}", h.id)) + .unwrap_or_default() + .into(), + ); + let confirmation = controller.confirmation(); + window.set_confirming(confirmation.is_some()); + window.set_confirming_revoke(confirmation.is_some_and(|(revoke, _)| revoke)); + window.set_confirmation_name( + confirmation + .and_then(|(_, id)| controller.hooks().iter().find(|h| h.id == id)) + .map(|h| h.name.as_str()) + .unwrap_or_default() + .into(), + ); + let rows = controller + .hooks() + .iter() + .map(|h| { + let mode = i18n::text(if h.direction == "outgoing" { + "Outgoing events" + } else if h.provider == "github" { + "GitHub updates" + } else { + "Incoming message" + }); + let paused = !h.suspended_reason.is_empty(); + let detail = if paused { + i18n::text("Paused: delivery queue full. Rotate the secret to resume.") + } else if h.direction == "outgoing" { + format!("{mode} · {}", h.endpoint) + } else { + mode + }; + WebhookRow { + id: h.id.as_str().into(), + name: h.name.as_str().into(), + detail: detail.into(), + paused, + } + }) + .collect::>(); + window.set_hooks(ModelRc::from(Rc::new(VecModel::from(rows)))); +} +fn sync_palette(main: &MainWindow, dialog: &WebhookWindow) { + let source = main.global::(); + let target = dialog.global::(); + target.set_bg_base(source.get_bg_base()); + target.set_bg_surface(source.get_bg_surface()); + target.set_bg_rail(source.get_bg_rail()); + target.set_bg_hover(source.get_bg_hover()); + target.set_bg_interactive(source.get_bg_interactive()); + target.set_bg_elevated(source.get_bg_elevated()); + target.set_selected(source.get_selected()); + target.set_border(source.get_border()); + target.set_accent(source.get_accent()); + target.set_text_primary(source.get_text_primary()); + target.set_text_secondary(source.get_text_secondary()); + target.set_text_muted(source.get_text_muted()); + target.set_text_disabled(source.get_text_disabled()); + target.set_danger(source.get_danger()); + target.set_success(source.get_success()); + target.set_font_size(source.get_font_size()); + target.set_on_accent(source.get_on_accent()); +} diff --git a/src/ui_smoke.rs b/src/ui_smoke.rs index 0ae57a3..a04abee 100644 --- a/src/ui_smoke.rs +++ b/src/ui_smoke.rs @@ -71,6 +71,8 @@ fn native_shell_keeps_drafts_settings_and_models_between_ticks() { timestamp: 0, edited: false, reactions: Default::default(), reply_to: None, }]); let state = Rc::new(RefCell::new(AppState { + webhooks: None, + connection_generation: 0, identity, net, ui, diff --git a/src/webhooks/controller.rs b/src/webhooks/controller.rs new file mode 100644 index 0000000..ceadde8 --- /dev/null +++ b/src/webhooks/controller.rs @@ -0,0 +1,409 @@ +use crate::net::{ + NetCommand, WebhookOperation, + wire::{WebhookInfo, WebhookRequest}, +}; +use std::time::{Duration, Instant}; + +/// A new connection gets a fresh generation, even when reconnecting to the same server. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct Scope { + pub connection: u64, + pub channel_id: String, + pub channel_name: String, + pub guild_id: String, +} +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum Kind { + Incoming, + GitHub, + Outgoing, +} +impl Kind { + pub fn from_index(index: i32) -> Option { + match index { + 0 => Some(Self::Incoming), + 1 => Some(Self::GitHub), + 2 => Some(Self::Outgoing), + _ => None, + } + } +} +pub struct Form { + pub name: String, + pub kind: Kind, + pub endpoint: String, +} +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum Error { + Disconnected, + InvalidChannel, + Busy, + PermissionDenied, + InvalidName, + InvalidEndpoint, + MissingHook, + RequestFailed, + RateLimited, + TimedOut, + InvalidResponse, + SecretVisible, +} +impl Error { + pub fn message(self) -> &'static str { + match self { + Self::Disconnected => "Connect to a server before managing webhooks.", + Self::InvalidChannel => "Choose a server text channel before managing webhooks.", + Self::Busy => "Wait for the current webhook request to finish.", + Self::PermissionDenied => { + "Only the server owner or members with Manage Webhooks can change integrations." + } + Self::InvalidName => "Use a webhook name between 1 and 64 characters.", + Self::InvalidEndpoint => { + "Enter an HTTPS destination without credentials, query parameters or a fragment." + } + Self::MissingHook => "This webhook is no longer available. Refresh the list.", + Self::RequestFailed => { + "The server could not complete this webhook request. Refresh before trying again." + } + Self::RateLimited => { + "Too many webhook requests, or this channel has reached its integration limit." + } + Self::TimedOut => { + "The request timed out. Its result is unknown; refresh before making another change." + } + Self::InvalidResponse => { + "The server returned an invalid webhook response. Refresh before trying again." + } + Self::SecretVisible => "Copy or hide the current secret before making another change.", + } + } +} +struct Secret(Vec); +impl Drop for Secret { + fn drop(&mut self) { + self.0.fill(0) + } +} +struct Pending { + request_id: String, + operation: WebhookOperation, + hook_id: String, + deadline: Instant, +} +/// One outstanding request with explicit correlation; late responses are always discarded. +#[derive(Default)] +pub struct Controller { + scope: Option, + connected: bool, + open: bool, + allowed: bool, + hooks: Vec, + pending: Option, + secret: Option, + secret_hook: Option, + confirmation: Option<(WebhookOperation, String)>, + error: Option, +} +impl Controller { + pub fn scope(&self) -> Option<&Scope> { + self.scope.as_ref() + } + pub fn hooks(&self) -> &[WebhookInfo] { + &self.hooks + } + pub fn error(&self) -> Option { + self.error + } + pub fn is_open(&self) -> bool { + self.open + } + pub fn busy(&self) -> bool { + self.pending.is_some() + } + pub fn allowed(&self) -> bool { + self.allowed && self.connected + } + pub fn secret(&self) -> &str { + self.secret + .as_ref() + .and_then(|s| std::str::from_utf8(&s.0).ok()) + .unwrap_or_default() + } + pub fn secret_hook(&self) -> Option<&WebhookInfo> { + self.secret_hook.as_ref() + } + pub fn confirmation(&self) -> Option<(bool, &str)> { + self.confirmation + .as_ref() + .map(|(op, id)| (matches!(op, WebhookOperation::Revoke), id.as_str())) + } + pub fn show_error(&mut self, error: Error) { + self.error = Some(error) + } + pub fn open(&mut self, scope: Scope, connected: bool) -> Result { + self.close(); + self.scope = Some(scope); + self.connected = connected; + self.open = true; + self.allowed = false; + self.hooks.clear(); + self.error = None; + if !connected { + return self.fail(Error::Disconnected); + } + if self + .scope + .as_ref() + .is_none_or(|s| s.channel_id.is_empty() || s.guild_id.is_empty()) + { + return self.fail(Error::InvalidChannel); + } + self.refresh() + } + pub fn close(&mut self) { + self.open = false; + self.pending = None; + self.confirmation = None; + self.hide_secret(); + } + pub fn disconnected(&mut self) { + self.connected = false; + self.allowed = false; + self.pending = None; + self.confirmation = None; + self.hooks.clear(); + self.hide_secret(); + if self.open { + self.error = Some(Error::Disconnected) + } + } + pub fn hide_secret(&mut self) { + self.secret = None; + self.secret_hook = None; + } + fn fail(&mut self, error: Error) -> Result { + self.error = Some(error); + Err(error) + } + fn ready(&mut self, write: bool) -> Result<(), Error> { + if !self.connected || !self.open { + return self.fail(Error::Disconnected); + } + if self.pending.is_some() { + return self.fail(Error::Busy); + } + if write && !self.allowed { + return self.fail(Error::PermissionDenied); + } + if self.secret.is_some() { + return self.fail(Error::SecretVisible); + } + Ok(()) + } + fn command(&mut self, operation: WebhookOperation, mut request: WebhookRequest) -> NetCommand { + let id = uuid::Uuid::new_v4().to_string(); + request.request_id = id.clone(); + self.pending = Some(Pending { + request_id: id, + operation, + hook_id: request.id.clone(), + deadline: Instant::now() + Duration::from_secs(15), + }); + self.error = None; + self.confirmation = None; + NetCommand::Webhook { operation, request } + } + pub fn refresh(&mut self) -> Result { + self.ready(false)?; + let Some(scope) = &self.scope else { + return self.fail(Error::InvalidChannel); + }; + Ok(self.command( + WebhookOperation::List, + WebhookRequest { + channel_id: scope.channel_id.clone(), + ..Default::default() + }, + )) + } + pub fn create(&mut self, form: Form) -> Result { + self.ready(true)?; + let name = form.name.trim(); + if name.is_empty() || name.chars().count() > 64 || name.chars().any(char::is_control) { + return self.fail(Error::InvalidName); + } + let endpoint = form.endpoint.trim(); + if form.kind == Kind::Outgoing + && (!endpoint.starts_with("https://") + || endpoint.len() > 2048 + || endpoint.contains(['?', '#', '@']) + || endpoint.chars().any(char::is_control)) + { + return self.fail(Error::InvalidEndpoint); + } + let Some(scope) = &self.scope else { + return self.fail(Error::InvalidChannel); + }; + let (direction, provider) = match form.kind { + Kind::Incoming => ("incoming", "generic"), + Kind::GitHub => ("incoming", "github"), + Kind::Outgoing => ("outgoing", "generic"), + }; + let req = WebhookRequest { + channel_id: scope.channel_id.clone(), + name: name.into(), + direction: direction.into(), + provider: provider.into(), + endpoint: if form.kind == Kind::Outgoing { + endpoint.into() + } else { + String::new() + }, + ..Default::default() + }; + Ok(self.command(WebhookOperation::Create, req)) + } + pub fn ask_rotate(&mut self, id: &str) -> Result<(), Error> { + self.ask(WebhookOperation::Rotate, id) + } + pub fn ask_revoke(&mut self, id: &str) -> Result<(), Error> { + self.ask(WebhookOperation::Revoke, id) + } + fn ask(&mut self, operation: WebhookOperation, id: &str) -> Result<(), Error> { + self.ready(true)?; + if !self.hooks.iter().any(|h| h.id == id && !h.revoked) { + return self.fail(Error::MissingHook); + } + self.confirmation = Some((operation, id.into())); + self.error = None; + Ok(()) + } + pub fn cancel_confirmation(&mut self) { + self.confirmation = None; + } + pub fn confirm(&mut self) -> Result { + self.ready(true)?; + let Some((operation, id)) = self.confirmation.take() else { + return self.fail(Error::MissingHook); + }; + Ok(self.command( + operation, + WebhookRequest { + id, + channel_id: self + .scope + .as_ref() + .map(|s| s.channel_id.clone()) + .unwrap_or_default(), + ..Default::default() + }, + )) + } + /// This timeout never claims the mutation failed: the server may already have committed it. + pub fn expire(&mut self, now: Instant) -> bool { + if self.pending.as_ref().is_some_and(|p| now >= p.deadline) { + self.pending = None; + self.allowed = false; + self.error = Some(Error::TimedOut); + true + } else { + false + } + } + fn take_pending(&mut self, id: &str) -> Option { + if self.pending.as_ref().is_some_and(|p| p.request_id == id) { + self.pending.take() + } else { + None + } + } + fn valid_hook(&self, h: &WebhookInfo) -> bool { + self.scope + .as_ref() + .is_some_and(|s| h.channel_id == s.channel_id) + && !h.id.is_empty() + && h.id.len() <= 128 + && h.name.chars().count() <= 64 + && h.endpoint.len() <= 2048 + && matches!( + (h.direction.as_str(), h.provider.as_str()), + ("incoming", "generic" | "github") | ("outgoing", "generic") + ) + } + pub fn receive_list(&mut self, request_id: &str, hooks: Vec) -> bool { + let Some(pending) = self.take_pending(request_id) else { + return false; + }; + if !matches!(pending.operation, WebhookOperation::List) + || hooks.len() > 32 + || hooks.iter().any(|h| !self.valid_hook(h) || h.revoked) + { + self.allowed = false; + self.error = Some(Error::InvalidResponse); + return true; + } + let mut ids = std::collections::HashSet::new(); + if hooks.iter().any(|h| !ids.insert(&h.id)) { + self.allowed = false; + self.error = Some(Error::InvalidResponse); + return true; + } + self.hooks = hooks; + self.allowed = true; + self.error = None; + true + } + pub fn receive_result( + &mut self, + request_id: &str, + hook: Option, + secret: String, + ) -> bool { + let Some(pending) = self.take_pending(request_id) else { + return false; + }; + let Some(hook) = hook else { + self.allowed = false; + self.error = Some(Error::InvalidResponse); + return true; + }; + let revoke = matches!(pending.operation, WebhookOperation::Revoke); + if matches!(pending.operation, WebhookOperation::List) + || !self.valid_hook(&hook) + || (!matches!(pending.operation, WebhookOperation::Create) + && hook.id != pending.hook_id) + || hook.revoked != revoke + || (revoke && !secret.is_empty()) + || (!revoke && (secret.len() != 64 || !secret.bytes().all(|b| b.is_ascii_hexdigit()))) + { + self.allowed = false; + self.error = Some(Error::InvalidResponse); + return true; + } + self.hooks.retain(|h| h.id != hook.id); + if !revoke { + self.hooks.push(hook.clone()); + self.secret = Some(Secret(secret.into_bytes())); + self.secret_hook = Some(hook); + } + self.error = None; + true + } + pub fn receive_error(&mut self, request_id: &str, code: u32) -> bool { + if self.take_pending(request_id).is_none() { + return false; + } + self.hide_secret(); + self.confirmation = None; + self.error = Some(match code { + 4 => { + self.allowed = false; + self.hooks.clear(); + Error::PermissionDenied + } + 6 => Error::RateLimited, + _ => Error::RequestFailed, + }); + true + } +} diff --git a/src/webhooks/mod.rs b/src/webhooks/mod.rs new file mode 100644 index 0000000..ea72f73 --- /dev/null +++ b/src/webhooks/mod.rs @@ -0,0 +1,5 @@ +//! Ephemeral channel webhook management. No credentials enter settings or Debug output. +mod controller; +pub use controller::{Controller, Error, Form, Kind, Scope}; +#[cfg(test)] +mod tests; diff --git a/src/webhooks/tests.rs b/src/webhooks/tests.rs new file mode 100644 index 0000000..bafa3b0 --- /dev/null +++ b/src/webhooks/tests.rs @@ -0,0 +1,172 @@ +use super::*; +use crate::net::{NetCommand, WebhookOperation, wire::WebhookInfo}; +use std::time::{Duration, Instant}; +fn scope(channel: &str) -> Scope { + Scope { + connection: 1, + channel_id: channel.into(), + channel_name: channel.into(), + guild_id: "guild".into(), + } +} +fn request_id(command: NetCommand) -> String { + match command { + NetCommand::Webhook { request, .. } => request.request_id, + _ => panic!("management command required"), + } +} +fn hook(channel: &str) -> WebhookInfo { + WebhookInfo { + id: "hook".into(), + channel_id: channel.into(), + name: "Builds".into(), + direction: "incoming".into(), + provider: "generic".into(), + ..Default::default() + } +} +fn authorized() -> Controller { + let mut c = Controller::default(); + let id = request_id(c.open(scope("target"), true).unwrap()); + assert!(c.receive_list(&id, vec![hook("target")])); + c +} +#[test] +fn permission_gate_requires_successful_server_response() { + let mut c = Controller::default(); + let id = request_id(c.open(scope("target"), true).unwrap()); + assert!(!c.allowed()); + assert!(c.receive_error(&id, 4)); + assert!(!c.allowed()); + assert_eq!(c.error(), Some(Error::PermissionDenied)); + assert!(matches!( + c.create(Form { + name: "CI".into(), + kind: Kind::Incoming, + endpoint: String::new() + }), + Err(Error::PermissionDenied) + )); + assert!(Controller::default().open(scope("target"), false).is_err()); +} +#[test] +fn stale_responses_cannot_change_channel_or_expose_secret() { + let mut c = authorized(); + let first = request_id( + c.create(Form { + name: "CI".into(), + kind: Kind::Incoming, + endpoint: String::new(), + }) + .unwrap(), + ); + let second = request_id(c.open(scope("other"), true).unwrap()); + assert!(!c.receive_result(&first, Some(hook("target")), "a".repeat(64))); + assert!(c.secret().is_empty()); + assert!(c.receive_list(&second, vec![])); + assert!(c.allowed()); + assert_eq!(c.scope().unwrap().channel_id, "other"); +} +#[test] +fn secrets_are_ephemeral_and_only_create_or_rotate_can_show_them() { + let mut c = authorized(); + let id = request_id( + c.create(Form { + name: "CI".into(), + kind: Kind::Incoming, + endpoint: String::new(), + }) + .unwrap(), + ); + assert!(c.receive_result(&id, Some(hook("target")), "a".repeat(64))); + assert_eq!(c.secret(), "a".repeat(64)); + assert!(matches!(c.refresh(), Err(Error::SecretVisible))); + c.hide_secret(); + assert!(c.secret().is_empty()); + c.ask_rotate("hook").unwrap(); + let id = request_id(c.confirm().unwrap()); + assert!(c.receive_result(&id, Some(hook("target")), "b".repeat(64))); + c.close(); + assert!(c.secret().is_empty()); + let id = request_id(c.open(scope("target"), true).unwrap()); + c.receive_list(&id, vec![hook("target")]); + c.ask_revoke("hook").unwrap(); + let id = request_id(c.confirm().unwrap()); + let mut h = hook("target"); + h.revoked = true; + assert!(c.receive_result(&id, Some(h), String::new())); + assert!(c.hooks().is_empty()); +} +#[test] +fn revoke_and_rotation_need_explicit_confirmation() { + let mut c = authorized(); + c.ask_revoke("hook").unwrap(); + assert!(!c.busy()); + assert_eq!(c.confirmation(), Some((true, "hook"))); + c.cancel_confirmation(); + assert!(c.confirm().is_err()); + c.ask_rotate("hook").unwrap(); + match c.confirm().unwrap() { + NetCommand::Webhook { operation, request } => { + assert_eq!(operation, WebhookOperation::Rotate); + assert_eq!(request.id, "hook"); + assert!(!request.request_id.is_empty()); + } + _ => panic!(), + } +} +#[test] +fn timeouts_allow_refresh_but_never_apply_late_mutation_results() { + let mut c = authorized(); + c.ask_rotate("hook").unwrap(); + let id = request_id(c.confirm().unwrap()); + assert!(c.expire(Instant::now() + Duration::from_secs(16))); + assert_eq!(c.error(), Some(Error::TimedOut)); + assert!(!c.allowed()); + let refresh = request_id(c.refresh().unwrap()); + assert!(!c.receive_result(&id, Some(hook("target")), "b".repeat(64))); + assert!(c.receive_list(&refresh, vec![hook("target")])); + assert!(c.secret().is_empty()); +} +#[test] +fn cross_channel_or_malformed_server_results_fail_closed() { + let mut c = Controller::default(); + let id = request_id(c.open(scope("target"), true).unwrap()); + assert!(c.receive_list(&id, vec![hook("other")])); + assert!(!c.allowed()); + assert_eq!(c.error(), Some(Error::InvalidResponse)); + let id = request_id(c.refresh().unwrap()); + let h = hook("target"); + assert!(c.receive_list(&id, vec![h.clone(), h])); + assert!(!c.allowed()); +} +#[test] +fn validation_is_bounded_and_disconnect_clears_privileges() { + let mut c = authorized(); + assert!(matches!( + c.create(Form { + name: "x".repeat(65), + kind: Kind::Incoming, + endpoint: String::new() + }), + Err(Error::InvalidName) + )); + for endpoint in [ + "http://example.org", + "https://user@example.org", + "https://example.org/?token=x", + ] { + assert!(matches!( + c.create(Form { + name: "CI".into(), + kind: Kind::Outgoing, + endpoint: endpoint.into() + }), + Err(Error::InvalidEndpoint) + )); + } + c.disconnected(); + assert!(!c.allowed()); + assert!(c.hooks().is_empty()); + assert_eq!(c.error(), Some(Error::Disconnected)); +} diff --git a/tests/gateway_interop.rs b/tests/gateway_interop.rs new file mode 100644 index 0000000..2979392 --- /dev/null +++ b/tests/gateway_interop.rs @@ -0,0 +1,306 @@ +//! Real gateway fixture. Run with VNOX_GATEWAY_BIN=/path/to/vnox-gateway cargo test --test gateway_interop -- --ignored. +use std::{ + path::PathBuf, + process::{Child, Command, Stdio}, + time::Duration, +}; +use vnox_client::{ + identity, + net::{self, NetCommand, NetEvent, NetHandle}, +}; + +struct Gateway { + child: Child, + directory: PathBuf, +} +impl Drop for Gateway { + fn drop(&mut self) { + let _ = self.child.kill(); + let _ = self.child.wait(); + let _ = std::fs::remove_dir_all(&self.directory); + } +} +async fn event(handle: &NetHandle, predicate: impl Fn(&NetEvent) -> bool) -> NetEvent { + static STAGE: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0); + let stage = STAGE.fetch_add(1, std::sync::atomic::Ordering::Relaxed) + 1; + eprintln!("waiting for gateway fixture stage {stage}"); + tokio::time::timeout(Duration::from_secs(8), async { + loop { + if let Some(event) = handle.try_recv() { + if let NetEvent::Disconnected { ref reason, .. } = event { + panic!("unexpected disconnect: {reason}"); + } + if let NetEvent::Error { code, ref message } = event { + panic!("gateway error {code}: {message}"); + } + if predicate(&event) { + return event; + } + if let NetEvent::WebhookError(ref result) = event { + panic!( + "unexpected webhook error at stage {stage}, request {}: {} ({})", + result.request_id, result.message, result.code + ); + } + } + tokio::time::sleep(Duration::from_millis(5)).await; + } + }) + .await + .unwrap_or_else(|_| panic!("gateway event timeout at stage {stage}")) +} +fn free_port() -> u16 { + std::net::TcpListener::bind("127.0.0.1:0") + .unwrap() + .local_addr() + .unwrap() + .port() +} + +#[tokio::test] +#[ignore = "requires a freshly built current VNOX gateway binary"] +async fn native_network_connects_signed_v2_and_exchanges_protobuf_chat() { + let binary = std::env::var_os("VNOX_GATEWAY_BIN") + .expect("set VNOX_GATEWAY_BIN to the current server binary"); + let directory = std::env::temp_dir().join(format!("vnox-interop-{}", uuid::Uuid::new_v4())); + std::fs::create_dir_all(&directory).unwrap(); + let port = free_port(); + let config = format!( + r#" +[node] +name = "interop-node" +address = "127.0.0.1" +[gateway] +bind = "127.0.0.1:{port}" +admin_bind = "127.0.0.1:0" +message_rate_per_sec = 0 +[voice] +bind = "127.0.0.1:7700" +[storage] +data_dir = "{}" +backend = "sqlite" +sqlite_path = "{}/fixture.db" +[server] +mode = "private" +"#, + directory.display(), + directory.display() + ); + let config_path = directory.join("config.toml"); + std::fs::write(&config_path, config).unwrap(); + let child = Command::new(binary) + .arg("--config") + .arg(config_path) + .stdout(Stdio::null()) + .stderr(Stdio::inherit()) + .spawn() + .unwrap(); + let _gateway = Gateway { child, directory }; + let address = format!("127.0.0.1:{port}"); + tokio::time::timeout(Duration::from_secs(8), async { + loop { + if tokio::net::TcpStream::connect(&address).await.is_ok() { + break; + } + tokio::time::sleep(Duration::from_millis(20)).await; + } + }) + .await + .unwrap(); + // In-memory identity only; never touch a real profile or encrypted vault. + let alice_identity = identity::generate("interop-alice"); + let alice_user_id = alice_identity.pubkey_hex.clone(); + let alice = net::spawn(alice_identity); + alice + .send(NetCommand::Connect { + address, + auto_reconnect: false, + }) + .await; + event( + &alice, + |e| matches!(e,NetEvent::Connected {node_name,..} if node_name=="interop-node"), + ) + .await; + event( + &alice, + |e| matches!(e,NetEvent::ChannelState {channel_id,..} if channel_id=="general"), + ) + .await; + alice.send(NetCommand::ChannelList).await; + event(&alice, |e| matches!(e,NetEvent::ChannelListEvent {channels} if channels.iter().any(|c|c.channel_id=="general"))).await; + alice + .send(NetCommand::SendChat { + channel_id: "general".into(), + content: "Привет / hello {untrusted}".into(), + reply_to: None, + }) + .await; + let chat = event( + &alice, + |e| matches!(e,NetEvent::ChatMessage {content,..} if content=="Привет / hello {untrusted}"), + ) + .await; + let NetEvent::ChatMessage { message_id, .. } = chat else { + unreachable!() + }; + alice + .send(NetCommand::MessageEdit { + channel_id: "general".into(), + message_id: message_id.clone(), + content: "edited".into(), + }) + .await; + event( + &alice, + |e| matches!(e,NetEvent::MessageEdited {content,..} if content=="edited"), + ) + .await; + alice + .send(NetCommand::MessageDelete { + channel_id: "general".into(), + message_id, + }) + .await; + event(&alice, |e| matches!(e, NetEvent::MessageDeleted { .. })).await; + alice + .send(NetCommand::GuildCreate { + name: "Webhook fixture guild".into(), + }) + .await; + let guild = event(&alice, |e| matches!(e, NetEvent::GuildCreated { .. })).await; + let NetEvent::GuildCreated { guild_id, .. } = guild else { + unreachable!() + }; + alice + .send(NetCommand::ChannelCreate { + channel_id: "hook-fixture".into(), + channel_name: "Integrations".into(), + kind: "text".into(), + guild_id: Some(guild_id), + }) + .await; + event( + &alice, + |e| matches!(e,NetEvent::ChannelState {channel_id,..} if channel_id=="hook-fixture"), + ) + .await; + use net::{WebhookOperation, wire::WebhookRequest}; + alice + .send(NetCommand::Webhook { + operation: WebhookOperation::Create, + request: WebhookRequest { + channel_id: "hook-fixture".into(), + name: "Fixture".into(), + direction: "incoming".into(), + provider: "generic".into(), + request_id: "create-fixture".into(), + ..Default::default() + }, + }) + .await; + let result = event( + &alice, + |e| matches!(e,NetEvent::WebhookResult(result) if result.request_id=="create-fixture"), + ) + .await; + let NetEvent::WebhookResult(result) = result else { + unreachable!() + }; + assert!(!result.secret.is_empty()); + assert!( + !format!("{result:?}").contains(&result.secret), + "one-time secret must never enter Debug output" + ); + let first_secret = result.secret; + let id = result.hook.unwrap().id; + alice + .send(NetCommand::Webhook { + operation: WebhookOperation::List, + request: WebhookRequest { + channel_id: "hook-fixture".into(), + request_id: "list-fixture".into(), + ..Default::default() + }, + }) + .await; + event(&alice,|e|matches!(e,NetEvent::WebhookList(result) if result.request_id=="list-fixture" && result.hooks.iter().any(|hook|hook.id==id))).await; + alice + .send(NetCommand::Webhook { + operation: WebhookOperation::Rotate, + request: WebhookRequest { + id: id.clone(), + request_id: "rotate-fixture".into(), + ..Default::default() + }, + }) + .await; + let rotated = event( + &alice, + |e| matches!(e,NetEvent::WebhookResult(result) if result.request_id=="rotate-fixture"), + ) + .await; + let NetEvent::WebhookResult(rotated) = rotated else { + unreachable!() + }; + assert!( + !rotated.secret.is_empty() && rotated.secret != first_secret, + "rotation must return a fresh secret" + ); + alice + .send(NetCommand::Webhook { + operation: WebhookOperation::Revoke, + request: WebhookRequest { + id, + request_id: "revoke-fixture".into(), + ..Default::default() + }, + }) + .await; + event(&alice,|e|matches!(e,NetEvent::WebhookResult(result) if result.request_id=="revoke-fixture" && result.secret.is_empty() && result.hook.as_ref().is_some_and(|hook|hook.revoked))).await; + let guest_identity = identity::generate("webhook-unprivileged"); + let guest_user_id = guest_identity.pubkey_hex.clone(); + let guest = net::spawn(guest_identity); + guest + .send(NetCommand::Connect { + address: format!("127.0.0.1:{port}"), + auto_reconnect: false, + }) + .await; + event(&guest, |e| matches!(e, NetEvent::Connected { .. })).await; + guest + .send(NetCommand::Webhook { + operation: WebhookOperation::List, + request: WebhookRequest { + channel_id: "hook-fixture".into(), + request_id: "denied-fixture".into(), + ..Default::default() + }, + }) + .await; + event(&guest,|e|matches!(e,NetEvent::WebhookError(result) if result.request_id=="denied-fixture" && result.code==4)).await; + alice + .send(NetCommand::FriendRequest { + target_user_id: guest_user_id.clone(), + }) + .await; + event( + &guest, + |e| matches!(e,NetEvent::FriendRequested {user_id,..} if user_id==&alice_user_id), + ) + .await; + guest + .send(NetCommand::FriendAccept { + user_id: alice_user_id.clone(), + }) + .await; + event( + &alice, + |e| matches!(e,NetEvent::FriendAccepted {user_id,..} if user_id==&guest_user_id), + ) + .await; + alice.send(NetCommand::FriendList).await; + event(&alice,|e|matches!(e,NetEvent::FriendList {friends} if friends.iter().any(|friend|friend.user_id==guest_user_id))).await; + guest.send(NetCommand::Disconnect).await; + alice.send(NetCommand::Disconnect).await; +} diff --git a/translations/en/LC_MESSAGES/vnox-client.po b/translations/en/LC_MESSAGES/vnox-client.po index 2319d0c..992e947 100644 --- a/translations/en/LC_MESSAGES/vnox-client.po +++ b/translations/en/LC_MESSAGES/vnox-client.po @@ -14,6 +14,9 @@ msgstr "Accept" msgid "Add a passphrase to encrypt your saved secret key." msgstr "Add a passphrase to encrypt your saved secret key." +msgid "Add an integration" +msgstr "Add an integration" + msgid "Add by ID" msgstr "Add by ID" @@ -29,6 +32,9 @@ msgstr "All" msgid "All friends" msgstr "All friends" +msgid "An integration can post only in this channel, using its own identity." +msgstr "An integration can post only in this channel, using its own identity." + msgid "App theme" msgstr "App theme" @@ -62,18 +68,32 @@ msgstr "Blocked" msgid "Blocked users" msgstr "Blocked users" +msgid "Broadcast" +msgstr "Broadcast" + +msgid "Broadcasting · {n} viewer" +msgid_plural "Broadcasting · {n} viewers" +msgstr[0] "Broadcasting · {n} viewer" +msgstr[1] "Broadcasting · {n} viewers" + msgid "CSS variables in :root control colours and text size. CSS selectors, layout rules and external resources are not supported." msgstr "CSS variables in :root control colours and text size. CSS selectors, layout rules and external resources are not supported." msgid "Cancel" msgstr "Cancel" +msgid "Cancel broadcast" +msgstr "Cancel broadcast" + msgid "Change passphrase" msgstr "Change passphrase" msgid "Change photo" msgstr "Change photo" +msgid "Channel webhooks" +msgstr "Channel webhooks" + msgid "Choose a screen or window in the system dialog…" msgstr "Choose a screen or window in the system dialog…" @@ -98,6 +118,9 @@ msgstr "Choose your language. Changes apply immediately." msgid "Choose your microphone and audio output." msgstr "Choose your microphone and audio output." +msgid "Close" +msgstr "Close" + msgid "Collapse" msgstr "Collapse" @@ -107,15 +130,24 @@ msgstr "Connect" msgid "Connect to a server" msgstr "Connect to a server" +msgid "Connecting to broadcast…" +msgstr "Connecting to broadcast…" + msgid "Connecting…" msgstr "Connecting…" +msgid "Copy and hide" +msgstr "Copy and hide" + msgid "Copy this content into a file and keep it separately from this device." msgstr "Copy this content into a file and keep it separately from this device." msgid "Create backup" msgstr "Create backup" +msgid "Create webhook" +msgstr "Create webhook" + msgid "Crop photo" msgstr "Crop photo" @@ -188,9 +220,15 @@ msgstr "For example: /home/user/themes/my-vnox.css" msgid "Friends" msgstr "Friends" +msgid "GitHub updates" +msgstr "GitHub updates" + msgid "Headphones or speakers" msgstr "Headphones or speakers" +msgid "Hide secret" +msgstr "Hide secret" + msgid "Hold here to talk" msgstr "Hold here to talk" @@ -209,9 +247,18 @@ msgstr "Importing replaces your saved profile. Back up your current keys first." msgid "In voice" msgstr "In voice" +msgid "Incoming message" +msgstr "Incoming message" + msgid "Incoming requests" msgstr "Incoming requests" +msgid "Integration name" +msgstr "Integration name" + +msgid "It is shown once. Closing this window or hiding it removes it from the client." +msgstr "It is shown once. Closing this window or hiding it removes it from the client." + msgid "Join" msgstr "Join" @@ -221,6 +268,9 @@ msgstr "Joining voice…" msgid "Keep your keys in a safe place to restore your profile on another device." msgstr "Keep your keys in a safe place to restore your profile on another device." +msgid "LIVE" +msgstr "LIVE" + msgid "LOCAL PREVIEW" msgstr "LOCAL PREVIEW" @@ -239,9 +289,6 @@ msgstr "Less bandwidth" msgid "Light" msgstr "Light" -msgid "Local capture is available. Broadcasting to other members is not available until the media transport is connected." -msgstr "Local capture is available. Broadcasting to other members is not available until the media transport is connected." - msgid "Main variables: --vnox-bg, --vnox-surface, --vnox-text, --vnox-accent, --vnox-font-size. See the theme documentation for the full list." msgstr "Main variables: --vnox-bg, --vnox-surface, --vnox-text, --vnox-accent, --vnox-font-size. See the theme documentation for the full list." @@ -269,9 +316,15 @@ msgstr "Microphone is transmitting" msgid "Microphone uses your existing voice controls. This preview never enables or mixes it." msgstr "Microphone uses your existing voice controls. This preview never enables or mixes it." +msgid "Mute broadcast" +msgstr "Mute broadcast" + msgid "Mute microphone" msgstr "Mute microphone" +msgid "New deliveries stop. Existing messages stay in the channel." +msgstr "New deliveries stop. Existing messages stay in the channel." + msgid "New passphrase · at least 8 characters" msgstr "New passphrase · at least 8 characters" @@ -299,6 +352,9 @@ msgstr "No passphrase" msgid "No saved servers yet." msgstr "No saved servers yet." +msgid "No webhooks yet. Add an integration below." +msgstr "No webhooks yet. Add an integration below." + msgid "Nothing is being broadcast" msgstr "Nothing is being broadcast" @@ -317,6 +373,12 @@ msgstr "Online friends" msgid "Only a currently playing application can be selected. If it closes, capture stops without switching sources." msgstr "Only a currently playing application can be selected. If it closes, capture stops without switching sources." +msgid "Only public HTTPS destinations. Events: message.created." +msgstr "Only public HTTPS destinations. Events: message.created." + +msgid "Only the server owner or members with Manage Webhooks can change integrations." +msgstr "Only the server owner or members with Manage Webhooks can change integrations." + msgid "Open" msgstr "Open" @@ -326,6 +388,9 @@ msgstr "Open a friend's profile to send a message." msgid "Open microphone" msgstr "Open microphone" +msgid "Outgoing events" +msgstr "Outgoing events" + msgid "Photo" msgstr "Photo" @@ -341,6 +406,9 @@ msgstr "Preview" msgid "Preview active · not broadcasting" msgstr "Preview active · not broadcasting" +msgid "Preview stays local until you start broadcasting. Only members of your voice channel can watch." +msgstr "Preview stays local until you start broadcasting. Only members of your voice channel can watch." + msgid "Profile and keys" msgstr "Profile and keys" @@ -353,6 +421,9 @@ msgstr "Profile photo" msgid "Push to talk" msgstr "Push to talk" +msgid "Receive updates here, or send signed channel events to your own service." +msgstr "Receive updates here, or send signed channel events to your own service." + msgid "Reconnect automatically" msgstr "Reconnect automatically" @@ -383,9 +454,30 @@ msgstr "Reset" msgid "Reset crop" msgstr "Reset crop" +msgid "Revoke" +msgstr "Revoke" + +msgid "Revoke this webhook?" +msgstr "Revoke this webhook?" + +msgid "Revoke webhook" +msgstr "Revoke webhook" + +msgid "Rotate" +msgstr "Rotate" + +msgid "Rotate secret" +msgstr "Rotate secret" + +msgid "Rotate this secret?" +msgstr "Rotate this secret?" + msgid "Save passphrase" msgstr "Save passphrase" +msgid "Save this secret now" +msgstr "Save this secret now" + msgid "Saved servers" msgstr "Saved servers" @@ -416,9 +508,21 @@ msgstr "Share audio" msgid "Speaking" msgstr "Speaking" +msgid "Start broadcast" +msgstr "Start broadcast" + +msgid "Starting broadcast…" +msgstr "Starting broadcast…" + msgid "Stop" msgstr "Stop" +msgid "Stop broadcast" +msgstr "Stop broadcast" + +msgid "Stop watching" +msgstr "Stop watching" + msgid "System default" msgstr "System default" @@ -437,6 +541,9 @@ msgstr "Text size" msgid "The backup passphrase is optional. Without it, your backup contains an unprotected secret key." msgstr "The backup passphrase is optional. Without it, your backup contains an unprotected secret key." +msgid "The old secret stops working. Update the external integration with the new secret." +msgstr "The old secret stops working. Update the external integration with the new secret." + msgid "Theme editor" msgstr "Theme editor" @@ -458,12 +565,21 @@ msgstr "Unlock" msgid "Unlock profile" msgstr "Unlock profile" +msgid "Unmute broadcast" +msgstr "Unmute broadcast" + msgid "Unmute microphone" msgstr "Unmute microphone" msgid "Use photo" msgstr "Use photo" +msgid "Use this key to verify X-Vnox-Signature over the timestamp and raw request body." +msgstr "Use this key to verify X-Vnox-Signature over the timestamp and raw request body." + +msgid "Use your server's HTTPS webhook endpoint. Generic integrations send this secret in Authorization: Bearer; GitHub uses it as the webhook secret." +msgstr "Use your server's HTTPS webhook endpoint. Generic integrations send this secret in Authorization: Bearer; GitHub uses it as the webhook secret." + msgid "Video, application audio and your microphone stay separate." msgstr "Video, application audio and your microphone stay separate." @@ -488,6 +604,18 @@ msgstr "Voice rooms" msgid "Volume" msgstr "Volume" +msgid "Waiting for the server…" +msgstr "Waiting for the server…" + +msgid "Watch broadcast" +msgstr "Watch broadcast" + +msgid "Webhook secret" +msgstr "Webhook secret" + +msgid "Webhooks" +msgstr "Webhooks" + msgid "Your key" msgstr "Your key" diff --git a/translations/ru.json b/translations/ru.json index 52eed1d..5a27904 100644 --- a/translations/ru.json +++ b/translations/ru.json @@ -288,5 +288,68 @@ "Invalid video stride": "Некорректный шаг строк видеокадра.", "Missing GStreamer plugin: {name}. Install the PipeWire and base plugins.": "Отсутствует плагин GStreamer: {name}. Установи плагины PipeWire и базовый набор.", "Capture stopped: {error}": "Захват остановлен: {error}", - "Capture failed: {error}": "Ошибка захвата: {error}" + "Capture failed: {error}": "Ошибка захвата: {error}", + "Channel webhooks": "Вебхуки канала", + "Receive updates here, or send signed channel events to your own service.": "Получайте обновления в этом канале или отправляйте подписанные события в свой сервис.", + "Waiting for the server…": "Ожидание ответа сервера…", + "Save this secret now": "Сохраните секрет сейчас", + "It is shown once. Closing this window or hiding it removes it from the client.": "Секрет показывается один раз. После закрытия окна или скрытия он удаляется из клиента.", + "Webhook secret": "Секрет вебхука", + "Use this key to verify X-Vnox-Signature over the timestamp and raw request body.": "Проверяйте X-Vnox-Signature этим ключом: подпись включает время и исходное тело запроса.", + "Use your server's HTTPS webhook endpoint. Generic integrations send this secret in Authorization: Bearer; GitHub uses it as the webhook secret.": "Используйте HTTPS-адрес вебхуков вашего сервера. Обычные интеграции передают секрет в Authorization: Bearer; в GitHub укажите его как секрет вебхука.", + "Copy and hide": "Скопировать и скрыть", + "Hide secret": "Скрыть секрет", + "Revoke this webhook?": "Отозвать этот вебхук?", + "Rotate this secret?": "Заменить этот секрет?", + "New deliveries stop. Existing messages stay in the channel.": "Новые доставки прекратятся. Отправленные сообщения останутся в канале.", + "The old secret stops working. Update the external integration with the new secret.": "Старый секрет перестанет работать. Укажите новый секрет во внешней интеграции.", + "Revoke webhook": "Отозвать вебхук", + "Rotate secret": "Заменить секрет", + "No webhooks yet. Add an integration below.": "Вебхуков пока нет. Добавьте интеграцию ниже.", + "Rotate": "Заменить секрет", + "Revoke": "Отозвать", + "Add an integration": "Добавить интеграцию", + "Integration name": "Название интеграции", + "Incoming message": "Входящие сообщения", + "GitHub updates": "Обновления GitHub", + "Outgoing events": "Исходящие события", + "Only public HTTPS destinations. Events: message.created.": "Только публичные HTTPS-адреса. События: message.created.", + "An integration can post only in this channel, using its own identity.": "Интеграция публикует сообщения только в этом канале и от своего имени.", + "Create webhook": "Создать вебхук", + "Only the server owner or members with Manage Webhooks can change integrations.": "Изменять интеграции могут владелец сервера и участники с правом «Управление вебхуками».", + "Connect to a server before managing webhooks.": "Для управления вебхуками подключитесь к серверу.", + "Choose a server text channel before managing webhooks.": "Для управления вебхуками выберите текстовый канал сервера.", + "Wait for the current webhook request to finish.": "Дождитесь завершения текущего запроса.", + "Use a webhook name between 1 and 64 characters.": "Название вебхука должно содержать от 1 до 64 символов.", + "Enter an HTTPS destination without credentials, query parameters or a fragment.": "Введите HTTPS-адрес без данных для входа, параметров запроса и фрагмента.", + "This webhook is no longer available. Refresh the list.": "Этот вебхук больше недоступен. Обновите список.", + "The server could not complete this webhook request. Refresh before trying again.": "Сервер не смог выполнить запрос. Обновите список перед повторной попыткой.", + "Too many webhook requests, or this channel has reached its integration limit.": "Слишком много запросов либо достигнут лимит интеграций канала.", + "The request timed out. Its result is unknown; refresh before making another change.": "Время ожидания истекло. Результат неизвестен; обновите список перед следующим изменением.", + "The server returned an invalid webhook response. Refresh before trying again.": "Сервер вернул некорректный ответ. Обновите список перед повторной попыткой.", + "Copy or hide the current secret before making another change.": "Скопируйте или скройте текущий секрет перед следующим изменением.", + "Paused: delivery queue full. Rotate the secret to resume.": "Приостановлен: очередь доставки заполнена. Замените секрет для возобновления.", + "Close": "Закрыть", + "Broadcast": "Трансляция", + "Broadcasting · {0} viewers": "Трансляция · зрителей: {0}", + "Connecting to broadcast…": "Подключение к трансляции…", + "LIVE": "В ЭФИРЕ", + "Mute broadcast": "Выключить звук трансляции", + "Preview stays local until you start broadcasting. Only members of your voice channel can watch.": "Предпросмотр остаётся локальным, пока ты не начнёшь трансляцию. Смотреть её могут только участники твоей голосовой комнаты.", + "Start broadcast": "Начать трансляцию", + "Stop broadcast": "Остановить трансляцию", + "Stop watching": "Закрыть просмотр", + "Unmute broadcast": "Включить звук трансляции", + "Watch broadcast": "Смотреть трансляцию", + "Webhooks": "Вебхуки", + "Broadcasting · {n} viewer": [ + "Трансляция · {n} зритель", + "Трансляция · {n} зрителя", + "Трансляция · {n} зрителей" + ], + "Cancel broadcast": "Отменить трансляцию", + "Starting broadcast…": "Запуск трансляции…", + "WebRTC connection timed out": "Время ожидания соединения WebRTC истекло", + "Broadcast authorization timed out": "Время ожидания разрешения на трансляцию истекло", + "Broadcast connection timed out": "Время ожидания подключения к трансляции истекло" } diff --git a/translations/ru/LC_MESSAGES/vnox-client.po b/translations/ru/LC_MESSAGES/vnox-client.po index dddf900..f05dd88 100644 --- a/translations/ru/LC_MESSAGES/vnox-client.po +++ b/translations/ru/LC_MESSAGES/vnox-client.po @@ -14,6 +14,9 @@ msgstr "Принять" msgid "Add a passphrase to encrypt your saved secret key." msgstr "Добавь пароль, чтобы зашифровать сохранённый секретный ключ." +msgid "Add an integration" +msgstr "Добавить интеграцию" + msgid "Add by ID" msgstr "Добавить по ID" @@ -29,6 +32,9 @@ msgstr "Все" msgid "All friends" msgstr "Все друзья" +msgid "An integration can post only in this channel, using its own identity." +msgstr "Интеграция публикует сообщения только в этом канале и от своего имени." + msgid "App theme" msgstr "Тема приложения" @@ -62,18 +68,33 @@ msgstr "Блокировки" msgid "Blocked users" msgstr "Заблокированные пользователи" +msgid "Broadcast" +msgstr "Трансляция" + +msgid "Broadcasting · {n} viewer" +msgid_plural "Broadcasting · {n} viewers" +msgstr[0] "Трансляция · {n} зритель" +msgstr[1] "Трансляция · {n} зрителя" +msgstr[2] "Трансляция · {n} зрителей" + msgid "CSS variables in :root control colours and text size. CSS selectors, layout rules and external resources are not supported." msgstr "CSS-переменные в блоке :root меняют цвета и размер текста. Обычные CSS-селекторы, стили размещения и внешние ресурсы не поддерживаются." msgid "Cancel" msgstr "Отмена" +msgid "Cancel broadcast" +msgstr "Отменить трансляцию" + msgid "Change passphrase" msgstr "Изменить пароль" msgid "Change photo" msgstr "Изменить фото" +msgid "Channel webhooks" +msgstr "Вебхуки канала" + msgid "Choose a screen or window in the system dialog…" msgstr "Выбери экран или окно в системном диалоге…" @@ -98,6 +119,9 @@ msgstr "Выбери язык. Изменения применяются сра msgid "Choose your microphone and audio output." msgstr "Выбери, откуда брать голос и куда выводить звук." +msgid "Close" +msgstr "Закрыть" + msgid "Collapse" msgstr "Свернуть" @@ -107,15 +131,24 @@ msgstr "Подключиться" msgid "Connect to a server" msgstr "Подключиться к серверу" +msgid "Connecting to broadcast…" +msgstr "Подключение к трансляции…" + msgid "Connecting…" msgstr "Подключение…" +msgid "Copy and hide" +msgstr "Скопировать и скрыть" + msgid "Copy this content into a file and keep it separately from this device." msgstr "Скопируй содержимое в файл и сохрани отдельно от устройства." msgid "Create backup" msgstr "Создать копию" +msgid "Create webhook" +msgstr "Создать вебхук" + msgid "Crop photo" msgstr "Выбрать область фото" @@ -188,9 +221,15 @@ msgstr "Например: /home/user/themes/my-vnox.css" msgid "Friends" msgstr "Друзья" +msgid "GitHub updates" +msgstr "Обновления GitHub" + msgid "Headphones or speakers" msgstr "Наушники или колонки" +msgid "Hide secret" +msgstr "Скрыть секрет" + msgid "Hold here to talk" msgstr "Удерживай здесь, чтобы говорить" @@ -209,9 +248,18 @@ msgstr "Импорт заменит сохранённый профиль. Сн msgid "In voice" msgstr "В голосовой комнате" +msgid "Incoming message" +msgstr "Входящие сообщения" + msgid "Incoming requests" msgstr "Входящие запросы" +msgid "Integration name" +msgstr "Название интеграции" + +msgid "It is shown once. Closing this window or hiding it removes it from the client." +msgstr "Секрет показывается один раз. После закрытия окна или скрытия он удаляется из клиента." + msgid "Join" msgstr "Войти" @@ -221,6 +269,9 @@ msgstr "Подключение к голосовой комнате…" msgid "Keep your keys in a safe place to restore your profile on another device." msgstr "Сохрани ключи в безопасном месте, чтобы восстановить профиль на другом устройстве." +msgid "LIVE" +msgstr "В ЭФИРЕ" + msgid "LOCAL PREVIEW" msgstr "ЛОКАЛЬНЫЙ ПРОСМОТР" @@ -239,9 +290,6 @@ msgstr "Меньше трафика" msgid "Light" msgstr "Светлая" -msgid "Local capture is available. Broadcasting to other members is not available until the media transport is connected." -msgstr "Локальный захват доступен. Трансляция другим участникам появится после подключения транспорта медиа." - msgid "Main variables: --vnox-bg, --vnox-surface, --vnox-text, --vnox-accent, --vnox-font-size. See the theme documentation for the full list." msgstr "Основные переменные: --vnox-bg, --vnox-surface, --vnox-text, --vnox-accent, --vnox-font-size. Полный список описан в документации тем." @@ -269,9 +317,15 @@ msgstr "Микрофон передаёт звук" msgid "Microphone uses your existing voice controls. This preview never enables or mixes it." msgstr "Микрофон управляется обычными кнопками голосовой связи. Предпросмотр не включает и не подмешивает его." +msgid "Mute broadcast" +msgstr "Выключить звук трансляции" + msgid "Mute microphone" msgstr "Выключить микрофон" +msgid "New deliveries stop. Existing messages stay in the channel." +msgstr "Новые доставки прекратятся. Отправленные сообщения останутся в канале." + msgid "New passphrase · at least 8 characters" msgstr "Новый пароль · минимум 8 символов" @@ -299,6 +353,9 @@ msgstr "Без пароля" msgid "No saved servers yet." msgstr "Сохранённых серверов пока нет." +msgid "No webhooks yet. Add an integration below." +msgstr "Вебхуков пока нет. Добавьте интеграцию ниже." + msgid "Nothing is being broadcast" msgstr "Трансляция не ведётся" @@ -317,6 +374,12 @@ msgstr "Друзья в сети" msgid "Only a currently playing application can be selected. If it closes, capture stops without switching sources." msgstr "Можно выбрать только приложение, которое сейчас воспроизводит звук. Если оно закроется, захват остановится без смены источника." +msgid "Only public HTTPS destinations. Events: message.created." +msgstr "Только публичные HTTPS-адреса. События: message.created." + +msgid "Only the server owner or members with Manage Webhooks can change integrations." +msgstr "Изменять интеграции могут владелец сервера и участники с правом «Управление вебхуками»." + msgid "Open" msgstr "Открыть" @@ -326,6 +389,9 @@ msgstr "Открой профиль друга, чтобы написать ем msgid "Open microphone" msgstr "Открытый микрофон" +msgid "Outgoing events" +msgstr "Исходящие события" + msgid "Photo" msgstr "Фото" @@ -341,6 +407,9 @@ msgstr "Предпросмотр" msgid "Preview active · not broadcasting" msgstr "Предпросмотр активен · без трансляции" +msgid "Preview stays local until you start broadcasting. Only members of your voice channel can watch." +msgstr "Предпросмотр остаётся локальным, пока ты не начнёшь трансляцию. Смотреть её могут только участники твоей голосовой комнаты." + msgid "Profile and keys" msgstr "Профиль и ключи" @@ -353,6 +422,9 @@ msgstr "Фото профиля" msgid "Push to talk" msgstr "Нажми и говори" +msgid "Receive updates here, or send signed channel events to your own service." +msgstr "Получайте обновления в этом канале или отправляйте подписанные события в свой сервис." + msgid "Reconnect automatically" msgstr "Переподключаться при потере связи" @@ -383,9 +455,30 @@ msgstr "Сбросить" msgid "Reset crop" msgstr "Сбросить кадр" +msgid "Revoke" +msgstr "Отозвать" + +msgid "Revoke this webhook?" +msgstr "Отозвать этот вебхук?" + +msgid "Revoke webhook" +msgstr "Отозвать вебхук" + +msgid "Rotate" +msgstr "Заменить секрет" + +msgid "Rotate secret" +msgstr "Заменить секрет" + +msgid "Rotate this secret?" +msgstr "Заменить этот секрет?" + msgid "Save passphrase" msgstr "Сохранить пароль" +msgid "Save this secret now" +msgstr "Сохраните секрет сейчас" + msgid "Saved servers" msgstr "Сохранённые серверы" @@ -416,9 +509,21 @@ msgstr "Звук демонстрации" msgid "Speaking" msgstr "Говорит" +msgid "Start broadcast" +msgstr "Начать трансляцию" + +msgid "Starting broadcast…" +msgstr "Запуск трансляции…" + msgid "Stop" msgstr "Остановить" +msgid "Stop broadcast" +msgstr "Остановить трансляцию" + +msgid "Stop watching" +msgstr "Закрыть просмотр" + msgid "System default" msgstr "Как в системе" @@ -437,6 +542,9 @@ msgstr "Размер текста" msgid "The backup passphrase is optional. Without it, your backup contains an unprotected secret key." msgstr "Пароль резервной копии можно оставить пустым. Такая копия содержит секретный ключ без защиты." +msgid "The old secret stops working. Update the external integration with the new secret." +msgstr "Старый секрет перестанет работать. Укажите новый секрет во внешней интеграции." + msgid "Theme editor" msgstr "Редактор темы" @@ -458,12 +566,21 @@ msgstr "Разблокировать" msgid "Unlock profile" msgstr "Разблокировать профиль" +msgid "Unmute broadcast" +msgstr "Включить звук трансляции" + msgid "Unmute microphone" msgstr "Включить микрофон" msgid "Use photo" msgstr "Использовать фото" +msgid "Use this key to verify X-Vnox-Signature over the timestamp and raw request body." +msgstr "Проверяйте X-Vnox-Signature этим ключом: подпись включает время и исходное тело запроса." + +msgid "Use your server's HTTPS webhook endpoint. Generic integrations send this secret in Authorization: Bearer; GitHub uses it as the webhook secret." +msgstr "Используйте HTTPS-адрес вебхуков вашего сервера. Обычные интеграции передают секрет в Authorization: Bearer; в GitHub укажите его как секрет вебхука." + msgid "Video, application audio and your microphone stay separate." msgstr "Видео, звук приложения и микрофон передаются отдельно." @@ -488,6 +605,18 @@ msgstr "Голосовые комнаты" msgid "Volume" msgstr "Громкость" +msgid "Waiting for the server…" +msgstr "Ожидание ответа сервера…" + +msgid "Watch broadcast" +msgstr "Смотреть трансляцию" + +msgid "Webhook secret" +msgstr "Секрет вебхука" + +msgid "Webhooks" +msgstr "Вебхуки" + msgid "Your key" msgstr "Твой ключ"