Skip to main content

wreq_util/
emulate.rs

1#[macro_use]
2mod macros;
3pub mod compress;
4pub mod profile;
5
6use profile::{chrome::*, firefox::*, okhttp::*, opera::*, safari::*};
7#[cfg(feature = "emulation-serde")]
8use serde::{Deserialize, Serialize};
9use typed_builder::TypedBuilder;
10
11define_enum!(
12    /// Selects which client profile the request should look like.
13    ///
14    /// This controls the built-in TLS, HTTP/2, and header presets used for the
15    /// request. Variants cover browser-style profiles as well as other clients,
16    /// such as OkHttp.
17    dispatch,
18    Profile, Chrome100,
19    Emulation,
20
21    // Chrome versions
22    Chrome100 => ("chrome_100", v100::emulation),
23    Chrome101 => ("chrome_101", v101::emulation),
24    Chrome104 => ("chrome_104", v104::emulation),
25    Chrome105 => ("chrome_105", v105::emulation),
26    Chrome106 => ("chrome_106", v106::emulation),
27    Chrome107 => ("chrome_107", v107::emulation),
28    Chrome108 => ("chrome_108", v108::emulation),
29    Chrome109 => ("chrome_109", v109::emulation),
30    Chrome110 => ("chrome_110", v110::emulation),
31    Chrome114 => ("chrome_114", v114::emulation),
32    Chrome116 => ("chrome_116", v116::emulation),
33    Chrome117 => ("chrome_117", v117::emulation),
34    Chrome118 => ("chrome_118", v118::emulation),
35    Chrome119 => ("chrome_119", v119::emulation),
36    Chrome120 => ("chrome_120", v120::emulation),
37    Chrome123 => ("chrome_123", v123::emulation),
38    Chrome124 => ("chrome_124", v124::emulation),
39    Chrome126 => ("chrome_126", v126::emulation),
40    Chrome127 => ("chrome_127", v127::emulation),
41    Chrome128 => ("chrome_128", v128::emulation),
42    Chrome129 => ("chrome_129", v129::emulation),
43    Chrome130 => ("chrome_130", v130::emulation),
44    Chrome131 => ("chrome_131", v131::emulation),
45    Chrome132 => ("chrome_132", v132::emulation),
46    Chrome133 => ("chrome_133", v133::emulation),
47    Chrome134 => ("chrome_134", v134::emulation),
48    Chrome135 => ("chrome_135", v135::emulation),
49    Chrome136 => ("chrome_136", v136::emulation),
50    Chrome137 => ("chrome_137", v137::emulation),
51    Chrome138 => ("chrome_138", v138::emulation),
52    Chrome139 => ("chrome_139", v139::emulation),
53    Chrome140 => ("chrome_140", v140::emulation),
54    Chrome141 => ("chrome_141", v141::emulation),
55    Chrome142 => ("chrome_142", v142::emulation),
56    Chrome143 => ("chrome_143", v143::emulation),
57    Chrome144 => ("chrome_144", v144::emulation),
58    Chrome145 => ("chrome_145", v145::emulation),
59    Chrome146 => ("chrome_146", v146::emulation),
60    Chrome147 => ("chrome_147", v147::emulation),
61    Chrome148 => ("chrome_148", v148::emulation),
62
63    // Edge versions
64    Edge101 => ("edge_101", edge101::emulation),
65    Edge122 => ("edge_122", edge122::emulation),
66    Edge127 => ("edge_127", edge127::emulation),
67    Edge131 => ("edge_131", edge131::emulation),
68    Edge134 => ("edge_134", edge134::emulation),
69    Edge135 => ("edge_135", edge135::emulation),
70    Edge136 => ("edge_136", edge136::emulation),
71    Edge137 => ("edge_137", edge137::emulation),
72    Edge138 => ("edge_138", edge138::emulation),
73    Edge139 => ("edge_139", edge139::emulation),
74    Edge140 => ("edge_140", edge140::emulation),
75    Edge141 => ("edge_141", edge141::emulation),
76    Edge142 => ("edge_142", edge142::emulation),
77    Edge143 => ("edge_143", edge143::emulation),
78    Edge144 => ("edge_144", edge144::emulation),
79    Edge145 => ("edge_145", edge145::emulation),
80    Edge146 => ("edge_146", edge146::emulation),
81    Edge147 => ("edge_147", edge147::emulation),
82    Edge148 => ("edge_148", edge148::emulation),
83
84    // Opera versions
85    Opera116 => ("opera_116", opera116::emulation),
86    Opera117 => ("opera_117", opera117::emulation),
87    Opera118 => ("opera_118", opera118::emulation),
88    Opera119 => ("opera_119", opera119::emulation),
89    Opera120 => ("opera_120", opera120::emulation),
90    Opera121 => ("opera_121", opera121::emulation),
91    Opera122 => ("opera_122", opera122::emulation),
92    Opera123 => ("opera_123", opera123::emulation),
93    Opera124 => ("opera_124", opera124::emulation),
94    Opera125 => ("opera_125", opera125::emulation),
95    Opera126 => ("opera_126", opera126::emulation),
96    Opera127 => ("opera_127", opera127::emulation),
97    Opera128 => ("opera_128", opera128::emulation),
98    Opera129 => ("opera_129", opera129::emulation),
99    Opera130 => ("opera_130", opera130::emulation),
100    Opera131 => ("opera_131", opera131::emulation),
101
102    // Firefox versions
103    Firefox109 => ("firefox_109", ff109::emulation),
104    Firefox117 => ("firefox_117", ff117::emulation),
105    Firefox128 => ("firefox_128", ff128::emulation),
106    Firefox133 => ("firefox_133", ff133::emulation),
107    Firefox135 => ("firefox_135", ff135::emulation),
108    FirefoxPrivate135 => ("firefox_private_135", ff_private_135::emulation),
109    FirefoxAndroid135 => ("firefox_android_135", ff_android_135::emulation),
110    Firefox136 => ("firefox_136", ff136::emulation),
111    FirefoxPrivate136 => ("firefox_private_136", ff_private_136::emulation),
112    Firefox139 => ("firefox_139", ff139::emulation),
113    Firefox142 => ("firefox_142", ff142::emulation),
114    Firefox143 => ("firefox_143", ff143::emulation),
115    Firefox144 => ("firefox_144", ff144::emulation),
116    Firefox145 => ("firefox_145", ff145::emulation),
117    Firefox146 => ("firefox_146", ff146::emulation),
118    Firefox147 => ("firefox_147", ff147::emulation),
119    Firefox148 => ("firefox_148", ff148::emulation),
120    Firefox149 => ("firefox_149", ff149::emulation),
121    Firefox150 => ("firefox_150", ff150::emulation),
122    Firefox151 => ("firefox_151", ff151::emulation),
123
124    // Safari versions
125    SafariIos17_2 => ("safari_ios_17.2", safari_ios_17_2::emulation),
126    SafariIos17_4_1 => ("safari_ios_17.4.1", safari_ios_17_4_1::emulation),
127    SafariIos16_5 => ("safari_ios_16.5", safari_ios_16_5::emulation),
128    Safari15_3 => ("safari_15.3", safari15_3::emulation),
129    Safari15_5 => ("safari_15.5", safari15_5::emulation),
130    Safari15_6_1 => ("safari_15.6.1", safari15_6_1::emulation),
131    Safari16 => ("safari_16", safari16::emulation),
132    Safari16_5 => ("safari_16.5", safari16_5::emulation),
133    Safari17_0 => ("safari_17.0", safari17_0::emulation),
134    Safari17_2_1 => ("safari_17.2.1", safari17_2_1::emulation),
135    Safari17_4_1 => ("safari_17.4.1", safari17_4_1::emulation),
136    Safari17_5 => ("safari_17.5", safari17_5::emulation),
137    Safari17_6 => ("safari_17.6", safari17_6::emulation),
138    Safari18 => ("safari_18", safari18::emulation),
139    SafariIPad18 => ("safari_ipad_18", safari_ipad_18::emulation),
140    Safari18_2 => ("safari_18.2", safari18_2::emulation),
141    SafariIos18_1_1 => ("safari_ios_18.1.1", safari_ios_18_1_1::emulation),
142    Safari18_3 => ("safari_18.3", safari18_3::emulation),
143    Safari18_3_1 => ("safari_18.3.1", safari18_3_1::emulation),
144    Safari18_5 => ("safari_18.5", safari18_5::emulation),
145    Safari26 => ("safari_26", safari26::emulation),
146    Safari26_1 => ("safari_26.1", safari26_1::emulation),
147    Safari26_2 => ("safari_26.2", safari26_2::emulation),
148    Safari26_3 => ("safari_26.3", safari26_3::emulation),
149    Safari26_4 => ("safari_26.4", safari26_4::emulation),
150    SafariIPad26 => ("safari_ipad_26", safari_ipad_26::emulation),
151    SafariIpad26_2 => ("safari_ipad_26.2", safari_ipad_26_2::emulation),
152    SafariIos26 => ("safari_ios_26", safari_ios_26::emulation),
153    SafariIos26_2 => ("safari_ios_26.2", safari_ios_26_2::emulation),
154
155    // OkHttp versions
156    OkHttp3_9 => ("okhttp_3.9", okhttp3_9::emulation),
157    OkHttp3_11 => ("okhttp_3.11", okhttp3_11::emulation),
158    OkHttp3_13 => ("okhttp_3.13", okhttp3_13::emulation),
159    OkHttp3_14 => ("okhttp_3.14", okhttp3_14::emulation),
160    OkHttp4_9 => ("okhttp_4.9", okhttp4_9::emulation),
161    OkHttp4_10 => ("okhttp_4.10", okhttp4_10::emulation),
162    OkHttp4_12 => ("okhttp_4.12", okhttp4_12::emulation),
163    OkHttp5 => ("okhttp_5", okhttp5::emulation)
164
165);
166
167impl wreq::IntoEmulation for Profile {
168    #[inline]
169    fn into_emulation(self) -> wreq::Emulation {
170        Emulation::builder().profile(self).build().into_emulation()
171    }
172}
173
174define_enum!(
175    /// Selects which platform the client should look like.
176    ///
177    /// This mainly affects platform-specific headers and user-agent details.
178    /// In most cases you can keep the default unless you need to match a
179    /// specific Windows, macOS, Linux, Android, or iOS profile.
180    plain,
181    Platform, MacOS,
182    Windows => "windows",
183    MacOS => "macos",
184    Linux => "linux",
185    Android => "android",
186    IOS => "ios"
187);
188
189impl Platform {
190    #[inline]
191    const fn platform(&self) -> &'static str {
192        match self {
193            Platform::MacOS => "\"macOS\"",
194            Platform::Linux => "\"Linux\"",
195            Platform::Windows => "\"Windows\"",
196            Platform::Android => "\"Android\"",
197            Platform::IOS => "\"iOS\"",
198        }
199    }
200
201    #[inline]
202    const fn is_mobile(&self) -> bool {
203        matches!(self, Platform::Android | Platform::IOS)
204    }
205}
206
207/// Represents the configuration options for emulating a client profile and platform.
208///
209/// The `Emulation` struct allows you to configure various aspects of profile and platform
210/// emulation, including the profile, platform, and whether to enable certain features
211/// like HTTP/2 or headers.
212#[derive(Default, Clone, TypedBuilder)]
213pub struct Emulation {
214    /// Whether to change the profile (browser/okhttp) information.
215    #[builder(default)]
216    profile: Profile,
217
218    /// Whether to change the platform (Windows/macOS/Linux/Android/iOS) information.
219    #[builder(default)]
220    platform: Platform,
221
222    /// Whether to enable HTTP/2.
223    #[builder(default = true)]
224    http2: bool,
225
226    /// Whether to include default headers.
227    #[builder(default = true)]
228    headers: bool,
229}
230
231impl Emulation {
232    /// Returns a random variant of the `Profile` enum.
233    ///
234    /// # Examples
235    ///
236    /// ```
237    /// use wreq_util::Emulation;
238    ///
239    /// let random_emulation = Emulation::random();
240    /// println!("{:?}", random_emulation);
241    /// ```
242    pub fn random() -> Emulation {
243        let rand = crate::rand::fast_random();
244        Emulation::builder()
245            .profile(Profile::VARIANTS[(rand as usize) % Profile::VARIANTS.len()])
246            .platform(Platform::VARIANTS[((rand >> 32) as usize) % Platform::VARIANTS.len()])
247            .build()
248    }
249}
250
251impl wreq::IntoEmulation for Emulation {
252    #[inline]
253    fn into_emulation(self) -> wreq::Emulation {
254        self.profile.match_emulation(self)
255    }
256}