apprtc 0.4.0

AppRTC P2P/SFU Server in Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
//! Browser-facing WebSocket transport for the signaling service.
//!
//! This module owns TCP/TLS acceptance, the HTTP upgrade handshake, WebSocket framing,
//! and each browser connection's asynchronous lifetime. Protocol state remains in the
//! Sans-I/O Collider driven by [`crate::signaling_server`].

use crate::signaling_server::DriverCommand;
use futures_util::{SinkExt, StreamExt};
use rustls::ServerConfig;
use signaling::collider::ConnectionId;
use std::pin::Pin;
use std::sync::Arc;
use std::task::{Context, Poll};
use std::time::{Duration, Instant};
use tokio::io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt, ReadBuf};
use tokio::net::{TcpListener, TcpStream};
use tokio::sync::{mpsc, watch};
use tokio::task::JoinSet;
use tokio_rustls::TlsAcceptor;
use tokio_rustls::server::TlsStream;
use tokio_tungstenite::WebSocketStream;
use tokio_tungstenite::tungstenite::Message;
use tokio_tungstenite::tungstenite::handshake::derive_accept_key;
use tokio_tungstenite::tungstenite::protocol::{Role, WebSocketConfig};

const SOCKET_OUTPUT_CAPACITY: usize = 1024;
/// Maximum size of one signaling WebSocket message/frame.
///
/// SDP and trickle-ICE messages are normally only a few KiB, but a 1 MiB
/// limit leaves room for larger browser-generated descriptions without
/// allowing an unbounded allocation on the dedicated signaling host.
const MAX_WS_MESSAGE_SIZE: usize = 1024 * 1024;
/// An idle socket (no text, no ping) is closed after this long.
const WS_IDLE_TIMEOUT: Duration = Duration::from_secs(60 * 60 * 24);
/// The TLS handshake and HTTP request head must complete within this long.
const HANDSHAKE_TIMEOUT: Duration = Duration::from_secs(10);
/// Bound the graceful WebSocket close flush so one stalled peer cannot block shutdown.
const WS_CLOSE_TIMEOUT: Duration = Duration::from_secs(5);

pub enum SocketOutput {
    Text(String),
    Close,
}

/// A plain or TLS stream over one accepted `TcpStream`.
enum Stream {
    Plain(TcpStream),
    Tls(Box<TlsStream<TcpStream>>),
}

impl AsyncRead for Stream {
    fn poll_read(
        mut self: Pin<&mut Self>,
        cx: &mut Context<'_>,
        buf: &mut ReadBuf<'_>,
    ) -> Poll<std::io::Result<()>> {
        match &mut *self {
            Stream::Plain(tcp) => Pin::new(tcp).poll_read(cx, buf),
            Stream::Tls(tls) => Pin::new(tls).poll_read(cx, buf),
        }
    }
}

impl AsyncWrite for Stream {
    fn poll_write(
        mut self: Pin<&mut Self>,
        cx: &mut Context<'_>,
        buf: &[u8],
    ) -> Poll<std::io::Result<usize>> {
        match &mut *self {
            Stream::Plain(tcp) => Pin::new(tcp).poll_write(cx, buf),
            Stream::Tls(tls) => Pin::new(tls).poll_write(cx, buf),
        }
    }

    fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<std::io::Result<()>> {
        match &mut *self {
            Stream::Plain(tcp) => Pin::new(tcp).poll_flush(cx),
            Stream::Tls(tls) => Pin::new(tls).poll_flush(cx),
        }
    }

