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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
use bytes::{Bytes, BytesMut};
use socks5_proto::{Address, Reply, Response, UdpHeader};
use std::{io::Result, net::SocketAddr};
use tokio::{
io::{AsyncReadExt, AsyncWriteExt},
net::{TcpStream, ToSocketAddrs, UdpSocket},
};
#[derive(Debug)]
pub struct Associate<S> {
stream: TcpStream,
_state: S,
}
#[derive(Debug)]
pub struct NeedReply;
#[derive(Debug)]
pub struct Ready;
impl Associate<NeedReply> {
#[inline]
pub(super) fn new(stream: TcpStream) -> Self {
Self {
stream,
_state: NeedReply,
}
}
#[inline]
pub async fn reply(mut self, reply: Reply, addr: Address) -> Result<Associate<Ready>> {
let resp = Response::new(reply, addr);
resp.write_to(&mut self.stream).await?;
Ok(Associate::<Ready>::new(self.stream))
}
#[inline]
pub fn local_addr(&self) -> Result<SocketAddr> {
self.stream.local_addr()
}
#[inline]
pub fn peer_addr(&self) -> Result<SocketAddr> {
self.stream.peer_addr()
}
#[inline]
pub async fn shutdown(&mut self) -> Result<()> {
self.stream.shutdown().await
}
}
impl Associate<Ready> {
#[inline]
fn new(stream: TcpStream) -> Self {
Self {
stream,
_state: Ready,
}
}
pub async fn wait_until_closed(&mut self) -> Result<()> {
loop {
match self.stream.read(&mut [0]).await {
Ok(0) => break Ok(()),
Ok(_) => {}
Err(err) => break Err(err),
}
}
}
#[inline]
pub fn local_addr(&self) -> Result<SocketAddr> {
self.stream.local_addr()
}
#[inline]
pub fn peer_addr(&self) -> Result<SocketAddr> {
self.stream.peer_addr()
}
#[inline]
pub async fn shutdown(&mut self) -> Result<()> {
self.stream.shutdown().await
}
}
#[derive(Debug)]
pub struct AssociateUdpSocket(UdpSocket);
impl AssociateUdpSocket {
#[inline]
pub async fn connect<A: ToSocketAddrs>(&self, addr: A) -> Result<()> {
self.0.connect(addr).await
}
#[inline]
pub fn local_addr(&self) -> Result<SocketAddr> {
self.0.local_addr()
}
#[inline]
pub fn peer_addr(&self) -> Result<SocketAddr> {
self.0.peer_addr()
}
pub async fn recv(&self) -> Result<(Bytes, u8, Address)> {
loop {
let mut buf = vec![0; 65535];
let len = self.0.recv(&mut buf).await?;
buf.truncate(len);
let pkt = Bytes::from(buf);
if let Ok(header) = UdpHeader::read_from(&mut pkt.as_ref()).await {
return Ok((pkt, header.frag, header.address));
}
}
}
pub async fn recv_from(&self) -> Result<(Bytes, u8, Address, SocketAddr)> {
loop {
let mut buf = vec![0; 65535];
let (len, src_addr) = self.0.recv_from(&mut buf).await?;
buf.truncate(len);
let pkt = Bytes::from(buf);
if let Ok(header) = UdpHeader::read_from(&mut pkt.as_ref()).await {
let pkt = pkt.slice(header.serialized_len()..);
return Ok((pkt, header.frag, header.address, src_addr));
}
}
}
pub async fn send<P: AsRef<[u8]>>(
&self,
pkt: P,
frag: u8,
from_addr: Address,
) -> Result<usize> {
let header = UdpHeader::new(frag, from_addr);
let mut buf = BytesMut::with_capacity(header.serialized_len() + pkt.as_ref().len());
header.write_to_buf(&mut buf);
buf.extend_from_slice(pkt.as_ref());
self.0
.send(&buf)
.await
.map(|len| len - header.serialized_len())
}
pub async fn send_to<P: AsRef<[u8]>>(
&self,
pkt: P,
frag: u8,
from_addr: Address,
to_addr: SocketAddr,
) -> Result<usize> {
let header = UdpHeader::new(frag, from_addr);
let mut buf = BytesMut::with_capacity(header.serialized_len() + pkt.as_ref().len());
header.write_to_buf(&mut buf);
buf.extend_from_slice(pkt.as_ref());
self.0
.send_to(&buf, to_addr)
.await
.map(|len| len - header.serialized_len())
}
}
impl From<UdpSocket> for AssociateUdpSocket {
#[inline]
fn from(socket: UdpSocket) -> Self {
AssociateUdpSocket(socket)
}
}
impl From<AssociateUdpSocket> for UdpSocket {
#[inline]
fn from(associate: AssociateUdpSocket) -> Self {
associate.0
}
}