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
use std::net::SocketAddr;

use crate::error::ProxyStreamError;

#[derive(Debug, Clone)]
pub enum DestinationAddress {
    Domain(String, u16),
    Ip(SocketAddr),
}

impl DestinationAddress {
    pub fn to_bytes(&self) -> Vec<u8> {
        match self {
            DestinationAddress::Domain(domain, port) => {
                [domain.as_bytes(), port.to_be_bytes().as_ref()].concat()
            }
            DestinationAddress::Ip(addr) => match addr {
                SocketAddr::V4(addr) => {
                    [&addr.ip().octets(), addr.port().to_be_bytes().as_ref()].concat()
                }
                SocketAddr::V6(addr) => {
                    [&addr.ip().octets(), addr.port().to_be_bytes().as_ref()].concat()
                }
            },
        }
    }
    pub fn from_bytes(buf: &[u8], ip: bool) -> Result<Self, ProxyStreamError> {
        if buf.len() < 3 {
            return Err(ProxyStreamError::InvalidAddress);
        }
        if ip {
            let port = u16::from_be_bytes([buf[buf.len() - 2], buf[buf.len() - 1]]);
            let ip = match buf.len() {
                6 => {
                    let mut octets = [0; 4];
                    octets.copy_from_slice(&buf[0..4]);
                    SocketAddr::V4(std::net::SocketAddrV4::new(
                        std::net::Ipv4Addr::new(octets[0], octets[1], octets[2], octets[3]),
                        port,
                    ))
                }
                18 => {
                    let mut octets = [0; 16];
                    octets.copy_from_slice(&buf[0..16]);
                    SocketAddr::V6(std::net::SocketAddrV6::new(
                        std::net::Ipv6Addr::new(
                            u16::from_be_bytes([octets[0], octets[1]]),
                            u16::from_be_bytes([octets[2], octets[3]]),
                            u16::from_be_bytes([octets[4], octets[5]]),
                            u16::from_be_bytes([octets[6], octets[7]]),
                            u16::from_be_bytes([octets[8], octets[9]]),
                            u16::from_be_bytes([octets[10], octets[11]]),
                            u16::from_be_bytes([octets[12], octets[13]]),
                            u16::from_be_bytes([octets[14], octets[15]]),
                        ),
                        port,
                        0,
                        0,
                    ))
                }
                _ => return Err(ProxyStreamError::InvalidAddress),
            };
            Ok(DestinationAddress::Ip(ip))
        } else {
            let port = u16::from_be_bytes([buf[buf.len() - 2], buf[buf.len() - 1]]);
            let domain = String::from_utf8_lossy(&buf[0..buf.len() - 2]).to_string();
            Ok(DestinationAddress::Domain(domain, port))
        }
    }
}

impl Default for DestinationAddress {
    fn default() -> Self {
        DestinationAddress::Ip(SocketAddr::from(([0, 0, 0, 0], 0)))
    }
}
pub trait ToSocketDestination {
    fn to_destination_address(&self) -> Result<DestinationAddress, ProxyStreamError>;
}

impl ToSocketDestination for SocketAddr {
    fn to_destination_address(&self) -> Result<DestinationAddress, ProxyStreamError> {
        Ok(DestinationAddress::Ip(*self))
    }
}

impl ToSocketDestination for &str {
    fn to_destination_address(&self) -> Result<DestinationAddress, ProxyStreamError> {
        if let Ok(ip) = self.parse::<SocketAddr>() {
            return Ok(DestinationAddress::Ip(ip));
        }
        self.rsplit_once(':')
            .and_then(|(domain, port)| {
                port.parse::<u16>()
                    .ok()
                    .map(|port| DestinationAddress::Domain(domain.to_string(), port))
            })
            .ok_or(ProxyStreamError::InvalidAddress)
    }
}