mimic-rs
High-performance User-Agent to Sec-CH-UA Client Hints converter for Rust.
Overview
mimic-rs parses User-Agent strings from Chromium-based browsers and generates the corresponding Sec-CH-UA (Client Hints) headers. This is useful for:
- HTTP clients that need to send proper Client Hints headers
- Browser fingerprinting research
- Web scraping with accurate browser emulation
- Testing servers that rely on Client Hints
Features
- Zero-copy parsing where possible
- Support for Chrome, Edge, Brave, Opera, Vivaldi, Samsung Internet, Yandex, and generic Chromium
- Accurate greased brand generation matching Chromium's algorithm
- Platform detection (Windows, macOS, Linux, Android, Chrome OS)
- Mobile browser detection
- Optional
serdesupport for serialization - No unsafe code
Installation
Add to your Cargo.toml:
[]
= "0.1"
With serde support:
[]
= { = "0.1", = ["serde"] }
Usage
Basic Usage
use ClientHints;
Output:
sec-ch-ua: "Not A(Brand";v="8", "Chromium";v="120", "Google Chrome";v="120"
sec-ch-ua-mobile: ?0
sec-ch-ua-platform: "Windows"
Manual Construction
use ;
let hints = new;
println!;
println!;
Getting All Headers at Once
use ClientHints;
let ua = "Mozilla/5.0 (Linux; Android 13) Chrome/120.0.0.0 Mobile Safari/537.36";
let hints = from_ua.unwrap;
let = hints.all_headers;
Supported Browsers
| Browser | Detection Token | Brand Name |
|---|---|---|
| Chrome | Chrome/ |
Google Chrome |
| Edge | Edg/, EdgA/, EdgiOS/ |
Microsoft Edge |
| Brave | Brave |
Brave |
| Opera | OPR/, Opera/ |
Opera |
| Vivaldi | Vivaldi/ |
Vivaldi |
| Samsung Internet | SamsungBrowser/ |
Samsung Internet |
| Yandex | YaBrowser/, Yandex/ |
Yandex Browser |
| Chromium | Chromium/ |
Chromium |
Supported Platforms
- Windows
- macOS
- Linux
- Android
- Chrome OS
API Reference
ClientHints
Main struct containing parsed browser information.
Methods
from_ua(user_agent: &str) -> Result<ClientHints, ParseError>- Parse a User-Agent stringnew(brand, major_version, full_version, platform, is_mobile)- Create manuallybrand() -> Brand- Get the detected browser brandmajor_version() -> u32- Get the major version numberfull_version() -> &str- Get the full version stringplatform() -> Platform- Get the detected platformis_mobile() -> bool- Check if mobile browsersec_ch_ua() -> String- Generatesec-ch-uaheader valuesec_ch_ua_full_version_list() -> String- Generatesec-ch-ua-full-version-listheadersec_ch_ua_mobile() -> &'static str- Generatesec-ch-ua-mobileheader (?0or?1)sec_ch_ua_platform() -> String- Generatesec-ch-ua-platformheaderall_headers() -> (String, &'static str, String)- Get all three main headers
Brand
Enum representing browser brands:
Chrome,Edge,Brave,Opera,Vivaldi,Samsung,Yandex,Chromium
Platform
Enum representing operating systems:
Windows,MacOS,Linux,Android,ChromeOS,Unknown
ParseError
Error types:
EmptyUserAgent- Empty input stringNotChromiumBased- Not a Chromium browserInvalidVersion- Could not parse versionChromeTokenNotFound- No Chrome/Chromium token found
Performance
The library is optimized for speed:
- No regex dependencies
- Minimal allocations
- Preallocated string buffers
#[inline]on hot paths
Run benchmarks:
How Greased Brands Work
Chromium browsers include a "greased" fake brand in the sec-ch-ua header to prevent servers from relying on specific patterns. The greased brand changes based on the major version:
- Characters cycle through:
,(,:,-,. - Versions cycle through:
8,99,24 - Order of brands is permuted based on version
This library accurately replicates Chromium's greasing algorithm.
License
MIT License - see LICENSE for details.
Acknowledgments
Inspired by mimic (Go).