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
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
#[macro_use]
extern crate amplify;
mod encoding;
mod error;
use std::io;
use cypheraddr::{Host, HostName, NetAddr};
pub use error::ServerError;
use crate::encoding::{Encoding, EncodingError, DOMAIN, IPV4, IPV6};
#[derive(Debug, Display, Error, From)]
#[display(inner)]
pub enum Error {
#[from]
Server(ServerError),
#[from]
Encoding(EncodingError),
InvalidReply,
VersionNotSupported(u8),
AuthRequired,
Completed,
Closed,
}
#[derive(Debug)]
pub enum Socks5 {
Initial(NetAddr<HostName>, bool),
Connected(NetAddr<HostName>),
Awaiting,
Reading(u8, u8),
Active(NetAddr<HostName>),
Rejected(ServerError),
Failed(Error),
}
impl Socks5 {
pub fn with(addr: impl Into<NetAddr<HostName>>, force_proxy: bool) -> Self {
Self::Initial(addr.into(), force_proxy)
}
pub fn advance(&mut self, input: &[u8]) -> Result<Vec<u8>, Error> {
match self {
Socks5::Initial(addr, false) if !addr.requires_proxy() => {
*self = Socks5::Active(addr.clone());
Ok(vec![])
}
Socks5::Initial(addr, _) => {
debug_assert!(input.is_empty());
let out = vec![0x05, 0x01, 0x00];
*self = Socks5::Connected(addr.clone());
Ok(out)
}
Socks5::Connected(addr) => {
if input.len() != 2 {
*self = Socks5::Failed(Error::InvalidReply);
return Err(Error::InvalidReply);
}
if input[0] != 0x05 {
*self = Socks5::Failed(Error::VersionNotSupported(input[0]));
return Err(Error::VersionNotSupported(input[0]));
}
if input[1] != 0x00 {
*self = Socks5::Failed(Error::AuthRequired);
return Err(Error::AuthRequired);
}
let mut out = vec![0x05, 0x01, 0x00];
addr.encode(&mut out)?;
*self = Socks5::Awaiting;
Ok(out)
}
Socks5::Awaiting => {
debug_assert_eq!(input.len(), 3);
if input[0] != 0x00 {
let err = ServerError::from(input[1]);
*self = Socks5::Rejected(err);
return Err(Error::Closed);
}
*self = Socks5::Reading(input[1], input[2]);
Ok(vec![])
}
Socks5::Reading(code1, code2) => {
let mut vec = Vec::with_capacity(input.len() + 2);
vec.extend_from_slice(&[*code1, *code2]);
vec.extend_from_slice(input);
let mut cursor = io::Cursor::new(vec);
let addr = NetAddr::<HostName>::decode(&mut cursor)?;
*self = Socks5::Active(addr);
Ok(vec![])
}
Socks5::Active(_) => Err(Error::Completed),
_ => Err(Error::Closed),
}
}
pub fn next_read_len(&self) -> usize {
match self {
Socks5::Initial(_, _) => 0,
Socks5::Connected(_) => 2,
Socks5::Awaiting => 3,
Socks5::Reading(ty, _) if *ty == IPV4 => 4,
Socks5::Reading(ty, _) if *ty == IPV6 => 16,
Socks5::Reading(ty, len) if *ty == DOMAIN => *len as usize,
Socks5::Reading(_, _) => 1,
Socks5::Active(_) | Socks5::Rejected(_) | Socks5::Failed(_) => 0,
}
}
}