proxie/tokio/
mod.rs

1mod http;
2mod socks5;
3
4use core::task::{Context, Poll};
5use std::{
6    result,
7    pin::Pin,
8    io::{Error, IoSlice},
9};
10use tokio::{
11    net::TcpStream,
12    io::{AsyncRead, AsyncWrite, ReadBuf},
13};
14use async_trait::async_trait;
15use crate::{
16    prelude::*,
17    target::ToTarget,
18};
19
20#[async_trait]
21pub trait AsyncProxy {
22    async fn connect(&self, addr: impl ToTarget + Send) -> Result<ProxyTcpStream>;
23}
24
25pub struct ProxyTcpStream {
26    stream: TcpStream,
27}
28
29impl ProxyTcpStream {
30    pub fn into_tcpstream(self) -> TcpStream {
31        self.stream
32    }
33}
34
35impl AsyncRead for ProxyTcpStream {
36    fn poll_read(
37        mut self: Pin<&mut Self>,
38        cx: &mut Context<'_>,
39        buf: &mut ReadBuf<'_>
40    ) -> Poll<result::Result<(), Error>> {
41        Pin::new(&mut self.stream).poll_read(cx, buf)
42    }
43}
44
45impl AsyncWrite for ProxyTcpStream {
46    fn poll_write(
47        mut self: Pin<&mut Self>,
48        cx: &mut Context<'_>,
49        buf: &[u8]
50    ) -> Poll<result::Result<usize, Error>> {
51        Pin::new(&mut self.stream).poll_write(cx, buf)
52    }
53
54    fn poll_write_vectored(
55        mut self: Pin<&mut Self>,
56        cx: &mut Context<'_>,
57        bufs: &[IoSlice<'_>]
58    ) -> Poll<result::Result<usize, Error>> {
59        Pin::new(&mut self.stream).poll_write_vectored(cx, bufs)
60    }
61
62    fn is_write_vectored(&self) -> bool {
63        self.stream.is_write_vectored()
64    }
65
66    fn poll_flush(
67        mut self: Pin<&mut Self>,
68        cx: &mut Context<'_>
69    ) -> Poll<result::Result<(), Error>> {
70        Pin::new(&mut self.stream).poll_flush(cx)
71    }
72
73    fn poll_shutdown(
74        mut self: Pin<&mut Self>,
75        cx: &mut Context<'_>
76    ) -> Poll<result::Result<(), Error>> {
77        Pin::new(&mut self.stream).poll_shutdown(cx)
78    }
79}