Skip to content

Commit

Permalink
feat(tls): Add preconfigured TLS settings
Browse files Browse the repository at this point in the history
  • Loading branch information
0x676e67 committed Aug 13, 2024
1 parent 87f078f commit 1df1812
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 14 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ An fast asynchronous Rust `Http`/`WebSocket` Client with `TLS`/`JA3`/`JA4`/`HTTP

- `Async` or `blocking` Clients
- `Plain`, `JSON`, `urlencoded`, `multipart` bodies
- Customizable `headers` order
- `Headers` order
- Customizable `redirect` policy
- Cookie Store
- `HTTP`/`HTTPS`/`SOCKS5` Proxies
- `HTTPS`/`WebSocket` via [BoringSSL](https://github.com/cloudflare/boring)
- `JA3`/`JA4`/`HTTP2` fingerprint
- Preconfigured `TLS`/`HTTP2` settings
- Impersonate `Chrome`/`Safari`/`Edge`/`OkHttp`

Additional learning resources include:
Expand Down
10 changes: 8 additions & 2 deletions examples/pre_configured_tls.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::error::Error;

use boring::ssl::{SslConnector, SslMethod};
use http::HeaderValue;
use hyper::{PseudoOrder, SettingsOrder, StreamDependency, StreamId};
use rquest::{
tls::{Http2FrameSettings, TlsExtensionSettings, TlsSettings, Version},
Expand Down Expand Up @@ -42,9 +43,14 @@ async fn main() -> Result<(), Box<dyn Error>> {
},
};

// Build a client to mimic Edge127
// Build a client with pre-configured TLS settings
let client = rquest::Client::builder()
.use_preconfigured_tls(|_headers| settings)
.use_preconfigured_tls(
|| settings,
|headers| {
headers.insert("user-agent", HeaderValue::from_static("rquest"));
},
)
.enable_ech_grease()
.permute_extensions()
.build()?;
Expand Down
10 changes: 4 additions & 6 deletions src/async_impl/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,14 +280,12 @@ impl ClientBuilder {

/// Use the preconfigured TLS settings.
#[cfg(feature = "boring-tls")]
#[cfg(feature = "boring-tls")]
pub fn use_preconfigured_tls<F>(self, f: F) -> ClientBuilder
pub fn use_preconfigured_tls<F1, F2>(self, f1: F1, f2: F2) -> ClientBuilder
where
F: FnOnce(&mut HeaderMap) -> TlsSettings,
F1: FnOnce() -> TlsSettings,
F2: FnOnce(&mut HeaderMap),
{
let mut headers = self.config.headers.clone();
let settings = f(&mut headers);
self.apply_tls_settings(settings, |_| {})
self.apply_tls_settings(f1(), f2)
}

/// Apply the given TLS settings and header function.
Expand Down
20 changes: 15 additions & 5 deletions src/blocking/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ use super::request::{Request, RequestBuilder};
use super::response::Response;
use super::wait;
#[cfg(feature = "boring-tls")]
use crate::tls;
#[cfg(feature = "boring-tls")]
use crate::tls::Impersonate;
use crate::tls::{Impersonate, TlsSettings, Version};
use crate::{async_impl, header, redirect, IntoUrl, Method, Proxy};
#[cfg(feature = "boring-tls")]
use header::HeaderMap;
#[cfg(feature = "http2")]
use hyper::{PseudoOrder, SettingsOrder, StreamDependency};

Expand Down Expand Up @@ -107,6 +107,16 @@ impl ClientBuilder {
self.with_inner(move |inner| inner.impersonate(ver))
}

/// Use the preconfigured TLS settings.
#[cfg(feature = "boring-tls")]
pub fn use_preconfigured_tls<F1, F2>(self, f1: F1, f2: F2) -> ClientBuilder
where
F1: FnOnce() -> TlsSettings,
F2: FnOnce(&mut HeaderMap),
{
self.with_inner(move |inner| inner.use_preconfigured_tls(f))
}

/// Enable Encrypted Client Hello (Secure SNI)
#[cfg_attr(docsrs, doc(cfg(feature = "boring-tls")))]
pub fn enable_ech_grease(self) -> ClientBuilder {
Expand Down Expand Up @@ -699,7 +709,7 @@ impl ClientBuilder {
///
/// feature to be enabled.
#[cfg(feature = "boring-tls")]
pub fn min_tls_version(self, version: tls::Version) -> ClientBuilder {
pub fn min_tls_version(self, version: Version) -> ClientBuilder {
self.with_inner(|inner| inner.min_tls_version(version))
}

Expand All @@ -718,7 +728,7 @@ impl ClientBuilder {
///
/// feature to be enabled.
#[cfg(feature = "boring-tls")]
pub fn max_tls_version(self, version: tls::Version) -> ClientBuilder {
pub fn max_tls_version(self, version: Version) -> ClientBuilder {
self.with_inner(|inner| inner.max_tls_version(version))
}

Expand Down

0 comments on commit 1df1812

Please sign in to comment.