跳到主要内容

Rust

配置 Rust reqwest 请求的代理与证书处理,使其 HTTP/HTTPS 流量显示在 Proxyman 中。

1. 问题​

由于以下原因,Proxyman 无法自动抓取 Rust 中 reqwest 的 HTTP/HTTPS 流量:

  • reqwest 不遵循系统 HTTP Proxy 设置,因此没有流量经过 Proxyman
  • reqwest 不信任 Proxyman 的自签名证书,因此会出现 SSL 错误

2. 解决方案​

  • 手动将代理设置为 Proxyman,地址为 http://localhost:9090
  • 禁用 SSL 验证
use anyhow::Result;
use reqwest::Url;

#[tokio::main]
async fn main() -> Result<()> {
let proxy_url = Url::parse("http://localhost:9090")?;
let client = reqwest::Client::builder()
.danger_accept_invalid_certs(true) // trust self-signed certificate
.proxy(reqwest::Proxy::https(proxy_url)?) // Proxy to Proxyman
.build()?;
let response = client.get("https://httpbin.org/get").send().await?;

if response.status().is_success() {
let body = response.text().await?;
println!("Response Text: {}", body);
} else {
println!("Request failed with status: {}", response.status());
}

Ok(())
}
信息

对于忽略系统代理的应用,你也可以尝试 Network Extension(Beta,需要 macOS 26 或更高版本)。它无需在每个应用中设置代理即可抓取流量。对于 HTTPS,你的 Rust 客户端仍需信任 Proxyman 证书,或者像上面的示例那样跳过验证。