1mod device;
2#[cfg(feature = "emulation-rand")]
3mod rand;
4
5use device::{chrome::*, firefox::*, okhttp::*, safari::*};
6use rquest::{EmulationProvider, EmulationProviderFactory};
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 #[derive(Clone, Copy, Hash, Debug, PartialEq, Eq)]
16 #[cfg_attr(feature = "emulation-rand", derive(Serialize, Deserialize, VariantArray))]
17 pub enum $name {
18 $(
19 #[cfg_attr(feature = "emulation-rand", serde(rename = $rename))]
20 $variant,
21 )*
22 }
23
24 impl Default for $name {
25 fn default() -> Self {
26 $name::$default_variant
27 }
28 }
29 };
30}
31
32define_emulation_enum!(
33 Emulation, Chrome133,
61 Chrome100 => "chrome_100",
62 Chrome101 => "chrome_101",
63 Chrome104 => "chrome_104",
64 Chrome105 => "chrome_105",
65 Chrome106 => "chrome_106",
66 Chrome107 => "chrome_107",
67 Chrome108 => "chrome_108",
68 Chrome109 => "chrome_109",
69 Chrome114 => "chrome_114",
70 Chrome116 => "chrome_116",
71 Chrome117 => "chrome_117",
72 Chrome118 => "chrome_118",
73 Chrome119 => "chrome_119",
74 Chrome120 => "chrome_120",
75 Chrome123 => "chrome_123",
76 Chrome124 => "chrome_124",
77 Chrome126 => "chrome_126",
78 Chrome127 => "chrome_127",
79 Chrome128 => "chrome_128",
80 Chrome129 => "chrome_129",
81 Chrome130 => "chrome_130",
82 Chrome131 => "chrome_131",
83 Chrome132 => "chrome_132",
84 Chrome133 => "chrome_133",
85 Chrome134 => "chrome_134",
86 SafariIos17_2 => "safari_ios_17.2",
87 SafariIos17_4_1 => "safari_ios_17.4.1",
88 SafariIos16_5 => "safari_ios_16.5",
89 Safari15_3 => "safari_15.3",
90 Safari15_5 => "safari_15.5",
91 Safari15_6_1 => "safari_15.6.1",
92 Safari16 => "safari_16",
93 Safari16_5 => "safari_16.5",
94 Safari17_0 => "safari_17.0",
95 Safari17_2_1 => "safari_17.2.1",
96 Safari17_4_1 => "safari_17.4.1",
97 Safari17_5 => "safari_17.5",
98 Safari18 => "safari_18",
99 SafariIPad18 => "safari_ipad_18",
100 Safari18_2 => "safari_18.2",
101 SafariIos18_1_1 => "safari_ios_18.1.1",
102 Safari18_3 => "safari_18.3",
103 Safari18_3_1 => "safari_18.3.1",
104 OkHttp3_9 => "okhttp_3.9",
105 OkHttp3_11 => "okhttp_3.11",
106 OkHttp3_13 => "okhttp_3.13",
107 OkHttp3_14 => "okhttp_3.14",
108 OkHttp4_9 => "okhttp_4.9",
109 OkHttp4_10 => "okhttp_4.10",
110 OkHttp4_12 => "okhttp_4.12",
111 OkHttp5 => "okhttp_5",
112 Edge101 => "edge_101",
113 Edge122 => "edge_122",
114 Edge127 => "edge_127",
115 Edge131 => "edge_131",
116 Edge134 => "edge_134",
117 Firefox109 => "firefox_109",
118 Firefox117 => "firefox_117",
119 Firefox128 => "firefox_128",
120 Firefox133 => "firefox_133",
121 Firefox135 => "firefox_135",
122 FirefoxPrivate135 => "firefox_private_135",
123 FirefoxAndroid135 => "firefox_android_135",
124 Firefox136 => "firefox_136",
125 FirefoxPrivate136 => "firefox_private_136"
126);
127
128impl EmulationProviderFactory for Emulation {
130 fn emulation(self) -> EmulationProvider {
131 EmulationOption::builder()
132 .emulation(self)
133 .build()
134 .emulation()
135 }
136}
137
138#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Deserialize, Serialize)]
164pub enum EmulationOS {
165 #[serde(rename = "windows")]
166 Windows,
167 #[serde(rename = "macos")]
168 #[default]
169 MacOS,
170 #[serde(rename = "linux")]
171 Linux,
172 #[serde(rename = "android")]
173 Android,
174 #[serde(rename = "ios")]
175 IOS,
176}
177
178impl EmulationOS {
180 #[inline]
181 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 fn is_mobile(&self) -> bool {
193 matches!(self, EmulationOS::Android | EmulationOS::IOS)
194 }
195}
196
197#[derive(Default, TypedBuilder)]
229pub struct EmulationOption {
230 #[builder(default)]
232 emulation: Emulation,
233
234 #[builder(default)]
236 emulation_os: EmulationOS,
237
238 #[builder(default = false)]
240 skip_http2: bool,
241
242 #[builder(default = false)]
244 skip_headers: bool,
245}
246
247macro_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::Chrome114 => v114::emulation,
273 Emulation::Chrome116 => v116::emulation,
274 Emulation::Chrome117 => v117::emulation,
275 Emulation::Chrome118 => v118::emulation,
276 Emulation::Chrome119 => v119::emulation,
277 Emulation::Chrome120 => v120::emulation,
278 Emulation::Chrome123 => v123::emulation,
279 Emulation::Chrome124 => v124::emulation,
280 Emulation::Chrome126 => v126::emulation,
281 Emulation::Chrome127 => v127::emulation,
282 Emulation::Chrome128 => v128::emulation,
283 Emulation::Chrome129 => v129::emulation,
284 Emulation::Chrome130 => v130::emulation,
285 Emulation::Chrome131 => v131::emulation,
286 Emulation::Chrome132 => v132::emulation,
287 Emulation::Chrome133 => v133::emulation,
288 Emulation::Chrome134 => v134::emulation,
289
290 Emulation::SafariIos17_2 => safari_ios_17_2::emulation,
291 Emulation::SafariIos17_4_1 => safari_ios_17_4_1::emulation,
292 Emulation::SafariIos16_5 => safari_ios_16_5::emulation,
293 Emulation::Safari15_3 => safari15_3::emulation,
294 Emulation::Safari15_5 => safari15_5::emulation,
295 Emulation::Safari15_6_1 => safari15_6_1::emulation,
296 Emulation::Safari16 => safari16::emulation,
297 Emulation::Safari16_5 => safari16_5::emulation,
298 Emulation::Safari17_0 => safari17_0::emulation,
299 Emulation::Safari17_2_1 => safari17_2_1::emulation,
300 Emulation::Safari17_4_1 => safari17_4_1::emulation,
301 Emulation::Safari17_5 => safari17_5::emulation,
302 Emulation::Safari18 => safari18::emulation,
303 Emulation::SafariIPad18 => safari_ipad_18::emulation,
304 Emulation::Safari18_2 => safari18_2::emulation,
305 Emulation::SafariIos18_1_1 => safari_ios_18_1_1::emulation,
306 Emulation::Safari18_3 => safari18_3::emulation,
307 Emulation::Safari18_3_1 => safari18_3_1::emulation,
308
309 Emulation::OkHttp3_9 => okhttp3_9::emulation,
310 Emulation::OkHttp3_11 => okhttp3_11::emulation,
311 Emulation::OkHttp3_13 => okhttp3_13::emulation,
312 Emulation::OkHttp3_14 => okhttp3_14::emulation,
313 Emulation::OkHttp4_9 => okhttp4_9::emulation,
314 Emulation::OkHttp4_10 => okhttp4_10::emulation,
315 Emulation::OkHttp4_12 => okhttp4_12::emulation,
316 Emulation::OkHttp5 => okhttp5::emulation,
317
318 Emulation::Edge101 => edge101::emulation,
319 Emulation::Edge122 => edge122::emulation,
320 Emulation::Edge127 => edge127::emulation,
321 Emulation::Edge131 => edge131::emulation,
322 Emulation::Edge134 => edge134::emulation,
323
324 Emulation::Firefox109 => ff109::emulation,
325 Emulation::Firefox117 => ff117::emulation,
326 Emulation::Firefox128 => ff128::emulation,
327 Emulation::Firefox133 => ff133::emulation,
328 Emulation::Firefox135 => ff135::emulation,
329 Emulation::FirefoxPrivate135 => ff_private_135::emulation,
330 Emulation::FirefoxAndroid135 => ff_android_135::emulation,
331 Emulation::Firefox136 => ff136::emulation,
332 Emulation::FirefoxPrivate136 => ff_private_136::emulation
333 )
334 }
335}