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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
use crate::general::ConnectionTimeouts;
use crate::clients::socks4::general::ErrorKind;
use crate::proxy::ProxyStream;
use tokio::net::TcpStream;
use tokio::io::{AsyncRead, AsyncWrite};
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::time::timeout;
use std::pin::Pin;
use core::task::{Poll, Context};
use std::net::SocketAddrV4;
use std::io;
pub struct Socks4NoIdent {
wrapped_stream: TcpStream
}
#[repr(u8)]
pub enum Command {
TcpConnectionEstablishment = 1,
TcpPortBinding
}
pub struct ConnParams {
dest_addr: SocketAddrV4,
timeouts: ConnectionTimeouts
}
impl ConnParams {
pub fn new(dest_addr: SocketAddrV4, timeouts: ConnectionTimeouts)
-> ConnParams
{
ConnParams { dest_addr, timeouts }
}
}
#[async_trait::async_trait]
impl ProxyStream for Socks4NoIdent {
type Stream = TcpStream;
type ErrorKind = ErrorKind;
type ConnParams = ConnParams;
async fn connect(mut stream: Self::Stream, params: Self::ConnParams)
-> Result<Self, Self::ErrorKind>
{
let buf_len = 1 + 1 + 2 + 4 + 1;
let mut buf = Vec::with_capacity(buf_len);
buf.push(4);
buf.push(Command::TcpConnectionEstablishment as u8);
let port_in_bytes = params.dest_addr.port().to_be_bytes();
buf.extend_from_slice(&port_in_bytes[..]);
let ipaddr_in_bytes = params.dest_addr.ip().octets();
buf.extend_from_slice(&ipaddr_in_bytes[..]);
buf.push(0);
let future = stream.write_all(&buf);
let future = timeout(params.timeouts.write_timeout, future);
let _ = future.await.map_err(|_| ErrorKind::OperationTimeoutReached)?
.map_err(|e| ErrorKind::IOError(e))?;
let future = stream.read(&mut buf);
let future = timeout(params.timeouts.read_timeout, future);
let read_bytes = future.await.map_err(|_| ErrorKind::OperationTimeoutReached)?
.map_err(|e| ErrorKind::IOError(e))?;
if read_bytes != 8 {
return Err(ErrorKind::BadBuffer)
}
match buf[1] {
0x5a => Ok(Socks4NoIdent { wrapped_stream: stream }),
0x5b => Err(ErrorKind::RequestDenied),
0x5c => Err(ErrorKind::IdentIsUnavailable),
0x5d => Err(ErrorKind::BadIdent),
_ => Err(ErrorKind::BadBuffer)
}
}
}
impl AsyncRead for Socks4NoIdent {
fn poll_read(self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &mut [u8])
-> Poll<io::Result<usize>>
{
let pinned = &mut Pin::into_inner(self).wrapped_stream;
Pin::new(pinned).poll_read(cx, buf)
}
}
impl AsyncWrite for Socks4NoIdent {
fn poll_write(self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &[u8])
-> Poll<Result<usize, io::Error>>
{
let stream = &mut Pin::into_inner(self).wrapped_stream;
Pin::new(stream).poll_write(cx, buf)
}
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>)
-> Poll<Result<(), io::Error>>
{
let stream = &mut Pin::into_inner(self).wrapped_stream;
Pin::new(stream).poll_flush(cx)
}
fn poll_shutdown(self: Pin<&mut Self>, cx: &mut Context<'_>)
-> Poll<Result<(), io::Error>>
{
let stream = &mut Pin::into_inner(self).wrapped_stream;
Pin::new(stream).poll_shutdown(cx)
}
}