1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
//! An example TCP proxy.
use monoio::{
io::{AsyncReadRent, AsyncWriteRent, AsyncWriteRentExt, Splitable},
net::{TcpListener, TcpStream},
};
const LISTEN_ADDRESS: &str = "127.0.0.1:50005";
const TARGET_ADDRESS: &str = "127.0.0.1:50006";
#[monoio::main(entries = 512, timer_enabled = false)]
async fn main() {
let listener = TcpListener::bind(LISTEN_ADDRESS)
.unwrap_or_else(|_| panic!("[Server] Unable to bind to {LISTEN_ADDRESS}"));
loop {
if let Ok((in_conn, _addr)) = listener.accept().await {
let out_conn = TcpStream::connect(TARGET_ADDRESS).await;
if let Ok(out_conn) = out_conn {
monoio::spawn(async move {
let (mut in_r, mut in_w) = in_conn.into_split();
let (mut out_r, mut out_w) = out_conn.into_split();
let _ = monoio::join!(
copy_one_direction(&mut in_r, &mut out_w),
copy_one_direction(&mut out_r, &mut in_w),
);
println!("relay finished");
});
} else {
eprintln!("dial outbound connection failed");
}
} else {
eprintln!("accept connection failed");
return;
}
}
}
async fn copy_one_direction<FROM: AsyncReadRent, TO: AsyncWriteRent>(
mut from: FROM,
to: &mut TO,
) -> Result<Vec<u8>, std::io::Error> {
let mut buf = Vec::with_capacity(8 * 1024);
let mut res;
loop {
// read
(res, buf) = from.read(buf).await;
if res? == 0 {
return Ok(buf);
}
// write all
(res, buf) = to.write_all(buf).await;
res?;
// clear
buf.clear();
}
}
|