tx5-connection 0.8.1

holochain webrtc connection
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
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
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
use super::*;
use std::sync::atomic::Ordering;

pub(crate) enum ConnCmd {
    SigRecv(tx5_signal::SignalMessage),
    WebrtcRecv(webrtc::WebrtcEvt),
    SendMessage(Vec<u8>),
    WebrtcTimeoutCheck,
    WebrtcClosed,
}

/// Receive messages from a tx5 connection.
pub struct ConnRecv(CloseRecv<Vec<u8>>);

impl ConnRecv {
    /// Receive up to 16KiB of message data.
    pub async fn recv(&mut self) -> Option<Vec<u8>> {
        self.0.recv().await
    }
}

/// A tx5 connection.
pub struct Conn {
    ready: Arc<tokio::sync::Semaphore>,
    pub_key: PubKey,
    cmd_send: CloseSend<ConnCmd>,
    conn_task: tokio::task::JoinHandle<()>,
    keepalive_task: tokio::task::JoinHandle<()>,
    is_webrtc: Arc<std::sync::atomic::AtomicBool>,
    send_msg_count: Arc<std::sync::atomic::AtomicU64>,
    send_byte_count: Arc<std::sync::atomic::AtomicU64>,
    recv_msg_count: Arc<std::sync::atomic::AtomicU64>,
    recv_byte_count: Arc<std::sync::atomic::AtomicU64>,
    hub_cmd_send: tokio::sync::mpsc::Sender<HubCmd>,
}

macro_rules! netaudit {
    ($lvl:ident, $($all:tt)*) => {
        ::tracing::event!(
            target: "NETAUDIT",
            ::tracing::Level::$lvl,
            m = "tx5-connection",
            $($all)*
        );
    };
}

impl Drop for Conn {
    fn drop(&mut self) {
        netaudit!(DEBUG, pub_key = ?self.pub_key, a = "drop");

        self.conn_task.abort();
        self.keepalive_task.abort();

        let hub_cmd_send = self.hub_cmd_send.clone();
        let pub_key = self.pub_key.clone();
        tokio::task::spawn(async move {
            let _ = hub_cmd_send.send(HubCmd::Disconnect(pub_key)).await;
        });
    }
}

impl Conn {
    #[cfg(test)]
    pub(crate) fn test_kill_keepalive_task(&self) {
        self.keepalive_task.abort();
    }

    pub(crate) fn priv_new(
        webrtc_config: WebRtcConfig,
        is_polite: bool,
        pub_key: PubKey,
        client: Weak<tx5_signal::SignalConnection>,
        config: Arc<HubConfig>,
        hub_cmd_send: tokio::sync::mpsc::Sender<HubCmd>,
    ) -> (Arc<Self>, ConnRecv, CloseSend<ConnCmd>) {
        netaudit!(DEBUG, ?webrtc_config, ?pub_key, ?is_polite, a = "open",);

        // set up some metrics
        let is_webrtc = Arc::new(std::sync::atomic::AtomicBool::new(false));
        let send_msg_count = Arc::new(std::sync::atomic::AtomicU64::new(0));
        let send_byte_count = Arc::new(std::sync::atomic::AtomicU64::new(0));
        let recv_msg_count = Arc::new(std::sync::atomic::AtomicU64::new(0));
        let recv_byte_count = Arc::new(std::sync::atomic::AtomicU64::new(0));

        // zero len semaphore.. we actually just wait for the close
        let ready = Arc::new(tokio::sync::Semaphore::new(0));

        let (mut msg_send, msg_recv) = CloseSend::sized_channel(1024);
        let (cmd_send, cmd_recv) = CloseSend::sized_channel(1024);

        // signal keepalive task
        let keepalive_dur = config.signal_config.max_idle / 2;
        let client2 = client.clone();
        let pub_key2 = pub_key.clone();
        let keepalive_task = tokio::task::spawn(async move {
            loop {
                tokio::time::sleep(keepalive_dur).await;

                if let Some(client) = client2.upgrade() {
                    if client.send_keepalive(&pub_key2).await.is_err() {
                        break;
                    }
                } else {
                    break;
                }
            }
        });

        msg_send.set_close_on_drop(true);

        // con_task is the main event loop for a connection
        let con_task_fut = con_task(
            is_polite,
            webrtc_config,
            TaskCore {
                client,
                config,
                pub_key: pub_key.clone(),
                cmd_send: cmd_send.clone(),
                cmd_recv,
                send_msg_count: send_msg_count.clone(),
                send_byte_count: send_byte_count.clone(),
                recv_msg_count: recv_msg_count.clone(),
                recv_byte_count: recv_byte_count.clone(),
                msg_send,
                ready: ready.clone(),
                is_webrtc: is_webrtc.clone(),
            },
        );
        let conn_task = tokio::task::spawn(con_task_fut);

        let mut cmd_send2 = cmd_send.clone();
        cmd_send2.set_close_on_drop(true);
        let this = Self {
            ready,
            pub_key,
            cmd_send: cmd_send2,
            conn_task,
            keepalive_task,
            is_webrtc,
            send_msg_count,
            send_byte_count,
            recv_msg_count,
            recv_byte_count,
            hub_cmd_send,
        };

        (Arc::new(this), ConnRecv(msg_recv), cmd_send)
    }

