Skip to content

Commit be107b0

Browse files
committed
feat: allow to get peer address for any session stream
1 parent f5e8c80 commit be107b0

File tree

1 file changed

+54
-2
lines changed

1 file changed

+54
-2
lines changed

src/net/session.rs

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,114 @@
1+
use anyhow::Result;
12
use fast_socks5::client::Socks5Stream;
23
use std::pin::Pin;
4+
use std::net::SocketAddr;
35
use std::time::Duration;
46
use tokio::io::{AsyncBufRead, AsyncRead, AsyncWrite, BufStream, BufWriter};
57
use tokio_io_timeout::TimeoutStream;
8+
use tokio::net::TcpStream;
69

710
pub(crate) trait SessionStream:
811
AsyncRead + AsyncWrite + Unpin + Send + Sync + std::fmt::Debug
912
{
1013
/// Change the read timeout on the session stream.
1114
fn set_read_timeout(&mut self, timeout: Option<Duration>);
15+
16+
/// Returns the remote address that this stream is connected to.
17+
///
18+
/// If the connection is proxied, returns `None`.
19+
fn peer_addr(&self) -> Result<Option<SocketAddr>>;
1220
}
1321

1422
impl SessionStream for Box<dyn SessionStream> {
1523
fn set_read_timeout(&mut self, timeout: Option<Duration>) {
1624
self.as_mut().set_read_timeout(timeout);
1725
}
26+
27+
fn peer_addr(&self) -> Result<Option<SocketAddr>> {
28+
let addr = self.as_ref().peer_addr()?;
29+
Ok(addr)
30+
}
1831
}
1932
impl<T: SessionStream> SessionStream for async_native_tls::TlsStream<T> {
2033
fn set_read_timeout(&mut self, timeout: Option<Duration>) {
2134
self.get_mut().set_read_timeout(timeout);
2235
}
36+
37+
fn peer_addr(&self) -> Result<Option<SocketAddr>> {
38+
let addr = self.get_ref().peer_addr()?;
39+
Ok(addr)
40+
}
2341
}
2442
impl<T: SessionStream> SessionStream for tokio_rustls::client::TlsStream<T> {
2543
fn set_read_timeout(&mut self, timeout: Option<Duration>) {
2644
self.get_mut().0.set_read_timeout(timeout);
2745
}
46+
47+
fn peer_addr(&self) -> Result<Option<SocketAddr>> {
48+
let addr = self.get_ref().0.peer_addr()?;
49+
Ok(addr)
50+
}
2851
}
2952
impl<T: SessionStream> SessionStream for BufStream<T> {
3053
fn set_read_timeout(&mut self, timeout: Option<Duration>) {
3154
self.get_mut().set_read_timeout(timeout);
3255
}
56+
57+
fn peer_addr(&self) -> Result<Option<SocketAddr>> {
58+
let addr = self.get_ref().peer_addr()?;
59+
Ok(addr)
60+
}
3361
}
3462
impl<T: SessionStream> SessionStream for BufWriter<T> {
3563
fn set_read_timeout(&mut self, timeout: Option<Duration>) {
3664
self.get_mut().set_read_timeout(timeout);
3765
}
66+
67+
fn peer_addr(&self) -> Result<Option<SocketAddr>> {
68+
let addr = self.get_ref().peer_addr()?;
69+
Ok(addr)
70+
}
3871
}
39-
impl<T: AsyncRead + AsyncWrite + Send + Sync + std::fmt::Debug> SessionStream
40-
for Pin<Box<TimeoutStream<T>>>
72+
impl SessionStream for Pin<Box<TimeoutStream<TcpStream>>>
4173
{
4274
fn set_read_timeout(&mut self, timeout: Option<Duration>) {
4375
self.as_mut().set_read_timeout_pinned(timeout);
4476
}
77+
78+
fn peer_addr(&self) -> Result<Option<SocketAddr>> {
79+
let addr = self.get_ref().peer_addr()?;
80+
Ok(Some(addr))
81+
}
4582
}
4683
impl<T: SessionStream> SessionStream for Socks5Stream<T> {
4784
fn set_read_timeout(&mut self, timeout: Option<Duration>) {
4885
self.get_socket_mut().set_read_timeout(timeout)
4986
}
87+
88+
fn peer_addr(&self) -> Result<Option<SocketAddr>> {
89+
let addr = self.get_socket_ref().peer_addr()?;
90+
Ok(addr)
91+
}
5092
}
5193
impl<T: SessionStream> SessionStream for shadowsocks::ProxyClientStream<T> {
5294
fn set_read_timeout(&mut self, timeout: Option<Duration>) {
5395
self.get_mut().set_read_timeout(timeout)
5496
}
97+
98+
fn peer_addr(&self) -> Result<Option<SocketAddr>> {
99+
let addr = self.get_ref().peer_addr()?;
100+
Ok(addr)
101+
}
55102
}
56103
impl<T: SessionStream> SessionStream for async_imap::DeflateStream<T> {
57104
fn set_read_timeout(&mut self, timeout: Option<Duration>) {
58105
self.get_mut().set_read_timeout(timeout)
59106
}
107+
108+
fn peer_addr(&self) -> Result<Option<SocketAddr>> {
109+
let addr = self.get_ref().peer_addr()?;
110+
Ok(addr)
111+
}
60112
}
61113

62114
/// Session stream with a read buffer.

0 commit comments

Comments
 (0)