use crate::choice::random_item;
use crate::fingerprint::ja3::{compute_ja3, compute_ja4, ClientHelloFields};
use crate::fingerprint::StealthProfile;
use crate::rotation::named_profile;
#[cfg(feature = "http")]
use scanclient::tls_impersonate::ImpersonateProfile;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct TlsProfile {
pub name: &'static str,
pub tls_version: u16,
pub cipher_suites: &'static [u16],
pub extensions: &'static [u16],
pub elliptic_curves: &'static [u16],
pub ec_point_formats: &'static [u8],
pub alpn_protocols: &'static [&'static str],
pub expected_ja3: &'static str,
pub signature_algorithms: &'static [u16],
pub include_grease: bool,
pub supported_versions: &'static [u16],
}
mod catalog;
use catalog::{ALL_PROFILES, CHROME_120, CHROME_146, FIREFOX_150, SAFARI_18, SAFARI_IOS_18};
const GREASE_VALUES: &[u16] = &[
0x0a0a, 0x1a1a, 0x2a2a, 0x3a3a, 0x4a4a, 0x5a5a, 0x6a6a, 0x7a7a, 0x8a8a, 0x9a9a, 0xaaaa, 0xbaba,
0xcaca, 0xdada, 0xeaea, 0xfafa,
];
fn random_grease() -> u16 {
random_item(GREASE_VALUES)
.copied()
.unwrap_or(GREASE_VALUES[0])
}
#[cfg(feature = "http")]
const CHROME_LEGACY_IMPERSONATE_PROFILES: &[ImpersonateProfile] = &[ImpersonateProfile::Chrome120];
#[cfg(feature = "http")]
const CHROMIUM_IMPERSONATE_PROFILES: &[ImpersonateProfile] =
&[ImpersonateProfile::Chrome131, ImpersonateProfile::Chrome120];
#[cfg(feature = "http")]
const EDGE_IMPERSONATE_PROFILES: &[ImpersonateProfile] = &[ImpersonateProfile::Edge131];
#[cfg(feature = "http")]
const FIREFOX_IMPERSONATE_PROFILES: &[ImpersonateProfile] = &[ImpersonateProfile::Firefox133];
#[cfg(feature = "http")]
const SAFARI_IMPERSONATE_PROFILES: &[ImpersonateProfile] = &[
ImpersonateProfile::Safari18,
ImpersonateProfile::SafariIpad18,
ImpersonateProfile::Safari17_5,
];
#[cfg(feature = "http")]
const CHROMIUM_ALT_IMPERSONATE_PROFILES: &[ImpersonateProfile] = &[
ImpersonateProfile::Chrome131,
ImpersonateProfile::Chrome120,
ImpersonateProfile::Edge131,
];
#[cfg(feature = "http")]
const NO_IMPERSONATE_PROFILES: &[ImpersonateProfile] = &[];
#[must_use]
pub fn profiles() -> &'static [TlsProfile] {
ALL_PROFILES
}
#[must_use]
pub const fn profile_for_stealth_profile(profile: StealthProfile) -> Option<&'static TlsProfile> {
match profile {
StealthProfile::ChromeWindowsStable
| StealthProfile::ChromeLinux
| StealthProfile::ChromeAndroid
| StealthProfile::ChromeMacStable
| StealthProfile::EdgeWindowsStable
| StealthProfile::BraveWindows
| StealthProfile::OperaWindows
| StealthProfile::SamsungInternetAndroid => Some(&CHROME_146),
StealthProfile::ChromeWindowsLegacy96 => Some(&CHROME_120),
StealthProfile::FirefoxLinux
| StealthProfile::FirefoxWindows
| StealthProfile::FirefoxMacStable => Some(&FIREFOX_150),
StealthProfile::SafariIphone | StealthProfile::SafariIpad => Some(&SAFARI_IOS_18),
StealthProfile::SafariMacStable => Some(&SAFARI_18),
StealthProfile::Ie11Windows => None,
_ => None,
}
}
#[cfg(feature = "http")]
#[must_use]
pub const fn default_impersonate_profile_for_stealth_profile(
profile: StealthProfile,
) -> ImpersonateProfile {
match profile {
StealthProfile::ChromeWindowsLegacy96 => ImpersonateProfile::Chrome120,
StealthProfile::Ie11Windows => ImpersonateProfile::Chrome120,
StealthProfile::EdgeWindowsStable => ImpersonateProfile::Edge131,
StealthProfile::FirefoxLinux
| StealthProfile::FirefoxWindows
| StealthProfile::FirefoxMacStable => ImpersonateProfile::Firefox133,
StealthProfile::SafariMacStable => ImpersonateProfile::Safari18,
StealthProfile::SafariIphone | StealthProfile::SafariIpad => {
ImpersonateProfile::SafariIpad18
}
StealthProfile::ChromeWindowsStable
| StealthProfile::ChromeMacStable
| StealthProfile::ChromeAndroid
| StealthProfile::ChromeLinux
| StealthProfile::BraveWindows
| StealthProfile::OperaWindows
| StealthProfile::SamsungInternetAndroid => ImpersonateProfile::Chrome131,
_ => ImpersonateProfile::Chrome131,
}
}
#[cfg(feature = "http")]
#[must_use]
pub const fn compatible_impersonate_profiles_for_stealth_profile(
profile: StealthProfile,
) -> &'static [ImpersonateProfile] {
match profile {
StealthProfile::ChromeWindowsLegacy96 => CHROME_LEGACY_IMPERSONATE_PROFILES,
StealthProfile::Ie11Windows => NO_IMPERSONATE_PROFILES,
StealthProfile::ChromeWindowsStable
| StealthProfile::ChromeMacStable
| StealthProfile::ChromeAndroid
| StealthProfile::ChromeLinux
| StealthProfile::SamsungInternetAndroid => CHROMIUM_IMPERSONATE_PROFILES,
StealthProfile::EdgeWindowsStable => EDGE_IMPERSONATE_PROFILES,
StealthProfile::FirefoxLinux
| StealthProfile::FirefoxWindows
| StealthProfile::FirefoxMacStable => FIREFOX_IMPERSONATE_PROFILES,
StealthProfile::SafariIphone
| StealthProfile::SafariIpad
| StealthProfile::SafariMacStable => SAFARI_IMPERSONATE_PROFILES,
StealthProfile::BraveWindows | StealthProfile::OperaWindows => {
CHROMIUM_ALT_IMPERSONATE_PROFILES
}
_ => NO_IMPERSONATE_PROFILES,
}
}
#[cfg(feature = "http")]
#[must_use]
pub fn impersonate_profile_matches_stealth_profile(
browser: StealthProfile,
tls: ImpersonateProfile,
) -> bool {
compatible_impersonate_profiles_for_stealth_profile(browser).contains(&tls)
}
#[cfg(feature = "http")]
#[must_use]
pub const fn impersonate_profile_family(profile: ImpersonateProfile) -> Option<&'static str> {
match profile {
ImpersonateProfile::Chrome120
| ImpersonateProfile::Chrome131
| ImpersonateProfile::Edge131 => Some("chrome"),
ImpersonateProfile::Firefox133 => Some("firefox"),
ImpersonateProfile::Safari17_5
| ImpersonateProfile::Safari18
| ImpersonateProfile::SafariIpad18 => Some("safari"),
_ => None,
}
}
#[must_use]
pub fn profile_for(browser: &str) -> Option<&'static TlsProfile> {
let browser = browser.trim();
if browser.is_empty() {
return None;
}
if let Some(profile) = named_profile(browser) {
return profile_for_stealth_profile(profile);
}
let lower = browser.to_ascii_lowercase();
ALL_PROFILES
.iter()
.find(|profile| profile.name.to_ascii_lowercase().contains(&lower))
}
#[must_use]
pub fn random_profile() -> Option<&'static TlsProfile> {
random_item(ALL_PROFILES)
}
#[must_use]
pub fn build_cipher_suites(profile: &TlsProfile) -> Vec<u16> {
let mut suites = Vec::with_capacity(profile.cipher_suites.len() + 1);
if profile.include_grease {
suites.push(random_grease());
}
suites.extend_from_slice(profile.cipher_suites);
suites
}
#[must_use]
pub fn build_extensions(profile: &TlsProfile) -> Vec<u16> {
let mut extensions = Vec::with_capacity(profile.extensions.len() + 2);
if profile.include_grease {
extensions.push(random_grease());
}
extensions.extend_from_slice(profile.extensions);
if profile.include_grease {
extensions.push(random_grease());
}
extensions
}
#[must_use]
pub fn build_supported_groups(profile: &TlsProfile) -> Vec<u16> {
let mut groups = Vec::with_capacity(profile.elliptic_curves.len() + 1);
if profile.include_grease {
groups.push(random_grease());
}
groups.extend_from_slice(profile.elliptic_curves);
groups
}
#[must_use]
pub fn client_hello_fields(profile: &TlsProfile) -> ClientHelloFields {
ClientHelloFields {
version: profile.tls_version,
cipher_suites: profile.cipher_suites.to_vec(),
extensions: profile.extensions.to_vec(),
supported_groups: profile.elliptic_curves.to_vec(),
ec_point_formats: profile.ec_point_formats.to_vec(),
alpn: profile
.alpn_protocols
.iter()
.map(|protocol| protocol.as_bytes().to_vec())
.collect(),
signature_algorithms: profile.signature_algorithms.to_vec(),
supported_versions: profile.supported_versions.to_vec(),
}
}
#[must_use]
pub fn compute_ja3_string(profile: &TlsProfile) -> String {
compute_ja3(&client_hello_fields(profile))
}
#[must_use]
pub fn compute_ja4_string(profile: &TlsProfile) -> String {
compute_ja4(&client_hello_fields(profile))
}
#[must_use]
pub fn profile_summary(profile: &TlsProfile) -> String {
format!(
"{}: TLS {:#06x}, {} ciphers, {} extensions, GREASE={}, ALPN=[{}]",
profile.name,
profile.tls_version,
profile.cipher_suites.len(),
profile.extensions.len(),
profile.include_grease,
profile.alpn_protocols.join(", "),
)
}
#[cfg(test)]
#[path = "tls_profiles/tests.rs"]
mod tests;