# reqwest-ss-proxy
[English](#english) | [中文](#中文)
---
## English
A `hyper` connector and `reqwest` middleware for proxying requests through a Shadowsocks server.
### Features
- **Hyper Connector**: Implements `tower::Service` to act as a connector for `hyper 1.x`.
- **Reqwest Middleware**: Provides a middleware for `reqwest` to easily proxy requests.
- **Easy to Use**: Create a connector or middleware from a Shadowsocks URL.
- **Async**: Fully asynchronous with `tokio`.
### Usage
#### With `hyper`
```rust
use reqwest_ss_proxy::SsConnector;
use hyper_util::client::legacy::Client;
use hyper::body::Buf;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// 1. Create the SsConnector from a Shadowsocks URL.
let connector = SsConnector::new("ss://aes-256-gcm:password@server:port")? ;
// 2. Create a hyper client with the connector.
let client = Client::builder(hyper_util::rt::TokioExecutor::new()).build(connector);
// 3. Send a request.
let mut res = client.get("https://ifconfig.me/ip".parse()?).await? ;
while let Some(chunk) = res.body_mut().data().await {
println!("IP: {}", String::from_utf8_lossy(&chunk?));
}
Ok(())
}
```
#### With `reqwest`
```rust
use reqwest_ss_proxy::{SsConnector, reqwest::SsMiddleware};
use reqwest_middleware::{ClientBuilder, ClientWithMiddleware};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// 1. Create the SsConnector.
let connector = SsConnector::new("ss://aes-256-gcm:password@server:port")? ;
// 2. Create the SsMiddleware from the connector.
let ss_middleware = SsMiddleware::new(connector);
// 3. Build the reqwest client with the middleware.
let client = reqwest::Client::builder().build()? ;
let client_with_middleware: ClientWithMiddleware = ClientBuilder::new(client).with(ss_middleware).build();
// 4. Send the request using the reqwest API.
let res = client_with_middleware.get("http://ifconfig.me/ip").send().await? ;
let ip = res.text().await? ;
println!("IP: {}", ip);
Ok(())
}
```
---
## 中文
一个用于 `hyper` 的连接器和 `reqwest` 的中间件,可通过 Shadowsocks 服务器代理请求。
### 特性
- **Hyper 连接器**: 实现了 `tower::Service`,可作为 `hyper 1.x` 的连接器。
- **Reqwest 中间件**: 提供了 `reqwest` 中间件,可轻松代理请求。
- **易于使用**: 从 Shadowsocks URL 创建连接器或中间件。
- **异步**: 基于 `tokio` 的完全异步实现。
### 使用
#### 配合 `hyper`
```rust
use reqwest_ss_proxy::SsConnector;
use hyper_util::client::legacy::Client;
use hyper::body::Buf;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// 1. 从 Shadowsocks URL 创建 SsConnector。
let connector = SsConnector::new("ss://aes-256-gcm:password@server:port")? ;
// 2. 使用连接器创建 hyper 客户端。
let client = Client::builder(hyper_util::rt::TokioExecutor::new()).build(connector);
// 3. 发送请求。
let mut res = client.get("http://ifconfig.me/ip".parse()?).await? ;
while let Some(chunk) = res.body_mut().data().await {
println!("IP: {}", String::from_utf8_lossy(&chunk?));
}
Ok(())
}
```
#### 配合 `reqwest`
```rust
use reqwest_ss_proxy::{SsConnector, reqwest::SsMiddleware};
use reqwest_middleware::{ClientBuilder, ClientWithMiddleware};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// 1. 创建 SsConnector。
let connector = SsConnector::new("ss://aes-256-gcm:password@server:port")? ;
// 2. 从连接器创建 SsMiddleware。
let ss_middleware = SsMiddleware::new(connector);
// 3. 使用中间件构建 reqwest 客户端。
let client = reqwest::Client::builder().build()? ;
let client_with_middleware: ClientWithMiddleware = ClientBuilder::new(client).with(ss_middleware).build();
// 4. 使用 reqwest API 发送请求。
let res = client_with_middleware.get("https://ifconfig.me/ip").send().await? ;
let ip = res.text().await? ;
println!("IP: {}", ip);
Ok(())
}
```
<+ ../about.md >