gel-stream 0.4.5

A library for streaming data between clients and servers.
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
use crate::{
    common::tokio_stream::TokioListenerStream, ConnectionError, LocalAddress, Preview,
    PreviewConfiguration, ResolvedTarget, RewindStream, Ssl, StreamUpgrade, TlsDriver,
    TlsServerParameterProvider, UpgradableStream, DEFAULT_TLS_BACKLOG,
};
use futures::{stream::FuturesUnordered, StreamExt};
use std::{
    future::Future,
    pin::Pin,
    task::{ready, Poll},
};
use std::{net::SocketAddr, path::Path};
use tokio::io::AsyncReadExt;

type Connection<D = Ssl> = UpgradableStream<crate::BaseStream, D>;

pub struct Acceptor<const PREVIEW: bool = false> {
    resolved_target: ResolvedTarget,
    tls_provider: Option<TlsServerParameterProvider>,
    should_upgrade: bool,
    options: StreamOptions<PREVIEW>,
}

#[derive(Debug, Clone, Copy)]
struct StreamOptions<const PREVIEW: bool> {
    ignore_missing_tls_close_notify: bool,
    preview_configuration: Option<PreviewConfiguration>,
    tcp_backlog: Option<u32>,
    tls_backlog: Option<u32>,
}

impl<const PREVIEW: bool> Default for StreamOptions<PREVIEW> {
    fn default() -> Self {
        Self {
            ignore_missing_tls_close_notify: false,
            preview_configuration: None,
            tcp_backlog: None,
            tls_backlog: None,
        }
    }
}

impl Acceptor<false> {
    pub fn new(target: ResolvedTarget) -> Self {
        Self {
            resolved_target: target,
            tls_provider: None,
            should_upgrade: false,
            options: Default::default(),
        }
    }

    pub fn new_tls(target: ResolvedTarget, provider: TlsServerParameterProvider) -> Self {
        Self {
            resolved_target: target,
            tls_provider: Some(provider),
            should_upgrade: true,
            options: Default::default(),
        }
    }

    pub fn new_starttls(target: ResolvedTarget, provider: TlsServerParameterProvider) -> Self {
        Self {
            resolved_target: target,
            tls_provider: Some(provider),
            should_upgrade: false,
            options: Default::default(),
        }
    }

    pub fn new_tcp(addr: SocketAddr) -> Self {
        Self {
            resolved_target: ResolvedTarget::SocketAddr(addr),
            tls_provider: None,
            should_upgrade: false,
            options: Default::default(),
        }
    }

    pub fn new_tcp_tls(addr: SocketAddr, provider: TlsServerParameterProvider) -> Self {
        Self {
            resolved_target: ResolvedTarget::SocketAddr(addr),
            tls_provider: Some(provider),
            should_upgrade: true,
            options: Default::default(),
        }
    }

    pub fn new_tcp_starttls(addr: SocketAddr, provider: TlsServerParameterProvider) -> Self {
        Self {
            resolved_target: ResolvedTarget::SocketAddr(addr),
            tls_provider: Some(provider),
            should_upgrade: false,
            options: Default::default(),
        }
    }

    pub fn new_unix_path(path: impl AsRef<Path>) -> Result<Self, std::io::Error> {
        #[cfg(unix)]
        {
            Ok(Self {
                resolved_target: ResolvedTarget::from(
                    std::os::unix::net::SocketAddr::from_pathname(path)?,
                ),
                tls_provider: None,
                should_upgrade: false,
                options: Default::default(),
            })
        }
        #[cfg(not(unix))]
        {
            Err(std::io::Error::new(
                std::io::ErrorKind::Unsupported,
                "Unix domain sockets are not supported on this platform",
            ))
        }
    }

    pub fn new_unix_domain(domain: impl AsRef<[u8]>) -> Result<Self, std::io::Error> {
        #[cfg(any(target_os = "linux", target_os = "android"))]
        {
            use std::os::linux::net::SocketAddrExt;
            Ok(Self {
                resolved_target: ResolvedTarget::from(
                    std::os::unix::net::SocketAddr::from_abstract_name(domain)?,
                ),
                tls_provider: None,
                should_upgrade: false,
                options: Default::default(),
            })
        }
        #[cfg(not(any(target_os = "linux", target_os = "android")))]
        {
            Err(std::io::Error::new(
                std::io::ErrorKind::Unsupported,
                "Unix domain sockets are not supported on this platform",
            ))
        }
    }

