diff --git a/vnt/Cargo.toml b/vnt/Cargo.toml index fd98962..e6f6e4e 100644 --- a/vnt/Cargo.toml +++ b/vnt/Cargo.toml @@ -6,6 +6,7 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +http_req = { git = "https://github.com/lmq8267/http_req.git", default-features = false, features = ["rust-tls"] } tun-rs = { version = "2", optional = true, features = ["experimental"] } packet = { path = "./packet" } bytes = "1.5.0" diff --git a/vnt/src/channel/ws_channel.rs b/vnt/src/channel/ws_channel.rs index 30af458..b11eed8 100644 --- a/vnt/src/channel/ws_channel.rs +++ b/vnt/src/channel/ws_channel.rs @@ -112,7 +112,7 @@ where url = redirect.to_string(); } println!("Location:{}", url); - log::info!("最终地址: {}", url); + log::info!("修改后的重定向地址: {}", url); continue; } } diff --git a/vnt/src/util/dns_query.rs b/vnt/src/util/dns_query.rs index 58d3399..25643a5 100644 --- a/vnt/src/util/dns_query.rs +++ b/vnt/src/util/dns_query.rs @@ -149,7 +149,7 @@ pub fn dns_query_all( } continue; } - + let end_index = current_domain .rfind(':') .with_context(|| format!("{:?} not port", current_domain))?; @@ -235,56 +235,73 @@ fn check_for_redirect(domain: &String) -> anyhow::Result> { } else { format!("http://{}", domain) }; - // 解析 URL - let uri = match Uri::try_from(url.as_str()) { - Ok(u) => u, - Err(e) => { - println!("解析地址失败: {}", e); - return Ok(None); - } - }; - let mut response_body = Vec::new(); + let mut count = 0; // 重定向次数计数器 + let mut last_redirect_url: Option = None; // 记录最后一个重定向的 URL - // 发送 HTTP 请求 - let response = match Request::new(&uri) - .timeout(Duration::from_secs(20)) - .redirect_policy(RedirectPolicy::Limit(0)) - .send(&mut response_body) - { - Ok(resp) => { - println!("HTTP Status Code: {}", resp.status_code()); - resp + loop { + count += 1; + if count > 3 { + println!("重定向次数超过 3 次,跳过"); + return Ok(last_redirect_url); } - Err(_) => { - return Ok(None); - } - }; - let body_str = String::from_utf8_lossy(&response_body); - let cleaned_body = body_str.replace('\n', "").replace('\r', ""); - println!("Response Body: {}", cleaned_body); - // 处理 3XX 重定向 - if response.status_code().is_redirect() { - if let Some(location) = response.headers().get("Location") { - url = location.to_string().trim_end_matches('/').to_string(); - println!("Location: {}", url); - return Ok(Some(url)); - } - } - - // 处理 200 响应 - if response.status_code().is_success() { - for line in body_str.lines() { - let trimmed = line.trim(); - if parse_host_port(trimmed) { - println!("text: {}", trimmed); - return Ok(Some(trimmed.to_string())); + // 解析 URL + let uri = match Uri::try_from(url.as_str()) { + Ok(u) => { + u } - } - return Ok(None); + Err(e) => { + println!("解析地址失败: {}", e); + return Ok(last_redirect_url); + } + }; + + let mut response_body = Vec::new(); + + // 发送 HTTP 请求 + let response = match Request::new(&uri) + .timeout(Duration::from_secs(10)) + .redirect_policy(RedirectPolicy::Limit(0)) + .send(&mut response_body) + { + Ok(resp) => { + println!("HTTP Status Code: {}", resp.status_code()); + resp + } + Err(e) => { + return Ok(last_redirect_url); + } + }; + + let body_str = String::from_utf8_lossy(&response_body); + let cleaned_body = body_str.replace('\n', "").replace('\r', ""); + println!("Response Body: {}", cleaned_body); + // 处理 3XX 重定向 + if response.status_code().is_redirect() { + if let Some(location) = response.headers().get("Location") { + url = location.to_string().trim_end_matches('/').to_string(); + last_redirect_url = Some(url.clone()); // 更新最后的重定向地址 + println!("Location: {}", url); + continue; + } else { + return Ok(last_redirect_url); + } + } + + // 处理 200 响应 + else if response.status_code().is_success() { + for line in body_str.lines() { + let trimmed = line.trim(); + if parse_host_port(trimmed) { + println!("text: {}", trimmed); + return Ok(Some(trimmed.to_string())); + } + } + return Ok(last_redirect_url); + } + return Ok(last_redirect_url); } - Ok(None) } /// 去掉 http:// 或 https:// 前缀