dlzht_socks5/errors.rs
1//! error module include all errors occur handling SOCKS5
2
3use std::fmt::{Display, Formatter};
4
5#[derive(Debug)]
6pub enum SocksError {
7 StdIoError(std::io::Error),
8
9 /// Parse SOCKS5 protocol error
10 InvalidPackageErr(InvalidPackageKind),
11
12 /// Invalid auth method, not 0x00 or 0x02
13 UnsupportedAuthMethod,
14
15 /// Username/Password auth is not passed
16 PasswordAuthNotPassed,
17
18 /// None auth method is supported error
19 UnsupportedCommand(u8),
20
21 /// Execute command error
22 ExecuteCommandErr(ExecuteCmdKind),
23
24 /// Build SOCKS5 server error
25 BuildSocksServerErr(BuildSocksKind),
26
27 /// Build SOCKS5 client error
28 BuildSocksClientErr(BuildSocksKind),
29}
30
31/// Errors that can occur parsing SOCKS5 protocol.
32#[derive(Debug)]
33pub enum InvalidPackageKind {
34 /// `Version` byte is not 0x05
35 InvalidSocks5Version(u8),
36
37 /// None auth method is specified
38 InvalidAuthMethodNum(u8),
39
40 /// Duplicated auth method is specified
41 DuplicatedAuthMethod(u8),
42
43 /// `Version of the sub negotiation` byte is not 0x01
44 InvalidSubNegVersion(u8),
45
46 /// Length of username can not be 0
47 InvalidUsernameLength(u8),
48
49 /// Length of password can not be 0
50 InvalidPasswordLength(u8),
51
52 /// `RSV` byte is not 0x00
53 InvalidSocks5RsvByte(u8),
54
55 /// `ATYP` byte is not IP V4 address(0x01), DOMAINNAME(0x03) or IP V6 address(0x04)
56 InvalidAddressType(u8),
57
58 /// `CMD` byte is not CONNECT(0x01), BIND(0x02) or UDP ASSOCIATE(0x03)
59 InvalidRequestsCmd(u8),
60
61 /// Domain name is not valid UFT8 string
62 InvalidDomainAddress(Vec<u8>),
63}
64
65/// Errors of building SOCKS5 client or server
66#[derive(Debug)]
67pub enum BuildSocksKind {
68 /// invalid username, length(byte) of username is 0 or greater than 255
69 InvalidUsername,
70
71 /// invalid password, length(byte) of password is 0 or greater than 255
72 InvalidPassword,
73
74 /// invalid auth method, neither `NO AUTHENTICATION REQUIRED` nor `USERNAME/PASSWORD` is specified
75 InvalidAuthMethod,
76}
77
78/// Errors of executing socks5 command
79#[derive(Debug)]
80pub enum ExecuteCmdKind {
81 /// Errors occur in server side, inner error is `std::io::Error`
82 Server(std::io::Error),
83
84 /// Errors occur in client side, receiving from server
85 Client(u8),
86}
87
88impl Display for SocksError {
89 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
90 return write!(f, "{:?}", self);
91 }
92}
93
94impl std::error::Error for SocksError {}
95
96impl From<std::io::Error> for SocksError {
97 fn from(error: std::io::Error) -> Self {
98 return SocksError::StdIoError(error);
99 }
100}
101
102impl From<std::string::FromUtf8Error> for SocksError {
103 fn from(error: std::string::FromUtf8Error) -> Self {
104 return SocksError::InvalidPackageErr(InvalidPackageKind::InvalidDomainAddress(
105 error.into_bytes(),
106 ));
107 }
108}
109
110/// Result type often returned from methods that can have socks5 `Error`s.
111pub type SocksResult<T> = Result<T, SocksError>;