atm0s_reverse_proxy_agent/
connection.rs

1//! Tunnel is a trait that defines the interface for a tunnel which connect to connector port of relayer.
2
3use std::{fmt::Debug, str::FromStr};
4
5use protocol::stream::TunnelStream;
6use tokio::io::{AsyncRead, AsyncWrite};
7
8#[cfg(feature = "quic")]
9pub mod quic;
10
11#[cfg(feature = "tcp")]
12pub mod tcp;
13
14#[cfg(feature = "tls")]
15pub mod tls;
16
17#[derive(Debug, Clone)]
18pub enum Protocol {
19    #[cfg(feature = "tcp")]
20    Tcp,
21    #[cfg(feature = "tls")]
22    Tls,
23    #[cfg(feature = "quic")]
24    Quic,
25}
26
27impl FromStr for Protocol {
28    type Err = &'static str;
29    fn from_str(s: &str) -> Result<Self, Self::Err> {
30        match s {
31            #[cfg(feature = "tcp")]
32            "tcp" | "TCP" => Ok(Protocol::Tcp),
33            #[cfg(feature = "tls")]
34            "tls" | "TLS" => Ok(Protocol::Tls),
35            #[cfg(feature = "quic")]
36            "quic" | "QUIC" => Ok(Protocol::Quic),
37            _ => Err("invalid protocol"),
38        }
39    }
40}
41
42pub trait SubConnection: AsyncRead + AsyncWrite + Unpin + Send + Sync {}
43
44impl<R: AsyncRead + Unpin + Send + Sync, W: AsyncWrite + Unpin + Send + Sync> SubConnection for TunnelStream<R, W> {}
45
46#[async_trait::async_trait]
47pub trait Connection<S: SubConnection>: Send + Sync {
48    async fn create_outgoing(&mut self) -> anyhow::Result<S>;
49    async fn recv(&mut self) -> anyhow::Result<S>;
50}