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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
use core::convert::TryFrom;
use core::fmt;
use bytes::BytesMut;
use num_derive::FromPrimitive;
use num_traits::FromPrimitive;
use snafu::Snafu;
pub use addr::{Addr, HasAddr};
pub use cmd_rep::{CmdRepr, Packet, RepRepr};
pub use method_selection::{
ReplyPacket as MethodsPacket, ReplyRepr as MethodsRepr, RequestPacket as MethodPacket,
RequestRepr as MethodRepr,
};
pub use rfc1929::{
ReplyPacket as AuthReplyPacket, ReplyRepr as AuthReplyRepr, RequestPacket as UserPassPacket,
RequestRepr as UserPassRepr,
};
pub use udp::{Packet as UdpPacket, Repr as UdpRepr};
mod addr;
mod cmd_rep;
mod method_selection;
mod rfc1929;
mod udp;
#[derive(Debug, PartialEq)]
pub enum Error {
Malformed,
Truncated,
#[cfg(not(all(feature = "proto-ipv4", feature = "proto-ipv6")))]
UnsupportedAtyp,
AddrParseError,
}
impl std::error::Error for Error {}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:#?}", self)
}
}
impl From<std::str::Utf8Error> for Error {
fn from(_val: std::str::Utf8Error) -> Self {
Error::Malformed
}
}
impl From<smolsocket::Error> for Error {
fn from(_val: smolsocket::Error) -> Self {
Error::AddrParseError
}
}
pub(crate) type Result<T> = core::result::Result<T, Error>;
mod field {
use crate::field::*;
pub const VER: usize = 0;
pub const NMETHODS: usize = 1;
pub const METHODS: Rest = 2..;
pub fn methods(nmethods: u8) -> Field {
2..2 + nmethods as usize
}
pub const METHOD: usize = 1;
pub const CMD_OR_REP: usize = 1;
pub const RSV: usize = 2;
pub const ATYP: usize = 3;
pub const ADDR_PORT: Rest = 4..;
pub const UDP_RSV: Field = 0..2;
pub const UDP_FRAG: usize = 2;
pub const ULEN: usize = 1;
pub const UNAME_PLEN_PASSWD: Rest = 2..;
pub const STATUS: usize = 1;
}
#[derive(Clone, Copy, Debug, Deserialize, Eq, FromPrimitive, Hash, PartialEq, Serialize, Snafu)]
#[repr(u8)]
pub enum Ver {
#[snafu(display("Socks4"))]
SOCKS4 = 0x04,
#[snafu(display("Socks5"))]
SOCKS5 = 0x05,
}
impl TryFrom<u8> for Ver {
type Error = Error;
fn try_from(val: u8) -> Result<Self> {
FromPrimitive::from_u8(val).ok_or(Error::Malformed)
}
}
impl Into<u8> for &Ver {
fn into(self) -> u8 {
*self as u8
}
}
impl From<Ver> for u8 {
fn from(val: Ver) -> Self {
val as u8
}
}
#[derive(Clone, Copy, Debug, Deserialize, Eq, FromPrimitive, Hash, PartialEq, Serialize, Snafu)]
#[repr(u8)]
pub enum Method {
#[snafu(display("NO AUTHENTICATION REQUIRED"))]
NoAuth = 0x00,
#[snafu(display("GSSAPI"))]
GssApi = 0x01,
#[snafu(display("USERNAME/PASSWORD"))]
UserPass = 0x02,
#[snafu(display("NO ACCEPTABLE METHODS"))]
NoMethods = 0xFF,
}
impl Method {
pub fn try_from_slice(methods: &[u8]) -> Result<Vec<Method>> {
let mut result = Vec::with_capacity(methods.len());
for var in methods {
result.push(Method::try_from(*var)?);
}
Ok(result)
}
}
impl TryFrom<u8> for Method {
type Error = Error;
fn try_from(val: u8) -> Result<Self> {
FromPrimitive::from_u8(val).ok_or(Error::Malformed)
}
}
impl From<Method> for u8 {
fn from(val: Method) -> Self {
val as u8
}
}
impl From<&Method> for u8 {
fn from(val: &Method) -> Self {
*val as u8
}
}
trait ToU8Vec<T> {
fn to_u8_vec(&self) -> Vec<u8>;
}
impl ToU8Vec<Method> for &[Method] {
fn to_u8_vec(&self) -> Vec<u8> {
self.iter().map(|&m| m as u8).collect()
}
}
#[derive(Clone, Copy, Debug, Deserialize, Eq, FromPrimitive, Hash, PartialEq, Serialize, Snafu)]
#[repr(u8)]
pub enum Cmd {
#[snafu(display("Connect"))]
Connect = 0x01,
#[snafu(display("Bind"))]
Bind = 0x02,
#[snafu(display("UDP associate"))]
UdpAssociate = 0x03,
}
impl TryFrom<u8> for Cmd {
type Error = Error;
fn try_from(val: u8) -> Result<Self> {
FromPrimitive::from_u8(val).ok_or(Error::Malformed)
}
}
impl From<Cmd> for u8 {
fn from(val: Cmd) -> Self {
val as u8
}
}
#[derive(Clone, Copy, Debug, Deserialize, Eq, FromPrimitive, Hash, PartialEq, Serialize, Snafu)]
#[repr(u8)]
pub enum Atyp {
#[snafu(display("IPv4"))]
V4 = 0x01,
#[snafu(display("Domain"))]
Domain = 0x03,
#[snafu(display("IPv6"))]
V6 = 0x04,
}
impl TryFrom<u8> for Atyp {
type Error = Error;
fn try_from(val: u8) -> Result<Self> {
FromPrimitive::from_u8(val).ok_or(Error::Malformed)
}
}
impl From<Atyp> for u8 {
fn from(val: Atyp) -> Self {
val as u8
}
}
#[derive(Clone, Copy, Debug, Deserialize, Eq, FromPrimitive, Hash, PartialEq, Serialize, Snafu)]
#[repr(u8)]
pub enum Rep {
#[snafu(display("Succeeded"))]
Success = 0x00,
#[snafu(display("General SOCKS server failure"))]
Failure = 0x01,
#[snafu(display("Connection not allowed by ruleset"))]
RuleFailure = 0x02,
#[snafu(display("Network unreachable"))]
NetworkUnreachable = 0x03,
#[snafu(display("Host unreachable"))]
HostUnreachable = 0x04,
#[snafu(display("Connection refused"))]
ConnectionRefused = 0x05,
#[snafu(display("TTL expired"))]
TtlExpired = 0x06,
#[snafu(display("Command not supported"))]
CommandNotSupported = 0x07,
#[snafu(display("Address type not supported"))]
AddrTypeNotSupported = 0x08,
}
impl TryFrom<u8> for Rep {
type Error = Error;
fn try_from(val: u8) -> Result<Self> {
FromPrimitive::from_u8(val).ok_or(Error::Malformed)
}
}
impl From<Rep> for u8 {
fn from(val: Rep) -> Self {
val as u8
}
}
pub(crate) trait Encodable {
fn try_encode(&self, dst: &mut BytesMut) -> Result<()>;
}
pub(crate) trait Encoder<Item> {
fn encode(item: &Item, dst: &mut BytesMut) -> Result<()>;
}
pub(crate) trait Decoder<Item> {
fn decode(src: &mut BytesMut) -> Result<Option<Item>>;
}