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
// The MIT License (MIT)

// Copyright (c) 2014 Y. T. CHUNG <zonyitoo@gmail.com>

// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:

// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

//! TCP relay client implementation

use std::io::{self, Read, Write};
use std::net::SocketAddr;

use tokio_core::reactor::Handle;
use tokio_core::net::TcpStream;
use tokio_core::io::flush;

use futures::{self, Future};

use relay::socks5::{self, HandshakeRequest, HandshakeResponse, Address, TcpRequestHeader, TcpResponseHeader, Command,
                    Reply};
use relay::{BoxIoFuture, boxed_future};

/// Socks5 proxy client
pub struct Socks5Client {
    stream: TcpStream,
}

impl Socks5Client {
    /// Connects to `addr` via `proxy`
    pub fn connect<A>(addr: A, proxy: SocketAddr, handle: Handle) -> BoxIoFuture<Socks5Client>
        where Address: From<A>,
              A: 'static
    {
        let fut = futures::lazy(move || TcpStream::connect(&proxy, &handle))
            .and_then(move |s| {
                // 1. Handshake
                let hs = HandshakeRequest::new(vec![socks5::SOCKS5_AUTH_METHOD_NONE]);
                trace!("Client connected, going to send handshake: {:?}", hs);

                hs.write_to(s)
                    .and_then(flush)
                    .and_then(|s| HandshakeResponse::read_from(s))
                    .and_then(|(s, rsp)| {
                        trace!("Got handshake response: {:?}", rsp);
                        assert_eq!(rsp.chosen_method, socks5::SOCKS5_AUTH_METHOD_NONE);
                        Ok(s)
                    })
            })
            .and_then(move |s| {
                // 2. Send request header
                let h = TcpRequestHeader::new(Command::TcpConnect, From::from(addr));
                trace!("Going to connect, req: {:?}", h);
                h.write_to(s)
                    .and_then(flush)
                    .and_then(|s| TcpResponseHeader::read_from(s).map_err(From::from))
                    .and_then(|(s, rsp)| {
                        trace!("Got response: {:?}", rsp);
                        match rsp.reply {
                            Reply::Succeeded => Ok(s),
                            r => {
                                let err = io::Error::new(io::ErrorKind::Other, format!("{}", r));
                                Err(err)
                            }
                        }
                    })
            })
            .map(|s| Socks5Client { stream: s });

        boxed_future(fut)
    }

    /// UDP Associate `addr` via `proxy`
    pub fn udp_associate<A>(addr: A, proxy: SocketAddr, handle: Handle) -> BoxIoFuture<(Socks5Client, Address)>
        where Address: From<A>,
              A: 'static
    {
        let fut = futures::lazy(move || TcpStream::connect(&proxy, &handle))
            .and_then(move |s| {
                // 1. Handshake
                let hs = HandshakeRequest::new(vec![socks5::SOCKS5_AUTH_METHOD_NONE]);
                trace!("Client connected, going to send handshake: {:?}", hs);

                hs.write_to(s)
                    .and_then(flush)
                    .and_then(|s| HandshakeResponse::read_from(s))
                    .and_then(|(s, rsp)| {
                        trace!("Got handshake response: {:?}", rsp);
                        assert_eq!(rsp.chosen_method, socks5::SOCKS5_AUTH_METHOD_NONE);
                        Ok(s)
                    })
            })
            .and_then(move |s| {
                // 2. Send request header
                let h = TcpRequestHeader::new(Command::UdpAssociate, From::from(addr));
                trace!("Going to connect, req: {:?}", h);
                h.write_to(s)
                    .and_then(flush)
                    .and_then(|s| TcpResponseHeader::read_from(s).map_err(From::from))
                    .and_then(|(s, rsp)| {
                        trace!("Got response: {:?}", rsp);
                        match rsp.reply {
                            Reply::Succeeded => Ok((s, rsp.address)),
                            r => {
                                let err = io::Error::new(io::ErrorKind::Other, format!("{}", r));
                                Err(err)
                            }
                        }
                    })
            })
            .map(|(s, a)| (Socks5Client { stream: s }, a));

        boxed_future(fut)
    }
}

impl Read for Socks5Client {
    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
        self.stream.read(buf)
    }
}

impl Write for Socks5Client {
    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
        self.stream.write(buf)
    }

    fn flush(&mut self) -> io::Result<()> {
        self.stream.flush()
    }
}