Skip to content

Commit

Permalink
do not drop connection if socket option not supported: zhboner#51
Browse files Browse the repository at this point in the history
  • Loading branch information
zephyrchien committed Feb 2, 2022
1 parent 9b1959f commit ae84635
Showing 1 changed file with 25 additions and 19 deletions.
44 changes: 25 additions & 19 deletions src/relay/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use std::net::SocketAddr;
use std::time::Duration;
use futures::try_join;

use log::{debug, info};
use log::{warn, debug};

use tokio::net::TcpSocket;

Expand All @@ -49,6 +49,13 @@ impl Display for TcpDirection {
}
}

macro_rules! setsockopt_warn {
($op: expr, $opt: expr) => {{
let _ =
$op.map_err(|e| warn!("failed to set socket option $opt: {}", &e));
}};
}

#[allow(unused_variables)]
pub async fn proxy(
mut inbound: TcpStream,
Expand Down Expand Up @@ -76,9 +83,12 @@ pub async fn proxy(
SocketAddr::V4(_) => TcpSocket::new_v4()?,
SocketAddr::V6(_) => TcpSocket::new_v6()?,
};
socket.set_reuseaddr(true)?;

setsockopt_warn!(socket.set_reuseaddr(true), "reuseaddr");

#[cfg(unix)]
socket.set_reuseport(true)?;
setsockopt_warn!(socket.set_reuseport(true), "reuseport");

socket.bind(x)?;

#[cfg(feature = "tfo")]
Expand All @@ -94,10 +104,10 @@ pub async fn proxy(
None => TcpStream::connect(remote).await?,
};

info!("new tcp connection to remote {}", &remote);
debug!("new tcp connection to {}", &remote);

inbound.set_nodelay(true)?;
outbound.set_nodelay(true)?;
setsockopt_warn!(inbound.set_nodelay(true), "nodelay");
setsockopt_warn!(outbound.set_nodelay(true), "nodelay");

let (ri, wi) = inbound.split();
let (ro, wo) = outbound.split();
Expand Down Expand Up @@ -128,7 +138,13 @@ pub async fn proxy(
)
};

info!("tcp forward compelete or abort, close these 2 connection");
debug!(
"tcp forward compelete: {}",
match &res {
Ok(_) => String::from("ok"),
Err(e) => e.to_string(),
}
);

// ignore read/write n bytes
res.map(|_| ())
Expand Down Expand Up @@ -268,12 +284,7 @@ mod zero_copy {
clear_readiness(rx, Interest::READABLE);
break;
}
_ => {
break 'LOOP Err(Error::new(
ErrorKind::Other,
"failed to splice from tcp connection",
))
}
_ => break 'LOOP Err(Error::last_os_error()),
}
}
// write until the pipe is empty
Expand All @@ -284,12 +295,7 @@ mod zero_copy {
x if x < 0 && is_wouldblock() => {
clear_readiness(wx, Interest::WRITABLE);
}
_ => {
break 'LOOP Err(Error::new(
ErrorKind::Other,
"failed to splice to tcp connection",
))
}
_ => break 'LOOP Err(Error::last_os_error()),
}
}
// complete
Expand Down

0 comments on commit ae84635

Please sign in to comment.