    /// Wait until this connection is ready to send / receive data.
    pub async fn ready(&self) {
        // this will error when we close the semaphore waking up the task
        let _ = self.ready.acquire().await;
    }

    /// Returns `true` if we successfully connected over webrtc.
    pub fn is_using_webrtc(&self) -> bool {
        self.is_webrtc.load(Ordering::SeqCst)
    }

    /// The pub key of the remote peer this is connected to.
    pub fn pub_key(&self) -> &PubKey {
        &self.pub_key
    }

    /// Send up to 16KiB of message data.
    pub async fn send(&self, msg: Vec<u8>) -> Result<()> {
        self.cmd_send.send(ConnCmd::SendMessage(msg)).await
    }

    /// Get connection statistics.
    pub fn get_stats(&self) -> ConnStats {
        ConnStats {
            send_msg_count: self.send_msg_count.load(Ordering::Relaxed),
            send_byte_count: self.send_byte_count.load(Ordering::Relaxed),
            recv_msg_count: self.recv_msg_count.load(Ordering::Relaxed),
            recv_byte_count: self.recv_byte_count.load(Ordering::Relaxed),
        }
    }
}

/// Connection statistics.
#[derive(Default)]
pub struct ConnStats {
    /// message count sent.
    pub send_msg_count: u64,

    /// byte count sent.
    pub send_byte_count: u64,

    /// message count received.
    pub recv_msg_count: u64,

    /// byte count received.
    pub recv_byte_count: u64,
}

struct TaskCore {
    config: Arc<HubConfig>,
    client: Weak<tx5_signal::SignalConnection>,
    pub_key: PubKey,
    cmd_send: CloseSend<ConnCmd>,
    cmd_recv: CloseRecv<ConnCmd>,
    msg_send: CloseSend<Vec<u8>>,
    ready: Arc<tokio::sync::Semaphore>,
    is_webrtc: Arc<std::sync::atomic::AtomicBool>,
    send_msg_count: Arc<std::sync::atomic::AtomicU64>,
    send_byte_count: Arc<std::sync::atomic::AtomicU64>,
    recv_msg_count: Arc<std::sync::atomic::AtomicU64>,
    recv_byte_count: Arc<std::sync::atomic::AtomicU64>,
}

impl TaskCore {
    async fn handle_recv_msg(
        &self,
        msg: Vec<u8>,
    ) -> std::result::Result<(), ()> {
        self.recv_msg_count.fetch_add(1, Ordering::Relaxed);
        self.recv_byte_count
            .fetch_add(msg.len() as u64, Ordering::Relaxed);
        if self.msg_send.send(msg).await.is_err() {
            netaudit!(
                DEBUG,
                pub_key = ?self.pub_key,
                a = "close: msg_send closed",
            );
            Err(())
        } else {
            Ok(())
        }
    }

    fn track_send_msg(&self, len: usize) {
        self.send_msg_count.fetch_add(1, Ordering::Relaxed);
        self.send_byte_count
            .fetch_add(len as u64, Ordering::Relaxed);
    }
}

