socks5_impl/server/connection/
bind.rs1use crate::{
2 Address, AsyncStreamOperation,
3 protocol::{Reply, Response},
4};
5use std::{
6 marker::PhantomData,
7 net::{SocketAddr, ToSocketAddrs},
8 pin::Pin,
9 task::{Context, Poll},
10};
11use tokio::{
12 io::{AsyncRead, AsyncWrite, AsyncWriteExt, ReadBuf},
13 net::{
14 TcpListener, TcpStream,
15 tcp::{ReadHalf, WriteHalf},
16 },
17};
18
19#[derive(Debug)]
29pub struct Bind<S> {
30 stream: TcpStream,
31 _state: PhantomData<S>,
32}
33
34#[derive(Debug, Default)]
36pub struct NeedFirstReply;
37
38#[derive(Debug, Default)]
40pub struct NeedSecondReply;
41
42#[derive(Debug, Default)]
44pub struct Ready;
45
46impl Bind<NeedFirstReply> {
47 #[inline]
48 pub(super) fn new(stream: TcpStream) -> Self {
49 Self {
50 stream,
51 _state: PhantomData,
52 }
53 }
54
55 pub async fn reply(mut self, reply: Reply, addr: Address) -> std::io::Result<Bind<NeedSecondReply>> {
59 let resp = Response::new(reply, addr);
60 resp.write_to_async_stream(&mut self.stream).await?;
61 Ok(Bind::<NeedSecondReply>::new(self.stream))
62 }
63
64 pub async fn accept(self, bind_addr: Address) -> std::io::Result<(Bind<NeedSecondReply>, TcpStream)> {
70 let bind_addr = bind_addr
71 .to_socket_addrs()?
72 .next()
73 .ok_or_else(|| std::io::Error::new(std::io::ErrorKind::InvalidInput, "invalid bind address"))?;
74
75 let listener = match TcpListener::bind(bind_addr).await {
76 Ok(listener) => listener,
77 Err(err) => {
78 let _ = self.reply(Reply::GeneralFailure, Address::unspecified()).await;
79 return Err(err);
80 }
81 };
82
83 let local_addr = listener.local_addr()?;
84 let bind = self.reply(Reply::Succeeded, Address::from(local_addr)).await?;
85 let (incoming, _) = listener.accept().await?;
86 Ok((bind, incoming))
87 }
88
89 pub async fn bind(self, bind_addr: Address) -> std::io::Result<(Bind<Ready>, TcpStream)> {
91 let (bind, incoming) = self.accept(bind_addr).await?;
92 let remote_addr = incoming.peer_addr()?;
93 let conn = match bind.reply(Reply::Succeeded, Address::from(remote_addr)).await {
94 Ok(conn) => conn,
95 Err((err, _stream)) => return Err(err),
96 };
97 Ok((conn, incoming))
98 }
99
100 #[inline]
102 pub async fn shutdown(&mut self) -> std::io::Result<()> {
103 self.stream.shutdown().await
104 }
105
106 #[inline]
108 pub fn local_addr(&self) -> std::io::Result<SocketAddr> {
109 self.stream.local_addr()
110 }
111
112 #[inline]
114 pub fn peer_addr(&self) -> std::io::Result<SocketAddr> {
115 self.stream.peer_addr()
116 }
117
118 #[inline]
122 pub fn nodelay(&self) -> std::io::Result<bool> {
123 self.stream.nodelay()
124 }
125
126 pub fn set_nodelay(&self, nodelay: bool) -> std::io::Result<()> {
132 self.stream.set_nodelay(nodelay)
133 }
134
135 pub fn ttl(&self) -> std::io::Result<u32> {
139 self.stream.ttl()
140 }
141
142 pub fn set_ttl(&self, ttl: u32) -> std::io::Result<()> {
146 self.stream.set_ttl(ttl)
147 }
148}
149
150impl Bind<NeedSecondReply> {
151 #[inline]
152 fn new(stream: TcpStream) -> Self {
153 Self {
154 stream,
155 _state: PhantomData,
156 }
157 }
158
159 pub async fn reply(mut self, reply: Reply, addr: Address) -> Result<Bind<Ready>, (std::io::Error, TcpStream)> {
163 let resp = Response::new(reply, addr);
164
165 if let Err(err) = resp.write_to_async_stream(&mut self.stream).await {
166 return Err((err, self.stream));
167 }
168
169 Ok(Bind::<Ready>::new(self.stream))
170 }
171
172 #[inline]
174 pub async fn shutdown(&mut self) -> std::io::Result<()> {
175 self.stream.shutdown().await
176 }
177
178 #[inline]
180 pub fn local_addr(&self) -> std::io::Result<SocketAddr> {
181 self.stream.local_addr()
182 }
183
184 #[inline]
186 pub fn peer_addr(&self) -> std::io::Result<SocketAddr> {
187 self.stream.peer_addr()
188 }
189
190 #[inline]
195 pub fn nodelay(&self) -> std::io::Result<bool> {
196 self.stream.nodelay()
197 }
198
199 pub fn set_nodelay(&self, nodelay: bool) -> std::io::Result<()> {
205 self.stream.set_nodelay(nodelay)
206 }
207
208 pub fn ttl(&self) -> std::io::Result<u32> {
212 self.stream.ttl()
213 }
214
215 pub fn set_ttl(&self, ttl: u32) -> std::io::Result<()> {
219 self.stream.set_ttl(ttl)
220 }
221}
222
223impl Bind<Ready> {
224 #[inline]
225 fn new(stream: TcpStream) -> Self {
226 Self {
227 stream,
228 _state: PhantomData,
229 }
230 }
231
232 #[inline]
234 pub fn split(&mut self) -> (ReadHalf<'_>, WriteHalf<'_>) {
235 self.stream.split()
236 }
237}
238
239impl std::ops::Deref for Bind<Ready> {
240 type Target = TcpStream;
241
242 #[inline]
243 fn deref(&self) -> &Self::Target {
244 &self.stream
245 }
246}
247
248impl std::ops::DerefMut for Bind<Ready> {
249 #[inline]
250 fn deref_mut(&mut self) -> &mut Self::Target {
251 &mut self.stream
252 }
253}
254
255impl AsyncRead for Bind<Ready> {
256 #[inline]
257 fn poll_read(mut self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &mut ReadBuf<'_>) -> Poll<std::io::Result<()>> {
258 Pin::new(&mut self.stream).poll_read(cx, buf)
259 }
260}
261
262impl AsyncWrite for Bind<Ready> {
263 #[inline]
264 fn poll_write(mut self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &[u8]) -> Poll<std::io::Result<usize>> {
265 Pin::new(&mut self.stream).poll_write(cx, buf)
266 }
267
268 #[inline]
269 fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<std::io::Result<()>> {
270 Pin::new(&mut self.stream).poll_flush(cx)
271 }
272
273 #[inline]
274 fn poll_shutdown(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<std::io::Result<()>> {
275 Pin::new(&mut self.stream).poll_shutdown(cx)
276 }
277}
278
279impl<S> From<Bind<S>> for TcpStream {
280 #[inline]
281 fn from(conn: Bind<S>) -> Self {
282 conn.stream
283 }
284}