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
use crate::{Error, Result};
use caret::caret_int;
use std::fmt;
use std::net::IpAddr;
use tor_error::bad_api_usage;
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum SocksVersion {
V4,
V5,
}
impl TryFrom<u8> for SocksVersion {
type Error = Error;
fn try_from(v: u8) -> Result<SocksVersion> {
match v {
4 => Ok(SocksVersion::V4),
5 => Ok(SocksVersion::V5),
_ => Err(Error::BadProtocol(v)),
}
}
}
#[derive(Clone, Debug)]
pub struct SocksRequest {
version: SocksVersion,
cmd: SocksCmd,
addr: SocksAddr,
port: u16,
auth: SocksAuth,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[allow(clippy::exhaustive_enums)]
pub enum SocksAddr {
Hostname(SocksHostname),
Ip(IpAddr),
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct SocksHostname(String);
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum SocksAuth {
NoAuth,
Socks4(Vec<u8>),
Username(Vec<u8>, Vec<u8>),
}
caret_int! {
pub struct SocksCmd(u8) {
CONNECT = 1,
BIND = 2,
UDP_ASSOCIATE = 3,
RESOLVE = 0xF0,
RESOLVE_PTR = 0xF1,
}
}
caret_int! {
pub struct SocksStatus(u8) {
SUCCEEDED = 0x00,
GENERAL_FAILURE = 0x01,
NOT_ALLOWED = 0x02,
NETWORK_UNREACHABLE = 0x03,
HOST_UNREACHABLE = 0x04,
CONNECTION_REFUSED = 0x05,
TTL_EXPIRED = 0x06,
COMMAND_NOT_SUPPORTED = 0x07,
ADDRTYPE_NOT_SUPPORTED = 0x08,
}
}
impl SocksCmd {
fn recognized(self) -> bool {
matches!(
self,
SocksCmd::CONNECT | SocksCmd::RESOLVE | SocksCmd::RESOLVE_PTR
)
}
fn requires_port(self) -> bool {
matches!(
self,
SocksCmd::CONNECT | SocksCmd::BIND | SocksCmd::UDP_ASSOCIATE
)
}
}
impl SocksStatus {
pub(crate) fn into_socks4_status(self) -> u8 {
match self {
SocksStatus::SUCCEEDED => 0x5A,
_ => 0x5B,
}
}
}
impl TryFrom<String> for SocksHostname {
type Error = Error;
fn try_from(s: String) -> Result<SocksHostname> {
if s.len() > 255 {
Err(bad_api_usage!("hostname too long").into())
} else {
Ok(SocksHostname(s))
}
}
}
impl AsRef<str> for SocksHostname {
fn as_ref(&self) -> &str {
self.0.as_ref()
}
}
impl SocksRequest {
pub(crate) fn new(
version: SocksVersion,
cmd: SocksCmd,
addr: SocksAddr,
port: u16,
auth: SocksAuth,
) -> Result<Self> {
if !cmd.recognized() {
return Err(Error::NotImplemented);
}
if port == 0 && cmd.requires_port() {
return Err(Error::Syntax);
}
Ok(SocksRequest {
version,
cmd,
addr,
port,
auth,
})
}
pub fn version(&self) -> SocksVersion {
self.version
}
pub fn command(&self) -> SocksCmd {
self.cmd
}
pub fn auth(&self) -> &SocksAuth {
&self.auth
}
pub fn port(&self) -> u16 {
self.port
}
pub fn addr(&self) -> &SocksAddr {
&self.addr
}
}
impl fmt::Display for SocksAddr {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
SocksAddr::Ip(a) => write!(f, "{}", a),
SocksAddr::Hostname(h) => write!(f, "{}", h.0),
}
}
}
#[cfg(test)]
mod test {
#![allow(clippy::unwrap_used)]
use super::*;
#[test]
fn display_sa() {
let a = SocksAddr::Ip(IpAddr::V4("127.0.0.1".parse().unwrap()));
assert_eq!(a.to_string(), "127.0.0.1");
let a = SocksAddr::Ip(IpAddr::V6("f00::9999".parse().unwrap()));
assert_eq!(a.to_string(), "f00::9999");
let a = SocksAddr::Hostname("www.torproject.org".to_string().try_into().unwrap());
assert_eq!(a.to_string(), "www.torproject.org");
}
#[test]
fn ok_request() {
let localhost_v4 = SocksAddr::Ip(IpAddr::V4("127.0.0.1".parse().unwrap()));
let r = SocksRequest::new(
SocksVersion::V4,
SocksCmd::CONNECT,
localhost_v4.clone(),
1024,
SocksAuth::NoAuth,
)
.unwrap();
assert_eq!(r.version(), SocksVersion::V4);
assert_eq!(r.command(), SocksCmd::CONNECT);
assert_eq!(r.addr(), &localhost_v4);
assert_eq!(r.auth(), &SocksAuth::NoAuth);
}
#[test]
fn bad_request() {
let localhost_v4 = SocksAddr::Ip(IpAddr::V4("127.0.0.1".parse().unwrap()));
let e = SocksRequest::new(
SocksVersion::V4,
SocksCmd::BIND,
localhost_v4.clone(),
1024,
SocksAuth::NoAuth,
);
assert!(matches!(e, Err(Error::NotImplemented)));
let e = SocksRequest::new(
SocksVersion::V4,
SocksCmd::CONNECT,
localhost_v4,
0,
SocksAuth::NoAuth,
);
assert!(matches!(e, Err(Error::Syntax)));
}
}