reqwest_ss_proxy 0.1.3

A reqwest middleware for proxying requests through Shadowsocks. / 一个用于 reqwest 的 Shadowsocks 代理中间件。
Documentation
# reqwest_ss_proxy

[English](#english) | [中文](#中文)

---

## English

A `reqwest` middleware for proxying requests through a Shadowsocks server.

### Installation

Add this to your `Cargo.toml`:

```toml
[dependencies]
reqwest_ss_proxy = "0.1.1"
anyhow = "1.0"
tokio = { version = "1", features = ["full"] }
reqwest = "0.12"
reqwest-middleware = "0.4"
```

### Usage

Here is an example of how to use `SsMiddleware` with `reqwest_middleware::ClientBuilder`, based on `tests/main.rs`.

```rust
use anyhow::Result;
use reqwest_middleware::{ClientBuilder, ClientWithMiddleware};
use reqwest_ss_proxy::{reqwest::SsMiddleware, SsConnector};

#[tokio::main]
async fn main() -> Result<()> {
  // 1. Create the SsConnector from your Shadowsocks URL.
  //    Replace with your Shadowsocks server URL.
  let url = "ss://aes-256-gcm:password@server:port";
  let connector = match SsConnector::new(url) {
    Ok(c) => c,
    Err(e) => {
      panic!("Failed to create connector: {}", e);
    }
  };

  // 2. Create the SsMiddleware from the connector.
  let ss_middleware = SsMiddleware::new(connector);

  // 3. Build the reqwest client with the middleware.
  let client: ClientWithMiddleware =
    ClientBuilder::new(reqwest::Client::new()).with(ss_middleware).build();

  // 4. Send requests through the proxy.
  for test_url in ["http://ifconfig.me/ip", "https://ifconfig.me/ip"] {
    match client.get(test_url).send().await {
      Ok(res) => {
        let status = res.status();
        let ip = res.text().await?;
        println!("Request to {test_url}: Status={status}, IP={ip}");
      }
      Err(e) => {
        eprintln!("Request to {test_url} failed: {e}");
      }
    }
  }

  Ok(())
}
```

---

## 中文

一个用于 `reqwest` 的 Shadowsocks 代理中间件。

### 安装

将此添加到您的 `Cargo.toml` 中:

```toml
[dependencies]
reqwest_ss_proxy = "0.1.1"
anyhow = "1.0"
tokio = { version = "1", features = ["full"] }
reqwest = "0.12"
reqwest-middleware = "0.4"
```

### 使用

这是一个如何将 `SsMiddleware` 与 `reqwest_middleware::ClientBuilder` 结合使用的示例,参考自 `tests/main.rs`。

```rust
use anyhow::Result;
use reqwest_middleware::{ClientBuilder, ClientWithMiddleware};
use reqwest_ss_proxy::{reqwest::SsMiddleware, SsConnector};

#[tokio::main]
async fn main() -> Result<()> {
  // 1. 从你的 Shadowsocks URL 创建 SsConnector。
  //    请替换为你的 Shadowsocks 服务器 URL。
  let url = "ss://aes-256-gcm:password@server:port";
  let connector = match SsConnector::new(url) {
    Ok(c) => c,
    Err(e) => {
      panic!("创建连接器失败: {}", e);
    }
  };

  // 2. 从连接器创建 SsMiddleware。
  let ss_middleware = SsMiddleware::new(connector);

  // 3. 使用中间件构建 reqwest 客户端。
  let client: ClientWithMiddleware =
    ClientBuilder::new(reqwest::Client::new()).with(ss_middleware).build();

  // 4. 通过代理发送请求。
  for test_url in ["http://ifconfig.me/ip", "https://ifconfig.me/ip"] {
    match client.get(test_url).send().await {
      Ok(res) => {
        let status = res.status();
        let ip = res.text().await?;
        println!("请求 {test_url}: Status={status}, IP={ip}");
      }
      Err(e) => {
        eprintln!("请求 {test_url} 失败: {e}");
      }
    }
  }

  Ok(())
}
```

<+ ../about.md >