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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
use core::convert::TryFrom;
use core::fmt;

use bytes::BytesMut;
use num_derive::FromPrimitive;
use num_traits::FromPrimitive;
use snafu::Snafu;

pub use addr::{Addr, HasAddr};
pub use cmd_rep::{CmdRepr, Packet, RepRepr};
pub use method_selection::{
    ReplyPacket as MethodsPacket, ReplyRepr as MethodsRepr, RequestPacket as MethodPacket,
    RequestRepr as MethodRepr,
};
pub use rfc1929::{
    ReplyPacket as AuthReplyPacket, ReplyRepr as AuthReplyRepr, RequestPacket as UserPassPacket,
    RequestRepr as UserPassRepr,
};
pub use udp::{Packet as UdpPacket, Repr as UdpRepr};

mod addr;
mod cmd_rep;
mod method_selection;
mod rfc1929;
mod udp;

#[derive(Debug, PartialEq)]
pub enum Error {
    Malformed,
    Truncated,
    #[cfg(not(all(feature = "proto-ipv4", feature = "proto-ipv6")))]
    UnsupportedAtyp,
    AddrParseError,
}

impl std::error::Error for Error {}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{:#?}", self)
    }
}

impl From<std::str::Utf8Error> for Error {
    fn from(_val: std::str::Utf8Error) -> Self {
        Error::Malformed
    }
}

impl From<smolsocket::Error> for Error {
    fn from(_val: smolsocket::Error) -> Self {
        Error::AddrParseError
    }
}

// pub(crate) type OptionResult<T> = core::result::Result<Option<T>, Error>;
pub(crate) type Result<T> = core::result::Result<T, Error>;

mod field {
    use crate::field::*;

    pub const VER: usize = 0;

    /// method selection request
    pub const NMETHODS: usize = 1;
    /// method selection request
    pub const METHODS: Rest = 2..;

    pub fn methods(nmethods: u8) -> Field {
        2..2 + nmethods as usize
    }

    /// the method server selected
    pub const METHOD: usize = 1;

    /// request CMD or reply REP
    pub const CMD_OR_REP: usize = 1;
    /// request RSV, reply RSV
    pub const RSV: usize = 2;
    /// request ATYP, reply ATYP, udp_frag ATYP
    pub const ATYP: usize = 3;
    // pub const DOMAIN_LEN: usize = 4;
    /// request or reply ADDR_PORT (DOMAIN_LEN if address is domain:port) (Variable, length of ADDR depends on ATYP, PORT is always u16)
    pub const ADDR_PORT: Rest = 4..;
    // pub const IP4_ADDR_PORT: Field = 4..10;
    // pub const IP4_ADDR: Field = 4..8;
    // pub const IP4_PORT: Field = 8..10;
    // pub const IP6_ADDR_PORT: Field = 4..22;
    // pub const IP6_ADDR: Field = 4..20;
    // pub const IP6_PORT: Field = 20..22;

    /// udp_frag RSV
    pub const UDP_RSV: Field = 0..2;
    /// udp_frag FRAG
    pub const UDP_FRAG: usize = 2;
    // udp_frag (Variable, length of ADDR depends on ATYP, PORT is always u16)
    // pub const UDP_ADDR_PORT_DATA: Rest = 4..;
    // pub const UDP_IP4_DATA: Rest = 10..;
    // pub const UDP_IP6_DATA: Rest = 22..;

    /// rfc1929 request ULEN
    pub const ULEN: usize = 1;
    /// rfc1929 request UNAME, PLEN, and PASSWD
    pub const UNAME_PLEN_PASSWD: Rest = 2..;
    /// rfc1929 reply STATUS
    pub const STATUS: usize = 1;
}

#[derive(Clone, Copy, Debug, Deserialize, Eq, FromPrimitive, Hash, PartialEq, Serialize, Snafu)]
#[repr(u8)]
pub enum Ver {
    #[snafu(display("Socks4"))]
    SOCKS4 = 0x04,
    #[snafu(display("Socks5"))]
    SOCKS5 = 0x05,
}

impl TryFrom<u8> for Ver {
    type Error = Error;

    fn try_from(val: u8) -> Result<Self> {
        FromPrimitive::from_u8(val).ok_or(Error::Malformed)
    }
}

impl Into<u8> for &Ver {
    fn into(self) -> u8 {
        *self as u8
    }
}

impl From<Ver> for u8 {
    fn from(val: Ver) -> Self {
        val as u8
    }
}

// The values currently defined for METHOD are:
//
// o  X'00' NO AUTHENTICATION REQUIRED
// o  X'01' GSSAPI
// o  X'02' USERNAME/PASSWORD
// o  X'03' to X'7F' IANA ASSIGNED
// o  X'80' to X'FE' RESERVED FOR PRIVATE METHODS
// o  X'FF' NO ACCEPTABLE METHODS
#[derive(Clone, Copy, Debug, Deserialize, Eq, FromPrimitive, Hash, PartialEq, Serialize, Snafu)]
#[repr(u8)]
pub enum Method {
    #[snafu(display("NO AUTHENTICATION REQUIRED"))]
    NoAuth = 0x00,
    #[snafu(display("GSSAPI"))]
    GssApi = 0x01,
    #[snafu(display("USERNAME/PASSWORD"))]
    UserPass = 0x02,
    // X'03' to X'7F' IANA ASSIGNED
    // X'80' to X'FE' RESERVED FOR PRIVATE METHODS
    #[snafu(display("NO ACCEPTABLE METHODS"))]
    NoMethods = 0xFF,
}

