rport 0.2.56

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
use crate::{
    acl::Acl,
    config::{IceServerConfig, RportConfig},
    dtls_signaling::{DtlsClient, SignalingMessage, Target, send_message, recv_message},
    webrtc_config::WebRTCConfig,
};
use anyhow::{anyhow, Result};
use bytes::Bytes;
use rustrtc::{
    transports::{
        dtls::DtlsTransport,
        sctp::{DataChannelConfig, DataChannelEvent},
    },
    IceCandidate, IceGatheringState, PeerConnection, PeerConnectionState, SdpType,
    SessionDescription,
};
use std::collections::HashMap;
use std::sync::Arc;
use std::time::Duration;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::net::TcpStream;
use tokio::sync::mpsc;
use tracing::{debug, error, info, warn};

pub const RECONNECT_INTERVAL: u64 = 5;

/// Owns a WebRTC peer connection and all background tasks spawned for it.
/// On drop, closes the PC (which unblocks tasks waiting on its channels)
/// and aborts any tasks that are still alive.
struct ActiveSession {
    pc: Arc<PeerConnection>,
    tasks: Vec<tokio::task::JoinHandle<()>>,
}

impl ActiveSession {
    fn close(&mut self) {
        self.pc.close();
        for task in self.tasks.drain(..) {
            task.abort();
        }
    }
}

impl Drop for ActiveSession {
    fn drop(&mut self) {
        self.close();
    }
}

pub struct Agent {
    server_url: String,
    token: String,
    id: String,
    webrtc_config: WebRTCConfig,
    acl: Option<Acl>,
}