    fn poll_shutdown(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<std::io::Result<()>> {
        match &mut *self {
            Stream::Plain(tcp) => Pin::new(tcp).poll_shutdown(cx),
            Stream::Tls(tls) => Pin::new(tls).poll_shutdown(cx),
        }
    }
}

/// Accept connections until `stop_rx` fires; upgrade the supported WebSocket endpoint.
pub async fn run(
    mut stop_rx: watch::Receiver<()>,
    commands: mpsc::Sender<DriverCommand>,
    listener: TcpListener,
    tls_config: Option<Arc<ServerConfig>>,
) {
    let tls_acceptor = tls_config.map(TlsAcceptor::from);
    let mut next_connection_id: ConnectionId = 1;
    let mut connections = JoinSet::new();
    loop {
        tokio::select! {
            _ = stop_rx.changed() => break,
            Some(result) = connections.join_next(), if !connections.is_empty() => {
                if let Err(error) = result {
                    log::warn!("signaling connection task failed: {error}");
                }
            }
            accepted = listener.accept() => match accepted {
                Ok((tcp, _peer)) => {
                    let connection_id = next_connection_id;
                    next_connection_id += 1;
                    let tls_acceptor = tls_acceptor.clone();
                    let commands = commands.clone();
                    let connection_stop_rx = stop_rx.clone();
                    connections.spawn(async move {
                        if let Err(err) = handle_connection(
                            connection_stop_rx,
                            tcp,
                            tls_acceptor,
                            connection_id,
                            commands,
                        )
                        .await
                        {
                            log::trace!("connection ended: {err}");
                        }
                    });
                }
                Err(err) => log::warn!("accept error: {err}"),
            },
        }
    }
    while let Some(result) = connections.join_next().await {
        if let Err(error) = result {
            log::warn!("signaling connection task failed during shutdown: {error}");
        }
    }
    log::info!("signaling accept loop stopped");
}

/// One accepted connection: optional TLS handshake, read the HTTP request head, then
/// either upgrade `/ws` to a WebSocket or answer with HTTP status.
async fn handle_connection(
    mut stop_rx: watch::Receiver<()>,
    tcp: TcpStream,
    tls_acceptor: Option<TlsAcceptor>,
    connection_id: ConnectionId,
    commands: mpsc::Sender<DriverCommand>,
) -> anyhow::Result<()> {
    let handshake = tokio::time::timeout(HANDSHAKE_TIMEOUT, async {
        let mut stream = match tls_acceptor {
            Some(acceptor) => Stream::Tls(Box::new(acceptor.accept(tcp).await?)),
            None => Stream::Plain(tcp),
        };
        let head = read_http_head(&mut stream).await?;
        anyhow::Ok((stream, head))
    });
    let (mut stream, head) = tokio::select! {
        _ = stop_rx.changed() => {
            log::debug!("connection stopped during TLS/HTTP handshake: connection_id={connection_id}");
            return Ok(());
        }
        result = handshake => result
            .map_err(|_| anyhow::anyhow!("TLS/HTTP handshake timed out"))??,
    };
    let request = HttpRequest::parse(&head);
    log::trace!(
        "HTTP request: connection_id={connection_id} method={} path={}",
        request.method,
        request.path
    );

    let is_upgrade = request
        .header("upgrade")
        .is_some_and(|v| v.eq_ignore_ascii_case("websocket"))
        && request.header("sec-websocket-key").is_some();

    match request.path.as_str() {
        "/ws" | "/ws/" => {}
        _ => return respond(&mut stream, "404 Not Found", "not found").await,
    }
    if !is_upgrade {
        return respond(&mut stream, "426 Upgrade Required", "upgrade required").await;
    }

    let key = request.header("sec-websocket-key").unwrap();
    let accept = derive_accept_key(key.as_bytes());
    let response = format!(
        "HTTP/1.1 101 Switching Protocols\r\n\
         Upgrade: websocket\r\n\
         Connection: Upgrade\r\n\
         Sec-WebSocket-Accept: {accept}\r\n\r\n"
    );
    stream.write_all(response.as_bytes()).await?;
    stream.flush().await?;

    let config = WebSocketConfig::default()
        .max_message_size(Some(MAX_WS_MESSAGE_SIZE))
        .max_frame_size(Some(MAX_WS_MESSAGE_SIZE));
    let ws = WebSocketStream::from_raw_socket(stream, Role::Server, Some(config)).await;
    ws_session(stop_rx, ws, connection_id, commands).await;
    Ok(())
}

async fn read_http_head(stream: &mut Stream) -> anyhow::Result<Vec<u8>> {
    let mut buf = Vec::with_capacity(1024);
    let mut chunk = [0u8; 1024];
    loop {
        let n = stream.read(&mut chunk).await?;
        if n == 0 {
            anyhow::bail!("connection closed before request head");
        }
        buf.extend_from_slice(&chunk[..n]);
        if buf.windows(4).any(|w| w == b"\r\n\r\n") {
            break;
        }
        anyhow::ensure!(buf.len() <= 64 * 1024, "request head too large");
    }
    Ok(buf)
}

struct HttpRequest {
    method: String,
    path: String,
    headers: Vec<(String, String)>,
}

impl HttpRequest {
    fn parse(head: &[u8]) -> Self {
        let text = String::from_utf8_lossy(head);
        let mut lines = text.split("\r\n");
        let mut request_line = lines.next().unwrap_or("").split_whitespace();
        let method = request_line.next().unwrap_or("").to_owned();
        let path = request_line.next().unwrap_or("/").to_owned();
        let headers = lines
            .take_while(|line| !line.is_empty())
            .filter_map(|line| {
                let (key, value) = line.split_once(':')?;
                Some((key.trim().to_owned(), value.trim().to_owned()))
            })
            .collect();
        Self {
            method,
            path,
            headers,
        }
    }

