Skip to content

Commit e287127

Browse files
authored
fix: close rustls stream on drop (#96)
1 parent 628e8bb commit e287127

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

suppaftp/src/sync_ftp/tls/rustls.rs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,10 @@ impl TlsConnector for RustlsConnector {
3838
let connection = ClientConnection::new(Arc::clone(&self.connector), server_name)
3939
.map_err(|e| FtpError::SecureError(e.to_string()))?;
4040
let stream = StreamOwned::new(connection, stream);
41-
Ok(RustlsStream { stream })
41+
Ok(RustlsStream {
42+
stream,
43+
ssl_shutdown: true,
44+
})
4245
}
4346
}
4447

@@ -49,14 +52,17 @@ impl TlsConnector for RustlsConnector {
4952
#[derive(Debug)]
5053
pub struct RustlsStream {
5154
stream: StreamOwned<ClientConnection, TcpStream>,
55+
ssl_shutdown: bool,
5256
}
5357

5458
impl TlsStream for RustlsStream {
5559
type InnerStream = StreamOwned<ClientConnection, TcpStream>;
5660

5761
/// Get underlying tcp stream
58-
fn tcp_stream(self) -> TcpStream {
62+
fn tcp_stream(mut self) -> TcpStream {
5963
let mut stream = self.get_ref().try_clone().unwrap();
64+
// Don't perform shutdown later
65+
self.ssl_shutdown = false;
6066
// flush stream (otherwise can cause bad chars on channel)
6167
if let Err(err) = stream.flush() {
6268
error!("Error in flushing tcp stream: {}", err);
@@ -75,3 +81,17 @@ impl TlsStream for RustlsStream {
7581
&mut self.stream
7682
}
7783
}
84+
85+
impl Drop for RustlsStream {
86+
fn drop(&mut self) {
87+
if self.ssl_shutdown {
88+
if let Err(err) = self.stream.flush() {
89+
error!("error in flushing rustls stream on drop: {err}");
90+
}
91+
self.stream.conn.send_close_notify();
92+
if let Err(err) = self.stream.conn.write_tls(&mut self.stream.sock) {
93+
error!("error in terminating rustls stream: {err}");
94+
}
95+
}
96+
}
97+
}

0 commit comments

Comments
 (0)