Skip to content
This repository has been archived by the owner on Jan 16, 2025. It is now read-only.

feat(impersonate): Optimize reuse of impersonate configuration #61

Merged
merged 3 commits into from
Jul 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -199,12 +199,17 @@ path = "examples/simple.rs"
[[example]]
name = "impersonate"
path = "examples/impersonate.rs"
required-features = ["blocking", "socks", "cookies", "zstd", "hickory-dns"]
required-features = ["blocking", "socks", "zstd", "hickory-dns"]

[[example]]
name = "psk_impersonate"
path = "examples/psk_impersonate.rs"
required-features = ["blocking", "socks", "zstd", "hickory-dns"]

[[example]]
name = "set_proxies"
path = "examples/set_proxies.rs"
required-features = ["blocking", "socks", "cookies", "zstd", "hickory-dns", "socks"]
required-features = ["blocking", "socks", "zstd", "hickory-dns", "socks"]

[[example]]
name = "set_local_address"
Expand Down
1 change: 0 additions & 1 deletion examples/impersonate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ async fn main() -> Result<(), Box<dyn Error>> {
.impersonate(Impersonate::Edge127)
.enable_ech_grease()
.permute_extensions()
.cookie_store(true)
.build()?;

// Use the API you're already familiar with
Expand Down
22 changes: 22 additions & 0 deletions examples/psk_impersonate.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use rquest;
use rquest::impersonate::Impersonate;
use std::error::Error;

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
// Build a client to mimic Edge127
let client = rquest::Client::builder()
.impersonate(Impersonate::Edge127)
.enable_ech_grease()
.permute_extensions()
.build()?;

// Use the API you're already familiar with
let _ = client.get("https://tls.peet.ws/api/all").send().await?;

// Now, let's impersonate a PSK
let resp = client.get("https://tls.peet.ws/api/all").send().await?;
println!("{}", resp.text().await?);

Ok(())
}
1 change: 0 additions & 1 deletion examples/set_proxies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
.impersonate(Impersonate::Chrome126)
.enable_ech_grease()
.permute_extensions()
.cookie_store(true)
.build()?;