impl Agent {
    pub fn new(
        server_url: &str,
        token: &str,
        id: &str,
        ice_servers: Option<Vec<IceServerConfig>>,
        enable_upnp: bool,
        acl: Option<Acl>,
        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(),
            id: id.to_string(),
            webrtc_config,
            acl,
        }
    }

    pub async fn run(&self) -> Result<()> {
        info!("Starting DTLS agent '{}' connecting to server {}", self.id, self.server_url);
        loop {
            match self.dtls_server_connect().await {
                Ok(_) => info!("DTLS server connection ended normally"),
                Err(e) => error!("DTLS server connection failed: {}", e),
            }
            info!("Reconnecting in {} seconds...", RECONNECT_INTERVAL);
            tokio::time::sleep(Duration::from_secs(RECONNECT_INTERVAL)).await;
        }
    }

    async fn dtls_server_connect(&self) -> Result<()> {
        let mut client = DtlsClient::connect(&self.server_url, None).await?;
        info!("DTLS connected to server {}", self.server_url);

        // Request ICE server configuration from signaling server
        info!("Requesting ICE server config from signaling server");
        let _ = client.send(&SignalingMessage::GetIceServers).await;

        let extra_ice_servers: Vec<IceServerConfig> = match tokio::time::timeout(
            Duration::from_secs(5),
            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![]
            }
        };

        send_message(&client.dtls, &SignalingMessage::Register {
            token: self.token.clone(),
            id: self.id.clone(),
        }).await?;
        info!("Registered with server as '{}'", self.id);

        // Keepalive
        let dtls = client.dtls.clone();
        let keepalive = async move {
            loop {
                tokio::time::sleep(Duration::from_secs(10)).await;
                if send_message(&dtls, &SignalingMessage::Ping).await.is_err() {
                    warn!("DTLS keepalive send failed");
                    break;
                }
            }
        };

        let message_loop = async {
            // One agent can serve multiple concurrent client sessions, each
            // keyed by its server-assigned session_id. A session reaps itself
            // when its peer connection reaches a terminal state (Failed/Closed)
            // by reporting back on `done_tx`.
            let mut active_sessions: HashMap<String, ActiveSession> = HashMap::new();
            let (done_tx, mut done_rx) = mpsc::unbounded_channel::<String>();

            loop {
                let msg = tokio::select! {
                    biased;
                    // Self-reap: a session's peer connection ended.
                    sid = done_rx.recv() => {
                        if let Some(sid) = sid {
                            if let Some(mut session) = active_sessions.remove(&sid) {
                                info!("Session {} ended ({} active session(s) remain)",
                                      sid, active_sessions.len());
                                session.close();
                            }
                        }
                        continue;
                    }
                    msg_result = tokio::time::timeout(
                        Duration::from_secs(45),
                        recv_message(&mut client.data_rx),
                    ) => {
                        match msg_result {
                            Ok(Ok(m)) => m,
                            Ok(Err(e)) => {
                                error!("DTLS recv error: {}", e);
                                break;
                            }
                            Err(_) => {
                                warn!("DTLS recv timeout (45s)");
                                break;
                            }
                        }
                    }
                };

                match msg {
                    SignalingMessage::Offer { session_id, offer_sdp, targets, .. } => {
                        info!("Received offer from server: session={}", session_id);
                        let dtls = client.dtls.clone();
                        let wc = self.webrtc_config.clone();
                        let ac = self.acl.clone();

                        // Replace a prior session with the same id (client reconnect).
                        if let Some(mut old) = active_sessions.remove(&session_id) {
                            info!("Replacing existing session {}", session_id);
                            old.close();
                        }

                        match handle_offer(
                            dtls, &session_id, &offer_sdp, targets,
                            ac, wc, &extra_ice_servers,
                            done_tx.clone(),
                        ).await {
                            Ok(session) => {
                                active_sessions.insert(session_id.clone(), session);
                                info!("Session {} active ({} total)", session_id, active_sessions.len());
                            }
                            Err(e) => {
                                error!("Failed to handle offer {}: {}", session_id, e);
                            }
                        }
                    }
                    SignalingMessage::Candidate { session_id, candidate, .. } => {
                        if let Some(session) = active_sessions.get(&session_id) {
                            if let Ok(c) = IceCandidate::from_sdp(&candidate) {
                                session.pc.add_ice_candidate(c).ok();
                            }
                        } else {
                            debug!("Candidate for unknown session {}, ignoring", session_id);
                        }
                    }
                    SignalingMessage::EndOfCandidates { session_id } => {
                        debug!("End-of-candidates for session {}", session_id);
                    }
                    SignalingMessage::Ping => {
                        send_message(&client.dtls, &SignalingMessage::Pong).await.ok();
                    }
                    SignalingMessage::Pong => {}
                    SignalingMessage::Error { reason, .. } => warn!("Server error: {}", reason),
                    other => warn!("Unexpected message from server: {:?}", other),
                }
            }

            // Cleanup all active sessions on exit
            for (_, mut session) in active_sessions.drain() {
                session.close();
            }
        };

        tokio::select! {
            _ = keepalive => {}
            _ = message_loop => {}
        }

        Err(anyhow!("DTLS server connection lost"))
    }
}

