Skip to content

Commit

Permalink
fix ProxyClientStreamWriteHalf that allows sending empty buffers
Browse files Browse the repository at this point in the history
ref #232
  • Loading branch information
zonyitoo committed Dec 19, 2020
1 parent aa3822d commit 3a658e6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
6 changes: 6 additions & 0 deletions crates/shadowsocks-service/src/local/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ where
Err(..) => {
// Timeout. Send handshake to server.
shadow_writer.write(&[]).await?;

trace!(
"tcp tunnel {} -> {} sent handshake without data",
peer_addr,
target_addr
);
}
}
}
Expand Down
13 changes: 9 additions & 4 deletions crates/shadowsocks/src/relay/tcprelay/proxy_stream/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,10 +232,6 @@ where
S: AsyncWrite + Unpin,
{
fn poll_write(mut self: Pin<&mut Self>, cx: &mut task::Context<'_>, buf: &[u8]) -> Poll<Result<usize, io::Error>> {
if buf.is_empty() {
return Ok(0).into();
}

if self.addr.is_none() {
// For all subsequence calls, just proxy it to self.writer
return self.writer.poll_write_encrypted(cx, buf);
Expand All @@ -250,6 +246,15 @@ where

ready!(self.writer.poll_write_encrypted(cx, &buffer))?;

// NOTE:
// poll_write will return Ok(0) if buf.len() == 0
// But for the first call, this function will eventually send the handshake packet (IV/Salt + ADDR) to the remote address.
//
// https://github.com/shadowsocks/shadowsocks-rust/issues/232
//
// For protocols that requires *Server Hello* message, like FTP, clients won't send anything to the server until server sends handshake messages.
// This could be achieved by calling poll_write with an empty input buffer.

Ok(buf.len()).into()
}

Expand Down

0 comments on commit 3a658e6

Please sign in to comment.