rquest_util/emulation/
mod.rs

1mod device;
2#[cfg(feature = "emulation-rand")]
3mod rand;
4
5use device::{chrome::*, firefox::*, okhttp::*, safari::*};
6use rquest::{EmulationProvider, EmulationProviderFactory};
7#[cfg(feature = "emulation-serde")]
8use serde::{Deserialize, Serialize};
9#[cfg(feature = "emulation-rand")]
10use strum_macros::VariantArray;
11use typed_builder::TypedBuilder;
12
13macro_rules! define_emulation_enum {
14    ($(#[$meta:meta])* $name:ident, $default_variant:ident, $($variant:ident => $rename:expr),*) => {
15        $(#[$meta])*
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 rquest_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
92    SafariIos17_2 => "safari_ios_17.2",
93    SafariIos17_4_1 => "safari_ios_17.4.1",
94    SafariIos16_5 => "safari_ios_16.5",
95    Safari15_3 => "safari_15.3",
96    Safari15_5 => "safari_15.5",
97    Safari15_6_1 => "safari_15.6.1",
98    Safari16 => "safari_16",
99    Safari16_5 => "safari_16.5",
100    Safari17_0 => "safari_17.0",
101    Safari17_2_1 => "safari_17.2.1",
102    Safari17_4_1 => "safari_17.4.1",
103    Safari17_5 => "safari_17.5",
104    Safari18 => "safari_18",
105    SafariIPad18 => "safari_ipad_18",
106    Safari18_2 => "safari_18.2",
107    SafariIos18_1_1 => "safari_ios_18.1.1",
108    Safari18_3 => "safari_18.3",
109    Safari18_3_1 => "safari_18.3.1",
110    OkHttp3_9 => "okhttp_3.9",
111    OkHttp3_11 => "okhttp_3.11",
112    OkHttp3_13 => "okhttp_3.13",
113    OkHttp3_14 => "okhttp_3.14",
114    OkHttp4_9 => "okhttp_4.9",
115    OkHttp4_10 => "okhttp_4.10",
116    OkHttp4_12 => "okhttp_4.12",
117    OkHttp5 => "okhttp_5",
118    Edge101 => "edge_101",
119    Edge122 => "edge_122",
120    Edge127 => "edge_127",
121    Edge131 => "edge_131",
122    Edge134 => "edge_134",
123    Firefox109 => "firefox_109",
124    Firefox117 => "firefox_117",
125    Firefox128 => "firefox_128",
126    Firefox133 => "firefox_133",
127    Firefox135 => "firefox_135",
128    FirefoxPrivate135 => "firefox_private_135",
129    FirefoxAndroid135 => "firefox_android_135",
130    Firefox136 => "firefox_136",
131    FirefoxPrivate136 => "firefox_private_136"
132);
133
134/// ======== Emulation impls ========
135impl EmulationProviderFactory for Emulation {
136    fn emulation(self) -> EmulationProvider {
137        EmulationOption::builder()
138            .emulation(self)
139            .build()
140            .emulation()
141    }
142}
143
144define_emulation_enum!(
145    /// Represents different operating systems for impersonation.
146    ///
147    /// The `EmulationOS` enum provides variants for different operating systems that can be used
148    /// to emulation HTTP requests. Each variant corresponds to a specific operating system.
149    ///
150    /// # Naming Convention
151    ///
152    /// The naming convention for the variants follows the pattern `os_name`, where
153    /// `os_name` is the name of the operating system (e.g., `windows`, `macos`, `linux`, `android`, `ios`).
154    ///
155    /// The serialized names of the variants use lowercase letters to represent the operating system names.
156    /// For example, `Windows` is serialized as `"windows"`.
157    ///
158    /// # Examples
159    ///
160    /// ```rust
161    /// use rquest::EmulationOS;
162    ///
163    /// let emulation_os = EmulationOS::Windows;
164    /// let serialized = serde_json::to_string(&emulation_os).unwrap();
165    /// assert_eq!(serialized, "\"windows\"");
166    ///
167    /// let deserialized: EmulationOS = serde_json::from_str(&serialized).unwrap();
168    /// assert_eq!(deserialized, EmulationOS::Windows);
169    /// ```
170    EmulationOS, MacOS,
171    Windows => "windows",
172    MacOS => "macos",
173    Linux => "linux",
174    Android => "android",
175    IOS => "ios"
176);
177
178/// ======== EmulationOS impls ========
179impl EmulationOS {
180    #[inline]
181    const fn platform(&self) -> &'static str {
182        match self {
183            EmulationOS::MacOS => "\"macOS\"",
184            EmulationOS::Linux => "\"Linux\"",
185            EmulationOS::Windows => "\"Windows\"",
186            EmulationOS::Android => "\"Android\"",
187            EmulationOS::IOS => "\"iOS\"",
188        }
189    }
190
191    #[inline]
192    const fn is_mobile(&self) -> bool {
193        matches!(self, EmulationOS::Android | EmulationOS::IOS)
194    }
195}
196
197/// Represents the configuration options for emulating a browser and operating system.
198///
199/// The `EmulationOption` struct allows you to configure various aspects of browser and OS emulation,
200/// including the browser version, operating system, and whether to skip certain features like HTTP/2
201/// or headers.
202///
203/// This struct is typically used to build an `EmulationProvider` that can be applied to HTTP clients
204/// for making requests that mimic specific browser and OS configurations.
205///
206/// # Fields
207///
208/// - `emulation`: The browser version to emulate. Defaults to `Emulation::default()`.
209/// - `emulation_os`: The operating system to emulate. Defaults to `EmulationOS::default()`.
210/// - `skip_http2`: Whether to skip HTTP/2 support. Defaults to `false`.
211/// - `skip_headers`: Whether to skip adding default headers. Defaults to `false`.
212///
213/// # Examples
214///
215/// ```rust
216/// use rquest_util::{Emulation, EmulationOS, EmulationOption};
217///
218/// let emulation_option = EmulationOption::builder()
219///     .emulation(Emulation::Chrome134)
220///     .emulation_os(EmulationOS::MacOS)
221///     .skip_http2(true)
222///     .skip_headers(false)
223///     .build();
224///
225/// // Use `emulation_option` to create an EmulationProvider
226/// let emulation_provider = emulation_option.emulation();
227/// ```
228#[derive(Default, TypedBuilder)]
229pub struct EmulationOption {
230    /// The browser version to emulation.
231    #[builder(default)]
232    emulation: Emulation,
233
234    /// The operating system.
235    #[builder(default)]
236    emulation_os: EmulationOS,
237
238    /// Whether to skip HTTP/2.
239    #[builder(default = false)]
240    skip_http2: bool,
241
242    /// Whether to skip headers.
243    #[builder(default = false)]
244    skip_headers: bool,
245}
246
247/// ======== EmulationOption impls ========
248macro_rules! emulation_match {
249    ($ver:expr, $opt:expr, $($variant:pat => $path:expr),+) => {
250        match $ver {
251            $(
252                $variant => $path($opt),
253            )+
254        }
255    }
256}
257
258impl EmulationProviderFactory for EmulationOption {
259    fn emulation(self) -> EmulationProvider {
260        emulation_match!(
261            self.emulation,
262            self,
263
264            Emulation::Chrome100 => v100::emulation,
265            Emulation::Chrome101 => v101::emulation,
266            Emulation::Chrome104 => v104::emulation,
267            Emulation::Chrome105 => v105::emulation,
268            Emulation::Chrome106 => v106::emulation,
269            Emulation::Chrome107 => v107::emulation,
270            Emulation::Chrome108 => v108::emulation,
271            Emulation::Chrome109 => v109::emulation,
272            Emulation::Chrome110 => v110::emulation,
273            Emulation::Chrome114 => v114::emulation,
274            Emulation::Chrome116 => v116::emulation,
275            Emulation::Chrome117 => v117::emulation,
276            Emulation::Chrome118 => v118::emulation,
277            Emulation::Chrome119 => v119::emulation,
278            Emulation::Chrome120 => v120::emulation,
279            Emulation::Chrome123 => v123::emulation,
280            Emulation::Chrome124 => v124::emulation,
281            Emulation::Chrome126 => v126::emulation,
282            Emulation::Chrome127 => v127::emulation,
283            Emulation::Chrome128 => v128::emulation,
284            Emulation::Chrome129 => v129::emulation,
285            Emulation::Chrome130 => v130::emulation,
286            Emulation::Chrome131 => v131::emulation,
287            Emulation::Chrome132 => v132::emulation,
288            Emulation::Chrome133 => v133::emulation,
289            Emulation::Chrome134 => v134::emulation,
290            Emulation::Chrome135 => v135::emulation,
291            Emulation::Chrome136 => v136::emulation,
292
293            Emulation::SafariIos17_2 => safari_ios_17_2::emulation,
294            Emulation::SafariIos17_4_1 => safari_ios_17_4_1::emulation,
295            Emulation::SafariIos16_5 => safari_ios_16_5::emulation,
296            Emulation::Safari15_3 => safari15_3::emulation,
297            Emulation::Safari15_5 => safari15_5::emulation,
298            Emulation::Safari15_6_1 => safari15_6_1::emulation,
299            Emulation::Safari16 => safari16::emulation,
300            Emulation::Safari16_5 => safari16_5::emulation,
301            Emulation::Safari17_0 => safari17_0::emulation,
302            Emulation::Safari17_2_1 => safari17_2_1::emulation,
303            Emulation::Safari17_4_1 => safari17_4_1::emulation,
304            Emulation::Safari17_5 => safari17_5::emulation,
305            Emulation::Safari18 => safari18::emulation,
306            Emulation::SafariIPad18 => safari_ipad_18::emulation,
307            Emulation::Safari18_2 => safari18_2::emulation,
308            Emulation::SafariIos18_1_1 => safari_ios_18_1_1::emulation,
309            Emulation::Safari18_3 => safari18_3::emulation,
310            Emulation::Safari18_3_1 => safari18_3_1::emulation,
311
312            Emulation::OkHttp3_9 => okhttp3_9::emulation,
313            Emulation::OkHttp3_11 => okhttp3_11::emulation,
314            Emulation::OkHttp3_13 => okhttp3_13::emulation,
315            Emulation::OkHttp3_14 => okhttp3_14::emulation,
316            Emulation::OkHttp4_9 => okhttp4_9::emulation,
317            Emulation::OkHttp4_10 => okhttp4_10::emulation,
318            Emulation::OkHttp4_12 => okhttp4_12::emulation,
319            Emulation::OkHttp5 => okhttp5::emulation,
320
321            Emulation::Edge101 => edge101::emulation,
322            Emulation::Edge122 => edge122::emulation,
323            Emulation::Edge127 => edge127::emulation,
324            Emulation::Edge131 => edge131::emulation,
325            Emulation::Edge134 => edge134::emulation,
326
327            Emulation::Firefox109 => ff109::emulation,
328            Emulation::Firefox117 => ff117::emulation,
329            Emulation::Firefox128 => ff128::emulation,
330            Emulation::Firefox133 => ff133::emulation,
331            Emulation::Firefox135 => ff135::emulation,
332            Emulation::FirefoxPrivate135 => ff_private_135::emulation,
333            Emulation::FirefoxAndroid135 => ff_android_135::emulation,
334            Emulation::Firefox136 => ff136::emulation,
335            Emulation::FirefoxPrivate136 => ff_private_136::emulation
336        )
337    }
338}