feat: add bytes dependency and optimize buffer handling in test_delay function

This commit is contained in:
Tunglies 2026-04-12 03:21:21 +08:00
parent 6fea76f7e3
commit 20fddc5cff
No known key found for this signature in database
GPG Key ID: B9B01B389469B3E8
3 changed files with 6 additions and 4 deletions

1
Cargo.lock generated
View File

@ -1118,6 +1118,7 @@ dependencies = [
"base64 0.22.1",
"bitflags 2.11.0",
"boa_engine",
"bytes",
"chrono",
"clash-verge-draft",
"clash-verge-i18n",

View File

@ -108,6 +108,7 @@ webpki-roots = "1.0"
rust_iso3166 = "0.1.14"
# Use the git repo until the next release after v2.0.0.
dark-light = { git = "https://github.com/rust-dark-light/dark-light" }
bytes = "1.11.1"
[target.'cfg(target_os = "macos")'.dependencies]
objc2 = "0.6"

View File

@ -5,6 +5,7 @@ use crate::{
process::AsyncHandler,
utils,
};
use bytes::BytesMut;
use clash_verge_logging::{Type, logging};
use serde_yaml_ng::{Mapping, Value};
use smartstring::alias::String;
@ -127,6 +128,7 @@ pub async fn test_delay(url: String) -> anyhow::Result<u32> {
tokio::time::timeout(Duration::from_secs(10), async {
let start = Instant::now();
let mut buf = BytesMut::with_capacity(1024);
if is_https {
let stream = match proxy_port {
@ -134,9 +136,8 @@ pub async fn test_delay(url: String) -> anyhow::Result<u32> {
let mut s = TcpStream::connect(format!("127.0.0.1:{pp}")).await?;
s.write_all(format!("CONNECT {host}:{port} HTTP/1.1\r\nHost: {host}:{port}\r\n\r\n").as_bytes())
.await?;
let mut buf = [0u8; 1024];
let n = s.read(&mut buf).await?;
if !std::str::from_utf8(&buf[..n]).unwrap_or("").contains("200") {
s.read_buf(&mut buf).await?;
if !buf.windows(3).any(|w| w == b"200") {
return Err(anyhow::anyhow!("Proxy CONNECT failed"));
}
s
@ -166,7 +167,6 @@ pub async fn test_delay(url: String) -> anyhow::Result<u32> {
),
};
stream.write_all(req.as_bytes()).await?;
let mut buf = [0u8; 1024];
let _ = stream.read(&mut buf).await?;
}