feat(client): protocol v2 wire layer, screen sharing, channel webhooks and secure voice
Verified against the real gateway via tests/gateway_interop.rs.
This commit is contained in:
parent
8460fb28fd
commit
3d7dd5413b
65 changed files with 6048 additions and 1156 deletions
|
|
@ -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)]
|
||||
|
|
|
|||
|
|
@ -23,6 +23,7 @@ pub fn run(
|
|||
bitrate_bps: Arc<AtomicU32>,
|
||||
local_speaking: Arc<AtomicBool>,
|
||||
stopped: Arc<AtomicBool>,
|
||||
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];
|
||||
|
|
|
|||
148
src/audio/pipeline/media.rs
Normal file
148
src/audio/pipeline/media.rs
Normal file
|
|
@ -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<Mutex<HashMap<String, VecDeque<[f32; 2]>>>>,
|
||||
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<f32> = frame
|
||||
.samples
|
||||
.chunks(channels)
|
||||
.map(|frame| frame[0])
|
||||
.collect();
|
||||
let right: Vec<f32> = 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<Mutex<TapState>>);
|
||||
#[derive(Default)]
|
||||
struct TapState {
|
||||
enabled: bool,
|
||||
queue: VecDeque<AudioFrame>,
|
||||
}
|
||||
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<AudioFrame> {
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
|
@ -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<AtomicBool>,
|
||||
encoded_rx: Receiver<EncodedFrame>,
|
||||
microphone_tap: media::MicrophoneTap,
|
||||
media_mixer: MediaMixer,
|
||||
voice_tx: Sender<IncomingVoice>,
|
||||
_encoded_tx: Sender<EncodedFrame>,
|
||||
gate: Arc<Mutex<TransmitGate>>,
|
||||
|
|
@ -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<crate::media::AudioFrame> {
|
||||
self.microphone_tap.pop()
|
||||
}
|
||||
pub fn media_mixer(&self) -> MediaMixer {
|
||||
self.media_mixer.clone()
|
||||
}
|
||||
|
||||
pub fn poll_encoded(&self) -> Option<EncodedFrame> {
|
||||
self.encoded_rx.try_recv().ok()
|
||||
}
|
||||
|
|
@ -111,6 +127,10 @@ pub fn start(cfg: PipelineConfig) -> anyhow::Result<AudioPipeline> {
|
|||
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::<EncodedFrame>();
|
||||
let (voice_tx, voice_rx) = mpsc::channel::<IncomingVoice>();
|
||||
|
|
@ -148,6 +168,7 @@ pub fn start(cfg: PipelineConfig) -> anyhow::Result<AudioPipeline> {
|
|||
bitrate_cap,
|
||||
local_speaking_cap,
|
||||
capture_stopped,
|
||||
tap_capture,
|
||||
) {
|
||||
tracing::error!("capture: {e}");
|
||||
}
|
||||
|
|
@ -160,6 +181,7 @@ pub fn start(cfg: PipelineConfig) -> anyhow::Result<AudioPipeline> {
|
|||
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<AudioPipeline> {
|
|||
Ok(AudioPipeline {
|
||||
stopped,
|
||||
encoded_rx,
|
||||
microphone_tap,
|
||||
media_mixer,
|
||||
voice_tx,
|
||||
_encoded_tx: encoded_tx,
|
||||
gate,
|
||||
|
|
|
|||
|
|
@ -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<Mutex<f32>>,
|
||||
jitter_cfg: Arc<Mutex<JitterConfig>>,
|
||||
last_remote_voice_ms: Arc<AtomicU64>,
|
||||
media_mixer: super::media::MediaMixer,
|
||||
) -> Result<()> {
|
||||
let mut decoder = opus::Decoder::new(OPUS_RATE, opus::Channels::Mono)?;
|
||||
let pb: Arc<Mutex<Vec<f32>>> = 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,
|
||||
|
|
|
|||
|
|
@ -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<NetEvent> {
|
||||
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<Session> {
|
||||
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<NetHandle> {
|
||||
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(())
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,3 +6,5 @@ pub mod jitter;
|
|||
pub mod net;
|
||||
|
||||
pub mod media;
|
||||
|
||||
pub mod webhooks;
|
||||
|
|
|
|||
43
src/main.rs
43
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<Rc<ui::webhooks::WebhookUi>>,
|
||||
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<RefCell<AppState>>) {
|
||||
{
|
||||
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));
|
||||
|
|
|
|||
575
src/media/broadcast.rs
Normal file
575
src/media/broadcast.rs
Normal file
|
|
@ -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<Arc<VideoFrame>>,
|
||||
audio: broadcast::Receiver<Arc<AudioFrame>>,
|
||||
latest: Option<Arc<VideoFrame>>,
|
||||
peers: HashMap<String, Peer>,
|
||||
}
|
||||
struct Viewer {
|
||||
share: AvailableShare,
|
||||
peer: Option<Peer>,
|
||||
pending_ice: Vec<(u32, String)>,
|
||||
started: std::time::Instant,
|
||||
}
|
||||
#[derive(Default)]
|
||||
pub struct BroadcastManager {
|
||||
session: String,
|
||||
channel: Option<String>,
|
||||
available: HashMap<String, AvailableShare>,
|
||||
publisher: Option<Publisher>,
|
||||
viewers: HashMap<String, Viewer>,
|
||||
outgoing: VecDeque<MediaControl>,
|
||||
errors: VecDeque<String>,
|
||||
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<String, String> {
|
||||
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<AvailableShare> {
|
||||
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<RemoteFeed> {
|
||||
Some(self.viewers.get(id)?.peer.as_ref()?.remote.clone())
|
||||
}
|
||||
pub fn take_control(&mut self) -> Option<MediaControl> {
|
||||
self.outgoing.pop_front()
|
||||
}
|
||||
pub fn take_error(&mut self) -> Option<String> {
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
|
@ -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<Mutex<Option<VideoFrame>>>,
|
||||
video: Arc<Mutex<Option<Arc<VideoFrame>>>>,
|
||||
video_tx: Arc<tokio::sync::broadcast::Sender<Arc<VideoFrame>>>,
|
||||
audio_tx: Arc<tokio::sync::broadcast::Sender<Arc<super::AudioFrame>>>,
|
||||
audio: Arc<Mutex<f32>>,
|
||||
received_audio: Arc<AtomicBool>,
|
||||
}
|
||||
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<VideoFrame> {
|
||||
#[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<Arc<VideoFrame>> {
|
||||
self.video_tx.subscribe()
|
||||
}
|
||||
pub fn subscribe_audio(&self) -> tokio::sync::broadcast::Receiver<Arc<super::AudioFrame>> {
|
||||
self.audio_tx.subscribe()
|
||||
}
|
||||
pub fn take_frame(&self) -> Option<Arc<VideoFrame>> {
|
||||
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<VideoFrame, String> {
|
||||
pub(super) fn copy_video(sample: &gst::Sample) -> Result<VideoFrame, String> {
|
||||
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<gst::Pipel
|
|||
.build();
|
||||
let level = feed.audio.clone();
|
||||
let received = feed.received_audio.clone();
|
||||
let tx = feed.audio_tx.clone();
|
||||
sink.set_callbacks(
|
||||
gstreamer_app::AppSinkCallbacks::builder()
|
||||
.new_sample(move |sink| {
|
||||
|
|
@ -273,15 +304,22 @@ fn audio_pipeline(source: gst::Element, feed: &CaptureFeed) -> Result<gst::Pipel
|
|||
if map.len() > 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(),
|
||||
|
|
|
|||
245
src/media/gateway_tests.rs
Normal file
245
src/media/gateway_tests.rs
Normal file
|
|
@ -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"
|
||||
);
|
||||
}
|
||||
|
|
@ -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;
|
||||
|
|
|
|||
646
src/media/peer.rs
Normal file
646
src/media/peer.rs
Normal file
|
|
@ -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<Mutex<Option<Arc<VideoFrame>>>>,
|
||||
app: Arc<Mutex<std::collections::VecDeque<AudioFrame>>>,
|
||||
mic: Arc<Mutex<std::collections::VecDeque<AudioFrame>>>,
|
||||
pub video_frames: Arc<AtomicU64>,
|
||||
pub application_samples: Arc<AtomicU64>,
|
||||
pub microphone_samples: Arc<AtomicU64>,
|
||||
}
|
||||
impl RemoteFeed {
|
||||
pub fn take_video(&self) -> Option<Arc<VideoFrame>> {
|
||||
self.video.lock().ok()?.take()
|
||||
}
|
||||
pub fn take_audio(&self, track: AudioTrack) -> Option<AudioFrame> {
|
||||
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<String>,
|
||||
pub turn: Option<String>,
|
||||
}
|
||||
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<gstreamer_app::AppSrc>,
|
||||
app: Option<gstreamer_app::AppSrc>,
|
||||
mic: Option<gstreamer_app::AppSrc>,
|
||||
events: mpsc::Receiver<Signal>,
|
||||
tx: mpsc::SyncSender<Signal>,
|
||||
pub remote: RemoteFeed,
|
||||
remote_set: bool,
|
||||
started: std::time::Instant,
|
||||
pending_ice: Vec<(u32, String)>,
|
||||
app_time: Mutex<Option<u64>>,
|
||||
mic_time: Mutex<Option<u64>>,
|
||||
}
|
||||
|
||||
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, String> {
|
||||
Self::new(true, tracks, &IceServers::default())
|
||||
}
|
||||
pub fn viewer(tracks: Tracks) -> Result<Self, String> {
|
||||
Self::new(false, tracks, &IceServers::default())
|
||||
}
|
||||
pub fn publisher_with_ice(tracks: Tracks, ice: &IceServers) -> Result<Self, String> {
|
||||
Self::new(true, tracks, ice)
|
||||
}
|
||||
pub fn viewer_with_ice(tracks: Tracks, ice: &IceServers) -> Result<Self, String> {
|
||||
Self::new(false, tracks, ice)
|
||||
}
|
||||
fn new(publisher: bool, tracks: Tracks, ice: &IceServers) -> Result<Self, String> {
|
||||
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::<u32>(), values[2].get::<String>())
|
||||
{
|
||||
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<Signal> {
|
||||
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<Option<Signal>, 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::<rtc::WebRTCPeerConnectionState>("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::<rtc::WebRTCPeerConnectionState>("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<u8> = 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<gstreamer_app::AppSrc, String> {
|
||||
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::<gstreamer_app::AppSrc>()
|
||||
.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::<rtc::WebRTCRTPTransceiver>("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<Signal>) {
|
||||
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::<rtc::WebRTCSessionDescription>(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::<gst::Promise>],
|
||||
);
|
||||
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::<gst::Structure>, &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::<i32>("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::<gstreamer_app::AppSink>()
|
||||
.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<f32> = 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());
|
||||
}
|
||||
}
|
||||
|
|
@ -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<f32>,
|
||||
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(())
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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<u8> {
|
||||
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<u8> {
|
||||
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<Vec<u8>> {
|
||||
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<Vec<u8>> {
|
||||
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:?}"))
|
||||
|
|
|
|||
|
|
@ -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::<Sha256>::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<u8> {
|
||||
cipher::encrypt_with_key(&self.c2s_key, &self.cid, seq, plaintext)
|
||||
pub fn encrypt_c2s_aad(&self, seq: u64, plaintext: &[u8], aad: &[u8]) -> Vec<u8> {
|
||||
cipher::encrypt_with_aad(&self.c2s_key, &self.cid, seq, plaintext, aad)
|
||||
}
|
||||
|
||||
pub fn decrypt_s2c(&self, seq: u64, ciphertext: &[u8]) -> anyhow::Result<Vec<u8>> {
|
||||
cipher::decrypt_with_key(&self.s2c_key, &self.cid, seq, ciphertext)
|
||||
}
|
||||
|
||||
pub fn decrypt_c2s(&self, seq: u64, ciphertext: &[u8]) -> anyhow::Result<Vec<u8>> {
|
||||
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<Vec<u8>> {
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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<RawPacket> {
|
||||
pub async fn read_encrypted(
|
||||
stream: &mut (impl tokio::io::AsyncRead + Unpin),
|
||||
crypto: &SessionCrypto,
|
||||
) -> Result<RawPacket> {
|
||||
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<Result<RawPacket>>,
|
||||
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<RawPacket> {
|
||||
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<u8> {
|
||||
let mut keys = [0u8; 64];
|
||||
Hkdf::<Sha256>::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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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<u8>,
|
||||
}
|
||||
|
||||
|
|
@ -37,29 +37,45 @@ pub fn decode_frame(bytes: &[u8]) -> Result<RawPacket> {
|
|||
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<RawPacket> {
|
||||
pub async fn read(stream: &mut (impl tokio::io::AsyncRead + Unpin)) -> Result<RawPacket> {
|
||||
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(())
|
||||
|
|
|
|||
|
|
@ -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::<String>()
|
||||
);
|
||||
|
||||
Ok((sess.session_id, hello.node_name, hello.private_mode, crypto))
|
||||
|
|
|
|||
|
|
@ -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<NetEvent>) -> 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<NetEvent>) -> 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<NetEvent>) -> 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<NetEvent>) -> 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<NetEvent>) -> 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<NetEvent>) -> 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<NetEvent>) -> 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<NetEvent>) -> 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<NetEvent>) -> 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<NetEvent>) -> 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<NetEvent>) -> 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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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<NetEvent>) -> 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<NetEvent>) -> 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<NetEvent>) -> 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<NetEvent>) -> 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<NetEvent>) -> 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<NetEvent>) -> 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<NetEvent>) -> 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<NetEvent>) -> 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<NetEvent>) -> 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<NetEvent>) -> 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<NetEvent>) -> 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<NetEvent>) -> 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<NetEvent>) -> 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,
|
||||
|
|
|
|||
|
|
@ -11,7 +11,35 @@ mod social;
|
|||
|
||||
pub async fn incoming(pid: u16, payload: &[u8], tx: &mpsc::Sender<NetEvent>) -> 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<NetEvent>) ->
|
|||
| 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
|
||||
|
|
|
|||
|
|
@ -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<NetEvent>) -> 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<NetEvent>) -> 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<NetEvent>) -> 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<NetEvent>) -> 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<NetEvent>) -> 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<NetEvent>) -> 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<NetEvent>) -> 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<NetEvent>) -> 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<NetEvent>) -> 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<NetEvent>) -> 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<NetEvent>) -> Re
|
|||
.await;
|
||||
}
|
||||
PID_BLOCK_LIST => {
|
||||
// Server may send {"blocked": [...]} — try to parse properly first,
|
||||
// fall back to JSON value extraction.
|
||||
let blocked: Vec<String> = match serde_json::from_slice::<BlockListPayload>(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;
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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<WireMember>,
|
||||
pub voice_endpoint: String,
|
||||
#[serde(default)]
|
||||
pub guild_id: Option<String>,
|
||||
}
|
||||
|
||||
#[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<String>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||
pub struct ChannelDeletePayload {
|
||||
pub channel_id: String,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||
pub struct ChannelListPayload {
|
||||
pub channels: Vec<ChannelListItem>,
|
||||
}
|
||||
|
||||
#[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<String>,
|
||||
}
|
||||
|
||||
#[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<String>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub struct ChatHistoryPayload {
|
||||
pub channel_id: String,
|
||||
pub messages: Vec<ChatMessagePayload>,
|
||||
}
|
||||
|
||||
#[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,
|
||||
};
|
||||
|
|
|
|||
|
|
@ -1,162 +1,23 @@
|
|||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub struct E2eeDmKeyExchangePayload {
|
||||
pub dm_id: String,
|
||||
pub e2ee_public_key: Vec<u8>,
|
||||
}
|
||||
|
||||
#[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<u8>,
|
||||
pub timestamp: i64,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub struct E2eeDmHistoryPayload {
|
||||
pub dm_id: String,
|
||||
#[serde(default)]
|
||||
pub messages: Vec<E2eeDmMessagePayload>,
|
||||
#[serde(default)]
|
||||
pub limit: Option<i64>,
|
||||
}
|
||||
|
||||
#[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<DmMessagePayload>,
|
||||
#[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<DmMessagePayload>,
|
||||
#[serde(default)]
|
||||
pub search_query: Option<String>,
|
||||
#[serde(default)]
|
||||
pub limit: Option<i64>,
|
||||
}
|
||||
|
||||
#[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<String>,
|
||||
/// Free-form activity text (e.g. "CS2 ranked").
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub activity_text: Option<String>,
|
||||
/// Free-form custom status text (Discord-style "what's on your mind?").
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub custom_status: Option<String>,
|
||||
}
|
||||
|
||||
#[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<String>,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub activity: Option<String>,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub activity_text: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub struct PresenceSyncPayload {
|
||||
pub presences: Vec<PresenceInfoPayload>,
|
||||
}
|
||||
|
||||
#[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<String>,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub activity: Option<String>,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub activity_text: Option<String>,
|
||||
}
|
||||
|
||||
#[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<FriendInfoPayload>,
|
||||
}
|
||||
|
||||
#[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<String>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub struct FriendEventPayload {
|
||||
pub event_type: String,
|
||||
pub user_id: String,
|
||||
pub nickname: Option<String>,
|
||||
}
|
||||
// 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;
|
||||
|
|
|
|||
|
|
@ -1,176 +1,25 @@
|
|||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub struct GuildCreatePayload {
|
||||
pub name: String,
|
||||
pub icon_url: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||
pub struct GuildInfoPayload {
|
||||
pub guild_id: String,
|
||||
pub name: String,
|
||||
pub owner_id: String,
|
||||
pub icon_url: Option<String>,
|
||||
pub member_count: u32,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub struct GuildListPayload {
|
||||
pub guilds: Vec<GuildInfoPayload>,
|
||||
}
|
||||
|
||||
#[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<String>,
|
||||
}
|
||||
|
||||
#[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<i64>,
|
||||
pub expires_in_seconds: Option<i64>,
|
||||
}
|
||||
|
||||
#[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<String>,
|
||||
pub permissions: Option<u64>,
|
||||
}
|
||||
|
||||
#[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<i64>,
|
||||
pub uses: i64,
|
||||
pub expires_at: Option<i64>,
|
||||
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<String>,
|
||||
#[serde(default)]
|
||||
pub target_type: Option<String>,
|
||||
#[serde(default)]
|
||||
pub reason: Option<String>,
|
||||
pub created_at: i64,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||
pub struct GuildAuditLogPayload {
|
||||
pub guild_id: String,
|
||||
pub entries: Vec<AuditLogEntryPayload>,
|
||||
}
|
||||
|
||||
#[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<GuildMemberInfoPayload>,
|
||||
}
|
||||
|
||||
#[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<GuildRoleInfoPayload>,
|
||||
}
|
||||
// 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;
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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<NetCommand>,
|
||||
event_tx: &mpsc::Sender<NetEvent>,
|
||||
) -> 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<VoiceState> = None;
|
||||
let mut used_voice_routes = std::collections::HashSet::new();
|
||||
let mut voice_endpoint: Option<std::net::SocketAddr> = 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::<ChannelStatePayload>(&pkt.payload)
|
||||
&& !p.voice_endpoint.is_empty()
|
||||
&& let Ok(p) = wire::decode::<ChannelStatePayload>(&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?,
|
||||
|
|
|
|||
|
|
@ -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?;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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?;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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<UdpSocket>,
|
||||
pub seq: u64,
|
||||
struct ReceiveState {
|
||||
credentials: Credentials,
|
||||
endpoint: SocketAddr,
|
||||
replay: ReplayWindow,
|
||||
}
|
||||
pub struct State {
|
||||
udp: Arc<UdpSocket>,
|
||||
credentials: Option<Credentials>,
|
||||
endpoint: Option<SocketAddr>,
|
||||
receive: Arc<Mutex<Option<ReceiveState>>>,
|
||||
receiver: tokio::task::JoinHandle<()>,
|
||||
sequence: u64,
|
||||
}
|
||||
|
||||
impl State {
|
||||
pub async fn new(crypto: &SessionCrypto, event_tx: &mpsc::Sender<NetEvent>) -> Result<Self> {
|
||||
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<NetEvent>, ipv6: bool) -> Result<Self> {
|
||||
let udp = Arc::new(UdpSocket::bind(if ipv6 { "[::]:0" } else { "0.0.0.0:0" }).await?);
|
||||
let receive = Arc::new(Mutex::new(None::<ReceiveState>));
|
||||
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<std::net::SocketAddr>,
|
||||
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<u64>,
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@ pub async fn net_task(
|
|||
let mut target: Option<SessionTarget> = 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<NetCommand>) -> Option<Ses
|
|||
channels: vec!["general".into()],
|
||||
});
|
||||
}
|
||||
Some(NetCommand::Disconnect) | None => 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"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
112
src/net/transport.rs
Normal file
112
src/net/transport.rs
Normal file
|
|
@ -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<T: AsyncRead + AsyncWrite + Unpin + Send> AsyncStream for T {}
|
||||
pub type Stream = Box<dyn AsyncStream>;
|
||||
|
||||
pub struct Connected {
|
||||
pub stream: Stream,
|
||||
pub peer: std::net::SocketAddr,
|
||||
}
|
||||
pub async fn connect(address: &str) -> Result<Stream> {
|
||||
Ok(connect_with_peer(address).await?.stream)
|
||||
}
|
||||
pub async fn connect_with_peer(address: &str) -> Result<Connected> {
|
||||
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::<std::net::IpAddr>()
|
||||
.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();
|
||||
}
|
||||
}
|
||||
|
|
@ -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,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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<crate::net::payloads::E2eeDmMessagePayload>,
|
||||
},
|
||||
}
|
||||
|
||||
/// One-time management secret. Debug deliberately omits it.
|
||||
#[derive(Clone)]
|
||||
pub struct WebhookManagementResult {
|
||||
pub request_id: String,
|
||||
pub hook: Option<crate::net::wire::WebhookInfo>,
|
||||
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()
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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};
|
||||
|
|
|
|||
|
|
@ -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<crate::net::wire::VoiceSession> for Credentials {
|
||||
type Error = anyhow::Error;
|
||||
fn try_from(value: crate::net::wire::VoiceSession) -> Result<Self> {
|
||||
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<u8> {
|
||||
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<Vec<u8>> {
|
||||
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<u64>,
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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<UdpSocket>, crypto: SessionCrypto, tx: mpsc::Sender<NetEvent>) {
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
9
src/net/wire/mod.rs
Normal file
9
src/net/wire/mod.rs
Normal file
|
|
@ -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<Vec<u8>> {
|
||||
Ok(message.encode_to_vec())
|
||||
}
|
||||
pub fn decode<T: prost::Message + Default>(bytes: &[u8]) -> anyhow::Result<T> {
|
||||
Ok(T::decode(bytes)?)
|
||||
}
|
||||
455
src/ui/media.rs
455
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<std::cell::RefCell<crate::AppState>>,
|
||||
) -> 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<Weak<Controller>> = 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<Vec<AudioSource>, String>),
|
||||
Capture(u64, Result<CaptureSession, String>),
|
||||
}
|
||||
pub struct Controller {
|
||||
window: ScreenShareWindow,
|
||||
viewer: ScreenViewerWindow,
|
||||
state: Weak<RefCell<crate::AppState>>,
|
||||
manager: RefCell<BroadcastManager>,
|
||||
shares: RefCell<Vec<AvailableShare>>,
|
||||
viewing: RefCell<Option<String>>,
|
||||
sources: RefCell<Vec<AudioSource>>,
|
||||
selected: RefCell<Vec<AudioSource>>,
|
||||
session: RefCell<Option<CaptureSession>>,
|
||||
|
|
@ -156,11 +194,26 @@ mod native {
|
|||
error: RefCell<Option<crate::ui::i18n::Message>>,
|
||||
}
|
||||
impl Controller {
|
||||
pub fn new(window: ScreenShareWindow) -> Rc<Self> {
|
||||
pub fn new(
|
||||
window: ScreenShareWindow,
|
||||
state: &Rc<RefCell<crate::AppState>>,
|
||||
) -> Result<Rc<Self>, 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<slint::SharedString> =
|
||||
selected.iter().map(|s| s.name.as_str().into()).collect();
|
||||
let labels: Vec<slint::SharedString> = 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::<slint::Rgba8Pixel>::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<slint::SharedString> = 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::<slint::Rgba8Pixel>::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::<Theme>();
|
||||
let target = dialog.global::<Theme>();
|
||||
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<RefCell<Vec<Rc<MinimalSoftwareWindow>>>>);
|
||||
impl Platform for PlatformAdapter {
|
||||
fn create_window_adapter(&self) -> Result<Rc<dyn WindowAdapter>, 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::<Rgb8Pixel>::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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,3 +7,5 @@ pub mod preferences;
|
|||
pub mod slint;
|
||||
pub mod state;
|
||||
pub mod update;
|
||||
|
||||
pub mod webhooks;
|
||||
|
|
|
|||
|
|
@ -81,6 +81,7 @@ export component ChatPanel inherits Rectangle {
|
|||
in-out property <bool> 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(); } }
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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 <image> preview;
|
||||
in-out property <bool> has-frame;
|
||||
|
|
@ -15,21 +15,33 @@ export component ScreenShareWindow inherits Window {
|
|||
in-out property <int> source-index;
|
||||
in-out property <int> audio-mode;
|
||||
in-out property <float> audio-level;
|
||||
in-out property <bool> can-broadcast;
|
||||
in-out property <bool> publishing;
|
||||
in-out property <bool> publication-starting;
|
||||
in-out property <int> viewers;
|
||||
in-out property <[string]> broadcasts;
|
||||
in-out property <int> 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 <image> frame;
|
||||
in-out property <bool> has-frame;
|
||||
in-out property <bool> audio-enabled: true;
|
||||
in-out property <string> 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; }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,8 +7,9 @@ component ThemeChoice inherits Rectangle {
|
|||
in property <color> preview-background;
|
||||
in property <color> preview-sidebar;
|
||||
in property <color> preview-ink;
|
||||
in property <color> 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; }
|
||||
|
|
|
|||
126
src/ui/slint/webhooks.slint
Normal file
126
src/ui/slint/webhooks.slint
Normal file
|
|
@ -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 <string> channel-name;
|
||||
in property <[WebhookRow]> hooks;
|
||||
in property <bool> allowed;
|
||||
in property <bool> busy;
|
||||
in property <string> error;
|
||||
in property <string> secret;
|
||||
in property <string> secret-name;
|
||||
in property <string> secret-path;
|
||||
in property <bool> secret-outgoing;
|
||||
in property <string> confirmation-name;
|
||||
in property <bool> confirming;
|
||||
in property <bool> confirming-revoke;
|
||||
in-out property <string> new-name;
|
||||
in-out property <string> new-endpoint;
|
||||
in-out property <int> 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(); } }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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())],
|
||||
|
|
|
|||
287
src/ui/webhooks.rs
Normal file
287
src/ui/webhooks.rs
Normal file
|
|
@ -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<dyn Fn(NetCommand)>;
|
||||
/// Keep this bridge alive for the lifetime of the application. The caller feeds correlated network responses.
|
||||
pub struct WebhookUi {
|
||||
window: WebhookWindow,
|
||||
controller: Rc<RefCell<Controller>>,
|
||||
send: Send,
|
||||
_timer: slint::Timer,
|
||||
}
|
||||
impl WebhookUi {
|
||||
pub fn new(send: Send) -> Result<Self, slint::PlatformError> {
|
||||
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<WebhookInfo>) {
|
||||
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<WebhookInfo>, 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::<Vec<_>>();
|
||||
window.set_hooks(ModelRc::from(Rc::new(VecModel::from(rows))));
|
||||
}
|
||||
fn sync_palette(main: &MainWindow, dialog: &WebhookWindow) {
|
||||
let source = main.global::<Theme>();
|
||||
let target = dialog.global::<Theme>();
|
||||
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());
|
||||
}
|
||||
|
|
@ -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,
|
||||
|
|
|
|||
409
src/webhooks/controller.rs
Normal file
409
src/webhooks/controller.rs
Normal file
|
|
@ -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<Self> {
|
||||
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<u8>);
|
||||
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<Scope>,
|
||||
connected: bool,
|
||||
open: bool,
|
||||
allowed: bool,
|
||||
hooks: Vec<WebhookInfo>,
|
||||
pending: Option<Pending>,
|
||||
secret: Option<Secret>,
|
||||
secret_hook: Option<WebhookInfo>,
|
||||
confirmation: Option<(WebhookOperation, String)>,
|
||||
error: Option<Error>,
|
||||
}
|
||||
impl Controller {
|
||||
pub fn scope(&self) -> Option<&Scope> {
|
||||
self.scope.as_ref()
|
||||
}
|
||||
pub fn hooks(&self) -> &[WebhookInfo] {
|
||||
&self.hooks
|
||||
}
|
||||
pub fn error(&self) -> Option<Error> {
|
||||
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<NetCommand, Error> {
|
||||
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<T>(&mut self, error: Error) -> Result<T, Error> {
|
||||
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<NetCommand, Error> {
|
||||
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<NetCommand, Error> {
|
||||
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<NetCommand, Error> {
|
||||
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<Pending> {
|
||||
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<WebhookInfo>) -> 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<WebhookInfo>,
|
||||
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
|
||||
}
|
||||
}
|
||||
5
src/webhooks/mod.rs
Normal file
5
src/webhooks/mod.rs
Normal file
|
|
@ -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;
|
||||
172
src/webhooks/tests.rs
Normal file
172
src/webhooks/tests.rs
Normal file
|
|
@ -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));
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue