1mod http;
2mod socks5;
3
4use std::{
5 result,
6 pin::Pin,
7};
8use async_std::{
9 net::TcpStream,
10 io::Error,
11 task::{Context, Poll},
12};
13use futures::{AsyncRead, AsyncWrite};
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 self: Pin<&mut Self>,
38 cx: &mut Context<'_>,
39 buf: &mut [u8]
40 ) -> Poll<result::Result<usize, Error>> {
41 Pin::new(&mut &self.stream).poll_read(cx, buf)
42 }
43}
44
45impl AsyncRead for &ProxyTcpStream {
46 fn poll_read(
47 self: Pin<&mut Self>,
48 cx: &mut Context<'_>,
49 buf: &mut [u8]
50 ) -> Poll<result::Result<usize, Error>> {
51 Pin::new(&mut &self.stream).poll_read(cx, buf)
52 }
53}
54
55impl AsyncWrite for ProxyTcpStream {
56 fn poll_write(
57 self: Pin<&mut Self>,
58 cx: &mut Context<'_>,
59 buf: &[u8]
60 ) -> Poll<result::Result<usize, Error>> {
61 Pin::new(&mut &self.stream).poll_write(cx, buf)
62 }
63
64 fn poll_flush(
65 self: Pin<&mut Self>,
66 cx: &mut Context<'_>
67 ) -> Poll<result::Result<(), Error>> {
68 Pin::new(&mut &self.stream).poll_flush(cx)
69 }
70
71 fn poll_close(
72 self: Pin<&mut Self>,
73 cx: &mut Context<'_>
74 ) -> Poll<result::Result<(), Error>> {
75 Pin::new(&mut &self.stream).poll_close(cx)
76 }
77}
78
79impl AsyncWrite for &ProxyTcpStream {
80 fn poll_write(
81 self: Pin<&mut Self>,
82 cx: &mut Context<'_>,
83 buf: &[u8]
84 ) -> Poll<result::Result<usize, Error>> {
85 Pin::new(&mut &self.stream).poll_write(cx, buf)
86 }
87
88 fn poll_flush(
89 self: Pin<&mut Self>,
90 cx: &mut Context<'_>
91 ) -> Poll<result::Result<(), Error>> {
92 Pin::new(&mut &self.stream).poll_flush(cx)
93 }
94
95 fn poll_close(
96 self: Pin<&mut Self>,
97 cx: &mut Context<'_>
98 ) -> Poll<result::Result<(), Error>> {
99 Pin::new(&mut &self.stream).poll_close(cx)
100 }
101}