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
use crate::acme::ACME_TLS_ALPN_NAME;
use crate::AcmeState;
use async_rustls::rustls::Session;
use async_rustls::server::TlsStream;
use async_rustls::{Accept, TlsAcceptor};
use futures::stream::FuturesUnordered;
use futures::{AsyncRead, AsyncWrite, Stream};
use pin_project::pin_project;
use std::fmt::Debug;
use std::pin::Pin;
use std::task::{Context, Poll};

#[pin_project]
pub struct Incoming<
    TCP: AsyncRead + AsyncWrite + Unpin,
    ETCP,
    ITCP: Stream<Item = Result<TCP, ETCP>>,
    EC: Debug + 'static,
    EA: Debug + 'static,
> {
    #[pin]
    state: AcmeState<EC, EA>,
    acceptor: TlsAcceptor,
    #[pin]
    tcp_incoming: ITCP,
    #[pin]
    tcp_accepting: FuturesUnordered<Accept<TCP>>,
}

impl<
        TCP: AsyncRead + AsyncWrite + Unpin,
        ETCP,
        ITCP: Stream<Item = Result<TCP, ETCP>>,
        EC: Debug + 'static,
        EA: Debug + 'static,
    > Incoming<TCP, ETCP, ITCP, EC, EA>
{
    pub fn new(tcp_incoming: ITCP, state: AcmeState<EC, EA>, acceptor: TlsAcceptor) -> Self {
        Self {
            state,
            acceptor,
            tcp_incoming,
            tcp_accepting: FuturesUnordered::new(),
        }
    }
}

impl<
        TCP: AsyncRead + AsyncWrite + Unpin,
        ETCP,
        ITCP: Stream<Item = Result<TCP, ETCP>>,
        EC: Debug + 'static,
        EA: Debug + 'static,
    > Stream for Incoming<TCP, ETCP, ITCP, EC, EA>
{
    type Item = Result<TlsStream<TCP>, ETCP>;

    fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
        let mut this = self.project();
        loop {
            match this.state.as_mut().poll_next(cx) {
                Poll::Ready(Some(event)) => {
                    match event {
                        Ok(ok) => log::info!("event: {:?}", ok),
                        Err(err) => log::error!("event: {:?}", err),
                    }
                    continue;
                }
                Poll::Ready(None) => unreachable!(),
                Poll::Pending => {}
            }
            match this.tcp_accepting.as_mut().poll_next(cx) {
                Poll::Ready(Some(Ok(tls))) => match tls.get_ref().1.get_alpn_protocol() {
                    Some(ACME_TLS_ALPN_NAME) => {
                        log::info!("received TLS-ALPN-01 validation request")
                    }
                    _ => return Poll::Ready(Some(Ok(tls))),
                },
                Poll::Ready(Some(Err(err))) => log::error!("tls accept failed, {:?}", err),
                Poll::Ready(None) | Poll::Pending => {}
            }
            match this.tcp_incoming.as_mut().poll_next(cx) {
                Poll::Ready(Some(Ok(tcp))) => this.tcp_accepting.push(this.acceptor.accept(tcp)),
                Poll::Ready(Some(Err(err))) => return Poll::Ready(Some(Err(err))),
                Poll::Ready(None) => return Poll::Ready(None),
                Poll::Pending => return Poll::Pending,
            }
        }
    }
}