trz-gateway-client 0.2.23

Secure Proxy / Agents implementation in Rust
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
//! Client-side WebRTC signaling and reliable byte-stream transport.

use std::pin::Pin;
use std::task::Context;
use std::task::Poll;

use futures::SinkExt as _;
use futures::StreamExt as _;
use nameth::NamedEnumValues as _;
use nameth::nameth;
use tokio::io::AsyncRead;
use tokio::io::AsyncWrite;
use tokio::io::ReadBuf;
use tokio::sync::mpsc;
use tokio_tungstenite::connect_async_with_config;
use tokio_tungstenite::tungstenite;
use tokio_tungstenite::tungstenite::Message;
use tokio_tungstenite::tungstenite::client::IntoClientRequest as _;
use tracing::info;
use tracing::warn;
use trz_gateway_common::p2p::data_channel_io::DataChannelIo;
use trz_gateway_common::p2p::peer_connection::LocalIceEvent;
use trz_gateway_common::p2p::peer_connection::PeerConnection;
use trz_gateway_common::p2p::peer_connection::PeerConnectionBuilder;
use trz_gateway_common::p2p::protocol::FailureCode;
use trz_gateway_common::p2p::protocol::MAX_SDP_LEN;
use trz_gateway_common::p2p::protocol::P2pConnectionId;
use trz_gateway_common::p2p::protocol::PROTOCOL_VERSION;
use trz_gateway_common::p2p::protocol::SessionDescription;
use trz_gateway_common::p2p::protocol::SignalMessage;
use url::Url;

use crate::client::config::P2pClientConfig;

const ICE_QUEUE_CAPACITY: usize = 64;
const MAX_SIGNAL_MESSAGE_SIZE: usize = MAX_SDP_LEN + 64 * 1024;

/// Opens one fresh reliable P2P byte stream.
pub(crate) async fn connect(config: &P2pClientConfig) -> Result<P2pStream, P2pConnectError> {
    tokio::time::timeout(config.connect_timeout, connect_inner(config))
        .await
        .inspect(|result| match result {
            Ok(_p2p_stream) => info!("Got P2P connection"),
            Err(error) => warn!("Failed to create P2P connection: {error}"),
        })
        .map_err(|timeout: tokio::time::error::Elapsed| {
            warn!("Timed out creating P2P connection: {timeout}");
            P2pConnectError::Timeout("complete P2P connection")
        })?
}

async fn connect_inner(config: &P2pClientConfig) -> Result<P2pStream, P2pConnectError> {
    let request = signaling_request(config)?;
    let websocket_config = tungstenite::protocol::WebSocketConfig::default()
        .max_message_size(Some(MAX_SIGNAL_MESSAGE_SIZE))
        .max_frame_size(Some(MAX_SIGNAL_MESSAGE_SIZE));
    let (socket, _) = tokio::time::timeout(
        config.signaling_timeout,
        connect_async_with_config(request, Some(websocket_config), false),
    )
    .await
    .map_err(|_| P2pConnectError::Timeout("signaling WebSocket"))??;
    info!("Opened signaling websocket");
    let mut socket = scopeguard::guard(socket, |_| info!("Closing signaling websocket"));
    send_signal(
        &mut socket,
        &SignalMessage::Hello {
            protocol_version: PROTOCOL_VERSION,
        },
    )
    .await?;
    let connection_id = receive_start(&mut socket, config).await?;

    let (local_ice_tx, mut local_ice_rx) = mpsc::channel(ICE_QUEUE_CAPACITY);
    let peer = PeerConnectionBuilder::new(config.ice_servers.clone(), local_ice_tx)
        .build()
        .await?;
    let peer = PeerGuard::new(peer);
    let data_channel = peer.peer().create_reliable_data_channel().await?;
    let offer = peer.peer().create_offer().await?;
    send_signal(
        &mut socket,
        &SignalMessage::Description {
            connection_id,
            description: offer,
        },
    )
    .await?;

    let result = negotiate(
        &mut socket,
        config,
        connection_id,
        peer.peer(),
        &mut local_ice_rx,
        data_channel.wait_open(),
    )
    .await;
    let stream = match result {
        Ok(stream) => peer.into_stream(stream),
        Err(error) => {
            let _ = send_signal(&mut socket, &SignalMessage::Cancel { connection_id }).await;
            let _ = socket.close(None).await;
            return Err(error);
        }
    };
    let _ = socket.close(None).await;
    Ok(stream)
}

