zeloxy 0.5.1

A library for creating lightweight, async, and lag-free proxy connections.
Documentation
use std::pin::Pin;
use std::task::{Context, Poll};

use tokio::io::{AsyncRead, AsyncWrite, ReadBuf};
use tokio::net::TcpStream;

#[cfg(feature = "https")]
use tokio_rustls::client::TlsStream;

/// Прокси соединение
#[derive(Debug)]
pub enum ProxyConnection {
  /// Обычное TCP-соединение
  Tcp(TcpStream),

  /// TLS-соединение
  #[cfg(feature = "https")]
  Tls(TlsStream<TcpStream>),
}

impl AsyncRead for ProxyConnection {
  fn poll_read(self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &mut ReadBuf<'_>) -> Poll<std::io::Result<()>> {
    match self.get_mut() {
      #[cfg(any(feature = "http", feature = "socks4", feature = "socks5"))]
      Self::Tcp(stream) => Pin::new(stream).poll_read(cx, buf),
      #[cfg(feature = "https")]
      Self::Tls(stream) => Pin::new(stream).poll_read(cx, buf),
    }
  }
}

impl AsyncWrite for ProxyConnection {
  fn poll_write(self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &[u8]) -> Poll<std::io::Result<usize>> {
    match self.get_mut() {
      #[cfg(any(feature = "http", feature = "socks4", feature = "socks5"))]
      Self::Tcp(stream) => Pin::new(stream).poll_write(cx, buf),
      #[cfg(feature = "https")]
      Self::Tls(stream) => Pin::new(stream).poll_write(cx, buf),
    }
  }

  fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<std::io::Result<()>> {
    match self.get_mut() {
      #[cfg(any(feature = "http", feature = "socks4", feature = "socks5"))]
      Self::Tcp(stream) => Pin::new(stream).poll_flush(cx),
      #[cfg(feature = "https")]
      Self::Tls(stream) => Pin::new(stream).poll_flush(cx),
    }
  }

  fn poll_shutdown(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<std::io::Result<()>> {
    match self.get_mut() {
      #[cfg(any(feature = "http", feature = "socks4", feature = "socks5"))]
      Self::Tcp(stream) => Pin::new(stream).poll_shutdown(cx),
      #[cfg(feature = "https")]
      Self::Tls(stream) => Pin::new(stream).poll_shutdown(cx),
    }
  }
}