rport 0.2.49

A p2p port forwarding client using WebRTC datachannels
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
use std::sync::Arc;
use std::sync::atomic::{AtomicU64, Ordering};
use std::time::Duration;

use anyhow::{anyhow, Result};
use bytes::Bytes;
use rustrtc::{
    transports::sctp::{DataChannel, DataChannelConfig, DataChannelEvent},
    IceCandidate, IceGatheringState, PeerConnection, PeerConnectionEvent, SdpType, SessionDescription,
};
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::net::TcpListener;
use tracing::{debug, error, info, warn};

use crate::config::{ForwardMapping, IceServerConfig, RportConfig};
use crate::dtls_signaling::{send_message, DtlsClient, SignalingMessage, Target};
use crate::webrtc_config::WebRTCConfig;
use uuid::Uuid;

const DRAIN_TIMEOUT: Duration = Duration::from_secs(5);

#[derive(Debug, Default)]
pub struct ForwardStats {
    pub bytes_sent: AtomicU64,
    pub bytes_recv: AtomicU64,
    pub packets_sent: AtomicU64,
    pub packets_recv: AtomicU64,
}

#[allow(dead_code)]
pub fn spawn_stats_reporter(label: &str, stats: Arc<ForwardStats>) {
    let label = label.to_string();
    tokio::spawn(async move {
        let mut prev_sent = 0u64;
        let mut prev_recv = 0u64;
        loop {
            tokio::time::sleep(Duration::from_secs(10)).await;
            let sent = stats.bytes_sent.load(Ordering::Relaxed);
            let recv = stats.bytes_recv.load(Ordering::Relaxed);
            let p_sent = stats.packets_sent.load(Ordering::Relaxed);
            let p_recv = stats.packets_recv.load(Ordering::Relaxed);
            let d_sent = sent.saturating_sub(prev_sent);
            let d_recv = recv.saturating_sub(prev_recv);
            let sent_kbps = d_sent as f64 / 10.0 / 1024.0;
            let recv_kbps = d_recv as f64 / 10.0 / 1024.0;
            tracing::info!(
                "[stats] {} | sent: {}B ({} pkts, {:.1} KB/s) recv: {}B ({} pkts, {:.1} KB/s) total: {}B↑ {}B↓",
                label, sent, p_sent, sent_kbps, recv, p_recv, recv_kbps, sent, recv,
            );
            prev_sent = sent;
            prev_recv = recv;
        }
    });
}

//=== Generic stream ↔ WebRTC forwarder ===