#[allow(clippy::too_many_arguments)]
async fn handle_offer(
    dtls: Arc<DtlsTransport>,
    session_id: &str,
    offer_sdp: &str,
    targets: Option<Vec<Target>>,
    acl: Option<Acl>,
    webrtc_config: WebRTCConfig,
    extra_ice_servers: &[IceServerConfig],
    done_tx: mpsc::UnboundedSender<String>,
) -> Result<ActiveSession> {
    // Resolve targets
    let targets: Vec<(String, u16)> = if let Some(tgts) = targets {
        tgts.iter().map(|t| {
            let host = t.host.clone().unwrap_or_else(|| "127.0.0.1".to_string());
            (host, t.port)
        }).collect()
    } else {
        vec![("127.0.0.1".to_string(), 22)]
    };

    // ACL check
    if let Some(ref acl) = acl {
        for (host, port) in &targets {
            let ip = match tokio::net::lookup_host(format!("{}:{}", host, port)).await {
                Ok(mut addrs) => addrs.next().map(|a| a.ip()).unwrap_or_else(|| {
                    use std::net::IpAddr;
                    "0.0.0.0".parse::<IpAddr>().unwrap()
                }),
                Err(_) => {
                    send_message(&dtls, &SignalingMessage::Error {
                        session_id: session_id.to_string(),
                        reason: format!("Cannot resolve target: {}", host),
                    }).await.ok();
                    return Err(anyhow!("Cannot resolve target: {}", host));
                }
            };
            if !acl.is_allowed(&ip, *port) {
                send_message(&dtls, &SignalingMessage::Error {
                    session_id: session_id.to_string(),
                    reason: format!("Access denied: {}:{}", host, port),
                }).await.ok();
                return Err(anyhow!("ACL denied: {}:{}", host, port));
            }
        }
    }

    // Create WebRTC peer connection
    let peer_connection = webrtc_config.create_peer_connection_with(extra_ice_servers).await?;

    // Tasks spawned for this session — tracked for cleanup
    let mut tasks: Vec<tokio::task::JoinHandle<()>> = Vec::new();

    // Create data channels for each target
    struct FwdTarget {
        dc_label: String,
        host: String,
        port: u16,
    }

    let fwd_targets: Vec<FwdTarget> = targets.into_iter().map(|(host, port)| {
        let label = format!("fwd:{}:{}", host, port);
        FwdTarget { dc_label: label, host, port }
    }).collect();

    let mut data_channels = Vec::new();
    for ft in &fwd_targets {
        let dc_config = DataChannelConfig {
            ordered: true,
            label: ft.dc_label.clone(),
            ..Default::default()
        };
        let dc = peer_connection.create_data_channel(&ft.dc_label, Some(dc_config))?;
        data_channels.push((dc, ft.host.clone(), ft.port));
    }

    // Set remote description
    let offer = SessionDescription::parse(SdpType::Offer, &offer_sdp)?;
    peer_connection.set_remote_description(offer).await?;

    // Spawn forwarding handlers per data channel
    for (dc, host, port) in data_channels {
        let pc = peer_connection.clone();
        let h = host.clone();

        tasks.push(tokio::spawn(async move {
            let dc_id = dc.id;
            let (tcp_msg_tx, tcp_msg_rx) = mpsc::unbounded_channel::<Bytes>();
            let tcp_msg_rx = std::sync::Arc::new(std::sync::Mutex::new(Some(tcp_msg_rx)));

            loop {
                let event = match dc.recv().await {
                    Some(e) => e,
                    None => break,
                };

                match event {
                    DataChannelEvent::Open => {
                        info!("Data channel opened, forwarding to {}:{}", h, port);
                        let pc2 = pc.clone();
                        let h2 = h.clone();
                        let rx_arc = tcp_msg_rx.clone();

                        tokio::spawn(async move {
                            let rx_opt = rx_arc.lock().unwrap().take();
                            let mut rx = match rx_opt {
                                Some(r) => r,
                                None => return,
                            };

                            match TcpStream::connect(format!("{}:{}", h2, port)).await {
                                Ok(tcp) => {
                                    info!("TCP connected to {}:{}, forwarding...", h2, port);
                                    let (mut tcp_read, mut tcp_write) = tcp.into_split();
                                    let pc3 = pc2.clone();
                                    tokio::spawn(async move {
                                        let mut buf = [0u8; 1024];
                                        loop {
                                            match tcp_read.read(&mut buf).await {
                                                Ok(0) | Err(_) => break,
                                                Ok(n) => {
                                                    if pc3.send_data(dc_id, &buf[..n]).await.is_err() { break; }
                                                }
                                            }
                                        }
                                    });
                                    while let Some(data) = rx.recv().await {
                                        if tcp_write.write_all(&data).await.is_err() { break; }
                                        let _ = tcp_write.flush().await;
                                    }
                                }
                                Err(e) => error!("Failed to connect to {}:{}: {}", h2, port, e),
                            }
                        });
                    }
                    DataChannelEvent::Message(data) => {
                        let _ = tcp_msg_tx.send(Bytes::from(data));
                    }
                    DataChannelEvent::Close => {
                        info!("Data channel closed for {}:{}", h, port);
                        break;
                    }
                }
            }
        }));
    }

    // Drain PeerConnection events
    {
        let pc_drain = peer_connection.clone();
        tasks.push(tokio::spawn(async move {
            while let Some(_) = pc_drain.recv().await {}
        }));
    }

    // Session lifecycle monitor: reap this session from the map when its peer
    // connection reaches a terminal state. Disconnected is intentionally
    // ignored (may recover within the ICE grace period); the rustrtc disconnect
    // monitor promotes long-lived Disconnected -> Failed/Closed.
    {
        let pc_watch = peer_connection.clone();
        let sid = session_id.to_string();
        let done = done_tx.clone();
        tasks.push(tokio::spawn(async move {
            let mut state_rx = pc_watch.subscribe_peer_state();
            while state_rx.changed().await.is_ok() {
                let state = *state_rx.borrow();
                match state {
                    PeerConnectionState::Failed | PeerConnectionState::Closed => {
                        debug!("Peer connection for session {} -> {:?}, reaping", sid, state);
                        let _ = done.send(sid);
                        return;
                    }
                    PeerConnectionState::Connected => {
                        if let Some(pair) = pc_watch.ice_transport().get_selected_pair() {
                            info!(
                                "ICE selected pair (agent): local {} {:?} {} -> remote {} {:?} {} [nominated={}]",
                                pair.local.address, pair.local.typ, pair.local.transport,
                                pair.remote.address, pair.remote.typ, pair.remote.transport,
                                pair.nominated,
                            );
                        }
                    }
                    _ => {}
                }
            }
        }));
    }

    // Monitor selected ICE candidate pair — fires when a pair is nominated.
    {
        let mut pair_rx = peer_connection.ice_transport().subscribe_selected_pair();
        tasks.push(tokio::spawn(async move {
            while pair_rx.changed().await.is_ok() {
                if let Some(ref pair) = *pair_rx.borrow() {
                    info!(
                        "ICE pair nominated (agent): local {} {:?} {} -> remote {} {:?} {} [nominated={}]",
                        pair.local.address, pair.local.typ, pair.local.transport,
                        pair.remote.address, pair.remote.typ, pair.remote.transport,
                        pair.nominated,
                    );
                }
            }
        }));
    }

    // Send answer — if this fails, clean up spawned tasks
    let answer_result: Result<(), anyhow::Error> = async {
        let answer = peer_connection.create_answer().await?;
        peer_connection.set_local_description(answer)?;
        let answer_sdp = peer_connection.local_description()
            .ok_or_else(|| anyhow!("No local description"))?
            .to_sdp_string();
        send_message(&dtls, &SignalingMessage::Answer {
            session_id: session_id.to_string(),
            answer_sdp,
        }).await?;
        Ok(())
    }.await;

    if let Err(e) = answer_result {
        for t in &tasks { t.abort(); }
        peer_connection.close();
        return Err(e);
    }

    info!("WebRTC answer sent for session {}", session_id);

    // 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_c = dtls.clone();
        let sid = session_id.to_string();
        tasks.push(tokio::spawn(async move {
            if *gathering_state_rx.borrow() == IceGatheringState::Complete {
                let _ = send_message(&dtls_c, &SignalingMessage::EndOfCandidates {
                    session_id: sid.clone(),
                }).await;
                return;
            }
            loop {
                tokio::select! {
                    result = candidate_rx.recv() => {
                        match result {
                            Ok(candidate) => {
                                info!(
                                    "Local ICE candidate: {} {} {} {:?}",
                                    candidate.address, candidate.transport, candidate.priority, candidate.typ,
                                );
                                let _ = send_message(&dtls_c, &SignalingMessage::Candidate {
                                    session_id: sid.clone(),
                                    candidate: candidate.to_sdp(),
                                }).await;
                                if *gathering_state_rx.borrow() == IceGatheringState::Complete {
                                    let _ = send_message(&dtls_c, &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_c, &SignalingMessage::EndOfCandidates {
                                session_id: sid.clone(),
                            }).await;
                            break;
                        }
                    }
                }
            }
        }));
    }

    Ok(ActiveSession { pc: peer_connection, tasks })
}