Compare commits
13
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
82e1740d3c | ||
|
|
4dc1bcd546 | ||
|
|
067997f9ba | ||
|
|
660eb27a84 | ||
|
|
913b0b6b20 | ||
|
|
36fb8bfa9a | ||
|
|
2e9164745f | ||
|
|
3b640726d7 | ||
|
|
381e00bc0e | ||
|
|
1a3c481f4c | ||
|
|
f927567105 | ||
|
|
5c11947bd7 | ||
|
|
7349744d16 |
+29
@@ -2,10 +2,39 @@
|
||||
name = "peerspeak"
|
||||
version = "0.4.0"
|
||||
edition = "2024"
|
||||
description = "Decentralized peer-to-peer voice chat (Rust/iroh/PipeWire/Opus/iced)"
|
||||
# Application crate, not a crates.io library — refuse `cargo publish` and let
|
||||
# cargo-deny's [licenses.private] skip the missing-license check.
|
||||
publish = false
|
||||
|
||||
# Debian/Ubuntu packaging (cargo-deb). Mirrors packaging/PKGBUILD: only the main
|
||||
# `peerspeak` binary ships (not test_net/specview), plus the desktop entry and the
|
||||
# hicolor icon set. Runtime shared-lib deps (libpipewire, libopus, libc, …) are
|
||||
# resolved by dpkg-shlibdeps via `depends = "$auto"`. Build inside a Debian/Ubuntu
|
||||
# distrobox so the binary links that distro's glibc, then `cargo deb --no-build`.
|
||||
[package.metadata.deb]
|
||||
maintainer = "mollusk <jitty+lc1iz0dc@protonmail.com>"
|
||||
copyright = "2026, mollusk. Private build — not for redistribution."
|
||||
section = "net"
|
||||
priority = "optional"
|
||||
depends = "$auto"
|
||||
# pixelpass = in-room screen sharing; mpv = the screen-share viewer (vlc fallback).
|
||||
recommends = "pixelpass, mpv"
|
||||
extended-description = "Decentralized peer-to-peer voice chat over iroh (QUIC) with PipeWire audio, the Opus codec, and an iced GUI. Full-mesh, no central server."
|
||||
assets = [
|
||||
["target/release/peerspeak", "usr/bin/", "755"],
|
||||
["packaging/peerspeak.desktop", "usr/share/applications/", "644"],
|
||||
["assets/icons/peerspeak.svg", "usr/share/icons/hicolor/scalable/apps/peerspeak.svg", "644"],
|
||||
["assets/icons/peerspeak-16.png", "usr/share/icons/hicolor/16x16/apps/peerspeak.png", "644"],
|
||||
["assets/icons/peerspeak-24.png", "usr/share/icons/hicolor/24x24/apps/peerspeak.png", "644"],
|
||||
["assets/icons/peerspeak-32.png", "usr/share/icons/hicolor/32x32/apps/peerspeak.png", "644"],
|
||||
["assets/icons/peerspeak-48.png", "usr/share/icons/hicolor/48x48/apps/peerspeak.png", "644"],
|
||||
["assets/icons/peerspeak-64.png", "usr/share/icons/hicolor/64x64/apps/peerspeak.png", "644"],
|
||||
["assets/icons/peerspeak-128.png", "usr/share/icons/hicolor/128x128/apps/peerspeak.png", "644"],
|
||||
["assets/icons/peerspeak-256.png", "usr/share/icons/hicolor/256x256/apps/peerspeak.png", "644"],
|
||||
["assets/icons/peerspeak-512.png", "usr/share/icons/hicolor/512x512/apps/peerspeak.png", "644"],
|
||||
]
|
||||
|
||||
[lib]
|
||||
name = "peerspeak"
|
||||
path = "src/lib.rs"
|
||||
|
||||
@@ -225,6 +225,20 @@ state change; rate-limit pings), tickets from friends (validate defensively, no
|
||||
auto-join), the discovery publish (only when toggled, ideally auto-expiring).
|
||||
`cargo audit` (JSON store → no new deps expected). Field test on dopedart.
|
||||
|
||||
**Local hardening DONE 2026-06-27:** inbound friend-presence replies are now
|
||||
rate-limited per authenticated friend id (`PresenceRateLimiter`: burst 4, refill
|
||||
1/15s) and wired into the live friends listener before it builds a `Pong`; denied
|
||||
probes get the same silent no-data close as unauthorized probes. Existing
|
||||
defensive reply handling still validates room tickets against the authenticated
|
||||
friend id and never auto-joins. Verified with `cargo test presence`,
|
||||
`cargo test --lib`, `cargo clippy --all-targets -- -D warnings`, and
|
||||
`cargo audit --no-fetch --stale` (local DB; reports only the two already-allowed
|
||||
unmaintained advisories in `deny.toml`). A fresh advisory fetch was blocked in
|
||||
this sandbox by network restrictions.
|
||||
|
||||
**Remaining:** live 2-machine field test on dopedart, a fresh online
|
||||
`cargo audit`, and any follow-up findings from that test.
|
||||
|
||||
## The connect flow (the user's scenario, end to end)
|
||||
1. Friend X, at a coffee shop, opens peerspeak and starts a gathering labeled
|
||||
"HangOut."
|
||||
|
||||
+1
-1
@@ -1,7 +1,7 @@
|
||||
# Maintainer: mollusk <jitty+lc1iz0dc@protonmail.com>
|
||||
pkgname=peerspeak-git
|
||||
_pkgname=peerspeak
|
||||
pkgver=0.3.0.r229.g7fb1c96
|
||||
pkgver=0.4.0.r254.g913b0b6
|
||||
pkgrel=1
|
||||
pkgdesc="Decentralized peer-to-peer voice chat (Rust/iroh/PipeWire/Opus/iced)"
|
||||
arch=('x86_64')
|
||||
|
||||
@@ -8,7 +8,7 @@ it once, then you and I connect directly to each other.
|
||||
|
||||
## 1. Install it
|
||||
|
||||
1. Double-click **`peerspeak-0.3.0-setup.exe`** (the file I sent you).
|
||||
1. Double-click **`peerspeak-0.4.0-setup.exe`** (the file I sent you).
|
||||
|
||||
2. **Windows will probably show a blue "Windows protected your PC" warning.**
|
||||
This is normal — it shows up for any app that isn't from a big company with a
|
||||
|
||||
@@ -12,7 +12,7 @@ runtime, so there are no extra DLLs to bundle. The installer payload is just the
|
||||
## Version compatibility
|
||||
|
||||
The installer version tracks the crate version in `Cargo.toml` (currently
|
||||
**0.3.0**) — keep `MyAppVersion` in `peerspeak.iss` in sync when it changes.
|
||||
**0.4.0**) — keep `MyAppVersion` in `peerspeak.iss` in sync when it changes.
|
||||
|
||||
Per `VERSIONING.md`, a **MINOR** bump in `0.x` is a **breaking wire change**:
|
||||
peers on different MINOR versions can't connect (they fail fast at the
|
||||
|
||||
@@ -12,7 +12,7 @@
|
||||
; (x86_64-pc-windows-gnu, statically linked -- no extra DLLs needed).
|
||||
|
||||
#define MyAppName "PeerSpeak"
|
||||
#define MyAppVersion "0.3.0"
|
||||
#define MyAppVersion "0.4.0"
|
||||
#define MyAppPublisher "mollusk"
|
||||
#define MyAppExeName "peerspeak.exe"
|
||||
|
||||
|
||||
+871
-136
File diff suppressed because it is too large
Load Diff
+119
@@ -185,10 +185,129 @@ pub fn initials(name: &str) -> String {
|
||||
}
|
||||
}
|
||||
|
||||
/// A small content-addressed LRU cache mapping image bytes to a built value
|
||||
/// (e.g. an `iced` image handle), so the SAME value is reused across redraws
|
||||
/// instead of rebuilt every frame. Two Tier C F-03 properties beyond a plain
|
||||
/// hash map:
|
||||
///
|
||||
/// 1. **Bounded** — at most `cap` entries, evicting the least-recently-used on
|
||||
/// overflow, so a peer can't grow the cache without limit by publishing an
|
||||
/// endless stream of distinct valid avatars.
|
||||
/// 2. **Collision-safe** — a hit requires full byte equality, not just a matching
|
||||
/// 64-bit hash, so a hash collision can never return a different image's value.
|
||||
///
|
||||
/// Linear scan; intended for small `cap` (tens of entries).
|
||||
pub struct ByteLru<V> {
|
||||
cap: usize,
|
||||
/// `(content hash, content bytes, value)`; back = most recently used.
|
||||
entries: Vec<(u64, Vec<u8>, V)>,
|
||||
}
|
||||
|
||||
impl<V: Clone> ByteLru<V> {
|
||||
/// Create an LRU holding at most `cap` entries (`cap` is clamped to >= 1).
|
||||
pub fn new(cap: usize) -> Self {
|
||||
Self { cap: cap.max(1), entries: Vec::new() }
|
||||
}
|
||||
|
||||
/// Return the cached value for these exact `bytes`, building and inserting it
|
||||
/// on a miss (evicting the least-recently-used entry once over `cap`). A hit
|
||||
/// verifies full byte equality, so a 64-bit hash collision never returns the
|
||||
/// wrong value. A hit also refreshes the entry's recency.
|
||||
pub fn get_or_insert(&mut self, bytes: &[u8], build: impl FnOnce() -> V) -> V {
|
||||
use std::hash::{Hash, Hasher};
|
||||
let mut hasher = std::collections::hash_map::DefaultHasher::new();
|
||||
bytes.hash(&mut hasher);
|
||||
self.get_or_insert_hashed(hasher.finish(), bytes, build)
|
||||
}
|
||||
|
||||
/// Inner seam with the content `hash` supplied explicitly. Production callers
|
||||
/// use [`get_or_insert`]; tests use this to force a hash collision (different
|
||||
/// bytes, same hash) and exercise the byte-equality guard.
|
||||
fn get_or_insert_hashed(&mut self, hash: u64, bytes: &[u8], build: impl FnOnce() -> V) -> V {
|
||||
if let Some(idx) = self
|
||||
.entries
|
||||
.iter()
|
||||
.position(|(h, b, _)| *h == hash && b.as_slice() == bytes)
|
||||
{
|
||||
// LRU touch: move the hit entry to the back (most recent).
|
||||
let entry = self.entries.remove(idx);
|
||||
let val = entry.2.clone();
|
||||
self.entries.push(entry);
|
||||
return val;
|
||||
}
|
||||
|
||||
let val = build();
|
||||
if self.entries.len() >= self.cap {
|
||||
self.entries.remove(0); // evict least-recently-used
|
||||
}
|
||||
self.entries.push((hash, bytes.to_vec(), val.clone()));
|
||||
val
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
fn len(&self) -> usize {
|
||||
self.entries.len()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn byte_lru_reuses_value_for_identical_bytes() {
|
||||
let mut lru: ByteLru<u32> = ByteLru::new(4);
|
||||
let mut next = 0u32;
|
||||
let mut build = |lru: &mut ByteLru<u32>, b: &[u8]| {
|
||||
lru.get_or_insert(b, || {
|
||||
next += 1;
|
||||
next
|
||||
})
|
||||
};
|
||||
// Same bytes → same value, built only once.
|
||||
assert_eq!(build(&mut lru, b"alice"), 1);
|
||||
assert_eq!(build(&mut lru, b"alice"), 1);
|
||||
// Different bytes → a freshly built value.
|
||||
assert_eq!(build(&mut lru, b"bob"), 2);
|
||||
assert_eq!(lru.len(), 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn byte_lru_evicts_least_recently_used() {
|
||||
let mut lru: ByteLru<u32> = ByteLru::new(2);
|
||||
let mut n = 0u32;
|
||||
let mut ins = |lru: &mut ByteLru<u32>, b: &[u8]| {
|
||||
lru.get_or_insert(b, || {
|
||||
n += 1;
|
||||
n
|
||||
})
|
||||
};
|
||||
ins(&mut lru, b"a"); // -> 1
|
||||
ins(&mut lru, b"b"); // -> 2, cache = [a, b]
|
||||
ins(&mut lru, b"a"); // touch a, cache = [b, a]
|
||||
ins(&mut lru, b"c"); // evicts LRU (b), cache = [a, c]
|
||||
assert_eq!(lru.len(), 2);
|
||||
// `a` survived (recently touched) → still value 1, not rebuilt.
|
||||
assert_eq!(ins(&mut lru, b"a"), 1);
|
||||
// `b` was evicted → rebuilt with a new value.
|
||||
assert_eq!(ins(&mut lru, b"b"), 4);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn byte_lru_byte_equality_survives_a_hash_collision() {
|
||||
// Force the SAME 64-bit hash for two DIFFERENT byte strings (the case a
|
||||
// bare-hash cache would alias — Tier C F-03 collision bug).
|
||||
let mut lru: ByteLru<u32> = ByteLru::new(4);
|
||||
assert_eq!(lru.get_or_insert_hashed(42, b"alice", || 1), 1);
|
||||
// `bob` collides on the hash but differs in bytes → a MISS, built fresh,
|
||||
// NOT aliased to alice's value.
|
||||
assert_eq!(lru.get_or_insert_hashed(42, b"bob", || 2), 2);
|
||||
// Both coexist; each re-lookup returns its own value (build closure unused).
|
||||
assert_eq!(lru.get_or_insert_hashed(42, b"alice", || 99), 1);
|
||||
assert_eq!(lru.get_or_insert_hashed(42, b"bob", || 99), 2);
|
||||
assert_eq!(lru.len(), 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn initials_takes_first_two_words() {
|
||||
assert_eq!(initials("Alice"), "A");
|
||||
|
||||
+26
-5
@@ -64,9 +64,16 @@ pub enum CoreCommand {
|
||||
/// Set the pixelpass binary location (config override, empty = use `$PATH`).
|
||||
/// Sent at startup so screen-share can resolve the binary.
|
||||
SetPixelpassPath(Option<String>),
|
||||
/// Enumerate apps currently producing audio (for the screen-share audio
|
||||
/// picker, A23). Replies with [`UiEvent::AudioAppsListed`]. Cheap shell-out;
|
||||
/// safe to call each time the picker opens.
|
||||
ListAudioApps,
|
||||
/// Start sharing our screen: spawn a pixelpass host and announce its ticket
|
||||
/// on our presence so the room can watch. No-op when not in a call.
|
||||
StartScreenShare,
|
||||
/// `audio_app` selects which app's audio to capture: `Some(name)` captures
|
||||
/// only that app (avoiding the call-loopback echo, A23); `None` shares the
|
||||
/// whole desktop audio (the legacy behavior).
|
||||
StartScreenShare { audio_app: Option<String> },
|
||||
/// Stop sharing our screen: kill the pixelpass host and clear the presence
|
||||
/// ticket. No-op when not sharing.
|
||||
StopScreenShare,
|
||||
@@ -133,15 +140,29 @@ pub enum UiEvent {
|
||||
/// string, used to key their avatar (W4).
|
||||
ChatMessage { from: String, name: String, text: String, attachment: Option<crate::files::ChatAttachment> },
|
||||
/// An attachment's bytes are now available (auto-fetched for images, or
|
||||
/// fetched on demand for files). Keyed by attachment id so the UI can match
|
||||
/// it to the chat entry.
|
||||
AttachmentReady { id: crate::files::AttachmentId, data: Vec<u8> },
|
||||
/// fetched on demand for files). Keyed by `(from, id)`: the id is
|
||||
/// attacker-chosen, so a malicious peer can reuse a victim's id — the author
|
||||
/// disambiguates whose bytes these are and stops content aliasing (Tier C
|
||||
/// F-12).
|
||||
AttachmentReady { from: EndpointId, id: crate::files::AttachmentId, data: Vec<u8> },
|
||||
/// An attachment fetch failed (sender gone, too large, decode error, etc.).
|
||||
AttachmentFailed { id: crate::files::AttachmentId, error: String },
|
||||
AttachmentFailed { from: EndpointId, id: crate::files::AttachmentId, error: String },
|
||||
/// The apps currently producing audio, for the screen-share audio picker
|
||||
/// (A23). Sorted, deduplicated `application.name`s; empty when nothing is
|
||||
/// playing or enumeration isn't available. `app_audio_supported` reports
|
||||
/// whether the resolved pixelpass understands `--strict-audio`: when `false`
|
||||
/// (an older pixelpass) the picker must offer whole-desktop audio only, since
|
||||
/// a per-app share would pass a flag that older binary rejects (audit P2).
|
||||
AudioAppsListed { apps: Vec<String>, app_audio_supported: bool },
|
||||
/// Our own screen share started; the UI flips the Share button to "Stop".
|
||||
ScreenShareStarted,
|
||||
/// Our own screen share stopped (or failed to start).
|
||||
ScreenShareStopped,
|
||||
/// Per-app screen-share audio routing state (A23). `true` = the app we chose
|
||||
/// is now reaching viewers; `false` = its audio stopped, so under our strict
|
||||
/// run viewers currently hear silence. The UI shows a transient warning while
|
||||
/// `false`. Only meaningful while sharing a specific app (not whole-desktop).
|
||||
ShareAudioActive(bool),
|
||||
/// Our node identity (W7): the current node id string, and whether it is
|
||||
/// PERSISTED to disk. Sent once at startup and again after a regenerate.
|
||||
/// `persisted = false` means the key file couldn't be read/written and we're
|
||||
|
||||
+278
-37
@@ -134,6 +134,25 @@ type SeenConnected = Arc<std::sync::Mutex<HashSet<EndpointId>>>;
|
||||
type KnownPeers =
|
||||
Arc<std::sync::Mutex<HashMap<[u8; 32], HashMap<EndpointId, EndpointAddr>>>>;
|
||||
|
||||
/// Per-topic cap on the retained rejoin-bootstrap / recovery target table
|
||||
/// (Tier C recovery-identity cap). Set comfortably above the live-roster cap
|
||||
/// (`gossip::MAX_ACTIVE_PEERS`, 32) so a legitimate room — even one where every
|
||||
/// member drops at once during a relay outage — never hits it, while an insider
|
||||
/// who grace-cycles distinct identities (join, drop without a signed Leave,
|
||||
/// repeat) cannot grow the table without bound. Combined with the recovery
|
||||
/// terminal budget (which forgets a retained address when it gives up), abandoned
|
||||
/// identities drain on their own, so this cap is a deterministic ceiling rather
|
||||
/// than a pinnable slot pool.
|
||||
const MAX_RETAINED_PEERS: usize = 64;
|
||||
|
||||
/// Whether a peer may be inserted into a retained-target table at `len` entries.
|
||||
/// An update to an id already present is always allowed (it only refreshes an
|
||||
/// address); a brand-new id is admitted only while below the cap. Mirrors the
|
||||
/// gossip roster's `admit_into_roster` reject-when-full admission.
|
||||
fn admit_retained(len: usize, is_new_id: bool, cap: usize) -> bool {
|
||||
!is_new_id || len < cap
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
struct RecoveryContext {
|
||||
coordinator: RecoveryCoordinator,
|
||||
@@ -522,6 +541,7 @@ struct ActiveSession {
|
||||
event_task: tokio::task::JoinHandle<()>,
|
||||
conn_event_task: tokio::task::JoinHandle<()>,
|
||||
recovery_task: tokio::task::JoinHandle<()>,
|
||||
recovery_terminal_task: tokio::task::JoinHandle<()>,
|
||||
grace_timers: GraceTimers,
|
||||
transport: Arc<IrohTransport>,
|
||||
/// Loaded PipeWire echo-cancel module (if enabled); unloads on drop.
|
||||
@@ -557,6 +577,7 @@ impl ActiveSession {
|
||||
handle.abort();
|
||||
}
|
||||
self.recovery_task.abort();
|
||||
self.recovery_terminal_task.abort();
|
||||
crate::log_msg("Aborted tasks");
|
||||
|
||||
let audio_backend_clone = audio_backend.clone();
|
||||
@@ -744,25 +765,70 @@ async fn build_net_stack(
|
||||
})
|
||||
}
|
||||
|
||||
/// Maximum number of *automatic* chat-attachment fetches in flight at once.
|
||||
///
|
||||
/// Auto-fetch (inline image preview) is triggered by an untrusted peer's chat
|
||||
/// message, and each fetch is a detached task that can spend up to ~60s dialing
|
||||
/// and reading. Without a bound, a room insider could spam attachment-carrying
|
||||
/// chat to accumulate arbitrary pending tasks/dials (Tier C F-02). When the bound
|
||||
/// is reached we simply skip the auto-fetch; the descriptor still renders and the
|
||||
/// user can fetch it on demand (which is not rate-limited here).
|
||||
const MAX_INFLIGHT_ATTACHMENT_FETCHES: usize = 4;
|
||||
|
||||
/// In-flight `(author, attachment_id)` markers for bounded, deduplicated auto-
|
||||
/// fetches (Tier C F-02). Bounded by [`MAX_INFLIGHT_ATTACHMENT_FETCHES`].
|
||||
type InflightAttachments =
|
||||
Arc<std::sync::Mutex<HashSet<(EndpointId, crate::files::AttachmentId)>>>;
|
||||
|
||||
/// RAII bookkeeping for one bounded auto-fetch: holds the concurrency permit for
|
||||
/// the task's lifetime and clears the in-flight `(author, id)` marker when the
|
||||
/// fetch finishes (success OR failure), so the same image can be retried later.
|
||||
struct AutoFetchGuard {
|
||||
_permit: tokio::sync::OwnedSemaphorePermit,
|
||||
inflight: InflightAttachments,
|
||||
key: (EndpointId, crate::files::AttachmentId),
|
||||
}
|
||||
|
||||
impl Drop for AutoFetchGuard {
|
||||
fn drop(&mut self) {
|
||||
self.inflight.lock().unwrap().remove(&self.key);
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether to AUTO-fetch a chat image attachment. Only authenticated roster
|
||||
/// authors qualify (closing the non-roster injection vector), and a `(author,
|
||||
/// id)` already being fetched is skipped (dedup). The concurrency bound itself is
|
||||
/// enforced separately by the permit. Pure → unit-testable (Tier C F-02).
|
||||
fn should_auto_fetch(is_image: bool, author_in_roster: bool, already_inflight: bool) -> bool {
|
||||
is_image && author_in_roster && !already_inflight
|
||||
}
|
||||
|
||||
/// Fetch a chat attachment's bytes from `from` over the file plane in a detached
|
||||
/// task, then report the result to the UI via [`UiEvent::AttachmentReady`] /
|
||||
/// [`UiEvent::AttachmentFailed`] keyed by the attachment id. For images
|
||||
/// [`UiEvent::AttachmentFailed`], tagged with `from` so the UI keys the bytes by
|
||||
/// `(author, id)` and can't alias a same-id attachment from another sender. For images
|
||||
/// (`is_image`) the bytes are defensively re-validated (decodable + within pixel
|
||||
/// limits) before being handed to the renderer; an "Image" that doesn't decode is
|
||||
/// reported as a failure rather than rendered.
|
||||
/// reported as a failure rather than rendered. `guard` is `Some` for bounded
|
||||
/// auto-fetches and `None` for user-initiated fetches; it is dropped when the
|
||||
/// task ends, releasing the concurrency permit and the dedup marker.
|
||||
fn spawn_attachment_fetch(
|
||||
transport: Arc<IrohTransport>,
|
||||
ui_tx: mpsc::Sender<UiEvent>,
|
||||
from: EndpointId,
|
||||
att: crate::files::ChatAttachment,
|
||||
is_image: bool,
|
||||
guard: Option<AutoFetchGuard>,
|
||||
) {
|
||||
tokio::spawn(async move {
|
||||
// Held for the whole fetch; dropped here on completion (Tier C F-02).
|
||||
let _guard = guard;
|
||||
match transport.fetch_attachment(from, &att).await {
|
||||
Ok(data) => {
|
||||
if is_image && crate::files::validate_image_bytes(&data).is_none() {
|
||||
let _ = ui_tx
|
||||
.send(UiEvent::AttachmentFailed {
|
||||
from,
|
||||
id: att.id,
|
||||
error: "received image failed to decode".to_string(),
|
||||
})
|
||||
@@ -770,12 +836,12 @@ fn spawn_attachment_fetch(
|
||||
return;
|
||||
}
|
||||
let _ = ui_tx
|
||||
.send(UiEvent::AttachmentReady { id: att.id, data })
|
||||
.send(UiEvent::AttachmentReady { from, id: att.id, data })
|
||||
.await;
|
||||
}
|
||||
Err(e) => {
|
||||
let _ = ui_tx
|
||||
.send(UiEvent::AttachmentFailed { id: att.id, error: e.to_string() })
|
||||
.send(UiEvent::AttachmentFailed { from, id: att.id, error: e.to_string() })
|
||||
.await;
|
||||
}
|
||||
}
|
||||
@@ -1008,22 +1074,34 @@ async fn run_core_loop(
|
||||
// Join, cleared on Leave.
|
||||
let current_room: Arc<std::sync::Mutex<Option<crate::presence::RoomPresence>>> =
|
||||
Arc::new(std::sync::Mutex::new(None));
|
||||
let presence_rate_limiter =
|
||||
Arc::new(std::sync::Mutex::new(crate::presence::PresenceRateLimiter::default()));
|
||||
|
||||
// Reply policy for the idle friends listener (B2): answer friends only, never
|
||||
// while invisible (`should_answer`), and report our current gathering so a friend
|
||||
// can one-click join. Reads the shared snapshots, so it stays correct as they
|
||||
// change and survives a network-stack rebuild. Pure-sync (no awaits, no lock held
|
||||
// across one). Built once and handed to every `build_net_stack`.
|
||||
// can one-click join. Rate-limits allowed friends before building a reply, so a
|
||||
// spammy saved peer gets the same silent close as an unauthorized peer. Reads the
|
||||
// shared snapshots, so it stays correct as they change and survives a network-stack
|
||||
// rebuild. Pure-sync (no awaits, no lock held across one). Built once and handed
|
||||
// to every `build_net_stack`.
|
||||
let friends_handler: crate::presence_net::Handler = {
|
||||
let friends = friends.clone();
|
||||
let presence_mode = presence_mode.clone();
|
||||
let current_room = current_room.clone();
|
||||
let presence_rate_limiter = presence_rate_limiter.clone();
|
||||
Arc::new(move |from| {
|
||||
let mode = *presence_mode.lock().unwrap();
|
||||
let allowed = crate::presence::should_answer(&from, &friends.lock().unwrap(), mode);
|
||||
if !allowed {
|
||||
return None;
|
||||
}
|
||||
if !presence_rate_limiter
|
||||
.lock()
|
||||
.unwrap()
|
||||
.allow(from, std::time::Instant::now())
|
||||
{
|
||||
return None;
|
||||
}
|
||||
let room = current_room.lock().unwrap().clone();
|
||||
Some(crate::presence::ControlMsg::Pong { room })
|
||||
})
|
||||
@@ -1799,7 +1877,7 @@ async fn run_core_loop(
|
||||
// The topic of the room this event loop serves, so peer add/remove
|
||||
// updates the right per-topic bucket in `known_peers` (A8 archive).
|
||||
let room_topic = topic_id;
|
||||
let (recovery_coordinator, recovery_task) =
|
||||
let (recovery_coordinator, recovery_task, recovery_terminal_rx) =
|
||||
RecoveryCoordinator::spawn(room_state.clone());
|
||||
let recovery_context = RecoveryContext {
|
||||
coordinator: recovery_coordinator,
|
||||
@@ -1808,17 +1886,51 @@ async fn run_core_loop(
|
||||
topic_id,
|
||||
};
|
||||
let recovery_events = recovery_context.clone();
|
||||
// Drain the recovery coordinator's terminal-eviction signals (Tier C
|
||||
// recovery-identity cap). When background recovery exhausts its budget
|
||||
// for a peer, forget its retained dial target so the per-topic retain
|
||||
// table drains, scrub residual seen-connected state, and surface the
|
||||
// failure. A peer that later returns can still rejoin via a gossip
|
||||
// announce, so giving up never blocks a legitimate reconnect.
|
||||
let recovery_terminal_ctx = recovery_context.clone();
|
||||
let seen_connected_terminal = seen_connected.clone();
|
||||
let ui_tx_terminal = ui_tx.clone();
|
||||
let recovery_terminal_task = tokio::spawn(async move {
|
||||
let mut terminal_rx = recovery_terminal_rx;
|
||||
while let Some(peer_id) = terminal_rx.recv().await {
|
||||
crate::log_msg(&format!(
|
||||
"Background recovery gave up on peer {peer_id:?}; forgetting retained target"
|
||||
));
|
||||
recovery_terminal_ctx.forget(peer_id);
|
||||
seen_connected_terminal.lock().unwrap().remove(&peer_id);
|
||||
let _ = ui_tx_terminal
|
||||
.send(UiEvent::PeerConnectionFailed { id: peer_id })
|
||||
.await;
|
||||
}
|
||||
});
|
||||
// Friends store + ui sender, so a connected peer who is a friend has
|
||||
// their saved address auto-healed (W7) — populates `last_addr` so the
|
||||
// presence scheduler can reach them later.
|
||||
let friends_events = friends.clone();
|
||||
let friends_read_only_events = friends_read_only;
|
||||
// Bounded, deduplicated auto-fetch of chat image attachments (Tier C
|
||||
// F-02): the permit pool caps concurrent fetch tasks; the in-flight
|
||||
// set dedups identical (author, id) pairs.
|
||||
let attachment_limiter =
|
||||
Arc::new(tokio::sync::Semaphore::new(MAX_INFLIGHT_ATTACHMENT_FETCHES));
|
||||
let inflight_attachments: InflightAttachments =
|
||||
Arc::new(std::sync::Mutex::new(HashSet::new()));
|
||||
let event_task = tokio::spawn(async move {
|
||||
// The authenticated roster for this room, maintained from the
|
||||
// same sequential event stream. Only its members may trigger an
|
||||
// automatic attachment fetch (Tier C F-02).
|
||||
let mut roster: HashSet<EndpointId> = HashSet::new();
|
||||
while let Some(event) = room_events.recv().await {
|
||||
match event {
|
||||
RoomEvent::PeerJoined(peer_id, state) => {
|
||||
// A (re)join means the peer is back — cancel any
|
||||
// pending reconnect grace timer before re-adding it.
|
||||
roster.insert(peer_id);
|
||||
cancel_grace_timer(&grace_timers_events, &peer_id);
|
||||
recovery_events.cancel(peer_id);
|
||||
transport_events.admit_audio_sender(peer_id);
|
||||
@@ -1843,13 +1955,22 @@ async fn run_core_loop(
|
||||
.await;
|
||||
}
|
||||
// Retain this peer under this room's topic as a
|
||||
// future rejoin bootstrap target (A8).
|
||||
known_peers_events
|
||||
.lock()
|
||||
.unwrap()
|
||||
.entry(room_topic)
|
||||
.or_default()
|
||||
.insert(peer_id, state.addr.clone());
|
||||
// future rejoin bootstrap target (A8), bounded by the
|
||||
// per-topic retain cap (Tier C recovery-identity cap):
|
||||
// refreshing a peer we already track is always allowed,
|
||||
// a brand-new identity only while below the cap.
|
||||
{
|
||||
let mut kp = known_peers_events.lock().unwrap();
|
||||
let bucket = kp.entry(room_topic).or_default();
|
||||
let is_new_id = !bucket.contains_key(&peer_id);
|
||||
if admit_retained(bucket.len(), is_new_id, MAX_RETAINED_PEERS) {
|
||||
bucket.insert(peer_id, state.addr.clone());
|
||||
} else {
|
||||
crate::log_msg(&format!(
|
||||
"Retain table full ({MAX_RETAINED_PEERS}); not retaining new peer {peer_id:?} for rejoin"
|
||||
));
|
||||
}
|
||||
}
|
||||
// If a multitrack recording is live, give this peer
|
||||
// its own stem track (silence-padded back to t=0).
|
||||
if is_multitrack_events.load(Ordering::Relaxed)
|
||||
@@ -1862,6 +1983,7 @@ async fn run_core_loop(
|
||||
}
|
||||
RoomEvent::PeerLeft(peer_id) => {
|
||||
// Graceful leave — evict immediately.
|
||||
roster.remove(&peer_id);
|
||||
cancel_grace_timer(&grace_timers_events, &peer_id);
|
||||
seen_connected_events.lock().unwrap().remove(&peer_id);
|
||||
// A signed Leave cancels background recovery and
|
||||
@@ -1899,13 +2021,21 @@ async fn run_core_loop(
|
||||
.await;
|
||||
}
|
||||
// Refresh this room's retained rejoin target with the
|
||||
// fresh addr (A8).
|
||||
known_peers_events
|
||||
.lock()
|
||||
.unwrap()
|
||||
.entry(room_topic)
|
||||
.or_default()
|
||||
.insert(peer_id, state.addr.clone());
|
||||
// fresh addr (A8), under the per-topic retain cap. A
|
||||
// re-announce from a peer we already track always
|
||||
// refreshes; a new identity is bounded by the cap.
|
||||
{
|
||||
let mut kp = known_peers_events.lock().unwrap();
|
||||
let bucket = kp.entry(room_topic).or_default();
|
||||
let is_new_id = !bucket.contains_key(&peer_id);
|
||||
if admit_retained(bucket.len(), is_new_id, MAX_RETAINED_PEERS) {
|
||||
bucket.insert(peer_id, state.addr.clone());
|
||||
} else {
|
||||
crate::log_msg(&format!(
|
||||
"Retain table full ({MAX_RETAINED_PEERS}); not retaining new peer {peer_id:?} for rejoin"
|
||||
));
|
||||
}
|
||||
}
|
||||
let _ = ui_tx_events.send(UiEvent::PeerUpdated { id: peer_id, state }).await;
|
||||
}
|
||||
RoomEvent::ChatMessage { from, name, text, ts: _, attachment } => {
|
||||
@@ -1913,16 +2043,47 @@ async fn run_core_loop(
|
||||
// without a click; non-image files wait for an explicit
|
||||
// FetchAttachment (the "Save" chip). The descriptor was
|
||||
// already filename-sanitized + size-capped on ingest.
|
||||
if let Some(att) = attachment.clone()
|
||||
&& att.kind == crate::files::AttachmentKind::Image
|
||||
{
|
||||
spawn_attachment_fetch(
|
||||
transport_events.clone(),
|
||||
ui_tx_events.clone(),
|
||||
from,
|
||||
att,
|
||||
true,
|
||||
);
|
||||
//
|
||||
// The auto path is an untrusted-peer-triggered detached
|
||||
// task, so it is gated (Tier C F-02): only roster authors
|
||||
// qualify, identical (author,id) pairs are deduped, and a
|
||||
// permit pool caps concurrent fetch tasks. The chat TEXT
|
||||
// is always forwarded (it's sanitized at the UI edge);
|
||||
// only the fetch is bounded.
|
||||
if let Some(att) = attachment.clone() {
|
||||
let is_image = att.kind == crate::files::AttachmentKind::Image;
|
||||
let key = (from, att.id);
|
||||
let already_inflight =
|
||||
inflight_attachments.lock().unwrap().contains(&key);
|
||||
if should_auto_fetch(is_image, roster.contains(&from), already_inflight) {
|
||||
// Reserve the dedup slot, then a permit. If the
|
||||
// pool is exhausted, drop the auto-fetch (and the
|
||||
// dedup marker) — the descriptor still shows and
|
||||
// the user can fetch on demand.
|
||||
inflight_attachments.lock().unwrap().insert(key);
|
||||
match attachment_limiter.clone().try_acquire_owned() {
|
||||
Ok(permit) => {
|
||||
spawn_attachment_fetch(
|
||||
transport_events.clone(),
|
||||
ui_tx_events.clone(),
|
||||
from,
|
||||
att,
|
||||
true,
|
||||
Some(AutoFetchGuard {
|
||||
_permit: permit,
|
||||
inflight: inflight_attachments.clone(),
|
||||
key,
|
||||
}),
|
||||
);
|
||||
}
|
||||
Err(_) => {
|
||||
inflight_attachments.lock().unwrap().remove(&key);
|
||||
crate::log_msg(
|
||||
"Chat attachment auto-fetch limit reached; skipping (fetch on demand)",
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
let _ = ui_tx_events.send(UiEvent::ChatMessage {
|
||||
from: from.to_string(),
|
||||
@@ -1992,6 +2153,7 @@ async fn run_core_loop(
|
||||
event_task,
|
||||
conn_event_task,
|
||||
recovery_task,
|
||||
recovery_terminal_task,
|
||||
grace_timers,
|
||||
transport: transport.clone(),
|
||||
#[cfg(target_os = "linux")]
|
||||
@@ -2467,12 +2629,15 @@ async fn run_core_loop(
|
||||
CoreCommand::FetchAttachment { from, attachment } => {
|
||||
if let Some(session) = &active_session {
|
||||
let is_image = attachment.kind == crate::files::AttachmentKind::Image;
|
||||
// User-initiated (the "Save" chip): not bounded here — a human
|
||||
// click rate-limits it. The auto path (F-02) passes a guard.
|
||||
spawn_attachment_fetch(
|
||||
session.transport.clone(),
|
||||
ui_tx.clone(),
|
||||
from,
|
||||
attachment,
|
||||
is_image,
|
||||
None,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -2481,7 +2646,29 @@ async fn run_core_loop(
|
||||
pixelpass_override = path.filter(|p| !p.trim().is_empty());
|
||||
}
|
||||
|
||||
CoreCommand::StartScreenShare => {
|
||||
CoreCommand::ListAudioApps => {
|
||||
// Probe whether this pixelpass supports `--strict-audio` before
|
||||
// offering per-app capture: an older binary would reject the flag
|
||||
// and hard-fail the share (audit P2). When unsupported (or
|
||||
// pixelpass is missing), skip enumeration and let the picker show
|
||||
// whole-desktop audio only — never a best-effort `--app` that
|
||||
// would reopen the A23 echo.
|
||||
let app_audio_supported =
|
||||
match crate::screenshare::pixelpass_path(pixelpass_override.as_deref()) {
|
||||
Some(bin) => crate::screenshare::supports_strict_audio(&bin).await,
|
||||
None => false,
|
||||
};
|
||||
let apps = if app_audio_supported {
|
||||
crate::screenshare::list_audio_apps().await
|
||||
} else {
|
||||
Vec::new()
|
||||
};
|
||||
let _ = ui_tx
|
||||
.send(UiEvent::AudioAppsListed { apps, app_audio_supported })
|
||||
.await;
|
||||
}
|
||||
|
||||
CoreCommand::StartScreenShare { audio_app } => {
|
||||
let Some(session) = &mut active_session else {
|
||||
let _ = ui_tx
|
||||
.send(UiEvent::Error("Join a call before sharing your screen".into()))
|
||||
@@ -2502,7 +2689,33 @@ async fn run_core_loop(
|
||||
continue;
|
||||
}
|
||||
};
|
||||
match crate::screenshare::spawn_host(&bin).await {
|
||||
// Forward pixelpass `app_audio` events (only emitted when an app
|
||||
// is selected) to the UI so it can warn when the chosen app's
|
||||
// audio drops. The channel closes when the host dies (drain hits
|
||||
// EOF), ending the forwarder task on its own.
|
||||
let notices = audio_app.as_deref().map(|_| {
|
||||
let (tx, mut rx) =
|
||||
tokio::sync::mpsc::unbounded_channel::<crate::screenshare::PixelpassEvent>();
|
||||
let ui_tx_notices = ui_tx.clone();
|
||||
tokio::spawn(async move {
|
||||
while let Some(ev) = rx.recv().await {
|
||||
let active = match ev {
|
||||
crate::screenshare::PixelpassEvent::AppAudioRouted => true,
|
||||
crate::screenshare::PixelpassEvent::AppAudioLost => false,
|
||||
_ => continue,
|
||||
};
|
||||
if ui_tx_notices
|
||||
.send(UiEvent::ShareAudioActive(active))
|
||||
.await
|
||||
.is_err()
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
});
|
||||
tx
|
||||
});
|
||||
match crate::screenshare::spawn_host(&bin, audio_app.as_deref(), notices).await {
|
||||
Ok((child, ticket)) => {
|
||||
crate::log_msg("Screen share host started");
|
||||
session.screenshare_host = Some(child);
|
||||
@@ -2575,11 +2788,39 @@ async fn run_core_loop(
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::{
|
||||
apply_volume, audio_datagram_len_ok, frame_level, mix_frames, mix_stereo_frames,
|
||||
next_game_change, stereo_to_mono, KnownPeers, MicLevelMeter, PeerSpeakTicket,
|
||||
MAX_OPUS_PAYLOAD, MIC_LEVEL_REPORT_SAMPLES,
|
||||
admit_retained, apply_volume, audio_datagram_len_ok, frame_level, mix_frames,
|
||||
mix_stereo_frames, next_game_change, should_auto_fetch, stereo_to_mono, KnownPeers,
|
||||
MicLevelMeter, PeerSpeakTicket, MAX_OPUS_PAYLOAD, MAX_RETAINED_PEERS,
|
||||
MIC_LEVEL_REPORT_SAMPLES,
|
||||
};
|
||||
|
||||
#[test]
|
||||
fn admit_retained_rejects_only_new_ids_at_the_cap() {
|
||||
// Below the cap, a brand-new identity is retained.
|
||||
assert!(admit_retained(0, true, MAX_RETAINED_PEERS));
|
||||
assert!(admit_retained(MAX_RETAINED_PEERS - 1, true, MAX_RETAINED_PEERS));
|
||||
// At the cap, a brand-new identity is refused — this is the bound that stops
|
||||
// an insider grace-cycling distinct identities from growing the retain table.
|
||||
assert!(!admit_retained(MAX_RETAINED_PEERS, true, MAX_RETAINED_PEERS));
|
||||
// A peer already tracked always refreshes, even at (or past) the cap: it only
|
||||
// updates an existing address and never adds a slot.
|
||||
assert!(admit_retained(MAX_RETAINED_PEERS, false, MAX_RETAINED_PEERS));
|
||||
assert!(admit_retained(MAX_RETAINED_PEERS + 5, false, MAX_RETAINED_PEERS));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn auto_fetch_only_for_roster_images_not_already_inflight() {
|
||||
// The happy path: a roster author's brand-new image attachment.
|
||||
assert!(should_auto_fetch(true, true, false));
|
||||
// A non-image (generic file) never auto-fetches — it waits for "Save".
|
||||
assert!(!should_auto_fetch(false, true, false));
|
||||
// A non-roster author (e.g. a sock puppet that never announced) is rejected,
|
||||
// closing the F-02 unbounded-task vector.
|
||||
assert!(!should_auto_fetch(true, false, false));
|
||||
// An identical (author,id) already being fetched is deduped.
|
||||
assert!(!should_auto_fetch(true, true, true));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn retained_peers_are_shared_by_topic_across_restamped_tickets() {
|
||||
let topic_id = [23u8; 32];
|
||||
|
||||
+68
-8
@@ -22,6 +22,27 @@ fn recovery_delay(attempt: usize) -> Duration {
|
||||
RECOVERY_DELAYS[attempt.min(RECOVERY_DELAYS.len() - 1)]
|
||||
}
|
||||
|
||||
/// Terminal retry budget for background recovery. After this many failed attempts
|
||||
/// the coordinator gives up: it drops the entry, frees the active slot, and signals
|
||||
/// the event task to forget the retained address (Tier C recovery-identity cap).
|
||||
///
|
||||
/// With the [`RECOVERY_DELAYS`] backoff this is roughly seven minutes of dialing
|
||||
/// (1+2+4+8+15+30+60s, then 60s steps), far beyond any normal transient outage. A
|
||||
/// genuine peer returning after a longer outage still rejoins on its own via a
|
||||
/// gossip announce, so giving up only stops us from dialing a peer that is not
|
||||
/// coming back — it does not break legitimate reconnect-after-outage.
|
||||
const RECOVERY_TERMINAL_ATTEMPTS: usize = 12;
|
||||
|
||||
/// Capacity of the terminal-eviction notification channel. Bounded; on the rare
|
||||
/// event of saturation the entry is still removed (the dial work stops) and only
|
||||
/// the retained-address forget is skipped, which the per-topic retain cap bounds.
|
||||
const RECOVERY_TERMINAL_CAPACITY: usize = 64;
|
||||
|
||||
/// Whether `attempt` completed recoveries have exhausted the terminal budget.
|
||||
fn recovery_is_terminal(attempt: usize, max_attempts: usize) -> bool {
|
||||
attempt >= max_attempts
|
||||
}
|
||||
|
||||
enum RecoveryCommand {
|
||||
Start {
|
||||
peer_id: EndpointId,
|
||||
@@ -60,19 +81,24 @@ pub(super) struct RecoveryCoordinator {
|
||||
}
|
||||
|
||||
impl RecoveryCoordinator {
|
||||
pub(super) fn spawn(room_state: Arc<IrohGossipState>) -> (Self, JoinHandle<()>) {
|
||||
pub(super) fn spawn(
|
||||
room_state: Arc<IrohGossipState>,
|
||||
) -> (Self, JoinHandle<()>, mpsc::Receiver<EndpointId>) {
|
||||
Self::spawn_inner(room_state)
|
||||
}
|
||||
|
||||
fn spawn_inner(room_state: Arc<dyn RecoveryRoom>) -> (Self, JoinHandle<()>) {
|
||||
fn spawn_inner(
|
||||
room_state: Arc<dyn RecoveryRoom>,
|
||||
) -> (Self, JoinHandle<()>, mpsc::Receiver<EndpointId>) {
|
||||
let (tx, rx) = mpsc::channel(RECOVERY_COMMAND_CAPACITY);
|
||||
let (terminal_tx, terminal_rx) = mpsc::channel(RECOVERY_TERMINAL_CAPACITY);
|
||||
let active = Arc::new(Mutex::new(HashSet::new()));
|
||||
let handle = Self {
|
||||
tx,
|
||||
active: active.clone(),
|
||||
};
|
||||
let task = tokio::spawn(run_coordinator(room_state, active, rx));
|
||||
(handle, task)
|
||||
let task = tokio::spawn(run_coordinator(room_state, active, rx, terminal_tx));
|
||||
(handle, task, terminal_rx)
|
||||
}
|
||||
|
||||
/// Reserve one recovery slot before grace-expiry teardown begins. Returns
|
||||
@@ -116,6 +142,7 @@ async fn run_coordinator(
|
||||
room_state: Arc<dyn RecoveryRoom>,
|
||||
active: Arc<Mutex<HashSet<EndpointId>>>,
|
||||
mut rx: mpsc::Receiver<RecoveryCommand>,
|
||||
terminal_tx: mpsc::Sender<EndpointId>,
|
||||
) {
|
||||
let mut entries: HashMap<EndpointId, RecoveryEntry> = HashMap::new();
|
||||
|
||||
@@ -155,9 +182,24 @@ async fn run_coordinator(
|
||||
entries.remove(&peer_id);
|
||||
continue;
|
||||
}
|
||||
if let Some(entry) = entries.get_mut(&peer_id) {
|
||||
// Advance the backoff, then check the terminal budget.
|
||||
// `attempt` counts completed attempts, so the delay
|
||||
// uses the current value before it is incremented.
|
||||
let terminal = if let Some(entry) = entries.get_mut(&peer_id) {
|
||||
entry.next_attempt = scheduled_at + recovery_delay(entry.attempt);
|
||||
entry.attempt = entry.attempt.saturating_add(1);
|
||||
recovery_is_terminal(entry.attempt, RECOVERY_TERMINAL_ATTEMPTS)
|
||||
} else {
|
||||
false
|
||||
};
|
||||
if terminal {
|
||||
// Give up on a peer that has not returned within the
|
||||
// budget: drop its entry, free the active slot, and
|
||||
// signal the event task to forget its retained
|
||||
// address so the per-topic retain table drains.
|
||||
entries.remove(&peer_id);
|
||||
active.lock().unwrap().remove(&peer_id);
|
||||
let _ = terminal_tx.try_send(peer_id);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -210,6 +252,23 @@ mod tests {
|
||||
assert_eq!(actual, vec![1, 2, 4, 8, 15, 30, 60, 60, 60, 60]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn recovery_budget_is_terminal_only_at_or_past_the_cap() {
|
||||
assert!(!recovery_is_terminal(0, RECOVERY_TERMINAL_ATTEMPTS));
|
||||
assert!(!recovery_is_terminal(
|
||||
RECOVERY_TERMINAL_ATTEMPTS - 1,
|
||||
RECOVERY_TERMINAL_ATTEMPTS
|
||||
));
|
||||
assert!(recovery_is_terminal(
|
||||
RECOVERY_TERMINAL_ATTEMPTS,
|
||||
RECOVERY_TERMINAL_ATTEMPTS
|
||||
));
|
||||
assert!(recovery_is_terminal(
|
||||
RECOVERY_TERMINAL_ATTEMPTS + 5,
|
||||
RECOVERY_TERMINAL_ATTEMPTS
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn recovery_slots_are_deduplicated_and_cancel_immediately() {
|
||||
let (tx, mut rx) = mpsc::channel(4);
|
||||
@@ -244,9 +303,10 @@ mod tests {
|
||||
#[tokio::test]
|
||||
async fn coordinator_attempts_rebootstrap_immediately() {
|
||||
let (attempts_tx, mut attempts_rx) = mpsc::unbounded_channel();
|
||||
let (coordinator, task) = RecoveryCoordinator::spawn_inner(Arc::new(RecordingRoom {
|
||||
attempts: attempts_tx,
|
||||
}));
|
||||
let (coordinator, task, _terminal_rx) =
|
||||
RecoveryCoordinator::spawn_inner(Arc::new(RecordingRoom {
|
||||
attempts: attempts_tx,
|
||||
}));
|
||||
let peer_id = SecretKey::generate().public();
|
||||
let addr = EndpointAddr::from(peer_id);
|
||||
|
||||
|
||||
+225
-9
@@ -1,11 +1,11 @@
|
||||
use crate::network::{RoomState, NetError, PeerState, RoomEvent, PeerSpeakTicket};
|
||||
use iroh::{Endpoint, EndpointAddr, EndpointId, SecretKey, Signature};
|
||||
use iroh::{Endpoint, EndpointAddr, EndpointId, SecretKey, Signature, TransportAddr};
|
||||
use iroh_gossip::net::Gossip;
|
||||
use iroh_gossip::proto::TopicId;
|
||||
use tokio::sync::mpsc;
|
||||
use tokio::sync::mpsc::Receiver;
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use std::collections::{BTreeSet, HashMap, HashSet};
|
||||
use async_trait::async_trait;
|
||||
use tokio_stream::StreamExt;
|
||||
use serde::{Serialize, Deserialize};
|
||||
@@ -131,6 +131,89 @@ fn admit_state_mutation(
|
||||
true
|
||||
}
|
||||
|
||||
/// Size at which we prune stale entries from the replay-tracking map (Tier C
|
||||
/// F-01 audit). `admit_state_mutation` records `(author, kind)` for every signed
|
||||
/// mutation, so an insider sending validly signed `Leave`s from unlimited
|
||||
/// generated keys would otherwise grow it for the room's lifetime. A mutation
|
||||
/// older than the freshness window can never be the deciding `last_ts` for an
|
||||
/// in-window message — `verify_gossip`'s timestamp check rejects such a replay
|
||||
/// first — so dropping those entries cannot weaken replay protection; it bounds
|
||||
/// the map to roughly the authors seen within one freshness window.
|
||||
const STATE_MUTATIONS_SOFT_CAP: usize = 256;
|
||||
|
||||
/// Drop replay-tracking entries whose timestamp is older than `window_ms` before
|
||||
/// `now_ms` (see [`STATE_MUTATIONS_SOFT_CAP`]). Pure → unit-testable.
|
||||
fn prune_stale_mutations(
|
||||
seen: &mut HashMap<(EndpointId, StateMutationKind), u64>,
|
||||
now_ms: u64,
|
||||
window_ms: u64,
|
||||
) {
|
||||
let floor = now_ms.saturating_sub(window_ms);
|
||||
seen.retain(|_, last_ts| *last_ts >= floor);
|
||||
}
|
||||
|
||||
/// Maximum number of distinct peers we hold in a room roster at once.
|
||||
///
|
||||
/// Everyone with the room ticket is an authenticated *insider*: a signature only
|
||||
/// proves ownership of the generated keypair it was made with, not that the
|
||||
/// author is a distinct human. A malicious member can therefore mint many valid
|
||||
/// signed identities. Voice is full-mesh (each peer dials every other), so a real
|
||||
/// room is realistically well under this bound; the cap exists purely so a flood
|
||||
/// of sock-puppet `Announce`s can't grow our peer map / audio supervisors / dials
|
||||
/// without limit (Tier C F-01).
|
||||
const MAX_ACTIVE_PEERS: usize = 32;
|
||||
|
||||
/// Maximum transport addresses we retain from a single peer announce. iroh
|
||||
/// normally advertises a handful (a few LAN/WAN IP candidates plus one home
|
||||
/// relay); the cap stops an insider stuffing a large unique address set into each
|
||||
/// announce to inflate the address lookup and the dialer's candidate list.
|
||||
const MAX_PEER_ADDRS: usize = 8;
|
||||
|
||||
/// Maximum byte length of a relay URL we accept inside a peer address. A relay
|
||||
/// URL is normal-length; anything longer is dropped rather than retained.
|
||||
const MAX_RELAY_URL_LEN: usize = 256;
|
||||
|
||||
/// Bound an untrusted peer's advertised address set before we retain it / hand it
|
||||
/// to the address lookup and dialer (Tier C F-01). Drops transport kinds we never
|
||||
/// use (`Custom`) and over-long relay URLs, then truncates to at most
|
||||
/// [`MAX_PEER_ADDRS`] addresses. `BTreeSet` iteration is deterministic, so the
|
||||
/// kept subset is stable. Pure → unit-testable.
|
||||
fn sanitize_endpoint_addr(addr: &EndpointAddr) -> EndpointAddr {
|
||||
let addrs: BTreeSet<TransportAddr> = addr
|
||||
.addrs
|
||||
.iter()
|
||||
.filter(|a| match a {
|
||||
TransportAddr::Relay(url) => url.as_str().len() <= MAX_RELAY_URL_LEN,
|
||||
TransportAddr::Ip(_) => true,
|
||||
// `TransportAddr` is #[non_exhaustive]; we only speak IP + relay, so
|
||||
// anything else (Custom / future kinds) is dropped, not retained.
|
||||
_ => false,
|
||||
})
|
||||
.take(MAX_PEER_ADDRS)
|
||||
.cloned()
|
||||
.collect();
|
||||
EndpointAddr { id: addr.id, addrs }
|
||||
}
|
||||
|
||||
/// Whether an `Announce` may enter the roster. Only a brand-new author
|
||||
/// (`subject_to_cap`) is gated by [`MAX_ACTIVE_PEERS`]; updates to an
|
||||
/// already-present peer AND re-announces from a peer mid-reconnect (which
|
||||
/// already held a slot) always pass — exempting reconnects keeps a full room
|
||||
/// from rejecting a legitimately reconnecting member and orphaning its recovery
|
||||
/// state (Tier C F-01 audit). Pure → unit-testable.
|
||||
fn admit_into_roster(roster_len: usize, subject_to_cap: bool, max_peers: usize) -> bool {
|
||||
!subject_to_cap || roster_len < max_peers
|
||||
}
|
||||
|
||||
/// Whether a received `Announce`'s author is gated by the roster cap. A peer
|
||||
/// already in the roster (`is_new == false`, an ordinary update) or one
|
||||
/// mid-reconnect (`is_reconnecting`, it already held a slot) is exempt; only a
|
||||
/// brand-new author counts against [`MAX_ACTIVE_PEERS`] (Tier C F-01 audit).
|
||||
/// Pure → unit-testable.
|
||||
fn announce_subject_to_cap(is_new: bool, is_reconnecting: bool) -> bool {
|
||||
is_new && !is_reconnecting
|
||||
}
|
||||
|
||||
fn peer_state_for_log(state: &PeerState) -> String {
|
||||
format!(
|
||||
"name={:?}, muted={}, addr_id={}, addrs={}, sharing={}, game={:?}",
|
||||
@@ -390,6 +473,18 @@ impl RoomState for IrohGossipState {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Keep the replay-tracking map bounded: prune entries
|
||||
// older than the freshness window once it grows past the
|
||||
// soft cap (Tier C F-01 audit). Stale entries can't gate
|
||||
// an in-window message, so this never weakens replay
|
||||
// protection.
|
||||
if state_mutations_seen.len() > STATE_MUTATIONS_SOFT_CAP {
|
||||
prune_stale_mutations(
|
||||
&mut state_mutations_seen,
|
||||
now_millis(),
|
||||
GOSSIP_FRESHNESS_MS,
|
||||
);
|
||||
}
|
||||
if !admit_state_mutation(
|
||||
&mut state_mutations_seen,
|
||||
payload.author,
|
||||
@@ -436,16 +531,49 @@ impl RoomState for IrohGossipState {
|
||||
let cleaned = crate::sanitize::sanitize_game_label(&g);
|
||||
(!cleaned.is_empty()).then_some(cleaned)
|
||||
});
|
||||
disconnected_peers.lock().unwrap().remove(&payload.author);
|
||||
let (is_new, state_changed) = {
|
||||
// Bound an insider's advertised address set
|
||||
// before we retain it / hand it to the dialer
|
||||
// (Tier C F-01).
|
||||
state.addr = sanitize_endpoint_addr(&state.addr);
|
||||
// A peer reconnecting from a transient drop sits
|
||||
// in `disconnected_peers` (not the live roster);
|
||||
// it already held a slot, so it must be re-admitted
|
||||
// regardless of the cap, and its disconnect marker
|
||||
// cleared ONLY once re-admitted — clearing it before
|
||||
// a possible reject would orphan its recovery state
|
||||
// (Tier C F-01 audit).
|
||||
let is_reconnecting =
|
||||
disconnected_peers.lock().unwrap().contains(&payload.author);
|
||||
let admitted = {
|
||||
let mut peer_map = peers.lock().unwrap();
|
||||
let is_new = !peer_map.contains_key(&payload.author);
|
||||
let state_changed = peer_map.get(&payload.author) != Some(&state);
|
||||
if is_new || state_changed {
|
||||
peer_map.insert(payload.author, state.clone());
|
||||
// Cap the roster so a flood of signed
|
||||
// sock-puppet identities can't grow our
|
||||
// memory/tasks/dials without bound (Tier C
|
||||
// F-01). Existing-peer updates and reconnects
|
||||
// are exempt; only brand-new authors are gated.
|
||||
let subject_to_cap = announce_subject_to_cap(is_new, is_reconnecting);
|
||||
if !admit_into_roster(peer_map.len(), subject_to_cap, MAX_ACTIVE_PEERS) {
|
||||
None
|
||||
} else {
|
||||
let state_changed = peer_map.get(&payload.author) != Some(&state);
|
||||
if is_new || state_changed {
|
||||
peer_map.insert(payload.author, state.clone());
|
||||
}
|
||||
Some((is_new, state_changed))
|
||||
}
|
||||
(is_new, state_changed)
|
||||
};
|
||||
let Some((is_new, state_changed)) = admitted else {
|
||||
crate::log_msg(&format!(
|
||||
"Gossip roster full ({MAX_ACTIVE_PEERS}); rejecting new peer {}",
|
||||
crate::short_id(&payload.author.to_string())
|
||||
));
|
||||
continue;
|
||||
};
|
||||
// Admitted — now it is safe to clear any reconnect
|
||||
// marker (a rejected announce above leaves it intact
|
||||
// so a later signed Leave still cleans up).
|
||||
disconnected_peers.lock().unwrap().remove(&payload.author);
|
||||
|
||||
if is_new {
|
||||
crate::log_msg(&format!(
|
||||
@@ -453,7 +581,12 @@ impl RoomState for IrohGossipState {
|
||||
crate::short_id(&payload.author.to_string()),
|
||||
peer_state_for_log(&state)
|
||||
));
|
||||
address_lookup.add_endpoint_info(state.addr.clone());
|
||||
// Replace (not union) the lookup's record for
|
||||
// this id with the authenticated, sanitized
|
||||
// address set, so leave/re-announce cycles
|
||||
// can't accumulate attacker-supplied history
|
||||
// (Tier C F-01).
|
||||
let _ = address_lookup.set_endpoint_info(state.addr.clone());
|
||||
let _ = event_tx.send(RoomEvent::PeerJoined(payload.author, state)).await;
|
||||
} else if state_changed {
|
||||
crate::log_msg(&format!(
|
||||
@@ -466,6 +599,11 @@ impl RoomState for IrohGossipState {
|
||||
}
|
||||
GossipMessage::Leave => {
|
||||
crate::log_msg(&format!("Gossip peer leave request from author={:?}", payload.author));
|
||||
// Drop this id's address-lookup entry so cycling
|
||||
// distinct identities through Announce→Leave can't
|
||||
// grow the lookup for the room's lifetime (Tier C
|
||||
// F-01 audit). Re-announce re-populates it.
|
||||
let _ = address_lookup.remove_endpoint_info(payload.author);
|
||||
let removed = peers.lock().unwrap().remove(&payload.author).is_some();
|
||||
let was_disconnected = disconnected_peers
|
||||
.lock()
|
||||
@@ -768,6 +906,84 @@ mod tests {
|
||||
assert!(!bootstrap.contains(&me));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn admit_into_roster_caps_new_authors_but_not_updates() {
|
||||
// New authors are admitted while there's room...
|
||||
assert!(admit_into_roster(0, true, 3));
|
||||
assert!(admit_into_roster(2, true, 3));
|
||||
// ...rejected once the roster is full...
|
||||
assert!(!admit_into_roster(3, true, 3));
|
||||
assert!(!admit_into_roster(10, true, 3));
|
||||
// ...but an existing peer's update always passes, even at/over the cap.
|
||||
assert!(admit_into_roster(3, false, 3));
|
||||
assert!(admit_into_roster(99, false, 3));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reconnecting_and_existing_peers_are_exempt_from_the_cap() {
|
||||
// A brand-new author counts against the cap...
|
||||
assert!(announce_subject_to_cap(/* is_new */ true, /* is_reconnecting */ false));
|
||||
// ...but an ordinary update from an in-roster peer does not...
|
||||
assert!(!announce_subject_to_cap(false, false));
|
||||
// ...and neither does a re-announce from a peer mid-reconnect, even
|
||||
// though it was removed from the live roster (the F-01-audit fix: a full
|
||||
// room must not reject a legitimately reconnecting member).
|
||||
assert!(!announce_subject_to_cap(true, true));
|
||||
// Combined with admit_into_roster: a reconnecting author passes at a full
|
||||
// roster, a brand-new one does not.
|
||||
assert!(admit_into_roster(3, announce_subject_to_cap(true, true), 3));
|
||||
assert!(!admit_into_roster(3, announce_subject_to_cap(true, false), 3));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn prune_stale_mutations_drops_only_out_of_window_entries() {
|
||||
let a = fresh_id();
|
||||
let b = fresh_id();
|
||||
let mut seen = HashMap::new();
|
||||
seen.insert((a, StateMutationKind::Announce), 10_000u64);
|
||||
seen.insert((b, StateMutationKind::Leave), 250_000u64);
|
||||
// now = 300_000, window = 120_000 → floor 180_000. The 10_000 entry is
|
||||
// stale (and could never gate an in-window message), the 250_000 is live.
|
||||
prune_stale_mutations(&mut seen, 300_000, GOSSIP_FRESHNESS_MS);
|
||||
assert_eq!(seen.len(), 1);
|
||||
assert!(seen.contains_key(&(b, StateMutationKind::Leave)));
|
||||
assert!(!seen.contains_key(&(a, StateMutationKind::Announce)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sanitize_endpoint_addr_caps_address_count() {
|
||||
use std::net::SocketAddr;
|
||||
let id = fresh_id();
|
||||
// An insider stuffs far more addresses than MAX_PEER_ADDRS into one announce.
|
||||
let many: Vec<TransportAddr> = (0..(MAX_PEER_ADDRS as u16 + 50))
|
||||
.map(|i| TransportAddr::Ip(SocketAddr::from(([127, 0, 0, 1], 1000 + i))))
|
||||
.collect();
|
||||
let addr = EndpointAddr::from_parts(id, many);
|
||||
let out = sanitize_endpoint_addr(&addr);
|
||||
assert_eq!(out.id, id);
|
||||
assert_eq!(out.addrs.len(), MAX_PEER_ADDRS);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sanitize_endpoint_addr_drops_overlong_relay_url() {
|
||||
use std::str::FromStr;
|
||||
let id = fresh_id();
|
||||
let short = iroh::RelayUrl::from_str("https://relay.example/").unwrap();
|
||||
let long = iroh::RelayUrl::from_str(&format!(
|
||||
"https://relay.example/{}",
|
||||
"a".repeat(MAX_RELAY_URL_LEN)
|
||||
))
|
||||
.unwrap();
|
||||
assert!(long.as_str().len() > MAX_RELAY_URL_LEN);
|
||||
let addr = EndpointAddr::from_parts(
|
||||
id,
|
||||
[TransportAddr::Relay(short.clone()), TransportAddr::Relay(long)],
|
||||
);
|
||||
let out = sanitize_endpoint_addr(&addr);
|
||||
let relays: Vec<_> = out.relay_urls().cloned().collect();
|
||||
assert_eq!(relays, vec![short], "over-long relay URL must be dropped");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_gossip_message_leave_round_trip() {
|
||||
let original = GossipMessage::Leave;
|
||||
|
||||
@@ -15,6 +15,16 @@
|
||||
use crate::friends::FriendStore;
|
||||
use iroh::EndpointId;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::collections::HashMap;
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
/// Maximum immediate presence replies to one friend before throttling. Normal
|
||||
/// presence polling is once per minute, so this only catches repeated/manual or
|
||||
/// abusive probes while still allowing a short burst after app startup.
|
||||
pub const PRESENCE_RATE_LIMIT_BURST: u32 = 4;
|
||||
|
||||
/// Refill one presence-reply token per friend at this cadence.
|
||||
pub const PRESENCE_RATE_LIMIT_REFILL: Duration = Duration::from_secs(15);
|
||||
|
||||
/// The user's presence posture — how reachable they are to friends while idle.
|
||||
/// Persisted in `AppConfig`; the default keeps you privately reachable to friends
|
||||
@@ -100,6 +110,48 @@ pub fn should_answer(from: &EndpointId, friends: &FriendStore, mode: PresenceMod
|
||||
mode.answers_pings() && friends.contains(from)
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
struct RateBucket {
|
||||
tokens: u32,
|
||||
last_refill: Instant,
|
||||
}
|
||||
|
||||
/// Per-friend limiter for inbound presence pings. It is intentionally keyed by
|
||||
/// the authenticated connection id, not payload data. Callers should only invoke
|
||||
/// it after [`should_answer`] passes, so strangers do not consume memory here.
|
||||
#[derive(Debug, Default, Clone)]
|
||||
pub struct PresenceRateLimiter {
|
||||
buckets: HashMap<EndpointId, RateBucket>,
|
||||
}
|
||||
|
||||
impl PresenceRateLimiter {
|
||||
/// Return whether `from` may receive a presence reply at `now`.
|
||||
///
|
||||
/// This is a token bucket: each friend starts with a small burst and regains
|
||||
/// one token every [`PRESENCE_RATE_LIMIT_REFILL`]. A denied probe should be
|
||||
/// answered with no data, matching the listener's "reveal nothing" policy.
|
||||
pub fn allow(&mut self, from: EndpointId, now: Instant) -> bool {
|
||||
let bucket = self.buckets.entry(from).or_insert(RateBucket {
|
||||
tokens: PRESENCE_RATE_LIMIT_BURST,
|
||||
last_refill: now,
|
||||
});
|
||||
|
||||
let elapsed = now.saturating_duration_since(bucket.last_refill);
|
||||
let refill = elapsed.as_secs() / PRESENCE_RATE_LIMIT_REFILL.as_secs();
|
||||
if refill > 0 {
|
||||
let refill = refill.min(u32::MAX as u64) as u32;
|
||||
bucket.tokens = PRESENCE_RATE_LIMIT_BURST.min(bucket.tokens.saturating_add(refill));
|
||||
bucket.last_refill = now;
|
||||
}
|
||||
|
||||
if bucket.tokens == 0 {
|
||||
return false;
|
||||
}
|
||||
bucket.tokens -= 1;
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
/// What we learned about a friend from a successful ping reply.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum FriendPresence {
|
||||
@@ -177,6 +229,36 @@ mod tests {
|
||||
assert!(!should_answer(&stranger, &friends, PresenceMode::Invisible));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn presence_rate_limiter_allows_a_small_burst_then_refills() {
|
||||
let mut limiter = PresenceRateLimiter::default();
|
||||
let friend = id();
|
||||
let now = Instant::now();
|
||||
|
||||
for _ in 0..PRESENCE_RATE_LIMIT_BURST {
|
||||
assert!(limiter.allow(friend, now));
|
||||
}
|
||||
assert!(!limiter.allow(friend, now));
|
||||
assert!(!limiter.allow(friend, now + PRESENCE_RATE_LIMIT_REFILL - Duration::from_millis(1)));
|
||||
|
||||
assert!(limiter.allow(friend, now + PRESENCE_RATE_LIMIT_REFILL));
|
||||
assert!(!limiter.allow(friend, now + PRESENCE_RATE_LIMIT_REFILL));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn presence_rate_limiter_is_per_peer() {
|
||||
let mut limiter = PresenceRateLimiter::default();
|
||||
let a = id();
|
||||
let b = id();
|
||||
let now = Instant::now();
|
||||
|
||||
for _ in 0..PRESENCE_RATE_LIMIT_BURST {
|
||||
assert!(limiter.allow(a, now));
|
||||
}
|
||||
assert!(!limiter.allow(a, now));
|
||||
assert!(limiter.allow(b, now));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn presence_mode_flags() {
|
||||
assert!(PresenceMode::Discoverable.publishes_to_discovery());
|
||||
|
||||
+405
-15
@@ -39,6 +39,10 @@ fn pixelpass_path_candidates(dir: &Path) -> [PathBuf; 1] {
|
||||
/// growth, but reject unbounded gossip payloads before the UI offers "Watch".
|
||||
const MAX_TICKET_LEN: usize = 512;
|
||||
|
||||
/// Upper bound on a PipeWire `application.name` we'll pass to `--app`. Real names
|
||||
/// are short ("Firefox", "mpv"); this only guards against a pathological value.
|
||||
const MAX_APP_NAME_LEN: usize = 256;
|
||||
|
||||
/// How long to wait for the host to emit its ticket / the viewer to connect
|
||||
/// before giving up and killing the child. Startup is normally sub-second; this
|
||||
/// is only a safety net so a hung pixelpass can't wedge the caller forever.
|
||||
@@ -64,6 +68,12 @@ pub enum PixelpassEvent {
|
||||
CaptureStarted,
|
||||
/// Host: capture pipeline torn down (on last viewer).
|
||||
CaptureStopped,
|
||||
/// Host (per-app audio): the chosen app's audio is now reaching viewers.
|
||||
AppAudioRouted,
|
||||
/// Host (per-app audio): the chosen app's last audio stream went away. Under
|
||||
/// our `--strict-audio` run this means viewers now hear silence (not the call
|
||||
/// echo) until the app produces audio again — we surface it as a warning.
|
||||
AppAudioLost,
|
||||
/// A recognized event we don't act on (e.g. `host_info`).
|
||||
Other,
|
||||
}
|
||||
@@ -98,6 +108,11 @@ pub fn parse_pixelpass_event(line: &str) -> Option<PixelpassEvent> {
|
||||
Some("stopped") => PixelpassEvent::CaptureStopped,
|
||||
_ => PixelpassEvent::Other,
|
||||
},
|
||||
"app_audio" => match v.get("state").and_then(|s| s.as_str()) {
|
||||
Some("routed") => PixelpassEvent::AppAudioRouted,
|
||||
Some("lost") => PixelpassEvent::AppAudioLost,
|
||||
_ => PixelpassEvent::Other,
|
||||
},
|
||||
_ => PixelpassEvent::Other,
|
||||
};
|
||||
Some(ev)
|
||||
@@ -107,6 +122,144 @@ fn json_u32(v: &serde_json::Value, key: &str) -> u32 {
|
||||
v.get(key).and_then(|x| x.as_u64()).unwrap_or(0) as u32
|
||||
}
|
||||
|
||||
/// Build the argv for a pixelpass *host*. Always `--host --output json`; when
|
||||
/// `audio_app` is `Some`, append `--app=<name> --strict-audio` so pixelpass
|
||||
/// captures only that app's audio instead of the whole desktop sink monitor
|
||||
/// (which contains our own call playout → the viewer would hear themselves
|
||||
/// echoed back, backlog A23).
|
||||
///
|
||||
/// `--strict-audio` is what makes the fix a guarantee rather than best-effort:
|
||||
/// without it, pixelpass falls back to the whole-desktop loopback before the
|
||||
/// app's first stream routes and again if the app's audio later stops — both of
|
||||
/// which reintroduce the echo. With it, the viewer hears only the chosen app (or
|
||||
/// silence), and pixelpass emits `app_audio` events we surface as a warning.
|
||||
///
|
||||
/// The name is passed in the single-token `--app=<name>` form so a value that
|
||||
/// happens to begin with `-` can never be reparsed as a pixelpass flag (clap
|
||||
/// otherwise rejects hyphen-leading option values). The name is locally chosen
|
||||
/// (our own enumeration / the user's pick), not peer-supplied, but is still
|
||||
/// sanitized via [`sanitize_app_name`] before reaching here. Pure: no I/O.
|
||||
pub fn host_args(audio_app: Option<&str>) -> Vec<String> {
|
||||
let mut args = vec![
|
||||
"--host".to_string(),
|
||||
"--output".to_string(),
|
||||
"json".to_string(),
|
||||
];
|
||||
if let Some(name) = audio_app.and_then(sanitize_app_name) {
|
||||
args.push(format!("--app={name}"));
|
||||
args.push("--strict-audio".to_string());
|
||||
}
|
||||
args
|
||||
}
|
||||
|
||||
/// Validate a locally-chosen audio app name before it becomes a `--app` value:
|
||||
/// trim, reject empty / overlong, and reject names carrying control characters
|
||||
/// (newlines etc.) that have no place in a real `application.name`. `None` means
|
||||
/// "no valid app selected" — the caller then shares the whole desktop audio.
|
||||
pub fn sanitize_app_name(name: &str) -> Option<String> {
|
||||
let name = name.trim();
|
||||
let ok = !name.is_empty()
|
||||
&& name.len() <= MAX_APP_NAME_LEN
|
||||
&& !name.chars().any(|c| c.is_control());
|
||||
ok.then(|| name.to_string())
|
||||
}
|
||||
|
||||
/// Hard cap on how long enumeration waits for `pactl`. It runs inline on the core
|
||||
/// command loop (the picker awaits it before opening), so a wedged/slow `pactl`
|
||||
/// must not stall mute/deafen/leave/stop. On timeout we treat it like any other
|
||||
/// failure: empty list → "All system audio" only.
|
||||
const LIST_APPS_TIMEOUT: Duration = Duration::from_secs(2);
|
||||
|
||||
/// Enumerate the apps currently sending audio to a sink, deduplicated by
|
||||
/// `application.name`. Mirrors how pixelpass itself builds its interactive
|
||||
/// picker (`pactl -f json list sink-inputs`), so the names we return are exactly
|
||||
/// the ones `--app` matches against. Returns an empty list on any error (pactl
|
||||
/// missing, non-PipeWire host, nothing playing, or [`LIST_APPS_TIMEOUT`] elapsed)
|
||||
/// — a normal, handled state that leaves the picker showing only "All system
|
||||
/// audio".
|
||||
pub async fn list_audio_apps() -> Vec<String> {
|
||||
let run = Command::new("pactl")
|
||||
.args(["-f", "json", "list", "sink-inputs"])
|
||||
.stdin(Stdio::null())
|
||||
.stdout(Stdio::piped())
|
||||
.stderr(Stdio::null())
|
||||
// On [`LIST_APPS_TIMEOUT`] the `output()` future is dropped, which drops
|
||||
// the child — `kill_on_drop(true)` then SIGKILLs and reaps it so a wedged
|
||||
// `pactl` can't linger/accumulate across picker opens (audit P3).
|
||||
.kill_on_drop(true)
|
||||
.output();
|
||||
match tokio::time::timeout(LIST_APPS_TIMEOUT, run).await {
|
||||
Ok(Ok(o)) if o.status.success() => parse_audio_apps(&o.stdout),
|
||||
_ => Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Hard cap on the capability probe (`pixelpass --help`). Conservative: a slow or
|
||||
/// hung pixelpass degrades to "strict audio unsupported" → whole-desktop-only
|
||||
/// picker (safe), never a stalled core loop.
|
||||
const HELP_PROBE_TIMEOUT: Duration = Duration::from_secs(2);
|
||||
|
||||
/// Whether the resolved pixelpass understands `--strict-audio` (added in pixelpass
|
||||
/// `85fdebe`). peerspeak only offers per-app audio capture when it does: a per-app
|
||||
/// share always appends `--strict-audio`, and an **older** pixelpass would have
|
||||
/// clap reject the unknown flag → the host spawn hard-fails and the share is
|
||||
/// broken (audit P2, version skew). When unsupported the picker degrades to
|
||||
/// whole-desktop audio only — we never silently drop to best-effort `--app`, which
|
||||
/// would reintroduce the call echo (A23).
|
||||
///
|
||||
/// Any probe failure/timeout returns `false` (degrade to the safe path). The
|
||||
/// `--help` child is `kill_on_drop` so a hung pixelpass can't linger.
|
||||
pub async fn supports_strict_audio(bin: &Path) -> bool {
|
||||
let run = Command::new(bin)
|
||||
.arg("--help")
|
||||
.stdin(Stdio::null())
|
||||
.stdout(Stdio::piped())
|
||||
.stderr(Stdio::null())
|
||||
.kill_on_drop(true)
|
||||
.output();
|
||||
match tokio::time::timeout(HELP_PROBE_TIMEOUT, run).await {
|
||||
Ok(Ok(o)) => help_mentions_strict_audio(&o.stdout),
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
/// Pure check: does `pixelpass --help` advertise `--strict-audio`? Matches the
|
||||
/// flag token rather than a whole line, since clap may wrap/realign help text.
|
||||
pub fn help_mentions_strict_audio(help_stdout: &[u8]) -> bool {
|
||||
String::from_utf8_lossy(help_stdout).contains("--strict-audio")
|
||||
}
|
||||
|
||||
/// Parse `pactl -f json list sink-inputs` stdout into a sorted, deduplicated list
|
||||
/// of `application.name`s. Pure: no I/O. Unparseable input yields an empty list.
|
||||
/// Each name is passed through [`sanitize_app_name`] so the picker only ever
|
||||
/// offers names that will actually survive [`host_args`]; otherwise a name that
|
||||
/// parses here but fails sanitization later would be selectable yet silently
|
||||
/// drop the `--app` flag and revert the share to whole-desktop audio (A23 echo).
|
||||
pub fn parse_audio_apps(stdout: &[u8]) -> Vec<String> {
|
||||
let Ok(entries) = serde_json::from_slice::<Vec<SinkInput>>(stdout) else {
|
||||
return Vec::new();
|
||||
};
|
||||
let mut names: Vec<String> = entries
|
||||
.into_iter()
|
||||
.filter_map(|e| e.properties.application_name)
|
||||
.filter_map(|n| sanitize_app_name(&n))
|
||||
.collect();
|
||||
names.sort_unstable();
|
||||
names.dedup();
|
||||
names
|
||||
}
|
||||
|
||||
#[derive(serde::Deserialize)]
|
||||
struct SinkInput {
|
||||
properties: SinkInputProperties,
|
||||
}
|
||||
|
||||
#[derive(serde::Deserialize)]
|
||||
struct SinkInputProperties {
|
||||
#[serde(rename = "application.name")]
|
||||
application_name: Option<String>,
|
||||
}
|
||||
|
||||
/// Build the argv for a pixelpass *viewer*. The `ticket` is peer-supplied (it
|
||||
/// rides gossip presence, which is untrusted and spoofable), so flags come first
|
||||
/// and the ticket is passed as a positional **after a `--` end-of-options
|
||||
@@ -162,20 +315,30 @@ pub fn is_available(config_override: Option<&str>) -> bool {
|
||||
pixelpass_path(config_override).is_some()
|
||||
}
|
||||
|
||||
/// Spawn a pixelpass host (`pixelpass --host --output json`), wait for its
|
||||
/// startup ticket, and return the live child plus the ticket. The child keeps
|
||||
/// Spawn a pixelpass host (`pixelpass --host --output json [--app=<name>]`), wait
|
||||
/// for its startup ticket, and return the live child plus the ticket. When
|
||||
/// `audio_app` is `Some`, pixelpass captures only that app's audio instead of the
|
||||
/// whole desktop sink, which avoids the call-loopback echo (A23). The child keeps
|
||||
/// running (streaming to viewers) until killed or dropped; remaining stdout is
|
||||
/// drained in a background task so a full pipe can't stall the host. We do
|
||||
/// **not** pass `--max-viewers`: pixelpass bandwidth-measures its own safe cap,
|
||||
/// protecting the sharer's uplink, and refuses extras with `viewer_refused`.
|
||||
pub async fn spawn_host(bin: &Path) -> std::io::Result<(Child, String)> {
|
||||
pub async fn spawn_host(
|
||||
bin: &Path,
|
||||
audio_app: Option<&str>,
|
||||
notices: Option<tokio::sync::mpsc::UnboundedSender<PixelpassEvent>>,
|
||||
) -> std::io::Result<(Child, String)> {
|
||||
let mut child = Command::new(bin)
|
||||
.arg("--host")
|
||||
.arg("--output")
|
||||
.arg("json")
|
||||
.args(host_args(audio_app))
|
||||
.stdin(Stdio::null())
|
||||
.stdout(Stdio::piped())
|
||||
.stderr(Stdio::null())
|
||||
// Capture stderr (not null): pixelpass prints its startup precondition
|
||||
// failures there — a missing GStreamer plugin / `pactl`, each with an
|
||||
// actionable "Install hint: sudo apt install ..." line. If the host dies
|
||||
// before its ticket we fold that tail into our error so the user sees
|
||||
// *what to install* instead of a dead-end "exited before a ticket". On
|
||||
// the success path we drain it in the background so the pipe can't fill.
|
||||
.stderr(Stdio::piped())
|
||||
.kill_on_drop(true)
|
||||
.spawn()?;
|
||||
|
||||
@@ -183,6 +346,7 @@ pub async fn spawn_host(bin: &Path) -> std::io::Result<(Child, String)> {
|
||||
.stdout
|
||||
.take()
|
||||
.ok_or_else(|| std::io::Error::other("pixelpass host stdout missing"))?;
|
||||
let stderr = child.stderr.take();
|
||||
let mut lines = BufReader::new(stdout).lines();
|
||||
|
||||
let ticket = match read_until(&mut lines, |e| match e {
|
||||
@@ -194,9 +358,10 @@ pub async fn spawn_host(bin: &Path) -> std::io::Result<(Child, String)> {
|
||||
Ok(Some(t)) => t,
|
||||
Ok(None) => {
|
||||
let _ = child.kill().await;
|
||||
return Err(std::io::Error::other(
|
||||
"pixelpass host exited before emitting a ticket",
|
||||
));
|
||||
let detail = read_stderr_tail(stderr).await;
|
||||
return Err(std::io::Error::other(format!(
|
||||
"pixelpass host exited before emitting a ticket{detail}"
|
||||
)));
|
||||
}
|
||||
Err(e) => {
|
||||
let _ = child.kill().await;
|
||||
@@ -204,10 +369,65 @@ pub async fn spawn_host(bin: &Path) -> std::io::Result<(Child, String)> {
|
||||
}
|
||||
};
|
||||
|
||||
drain_in_background(lines, "host");
|
||||
if let Some(stderr) = stderr {
|
||||
drain_stderr_in_background(stderr);
|
||||
}
|
||||
drain_in_background(lines, "host", notices);
|
||||
Ok((child, ticket))
|
||||
}
|
||||
|
||||
/// Read a killed pixelpass child's stderr to EOF and reduce it to a short,
|
||||
/// user-facing diagnostic tail via [`pixelpass_failure_detail`]. Bounded: the
|
||||
/// caller kills the child first, so the pipe EOFs promptly. Returns an empty
|
||||
/// string when stderr was already taken or carried nothing useful.
|
||||
async fn read_stderr_tail(stderr: Option<tokio::process::ChildStderr>) -> String {
|
||||
use tokio::io::AsyncReadExt;
|
||||
let Some(mut stderr) = stderr else {
|
||||
return String::new();
|
||||
};
|
||||
let mut buf = Vec::new();
|
||||
let _ = stderr.read_to_end(&mut buf).await;
|
||||
pixelpass_failure_detail(&String::from_utf8_lossy(&buf))
|
||||
}
|
||||
|
||||
/// Discard a running pixelpass child's stderr in the background so its pipe
|
||||
/// can't fill and stall the host (mirrors [`drain_in_background`] for stdout).
|
||||
fn drain_stderr_in_background(mut stderr: tokio::process::ChildStderr) {
|
||||
use tokio::io::AsyncReadExt;
|
||||
tokio::spawn(async move {
|
||||
let mut buf = [0u8; 4096];
|
||||
while let Ok(n) = stderr.read(&mut buf).await {
|
||||
if n == 0 {
|
||||
break;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/// Extract a human-useful tail from a failed pixelpass child's stderr to append
|
||||
/// to our error. pixelpass writes actionable startup errors there (a missing
|
||||
/// GStreamer element / `pactl` plus an `Install hint: sudo apt install ...`
|
||||
/// line), which is exactly what a freshly-installed host needs to see. The
|
||||
/// decorative host banner (box-drawing) is dropped — it only prints on the
|
||||
/// success path, but we filter it defensively. Pure: no I/O. Returns an empty
|
||||
/// string when there's nothing worth surfacing (so callers can append blindly).
|
||||
pub fn pixelpass_failure_detail(stderr: &str) -> String {
|
||||
let useful: Vec<&str> = stderr
|
||||
.lines()
|
||||
.map(str::trim_end)
|
||||
.filter(|l| !l.trim().is_empty())
|
||||
.filter(|l| !l.trim_start().starts_with(['│', '┌', '└', '├']))
|
||||
.collect();
|
||||
if useful.is_empty() {
|
||||
return String::new();
|
||||
}
|
||||
// The anyhow error and its install hint are the *last* lines printed, so
|
||||
// keep the tail rather than the head.
|
||||
const MAX_LINES: usize = 12;
|
||||
let start = useful.len().saturating_sub(MAX_LINES);
|
||||
format!("\n\npixelpass reported:\n{}", useful[start..].join("\n"))
|
||||
}
|
||||
|
||||
/// Spawn a pixelpass viewer for `ticket`, wait for it to connect, and open the
|
||||
/// stream in a local player (mpv, falling back to vlc). Returns the live viewer
|
||||
/// child so the caller can kill it on room-leave; it also self-exits when the
|
||||
@@ -251,7 +471,7 @@ pub async fn spawn_viewer(bin: &Path, ticket: &str) -> std::io::Result<Child> {
|
||||
return Err(e);
|
||||
}
|
||||
|
||||
drain_in_background(lines, "viewer");
|
||||
drain_in_background(lines, "viewer", None);
|
||||
Ok(child)
|
||||
}
|
||||
|
||||
@@ -286,15 +506,24 @@ where
|
||||
}
|
||||
|
||||
/// Keep reading the child's stdout to EOF in the background so a full pipe can't
|
||||
/// stall it; log notable events for diagnostics.
|
||||
fn drain_in_background<R>(mut lines: tokio::io::Lines<BufReader<R>>, role: &'static str)
|
||||
where
|
||||
/// stall it; log notable events for diagnostics. When `notices` is `Some`, each
|
||||
/// parsed event is also forwarded to the caller (the core, which translates the
|
||||
/// `app_audio` ones into a UI warning); a send failure (receiver dropped) just
|
||||
/// stops forwarding, draining continues. The task ends on EOF (child exited).
|
||||
fn drain_in_background<R>(
|
||||
mut lines: tokio::io::Lines<BufReader<R>>,
|
||||
role: &'static str,
|
||||
notices: Option<tokio::sync::mpsc::UnboundedSender<PixelpassEvent>>,
|
||||
) where
|
||||
R: tokio::io::AsyncRead + Unpin + Send + 'static,
|
||||
{
|
||||
tokio::spawn(async move {
|
||||
while let Ok(Some(line)) = lines.next_line().await {
|
||||
if let Some(ev) = parse_pixelpass_event(&line) {
|
||||
crate::log_msg(&format!("pixelpass {role}: {}", event_for_log(&ev)));
|
||||
if let Some(tx) = ¬ices {
|
||||
let _ = tx.send(ev);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -313,6 +542,8 @@ fn event_for_log(ev: &PixelpassEvent) -> String {
|
||||
PixelpassEvent::Refused(reason) => format!("viewer_refused reason={reason:?}"),
|
||||
PixelpassEvent::CaptureStarted => "capture_started".to_string(),
|
||||
PixelpassEvent::CaptureStopped => "capture_stopped".to_string(),
|
||||
PixelpassEvent::AppAudioRouted => "app_audio_routed".to_string(),
|
||||
PixelpassEvent::AppAudioLost => "app_audio_lost".to_string(),
|
||||
PixelpassEvent::Other => "other".to_string(),
|
||||
}
|
||||
}
|
||||
@@ -384,6 +615,142 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn host_args_without_app_shares_whole_desktop() {
|
||||
// No app selected → no --app flag → pixelpass keeps its default
|
||||
// (whole-desktop) audio capture.
|
||||
assert_eq!(host_args(None), vec!["--host", "--output", "json"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn host_args_with_app_appends_single_token_flag() {
|
||||
// The chosen app rides in the `--app=<name>` single-token form so a
|
||||
// name beginning with `-` can never be reparsed as a flag (A23), plus
|
||||
// `--strict-audio` so pixelpass never falls back to whole-desktop audio.
|
||||
assert_eq!(
|
||||
host_args(Some("Firefox")),
|
||||
vec!["--host", "--output", "json", "--app=Firefox", "--strict-audio"]
|
||||
);
|
||||
// The hyphen-leading name is still bound to --app as a single token;
|
||||
// --strict-audio is the trailing flag.
|
||||
let args = host_args(Some("-rm -rf"));
|
||||
assert_eq!(args[3], "--app=-rm -rf");
|
||||
assert_eq!(args[4], "--strict-audio");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn host_args_blank_or_control_app_is_dropped() {
|
||||
// An empty / whitespace / control-laden selection is sanitized away,
|
||||
// falling back to whole-desktop capture rather than a broken flag.
|
||||
assert_eq!(host_args(Some(" ")), vec!["--host", "--output", "json"]);
|
||||
assert_eq!(host_args(Some("bad\nname")), vec!["--host", "--output", "json"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sanitize_app_name_trims_and_rejects_garbage() {
|
||||
assert_eq!(sanitize_app_name(" Firefox \n"), Some("Firefox".to_string()));
|
||||
assert_eq!(sanitize_app_name(""), None);
|
||||
assert_eq!(sanitize_app_name(" "), None);
|
||||
assert_eq!(sanitize_app_name("a\tb"), None);
|
||||
assert_eq!(sanitize_app_name(&"x".repeat(MAX_APP_NAME_LEN + 1)), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_audio_apps_dedups_and_sorts_by_application_name() {
|
||||
let stdout = br#"[
|
||||
{"index":1,"properties":{"application.name":"Firefox"}},
|
||||
{"index":2,"properties":{"application.name":"mpv"}},
|
||||
{"index":3,"properties":{"application.name":"Firefox"}},
|
||||
{"index":4,"properties":{"application.name":" Spotify "}},
|
||||
{"index":5,"properties":{"application.name":""}},
|
||||
{"index":6,"properties":{"other":"no name here"}}
|
||||
]"#;
|
||||
assert_eq!(
|
||||
parse_audio_apps(stdout),
|
||||
vec!["Firefox".to_string(), "Spotify".to_string(), "mpv".to_string()]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_audio_apps_empty_or_garbage_is_empty() {
|
||||
assert_eq!(parse_audio_apps(b""), Vec::<String>::new());
|
||||
assert_eq!(parse_audio_apps(b"not json"), Vec::<String>::new());
|
||||
assert_eq!(parse_audio_apps(b"[]"), Vec::<String>::new());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_audio_apps_drops_names_host_args_would_reject() {
|
||||
// Names that parse from pactl but fail `sanitize_app_name` (control chars,
|
||||
// overlong) must NOT be offered in the picker — otherwise the user could
|
||||
// pick one, `host_args` would silently drop `--app`, and the share would
|
||||
// revert to whole-desktop audio (A23 echo) with no signal. The valid name
|
||||
// survives; the control-char and overlong ones are filtered out.
|
||||
let overlong = "x".repeat(MAX_APP_NAME_LEN + 1);
|
||||
let stdout = format!(
|
||||
r#"[
|
||||
{{"index":1,"properties":{{"application.name":"mpv"}}}},
|
||||
{{"index":2,"properties":{{"application.name":"bad\nname"}}}},
|
||||
{{"index":3,"properties":{{"application.name":"{overlong}"}}}}
|
||||
]"#
|
||||
);
|
||||
assert_eq!(parse_audio_apps(stdout.as_bytes()), vec!["mpv".to_string()]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn failure_detail_surfaces_install_hint_and_drops_banner() {
|
||||
// The real shape of a fresh-host failure: anyhow error + install hint on
|
||||
// stderr. We must keep those (so the user knows what to apt install) and
|
||||
// drop the decorative banner box-drawing lines.
|
||||
let stderr = "\
|
||||
┌─ PixelPass · host ─────────────────────────────────────────
|
||||
│ display server : Wayland
|
||||
└────────────────────────────────────────────────────────────
|
||||
Error: GStreamer element `vah264enc` not available.
|
||||
Install hint: sudo apt install gstreamer1.0-plugins-bad
|
||||
";
|
||||
let detail = pixelpass_failure_detail(stderr);
|
||||
assert!(detail.starts_with("\n\npixelpass reported:\n"));
|
||||
assert!(detail.contains("vah264enc` not available"));
|
||||
assert!(detail.contains("sudo apt install gstreamer1.0-plugins-bad"));
|
||||
assert!(!detail.contains('│'), "banner box-drawing must be dropped");
|
||||
assert!(!detail.contains('┌'));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn failure_detail_empty_when_nothing_useful() {
|
||||
// Blank / banner-only stderr yields an empty string so the caller can
|
||||
// append it to the base message unconditionally without trailing noise.
|
||||
assert_eq!(pixelpass_failure_detail(""), "");
|
||||
assert_eq!(pixelpass_failure_detail(" \n \n"), "");
|
||||
assert_eq!(
|
||||
pixelpass_failure_detail("│ display server : Wayland\n│ capture : x\n"),
|
||||
""
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn failure_detail_keeps_only_the_tail() {
|
||||
// A long stderr is truncated to its last lines (where the real error
|
||||
// and hint live), not its head.
|
||||
let body: String = (0..30).map(|i| format!("line {i}\n")).collect();
|
||||
let detail = pixelpass_failure_detail(&body);
|
||||
assert!(detail.contains("line 29"));
|
||||
assert!(!detail.contains("line 0\n"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn help_probe_detects_strict_audio_flag() {
|
||||
// A new pixelpass advertises the flag; an old one doesn't. The probe must
|
||||
// match the token even when clap wraps the option onto its own line.
|
||||
let new_help = b"Options:\n --app <APP>\n --strict-audio\n With --app, never fall back...";
|
||||
assert!(help_mentions_strict_audio(new_help));
|
||||
let old_help = b"Options:\n --app <APP>\n --output <OUTPUT>\n -h, --help";
|
||||
assert!(!help_mentions_strict_audio(old_help));
|
||||
// Garbage / empty output degrades to "unsupported" (safe path).
|
||||
assert!(!help_mentions_strict_audio(b""));
|
||||
assert!(!help_mentions_strict_audio(&[0xff, 0xfe, 0x00]));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sanitize_ticket_accepts_pixelpass_endpoint_ticket_shape() {
|
||||
let ticket = "endpointaabwxjexzensznfvuudiapn5tyzws3angd2merarm";
|
||||
@@ -470,6 +837,29 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parses_app_audio_states() {
|
||||
// The wire contract from pixelpass's --strict-audio run (A23): routed =
|
||||
// the chosen app's audio is live; lost = it stopped (viewers now silent).
|
||||
assert_eq!(
|
||||
parse_pixelpass_event(r#"{"event":"app_audio","state":"routed"}"#),
|
||||
Some(PixelpassEvent::AppAudioRouted)
|
||||
);
|
||||
assert_eq!(
|
||||
parse_pixelpass_event(r#"{"event":"app_audio","state":"lost"}"#),
|
||||
Some(PixelpassEvent::AppAudioLost)
|
||||
);
|
||||
// Unknown / missing state is recognized-but-unused, not a parse failure.
|
||||
assert_eq!(
|
||||
parse_pixelpass_event(r#"{"event":"app_audio","state":"weird"}"#),
|
||||
Some(PixelpassEvent::Other)
|
||||
);
|
||||
assert_eq!(
|
||||
parse_pixelpass_event(r#"{"event":"app_audio"}"#),
|
||||
Some(PixelpassEvent::Other)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn recognized_but_unused_event_is_other() {
|
||||
assert_eq!(
|
||||
|
||||
Reference in New Issue
Block a user