Skip to content

Commit 6fcde16

Browse files
committed
fix
1 parent 828c25c commit 6fcde16

9 files changed

+45
-19
lines changed

Cargo.toml

+4-1
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,13 @@ pki-types = { package = "rustls-pki-types", version = "1" }
3636
s2n-quic = { version = "1", optional = true }
3737
quinn = { version = "0.10", optional = true }
3838

39-
4039
[target.'cfg(not(target_os = "windows"))'.dependencies]
4140
aws-lc-rs = { version = "1.5", features = ["bindgen"], optional = true }
4241

42+
[target.'cfg(not(target_arch = "arm"))'.dependencies]
43+
mimalloc = { version = "*" }
44+
45+
4346
[features]
4447
default = ["s2n_quic"]
4548
s2n_quic = ["dep:s2n-quic", "dep:aws-lc-rs"]

README.md

+3-4
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ Rust practice project
22

33
## Features
44

5-
- QUIC/TLS Transport
6-
- HTTP/Socks5 Proxy
5+
- QUIC/TLS Transport
6+
- HTTP/Socks5 Proxy
77

88

99
# Getting Started
@@ -40,5 +40,4 @@ $ ./rsnova --role client --cert ./cert.pem --listen 127.0.0.1:48100 --remote tl
4040
```
4141

4242
**Use Proxy**
43-
Now you can configure `socks5://127.0.0.1:48100` or `http://127.0.0.1:48100` as the proxy for your browser/tools.
44-
43+
Now you can configure `socks5://127.0.0.1:48100` or `http://127.0.0.1:48100` as the proxy for your browser/tools.

src/main.rs

+11
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ mod mux;
1717
mod tunnel;
1818
mod utils;
1919

