Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
37 changes: 18 additions & 19 deletions clash_lib/src/proxy/http/inbound/proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,23 +148,22 @@ pub async fn handle(
authenticator: ThreadSafeAuthenticator,
fw_mark: Option<u32>,
) {
tokio::task::spawn(async move {
if let Err(http_err) = http1::Builder::new()
.preserve_header_case(true)
.title_case_headers(true)
.serve_connection(
stream,
ProxyService {
src,
dispatcher,
authenticator,
fw_mark,
},
)
.with_upgrades()
.await
{
warn!("Error while serving HTTP connection: {}", http_err);
}
});
let result = http1::Builder::new()
.preserve_header_case(true)
.title_case_headers(true)
.serve_connection(
stream,
ProxyService {
src,
dispatcher,
authenticator,
fw_mark,
},
)
.with_upgrades()
.await;

if let Err(http_err) = result {
warn!("Error while serving HTTP connection: {}", http_err);
}
}
50 changes: 38 additions & 12 deletions clash_lib/src/proxy/mixed/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,37 @@ impl InboundHandlerTrait for MixedInbound {
let listener = TcpListener::bind(self.addr).await?;

loop {
let (socket, _) = listener.accept().await?;
let src_addr = socket.peer_addr()?;
let (socket, _) = match listener.accept().await {
Ok(s) => s,
Err(e) => {
warn!("failed to accept socket on {}: {:?}", self.addr, e);
continue;
}
};
let src_addr = match socket.peer_addr() {
Ok(a) => a,
Err(e) => {
warn!("failed to get peer address: {:?}", e);
continue;
}
};
if !self.allow_lan && src_addr.ip() != socket.local_addr()?.ip() {
warn!("Connection from {} is not allowed", src_addr);
continue;
}
apply_tcp_options(&socket)?;

let mut p = [0; 1];
let n = socket.peek(&mut p).await?;
let n = match socket.peek(&mut p).await {
Ok(n) => n,
Err(e) => {
warn!(
"failed to peek socket on mixed listener {}: {:?}",
self.addr, e
);
continue;
}
};
if n != 1 {
warn!("failed to peek socket on mixed listener {}", self.addr);
continue;
Expand Down Expand Up @@ -98,14 +119,19 @@ impl InboundHandlerTrait for MixedInbound {

_ => {
let src = socket.peer_addr()?;
http::handle_http(
Box::new(socket),
src,
dispatcher,
authenticator,
fw_mark,
)
.await;
let dispatcher = dispatcher.clone();
let authenticator = authenticator.clone();
let fw_mark = fw_mark;
tokio::spawn(async move {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the handle_http is already a spawn, maybe change the signature to sync and no need to spawn again here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or remote the spawn in handle_http to be consistent with the socks handle

Copy link
Contributor Author

@kkqin kkqin Jun 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’m planning to switch to a synchronous call with only minor changes. Please don’t mind this annoying warning.

http::handle_http(
Box::new(socket),
src,
dispatcher,
authenticator,
fw_mark,
)
.await;
});
}
}
}
Expand All @@ -114,4 +140,4 @@ impl InboundHandlerTrait for MixedInbound {
async fn listen_udp(&self) -> std::io::Result<()> {
Err(new_io_error("UDP is not supported"))
}
}
}
Loading