async fn con_task(
    is_polite: bool,
    webrtc_config: WebRtcConfig,
    mut task_core: TaskCore,
) {
    // first process the handshake
    if let Some(client) = task_core.client.upgrade() {
        let handshake_fut = async {
            let nonce = client.send_handshake_req(&task_core.pub_key).await?;

            let mut got_peer_res = false;
            let mut sent_our_res = false;

            while let Some(cmd) = task_core.cmd_recv.recv().await {
                match cmd {
                    ConnCmd::SigRecv(sig) => {
                        use tx5_signal::SignalMessage::*;
                        match sig {
                            HandshakeReq(oth_nonce) => {
                                client
                                    .send_handshake_res(
                                        &task_core.pub_key,
                                        oth_nonce,
                                    )
                                    .await?;
                                sent_our_res = true;
                            }
                            HandshakeRes(res_nonce) => {
                                if res_nonce != nonce {
                                    return Err(Error::other("nonce mismatch"));
                                }
                                got_peer_res = true;
                            }
                            // Ignore all other message types...
                            // they may be from previous sessions
                            _ => (),
                        }
                    }
                    ConnCmd::SendMessage(_) => {
                        return Err(Error::other("send before ready"));
                    }
                    ConnCmd::WebrtcTimeoutCheck
                    | ConnCmd::WebrtcRecv(_)
                    | ConnCmd::WebrtcClosed => {
                        // only emitted by the webrtc module
                        // which at this point hasn't yet been initialized
                        unreachable!()
                    }
                }
                if got_peer_res && sent_our_res {
                    break;
                }
            }

            Result::Ok(())
        };

        match tokio::time::timeout(
            task_core.config.signal_config.max_idle,
            handshake_fut,
        )
        .await
        {
            Err(_) | Ok(Err(_)) => {
                client.close_peer(&task_core.pub_key).await;
                return;
            }
            Ok(Ok(_)) => (),
        }
    } else {
        return;
    }

    // next, attempt webrtc
    let task_core = match con_task_attempt_webrtc(
        is_polite,
        webrtc_config,
        task_core,
    )
    .await
    {
        AttemptWebrtcResult::Abort => return,
        AttemptWebrtcResult::Fallback(task_core) => {
            if task_core.config.danger_deny_signal_relay {
                netaudit!(
                    INFO,
                    pub_key = ?task_core.pub_key,
                    a = "webrtc fallback: denied signal relay",
                );
                return;
            }

            task_core
        }
    };

    task_core.is_webrtc.store(false, Ordering::SeqCst);

    // if webrtc failed in a way that allows us to fall back to sbd,
    // use the fallback sbd messaging system
    con_task_fallback_use_signal(task_core).await;
}

async fn recv_cmd(task_core: &mut TaskCore) -> Option<ConnCmd> {
    match tokio::time::timeout(
        task_core.config.signal_config.max_idle,
        task_core.cmd_recv.recv(),
    )
    .await
    {
        Err(_) => {
            netaudit!(
                DEBUG,
                pub_key = ?task_core.pub_key,
                a = "close: connection idle",
            );
            None
        }
        Ok(None) => {
            netaudit!(
                DEBUG,
                pub_key = ?task_core.pub_key,
                a = "close: cmd_recv stream complete",
            );
            None
        }
        Ok(Some(cmd)) => Some(cmd),
    }
}

async fn webrtc_task(
    mut webrtc_recv: CloseRecv<webrtc::WebrtcEvt>,
    cmd_send: CloseSend<ConnCmd>,
) {
    while let Some(evt) = webrtc_recv.recv().await {
        if cmd_send.send(ConnCmd::WebrtcRecv(evt)).await.is_err() {
            break;
        }
    }
    netaudit!(DEBUG, a = "webrtc task closed, sending WebrtcClosed",);
    let _ = cmd_send.send(ConnCmd::WebrtcClosed).await;
}

enum AttemptWebrtcResult {
    Abort,
    Fallback(TaskCore),
}