let resp = client.get("https://api.ip.sb/ip").send().await?;
Expand Down
8 changes: 4 additions & 4 deletions src/async_impl/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ struct Config {
#[cfg(feature = "__tls")]
tls_info: bool,
#[cfg(feature = "__tls")]
tls: TlsBackend,
tls: Option<TlsBackend>,
http_version_pref: HttpVersionPref,
http09_responses: bool,
http1_title_case_headers: bool,
Expand Down Expand Up @@ -175,7 +175,7 @@ impl ClientBuilder {
#[cfg(feature = "__tls")]
tls_info: false,
#[cfg(feature = "__tls")]
tls: TlsBackend::default(),
tls: None,
http_version_pref: HttpVersionPref::All,
http09_responses: false,
http1_title_case_headers: false,
Expand Down Expand Up @@ -291,7 +291,7 @@ impl ClientBuilder {
http.set_connect_timeout(config.connect_timeout);

#[cfg(feature = "__tls")]
match config.tls {
match config.tls.unwrap_or_default() {
#[cfg(feature = "__boring")]
TlsBackend::BoringTls(tls) => Connector::new_boring_tls(
http,
Expand Down Expand Up @@ -1178,7 +1178,7 @@ impl ClientBuilder {
mut self,
connector: crate::impersonate::BoringTlsConnector,
) -> ClientBuilder {
self.config.tls = TlsBackend::BoringTls(connector);
self.config.tls = Some(TlsBackend::BoringTls(connector));
self
}

Expand Down
41 changes: 12 additions & 29 deletions src/impersonate/chrome/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use boring::ssl::{
CertCompressionAlgorithm, Error, SslConnector, SslConnectorBuilder, SslCurve, SslMethod,
SslVersion,
use boring::{
error::ErrorStack,
ssl::{CertCompressionAlgorithm, SslConnector, SslConnectorBuilder, SslMethod, SslVersion},
};

pub mod v100;
pub mod v101;
pub mod v104;
Expand Down Expand Up @@ -50,42 +49,26 @@ const CIPHER_LIST: [&str; 15] = [
"TLS_RSA_WITH_AES_256_CBC_SHA",
];

fn ssl_builder() -> SslConnectorBuilder {
let mut builder = SslConnector::builder(SslMethod::tls_client()).unwrap();
fn ssl_builder() -> Result<SslConnectorBuilder, ErrorStack> {
let mut builder = SslConnector::builder(SslMethod::tls_client())?;

builder.set_default_verify_paths().unwrap();
builder.set_default_verify_paths()?;

builder.set_grease_enabled(true);

builder.enable_ocsp_stapling();

builder.set_cipher_list(&CIPHER_LIST.join(":")).unwrap();
builder.set_cipher_list(&CIPHER_LIST.join(":"))?;

builder.set_sigalgs_list(&SIGALGS_LIST.join(":")).unwrap();
builder.set_sigalgs_list(&SIGALGS_LIST.join(":"))?;

builder.enable_signed_cert_timestamps();

builder
.add_cert_compression_alg(CertCompressionAlgorithm::Brotli)
.unwrap();

builder
.set_min_proto_version(Some(SslVersion::TLS1_2))
.unwrap();
builder.add_cert_compression_alg(CertCompressionAlgorithm::Brotli)?;

builder
.set_max_proto_version(Some(SslVersion::TLS1_3))
.unwrap();
builder.set_min_proto_version(Some(SslVersion::TLS1_2))?;

builder
}
builder.set_max_proto_version(Some(SslVersion::TLS1_3))?;

fn configure_curves_ssl(builder: &mut SslConnectorBuilder) -> Result<(), Error> {
builder.set_curves(&[
SslCurve::X25519_KYBER768_DRAFT00,
SslCurve::X25519,
SslCurve::SECP256R1,
SslCurve::SECP384R1,
])?;
Ok(())
Ok(builder)
}
4 changes: 1 addition & 3 deletions src/impersonate/chrome/v100.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::sync::Arc;

use super::ssl_builder;
use crate::impersonate::{BoringTlsConnector, Http2Data, ImpersonateSettings};
use http::{
Expand All @@ -11,7 +9,7 @@ use http::{

pub(crate) fn get_settings(headers: HeaderMap) -> ImpersonateSettings {
ImpersonateSettings {
tls_connector: BoringTlsConnector::new(Arc::new(ssl_builder)),
tls_connector: BoringTlsConnector::new(ssl_builder),
http2: Http2Data {
initial_stream_window_size: Some(6291456),
initial_connection_window_size: Some(15728640),
Expand Down
4 changes: 1 addition & 3 deletions src/impersonate/chrome/v101.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::sync::Arc;

use super::ssl_builder;
use crate::impersonate::{BoringTlsConnector, Http2Data, ImpersonateSettings};
use http::{
Expand All @@ -11,7 +9,7 @@ use http::{

pub(crate) fn get_settings(headers: HeaderMap) -> ImpersonateSettings {
ImpersonateSettings {
tls_connector: BoringTlsConnector::new(Arc::new(ssl_builder)),
tls_connector: BoringTlsConnector::new(ssl_builder),
http2: Http2Data {
initial_stream_window_size: Some(6291456),
initial_connection_window_size: Some(15728640),
Expand Down
4 changes: 1 addition & 3 deletions src/impersonate/chrome/v104.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::sync::Arc;

use super::ssl_builder;
use crate::impersonate::{BoringTlsConnector, Http2Data, ImpersonateSettings};
use http::{
Expand All @@ -11,7 +9,7 @@ use http::{

pub(crate) fn get_settings(headers: HeaderMap) -> ImpersonateSettings {
ImpersonateSettings {
tls_connector: BoringTlsConnector::new(Arc::new(ssl_builder)),
tls_connector: BoringTlsConnector::new(ssl_builder),
http2: Http2Data {
initial_stream_window_size: Some(6291456),
initial_connection_window_size: Some(15728640),
Expand Down
4 changes: 1 addition & 3 deletions src/impersonate/chrome/v105.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::sync::Arc;

use super::ssl_builder;
use crate::impersonate::{BoringTlsConnector, Http2Data, ImpersonateSettings};
use http::{
Expand All @@ -11,7 +9,7 @@ use http::{

pub(crate) fn get_settings(headers: HeaderMap) -> ImpersonateSettings {
ImpersonateSettings {
tls_connector: BoringTlsConnector::new(Arc::new(ssl_builder)),
tls_connector: BoringTlsConnector::new(ssl_builder),
http2: Http2Data {
initial_stream_window_size: Some(6291456),
initial_connection_window_size: Some(15728640),
Expand Down
4 changes: 1 addition & 3 deletions src/impersonate/chrome/v106.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::sync::Arc;

use super::ssl_builder;
use crate::impersonate::{BoringTlsConnector, Http2Data, ImpersonateSettings};
use http::{
Expand All @@ -11,7 +9,7 @@ use http::{

pub(crate) fn get_settings(headers: HeaderMap) -> ImpersonateSettings {
ImpersonateSettings {
tls_connector: BoringTlsConnector::new(Arc::new(ssl_builder)),
tls_connector: BoringTlsConnector::new(ssl_builder),
http2: Http2Data {
initial_stream_window_size: Some(6291456),
initial_connection_window_size: Some(15728640),
Expand Down
3 changes: 1 addition & 2 deletions src/impersonate/chrome/v107.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ use http::{
},
HeaderMap, HeaderValue,
};
use std::sync::Arc;

pub(crate) fn get_settings(headers: HeaderMap) -> ImpersonateSettings {
ImpersonateSettings {
tls_connector: BoringTlsConnector::new(Arc::new(ssl_builder)),
tls_connector: BoringTlsConnector::new(ssl_builder),
http2: Http2Data {
initial_stream_window_size: Some(6291456),
initial_connection_window_size: Some(15728640),
Expand Down
3 changes: 1 addition & 2 deletions src/impersonate/chrome/v108.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ use http::{
},
HeaderMap, HeaderValue,
};
use std::sync::Arc;

pub(crate) fn get_settings(headers: HeaderMap) -> ImpersonateSettings {
ImpersonateSettings {
tls_connector: BoringTlsConnector::new(Arc::new(ssl_builder)),
tls_connector: BoringTlsConnector::new(ssl_builder),
http2: Http2Data {
initial_stream_window_size: Some(6291456),
initial_connection_window_size: Some(15728640),
Expand Down
3 changes: 1 addition & 2 deletions src/impersonate/chrome/v109.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ use http::{
},
HeaderMap, HeaderValue,
};
use std::sync::Arc;

pub(crate) fn get_settings(headers: HeaderMap) -> ImpersonateSettings {
ImpersonateSettings {
tls_connector: BoringTlsConnector::new(Arc::new(ssl_builder)),
tls_connector: BoringTlsConnector::new(ssl_builder),
http2: Http2Data {
initial_stream_window_size: Some(6291456),
initial_connection_window_size: Some(15728640),
Expand Down
3 changes: 1 addition & 2 deletions src/impersonate/chrome/v114.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ use http::{
},
HeaderMap, HeaderValue,
};
use std::sync::Arc;

pub(crate) fn get_settings(headers: HeaderMap) -> ImpersonateSettings {
ImpersonateSettings {
tls_connector: BoringTlsConnector::new(Arc::new(ssl_builder)),
tls_connector: BoringTlsConnector::new(ssl_builder),
http2: Http2Data {
initial_stream_window_size: Some(6291456),
initial_connection_window_size: Some(15728640),
Expand Down
3 changes: 1 addition & 2 deletions src/impersonate/chrome/v116.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ use http::{
header::{ACCEPT, ACCEPT_ENCODING, ACCEPT_LANGUAGE, UPGRADE_INSECURE_REQUESTS, USER_AGENT},
HeaderMap, HeaderValue,
};
use std::sync::Arc;

pub(crate) fn get_settings(headers: HeaderMap) -> ImpersonateSettings {
ImpersonateSettings {
tls_connector: BoringTlsConnector::new(Arc::new(ssl_builder)),
tls_connector: BoringTlsConnector::new(ssl_builder),
http2: Http2Data {
initial_stream_window_size: Some(6291456),
initial_connection_window_size: Some(15728640),
Expand Down
3 changes: 1 addition & 2 deletions src/impersonate/chrome/v117.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ use http::{
header::{ACCEPT, ACCEPT_ENCODING, ACCEPT_LANGUAGE, UPGRADE_INSECURE_REQUESTS, USER_AGENT},
HeaderMap, HeaderValue,
};
use std::sync::Arc;

pub(crate) fn get_settings(headers: HeaderMap) -> ImpersonateSettings {
ImpersonateSettings {
tls_connector: BoringTlsConnector::new(Arc::new(ssl_builder)),
tls_connector: BoringTlsConnector::new(ssl_builder),
http2: Http2Data {
initial_stream_window_size: Some(6291456),
initial_connection_window_size: Some(15728640),
Expand Down
3 changes: 1 addition & 2 deletions src/impersonate/chrome/v118.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ use http::{
},
HeaderMap, HeaderValue,
};
use std::sync::Arc;

pub(crate) fn get_settings(headers: HeaderMap) -> ImpersonateSettings {
ImpersonateSettings {
tls_connector: BoringTlsConnector::new(Arc::new(ssl_builder)),
tls_connector: BoringTlsConnector::new(ssl_builder),
http2: Http2Data {
initial_stream_window_size: Some(6291456),
initial_connection_window_size: Some(15728640),
Expand Down
3 changes: 1 addition & 2 deletions src/impersonate/chrome/v119.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ use http::{
},
HeaderMap, HeaderValue,
};
use std::sync::Arc;

pub(crate) fn get_settings(headers: HeaderMap) -> ImpersonateSettings {
ImpersonateSettings {
tls_connector: BoringTlsConnector::new(Arc::new(ssl_builder)),
tls_connector: BoringTlsConnector::new(ssl_builder),
http2: Http2Data {
initial_stream_window_size: Some(6291456),
initial_connection_window_size: Some(15728640),
Expand Down
3 changes: 1 addition & 2 deletions src/impersonate/chrome/v120.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ use http::{
},
HeaderMap, HeaderValue,
};
use std::sync::Arc;

pub(crate) fn get_settings(headers: HeaderMap) -> ImpersonateSettings {
ImpersonateSettings {
tls_connector: BoringTlsConnector::new(Arc::new(ssl_builder)),
tls_connector: BoringTlsConnector::new(ssl_builder),
http2: Http2Data {
initial_stream_window_size: Some(6291456),
initial_connection_window_size: Some(15728640),
Expand Down
3 changes: 1 addition & 2 deletions src/impersonate/chrome/v123.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ use http::{
header::{ACCEPT, ACCEPT_ENCODING, ACCEPT_LANGUAGE, UPGRADE_INSECURE_REQUESTS, USER_AGENT},
HeaderMap, HeaderValue,
};
use std::sync::Arc;

pub(crate) fn get_settings(headers: HeaderMap) -> ImpersonateSettings {
ImpersonateSettings {
tls_connector: BoringTlsConnector::new(Arc::new(ssl_builder)),
tls_connector: BoringTlsConnector::new(ssl_builder),
http2: Http2Data {
initial_stream_window_size: Some(6291456),
initial_connection_window_size: Some(15728640),
Expand Down
14 changes: 7 additions & 7 deletions src/impersonate/chrome/v124.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
use super::{configure_curves_ssl, ssl_builder};
use super::ssl_builder;
use crate::impersonate::curves::configure_chrome_new_curves;
use crate::impersonate::{BoringTlsConnector, Http2Data, ImpersonateSettings};
0x676e67 marked this conversation as resolved.
Show resolved Hide resolved
use http::{
header::{ACCEPT, ACCEPT_ENCODING, ACCEPT_LANGUAGE, UPGRADE_INSECURE_REQUESTS, USER_AGENT},
HeaderMap, HeaderValue,
};
use std::sync::Arc;

pub(crate) fn get_settings(headers: HeaderMap) -> ImpersonateSettings {
ImpersonateSettings {
tls_connector: BoringTlsConnector::new(Arc::new(|| {
let mut builder = ssl_builder();
configure_curves_ssl(&mut builder).expect("Failed to configure curves SSL");
builder
})),
tls_connector: BoringTlsConnector::new(|| {
let mut builder = ssl_builder()?;
configure_chrome_new_curves(&mut builder)?;
Ok(builder)
}),
http2: Http2Data {
initial_stream_window_size: Some(6291456),
initial_connection_window_size: Some(15728640),
Expand Down
Loading
Loading