wreq_util/emulation/
mod.rs

1mod device;
2#[cfg(feature = "emulation-rand")]
3mod rand;
4
5use device::{chrome::*, firefox::*, okhttp::*, opera::*, safari::*};
6#[cfg(feature = "emulation-serde")]
7use serde::{Deserialize, Serialize};
8#[cfg(feature = "emulation-rand")]
9use strum_macros::VariantArray;
10use typed_builder::TypedBuilder;
11
12macro_rules! define_emulation_enum {
13    ($(#[$meta:meta])* $name:ident, $default_variant:ident, $($variant:ident => $rename:expr),*) => {
14        $(#[$meta])*
15        #[non_exhaustive]
16        #[derive(Clone, Copy, Hash, Debug, PartialEq, Eq)]
17        #[cfg_attr(feature = "emulation-rand", derive(VariantArray))]
18        #[cfg_attr(feature = "emulation-serde", derive(Deserialize, Serialize))]
19        pub enum $name {
20            $(
21                #[cfg_attr(feature = "emulation-serde", serde(rename = $rename))]
22                $variant,
23            )*
24        }
25
26        impl Default for $name {
27            fn default() -> Self {
28                $name::$default_variant
29            }
30        }
31    };
32}
33
34define_emulation_enum!(
35    /// Represents different browser versions for impersonation.
36    ///
37    /// The `Emulation` enum provides variants for different browser versions that can be used
38    /// to emulation HTTP requests. Each variant corresponds to a specific browser version.
39    ///
40    /// # Naming Convention
41    ///
42    /// The naming convention for the variants follows the pattern `browser_version`, where
43    /// `browser` is the name of the browser (e.g., `chrome`, `firefox`, `safari`) and `version`
44    /// is the version number. For example, `Chrome100` represents Chrome version 100.
45    ///
46    /// The serialized names of the variants use underscores to separate the browser name and
47    /// version number, following the pattern `browser_version`. For example, `Chrome100` is
48    /// serialized as `"chrome_100"`.
49    ///
50    /// # Examples
51    ///
52    /// ```rust
53    /// use wreq_util::Emulation;
54    ///
55    /// let emulation = Emulation::Chrome100;
56    /// let serialized = serde_json::to_string(&emulation).unwrap();
57    /// assert_eq!(serialized, "\"chrome_100\"");
58    ///
59    /// let deserialized: Emulation = serde_json::from_str(&serialized).unwrap();
60    /// assert_eq!(deserialized, Emulation::Chrome100);
61    /// ```
62    Emulation, Chrome133,
63    Chrome100 => "chrome_100",
64    Chrome101 => "chrome_101",
65    Chrome104 => "chrome_104",
66    Chrome105 => "chrome_105",
67    Chrome106 => "chrome_106",
68    Chrome107 => "chrome_107",
69    Chrome108 => "chrome_108",
70    Chrome109 => "chrome_109",
71    Chrome110 => "chrome_110",
72    Chrome114 => "chrome_114",
73    Chrome116 => "chrome_116",
74    Chrome117 => "chrome_117",
75    Chrome118 => "chrome_118",
76    Chrome119 => "chrome_119",
77    Chrome120 => "chrome_120",
78    Chrome123 => "chrome_123",
79    Chrome124 => "chrome_124",
80    Chrome126 => "chrome_126",
81    Chrome127 => "chrome_127",
82    Chrome128 => "chrome_128",
83    Chrome129 => "chrome_129",
84    Chrome130 => "chrome_130",
85    Chrome131 => "chrome_131",
86    Chrome132 => "chrome_132",
87    Chrome133 => "chrome_133",
88    Chrome134 => "chrome_134",
89    Chrome135 => "chrome_135",
90    Chrome136 => "chrome_136",
91    Chrome137 => "chrome_137",
92
93    SafariIos17_2 => "safari_ios_17.2",
94    SafariIos17_4_1 => "safari_ios_17.4.1",
95    SafariIos16_5 => "safari_ios_16.5",
96    Safari15_3 => "safari_15.3",
97    Safari15_5 => "safari_15.5",
98    Safari15_6_1 => "safari_15.6.1",
99    Safari16 => "safari_16",
100    Safari16_5 => "safari_16.5",
101    Safari17_0 => "safari_17.0",
102    Safari17_2_1 => "safari_17.2.1",
103    Safari17_4_1 => "safari_17.4.1",
104    Safari17_5 => "safari_17.5",
105    Safari18 => "safari_18",
106    SafariIPad18 => "safari_ipad_18",
107    Safari18_2 => "safari_18.2",
108    SafariIos18_1_1 => "safari_ios_18.1.1",
109    Safari18_3 => "safari_18.3",
110    Safari18_3_1 => "safari_18.3.1",
111    Safari18_5 => "safari_18.5",
112
113    OkHttp3_9 => "okhttp_3.9",
114    OkHttp3_11 => "okhttp_3.11",
115    OkHttp3_13 => "okhttp_3.13",
116    OkHttp3_14 => "okhttp_3.14",
117    OkHttp4_9 => "okhttp_4.9",
118    OkHttp4_10 => "okhttp_4.10",
119    OkHttp4_12 => "okhttp_4.12",
120    OkHttp5 => "okhttp_5",
121
122    Edge101 => "edge_101",
123    Edge122 => "edge_122",
124    Edge127 => "edge_127",
125    Edge131 => "edge_131",
126    Edge134 => "edge_134",
127
128    Firefox109 => "firefox_109",
129    Firefox117 => "firefox_117",
130    Firefox128 => "firefox_128",
131    Firefox133 => "firefox_133",
132    Firefox135 => "firefox_135",
133    FirefoxPrivate135 => "firefox_private_135",
134    FirefoxAndroid135 => "firefox_android_135",
135    Firefox136 => "firefox_136",
136    FirefoxPrivate136 => "firefox_private_136",
137    Firefox139 => "firefox_139",
138
139    Opera116 => "opera_116",
140    Opera117 => "opera_117",
141    Opera118 => "opera_118",
142    Opera119 => "opera_119"
143);
144
145/// ======== Emulation impls ========
146impl wreq::EmulationFactory for Emulation {
147    fn emulation(self) -> wreq::Emulation {
148        EmulationOption::builder()
149            .emulation(self)
150            .build()
151            .emulation()
152    }
153}
154
155define_emulation_enum!(
156    /// Represents different operating systems for impersonation.
157    ///
158    /// The `EmulationOS` enum provides variants for different operating systems that can be used
159    /// to emulation HTTP requests. Each variant corresponds to a specific operating system.
160    ///
161    /// # Naming Convention
162    ///
163    /// The naming convention for the variants follows the pattern `os_name`, where
164    /// `os_name` is the name of the operating system (e.g., `windows`, `macos`, `linux`, `android`, `ios`).
165    ///
166    /// The serialized names of the variants use lowercase letters to represent the operating system names.
167    /// For example, `Windows` is serialized as `"windows"`.
168    ///
169    /// # Examples
170    ///
171    /// ```rust
172    /// use wreq::EmulationOS;
173    ///
174    /// let emulation_os = EmulationOS::Windows;
175    /// let serialized = serde_json::to_string(&emulation_os).unwrap();
176    /// assert_eq!(serialized, "\"windows\"");
177    ///
178    /// let deserialized: EmulationOS = serde_json::from_str(&serialized).unwrap();
179    /// assert_eq!(deserialized, EmulationOS::Windows);
180    /// ```
181    EmulationOS, MacOS,
182    Windows => "windows",
183    MacOS => "macos",
184    Linux => "linux",
185    Android => "android",
186    IOS => "ios"
187);
188
189/// ======== EmulationOS impls ========
190impl EmulationOS {
191    #[inline]
192    const fn platform(&self) -> &'static str {
193        match self {
194            EmulationOS::MacOS => "\"macOS\"",
195            EmulationOS::Linux => "\"Linux\"",
196            EmulationOS::Windows => "\"Windows\"",
197            EmulationOS::Android => "\"Android\"",
198            EmulationOS::IOS => "\"iOS\"",
199        }
200    }
201
202    #[inline]
203    const fn is_mobile(&self) -> bool {
204        matches!(self, EmulationOS::Android | EmulationOS::IOS)
205    }
206}
207
208/// Represents the configuration options for emulating a browser and operating system.
209///
210/// The `EmulationOption` struct allows you to configure various aspects of browser and OS
211/// emulation, including the browser version, operating system, and whether to skip certain features
212/// like HTTP/2 or headers.
213///
214/// This struct is typically used to build an `EmulationProvider` that can be applied to HTTP
215/// clients for making requests that mimic specific browser and OS configurations.
216///
217/// # Fields
218///
219/// - `emulation`: The browser version to emulate. Defaults to `Emulation::default()`.
220/// - `emulation_os`: The operating system to emulate. Defaults to `EmulationOS::default()`.
221/// - `skip_http2`: Whether to skip HTTP/2 support. Defaults to `false`.
222/// - `skip_headers`: Whether to skip adding default headers. Defaults to `false`.
223///
224/// # Examples
225///
226/// ```rust
227/// use wreq_util::{Emulation, EmulationOS, EmulationOption};
228///
229/// let emulation_option = EmulationOption::builder()
230///     .emulation(Emulation::Chrome134)
231///     .emulation_os(EmulationOS::MacOS)
232///     .skip_http2(true)
233///     .skip_headers(false)
234///     .build();
235///
236/// // Use `emulation_option` to create an EmulationProvider
237/// let emulation_provider = emulation_option.emulation();
238/// ```
239#[derive(Default, Clone, TypedBuilder)]
240pub struct EmulationOption {
241    /// The browser version to emulation.
242    #[builder(default)]
243    emulation: Emulation,
244
245    /// The operating system.
246    #[builder(default)]
247    emulation_os: EmulationOS,
248
249    /// Whether to skip HTTP/2.
250    #[builder(default = false)]
251    skip_http2: bool,
252
253    /// Whether to skip headers.
254    #[builder(default = false)]
255    skip_headers: bool,
256}
257
258/// ======== EmulationOption impls ========
259macro_rules! emulation_match {
260    ($ver:expr, $opt:expr, $($variant:pat => $path:expr),+) => {
261        match $ver {
262            $(
263                $variant => $path($opt),
264            )+
265        }
266    }
267}
268
269impl wreq::EmulationFactory for EmulationOption {
270    fn emulation(self) -> wreq::Emulation {
271        emulation_match!(
272            self.emulation,
273            self,
274
275            Emulation::Chrome100 => v100::emulation,
276            Emulation::Chrome101 => v101::emulation,
277            Emulation::Chrome104 => v104::emulation,
278            Emulation::Chrome105 => v105::emulation,
279            Emulation::Chrome106 => v106::emulation,
280            Emulation::Chrome107 => v107::emulation,
281            Emulation::Chrome108 => v108::emulation,
282            Emulation::Chrome109 => v109::emulation,
283            Emulation::Chrome110 => v110::emulation,
284            Emulation::Chrome114 => v114::emulation,
285            Emulation::Chrome116 => v116::emulation,
286            Emulation::Chrome117 => v117::emulation,
287            Emulation::Chrome118 => v118::emulation,
288            Emulation::Chrome119 => v119::emulation,
289            Emulation::Chrome120 => v120::emulation,
290            Emulation::Chrome123 => v123::emulation,
291            Emulation::Chrome124 => v124::emulation,
292            Emulation::Chrome126 => v126::emulation,
293            Emulation::Chrome127 => v127::emulation,
294            Emulation::Chrome128 => v128::emulation,
295            Emulation::Chrome129 => v129::emulation,
296            Emulation::Chrome130 => v130::emulation,
297            Emulation::Chrome131 => v131::emulation,
298            Emulation::Chrome132 => v132::emulation,
299            Emulation::Chrome133 => v133::emulation,
300            Emulation::Chrome134 => v134::emulation,
301            Emulation::Chrome135 => v135::emulation,
302            Emulation::Chrome136 => v136::emulation,
303            Emulation::Chrome137 => v137::emulation,
304
305            Emulation::SafariIos17_2 => safari_ios_17_2::emulation,
306            Emulation::SafariIos17_4_1 => safari_ios_17_4_1::emulation,
307            Emulation::SafariIos16_5 => safari_ios_16_5::emulation,
308            Emulation::Safari15_3 => safari15_3::emulation,
309            Emulation::Safari15_5 => safari15_5::emulation,
310            Emulation::Safari15_6_1 => safari15_6_1::emulation,
311            Emulation::Safari16 => safari16::emulation,
312            Emulation::Safari16_5 => safari16_5::emulation,
313            Emulation::Safari17_0 => safari17_0::emulation,
314            Emulation::Safari17_2_1 => safari17_2_1::emulation,
315            Emulation::Safari17_4_1 => safari17_4_1::emulation,
316            Emulation::Safari17_5 => safari17_5::emulation,
317            Emulation::Safari18 => safari18::emulation,
318            Emulation::SafariIPad18 => safari_ipad_18::emulation,
319            Emulation::Safari18_2 => safari18_2::emulation,
320            Emulation::SafariIos18_1_1 => safari_ios_18_1_1::emulation,
321            Emulation::Safari18_3 => safari18_3::emulation,
322            Emulation::Safari18_3_1 => safari18_3_1::emulation,
323            Emulation::Safari18_5 => safari18_5::emulation,
324
325            Emulation::OkHttp3_9 => okhttp3_9::emulation,
326            Emulation::OkHttp3_11 => okhttp3_11::emulation,
327            Emulation::OkHttp3_13 => okhttp3_13::emulation,
328            Emulation::OkHttp3_14 => okhttp3_14::emulation,
329            Emulation::OkHttp4_9 => okhttp4_9::emulation,
330            Emulation::OkHttp4_10 => okhttp4_10::emulation,
331            Emulation::OkHttp4_12 => okhttp4_12::emulation,
332            Emulation::OkHttp5 => okhttp5::emulation,
333
334            Emulation::Edge101 => edge101::emulation,
335            Emulation::Edge122 => edge122::emulation,
336            Emulation::Edge127 => edge127::emulation,
337            Emulation::Edge131 => edge131::emulation,
338            Emulation::Edge134 => edge134::emulation,
339
340            Emulation::Firefox109 => ff109::emulation,
341            Emulation::Firefox117 => ff117::emulation,
342            Emulation::Firefox128 => ff128::emulation,
343            Emulation::Firefox133 => ff133::emulation,
344            Emulation::Firefox135 => ff135::emulation,
345            Emulation::FirefoxPrivate135 => ff_private_135::emulation,
346            Emulation::FirefoxAndroid135 => ff_android_135::emulation,
347            Emulation::Firefox136 => ff136::emulation,
348            Emulation::FirefoxPrivate136 => ff_private_136::emulation,
349            Emulation::Firefox139 => ff139::emulation,
350
351            Emulation::Opera116 => opera116::emulation,
352            Emulation::Opera117 => opera117::emulation,
353            Emulation::Opera118 => opera118::emulation,
354            Emulation::Opera119 => opera119::emulation
355        )
356    }
357}