async fn con_task_attempt_webrtc(
    is_polite: bool,
    webrtc_config: WebRtcConfig,
    mut task_core: TaskCore,
) -> AttemptWebrtcResult {
    use AttemptWebrtcResult::*;

    let timeout_dur = task_core.config.webrtc_connect_timeout;
    let timeout_cmd_send = task_core.cmd_send.clone();
    tokio::task::spawn(async move {
        tokio::time::sleep(timeout_dur).await;
        let _ = timeout_cmd_send.send(ConnCmd::WebrtcTimeoutCheck).await;
    });

    let (webrtc, webrtc_recv) = webrtc::new_backend_module(
        task_core.config.backend_module,
        is_polite,
        webrtc_config,
        // MAYBE - make this configurable
        4096,
    );

    struct AbortWebrtc(tokio::task::AbortHandle);

    impl Drop for AbortWebrtc {
        fn drop(&mut self) {
            self.0.abort();
        }
    }

    // ensure if we exit this loop that the tokio task is stopped
    let _abort_webrtc = AbortWebrtc(
        tokio::task::spawn(webrtc_task(
            webrtc_recv,
            task_core.cmd_send.clone(),
        ))
        .abort_handle(),
    );

    let mut is_ready = false;

    if task_core.config.danger_force_signal_relay {
        netaudit!(
            WARN,
            pub_key = ?task_core.pub_key,
            a = "webrtc fallback: test",
        );
        return Fallback(task_core);
    }

    // receive webrtc commands
    while let Some(cmd) = recv_cmd(&mut task_core).await {
        use tx5_signal::SignalMessage::*;
        use webrtc::WebrtcEvt::*;
        use ConnCmd::*;
        match cmd {
            SigRecv(HandshakeReq(_)) | SigRecv(HandshakeRes(_)) => {
                netaudit!(
                    DEBUG,
                    pub_key = ?task_core.pub_key,
                    a = "close: unexpected handshake msg",
                );
                break;
            }
            SigRecv(tx5_signal::SignalMessage::Message(msg)) => {
                if task_core.handle_recv_msg(msg).await.is_err() {
                    break;
                }
                netaudit!(
                    WARN,
                    pub_key = ?task_core.pub_key,
                    a = "webrtc fallback: remote sent us an sbd message",
                );
                // if we get a message from the remote, we have to assume
                // they are switching to fallback mode, and thus we cannot
                // use webrtc ourselves.
                return Fallback(task_core);
            }
            SigRecv(Offer(offer)) => {
                netaudit!(
                    TRACE,
                    pub_key = ?task_core.pub_key,
                    offer = String::from_utf8_lossy(&offer).to_string(),
                    a = "recv_offer",
                );
                if let Err(err) = webrtc.in_offer(offer).await {
                    netaudit!(
                        WARN,
                        pub_key = ?task_core.pub_key,
                        ?err,
                        a = "webrtc fallback: failed to parse received offer",
                    );
                    return Fallback(task_core);
                }
            }
            SigRecv(Answer(answer)) => {
                netaudit!(
                    TRACE,
                    pub_key = ?task_core.pub_key,
                    offer = String::from_utf8_lossy(&answer).to_string(),
                    a = "recv_answer",
                );
                if let Err(err) = webrtc.in_answer(answer).await {
                    netaudit!(
                        WARN,
                        pub_key = ?task_core.pub_key,
                        ?err,
                        a = "webrtc fallback: failed to parse received answer",
                    );
                    return Fallback(task_core);
                }
            }
            SigRecv(Ice(ice)) => {
                netaudit!(
                    TRACE,
                    pub_key = ?task_core.pub_key,
                    offer = String::from_utf8_lossy(&ice).to_string(),
                    a = "recv_ice",
                );
                if let Err(err) = webrtc.in_ice(ice).await {
                    netaudit!(
                        DEBUG,
                        pub_key = ?task_core.pub_key,
                        ?err,
                        a = "ignoring webrtc in_ice error",
                    );
                    // ice errors are often benign... just ignore it
                }
            }
            SigRecv(Keepalive) | SigRecv(Unknown) => {
                // these are no-ops
            }
            WebrtcRecv(GeneratedOffer(offer)) => {
                netaudit!(
                    TRACE,
                    pub_key = ?task_core.pub_key,
                    offer = String::from_utf8_lossy(&offer).to_string(),
                    a = "send_offer",
                );
                if let Some(client) = task_core.client.upgrade() {
                    if let Err(err) =
                        client.send_offer(&task_core.pub_key, offer).await
                    {
                        netaudit!(
                            DEBUG,
                            pub_key = ?task_core.pub_key,
                            ?err,
                            a = "webrtc send_offer error",
                        );
                        break;
                    }
                } else {
                    break;
                }
            }
            WebrtcRecv(GeneratedAnswer(answer)) => {
                netaudit!(
                    TRACE,
                    pub_key = ?task_core.pub_key,
                    offer = String::from_utf8_lossy(&answer).to_string(),
                    a = "send_answer",
                );
                if let Some(client) = task_core.client.upgrade() {
                    if let Err(err) =
                        client.send_answer(&task_core.pub_key, answer).await
                    {
                        netaudit!(
                            DEBUG,
                            pub_key = ?task_core.pub_key,
                            ?err,
                            a = "webrtc send_answer error",
                        );
                        break;
                    }
                } else {
                    break;
                }
            }
            WebrtcRecv(GeneratedIce(ice)) => {
                netaudit!(
                    TRACE,
                    pub_key = ?task_core.pub_key,
                    offer = String::from_utf8_lossy(&ice).to_string(),
                    a = "send_ice",
                );
                if let Some(client) = task_core.client.upgrade() {
                    if let Err(err) =
                        client.send_ice(&task_core.pub_key, ice).await
                    {
                        netaudit!(
                            DEBUG,
                            pub_key = ?task_core.pub_key,
                            ?err,
                            a = "webrtc send_ice error",
                        );
                        break;
                    }
                } else {
                    break;
                }
            }
            WebrtcRecv(webrtc::WebrtcEvt::Message(msg)) => {
                if task_core.handle_recv_msg(msg).await.is_err() {
                    break;
                }
            }
            WebrtcRecv(Ready) => {
                is_ready = true;
                task_core.is_webrtc.store(true, Ordering::SeqCst);
                task_core.ready.close();
            }
            SendMessage(msg) => {
                let len = msg.len();

                netaudit!(
                    TRACE,
                    pub_key = ?task_core.pub_key,
                    byte_len = len,
                    a = "queue msg for backend send",
                );
                if let Err(err) = webrtc.message(msg).await {
                    netaudit!(
                        WARN,
                        pub_key = ?task_core.pub_key,
                        ?err,
                        a = "webrtc fallback: failed to send message",
                    );
                    return Fallback(task_core);
                }

                task_core.track_send_msg(len);
            }
            WebrtcTimeoutCheck => {
                if !is_ready {
                    netaudit!(
                        WARN,
                        pub_key = ?task_core.pub_key,
                        a = "webrtc fallback: failed to ready within timeout",
                    );
                    return Fallback(task_core);
                }
            }
            WebrtcClosed => {
                netaudit!(
                    WARN,
                    pub_key = ?task_core.pub_key,
                    a = "webrtc processing task closed",
                );
                break;
            }
        }
    }

    Abort
}

async fn con_task_fallback_use_signal(mut task_core: TaskCore) {
    // closing the semaphore causes all the acquire awaits to end
    task_core.ready.close();

    while let Some(cmd) = recv_cmd(&mut task_core).await {
        match cmd {
            ConnCmd::SigRecv(tx5_signal::SignalMessage::Message(msg)) => {
                if task_core.handle_recv_msg(msg).await.is_err() {
                    break;
                }
            }
            ConnCmd::SendMessage(msg) => match task_core.client.upgrade() {
                Some(client) => {
                    let len = msg.len();
                    if let Err(err) =
                        client.send_message(&task_core.pub_key, msg).await
                    {
                        netaudit!(
                            DEBUG,
                            pub_key = ?task_core.pub_key,
                            ?err,
                            a = "close: sbd client send error",
                        );
                        break;
                    }
                    task_core.track_send_msg(len);
                }
                None => {
                    netaudit!(
                        DEBUG,
                        pub_key = ?task_core.pub_key,
                        a = "close: sbd client closed",
                    );
                    break;
                }
            },
            _ => (),
        }
    }
}