pub async fn forward_stream_to_webrtc<R, W>(
    peer_connection: Arc<PeerConnection>,
    data_channel: Arc<DataChannel>,
    connect_timeout: Option<u32>,
    stats: Option<Arc<ForwardStats>>,
    mut input: R,
    mut output: W,
    mut remote_msg_rx: tokio::sync::mpsc::UnboundedReceiver<Bytes>,
) -> Result<()>
where
    R: tokio::io::AsyncRead + Unpin + Send + 'static,
    W: tokio::io::AsyncWrite + Unpin + Send + 'static,
{
    let (open_tx, open_rx) = tokio::sync::oneshot::channel();
    let (msg_tx, mut msg_rx) = tokio::sync::mpsc::unbounded_channel();
    let dc_closed = tokio_util::sync::CancellationToken::new();

    let dc_clone = data_channel.clone();
    let pc_disc = peer_connection.clone();
    let dc_closed_tx = dc_closed.clone();
    let stats_clone = stats.clone();
    tokio::spawn(async move {
        let mut open_tx = Some(open_tx);
        while let Some(event) = dc_clone.recv().await {
            match event {
                DataChannelEvent::Open => {
                    debug!("Local data channel Open event");
                    if let Some(tx) = open_tx.take() { let _ = tx.send(()); }
                }
                DataChannelEvent::Message(data) => {
                    debug!("Local data channel received {} bytes", data.len());
                    if let Some(ref s) = stats_clone {
                        s.bytes_recv.fetch_add(data.len() as u64, Ordering::Relaxed);
                        s.packets_recv.fetch_add(1, Ordering::Relaxed);
                    }
                    let _ = msg_tx.send(data);
                }
                DataChannelEvent::Close => {
                    if let Some(reason) = pc_disc.disconnect_reason() {
                        tracing::warn!("Data channel closed (reason: {})", reason);
                    }
                    dc_closed_tx.cancel();
                    break;
                }
            }
        }
    });

    let connect_timeout = connect_timeout.unwrap_or(30);
    debug!("Waiting for data channel to open (timeout: {}s)...", connect_timeout);
    if let Err(_) = tokio::time::timeout(Duration::from_secs(connect_timeout.into()), open_rx).await {
        return Err(anyhow!("Data channel open timeout"));
    }
    debug!("Data channel is open, starting forwarding");

    const DISCONNECT_GRACE: Duration = Duration::from_secs(15);
    let pc_monitor = peer_connection.clone();
    let webrtc_dead = tokio_util::sync::CancellationToken::new();
    let webrtc_dead_tx = webrtc_dead.clone();
    tokio::spawn(async move {
        let mut state_rx = pc_monitor.subscribe_peer_state();
        loop {
            if state_rx.changed().await.is_err() { return; }
            let state = *state_rx.borrow();
            match state {
                rustrtc::PeerConnectionState::Failed | rustrtc::PeerConnectionState::Closed => {
                    if let Some(reason) = pc_monitor.disconnect_reason() {
                        tracing::warn!("WebRTC connection lost: {} (state: {:?})", reason, state);
                    } else { tracing::warn!("WebRTC connection lost: state {:?}", state); }
                    webrtc_dead_tx.cancel(); return;
                }
                rustrtc::PeerConnectionState::Disconnected => {
                    tracing::warn!("WebRTC disconnected; waiting up to {:?} for recovery", DISCONNECT_GRACE);
                    let deadline = tokio::time::Instant::now() + DISCONNECT_GRACE;
                    let mut recovered = false;
                    loop {
                        tokio::select! {
                            changed = state_rx.changed() => {
                                if changed.is_err() { break; }
                                match *state_rx.borrow() {
                                    rustrtc::PeerConnectionState::Connected => { recovered = true; break; }
                                    rustrtc::PeerConnectionState::Failed | rustrtc::PeerConnectionState::Closed => {
                                        if let Some(reason) = pc_monitor.disconnect_reason() {
                                            tracing::warn!("WebRTC connection lost during grace: {}", reason);
                                        }
                                        webrtc_dead_tx.cancel(); return;
                                    }
                                    _ => {}
                                }
                            }
                            _ = tokio::time::sleep_until(deadline) => { break; }
                        }
                    }
                    if !recovered {
                        if let Some(reason) = pc_monitor.disconnect_reason() {
                            tracing::warn!("WebRTC did not recover within {:?}, giving up: {}", DISCONNECT_GRACE, reason);
                        } else { tracing::warn!("WebRTC did not recover within {:?}, giving up", DISCONNECT_GRACE); }
                        webrtc_dead_tx.cancel(); return;
                    }
                }
                _ => {}
            }
        }
    });

    let pc_clone = peer_connection.clone();
    let dc_id = data_channel.id;
    let stats_input = stats.clone();
    let input_task = async move {
        let mut buffer = [0u8; 1200];
        loop {
            match input.read(&mut buffer).await {
                Ok(0) => { debug!("forward_stream_to_webrtc: input EOF"); break; }
                Ok(n) => {
                    debug!("Sending {} bytes to WebRTC (dc_id={})", n, dc_id);
                    if let Some(ref s) = stats_input {
                        s.bytes_sent.fetch_add(n as u64, Ordering::Relaxed);
                        s.packets_sent.fetch_add(1, Ordering::Relaxed);
                    }
                    if let Err(e) = pc_clone.send_data(dc_id, &buffer[..n]).await {
                        error!("Failed to send data through WebRTC: {}", e); break;
                    }
                }
                Err(e) => { debug!("forward_stream_to_webrtc: input read failed: {}", e); break; }
            }
        }
    };

    let mut output_task = tokio::spawn(async move {
        loop {
            tokio::select! {
                data = msg_rx.recv() => {
                    match data {
                        Some(data) => {
                            debug!("Writing {} bytes to TCP (from local DC)", data.len());
                            if output.write_all(&data).await.is_err() { break; }
                            if output.flush().await.is_err() { break; }
                        }
                        None => break,
                    }
                }
                data = remote_msg_rx.recv() => {
                    match data {
                        Some(data) => {
                            debug!("Writing {} bytes to TCP (from remote DC)", data.len());
                            if output.write_all(&data).await.is_err() { break; }
                            if output.flush().await.is_err() { break; }
                        }
                        None => break,
                    }
                }
            }
        }
    });

    tokio::select! {
        _ = webrtc_dead.cancelled() => { tracing::debug!("forward_stream_to_webrtc: exiting due to WebRTC disconnect"); }
        _ = dc_closed.cancelled() => { tracing::debug!("forward_stream_to_webrtc: data channel closed by remote"); }
        _ = input_task => {
            tracing::debug!("forward_stream_to_webrtc: input closed, waiting for drain");
            tokio::select! {
                _ = tokio::time::sleep(DRAIN_TIMEOUT) => {}
                _ = dc_closed.cancelled() => {}
                _ = &mut output_task => {}
            }
        }
        _ = &mut output_task => { tracing::debug!("forward_stream_to_webrtc: output closed"); }
    }
    Ok(())
}