async fn receive_start<S>(
    socket: &mut tokio_tungstenite::WebSocketStream<S>,
    config: &P2pClientConfig,
) -> Result<P2pConnectionId, P2pConnectError>
where
    S: AsyncRead + AsyncWrite + Unpin,
{
    tokio::time::timeout(config.signaling_timeout, async {
        loop {
            match socket
                .next()
                .await
                .ok_or(P2pConnectError::SignalingClosed)??
            {
                Message::Text(text) => match parse_signal(&text)? {
                    SignalMessage::Start { connection_id } => return Ok(connection_id),
                    SignalMessage::Ping => {
                        send_signal(socket, &SignalMessage::Pong).await?;
                    }
                    _ => return Err(P2pConnectError::Protocol("Expected session start".into())),
                },
                Message::Ping(_) | Message::Pong(_) => socket.flush().await?,
                Message::Close(_) => return Err(P2pConnectError::SignalingClosed),
                Message::Binary(_) | Message::Frame(_) => {
                    return Err(P2pConnectError::Protocol(
                        "Signaling messages must be JSON text".into(),
                    ));
                }
            }
        }
    })
    .await
    .map_err(|_| P2pConnectError::Timeout("signaling session start"))?
}

async fn negotiate<S, F>(
    socket: &mut tokio_tungstenite::WebSocketStream<S>,
    config: &P2pClientConfig,
    connection_id: P2pConnectionId,
    peer: &PeerConnection,
    local_ice_rx: &mut mpsc::Receiver<LocalIceEvent>,
    opened: F,
) -> Result<DataChannelIo, P2pConnectError>
where
    S: AsyncRead + AsyncWrite + Unpin,
    F: Future<
        Output = Result<
            DataChannelIo,
            trz_gateway_common::p2p::peer_connection::PeerConnectionError,
        >,
    >,
{
    tokio::pin!(opened);
    let timeout = tokio::time::sleep(config.handshake_timeout);
    tokio::pin!(timeout);
    let mut answer_received = false;
    loop {
        tokio::select! {
            local_ice = local_ice_rx.recv() => {
                let message = match local_ice.ok_or(P2pConnectError::LocalIceClosed)? {
                    LocalIceEvent::Candidate(candidate) => SignalMessage::IceCandidate {
                        connection_id,
                        candidate,
                    },
                    LocalIceEvent::EndOfCandidates => SignalMessage::EndOfCandidates {
                        connection_id,
                    },
                    LocalIceEvent::Error(error) => return Err(P2pConnectError::Protocol(error)),
                };
                send_signal(socket, &message).await?;
            }
            incoming = socket.next() => {
                let message = match incoming.ok_or(P2pConnectError::SignalingClosed)?? {
                    Message::Text(text) => parse_signal(&text)?,
                    Message::Ping(_) | Message::Pong(_) => {
                        socket.flush().await?;
                        continue;
                    }
                    Message::Close(_) => return Err(P2pConnectError::SignalingClosed),
                    Message::Binary(_) | Message::Frame(_) => {
                        return Err(P2pConnectError::Protocol(
                            "Signaling messages must be JSON text".into(),
                        ));
                    }
                };
                if message == SignalMessage::Ping {
                    send_signal(socket, &SignalMessage::Pong).await?;
                    continue;
                }
                if message.connection_id() != Some(connection_id) {
                    return Err(P2pConnectError::Protocol("Unexpected connection identifier".into()));
                }
                match message {
                    SignalMessage::Description {
                        description: description @ SessionDescription::Answer(_),
                        ..
                    } if !answer_received => {
                        peer.set_remote_description(description).await?;
                        answer_received = true;
                    }
                    SignalMessage::IceCandidate { candidate, .. } => {
                        peer.add_remote_candidate(candidate).await?;
                    }
                    SignalMessage::EndOfCandidates { .. } => {
                        peer.end_remote_candidates().await?;
                    }
                    SignalMessage::Cancel { .. } => return Err(P2pConnectError::PeerCancelled),
                    SignalMessage::Failure { code, detail, .. } => {
                        return Err(P2pConnectError::PeerFailure { code, detail });
                    }
                    _ => return Err(P2pConnectError::Protocol("Unexpected session message".into())),
                }
            }
            opened = &mut opened => return Ok(opened?),
            () = &mut timeout => return Err(P2pConnectError::Timeout("WebRTC handshake")),
        }
    }
}

fn signaling_request(
    config: &P2pClientConfig,
) -> Result<tungstenite::http::Request<()>, P2pConnectError> {
    let mut url = Url::parse(&config.signaling_url)?;
    match url.scheme() {
        "http" => url
            .set_scheme("ws")
            .map_err(|_| P2pConnectError::InvalidUrlScheme)?,
        "https" => url
            .set_scheme("wss")
            .map_err(|_| P2pConnectError::InvalidUrlScheme)?,
        "ws" | "wss" => {}
        _ => return Err(P2pConnectError::InvalidUrlScheme),
    }
    url.path_segments_mut()
        .map_err(|_| P2pConnectError::InvalidUrlScheme)?
        .pop_if_empty()
        .extend(["p2p", "connect", config.server_name.as_ref()]);
    Ok(url.as_str().into_client_request()?)
}