    pub async fn bind(
        self,
    ) -> Result<
        impl ::futures::Stream<Item = Result<Connection, ConnectionError>> + LocalAddress,
        ConnectionError,
    > {
        let stream = self
            .resolved_target
            .listen_raw(self.options.tcp_backlog)
            .await?;
        Ok(AcceptedStream::<Connection<Ssl>> {
            stream,
            should_upgrade: self.should_upgrade,
            ignore_missing_tls_close_notify: self.options.ignore_missing_tls_close_notify,
            tls_provider: self.tls_provider,
            tls_backlog: TlsAcceptBacklog::new(
                self.options.tls_backlog.unwrap_or(DEFAULT_TLS_BACKLOG) as _,
            ),
            preview_configuration: None,
            _phantom: None,
        })
    }

    #[allow(private_bounds)]
    pub async fn bind_explicit<D: TlsDriver>(
        self,
    ) -> Result<
        impl ::futures::Stream<Item = Result<Connection<D>, ConnectionError>> + LocalAddress,
        ConnectionError,
    > {
        let stream = self
            .resolved_target
            .listen_raw(self.options.tcp_backlog)
            .await?;
        Ok(AcceptedStream::<Connection<D>, D> {
            stream,
            ignore_missing_tls_close_notify: self.options.ignore_missing_tls_close_notify,
            should_upgrade: self.should_upgrade,
            tls_provider: self.tls_provider,
            tls_backlog: TlsAcceptBacklog::new(
                self.options.tls_backlog.unwrap_or(DEFAULT_TLS_BACKLOG) as _,
            ),
            preview_configuration: None,
            _phantom: None,
        })
    }

    /// Listen, and then accept one and only one connection from the listener.
    pub async fn accept_one(self) -> Result<Connection, ConnectionError> {
        let Some(conn) = self.bind().await?.next().await else {
            return Err(ConnectionError::Io(std::io::Error::new(
                std::io::ErrorKind::Interrupted,
                "No connection received",
            )));
        };
        conn
    }
}

impl Acceptor<true> {
    /// Create a new TCP/TLS acceptor that will preview the first
    /// [`PreviewConfiguration::max_preview_bytes`] bytes of the connection.
    pub fn new_tcp_tls_previewing(
        addr: SocketAddr,
        preview_configuration: PreviewConfiguration,
        provider: TlsServerParameterProvider,
    ) -> Self {
        Self {
            resolved_target: ResolvedTarget::SocketAddr(addr),
            tls_provider: Some(provider),
            should_upgrade: false,
            options: StreamOptions {
                preview_configuration: Some(preview_configuration),
                ..Default::default()
            },
        }
    }

    /// Create a new acceptor that will preview the first
    /// [`PreviewConfiguration::max_preview_bytes`] bytes of the connection.
    pub fn new_tls_previewing(
        addr: ResolvedTarget,
        preview_configuration: PreviewConfiguration,
        provider: TlsServerParameterProvider,
    ) -> Self {
        Self {
            resolved_target: addr,
            tls_provider: Some(provider),
            should_upgrade: false,
            options: StreamOptions {
                preview_configuration: Some(preview_configuration),
                ..Default::default()
            },
        }
    }

    /// Create a new acceptor that will preview the first
    /// [`PreviewConfiguration::max_preview_bytes`] bytes of the connection.
    pub fn new_previewing(
        addr: ResolvedTarget,
        preview_configuration: PreviewConfiguration,
    ) -> Self {
        Self {
            resolved_target: addr,
            tls_provider: None,
            should_upgrade: false,
            options: StreamOptions {
                preview_configuration: Some(preview_configuration),
                ..Default::default()
            },
        }
    }

    pub async fn bind(
        self,
    ) -> Result<
        impl ::futures::Stream<Item = Result<(Preview, Connection), ConnectionError>> + LocalAddress,
        ConnectionError,
    > {
        let stream = self
            .resolved_target
            .listen_raw(self.options.tcp_backlog)
            .await?;
        Ok(AcceptedStream::<(Preview, Connection<Ssl>)> {
            stream,
            should_upgrade: self.should_upgrade,
            ignore_missing_tls_close_notify: self.options.ignore_missing_tls_close_notify,
            tls_provider: self.tls_provider,
            tls_backlog: TlsAcceptBacklog::new(self.options.tls_backlog.unwrap_or(128) as _),
            preview_configuration: self.options.preview_configuration,
            _phantom: None,
        })
    }