20+
#[cfg(not(target_arch = "arm"))]
21+
#[global_allocator]
22+
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
23+
2024
#[derive(ValueEnum, Clone, Debug)]
2125
enum Protocol {
2226
Tls,
@@ -66,6 +70,9 @@ struct Args {
6670
#[clap(default_value = "1048576", long)]
6771
thread_stack_size: usize,
6872

73+
#[clap(default_value = "30", long)]
74+
idle_timeout_secs: usize,
75+
6976
#[clap(default_value = "mydomain.io", long)]
7077
tls_host: String,
7178

@@ -117,6 +124,7 @@ async fn service_main(args: &Args) -> anyhow::Result<()> {
117124
args.cert.as_ref().unwrap(),
118125
&args.tls_host,
119126
args.concurrent,
127+
args.idle_timeout_secs,
120128
)
121129
.await?
122130
}
@@ -126,6 +134,7 @@ async fn service_main(args: &Args) -> anyhow::Result<()> {
126134
args.cert.as_ref().unwrap(),
127135
&args.tls_host,
128136
args.concurrent,
137+
args.idle_timeout_secs,
129138
)
130139
.await?
131140
}
@@ -160,6 +169,7 @@ async fn service_main(args: &Args) -> anyhow::Result<()> {
160169
&args.listen,
161170
args.cert.as_ref().unwrap(),
162171
args.key.as_ref().unwrap(),
172+
args.idle_timeout_secs,
163173
)
164174
.await
165175
{
@@ -171,6 +181,7 @@ async fn service_main(args: &Args) -> anyhow::Result<()> {
171181
&args.listen,
172182
args.cert.as_ref().unwrap(),
173183
args.key.as_ref().unwrap(),
184+
args.idle_timeout_secs,
174185
)
175186
.await
176187
{

src/tunnel/client.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ impl<T: MuxConnection> MuxClientTrait for MuxClient<T> {
101101
pub(crate) async fn mux_client_loop<T: MuxClientTrait>(
102102
mut client: T,
103103
mut receiver: mpsc::UnboundedReceiver<Message>,
104+
idle_timeout_secs: usize,
104105
) where
105106
<T as MuxClientTrait>::SendStream: 'static,
106107
<T as MuxClientTrait>::RecvStream: 'static,
@@ -130,7 +131,7 @@ pub(crate) async fn mux_client_loop<T: MuxClientTrait>(
130131
&mut recv,
131132
&mut send,
132133
);
133-
if let Err(e) = stream.transfer().await {
134+
if let Err(e) = stream.transfer(idle_timeout_secs).await {
134135
tracing::error!("transfer finish:{}", e);
135136
}
136137
}

src/tunnel/s2n_quic_client.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ use super::client::MuxConnection;
1313
use super::Message;
1414

1515
pub struct S2NQuicConnection {
16-
// inner: Option<quinn::Connection>,
17-
// endpoint: Arc<quinn::Endpoint>,
1816
inner: Option<s2n_quic::Connection>,
1917
endpoint: Arc<s2n_quic::client::Client>,
2018
}
@@ -77,6 +75,7 @@ impl MuxClient<S2NQuicConnection> {
7775
cert_path: &Path,
7876
host: &String,
7977
count: usize,
78+
idle_timeout_secs: usize,
8079
) -> anyhow::Result<mpsc::UnboundedSender<Message>> {
8180
match url.scheme() {
8281
"quic" => {
@@ -107,7 +106,7 @@ impl MuxClient<S2NQuicConnection> {
107106
}
108107
client.conns.push(quic_conn);
109108
}
110-
tokio::spawn(mux_client_loop(client, receiver));
109+
tokio::spawn(mux_client_loop(client, receiver, idle_timeout_secs));
111110
Ok(sender)
112111
}
113112
_ => Err(anyhow!("unsupported schema:{:?}", url.scheme())),
@@ -145,6 +144,7 @@ pub async fn new_quic_client(
145144
cert_path: &Path,
146145
host: &String,
147146
count: usize,
147+
idle_timeout_secs: usize,
148148
) -> anyhow::Result<mpsc::UnboundedSender<Message>> {
149-
MuxClient::<S2NQuicConnection>::from(url, cert_path, host, count).await
149+
MuxClient::<S2NQuicConnection>::from(url, cert_path, host, count, idle_timeout_secs).await
150150
}

src/tunnel/s2n_quic_remote.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ pub async fn start_quic_remote_server(
1313
listen: &SocketAddr,
1414
cert_path: &Path,
1515
key_path: &Path,
16+
idle_timeout_secs: usize,
1617
) -> Result<()> {
1718
let io = s2n_quic::provider::io::tokio::Builder::default()
1819
.with_receive_address(*listen)?
@@ -30,7 +31,10 @@ pub async fn start_quic_remote_server(
3031
metrics::increment_gauge!("quic_server_proxy_streams", 1.0);
3132
let (mut recv_stream, mut send_stream) = stream.split();
3233
tokio::spawn(async move {
33-
if let Err(e) = handle_server_stream(&mut recv_stream, &mut send_stream).await {
34+
if let Err(e) =
35+
handle_server_stream(&mut recv_stream, &mut send_stream, idle_timeout_secs)
36+
.await
37+
{
3438
tracing::error!("failed: {reason}", reason = e.to_string());
3539
}
3640
metrics::decrement_gauge!("quic_server_proxy_streams", 1.0);

src/tunnel/stream.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -114,18 +114,18 @@ where
114114
}
115115
}
116116

117-
pub async fn transfer(&mut self) -> Result<()> {
117+
pub async fn transfer(&mut self, idle_timeout_secs: usize) -> Result<()> {
118118
let state = Arc::new(TransferState::new());
119119
let client_to_server = timeout_copy(
120120
&mut self.local_reader,
121121
&mut self.remote_writer,
122-
DEFAULT_TIMEOUT_SECS,
122+
idle_timeout_secs as u64,
123123
state.clone(),
124124
);
125125
let server_to_client = timeout_copy(
126126
&mut self.remote_reader,
127127
&mut self.local_writer,
128-
DEFAULT_TIMEOUT_SECS,
128+
idle_timeout_secs as u64,
129129
state.clone(),
130130
);
131131
try_join(client_to_server, server_to_client).await?;
@@ -136,6 +136,7 @@ where
136136
pub async fn handle_server_stream<'a, LR: AsyncReadExt + Unpin, LW: AsyncWriteExt + Unpin>(
137137
mut lr: &'a mut LR,
138138
lw: &'a mut LW,
139+
idle_timeout_secs: usize,
139140
) -> Result<()> {
140141
let timeout_secs = Duration::from_secs(DEFAULT_TIMEOUT_SECS);
141142
match timeout(timeout_secs, event::read_event(&mut lr)).await? {
@@ -163,7 +164,7 @@ pub async fn handle_server_stream<'a, LR: AsyncReadExt + Unpin, LW: AsyncWriteEx
163164
tokio::net::tcp::ReadHalf<'_>,
164165
tokio::net::tcp::WriteHalf<'_>,
165166
> = Stream::new(lr, lw, &mut remote_receiver, &mut remote_sender);
166-
stream.transfer().await
167+
stream.transfer(idle_timeout_secs).await
167168
}
168169
}
169170
}

src/tunnel/tls_client.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ impl MuxClient<TlsConnection> {
7070
cert_path: &Path,
7171
host: &String,
7272
count: usize,
73+
idle_timeout_secs: usize,
7374
) -> anyhow::Result<mpsc::UnboundedSender<Message>> {
7475
match url.scheme() {
7576
"tls" => {
@@ -98,7 +99,7 @@ impl MuxClient<TlsConnection> {
9899
}
99100
client.conns.push(tls_conn);
100101
}
101-
tokio::spawn(mux_client_loop(client, receiver));
102+
tokio::spawn(mux_client_loop(client, receiver, idle_timeout_secs));
102103
Ok(sender)
103104
}
104105
_ => Err(anyhow!("unsupported schema:{:?}", url.scheme())),
@@ -147,6 +148,7 @@ pub async fn new_tls_client(
147148
cert_path: &Path,
148149
host: &String,
149150
count: usize,
151+
idle_timeout_secs: usize,
150152
) -> anyhow::Result<mpsc::UnboundedSender<Message>> {
151-
MuxClient::<TlsConnection>::from(url, cert_path, host, count).await
153+
MuxClient::<TlsConnection>::from(url, cert_path, host, count, idle_timeout_secs).await
152154
}

src/tunnel/tls_remote.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ pub async fn start_tls_remote_server(
2323
listen: &SocketAddr,
2424
cert_path: &Path,
2525
key_path: &Path,
26+
idle_timeout_secs: usize,
2627
) -> Result<()> {
2728
// let key = fs::read(key_path.clone()).context("failed to read private key")?;
2829
// let key = rsa_private_keys(&mut BufReader::new(File::open(key_path)?))
@@ -86,7 +87,7 @@ pub async fn start_tls_remote_server(
8687
let fut = async move {
8788
let stream = acceptor.accept(stream).await?;
8889
tracing::info!("TLS connection incoming");
89-
handle_tls_connection(stream, conn_id).await?;
90+
handle_tls_connection(stream, conn_id, idle_timeout_secs).await?;
9091
Ok(()) as Result<()>
9192
};
9293

@@ -102,6 +103,7 @@ pub async fn start_tls_remote_server(
102103
async fn handle_tls_connection(
103104
conn: tokio_rustls::server::TlsStream<tokio::net::TcpStream>,
104105
id: u32,
106+
idle_timeout_secs: usize,
105107
) -> Result<()> {
106108
let (r, w) = tokio::io::split(conn);
107109
let mux_conn = mux::Connection::new(r, w, mux::Mode::Server, id);
@@ -112,7 +114,10 @@ async fn handle_tls_connection(
112114
tokio::spawn(async move {
113115
let stream_id = stream.id();
114116
let (mut stream_reader, mut stream_writer) = tokio::io::split(stream);
115-
if let Err(e) = handle_server_stream(&mut stream_reader, &mut stream_writer).await {
117+
if let Err(e) =
118+
handle_server_stream(&mut stream_reader, &mut stream_writer, idle_timeout_secs)
119+
.await
120+
{
116121
tracing::error!(
117122
"[{}/{}]failed: {reason}",
118123
id,

0 commit comments

Comments
 (0)