Skip to content

Commit 76105da

Browse files
committed
socks5: fix error handling in awaiting phase
Optimize error handling in other states
1 parent 2db9a90 commit 76105da

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

socks5-client/src/lib.rs

+12-11
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,7 @@ impl Socks5 {
9595
Ok(out)
9696
}
9797
Socks5::Connected(addr) => {
98-
if input.len() != 2 {
99-
*self = Socks5::Failed(Error::InvalidReply);
100-
return Err(Error::InvalidReply);
101-
}
98+
debug_assert_eq!(input.len(), 5);
10299
if input[0] != 0x05 {
103100
*self = Socks5::Failed(Error::VersionNotSupported(input[0]));
104101
return Err(Error::VersionNotSupported(input[0]));
@@ -114,13 +111,17 @@ impl Socks5 {
114111
Ok(out)
115112
}
116113
Socks5::Awaiting => {
117-
debug_assert_eq!(input.len(), 3);
118-
if input[0] != 0x00 {
114+
debug_assert_eq!(input.len(), 5);
115+
if input[0] != 0x05 {
116+
*self = Socks5::Failed(Error::VersionNotSupported(input[0]));
117+
return Err(Error::VersionNotSupported(input[0]));
118+
}
119+
if input[1] != 0x00 {
119120
let err = ServerError::from(input[1]);
120121
*self = Socks5::Rejected(err);
121122
return Err(Error::Closed);
122123
}
123-
*self = Socks5::Reading(input[1], input[2]);
124+
*self = Socks5::Reading(input[3], input[4]);
124125
Ok(vec![])
125126
}
126127
Socks5::Reading(code1, code2) => {
@@ -141,10 +142,10 @@ impl Socks5 {
141142
match self {
142143
Socks5::Initial(_, _) => 0,
143144
Socks5::Connected(_) => 2,
144-
Socks5::Awaiting => 3,
145-
Socks5::Reading(ty, _) if *ty == IPV4 => 4,
146-
Socks5::Reading(ty, _) if *ty == IPV6 => 16,
147-
Socks5::Reading(ty, len) if *ty == DOMAIN => *len as usize,
145+
Socks5::Awaiting => 5,
146+
Socks5::Reading(ty, _) if *ty == IPV4 => 5,
147+
Socks5::Reading(ty, _) if *ty == IPV6 => 17,
148+
Socks5::Reading(ty, len) if *ty == DOMAIN => *len as usize + 1,
148149
Socks5::Reading(_, _) => 1,
149150
Socks5::Active(_) | Socks5::Rejected(_) | Socks5::Failed(_) => 0,
150151
}

0 commit comments

Comments
 (0)