fn parse_signal(text: &str) -> Result<SignalMessage, P2pConnectError> {
    let message: SignalMessage = serde_json::from_str(text)?;
    message.validate()?;
    Ok(message)
}

async fn send_signal<S>(
    socket: &mut tokio_tungstenite::WebSocketStream<S>,
    message: &SignalMessage,
) -> Result<(), P2pConnectError>
where
    S: AsyncRead + AsyncWrite + Unpin,
{
    socket
        .send(Message::Text(serde_json::to_string(message)?.into()))
        .await?;
    Ok(())
}

struct PeerGuard(Option<PeerConnection>);

impl PeerGuard {
    fn new(peer: PeerConnection) -> Self {
        Self(Some(peer))
    }

    fn peer(&self) -> &PeerConnection {
        self.0.as_ref().expect("peer guard")
    }

    fn into_stream(mut self, io: DataChannelIo) -> P2pStream {
        P2pStream {
            io,
            peer: self.0.take(),
        }
    }
}

impl Drop for PeerGuard {
    fn drop(&mut self) {
        close_peer(self.0.take());
    }
}

/// Reliable WebRTC stream that keeps its peer connection alive.
pub(crate) struct P2pStream {
    io: DataChannelIo,
    peer: Option<PeerConnection>,
}

impl Drop for P2pStream {
    fn drop(&mut self) {
        close_peer(self.peer.take());
    }
}

fn close_peer(peer: Option<PeerConnection>) {
    if let Some(peer) = peer
        && let Ok(runtime) = tokio::runtime::Handle::try_current()
    {
        runtime.spawn(async move {
            let _ = peer.close().await;
        });
    }
}

impl AsyncRead for P2pStream {
    fn poll_read(
        mut self: Pin<&mut Self>,
        context: &mut Context<'_>,
        buffer: &mut ReadBuf<'_>,
    ) -> Poll<std::io::Result<()>> {
        Pin::new(&mut self.io).poll_read(context, buffer)
    }
}

impl AsyncWrite for P2pStream {
    fn poll_write(
        mut self: Pin<&mut Self>,
        context: &mut Context<'_>,
        buffer: &[u8],
    ) -> Poll<std::io::Result<usize>> {
        Pin::new(&mut self.io).poll_write(context, buffer)
    }

    fn poll_flush(
        mut self: Pin<&mut Self>,
        context: &mut Context<'_>,
    ) -> Poll<std::io::Result<()>> {
        Pin::new(&mut self.io).poll_flush(context)
    }

    fn poll_shutdown(
        mut self: Pin<&mut Self>,
        context: &mut Context<'_>,
    ) -> Poll<std::io::Result<()>> {
        Pin::new(&mut self.io).poll_shutdown(context)
    }
}

#[nameth]
#[derive(thiserror::Error, Debug)]
pub enum P2pConnectError {
    #[error("[{n}] Invalid signaling URL scheme", n = self.name())]
    InvalidUrlScheme,

    #[error("[{n}] Invalid signaling URL: {0}", n = self.name())]
    Url(#[from] url::ParseError),

    #[error("[{n}] Failed to build signaling request: {0}", n = self.name())]
    Request(#[from] tungstenite::http::Error),

    #[error("[{n}] Signaling WebSocket failed: {0}", n = self.name())]
    WebSocket(#[from] tungstenite::Error),

    #[error("[{n}] Invalid signaling JSON: {0}", n = self.name())]
    Json(#[from] serde_json::Error),

    #[error("[{n}] Invalid signaling protocol message: {0}", n = self.name())]
    Validation(#[from] trz_gateway_common::p2p::protocol::ValidationError),

    #[error("[{n}] Signaling protocol error: {0}", n = self.name())]
    Protocol(String),

    #[error("[{n}] Signaling connection closed", n = self.name())]
    SignalingClosed,

    #[error("[{n}] Local ICE event stream closed", n = self.name())]
    LocalIceClosed,

    #[error("[{n}] Peer cancelled the P2P connection", n = self.name())]
    PeerCancelled,

    #[error("[{n}] Peer rejected the P2P connection ({code:?}): {detail}", n = self.name())]
    PeerFailure { code: FailureCode, detail: String },

    #[error("[{n}] {0}", n = self.name())]
    PeerConnection(#[from] trz_gateway_common::p2p::peer_connection::PeerConnectionError),

    #[error("[{n}] Timed out while establishing {0}", n = self.name())]
    Timeout(&'static str),
}

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

    #[test]
    fn builds_encoded_signaling_url() {
        let config = P2pClientConfig::new(
            "https://signal.example/base/",
            "server name/with slash".into(),
        );
        let request = signaling_request(&config).unwrap();
        assert_eq!(
            "wss://signal.example/base/p2p/connect/server%20name%2Fwith%20slash",
            request.uri().to_string(),
        );
    }
}