webrtc_unreliable/
server.rs

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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
use std::{
    collections::{hash_map::Entry as HashMapEntry, HashMap, VecDeque},
    convert::AsRef,
    error::Error,
    fmt,
    future::Future,
    io::{Error as IoError, ErrorKind as IoErrorKind},
    net::SocketAddr,
    ops::Deref,
    pin::Pin,
    sync::Arc,
    task::Poll,
    time::{Duration, Instant},
};

use futures_channel::mpsc;
use futures_core::Stream;
use futures_util::{future::poll_fn, ready, select, FutureExt, SinkExt, StreamExt};
use http::{header, Response};
use openssl::ssl::SslAcceptor;
use rand::thread_rng;

use crate::{
    buffer_pool::{BufferHandle, BufferPool, OwnedBuffer},
    client::{Client, ClientError, MessageType, MAX_UDP_PAYLOAD_SIZE},
    crypto::SslConfig,
    runtime::{Runtime, UdpSocket},
    sdp::{gen_sdp_response, parse_sdp_fields, SdpFields},
    stun::{parse_stun_binding_request, write_stun_success_response},
    util::rand_string,
};

#[derive(Debug)]
pub enum SendError {
    /// Non-fatal error trying to send a message to an unknown, disconnected, or not fully
    /// established client.
    ClientNotConnected,
    /// Non-fatal error writing a WebRTC Data Channel message that is too large to fit in the
    /// maximum message length.
    IncompleteMessageWrite,
    /// I/O error on the underlying socket.  May or may not be fatal, depending on the specific
    /// error.
    Io(IoError),
}

impl fmt::Display for SendError {
    fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
        match self {
            SendError::ClientNotConnected => write!(f, "client is not connected"),
            SendError::IncompleteMessageWrite => {
                write!(f, "incomplete write of WebRTC Data Channel message")
            }
            SendError::Io(err) => fmt::Display::fmt(err, f),
        }
    }
}

impl Error for SendError {}

impl From<IoError> for SendError {
    fn from(err: IoError) -> SendError {
        SendError::Io(err)
    }
}

#[derive(Debug)]
pub enum SessionError {
    /// `SessionEndpoint` has beeen disconnected from its `Server` (the `Server` has been dropped).
    Disconnected,
    /// An error streaming the SDP descriptor
    StreamError(Box<dyn Error + Send + Sync + 'static>),
}

impl fmt::Display for SessionError {
    fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
        match self {
            SessionError::Disconnected => write!(f, "`SessionEndpoint` disconnected from `Server`"),
            SessionError::StreamError(e) => {
                write!(f, "error streaming the incoming SDP descriptor: {}", e)
            }
        }
    }
}

impl Error for SessionError {
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        match self {
            SessionError::Disconnected => None,
            SessionError::StreamError(e) => Some(e.as_ref()),
        }
    }
}

/// A reference to an internal buffer containing a received message.
pub struct MessageBuffer<'a>(BufferHandle<'a>);

impl<'a> Deref for MessageBuffer<'a> {
    type Target = Vec<u8>;

    fn deref(&self) -> &Vec<u8> {
        &self.0
    }
}

impl<'a> AsRef<[u8]> for MessageBuffer<'a> {
    fn as_ref(&self) -> &[u8] {
        &self.0
    }
}

pub struct MessageResult<'a> {
    pub message: MessageBuffer<'a>,
    pub message_type: MessageType,
    pub remote_addr: SocketAddr,
}

#[derive(Clone)]
pub struct SessionEndpoint {
    public_addr: SocketAddr,
    cert_fingerprint: Arc<String>,
    session_sender: mpsc::Sender<IncomingSession>,
}

