Skip to content

Commit b783463

Browse files
committed
fix: refactor
1 parent be91af5 commit b783463

File tree

4 files changed

+41
-23
lines changed

4 files changed

+41
-23
lines changed

src/api/mod.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,13 @@ where
6060
None => break,
6161
};
6262

63-
TCP_TOTAL_CONNECTION_COUNTER.with_label_values(&[endpoint]).inc();
63+
TCP_TOTAL_CONNECTION_COUNTER
64+
.with_label_values(&[endpoint])
65+
.inc();
6466
let open_counter = TCP_OPEN_CONNECTION_COUNTER.with_label_values(&[endpoint]);
6567
open_counter.inc();
6668

67-
let http = http.clone();
69+
let http = Arc::clone(&http);
6870
let service = service.clone();
6971

7072
tokio::spawn(

src/api/proxy.rs

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use ppp::model::{Addresses, Header};
55
use std::future::Future;
66
use std::io::IoSlice;
77
use std::net::{SocketAddr, SocketAddrV4, SocketAddrV6};
8+
use std::ops::{Deref, DerefMut};
89
use std::pin::Pin;
910
use std::task::{Context, Poll};
1011
use tokio::io::{AsyncRead, AsyncWrite, Error as IoError, ErrorKind, ReadBuf, Result as IoResult};
@@ -21,14 +22,14 @@ pub(super) fn wrap(
2122
proxy: ProxyProtocol,
2223
) -> impl Stream<Item = IoResult<impl Future<Output = IoResult<ProxyStream>>>> + Send {
2324
TcpListenerStream::new(listener)
24-
.map_ok(move |stream| {
25+
.map_ok(move |conn| conn.source(proxy))
26+
.map_ok(|mut conn| {
2527
let span = Span::current();
26-
span.record("remote.addr", &debug(stream.peer_addr()));
28+
span.record("remote.addr", &debug(conn.peer_addr()));
2729
let span_clone = span.clone();
2830

29-
let mut conn = stream.source(proxy);
3031
async move {
31-
match conn.proxy_peer().await {
32+
match conn.real_addr().await {
3233
Ok(Some(addr)) => {
3334
span.record("remote.real", &display(addr));
3435
}
@@ -68,8 +69,8 @@ pub(super) struct ProxyStream {
6869
}
6970

7071
impl ProxyStream {
71-
fn proxy_peer(&mut self) -> PeerAddrFuture<'_> {
72-
PeerAddrFuture { proxy_stream: self }
72+
fn real_addr(&mut self) -> RealAddrFuture<'_> {
73+
RealAddrFuture { proxy_stream: self }
7374
}
7475
}
7576

@@ -117,11 +118,25 @@ impl AsyncWrite for ProxyStream {
117118
}
118119
}
119120

120-
struct PeerAddrFuture<'a> {
121+
impl Deref for ProxyStream {
122+
type Target = TcpStream;
123+
124+
fn deref(&self) -> &Self::Target {
125+
&self.stream
126+
}
127+
}
128+
129+
impl DerefMut for ProxyStream {
130+
fn deref_mut(&mut self) -> &mut Self::Target {
131+
&mut self.stream
132+
}
133+
}
134+
135+
struct RealAddrFuture<'a> {
121136
proxy_stream: &'a mut ProxyStream,
122137
}
123138

124-
impl<'a> PeerAddrFuture<'a> {
139+
impl<'a> RealAddrFuture<'a> {
125140
fn format_header(&self, res: Header) -> Poll<<Self as Future>::Output> {
126141
let addr = match res.addresses {
127142
Addresses::IPv4 {
@@ -175,7 +190,7 @@ impl<'a> PeerAddrFuture<'a> {
175190
}
176191
}
177192

178-
impl Future for PeerAddrFuture<'_> {
193+
impl Future for RealAddrFuture<'_> {
179194
type Output = IoResult<Option<SocketAddr>>;
180195

181196
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {

src/api/tls.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ impl Acceptor {
5454
})
5555
}
5656

57-
fn create_server_config(db_cert: &Cert) -> Result<Arc<ServerConfig>> {
57+
fn create_server_config(&self, db_cert: &Cert) -> Result<Arc<ServerConfig>> {
5858
let (private, cert) = match (&db_cert.private, &db_cert.cert) {
59-
(Some(ref private), Some(ref cert)) => (private, cert),
59+
(Some(private), Some(cert)) => (private, cert),
6060
_ => return Err(anyhow!("Cert has no Cert or Private")),
6161
};
6262

@@ -88,10 +88,10 @@ impl Acceptor {
8888
};
8989
info!(timestamp = to_u64(&db_cert.update), "Found new cert");
9090

91-
let server_config = match Acceptor::create_server_config(&db_cert) {
91+
let server_config = match self.create_server_config(&db_cert) {
9292
Ok(server_config) => server_config,
9393
Err(e) => {
94-
error!("{:?}", e);
94+
error!("{}", e);
9595
let (_, server_config) = &*self.config.read();
9696
return Ok(TlsAcceptor::from(Arc::clone(server_config)));
9797
}

src/util.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
use std::io;
1+
use anyhow::Error;
2+
use std::io::{Error as IoError, ErrorKind};
23
use std::time::{SystemTime, UNIX_EPOCH};
34
use uuid::Uuid;
45

@@ -17,11 +18,11 @@ pub(crate) fn now() -> u64 {
1718
.as_secs()
1819
}
1920

20-
pub(crate) fn error<E: From<io::Error>>(err: impl Into<anyhow::Error>) -> E {
21+
pub(crate) fn error<I: Into<Error>, E: From<IoError>>(err: I) -> E {
2122
let err = err.into();
22-
let err = match err.downcast::<io::Error>() {
23+
let err = match err.downcast::<IoError>() {
2324
Ok(err) => err,
24-
Err(err) => io::Error::new(io::ErrorKind::Other, err),
25+
Err(err) => IoError::new(ErrorKind::Other, err),
2526
};
2627

2728
E::from(err)
@@ -63,8 +64,8 @@ mod tests {
6364

6465
#[test]
6566
fn io_error_works() {
66-
let expected = io::Error::new(io::ErrorKind::InvalidData, anyhow!("Hallo"));
67-
let err = io::Error::new(io::ErrorKind::InvalidData, anyhow!("Hallo"));
67+
let expected = IoError::new(ErrorKind::InvalidData, "Hallo");
68+
let err = IoError::new(ErrorKind::InvalidData, "Hallo");
6869

6970
let actual = match error(err) {
7071
acme_lib::Error::Io(err) => err,
@@ -83,9 +84,9 @@ mod tests {
8384
_ => panic!("Cannot match err"),
8485
};
8586

86-
assert_eq!(io::ErrorKind::Other, actual.kind());
87+
assert_eq!(ErrorKind::Other, actual.kind());
8788

88-
// is not equal as original error gast boxed by io error
89+
// is not equal because original error gets boxed in an io error
8990
assert_ne!(format!("{:?}", expected), format!("{:?}", actual));
9091

9192
// here we access the actual inner error

0 commit comments

Comments
 (0)