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

Commit

Permalink
fix(connector): Fix TLS session failure when changing address
Browse files Browse the repository at this point in the history
  • Loading branch information
0x676e67 committed Jul 27, 2024
1 parent f066c88 commit 05a9e54
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ __boring = [
"__tls",
"boring-sys",
"foreign-types",
"antidote",
]

__impersonate = ["__boring", "__browser_common"]
Expand Down Expand Up @@ -120,6 +121,7 @@ boring-sys = { package = "boring-sys-imp", version = "2", optional = true }
hyper-boring = { package = "hyper-boring-imp", version = "2", optional = true }
tokio-boring = { package = "tokio-boring-imp", version = "2", optional = true }
foreign-types = { version = "0.5.0", optional = true }
antidote = { version = "1.0.0", optional = true }

## cookies
cookie_crate = { version = "0.18", package = "cookie", optional = true }
Expand Down
29 changes: 16 additions & 13 deletions src/impersonate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,32 @@ pub mod profile;
mod safari;

use crate::connect::HttpConnector;
use antidote::Mutex;
use boring::{
error::ErrorStack,
ssl::{ConnectConfiguration, SslConnectorBuilder},
};
use http::HeaderMap;
use hyper_boring::HttpsConnector;
use hyper_boring::{HttpsConnector, SessionCache};
use profile::ClientProfile;
pub use profile::Impersonate;
use std::sync::Arc;
use tokio::sync::OnceCell;

/// A wrapper around a `SslConnectorBuilder` that allows for additional settings.
#[derive(Clone)]
pub struct BoringTlsConnector {
/// The inner `SslConnectorBuilder` function.
inner: Arc<dyn Fn() -> SslConnectorBuilder + Send + Sync>,
/// The cached `HttpsConnector`.
connector: Arc<OnceCell<HttpsConnector<HttpConnector>>>,
session: Arc<Mutex<SessionCache>>,
}

impl BoringTlsConnector {
/// Create a new `BoringTlsConnector` with the given function.
pub fn new(inner: Arc<dyn Fn() -> SslConnectorBuilder + Send + Sync>) -> BoringTlsConnector {
Self {
inner,
connector: Arc::new(OnceCell::new()),
session: Arc::new(Mutex::new(SessionCache::new())),
}
}

Expand All @@ -43,20 +43,17 @@ impl BoringTlsConnector {
let mut builder = (self.inner)();
alpn_and_cert_settings(context, &mut builder);

let http = match context.impersonate {
let psk_extension = match context.impersonate {
Impersonate::Chrome117
| Impersonate::Chrome120
| Impersonate::Chrome123
| Impersonate::Chrome124
| Impersonate::Chrome126 => self
.connector
.get_or_try_init(|| async { Self::create_connector(context, http, builder) })
.await?
.clone(),
_ => Self::create_connector(context, http, builder)?,
| Impersonate::Chrome126
| Impersonate::Edge122 => true,
_ => false,
};

Ok(http)
self.create_connector(context, http, builder, psk_extension)
}

/// Create a new `SslConnector` with the settings from the `ImpersonateContext`.
Expand All @@ -76,11 +73,17 @@ impl BoringTlsConnector {

/// Create a new `HttpsConnector` with the settings from the `ImpersonateContext`.
fn create_connector(
&self,
context: &ImpersonateContext,
http: HttpConnector,
builder: SslConnectorBuilder,
psk: bool,
) -> Result<HttpsConnector<HttpConnector>, ErrorStack> {
let mut http = HttpsConnector::with_connector(http, builder)?;
let mut http = if psk {
HttpsConnector::with_connecotr_and_cache(http, builder, self.session.clone())?
} else {
HttpsConnector::with_connector(http, builder)?
};
let context = context.clone();
http.set_callback(move |conf, _| Ok(add_application_settings(conf, &context)));
Ok(http)
Expand Down

0 comments on commit 05a9e54

Please sign in to comment.