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
//! SOCKS proxy support for Hyper clients
#![doc(html_root_url="https://docs.rs/hyper-socks/0.4.0")]
#![warn(missing_docs)]

extern crate socks;
extern crate hyper;

#[cfg(test)]
extern crate hyper_openssl;

use hyper::net::{NetworkConnector, HttpStream, HttpsStream, SslClient};
use socks::{Socks4Stream, Socks5Stream};
use std::io;
use std::net::{SocketAddr, ToSocketAddrs};

/// A connector that will produce HttpStreams proxied over a SOCKS4 server.
#[derive(Debug)]
pub struct Socks4HttpConnector {
    addrs: Vec<SocketAddr>,
    userid: String,
}

impl Socks4HttpConnector {
    /// Creates a new `Socks4HttpConnector` which will connect to the specified
    /// proxy with the specified userid.
    pub fn new<T: ToSocketAddrs>(proxy: T, userid: &str) -> io::Result<Socks4HttpConnector> {
        Ok(Socks4HttpConnector {
            addrs: try!(proxy.to_socket_addrs()).collect(),
            userid: userid.to_owned(),
        })
    }
}

impl NetworkConnector for Socks4HttpConnector {
    type Stream = HttpStream;

    fn connect(&self, host: &str, port: u16, scheme: &str) -> hyper::Result<HttpStream> {
        if scheme != "http" {
            return Err(io::Error::new(io::ErrorKind::InvalidInput, "invalid scheme for HTTP")
                           .into());
        }

        let socket = try!(Socks4Stream::connect(&self.addrs[..], (host, port), &self.userid));
        Ok(HttpStream(socket.into_inner()))
    }
}

/// A connector that will produce HttpsStreams proxied over a SOCKS4 server.
#[derive(Debug)]
pub struct Socks4HttpsConnector<S> {
    addrs: Vec<SocketAddr>,
    userid: String,
    ssl: S,
}

impl<S: SslClient> Socks4HttpsConnector<S> {
    /// Creates a new `Socks4HttpsConnector` which will connect to the specified
    /// proxy with the specified userid, and use the provided SSL implementation
    /// to encrypt the resulting stream.
    pub fn new<T: ToSocketAddrs>(proxy: T, userid: &str, ssl: S) -> io::Result<Self> {
        Ok(Socks4HttpsConnector {
            addrs: try!(proxy.to_socket_addrs()).collect(),
            userid: userid.to_owned(),
            ssl: ssl,
        })
    }
}

impl<S: SslClient> NetworkConnector for Socks4HttpsConnector<S> {
    type Stream = HttpsStream<S::Stream>;

    fn connect(&self, host: &str, port: u16, scheme: &str) -> hyper::Result<Self::Stream> {
        if scheme != "http" && scheme != "https" {
            return Err(io::Error::new(io::ErrorKind::InvalidInput, "invalid scheme for HTTPS")
                           .into());
        }

        let socket = try!(Socks4Stream::connect(&self.addrs[..], (host, port), &self.userid));
        let stream = HttpStream(socket.into_inner());

        if scheme == "http" {
            Ok(HttpsStream::Http(stream))
        } else {
            Ok(HttpsStream::Https(try!(self.ssl.wrap_client(stream, host))))
        }
    }
}

/// A connector that will produce HttpStreams proxied over a SOCKS5 server.
#[derive(Debug)]
pub struct Socks5HttpConnector {
    addrs: Vec<SocketAddr>,
}

impl Socks5HttpConnector {
    /// Creates a new `Socks5HttpConnector` which will connect to the specified
    /// proxy.
    pub fn new<T: ToSocketAddrs>(proxy: T) -> io::Result<Socks5HttpConnector> {
        Ok(Socks5HttpConnector {
            addrs: try!(proxy.to_socket_addrs()).collect(),
        })
    }
}

impl NetworkConnector for Socks5HttpConnector {
    type Stream = HttpStream;

    fn connect(&self, host: &str, port: u16, scheme: &str) -> hyper::Result<HttpStream> {
        if scheme != "http" {
            return Err(io::Error::new(io::ErrorKind::InvalidInput, "invalid scheme for HTTP")
                           .into());
        }

        let socket = try!(Socks5Stream::connect(&self.addrs[..], (host, port)));
        Ok(HttpStream(socket.into_inner()))
    }
}

