socks5_impl/server/connection/
bind.rs1use crate::protocol::{Address, AsyncStreamOperation, Reply, Response};
2use std::{
3 marker::PhantomData,
4 net::SocketAddr,
5 pin::Pin,
6 task::{Context, Poll},
7 time::Duration,
8};
9use tokio::{
10 io::{AsyncRead, AsyncWrite, AsyncWriteExt, ReadBuf},
11 net::{
12 TcpStream,
13 tcp::{ReadHalf, WriteHalf},
14 },
15};
16
17#[derive(Debug)]
27pub struct Bind<S> {
28 stream: TcpStream,
29 _state: PhantomData<S>,
30}
31
32#[derive(Debug, Default)]
34pub struct NeedFirstReply;
35
36#[derive(Debug, Default)]
38pub struct NeedSecondReply;
39
40#[derive(Debug, Default)]
42pub struct Ready;
43
44impl Bind<NeedFirstReply> {
45 #[inline]
46 pub(super) fn new(stream: TcpStream) -> Self {
47 Self {
48 stream,
49 _state: PhantomData,
50 }
51 }
52
53 pub async fn reply(mut self, reply: Reply, addr: Address) -> std::io::Result<Bind<NeedSecondReply>> {
57 let resp = Response::new(reply, addr);
58 resp.write_to_async_stream(&mut self.stream).await?;
59 Ok(Bind::<NeedSecondReply>::new(self.stream))
60 }
61
62 #[inline]
64 pub async fn shutdown(&mut self) -> std::io::Result<()> {
65 self.stream.shutdown().await
66 }
67
68 #[inline]
70 pub fn local_addr(&self) -> std::io::Result<SocketAddr> {
71 self.stream.local_addr()
72 }
73
74 #[inline]
76 pub fn peer_addr(&self) -> std::io::Result<SocketAddr> {
77 self.stream.peer_addr()
78 }
79
80 #[inline]
84 pub fn nodelay(&self) -> std::io::Result<bool> {
85 self.stream.nodelay()
86 }
87
88 pub fn set_nodelay(&self, nodelay: bool) -> std::io::Result<()> {
94 self.stream.set_nodelay(nodelay)
95 }
96
97 pub fn ttl(&self) -> std::io::Result<u32> {
101 self.stream.ttl()
102 }
103
104 pub fn set_ttl(&self, ttl: u32) -> std::io::Result<()> {
108 self.stream.set_ttl(ttl)
109 }
110}
111
112impl Bind<NeedSecondReply> {
113 #[inline]
114 fn new(stream: TcpStream) -> Self {
115 Self {
116 stream,
117 _state: PhantomData,
118 }
119 }
120
121 pub async fn reply(mut self, reply: Reply, addr: Address) -> Result<Bind<Ready>, (std::io::Error, TcpStream)> {
125 let resp = Response::new(reply, addr);
126
127 if let Err(err) = resp.write_to_async_stream(&mut self.stream).await {
128 return Err((err, self.stream));
129 }
130
131 Ok(Bind::<Ready>::new(self.stream))
132 }
133
134 #[inline]
136 pub async fn shutdown(&mut self) -> std::io::Result<()> {
137 self.stream.shutdown().await
138 }
139
140 #[inline]
142 pub fn local_addr(&self) -> std::io::Result<SocketAddr> {
143 self.stream.local_addr()
144 }
145
146 #[inline]
148 pub fn peer_addr(&self) -> std::io::Result<SocketAddr> {
149 self.stream.peer_addr()
150 }
151
152 #[inline]
157 pub fn nodelay(&self) -> std::io::Result<bool> {
158 self.stream.nodelay()
159 }
160
161 pub fn set_nodelay(&self, nodelay: bool) -> std::io::Result<()> {
167 self.stream.set_nodelay(nodelay)
168 }
169
170 pub fn ttl(&self) -> std::io::Result<u32> {
174 self.stream.ttl()
175 }
176
177 pub fn set_ttl(&self, ttl: u32) -> std::io::Result<()> {
181 self.stream.set_ttl(ttl)
182 }
183}
184
185impl Bind<Ready> {
186 #[inline]
187 fn new(stream: TcpStream) -> Self {
188 Self {
189 stream,
190 _state: PhantomData,
191 }
192 }
193
194 #[inline]
196 pub fn split(&mut self) -> (ReadHalf<'_>, WriteHalf<'_>) {
197 self.stream.split()
198 }
199}
200
201impl std::ops::Deref for Bind<Ready> {
202 type Target = TcpStream;
203
204 #[inline]
205 fn deref(&self) -> &Self::Target {
206 &self.stream
207 }
208}
209
210impl std::ops::DerefMut for Bind<Ready> {
211 #[inline]
212 fn deref_mut(&mut self) -> &mut Self::Target {
213 &mut self.stream
214 }
215}
216
217impl AsyncRead for Bind<Ready> {
218 #[inline]
219 fn poll_read(mut self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &mut ReadBuf<'_>) -> Poll<std::io::Result<()>> {
220 Pin::new(&mut self.stream).poll_read(cx, buf)
221 }
222}
223
224impl AsyncWrite for Bind<Ready> {
225 #[inline]
226 fn poll_write(mut self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &[u8]) -> Poll<std::io::Result<usize>> {
227 Pin::new(&mut self.stream).poll_write(cx, buf)
228 }
229
230 #[inline]
231 fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<std::io::Result<()>> {
232 Pin::new(&mut self.stream).poll_flush(cx)
233 }
234
235 #[inline]
236 fn poll_shutdown(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<std::io::Result<()>> {
237 Pin::new(&mut self.stream).poll_shutdown(cx)
238 }
239}
240
241impl<S> From<Bind<S>> for TcpStream {
242 #[inline]
243 fn from(conn: Bind<S>) -> Self {
244 conn.stream
245 }
246}