|
1 | | -use tokio::io; |
| 1 | +use tokio::io::copy_bidirectional; |
2 | 2 | use tokio::net::TcpStream; |
3 | 3 |
|
4 | | -pub async fn relay(client: TcpStream, upstream: TcpStream) -> Result<(), std::io::Error> { |
5 | | - let (mut cr, mut cw) = io::split(client); |
6 | | - let (mut ur, mut uw) = io::split(upstream); |
7 | | - |
8 | | - let c2u = tokio::spawn(async move { io::copy(&mut cr, &mut uw).await }); |
9 | | - let u2c = tokio::spawn(async move { io::copy(&mut ur, &mut cw).await }); |
10 | | - |
11 | | - tokio::select! { |
12 | | - r = c2u => { |
13 | | - match r { |
14 | | - Ok(Ok(n)) => tracing::debug!(bytes = n, "client->upstream finished"), |
15 | | - Ok(Err(e)) => tracing::debug!("client->upstream error: {}", e), |
16 | | - Err(e) => tracing::debug!("client->upstream task panicked: {}", e), |
17 | | - } |
18 | | - } |
19 | | - r = u2c => { |
20 | | - match r { |
21 | | - Ok(Ok(n)) => tracing::debug!(bytes = n, "upstream->client finished"), |
22 | | - Ok(Err(e)) => tracing::debug!("upstream->client error: {}", e), |
23 | | - Err(e) => tracing::debug!("upstream->client task panicked: {}", e), |
24 | | - } |
25 | | - } |
26 | | - } |
27 | | - |
| 4 | +pub async fn relay(mut client: TcpStream, mut upstream: TcpStream) -> Result<(), std::io::Error> { |
| 5 | + let (c2u, u2c) = copy_bidirectional(&mut client, &mut upstream).await?; |
| 6 | + tracing::debug!(c2u, u2c, "relay finished"); |
28 | 7 | Ok(()) |
29 | 8 | } |
0 commit comments