Skip to content

Commit 90dd94c

Browse files
committed
fix possible infinite loop issue for socks proxy
1 parent b9bb420 commit 90dd94c

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

src/server.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,11 @@ impl Server {
637637
.map_err(|_: Elapsed| ProxyError::Timeout)?
638638
.map_err(|_| ProxyError::BadRequest)?;
639639

640+
if nread == 0 {
641+
error!("invalid request");
642+
return Err(ProxyError::BadRequest);
643+
}
644+
640645
if proxy_handler.is_none() {
641646
proxy_handler = Some(Self::create_proxy_handler(
642647
&params.proto,

src/socks/socks_proxy_handler.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ use std::{
1414
};
1515
use tokio::{io::AsyncWriteExt, net::TcpStream};
1616

17+
const SOCKS_MAX_RETRY_COUNT: u8 = 20;
18+
1719
#[derive(PartialEq, Debug)]
1820
pub(crate) enum InternalParseState {
1921
SelectMethod,
@@ -26,6 +28,7 @@ pub struct SocksProxyHandler {
2628
bnd_addr: SocketAddr,
2729
state: InternalParseState,
2830
target_addr: Option<NetAddr>,
31+
retry_count: u8,
2932
}
3033

3134
impl SocksProxyHandler {
@@ -39,6 +42,7 @@ impl SocksProxyHandler {
3942
SocksVersion::V4 => InternalParseState::SelectedMethod,
4043
},
4144
target_addr: None,
45+
retry_count: 0,
4246
}
4347
}
4448

@@ -220,7 +224,17 @@ impl ProxyHandler for SocksProxyHandler {
220224
return ParseState::ReceivedRequest(self.target_addr.as_ref().unwrap());
221225
}
222226

223-
ParseState::Pending
227+
self.retry_count += 1;
228+
229+
if self.retry_count >= SOCKS_MAX_RETRY_COUNT {
230+
error!(
231+
"unexpected socks request failed with retry count: {}",
232+
self.retry_count
233+
);
234+
self.fail_with_resp()
235+
} else {
236+
ParseState::Pending
237+
}
224238
}
225239

226240
async fn handle(

src/socks/socks_resp_parser.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ pub(crate) enum State {
1010
SelectMethod,
1111
IdentifyingMethod,
1212
Connect,
13-
Connecting,
1413
NegotiationCompleted,
1514
ErrorOccurred(SocksError),
1615
}
@@ -67,7 +66,6 @@ impl SocksRespParser {
6766
}
6867

6968
if self.state == State::Connect {
70-
self.state = State::Connecting;
7169
let bytes = self.buffer.as_bytes();
7270
let resp_size = bytes.len();
7371

0 commit comments

Comments
 (0)