Skip to content

Commit

Permalink
socks5: read remanining data from stream upon failure
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-orlovsky committed May 5, 2024
1 parent f49d780 commit 9335259
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions socks5-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ pub enum Socks5 {
Awaiting,
Reading(u8, u8),
Active(NetAddr<HostName>),
Rejected(ServerError),
Rejected(ServerError, u8, u8),
Failed(Error),
}

Expand Down Expand Up @@ -116,10 +116,10 @@ impl Socks5 {
}
if input[1] != 0x00 {
let err = ServerError::from(input[1]);
*self = Socks5::Rejected(err);
return Err(Error::Closed);
*self = Socks5::Rejected(err, input[3], input[4]);
} else {
*self = Socks5::Reading(input[3], input[4]);
}
*self = Socks5::Reading(input[3], input[4]);
Ok(vec![])
}
Socks5::Reading(code1, code2) => {
Expand All @@ -132,7 +132,7 @@ impl Socks5 {
Ok(vec![])
}
Socks5::Active(_) => Err(Error::Completed),
Socks5::Rejected(_) | Socks5::Failed(_) => Err(Error::Closed),
Socks5::Rejected(_, _, _) | Socks5::Failed(_) => Err(Error::Closed),
}
}

Expand All @@ -141,11 +141,13 @@ impl Socks5 {
Socks5::Initial(_, _) => 0,
Socks5::Connected(_) => 2,
Socks5::Awaiting => 5,
Socks5::Reading(ty, _) if *ty == IPV4 => 5,
Socks5::Reading(ty, _) if *ty == IPV6 => 17,
Socks5::Reading(ty, len) if *ty == DOMAIN => *len as usize + 1,
Socks5::Reading(_, _) => 1,
Socks5::Active(_) | Socks5::Rejected(_) | Socks5::Failed(_) => 0,
Socks5::Reading(ty, _) | Socks5::Rejected(_, ty, _) if *ty == IPV4 => 5,
Socks5::Reading(ty, _) | Socks5::Rejected(_, ty, _) if *ty == IPV6 => 17,
Socks5::Reading(ty, len) | Socks5::Rejected(_, ty, len) if *ty == DOMAIN => {
*len as usize + 1
}
Socks5::Reading(_, _) | Socks5::Rejected(_, _, _) => 1,
Socks5::Active(_) | Socks5::Failed(_) => 0,
}
}
}

0 comments on commit 9335259

Please sign in to comment.