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
use byteorder::{NetworkEndian, WriteBytesExt};
use hyper::client::connect;
use std::io;
use std::net::SocketAddr;
use std::net::{Ipv4Addr, Ipv6Addr};
use tokio::net::tcp::TcpStream;
use tokio::prelude::*;


pub enum ProxyDest {
    Ipv4Addr(Ipv4Addr),
    Ipv6Addr(Ipv6Addr),
    Domain(String),
}

impl ProxyDest {
    fn apply_ipv4(buf: &mut Vec<u8>, addr: Ipv4Addr) {
        info!("Setting socks5 destination as ipv4: {:?}", addr);
        buf.push(0x01); // ipv4
        buf.extend(&addr.octets());
    }

    fn apply_ipv6(buf: &mut Vec<u8>, addr: Ipv6Addr) {
        info!("Setting socks5 destination as ipv6: {:?}", addr);
        buf.push(0x04); // ipv6
        buf.extend(&addr.octets());
    }

    fn apply_domain(buf: &mut Vec<u8>, domain: &str) {
        info!("Setting socks5 destination as domain: {:?}", domain);
        let domain = domain.bytes();
        buf.push(0x03); // domain
        buf.push(domain.len() as u8);
        buf.extend(domain);
    }

    fn apply(&self, buf: &mut Vec<u8>) {
        match self {
            ProxyDest::Ipv4Addr(addr) => Self::apply_ipv4(buf, *addr),
            ProxyDest::Ipv6Addr(addr) => Self::apply_ipv6(buf, *addr),
            ProxyDest::Domain(domain) => Self::apply_domain(buf, domain),
        }
    }

    pub fn from_hyper(dest: connect::Destination) -> (ProxyDest, u16) {
        let port = match (dest.scheme(), dest.port()) {
            (_, Some(port)) => port,
            ("https", None) => 443,
            ("http", None) => 80,
            (_, None) => 443, // TODO: raise error
        };

        let host = dest.host();
        let host = match host.parse::<Ipv4Addr>() {
            Ok(ipaddr) => ProxyDest::Ipv4Addr(ipaddr),
            _ => ProxyDest::Domain(host.to_string()),
        };

        (host, port)
    }
}

/// A `Future` that will resolve to an tcp connection.
#[must_use = "futures do nothing unless polled"]
pub struct ConnectionFuture(Box<dyn Future<Item = TcpStream, Error = io::Error> + Send>);

impl Future for ConnectionFuture {
    type Item = TcpStream;
    type Error = io::Error;

    fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
        self.0.poll()
    }
}

/// A `Future` that will resolve to an tcp connection.
#[must_use = "futures do nothing unless polled"]
pub struct SkipFuture(Box<dyn Future<Item = (TcpStream, usize), Error = io::Error> + Send>);

impl Future for SkipFuture {
    type Item = (TcpStream, usize);
    type Error = io::Error;

    fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
        self.0.poll()
    }
}

fn err<T: 'static + Send>(msg: &str) -> Box<dyn Future<Item = T, Error = io::Error> + Send> {
    Box::new(future::err(io::Error::new(io::ErrorKind::InvalidData, msg)))
}

fn socks5_request_connect(stream: TcpStream, buf: Vec<u8>, dest: &ProxyDest, port: u16) -> ConnectionFuture {
    info!("Reading socks5 server hello");

    // version
    if buf[0] != 0x05 {
        return ConnectionFuture(err("wrong version"));
    }

    // unauthenticated
    if buf[1] != 0x00 {
        return ConnectionFuture(err("auth failed"));
    }

    info!("Socks5 authentication successful");

    let mut buf = vec![
        0x05, // version
        0x01, // tcp connect
        0x00, // reserved
    ];

    dest.apply(&mut buf);
    buf.write_u16::<NetworkEndian>(port).unwrap();
    info!("Sending connect request");
    let fut = tokio::io::write_all(stream, buf)
        .and_then(|(stream, _)| future::ok(stream));
    ConnectionFuture(Box::new(fut))
}

pub fn connect(addr: &SocketAddr, dest: ProxyDest, port: u16) -> ConnectionFuture {
    let fut = TcpStream::connect(&addr)
        .and_then(|stream| {
            info!("Sending socks5 hello");
            tokio::io::write_all(stream, &[
                0x05, // version
                0x01, // number of supported auths
                0x00, // unauthenticated
            ])
        })
        .and_then(|(stream, _)| {
            let buf = vec![0; 2];
            tokio::io::read_exact(stream, buf)
        })
        .and_then(move |(stream, buf)| socks5_request_connect(stream, buf, &dest, port))
        .and_then(|stream| {
            let buf = vec![0; 4];
            tokio::io::read_exact(stream, buf)
        })
        .and_then(|(stream, buf)| {
            info!("Reading connect response");

            // version
            if buf[0] != 0x05 {
                return SkipFuture(err("wrong version"));
            }
            // status
            match buf[1] {
                0x00 => (),
                0x01 => return SkipFuture(err("general failure")),
                0x02 => return SkipFuture(err("connection not allowed by ruleset")),
                0x03 => return SkipFuture(err("network unreachable")),
                0x04 => return SkipFuture(err("host unreachable")),
                0x05 => return SkipFuture(err("connection refused by destination host")),
                0x06 => return SkipFuture(err("TTL expired")),
                0x07 => return SkipFuture(err("command not supported / protocol error")),
                0x08 => return SkipFuture(err("address type not supported")),
                _    => return SkipFuture(err("unknown connection error")),
            }
            // reserved
            if buf[2] != 0x00 {
                return SkipFuture(err("wrong reserved bytes"));
            }
            info!("Connection successful");

            match buf[3] {
                0x01 => SkipFuture(Box::new(future::ok((stream, 4)))), // ipv4
                0x03 => {
                    let buf = vec![0; 1];
                    let fut = tokio::io::read_exact(stream, buf)
                        .and_then(|(stream, buf)| {
                            Ok((stream, buf[0] as usize))
                        });
                    SkipFuture(Box::new(fut))
                },
                0x04 => SkipFuture(Box::new(future::ok((stream, 16)))), // ipv6
                _ => SkipFuture(err("wrong address type")),
            }
        })
        .and_then(|(stream, n)| {
            let buf = vec![0; n + 2];
            tokio::io::read_exact(stream, buf)
        })
        .and_then(|(stream, _)| {
            info!("Socks5 tunnel established");
            future::ok(stream)
        });
    ConnectionFuture(Box::new(fut))
}