|
| 1 | +use anyhow::Result; |
1 | 2 | use fast_socks5::client::Socks5Stream;
|
2 | 3 | use std::pin::Pin;
|
| 4 | +use std::net::SocketAddr; |
3 | 5 | use std::time::Duration;
|
4 | 6 | use tokio::io::{AsyncBufRead, AsyncRead, AsyncWrite, BufStream, BufWriter};
|
5 | 7 | use tokio_io_timeout::TimeoutStream;
|
| 8 | +use tokio::net::TcpStream; |
6 | 9 |
|
7 | 10 | pub(crate) trait SessionStream:
|
8 | 11 | AsyncRead + AsyncWrite + Unpin + Send + Sync + std::fmt::Debug
|
9 | 12 | {
|
10 | 13 | /// Change the read timeout on the session stream.
|
11 | 14 | 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>>; |
12 | 20 | }
|
13 | 21 |
|
14 | 22 | impl SessionStream for Box<dyn SessionStream> {
|
15 | 23 | fn set_read_timeout(&mut self, timeout: Option<Duration>) {
|
16 | 24 | self.as_mut().set_read_timeout(timeout);
|
17 | 25 | }
|
| 26 | + |
| 27 | + fn peer_addr(&self) -> Result<Option<SocketAddr>> { |
| 28 | + let addr = self.as_ref().peer_addr()?; |
| 29 | + Ok(addr) |
| 30 | + } |
18 | 31 | }
|
19 | 32 | impl<T: SessionStream> SessionStream for async_native_tls::TlsStream<T> {
|
20 | 33 | fn set_read_timeout(&mut self, timeout: Option<Duration>) {
|
21 | 34 | self.get_mut().set_read_timeout(timeout);
|
22 | 35 | }
|
| 36 | + |
| 37 | + fn peer_addr(&self) -> Result<Option<SocketAddr>> { |
| 38 | + let addr = self.get_ref().peer_addr()?; |
| 39 | + Ok(addr) |
| 40 | + } |
23 | 41 | }
|
24 | 42 | impl<T: SessionStream> SessionStream for tokio_rustls::client::TlsStream<T> {
|
25 | 43 | fn set_read_timeout(&mut self, timeout: Option<Duration>) {
|
26 | 44 | self.get_mut().0.set_read_timeout(timeout);
|
27 | 45 | }
|
| 46 | + |
| 47 | + fn peer_addr(&self) -> Result<Option<SocketAddr>> { |
| 48 | + let addr = self.get_ref().0.peer_addr()?; |
| 49 | + Ok(addr) |
| 50 | + } |
28 | 51 | }
|
29 | 52 | impl<T: SessionStream> SessionStream for BufStream<T> {
|
30 | 53 | fn set_read_timeout(&mut self, timeout: Option<Duration>) {
|
31 | 54 | self.get_mut().set_read_timeout(timeout);
|
32 | 55 | }
|
| 56 | + |
| 57 | + fn peer_addr(&self) -> Result<Option<SocketAddr>> { |
| 58 | + let addr = self.get_ref().peer_addr()?; |
| 59 | + Ok(addr) |
| 60 | + } |
33 | 61 | }
|
34 | 62 | impl<T: SessionStream> SessionStream for BufWriter<T> {
|
35 | 63 | fn set_read_timeout(&mut self, timeout: Option<Duration>) {
|
36 | 64 | self.get_mut().set_read_timeout(timeout);
|
37 | 65 | }
|
| 66 | + |
| 67 | + fn peer_addr(&self) -> Result<Option<SocketAddr>> { |
| 68 | + let addr = self.get_ref().peer_addr()?; |
| 69 | + Ok(addr) |
| 70 | + } |
38 | 71 | }
|
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>>> |
41 | 73 | {
|
42 | 74 | fn set_read_timeout(&mut self, timeout: Option<Duration>) {
|
43 | 75 | self.as_mut().set_read_timeout_pinned(timeout);
|
44 | 76 | }
|
| 77 | + |
| 78 | + fn peer_addr(&self) -> Result<Option<SocketAddr>> { |
| 79 | + let addr = self.get_ref().peer_addr()?; |
| 80 | + Ok(Some(addr)) |
| 81 | + } |
45 | 82 | }
|
46 | 83 | impl<T: SessionStream> SessionStream for Socks5Stream<T> {
|
47 | 84 | fn set_read_timeout(&mut self, timeout: Option<Duration>) {
|
48 | 85 | self.get_socket_mut().set_read_timeout(timeout)
|
49 | 86 | }
|
| 87 | + |
| 88 | + fn peer_addr(&self) -> Result<Option<SocketAddr>> { |
| 89 | + let addr = self.get_socket_ref().peer_addr()?; |
| 90 | + Ok(addr) |
| 91 | + } |
50 | 92 | }
|
51 | 93 | impl<T: SessionStream> SessionStream for shadowsocks::ProxyClientStream<T> {
|
52 | 94 | fn set_read_timeout(&mut self, timeout: Option<Duration>) {
|
53 | 95 | self.get_mut().set_read_timeout(timeout)
|
54 | 96 | }
|
| 97 | + |
| 98 | + fn peer_addr(&self) -> Result<Option<SocketAddr>> { |
| 99 | + let addr = self.get_ref().peer_addr()?; |
| 100 | + Ok(addr) |
| 101 | + } |
55 | 102 | }
|
56 | 103 | impl<T: SessionStream> SessionStream for async_imap::DeflateStream<T> {
|
57 | 104 | fn set_read_timeout(&mut self, timeout: Option<Duration>) {
|
58 | 105 | self.get_mut().set_read_timeout(timeout)
|
59 | 106 | }
|
| 107 | + |
| 108 | + fn peer_addr(&self) -> Result<Option<SocketAddr>> { |
| 109 | + let addr = self.get_ref().peer_addr()?; |
| 110 | + Ok(addr) |
| 111 | + } |
60 | 112 | }
|
61 | 113 |
|
62 | 114 | /// Session stream with a read buffer.
|
|
0 commit comments