    #[allow(private_bounds)]
    pub async fn bind_explicit<D: TlsDriver>(
        self,
    ) -> Result<
        impl ::futures::Stream<Item = Result<(Preview, Connection<D>), ConnectionError>> + LocalAddress,
        ConnectionError,
    > {
        let stream = self
            .resolved_target
            .listen_raw(self.options.tcp_backlog)
            .await?;
        Ok(AcceptedStream::<(Preview, Connection<D>), D> {
            stream,
            should_upgrade: self.should_upgrade,
            ignore_missing_tls_close_notify: self.options.ignore_missing_tls_close_notify,
            tls_provider: self.tls_provider,
            tls_backlog: TlsAcceptBacklog::new(
                self.options.tls_backlog.unwrap_or(DEFAULT_TLS_BACKLOG) as _,
            ),
            preview_configuration: self.options.preview_configuration,
            _phantom: None,
        })
    }

    /// Listen, and then accept one and only one connection from the listener.
    pub async fn accept_one(self) -> Result<(Preview, Connection), ConnectionError> {
        let Some(conn) = self.bind().await?.next().await else {
            return Err(ConnectionError::Io(std::io::Error::new(
                std::io::ErrorKind::Interrupted,
                "No connection received",
            )));
        };
        conn
    }
}

struct AcceptedStream<S, D: TlsDriver = Ssl> {
    stream: TokioListenerStream,
    should_upgrade: bool,
    ignore_missing_tls_close_notify: bool,
    tls_provider: Option<TlsServerParameterProvider>,
    tls_backlog: TlsAcceptBacklog<S>,
    preview_configuration: Option<PreviewConfiguration>,
    // Avoid using PhantomData because it fails to implement certain auto-traits
    _phantom: Option<&'static D>,
}

impl<S, D: TlsDriver> LocalAddress for AcceptedStream<S, D> {
    fn local_address(&self) -> std::io::Result<ResolvedTarget> {
        self.stream.local_address()
    }
}

impl<D: TlsDriver> futures::Stream for AcceptedStream<Connection<D>, D> {
    type Item = Result<Connection<D>, ConnectionError>;

    fn poll_next(
        mut self: std::pin::Pin<&mut Self>,
        cx: &mut std::task::Context<'_>,
    ) -> Poll<Option<Self::Item>> {
        let ignore_missing_tls_close_notify = self.ignore_missing_tls_close_notify;
        let make_stream = move |tls_provider: Option<TlsServerParameterProvider>, stream| {
            let mut stream = UpgradableStream::<_, D>::new_server(stream, tls_provider);
            if ignore_missing_tls_close_notify {
                stream.ignore_missing_close_notify();
            }
            stream
        };

        // If we're not upgrading, we can just return the stream as is and skip
        // the second-level backlog.
        if !self.should_upgrade {
            return self.as_mut().stream.poll_next_unpin(cx).map(|c| {
                c.map(|c| Ok(c.map(|(c, _t)| make_stream(self.tls_provider.clone(), c))?))
            });
        }

        // Fill the backlog to capacity as log as we have connections to accept.
        while !self.tls_backlog.is_full() {
            let Poll::Ready(r) = self.stream.poll_next_unpin(cx) else {
                if self.tls_backlog.is_empty() {
                    return Poll::Pending;
                }
                break;
            };

            let Some((stream, _t)) = r.transpose()? else {
                if self.tls_backlog.is_empty() {
                    return Poll::Ready(None);
                }
                break;
            };

            let tls_provider = self.tls_provider.clone();
            self.tls_backlog.push(async move {
                let stream = make_stream(tls_provider, stream);
                let stream = stream.secure_upgrade().await?;
                Ok(stream)
            })
        }

        // We've got at least one pending connection here
        debug_assert!(!self.tls_backlog.is_empty());
        let r = ready!(Pin::new(&mut self.tls_backlog).poll_next(cx))?;
        Poll::Ready(Some(Ok(r)))
    }
}

