Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/socks/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use connection::{
bind::{self, Bind},
connect::{self, Connect},
};
use tokio::net::TcpListener;
use tokio::{net::TcpListener, sync::Semaphore};

pub mod auth;
pub mod connection;
Expand All @@ -31,6 +31,7 @@ pub struct Socks5Server {
listener: TcpListener,
auth: Arc<AuthAdaptor>,
connector: Connector,
sem: Arc<Semaphore>,
}

impl Socks5Server {
Expand All @@ -54,6 +55,7 @@ impl Socks5Server {
listener: socket.listen(ctx.concurrent as _)?,
auth: Arc::new(auth),
connector: ctx.connector,
sem: Arc::new(Semaphore::new(ctx.concurrent)),
})
}
}
Expand All @@ -65,7 +67,14 @@ impl Serve for Socks5Server {
while let Ok((stream, socket_addr)) = self.listener.accept().await {
let connector = self.connector.clone();
let auth = self.auth.clone();
let sem = self.sem.clone();
tokio::spawn(async move {
let _permit = match sem.acquire().await {
Ok(permit) => permit,
Err(_) => {
return;
}
};
if let Err(err) = handle(
IncomingConnection::new(stream, auth),
socket_addr,
Expand Down
Loading