Skip to content

Commit

Permalink
use tokio-io-timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
llc1123 committed Jun 11, 2021
1 parent b76f9c5 commit d8040ce
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 8 deletions.
12 changes: 11 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,5 @@ hex = "0.4.3"
redis = { version = "0.20.0", features = ["tokio-comp"] }
tokio-openssl = "0.6.1"
openssl = { version = "0.10.34", features = ["vendored"] }
pin-project-lite = "0.2.6"
# pin-project-lite = "0.2.6"
tokio-io-timeout = "1.1.1"
11 changes: 6 additions & 5 deletions src/relay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::common::UdpStream;
use crate::inbound::{Inbound, InboundAccept, InboundRequest};
use crate::outbound::Outbound;
use crate::utils::count_stream::CountStream;
use crate::utils::timeout_stream::TimeoutStream;
// use crate::utils::timeout_stream::TimeoutStream;
use anyhow::{anyhow, bail, Context, Error, Result};
use futures::{SinkExt, StreamExt, TryStreamExt};
use log::error;
Expand All @@ -12,6 +12,7 @@ use std::{sync::Arc, time::Duration};
use tokio::net::TcpListener;
use tokio::sync::mpsc::{unbounded_channel, UnboundedReceiver};
use tokio::{io::copy_bidirectional, pin, select, time::timeout};
use tokio_io_timeout::TimeoutStream;
use tokio_stream::wrappers::UnboundedReceiverStream;

const FULL_CONE_TIMEOUT: Duration = Duration::from_secs(30);
Expand All @@ -36,8 +37,7 @@ where
match inbound_accept.request {
InboundRequest::TcpConnect { addr, stream } => {
let target = outbound.tcp_connect(&addr).await?;
pin!(target);
pin!(stream);
pin!(target, stream);
copy_bidirectional(&mut stream, &mut target)
.await
.map_err(|_| anyhow!("Connection reset by peer."))?;
Expand Down Expand Up @@ -94,13 +94,14 @@ where
.context("Set TCP_NODELAY failed")?;

let (stream, sender) = CountStream::new2(stream);
let stream = TimeoutStream::new(stream, self.tcp_timeout);
let mut stream = TimeoutStream::new(stream);
stream.set_read_timeout(self.tcp_timeout);

let inbound = self.inbound.clone();
let outbound = self.outbound.clone();
let t = tx.clone();
tokio::spawn(async move {
let inbound_accept = inbound.accept(stream, addr).await?;
let inbound_accept = inbound.accept(Box::pin(stream), addr).await?;
if let Some(accept) = inbound_accept {
// Here we got trojan password

Expand Down
2 changes: 1 addition & 1 deletion src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ pub mod config;
pub mod count_stream;
pub mod logger;
pub mod peekable_stream;
pub mod timeout_stream;
// pub mod timeout_stream;

0 comments on commit d8040ce

Please sign in to comment.