Skip to content

Commit

Permalink
feat(client): Adapt thread-safe update client configuration (#404)
Browse files Browse the repository at this point in the history
  • Loading branch information
0x676e67 authored Feb 7, 2025
1 parent a313ba0 commit e6397d6
Show file tree
Hide file tree
Showing 17 changed files with 357 additions and 327 deletions.
6 changes: 1 addition & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ percent-encoding = "2.3"
tokio = { version = "1", default-features = false, features = ["net","time"] }
pin-project-lite = "0.2.0"
ipnet = "2.11.0"
arc-swap = "1.7.0"

## util
socket2 = { version = "0.5", features = ["all"] }
Expand Down Expand Up @@ -309,11 +310,6 @@ name = "request_with_redirect"
path = "examples/request_with_redirect.rs"
required-features = ["full"]

[[example]]
name = "set_redirect"
path = "examples/set_redirect.rs"
required-features = ["full"]

[[example]]
name = "base_url"
path = "examples/base_url.rs"
Expand Down
8 changes: 2 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
[![CI](https://github.com/0x676e67/rquest/actions/workflows/ci.yml/badge.svg)](https://github.com/0x676e67/rquest/actions/workflows/ci.yml)
[![Crates.io License](https://img.shields.io/crates/l/rquest)](./LICENSE)
[![crates.io](https://img.shields.io/crates/v/rquest.svg)](https://crates.io/crates/rquest)
[![Documentation](https://docs.rs/rquest/badge.svg)](https://docs.rs/rquest)
[![Crates.io Total Downloads](https://img.shields.io/crates/d/rquest)](https://crates.io/crates/rquest)

> 🚀 Help me work seamlessly with open source sharing by [sponsoring me on GitHub](https://github.com/0x676e67/0x676e67/blob/main/SPONSOR.md)
Expand All @@ -20,11 +21,6 @@ An ergonomic, all-in-one HTTP client for spoofing any browser with `TLS`, `JA3`/
- HTTPS via BoringSSL
- Perfectly Chrome, Safari, and Firefox

Additional learning resources include:

- [API Documentation](https://docs.rs/rquest)
- [Repository Examples](https://github.com/0x676e67/rquest/tree/main/examples)

## Example

This asynchronous example uses [Tokio](https://tokio.rs) and enables some optional features. Your `Cargo.toml` could look like this:
Expand Down Expand Up @@ -131,7 +127,7 @@ If you would like to submit your contribution, please open a [Pull Request](http

## License

Apache-2.0 [LICENSE](LICENSE)
Licensed under either of Apache-2.0 [License](LICENSE)

## Accolades

Expand Down
4 changes: 2 additions & 2 deletions examples/base_url.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ async fn main() -> Result<(), rquest::Error> {
.init();

// Build a client to impersonate Edge131
let mut client = Client::builder()
let client = Client::builder()
.impersonate(Impersonate::Edge131)
.base_url("https://httpbin.org")
.build()?;
Expand All @@ -21,7 +21,7 @@ async fn main() -> Result<(), rquest::Error> {
println!("{}", resp.text().await?);

// Reset the base url
client.as_mut().base_url("https://tls.peet.ws");
client.as_mut().base_url("https://tls.peet.ws").apply()?;

// Send a request to tls.peet.ws /api/all
let resp = client.get("/api/all").send().await?;
Expand Down
5 changes: 3 additions & 2 deletions examples/client_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ async fn main() -> Result<(), rquest::Error> {
];

// Build a client to impersonate Chrome131
let mut client = Client::builder()
let client = Client::builder()
.impersonate(Impersonate::Chrome131)
.build()?;

Expand All @@ -31,7 +31,8 @@ async fn main() -> Result<(), rquest::Error> {
.impersonate(Impersonate::Safari18)
.headers_order(HEADER_ORDER)
.interface("utun4")
.base_url("https://tls.peet.ws");
.base_url("https://tls.peet.ws")
.apply()?;

let text = client.get("/api/all").send().await?.text().await?;
println!("{}", text);
Expand Down
2 changes: 1 addition & 1 deletion examples/headers_order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ async fn main() -> Result<(), rquest::Error> {
let url = "https://tls.peet.ws/api/all".parse().expect("Invalid url");

// Set a cookie
client.set_cookies(
client.as_ref().set_cookies(
&url,
vec![HeaderValue::from_static("foo=bar; Domain=tls.peet.ws")],
);
Expand Down
2 changes: 1 addition & 1 deletion examples/impersonate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use rquest::Impersonate;
async fn main() -> Result<(), rquest::Error> {
// Build a client to impersonate Firefox128
let client = rquest::Client::builder()
.impersonate(Impersonate::Firefox128)
.impersonate(Impersonate::Chrome131)
.build()?;

// Use the API you're already familiar with
Expand Down
9 changes: 6 additions & 3 deletions examples/set_cookie_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,20 @@ async fn main() -> Result<(), rquest::Error> {
env_logger::init_from_env(env_logger::Env::default().default_filter_or("debug"));

// Build a client to impersonate Chrome131
let mut client = rquest::Client::builder()
let client = rquest::Client::builder()
.impersonate(Impersonate::Chrome131)
.build()?;

let url = "https://tls.peet.ws/api/all".parse().expect("Invalid url");

// Set cookie provider
client.as_mut().cookie_provider(Arc::new(Jar::default()));
client
.as_mut()
.cookie_provider(Arc::new(Jar::default()))
.apply()?;

// Set a cookie
client.set_cookies(
client.as_ref().set_cookies(
&url,
vec![HeaderValue::from_static("foo=bar; Domain=tls.peet.ws")],
);
Expand Down
4 changes: 2 additions & 2 deletions examples/set_cookies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ async fn main() -> Result<(), rquest::Error> {
let url = "https://tls.peet.ws/api/all".parse().expect("Invalid url");

// Set a cookie
client.set_cookies(
client.as_ref().set_cookies(
&url,
vec![HeaderValue::from_static("foo=bar; Domain=tls.peet.ws")],
);

// Get cookies
let cookies = client.get_cookies(&url);
let cookies = client.as_ref().get_cookies(&url);
println!("{:?}", cookies);

// Use the API you're already familiar with
Expand Down
11 changes: 6 additions & 5 deletions examples/set_headers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use rquest::Impersonate;
#[tokio::main]
async fn main() -> Result<(), rquest::Error> {
// Build a client to impersonate Chrome131
let mut client = rquest::Client::builder()
let client = rquest::Client::builder()
.impersonate(Impersonate::Chrome131)
.build()?;

Expand All @@ -13,14 +13,15 @@ async fn main() -> Result<(), rquest::Error> {
println!("{}", resp.text().await?);

// Set a header
client
.as_mut()
.headers()
.insert(header::ACCEPT, HeaderValue::from_static("application/json"));
client.as_mut().headers(update_headers).apply()?;

// Use the API you're already familiar with
let resp = client.get("https://tls.peet.ws/api/all").send().await?;
println!("{}", resp.text().await?);

Ok(())
}

fn update_headers(headers: &mut http::HeaderMap) {
headers.insert(header::ACCEPT, HeaderValue::from_static("application/json"));
}
4 changes: 2 additions & 2 deletions examples/set_impersonate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ use rquest::{Client, Impersonate};
#[tokio::main]
async fn main() -> Result<(), rquest::Error> {
// Build a client to impersonate Chrome131
let mut client = Client::builder()
let client = Client::builder()
.impersonate(Impersonate::Chrome131)
.build()?;

// Change the impersonate to Safari18
client.as_mut().impersonate(Impersonate::Safari18);
client.as_mut().impersonate(Impersonate::Safari18).apply()?;
let resp = client.get("https://tls.peet.ws/api/all").send().await?;
println!("{}", resp.text().await?);

Expand Down
4 changes: 2 additions & 2 deletions examples/set_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ use rquest::{Client, Impersonate};
#[tokio::main]
async fn main() -> Result<(), rquest::Error> {
// Build a client to impersonate Chrome130
let mut client = Client::builder()
let client = Client::builder()
.impersonate(Impersonate::Chrome130)
.interface("eth0")
.build()?;

// Set the interface to eth1
client.as_mut().interface("eth1");
client.as_mut().interface("eth1").apply()?;

// Use the API you're already familiar with
let resp = client.get("https://api.ip.sb/ip").send().await?;
Expand Down
5 changes: 3 additions & 2 deletions examples/set_local_address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::net::IpAddr;
#[tokio::main]
async fn main() -> Result<(), rquest::Error> {
// Build a client to impersonate Chrome130
let mut client = rquest::Client::builder()
let client = rquest::Client::builder()
.impersonate(Impersonate::Chrome130)
.build()?;

Expand All @@ -15,7 +15,8 @@ async fn main() -> Result<(), rquest::Error> {
// Set the local address to `172.200.10.2`
client
.as_mut()
.local_address(IpAddr::from([172, 200, 10, 2]));
.local_address(IpAddr::from([172, 200, 10, 2]))
.apply()?;

// Use the API you're already familiar with
let resp = client.get("https://api.ip.sb/ip").send().await?;
Expand Down
40 changes: 9 additions & 31 deletions examples/set_proxies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,42 +7,20 @@ async fn main() -> Result<(), rquest::Error> {
.init();

// Build a client to impersonate Chrome130
let mut client = Client::builder()
let client = Client::builder()
.impersonate(Impersonate::Chrome130)
.no_proxy()
.build()?;

// Use the API you're already familiar with
// Set the proxies
let proxy = rquest::Proxy::all("socks5h://127.0.0.1:6153")?;
client.as_mut().proxies(vec![proxy]).apply()?;
let resp = client.get("https://api.ip.sb/ip").send().await?;
println!("{}", resp.text().await?);

// Set the proxies
{
let proxy = rquest::Proxy::all("socks5h://abc:123@127.0.0.1:6153")?;
client.as_mut().proxies(vec![proxy]);

let resp = client.get("https://api.ip.sb/ip").send().await?;
println!("{}", resp.text().await?);

let resp = client.get("https://api.ip.sb/ip").send().await?;
println!("{}", resp.text().await?);

let resp = client.get("https://api.ip.sb/ip").send().await?;
println!("{}", resp.text().await?);
}

// Clear the proxies
{
client.as_mut().proxies(None);

let resp = client.get("https://api.ip.sb/ip").send().await?;
println!("{}", resp.text().await?);

let resp = client.get("https://api.ip.sb/ip").send().await?;
println!("{}", resp.text().await?);

let resp = client.get("https://api.ip.sb/ip").send().await?;
println!("{}", resp.text().await?);
}

// Unset the proxies
client.as_mut().unset_proxies().apply()?;
let resp = client.get("https://api.ip.sb/ip").send().await?;
println!("{}", resp.text().await?);
Ok(())
}
28 changes: 0 additions & 28 deletions examples/set_redirect.rs

This file was deleted.

Loading

0 comments on commit e6397d6

Please sign in to comment.