    fn header(&self, name: &str) -> Option<&str> {
        self.headers
            .iter()
            .find(|(key, _)| key.eq_ignore_ascii_case(name))
            .map(|(_, value)| value.as_str())
    }
}

async fn respond(stream: &mut Stream, status: &str, body: &str) -> anyhow::Result<()> {
    let response = format!(
        "HTTP/1.1 {status}\r\n\
         Content-Type: text/plain; charset=utf-8\r\n\
         Content-Length: {}\r\n\
         Connection: close\r\n\r\n{body}",
        body.len()
    );
    stream.write_all(response.as_bytes()).await?;
    stream.flush().await?;
    Ok(())
}

/// Drive one browser's WebSocket for its lifetime.
async fn ws_session<S>(
    mut stop_rx: watch::Receiver<()>,
    ws: WebSocketStream<S>,
    connection_id: ConnectionId,
    commands: mpsc::Sender<DriverCommand>,
) where
    S: AsyncRead + AsyncWrite + Unpin,
{
    log::info!("Browser WebSocket connected: connection_id={connection_id}");
    let (output, mut outputs) = mpsc::channel::<SocketOutput>(SOCKET_OUTPUT_CAPACITY);
    if commands
        .send(DriverCommand::Connected {
            connection_id,
            output,
        })
        .await
        .is_err()
    {
        return;
    }

    let (mut writer, mut reader) = ws.split();
    let mut idle_deadline = tokio::time::Instant::now() + WS_IDLE_TIMEOUT;
    loop {
        tokio::select! {
            _ = stop_rx.changed() => {
                log::info!("Browser WebSocket shutdown requested: connection_id={connection_id}");
                let _ = tokio::time::timeout(WS_CLOSE_TIMEOUT, async {
                    writer.send(Message::Close(None)).await?;
                    writer.close().await
                }).await;
                break;
            }
            output = outputs.recv() => match output {
                Some(SocketOutput::Text(text)) => {
                    if writer.send(Message::text(text)).await.is_err() {
                        break;
                    }
                }
                Some(SocketOutput::Close) | None => {
                    let _ = writer.send(Message::Close(None)).await;
                    break;
                }
            },
            incoming = reader.next() => match incoming {
                Some(Ok(Message::Text(text))) => {
                    log::info!(
                        "WebSocket message: connection_id={connection_id} bytes={}",
                        text.len()
                    );
                    if commands.send(DriverCommand::Text {
                        connection_id,
                        text: text.to_string(),
                        now: Instant::now(),
                    }).await.is_err() {
                        break;
                    }
                    idle_deadline = tokio::time::Instant::now() + WS_IDLE_TIMEOUT;
                }
                Some(Ok(Message::Binary(bytes))) => {
                    log::warn!("Browser binary frame rejected: connection_id={connection_id} bytes={}", bytes.len());
                    break;
                }
                Some(Ok(Message::Ping(payload))) => {
                    log::info!(
                        "WebSocket keep-alive ping received: connection_id={connection_id} bytes={}",
                        payload.len()
                    );
                    idle_deadline = tokio::time::Instant::now() + WS_IDLE_TIMEOUT;
                }
                Some(Ok(Message::Pong(_))) => {}
                Some(Ok(Message::Frame(_))) => {}
                Some(Ok(Message::Close(_))) | Some(Err(_)) | None => break,
            },
            _ = tokio::time::sleep_until(idle_deadline) => break,
        }
    }

    let _ = commands
        .send(DriverCommand::Disconnected {
            connection_id,
            now: Instant::now(),
        })
        .await;
    log::info!("Browser WebSocket disconnected: connection_id={connection_id}");
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::signaling_server::COMMAND_CAPACITY;

    #[tokio::test]
    async fn websocket_session_sends_close_when_stop_signal_fires() {
        let (stop_tx, stop_rx) = watch::channel(());
        let (commands, mut command_rx) = mpsc::channel(COMMAND_CAPACITY);
        let (server_io, client_io) = tokio::io::duplex(4096);
        let (server_ws, mut client_ws) = tokio::join!(
            WebSocketStream::from_raw_socket(server_io, Role::Server, None),
            WebSocketStream::from_raw_socket(client_io, Role::Client, None),
        );

        let session = tokio::spawn(ws_session(stop_rx, server_ws, 42, commands));
        let output = match command_rx.recv().await.unwrap() {
            DriverCommand::Connected {
                connection_id: 42,
                output,
            } => output,
            _ => panic!("expected connected command"),
        };

        stop_tx.send(()).unwrap();
        let close = tokio::time::timeout(Duration::from_secs(5), client_ws.next())
            .await
            .unwrap()
            .unwrap()
            .unwrap();
        assert!(matches!(close, Message::Close(_)));
        tokio::time::timeout(Duration::from_secs(5), session)
            .await
            .unwrap()
            .unwrap();
        assert!(matches!(
            command_rx.recv().await,
            Some(DriverCommand::Disconnected {
                connection_id: 42,
                ..
            })
        ));
        drop(output);
    }
}