impl SessionEndpoint {
    /// Receives an incoming SDP descriptor of an `RTCSessionDescription` from a browser, informs
    /// the corresponding `Server` of the new WebRTC session, and returns a JSON object
    /// containing two fields:
    ///   1) An `answer` field which is an SDP descriptor that can be used to construct an
    ///      `RTCSessionDescription`.
    ///   2) a `candidate` field which is a configuration object for an `RTCIceCandidate`.
    ///
    /// The returned SDP descriptor contains a digest of the x509 certificate the server will use
    /// for DTLS, and the browser will ensure that this digest matches before starting a WebRTC
    /// connection.
    pub async fn session_request<I, E, S>(
        &mut self,
        sdp_descriptor: S,
    ) -> Result<String, SessionError>
    where
        I: AsRef<[u8]>,
        E: Error + Send + Sync + 'static,
        S: Stream<Item = Result<I, E>>,
    {
        const SERVER_USER_LEN: usize = 12;
        const SERVER_PASSWD_LEN: usize = 24;

        let SdpFields { ice_ufrag, mid, .. } = parse_sdp_fields(sdp_descriptor)
            .await
            .map_err(|e| SessionError::StreamError(e.into()))?;

        let (incoming_session, response) = {
            let mut rng = thread_rng();
            let server_user = rand_string(&mut rng, SERVER_USER_LEN);
            let server_passwd = rand_string(&mut rng, SERVER_PASSWD_LEN);

            let incoming_session = IncomingSession {
                server_user: server_user.clone(),
                server_passwd: server_passwd.clone(),
                remote_user: ice_ufrag,
            };

            let response = gen_sdp_response(
                &mut rng,
                &self.cert_fingerprint,
                &self.public_addr.ip().to_string(),
                self.public_addr.ip().is_ipv6(),
                self.public_addr.port(),
                &server_user,
                &server_passwd,
                &mid,
            );

            (incoming_session, response)
        };

        self.session_sender
            .send(incoming_session)
            .await
            .map_err(|_| SessionError::Disconnected)?;
        Ok(response)
    }

    /// Convenience method which returns an `http::Response` rather than a JSON string, with the
    /// correct format headers.
    pub async fn http_session_request<I, E, S>(
        &mut self,
        sdp_descriptor: S,
    ) -> Result<Response<String>, SessionError>
    where
        I: AsRef<[u8]>,
        E: Error + Send + Sync + 'static,
        S: Stream<Item = Result<I, E>>,
    {
        let r = self.session_request(sdp_descriptor).await?;
        Ok(Response::builder()
            .header(header::CONTENT_TYPE, "application/json")
            .body(r)
            .expect("could not construct session response"))
    }
}

pub struct Server<R: Runtime> {
    runtime: R,
    udp_socket: R::UdpSocket,
    session_endpoint: SessionEndpoint,
    incoming_session_stream: mpsc::Receiver<IncomingSession>,
    ssl_acceptor: Arc<SslAcceptor>,
    outgoing_udp: VecDeque<(OwnedBuffer, SocketAddr)>,
    incoming_rtc: VecDeque<(OwnedBuffer, SocketAddr, MessageType)>,
    buffer_pool: BufferPool,
    sessions: HashMap<SessionKey, Session>,
    clients: HashMap<SocketAddr, Client>,
    last_generate_periodic: Instant,
    last_cleanup: Instant,
    periodic_timer: Pin<Box<R::Timer>>,
}

impl<R: Runtime> Server<R> {
    /// Start a new WebRTC data channel server listening on `listen_addr` and advertising its
    /// publicly available address as `public_addr`.
    ///
    /// WebRTC connections must be started via an external communication channel from a browser via
    /// the `SessionEndpoint`, after which a WebRTC data channel can be opened.
    pub fn new(
        runtime: R,
        listen_addr: SocketAddr,
        public_addr: SocketAddr,
    ) -> Result<Self, IoError> {
        Server::with_ssl_config(
            runtime,
            listen_addr,
            public_addr,
            SslConfig::create().expect("WebRTC server could not initialize OpenSSL primitives"),
        )
    }

    /// Start a new WebRTC data channel server with the given `SslConfig`.
    ///
    /// This can be used to share self-signed TLS certificates between different `Server` instances,
    /// which is important in certain browsers (Firefox) when connecting to multiple WebRTC
    /// endpoints from the same page.
    pub fn with_ssl_config(
        runtime: R,
        listen_addr: SocketAddr,
        public_addr: SocketAddr,
        crypto: SslConfig,
    ) -> Result<Self, IoError> {
        const SESSION_BUFFER_SIZE: usize = 8;

        let udp_socket = runtime.bind_udp(listen_addr)?;

        let (session_sender, session_receiver) = mpsc::channel(SESSION_BUFFER_SIZE);

        log::info!(
            "new WebRTC data channel server listening on {}, public addr {}",
            listen_addr,
            public_addr
        );

        let session_endpoint = SessionEndpoint {
            public_addr,
            cert_fingerprint: Arc::new(crypto.fingerprint),
            session_sender,
        };

        let periodic_timer = Box::pin(runtime.timer(PERIODIC_TIMER_INTERVAL));

        Ok(Server {
            runtime,
            udp_socket,
            session_endpoint,
            incoming_session_stream: session_receiver,
            ssl_acceptor: crypto.ssl_acceptor,
            outgoing_udp: VecDeque::new(),
            incoming_rtc: VecDeque::new(),
            buffer_pool: BufferPool::new(),
            sessions: HashMap::new(),
            clients: HashMap::new(),
            last_generate_periodic: Instant::now(),
            last_cleanup: Instant::now(),
            periodic_timer,
        })
    }

