use crate::choice::random_item;
use crate::fingerprint::StealthProfile;
use crate::fingerprint::ja3::{compute_ja3, ClientHelloFields};
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,
}
const TLS13_CHROME_CIPHERS: &[u16] = &[0x1301, 0x1302, 0x1303];
const TLS13_FIREFOX_CIPHERS: &[u16] = &[0x1301, 0x1303, 0x1302];
const CHROME_EXTENSIONS: &[u16] = &[
0x0000, 0x0017, 0xff01, 0x000a, 0x000b, 0x0023, 0x0010, 0x0005, 0x0012, 0x0033, 0x002b, 0x000d,
0x002d, 0x001c, 0x001b,
];
const FIREFOX_EXTENSIONS: &[u16] = &[
0x0000, 0x0017, 0xff01, 0x000a, 0x000b, 0x0023, 0x0010, 0x0005, 0x000d, 0x0033, 0x002b, 0x002d,
0x001c, 0x0015,
];
const MODERN_CURVES: &[u16] = &[0x001d, 0x0017, 0x0018];
const EC_POINT_FORMATS: &[u8] = &[0x00];
const CHROME_SIG_ALGS: &[u16] = &[
0x0403, 0x0804, 0x0401, 0x0503, 0x0805, 0x0501, 0x0806, 0x0601,
];
const FIREFOX_SIG_ALGS: &[u16] = &[
0x0403, 0x0503, 0x0603, 0x0804, 0x0805, 0x0806, 0x0401, 0x0501, 0x0601,
];
const PYTHON_SIG_ALGS: &[u16] = &[
0x0403, 0x0804, 0x0401, 0x0503, 0x0805, 0x0501, 0x0806, 0x0601, 0x0201,
];
const CURL_EXTENSIONS: &[u16] = &[
0x0000, 0x000a, 0x000b, 0x000d, 0x0033, 0x002b, 0x002d, 0x0010,
];
const SAFARI_IOS_EXTENSIONS: &[u16] = &[
0x0000, 0x0017, 0xff01, 0x000a, 0x000b, 0x0023, 0x0010, 0x0005, 0x0012, 0x0033, 0x002b, 0x000d,
0x002d, 0x001b, 0x0039,
];
const PYTHON_REQUESTS_EXTENSIONS: &[u16] =
&[0x0000, 0x000a, 0x000d, 0x0033, 0x002b, 0x002d, 0x0010];
const CHROME_122: TlsProfile = TlsProfile {
name: "Chrome 122 / Windows 11",
tls_version: 0x0303,
cipher_suites: TLS13_CHROME_CIPHERS,
extensions: CHROME_EXTENSIONS,
elliptic_curves: MODERN_CURVES,
ec_point_formats: EC_POINT_FORMATS,
alpn_protocols: &["h2", "http/1.1"],
expected_ja3: "773906b0efdefa24a7f2b8eb6985bf37",
signature_algorithms: CHROME_SIG_ALGS,
include_grease: true,
};
const CHROME_120: TlsProfile = TlsProfile {
name: "Chrome 120 / macOS 14",
tls_version: 0x0303,
cipher_suites: TLS13_CHROME_CIPHERS,
extensions: CHROME_EXTENSIONS,
elliptic_curves: MODERN_CURVES,
ec_point_formats: EC_POINT_FORMATS,
alpn_protocols: &["h2", "http/1.1"],
expected_ja3: "cd08e31494f9531f560d64c695473da9",
signature_algorithms: CHROME_SIG_ALGS,
include_grease: true,
};
const FIREFOX_122: TlsProfile = TlsProfile {
name: "Firefox 122 / Linux",
tls_version: 0x0303,
cipher_suites: TLS13_FIREFOX_CIPHERS,
extensions: FIREFOX_EXTENSIONS,
elliptic_curves: MODERN_CURVES,
ec_point_formats: EC_POINT_FORMATS,
alpn_protocols: &["h2", "http/1.1"],
expected_ja3: "579ccef312d18482fc42e2b822ca2430",
signature_algorithms: FIREFOX_SIG_ALGS,
include_grease: false,
};
const FIREFOX_115_ESR: TlsProfile = TlsProfile {
name: "Firefox 115 ESR / Windows 10",
tls_version: 0x0303,
cipher_suites: TLS13_FIREFOX_CIPHERS,
extensions: FIREFOX_EXTENSIONS,
elliptic_curves: MODERN_CURVES,
ec_point_formats: EC_POINT_FORMATS,
alpn_protocols: &["h2", "http/1.1"],
expected_ja3: "c5a1d8f2e39abb68df7da538a0c53839",
signature_algorithms: FIREFOX_SIG_ALGS,
include_grease: false,
};
const SAFARI_17: TlsProfile = TlsProfile {
name: "Safari 17 / macOS 14",
tls_version: 0x0303,
cipher_suites: TLS13_CHROME_CIPHERS,
extensions: CHROME_EXTENSIONS,
elliptic_curves: MODERN_CURVES,
ec_point_formats: EC_POINT_FORMATS,
alpn_protocols: &["h2", "http/1.1"],
expected_ja3: "773906b0efdefa24a7f2b8eb6985bf37",
signature_algorithms: CHROME_SIG_ALGS,
include_grease: true,
};
const EDGE_120: TlsProfile = TlsProfile {
name: "Edge 120 / Windows 11",
tls_version: 0x0303,
cipher_suites: TLS13_CHROME_CIPHERS,
extensions: CHROME_EXTENSIONS,
elliptic_curves: MODERN_CURVES,
ec_point_formats: EC_POINT_FORMATS,
alpn_protocols: &["h2", "http/1.1"],
expected_ja3: "cd08e31494f9531f560d64c695473da9",
signature_algorithms: CHROME_SIG_ALGS,
include_grease: true,
};
const CURL_8_OPENSSL: TlsProfile = TlsProfile {
name: "curl 8 / OpenSSL 3",
tls_version: 0x0303,
cipher_suites: TLS13_CHROME_CIPHERS,
extensions: CURL_EXTENSIONS,
elliptic_curves: MODERN_CURVES,
ec_point_formats: EC_POINT_FORMATS,
alpn_protocols: &["h2", "http/1.1"],
expected_ja3: "b1ce3e0d1a7a3b63f4f6d8e4c1db2a5a",
signature_algorithms: CHROME_SIG_ALGS,
include_grease: false,
};
const SAFARI_IOS_17: TlsProfile = TlsProfile {
name: "Safari iOS 17 / iPhone",
tls_version: 0x0303,
cipher_suites: TLS13_CHROME_CIPHERS,
extensions: SAFARI_IOS_EXTENSIONS,
elliptic_curves: MODERN_CURVES,
ec_point_formats: EC_POINT_FORMATS,
alpn_protocols: &["h2", "http/1.1"],
expected_ja3: "e4e98fa90cad4b76c5f7fd9e3c3f9e2b",
signature_algorithms: CHROME_SIG_ALGS,
include_grease: true,
};
const PYTHON_REQUESTS: TlsProfile = TlsProfile {
name: "Python requests 2.31 / urllib3",
tls_version: 0x0303,
cipher_suites: TLS13_CHROME_CIPHERS,
extensions: PYTHON_REQUESTS_EXTENSIONS,
elliptic_curves: MODERN_CURVES,
ec_point_formats: EC_POINT_FORMATS,
alpn_protocols: &["h2", "http/1.1"],
expected_ja3: "c67e0093a0f49d2f7bc3e1e8f3b3d5a7",
signature_algorithms: PYTHON_SIG_ALGS,
include_grease: false,
};
const ALL_PROFILES: &[TlsProfile] = &[
CHROME_122,
CHROME_120,
FIREFOX_122,
FIREFOX_115_ESR,
SAFARI_17,
EDGE_120,
CURL_8_OPENSSL,
SAFARI_IOS_17,
PYTHON_REQUESTS,
];
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::Safari17_5, ImpersonateProfile::Safari18];
#[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::BraveWindows
| StealthProfile::OperaWindows
| StealthProfile::SamsungInternetAndroid => Some(&CHROME_122),
StealthProfile::ChromeWindowsLegacy96 | StealthProfile::ChromeMacStable => {
Some(&CHROME_120)
}
StealthProfile::EdgeWindowsStable => Some(&EDGE_120),
StealthProfile::FirefoxLinux => Some(&FIREFOX_122),
StealthProfile::FirefoxWindows => Some(&FIREFOX_115_ESR),
StealthProfile::SafariIphone | StealthProfile::SafariIpad => Some(&SAFARI_IOS_17),
StealthProfile::SafariMacStable => Some(&SAFARI_17),
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 => {
ImpersonateProfile::Firefox133
}
StealthProfile::SafariIphone
| StealthProfile::SafariIpad
| StealthProfile::SafariMacStable => ImpersonateProfile::Safari17_5,
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 => {
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)
}
#[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).to_string())
.collect(),
signature_algorithms: profile.signature_algorithms.to_vec(),
}
}
#[must_use]
pub fn compute_ja3_string(profile: &TlsProfile) -> String {
compute_ja3(&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;