//=== CliClient ===

pub struct CliClient {
    server_url: String,
    token: String,
    agent_id: String,
    webrtc_config: WebRTCConfig,
    wait_candidates: bool,
}

impl CliClient {
    pub fn new(
        server_url: &str,
        token: &str,
        agent_id: &str,
        ice_servers: Option<Vec<IceServerConfig>>,
        enable_upnp: bool,
        wait_candidates: bool,
        cfg: &RportConfig,
    ) -> Self {
        let webrtc_config = WebRTCConfig::new(
            server_url.to_string(),
            token.to_string(),
            ice_servers.unwrap_or_default(),
            enable_upnp,
            cfg,
        );
        Self {
            server_url: server_url.to_string(),
            token: token.to_string(),
            agent_id: agent_id.to_string(),
            webrtc_config,
            wait_candidates,
        }
    }

    //=== ProxyCommand mode ===

    pub async fn connect_proxy_command(
        &self,
        connect_timeout: Option<u32>,
        target_host: &str,
        target_port: u16,
    ) -> Result<()> {
        info!("ProxyCommand: agent '{}' target {}:{}", self.agent_id, target_host, target_port);
        let (pc, dc, remote_rx) = self.establish_webrtc(target_host, target_port).await?;
        forward_stream_to_webrtc(
            pc, dc, connect_timeout, None,
            tokio::io::stdin(), tokio::io::stdout(), remote_rx,
        ).await
    }

    //=== Port forward mode ===