impl Method {
    pub fn try_from_slice(methods: &[u8]) -> Result<Vec<Method>> {
        let mut result = Vec::with_capacity(methods.len());
        for var in methods {
            result.push(Method::try_from(*var)?);
        }
        Ok(result)
    }
}

impl TryFrom<u8> for Method {
    type Error = Error;

    fn try_from(val: u8) -> Result<Self> {
        FromPrimitive::from_u8(val).ok_or(Error::Malformed)
    }
}

impl From<Method> for u8 {
    fn from(val: Method) -> Self {
        val as u8
    }
}

impl From<&Method> for u8 {
    fn from(val: &Method) -> Self {
        *val as u8
    }
}

trait ToU8Vec<T> {
    fn to_u8_vec(&self) -> Vec<u8>;
}

impl ToU8Vec<Method> for &[Method] {
    fn to_u8_vec(&self) -> Vec<u8> {
        self.iter().map(|&m| m as u8).collect()
    }
}

/// SOCK5 CMD Type
#[derive(Clone, Copy, Debug, Deserialize, Eq, FromPrimitive, Hash, PartialEq, Serialize, Snafu)]
#[repr(u8)]
pub enum Cmd {
    #[snafu(display("Connect"))]
    Connect = 0x01,
    #[snafu(display("Bind"))]
    Bind = 0x02,
    #[snafu(display("UDP associate"))]
    UdpAssociate = 0x03,
}

impl TryFrom<u8> for Cmd {
    type Error = Error;

    fn try_from(val: u8) -> Result<Self> {
        FromPrimitive::from_u8(val).ok_or(Error::Malformed)
    }
}

impl From<Cmd> for u8 {
    fn from(val: Cmd) -> Self {
        val as u8
    }
}

// Addressing
// In an address field (DST.ADDR, BND.ADDR), the ATYP field specifies
// the type of address contained within the field:
//
// o  X'01'
// the address is a version-4 IP address, with a length of 4 octets
//
// o  X'03'
// the address field contains a fully-qualified domain name.  The first
// octet of the address field contains the number of octets of name that
// follow, there is no terminating NUL octet.
//
// o  X'04'
// the address is a version-6 IP address, with a length of 16 octets.
#[derive(Clone, Copy, Debug, Deserialize, Eq, FromPrimitive, Hash, PartialEq, Serialize, Snafu)]
#[repr(u8)]
pub enum Atyp {
    #[snafu(display("IPv4"))]
    V4 = 0x01,
    #[snafu(display("Domain"))]
    Domain = 0x03,
    #[snafu(display("IPv6"))]
    V6 = 0x04,
}

impl TryFrom<u8> for Atyp {
    type Error = Error;

    fn try_from(val: u8) -> Result<Self> {
        FromPrimitive::from_u8(val).ok_or(Error::Malformed)
    }
}

impl From<Atyp> for u8 {
    fn from(val: Atyp) -> Self {
        val as u8
    }
}

//     Where:
//
//          o  VER    protocol version: X'05'
//          o  REP    Reply field:
//             o  X'00' succeeded
//             o  X'01' general SOCKS server failure
//             o  X'02' connection not allowed by ruleset
//             o  X'03' Network unreachable
//             o  X'04' Host unreachable
//             o  X'05' Connection refused
//             o  X'06' TTL expired
//             o  X'07' Command not supported
//             o  X'08' Address type not supported
//             o  X'09' to X'FF' unassigned
//             o  RSV    RESERVED
//             o  ATYP   address type of following address
//             o  IP V4 address: X'01'
//             o  DOMAINNAME: X'03'
//             o  IP V6 address: X'04'
//          o  BND.ADDR       server bound address
//          o  BND.PORT       server bound port in network octet order
//
// Fields marked RESERVED (RSV) must be set to X'00'.
//
// If the chosen method includes encapsulation for purposes of
// authentication, integrity and/or confidentiality, the replies are
// encapsulated in the method-dependent encapsulation.
#[derive(Clone, Copy, Debug, Deserialize, Eq, FromPrimitive, Hash, PartialEq, Serialize, Snafu)]
#[repr(u8)]
pub enum Rep {
    #[snafu(display("Succeeded"))]
    Success = 0x00,
    #[snafu(display("General SOCKS server failure"))]
    Failure = 0x01,
    #[snafu(display("Connection not allowed by ruleset"))]
    RuleFailure = 0x02,
    #[snafu(display("Network unreachable"))]
    NetworkUnreachable = 0x03,
    #[snafu(display("Host unreachable"))]
    HostUnreachable = 0x04,
    #[snafu(display("Connection refused"))]
    ConnectionRefused = 0x05,
    #[snafu(display("TTL expired"))]
    TtlExpired = 0x06,
    #[snafu(display("Command not supported"))]
    CommandNotSupported = 0x07,
    #[snafu(display("Address type not supported"))]
    AddrTypeNotSupported = 0x08,
    // X'09' to X'FF' unassigned
}

impl TryFrom<u8> for Rep {
    type Error = Error;

    fn try_from(val: u8) -> Result<Self> {
        FromPrimitive::from_u8(val).ok_or(Error::Malformed)
    }
}

impl From<Rep> for u8 {
    fn from(val: Rep) -> Self {
        val as u8
    }
}

pub(crate) trait Encodable {
    fn try_encode(&self, dst: &mut BytesMut) -> Result<()>;
}

pub(crate) trait Encoder<Item> {
    fn encode(item: &Item, dst: &mut BytesMut) -> Result<()>;
}

pub(crate) trait Decoder<Item> {
    fn decode(src: &mut BytesMut) -> Result<Option<Item>>;
}