/// A connector that will produce HttpsStreams proxied over a SOCKS5 server.
#[derive(Debug)]
pub struct Socks5HttpsConnector<S> {
    addrs: Vec<SocketAddr>,
    ssl: S,
}

impl<S: SslClient> Socks5HttpsConnector<S> {
    /// Creates a new `Socks5HttpsConnector` which will connect to the specified proxy, and use the
    /// provided SSL implementation to encrypt the resulting stream.
    pub fn new<T: ToSocketAddrs>(proxy: T, ssl: S) -> io::Result<Self> {
        Ok(Socks5HttpsConnector {
            addrs: try!(proxy.to_socket_addrs()).collect(),
            ssl: ssl,
        })
    }
}

impl<S: SslClient> NetworkConnector for Socks5HttpsConnector<S> {
    type Stream = HttpsStream<S::Stream>;

    fn connect(&self, host: &str, port: u16, scheme: &str) -> hyper::Result<Self::Stream> {
        if scheme != "http" && scheme != "https" {
            return Err(io::Error::new(io::ErrorKind::InvalidInput, "invalid scheme for HTTPS")
                           .into());
        }

        let socket = try!(Socks5Stream::connect(&self.addrs[..], (host, port)));
        let stream = HttpStream(socket.into_inner());

        if scheme == "http" {
            Ok(HttpsStream::Http(stream))
        } else {
            Ok(HttpsStream::Https(try!(self.ssl.wrap_client(stream, host))))
        }
    }
}

#[cfg(test)]
mod test {
    use hyper;
    use hyper_openssl::OpensslClient;
    use std::io::Read;

    use super::*;

    #[test]
    fn google() {
        let connector = Socks4HttpConnector::new("127.0.0.1:8080", "").unwrap();
        let client = hyper::Client::with_connector(connector);
        let mut response = client.get("http://www.google.com").send().unwrap();

        assert!(response.status.is_success());
        let mut body = vec![];
        response.read_to_end(&mut body).unwrap();
    }

    #[test]
    fn google_ssl_http() {
        let ssl = OpensslClient::new().unwrap();
        let connector = Socks4HttpsConnector::new("127.0.0.1:8080", "", ssl).unwrap();
        let client = hyper::Client::with_connector(connector);
        let mut response = client.get("http://www.google.com").send().unwrap();

        assert!(response.status.is_success());
        let mut body = vec![];
        response.read_to_end(&mut body).unwrap();
    }

    #[test]
    fn google_ssl_https() {
        let ssl = OpensslClient::new().unwrap();
        let connector = Socks4HttpsConnector::new("127.0.0.1:8080", "", ssl).unwrap();
        let client = hyper::Client::with_connector(connector);
        let mut response = client.get("https://www.google.com").send().unwrap();

        assert!(response.status.is_success());
        let mut body = vec![];
        response.read_to_end(&mut body).unwrap();
    }

    #[test]
    fn google_v5() {
        let connector = Socks5HttpConnector::new("127.0.0.1:8080").unwrap();
        let client = hyper::Client::with_connector(connector);
        let mut response = client.get("http://www.google.com").send().unwrap();

        assert!(response.status.is_success());
        let mut body = vec![];
        response.read_to_end(&mut body).unwrap();
    }

    #[test]
    fn google_ssl_http_v5() {
        let ssl = OpensslClient::new().unwrap();
        let connector = Socks5HttpsConnector::new("127.0.0.1:8080", ssl).unwrap();
        let client = hyper::Client::with_connector(connector);
        let mut response = client.get("http://www.google.com").send().unwrap();

        assert!(response.status.is_success());
        let mut body = vec![];
        response.read_to_end(&mut body).unwrap();
    }

    #[test]
    fn google_ssl_https_v5() {
        let ssl = OpensslClient::new().unwrap();
        let connector = Socks5HttpsConnector::new("127.0.0.1:8080", ssl).unwrap();
        let client = hyper::Client::with_connector(connector);
        let mut response = client.get("https://www.google.com").send().unwrap();

        assert!(response.status.is_success());
        let mut body = vec![];
        response.read_to_end(&mut body).unwrap();
    }
}