    pub async fn connect_port_forwards(
        &self,
        connect_timeout: Option<u32>,
        forwards: &[ForwardMapping],
    ) -> Result<()> {
        let all_stats = Arc::new(ForwardStats::default());

        for fwd in forwards {
            let local_port = fwd.local_port.ok_or_else(|| {
                anyhow!("Port forward requires local port in -L spec")
            })?;
            let host = fwd.host.clone();
            let port = fwd.port;
            let webrtc_config = self.webrtc_config.clone();
            let srv = self.server_url.clone();
            let tok = self.token.clone();
            let agent_id = self.agent_id.clone();
            let stats = all_stats.clone();
            let timeout = connect_timeout;
            let wait_candidates = self.wait_candidates;

            tokio::spawn(async move {
                let listener = match TcpListener::bind(format!("127.0.0.1:{}", local_port)).await {
                    Ok(l) => l,
                    Err(e) => {
                        error!("Failed to bind local port {}: {}", local_port, e);
                        return;
                    }
                };
                info!("Port forward: listening on localhost:{} -> agent '{}' -> {}:{}",
                      local_port, agent_id, host, port);

                loop {
                    match listener.accept().await {
                        Ok((tcp_stream, addr)) => {
                            info!("New connection from {}", addr);
                            let (reader, writer) = tcp_stream.into_split();
                            let client = CliClient {
                                server_url: srv.clone(),
                                token: tok.clone(),
                                agent_id: agent_id.clone(),
                                webrtc_config: webrtc_config.clone(),
                                wait_candidates,
                            };
                            let stats = stats.clone();
                            let host_clone = host.clone();
                            tokio::spawn(async move {
                                match client.establish_webrtc(&host_clone, port).await {
                                    Ok((pc, dc, remote_rx)) => {
                                        if let Err(e) = forward_stream_to_webrtc(
                                            pc, dc, timeout, Some(stats), reader, writer, remote_rx,
                                        ).await {
                                            error!("Forwarding error: {}", e);
                                        }
                                    }
                                    Err(e) => {
                                        error!("Failed to establish WebRTC: {}", e);
                                    }
                                }
                            });
                        }
                        Err(e) => error!("Accept error: {}", e),
                    }
                }
            });
        }

        // Wait forever
        std::future::pending::<()>().await;
        Ok(())
    }