    /// Returns a `SessionEndpoint` which can be used to start new WebRTC sessions.
    ///
    /// WebRTC connections must be started via an external communication channel from a browser via
    /// the returned `SessionEndpoint`, and this communication channel will be used to exchange
    /// session descriptions in SDP format.
    ///
    /// The returned `SessionEndpoint` will notify this `Server` of new sessions via a shared async
    /// channel.  This is done so that the `SessionEndpoint` is easy to use in a separate server
    /// task (such as a `hyper` HTTP server).
    pub fn session_endpoint(&self) -> SessionEndpoint {
        self.session_endpoint.clone()
    }

    /// The total count of clients in any active state, whether still starting up, fully
    /// established, or still shutting down.
    pub fn active_clients(&self) -> usize {
        self.clients.values().filter(|c| !c.is_shutdown()).count()
    }

    /// List all the currently fully established client connections.
    pub fn connected_clients(&self) -> impl Iterator<Item = &SocketAddr> + '_ {
        self.clients.iter().filter_map(|(addr, client)| {
            if client.is_established() {
                Some(addr)
            } else {
                None
            }
        })
    }

    /// Returns true if the client has a completely established WebRTC data channel connection and
    /// can send messages back and forth.  Returns false for disconnected clients as well as those
    /// that are still starting up or are in the process of shutting down.
    pub fn is_connected(&self, remote_addr: &SocketAddr) -> bool {
        if let Some(client) = self.clients.get(remote_addr) {
            client.is_established()
        } else {
            false
        }
    }

    /// Disconect the given client, does nothing if the client is not currently connected.
    pub async fn disconnect(&mut self, remote_addr: &SocketAddr) -> Result<(), IoError> {
        if let Some(client) = self.clients.get_mut(remote_addr) {
            match client.start_shutdown() {
                Ok(true) => {
                    log::info!("starting shutdown for client {}", remote_addr);
                }
                Ok(false) => {}
                Err(err) => {
                    log::warn!(
                        "error starting shutdown for client {}: {}",
                        remote_addr,
                        err
                    );
                }
            }

            self.outgoing_udp
                .extend(client.take_outgoing_packets().map(|p| (p, *remote_addr)));
            self.send_outgoing().await?
        }

        Ok(())
    }

    /// Send the given message to the given remote client, if they are connected.
    ///
    /// The given message must be less than `MAX_MESSAGE_LEN`.
    pub async fn send(
        &mut self,
        message: &[u8],
        message_type: MessageType,
        remote_addr: &SocketAddr,
    ) -> Result<(), SendError> {
        let client = self
            .clients
            .get_mut(remote_addr)
            .ok_or(SendError::ClientNotConnected)?;

        match client.send_message(message_type, message) {
            Err(ClientError::NotConnected) | Err(ClientError::NotEstablished) => {
                return Err(SendError::ClientNotConnected).into();
            }
            Err(ClientError::IncompletePacketWrite) => {
                return Err(SendError::IncompleteMessageWrite).into();
            }
            Err(err) => {
                log::warn!(
                    "message send for client {} generated unexpected error, shutting down: {}",
                    remote_addr,
                    err
                );
                let _ = client.start_shutdown();
                return Err(SendError::ClientNotConnected).into();
            }
            Ok(()) => {}
        }

        self.outgoing_udp
            .extend(client.take_outgoing_packets().map(|p| (p, *remote_addr)));
        Ok(self.send_outgoing().await?)
    }

    /// Receive a WebRTC data channel message from any connected client.
    ///
    /// `Server::recv` *must* be called for proper operation of the server, as it also handles
    /// background tasks such as responding to STUN packets and timing out existing sessions.
    ///
    /// If the provided buffer is not large enough to hold the received message, the received
    /// message will be truncated, and the original length will be returned as part of
    /// `MessageResult`.
    pub async fn recv(&mut self) -> Result<MessageResult<'_>, IoError> {
        while self.incoming_rtc.is_empty() {
            self.process().await?;
        }

        let (message, remote_addr, message_type) = self.incoming_rtc.pop_front().unwrap();
        let message = MessageBuffer(self.buffer_pool.adopt(message));
        return Ok(MessageResult {
            message,
            message_type,
            remote_addr,
        });
    }

    // Accepts new incoming WebRTC sessions, times out existing WebRTC sessions, sends outgoing UDP
    // packets, receives incoming UDP packets, and responds to STUN packets.
    async fn process(&mut self) -> Result<(), IoError> {
        enum Next {
            IncomingSession(IncomingSession),
            IncomingPacket(usize, SocketAddr),
            PeriodicTimer,
        }

        let mut packet_buffer = self.buffer_pool.acquire();
        packet_buffer.resize(MAX_UDP_PAYLOAD_SIZE, 0);
        let next = {
            let recv_udp = {
                let udp_socket = &mut self.udp_socket;
                let packet_buffer = &mut packet_buffer;
                poll_fn(move |cx| udp_socket.poll_recv_from(cx, packet_buffer)).fuse()
            };

            let next_timer = {
                let runtime = &self.runtime;
                let periodic_timer = &mut self.periodic_timer;
                poll_fn(move |cx| {
                    ready!(periodic_timer.as_mut().poll(cx));
                    periodic_timer.set(runtime.timer(PERIODIC_TIMER_INTERVAL));
                    Poll::Ready(())
                })
                .fuse()
            };

            select! {
                incoming_session = self.incoming_session_stream.next() => {
                    Next::IncomingSession(
                        incoming_session.expect("connection to SessionEndpoint has closed")
                    )
                }
                res = { recv_udp } => {
                    let (len, remote_addr) = res?;
                    Next::IncomingPacket(len, remote_addr)
                }
                _ = { next_timer } => {
                    Next::PeriodicTimer
                }
            }
        };

        match next {
            Next::IncomingSession(incoming_session) => {
                drop(packet_buffer);
                self.accept_session(incoming_session)
            }
            Next::IncomingPacket(len, remote_addr) => {
                if len > MAX_UDP_PAYLOAD_SIZE {
                    return Err(IoError::new(
                        IoErrorKind::Other,
                        "failed to read entire datagram from socket",
                    ));
                }
                packet_buffer.truncate(len);
                let packet_buffer = packet_buffer.into_owned();
                self.receive_packet(remote_addr, packet_buffer);
                self.send_outgoing().await?;
            }
            Next::PeriodicTimer => {
                drop(packet_buffer);
                self.timeout_clients();
                self.generate_periodic_packets();
                self.send_outgoing().await?;
            }
        }

        Ok(())
    }

    // Send all pending outgoing UDP packets
    async fn send_outgoing(&mut self) -> Result<(), IoError> {
        while let Some((packet, remote_addr)) = self.outgoing_udp.pop_front() {
            let packet = self.buffer_pool.adopt(packet);
            let len = poll_fn({
                let udp_socket = &mut self.udp_socket;
                let packet = &packet;
                move |cx| udp_socket.poll_send_to(cx, packet, remote_addr)
            })
            .await?;
            let packet_len = packet.len();
            if len != packet_len {
                return Err(IoError::new(
                    IoErrorKind::Other,
                    "failed to write entire datagram to socket",
                ));
            }
        }
        Ok(())
    }

    // Handle a single incoming UDP packet, either by responding to it as a STUN binding request or
    // by handling it as part of an existing WebRTC connection.
    fn receive_packet(&mut self, remote_addr: SocketAddr, packet_buffer: OwnedBuffer) {
        let mut packet_buffer = self.buffer_pool.adopt(packet_buffer);
        if let Some(stun_binding_request) = parse_stun_binding_request(&packet_buffer[..]) {
            if let Some(session) = self.sessions.get_mut(&SessionKey {
                server_user: stun_binding_request.server_user,
                remote_user: stun_binding_request.remote_user,
            }) {
                session.ttl = Instant::now();
                packet_buffer.resize(MAX_UDP_PAYLOAD_SIZE, 0);
                let resp_len = write_stun_success_response(
                    stun_binding_request.transaction_id,
                    remote_addr,
                    session.server_passwd.as_bytes(),
                    &mut packet_buffer,
                )
                .expect("could not write stun response");

                packet_buffer.truncate(resp_len);
                self.outgoing_udp
                    .push_back((packet_buffer.into_owned(), remote_addr));

                match self.clients.entry(remote_addr) {
                    HashMapEntry::Vacant(vacant) => {
                        log::info!(
                            "beginning client data channel connection with {}",
                            remote_addr,
                        );

                        vacant.insert(
                            Client::new(&self.ssl_acceptor, self.buffer_pool.clone(), remote_addr)
                                .expect("could not create new client instance"),
                        );
                    }
                    HashMapEntry::Occupied(_) => {}
                }
            }
        } else {
            if let Some(client) = self.clients.get_mut(&remote_addr) {
                if let Err(err) = client.receive_incoming_packet(packet_buffer.into_owned()) {
                    if !client.shutdown_started() {
                        log::warn!(
                            "client {} had unexpected error receiving UDP packet, shutting down: {}",
                            remote_addr, err
                        );
                        let _ = client.start_shutdown();
                    }
                }
                self.outgoing_udp
                    .extend(client.take_outgoing_packets().map(|p| (p, remote_addr)));
                self.incoming_rtc.extend(
                    client
                        .receive_messages()
                        .map(|(message_type, message)| (message, remote_addr, message_type)),
                );
            }
        }
    }

    // Call `Client::generate_periodic` on all clients, if we are due to do so.
    fn generate_periodic_packets(&mut self) {
        if self.last_generate_periodic.elapsed() >= PERIODIC_PACKET_INTERVAL {
            self.last_generate_periodic = Instant::now();

            for (remote_addr, client) in &mut self.clients {
                if let Err(err) = client.generate_periodic() {
                    if !client.shutdown_started() {
                        log::warn!("error for client {}, shutting down: {}", remote_addr, err);
                        let _ = client.start_shutdown();
                    }
                }
                self.outgoing_udp
                    .extend(client.take_outgoing_packets().map(|p| (p, *remote_addr)));
            }
        }
    }

    // Clean up all client sessions / connections, if we are due to do so.
    fn timeout_clients(&mut self) {
        if self.last_cleanup.elapsed() >= CLEANUP_INTERVAL {
            self.last_cleanup = Instant::now();
            self.sessions.retain(|session_key, session| {
                if session.ttl.elapsed() < RTC_SESSION_TIMEOUT {
                    true
                } else {
                    log::info!(
                        "session timeout for server user '{}' and remote user '{}'",
                        session_key.server_user,
                        session_key.remote_user
                    );
                    false
                }
            });

            self.clients.retain(|remote_addr, client| {
                if !client.is_shutdown()
                    && client.last_activity().elapsed() < RTC_CONNECTION_TIMEOUT
                {
                    true
                } else {
                    if !client.is_shutdown() {
                        log::info!("connection timeout for client {}", remote_addr);
                    }
                    log::info!("client {} removed", remote_addr);
                    false
                }
            });
        }
    }

    fn accept_session(&mut self, incoming_session: IncomingSession) {
        log::info!(
            "session initiated with server user: '{}' and remote user: '{}'",
            incoming_session.server_user,
            incoming_session.remote_user
        );

        self.sessions.insert(
            SessionKey {
                server_user: incoming_session.server_user,
                remote_user: incoming_session.remote_user,
            },
            Session {
                server_passwd: incoming_session.server_passwd,
                ttl: Instant::now(),
            },
        );
    }
}

const RTC_CONNECTION_TIMEOUT: Duration = Duration::from_secs(30);
const RTC_SESSION_TIMEOUT: Duration = Duration::from_secs(30);
const CLEANUP_INTERVAL: Duration = Duration::from_secs(10);
const PERIODIC_PACKET_INTERVAL: Duration = Duration::from_secs(1);
const PERIODIC_TIMER_INTERVAL: Duration = Duration::from_secs(1);

#[derive(Eq, PartialEq, Hash, Clone, Debug)]
struct SessionKey {
    server_user: String,
    remote_user: String,
}

struct Session {
    server_passwd: String,
    ttl: Instant,
}

struct IncomingSession {
    pub server_user: String,
    pub server_passwd: String,
    pub remote_user: String,
}