impl<D: TlsDriver> futures::Stream for AcceptedStream<(Preview, Connection<D>), D> {
    type Item = Result<(Preview, Connection<D>), ConnectionError>;
    fn poll_next(
        mut self: std::pin::Pin<&mut Self>,
        cx: &mut std::task::Context<'_>,
    ) -> Poll<Option<Self::Item>> {
        // Fill the backlog to capacity as log as we have connections to accept.
        while !self.tls_backlog.is_full() {
            let Poll::Ready(r) = self.stream.poll_next_unpin(cx) else {
                if self.tls_backlog.is_empty() {
                    return Poll::Pending;
                }
                break;
            };

            let Some((mut stream, _t)) = r.transpose()? else {
                if self.tls_backlog.is_empty() {
                    return Poll::Ready(None);
                }
                break;
            };

            let tls_provider = self.tls_provider.clone();
            let preview_configuration = self.preview_configuration.unwrap();
            let ignore_missing_tls_close_notify = self.ignore_missing_tls_close_notify;
            self.tls_backlog.push(async move {
                let mut buf = smallvec::SmallVec::with_capacity(
                    preview_configuration.max_preview_bytes.get(),
                );
                buf.resize(preview_configuration.max_preview_bytes.get(), 0);
                stream.read_exact(&mut buf).await?;
                let mut stream = RewindStream::new(stream);
                stream.rewind(&buf);
                let preview = Preview::new(buf);
                let mut stream = UpgradableStream::<_, D>::new_server_preview(stream, tls_provider);
                if ignore_missing_tls_close_notify {
                    stream.ignore_missing_close_notify();
                }

                Ok((preview, stream))
            })
        }

        // We've got at least one pending connection here
        debug_assert!(!self.tls_backlog.is_empty());
        let r = ready!(Pin::new(&mut self.tls_backlog).poll_next(cx))?;
        Poll::Ready(Some(Ok(r)))
    }
}

struct TlsAcceptBacklog<C> {
    capacity: usize,
    #[allow(clippy::type_complexity)]
    futures: FuturesUnordered<
        Pin<Box<dyn Future<Output = Result<C, ConnectionError>> + Send + 'static>>,
    >,
}

impl<C> TlsAcceptBacklog<C> {
    fn new(capacity: usize) -> Self {
        Self {
            capacity,
            futures: FuturesUnordered::new(),
        }
    }

    fn is_full(&self) -> bool {
        self.futures.len() >= self.capacity
    }

    fn is_empty(&self) -> bool {
        self.futures.len() == 0
    }

    fn poll_next(
        mut self: std::pin::Pin<&mut Self>,
        cx: &mut std::task::Context<'_>,
    ) -> Poll<Result<C, ConnectionError>> {
        debug_assert!(!self.is_empty());
        self.futures.poll_next_unpin(cx).map(|r| r.unwrap())
    }

    fn push(&mut self, future: impl Future<Output = Result<C, ConnectionError>> + Send + 'static) {
        self.futures.push(Box::pin(future));
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{
        Connector, OpensslDriver, RustlsDriver, Target, TlsParameters, TlsServerParameters,
    };
    use std::net::*;
    use tokio::io::{AsyncReadExt, AsyncWriteExt};

    async fn test_acceptor_new_tcp_previewing<D: TlsDriver>() -> Result<(), ConnectionError> {
        let acceptor = Acceptor::new_tcp_tls_previewing(
            SocketAddr::from((Ipv4Addr::LOCALHOST, 0)),
            PreviewConfiguration::default(),
            TlsServerParameterProvider::new(TlsServerParameters::new_with_certificate(
                crate::test_keys::SERVER_KEY.clone_key(),
            )),
        );

        let mut conns = acceptor.bind_explicit::<D>().await?;

        let addr = conns.local_address()?;
        tokio::task::spawn(async move {
            let mut conn = Connector::new_resolved(addr).connect().await?;
            conn.write_all(b"HELLO WORLD").await
        });

        let (preview, mut conn) = conns.next().await.unwrap()?;
        assert_eq!(preview.len(), 8);
        assert_eq!(preview, b"HELLO WO");
        let mut string = String::new();
        conn.read_to_string(&mut string).await?;
        assert_eq!(string, "HELLO WORLD");

        let addr = conns.local_address()?;
        tokio::task::spawn(async move {
            let target = Target::new_resolved_tls(addr, TlsParameters::insecure());
            let mut conn = Connector::new(target)?.connect().await?;
            conn.write_all(b"HELLO WORLD").await
        });

        let (preview, conn) = conns.next().await.unwrap()?;
        assert_eq!(preview.len(), 8);
        assert!(matches!(preview.as_ref(), [0x16, 3, 1, ..]));
        let (preview, mut conn) = conn
            .secure_upgrade_preview(PreviewConfiguration::default())
            .await?;
        assert_eq!(preview.len(), 8);
        assert_eq!(preview, b"HELLO WO");

        let mut string = String::new();
        conn.read_to_string(&mut string).await?;
        assert_eq!(string, "HELLO WORLD");

        Ok(())
    }

    #[tokio::test]
    async fn test_acceptor_new_tcp_previewing_openssl() -> Result<(), ConnectionError> {
        test_acceptor_new_tcp_previewing::<OpensslDriver>().await
    }

    #[tokio::test]
    async fn test_acceptor_new_tcp_previewing_rustls() -> Result<(), ConnectionError> {
        test_acceptor_new_tcp_previewing::<RustlsDriver>().await
    }
}