    /// Connect to DTLS server, create WebRTC offer, exchange signaling, return PC + DC
    async fn establish_webrtc(
        &self,
        target_host: &str,
        target_port: u16,
    ) -> Result<(Arc<PeerConnection>, Arc<DataChannel>, tokio::sync::mpsc::UnboundedReceiver<Bytes>)> {
        let mut dtls_client = DtlsClient::connect(&self.server_url, None).await?;
        info!("DTLS connected to signaling server {}", self.server_url);

        // Request ICE server configuration (STUN + TURN with temporary credentials)
        info!("Requesting ICE server config from signaling server");
        let _ = dtls_client.send(&SignalingMessage::GetIceServers).await;

        let extra_ice_servers: Vec<IceServerConfig> = match tokio::time::timeout(
            Duration::from_secs(5),
            dtls_client.recv(),
        ).await {
            Ok(Ok(SignalingMessage::IceServers { ice_servers })) => {
                info!("Received ICE server config ({} servers) from signaling server", ice_servers.len());
                ice_servers.into_iter().map(|s| IceServerConfig {
                    urls: s.urls,
                    username: s.username,
                    credential: s.credential,
                }).collect()
            }
            Ok(Ok(other)) => {
                warn!("Expected IceServers after GetIceServers, got {:?}, using defaults", other);
                vec![]
            }
            Ok(Err(e)) => {
                warn!("Error receiving ICE servers: {}, using defaults", e);
                vec![]
            }
            Err(_) => {
                info!("No ICE server config from server (timeout), using defaults");
                vec![]
            }
        };

        let peer_connection = self.webrtc_config.create_peer_connection_with(&extra_ice_servers).await?;

        let label = format!("fwd:{}:{}", target_host, target_port);
        let dc_config = DataChannelConfig {
            ordered: true,
            label: label.clone(),
            ..Default::default()
        };
        let data_channel = peer_connection.create_data_channel(&label, Some(dc_config))?;

        let (remote_msg_tx, remote_msg_rx) = tokio::sync::mpsc::unbounded_channel::<Bytes>();

        let pc_drain = peer_connection.clone();
        let rt = remote_msg_tx.clone();
        let drain_label = label.clone();
        tokio::spawn(async move {
            while let Some(event) = pc_drain.recv().await {
                if let PeerConnectionEvent::DataChannel(dc) = event {
                    let label = dc.label.clone();
                    info!("Received remote data channel from agent: {}", label);
                    let tx = rt.clone();
                    tokio::spawn(async move {
                        while let Some(event) = dc.recv().await {
                            match event {
                                DataChannelEvent::Message(data) => {
                                    debug!("Remote DC '{}' received {} bytes from agent", label, data.len());
                                    let _ = tx.send(data);
                                }
                                DataChannelEvent::Close => {
                                    debug!("Remote DC '{}' closed by agent", label);
                                    break;
                                }
                                other => {
                                    debug!("Remote DC '{}' event: {:?}", label, other);
                                }
                            }
                        }
                    });
                } else {
                    debug!("Peer connection event (non-DC) for '{}'", drain_label);
                }
            }
        });

        let offer = peer_connection.create_offer().await?;
        peer_connection.set_local_description(offer)?;

        let session_id = Uuid::new_v4().to_string();

        // Wait for all ICE candidates before sending offer so that
        // old HTTP/SSE agents (which ignore trickle ICE) get all candidates in the SDP
        if self.wait_candidates {
            info!("Waiting for ICE gathering to complete (--wait-candidates)");
            peer_connection.wait_for_gathering_complete().await;
        }

        let offer_sdp = peer_connection.local_description()
            .ok_or_else(|| anyhow!("Failed to get local description"))?
            .to_sdp_string();

        info!("Sending offer for agent '{}' session {}", self.agent_id, session_id);
        dtls_client.send(&SignalingMessage::Offer {
            session_id: session_id.clone(),
            agent_id: self.agent_id.clone(),
            offer_sdp,
            targets: Some(vec![Target {
                host: Some(target_host.to_string()),
                port: target_port,
            }]),
        }).await?;

        // Trickle ICE: forward gathered candidates as they arrive
        let mut candidate_rx = peer_connection.subscribe_ice_candidates();
        let mut gathering_state_rx = peer_connection.subscribe_ice_gathering_state();
        let dtls = dtls_client.dtls.clone();
        let sid = session_id.clone();
        tokio::spawn(async move {
            if *gathering_state_rx.borrow() == IceGatheringState::Complete {
                let _ = send_message(&dtls, &SignalingMessage::EndOfCandidates {
                    session_id: sid.clone(),
                }).await;
                return;
            }
            loop {
                tokio::select! {
                    result = candidate_rx.recv() => {
                        match result {
                            Ok(candidate) => {
                                let _ = send_message(&dtls, &SignalingMessage::Candidate {
                                    session_id: sid.clone(),
                                    candidate: candidate.to_sdp(),
                                }).await;
                                if *gathering_state_rx.borrow() == IceGatheringState::Complete {
                                    let _ = send_message(&dtls, &SignalingMessage::EndOfCandidates {
                                        session_id: sid.clone(),
                                    }).await;
                                    break;
                                }
                            }
                            Err(tokio::sync::broadcast::error::RecvError::Lagged(_)) => continue,
                            Err(tokio::sync::broadcast::error::RecvError::Closed) => break,
                        }
                    }
                    _ = gathering_state_rx.changed() => {
                        if *gathering_state_rx.borrow() == IceGatheringState::Complete {
                            let _ = send_message(&dtls, &SignalingMessage::EndOfCandidates {
                                session_id: sid.clone(),
                            }).await;
                            break;
                        }
                    }
                }
            }
        });

        // Collect all trickle ICE candidates BEFORE setting the remote description.
        // This avoids the rustrtc issue where ICE transitions to Failed if the
        // first connectivity check batch fails (no pairs), ignoring subsequent
        // trickle candidates added via add_ice_candidate.
        let mut answer_received = false;
        let mut pending_answer_sdp = None;

        loop {
            tokio::select! {
                msg = dtls_client.recv() => {
                    let msg = match msg {
                        Ok(m) => m,
                        Err(e) => {
                            debug!("Signaling connection closed during candidate gathering: {}", e);
                            break;
                        }
                    };
                    match msg {
                        SignalingMessage::Answer { answer_sdp, .. } => {
                            info!("Received answer from agent (pending set_remote_description)");
                            pending_answer_sdp = Some(answer_sdp);
                            answer_received = true;
                        }
                        SignalingMessage::Candidate { candidate, .. } => {
                            if candidate.len() > 80 {
                                debug!("Received ICE candidate (truncated): {}...", &candidate[..80]);
                            } else {
                                debug!("Received ICE candidate: {}", candidate);
                            }
                            if let Ok(c) = IceCandidate::from_sdp(&candidate) {
                                peer_connection.add_ice_candidate(c).ok();
                            }
                        }
                        SignalingMessage::EndOfCandidates { .. } => {
                            debug!("Received end-of-candidates from agent");
                            if answer_received {
                                break;
                            }
                        }
                        SignalingMessage::IceServers { .. } | SignalingMessage::GetIceServers => {
                            debug!("Ignored delayed signaling message");
                        }
                        SignalingMessage::Error { reason, .. } => {
                            dtls_client.close();
                            return Err(anyhow!("Agent rejected offer: {}", reason));
                        }
                        other => {
                            debug!("Unexpected message during signaling: {:?}", other);
                            continue;
                        }
                    }
                }
                // Safety timeout: wait up to 10s after answer for trickle candidates
                _ = tokio::time::sleep(Duration::from_secs(10)) => {
                    if answer_received {
                        debug!("Timeout waiting for end-of-candidates, proceeding with gathered candidates");
                        break;
                    }
                }
            }
        }

        // Now set the remote description — ICE starts with ALL candidates already added
        if let Some(sdp) = pending_answer_sdp {
            let answer = SessionDescription::parse(SdpType::Answer, &sdp)?;
            peer_connection.set_remote_description(answer).await?;
            info!("WebRTC handshake completed for session {}", session_id);
        }

        // Monitor PC state in background
        let pc_monitor = peer_connection.clone();
        tokio::spawn(async move {
            let mut state_rx = pc_monitor.subscribe_peer_state();
            info!("PeerConnection initial state: {:?}", *state_rx.borrow());
            while let Ok(()) = state_rx.changed().await {
                let state = *state_rx.borrow();
                match state {
                    rustrtc::PeerConnectionState::Connected => {
                        info!("WebRTC connected");
                    }
                    rustrtc::PeerConnectionState::Disconnected => {
                        warn!("WebRTC disconnected");
                    }
                    rustrtc::PeerConnectionState::Failed => {
                        let reason = pc_monitor.disconnect_reason().map(|r| r.to_string()).unwrap_or_default();
                        warn!("WebRTC failed: {}", reason);
                    }
                    rustrtc::PeerConnectionState::Closed => {
                        info!("WebRTC closed");
                    }
                    other => {
                        info!("PeerConnection state: {:?}", other);
                    }
                }
                if matches!(state, rustrtc::PeerConnectionState::Failed | rustrtc::PeerConnectionState::Closed) {
                    if let Some(reason) = pc_monitor.disconnect_reason() {
                        info!("WebRTC ended: {} (state: {:?})", reason, state);
                    } else {
                        info!("WebRTC ended: state {:?}", state);
                    }
                    break;
                }
            }
        });

        Ok((peer_connection, data_channel, remote_msg_rx))
    }
}

impl Clone for CliClient {
    fn clone(&self) -> Self {
        Self {
            server_url: self.server_url.clone(),
            token: self.token.clone(),
            agent_id: self.agent_id.clone(),
            webrtc_config: self.webrtc_config.clone(),
            wait_candidates: self.wait_candidates,
        }
    }
}