Skip to main content

socks5_impl/protocol/
mod.rs

1mod command;
2pub mod handshake;
3mod reply;
4mod request;
5mod response;
6mod udp;
7
8pub use self::{
9    command::Command,
10    handshake::{AuthMethod, password_method},
11    reply::Reply,
12    request::Request,
13    response::Response,
14    udp::UdpHeader,
15};
16pub use ::bytes::BufMut;
17pub use ::url::Url;
18
19/// SOCKS protocol version, either 4 or 5
20#[repr(u8)]
21#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Default)]
22pub enum Version {
23    V4 = 4,
24    #[default]
25    V5 = 5,
26}
27
28impl TryFrom<u8> for Version {
29    type Error = std::io::Error;
30
31    fn try_from(value: u8) -> std::io::Result<Self> {
32        match value {
33            4 => Ok(Version::V4),
34            5 => Ok(Version::V5),
35            _ => Err(std::io::Error::new(std::io::ErrorKind::InvalidData, "invalid version")),
36        }
37    }
38}
39
40impl From<Version> for u8 {
41    fn from(v: Version) -> Self {
42        v as u8
43    }
44}
45
46impl std::fmt::Display for Version {
47    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
48        let v: u8 = (*self).into();
49        write!(f, "{v}")
50    }
51}