Skip to content

Commit

Permalink
Refactor: replace Box with future::Either
Browse files Browse the repository at this point in the history
  • Loading branch information
sorz committed Apr 18, 2018
1 parent 70d1e1e commit 96d9802
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 22 deletions.
7 changes: 3 additions & 4 deletions src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ impl Connectable for NewClientWithData {

impl ConnectedClient {
pub fn serve(self, shared_buf: SharedBuf)
-> Box<Future<Item=(), Error=()>> {
-> impl Future<Item=(), Error=()> {
let ConnectedClient { left, right, dest, server } = self;
// TODO: make keepalive configurable
let timeout = Some(Duration::from_secs(300));
Expand All @@ -149,7 +149,7 @@ impl ConnectedClient {
}

server.update_stats_conn_open();
let serve = pipe(left, right, server.clone(), shared_buf)
pipe(left, right, server.clone(), shared_buf)
.then(move |result| match result {
Ok(amt) => {
server.update_stats_conn_close();
Expand All @@ -163,8 +163,7 @@ impl ConnectedClient {
server.tag, dest);
Err(())
}
});
Box::new(serve)
})
}
}

6 changes: 3 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use std::io::Write;
use std::path::Path;
use std::net::SocketAddr;
use ini::Ini;
use futures::{Future, Stream};
use futures::{Future, Stream, future::Either};
use tokio_core::net::TcpListener;
use tokio_core::reactor::Core;
use tokio_uds::UnixListener;
Expand Down Expand Up @@ -120,11 +120,11 @@ fn main() {
sock, monitor.servers(), handle.clone());
let conn = client.and_then(move |client|
if remote_dns && client.dest.port == 443 {
Box::new(client.retrive_dest().and_then(move |client| {
Either::A(client.retrive_dest().and_then(move |client| {
client.connect_server(n_parallel)
}))
} else {
client.connect_server(0)
Either::B(client.connect_server(0))
});
let buf = shared_buf.clone();
let serv = conn.and_then(|client| client.serve(buf));
Expand Down
8 changes: 4 additions & 4 deletions src/proxy/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ use std::io::{self, Read, BufReader, ErrorKind};
use futures::{future, Future};
use tokio_core::net::TcpStream;
use tokio_io::io::{write_all, read_until};
use proxy::{Connect, Destination, Address};
use proxy::{Destination, Address};


pub fn handshake<T>(stream: TcpStream, addr: &Destination,
mut data: Option<T>, with_playload: bool)
-> Box<Connect>
-> impl Future<Item=TcpStream, Error=io::Error>
where T: AsRef<[u8]> + 'static {
let mut request = build_request(addr).into_bytes();
if with_playload && data.is_some() {
Expand Down Expand Up @@ -58,11 +58,11 @@ where T: AsRef<[u8]> + 'static {
// FIXME: may lost data in buffer?
}).map(|reader| reader.into_inner());
if let Some(data) = data {
Box::new(skip.and_then(|stream| {
future::Either::A(skip.and_then(|stream| {
write_all(stream, data).map(|(stream, _)| stream)
}))
} else {
Box::new(skip)
future::Either::B(skip)
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/proxy/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::str::FromStr;
use std::time::Duration;
use std::net::{SocketAddr, IpAddr};
use std::hash::{Hash, Hasher};
use futures::Future;
use futures::{Future, future::Either};
use tokio_core::net::TcpStream;
use tokio_core::reactor::Handle;
use ToMillis;
Expand Down Expand Up @@ -162,10 +162,10 @@ impl ProxyServer {
};
match proto {
ProxyProto::Socks5 =>
socks5::handshake(stream, &addr, data),
Either::A(socks5::handshake(stream, &addr, data)),
ProxyProto::Http { connect_with_payload } =>
http::handshake(stream, &addr, data,
connect_with_payload),
Either::B(http::handshake(stream, &addr, data,
connect_with_payload)),
}
});
Box::new(handshake)
Expand Down
13 changes: 6 additions & 7 deletions src/proxy/socks5.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
use std::net::IpAddr;
use std::io::Write;
use std::io::{self, Write};
use futures::Future;
use tokio_core::net::TcpStream;
use tokio_io::io::{read_exact, write_all};
use proxy::{Connect, Destination, Address};
use proxy::{Destination, Address};


pub fn handshake<T>(stream: TcpStream, addr: &Destination,
data: Option<T>) -> Box<Connect>
pub fn handshake<T>(stream: TcpStream, addr: &Destination, data: Option<T>)
-> impl Future<Item=TcpStream, Error=io::Error>
where T: AsRef<[u8]> {
let mut request = build_request(addr);
if let Some(data) = data {
// TODO: remove copying
request.extend(data.as_ref());
}
let handshake = write_all(stream, request).and_then(|(stream, _)| {
write_all(stream, request).and_then(|(stream, _)| {
read_exact(stream, vec![0; 12])
}).map(|(stream, _)| stream);
return Box::new(handshake)
}).map(|(stream, _)| stream)
}

fn build_request(addr: &Destination) -> Vec<u8> {
Expand Down

0 comments on commit 96d9802

Please sign in to comment.