stygian-proxy 0.16.0

High-performance, resilient proxy rotation for the Stygian scraping ecosystem.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
//! TLS-profiled HTTP client for the proxy health checker and fetcher.
//!
//! Enabled by the `tls-profiled` feature flag. Wraps
//! [`stygian_browser::tls::build_profiled_client_preset`] so that outgoing plain-HTTP
//! requests (health checks, proxy-list fetches) present the same TLS
//! fingerprint and HTTP header set as a real browser, reducing the chance that
//! the target blocks or fingerprints the checker itself.
//!
//! # Architecture
//!
//! ```text
//! ProxyManager / HealthChecker / FreeListFetcher
//!//!         ├── (default) vanilla reqwest::Client
//!//!         └── (tls-profiled feature) ProfiledRequester
//!//!                 └── reqwest::Client built from stygian_browser::TlsProfile
//!                         ├── TLS: cipher-suite order, ALPN, kx groups
//!                         ├── User-Agent matched to browser
//!                         └── Accept / Sec-CH-UA / sec-fetch-* headers
//! ```
//!
//! # Example
//!
//! ```no_run
//! use stygian_proxy::http_client::{ProfiledRequestMode, ProfiledRequester};
//!
//! # fn run() -> Result<(), Box<dyn std::error::Error>> {
//! let requester = ProfiledRequester::chrome_mode(ProfiledRequestMode::Preset)?;
//! let client = requester.client();
//! # Ok(())
//! # }
//! ```

use stygian_browser::tls::{
    CHROME_131, EDGE_131, FIREFOX_133, SAFARI_18, TlsControl, TlsProfile,
    build_profiled_client_preset, build_profiled_client_with_control,
};
use thiserror::Error;

pub use crate::types::ProfiledRequestMode;

// ─── error ───────────────────────────────────────────────────────────────────

/// Errors that can occur when building a [`ProfiledRequester`].
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum ProfiledRequesterError {
    /// The underlying TLS-profiled client could not be constructed.
    #[error("failed to build TLS-profiled client: {0}")]
    Build(#[from] stygian_browser::tls::TlsClientError),
}

// ─── ProfiledRequester ────────────────────────────────────────────────────────

/// A [`reqwest::Client`] pre-configured with a browser TLS fingerprint and
/// matching HTTP headers.
///
/// Use [`ProfiledRequester::chrome`], [`ProfiledRequester::firefox`],
/// [`ProfiledRequester::safari`], or [`ProfiledRequester::edge`] for
/// built-in profiles, or supply any [`TlsProfile`] via
/// [`ProfiledRequester::from_profile`].
///
/// The held `reqwest::Client` is cheap to clone (it is `Arc`-backed internally)
/// so `ProfiledRequester` itself implements `Clone`.
///
/// # Example
///
/// ```no_run
/// use stygian_proxy::http_client::ProfiledRequester;
///
/// # fn run() -> Result<(), Box<dyn std::error::Error>> {
/// let requester = ProfiledRequester::chrome()?;
///
/// // Pass a proxy URL to route requests through it.
/// let requester_via_proxy = ProfiledRequester::from_profile(&stygian_browser::tls::CHROME_131, Some("http://10.0.0.1:8080"))?;
/// # Ok(())
/// # }
/// ```
#[derive(Clone, Debug)]
pub struct ProfiledRequester {
    client: reqwest::Client,
    /// Static TLS profile this requester was built from.
    profile: &'static TlsProfile,
}

impl ProfiledRequester {
    /// Build from any static [`TlsProfile`].
    ///
    /// Pass `proxy_url` to route all requests through a proxy.
    ///
    /// # Errors
    ///
    /// Returns [`ProfiledRequesterError::Build`] if the TLS config or HTTP
    /// client cannot be constructed.
    pub fn from_profile(
        profile: &'static TlsProfile,
        proxy_url: Option<&str>,
    ) -> Result<Self, ProfiledRequesterError> {
        let client = build_profiled_client_preset(profile, proxy_url)?;
        Ok(Self { client, profile })
    }

    /// Build from any static [`TlsProfile`] using a selectable mode.
    ///
    /// This is the recommended high-level constructor when callers want to
    /// select compatibility behavior with a single parameter.
    ///
    /// # Errors
    ///
    /// Returns [`ProfiledRequesterError::Build`] if the TLS config or HTTP
    /// client cannot be constructed.
    pub fn from_profile_mode(
        profile: &'static TlsProfile,
        proxy_url: Option<&str>,
        mode: ProfiledRequestMode,
    ) -> Result<Self, ProfiledRequesterError> {
        let client = match mode {
            ProfiledRequestMode::Compatible => {
                build_profiled_client_with_control(profile, proxy_url, TlsControl::compatible())?
            }
            ProfiledRequestMode::Preset => build_profiled_client_preset(profile, proxy_url)?,
            ProfiledRequestMode::Strict => {
                build_profiled_client_with_control(profile, proxy_url, TlsControl::strict())?
            }
            ProfiledRequestMode::StrictAll => {
                build_profiled_client_with_control(profile, proxy_url, TlsControl::strict_all())?
            }
        };
        Ok(Self { client, profile })
    }

    /// Build from any static [`TlsProfile`] with an explicit [`TlsControl`].
    ///
    /// Use this constructor when you need deterministic compatibility/strict
    /// behavior independent of profile-name presets.
    ///
    /// # Errors
    ///
    /// Returns [`ProfiledRequesterError::Build`] if the TLS config or HTTP
    /// client cannot be constructed.
    pub fn from_profile_with_control(
        profile: &'static TlsProfile,
        proxy_url: Option<&str>,
        control: TlsControl,
    ) -> Result<Self, ProfiledRequesterError> {
        let client = build_profiled_client_with_control(profile, proxy_url, control)?;
        Ok(Self { client, profile })
    }

    /// Build from any static [`TlsProfile`] in compatible mode.
    ///
    /// Equivalent to calling [`Self::from_profile_with_control`] with
    /// [`TlsControl::compatible`].
    ///
    /// # Errors
    ///
    /// Returns [`ProfiledRequesterError::Build`] on construction failure.
    pub fn from_profile_compatible(
        profile: &'static TlsProfile,
        proxy_url: Option<&str>,
    ) -> Result<Self, ProfiledRequesterError> {
        Self::from_profile_mode(profile, proxy_url, ProfiledRequestMode::Compatible)
    }

    /// Build from any static [`TlsProfile`] in strict mode.
    ///
    /// Equivalent to calling [`Self::from_profile_with_control`] with
    /// [`TlsControl::strict`].
    ///
    /// # Errors
    ///
    /// Returns [`ProfiledRequesterError::Build`] on construction failure.
    pub fn from_profile_strict(
        profile: &'static TlsProfile,
        proxy_url: Option<&str>,
    ) -> Result<Self, ProfiledRequesterError> {
        Self::from_profile_mode(profile, proxy_url, ProfiledRequestMode::Strict)
    }

    /// Build from any static [`TlsProfile`] in strict-all mode.
    ///
    /// Equivalent to calling [`Self::from_profile_with_control`] with
    /// [`TlsControl::strict_all`].
    ///
    /// # Errors
    ///
    /// Returns [`ProfiledRequesterError::Build`] on construction failure.
    pub fn from_profile_strict_all(
        profile: &'static TlsProfile,
        proxy_url: Option<&str>,
    ) -> Result<Self, ProfiledRequesterError> {
        Self::from_profile_mode(profile, proxy_url, ProfiledRequestMode::StrictAll)
    }

    /// Build a Chrome 131-profiled requester.
    ///
    /// # Errors
    ///
    /// Returns [`ProfiledRequesterError::Build`] on construction failure.
    pub fn chrome() -> Result<Self, ProfiledRequesterError> {
        Self::from_profile(&CHROME_131, None)
    }

    /// Build a Chrome 131-profiled requester with the selected mode.
    ///
    /// # Errors
    ///
    /// Returns [`ProfiledRequesterError::Build`] on construction failure.
    pub fn chrome_mode(mode: ProfiledRequestMode) -> Result<Self, ProfiledRequesterError> {
        Self::from_profile_mode(&CHROME_131, None, mode)
    }

    /// Build a Firefox 133-profiled requester.
    ///
    /// # Errors
    ///
    /// Returns [`ProfiledRequesterError::Build`] on construction failure.
    pub fn firefox() -> Result<Self, ProfiledRequesterError> {
        Self::from_profile(&FIREFOX_133, None)
    }

    /// Build a Firefox 133-profiled requester with the selected mode.
    ///
    /// # Errors
    ///
    /// Returns [`ProfiledRequesterError::Build`] on construction failure.
    pub fn firefox_mode(mode: ProfiledRequestMode) -> Result<Self, ProfiledRequesterError> {
        Self::from_profile_mode(&FIREFOX_133, None, mode)
    }

    /// Build a Safari 18-profiled requester.
    ///
    /// # Errors
    ///
    /// Returns [`ProfiledRequesterError::Build`] on construction failure.
    pub fn safari() -> Result<Self, ProfiledRequesterError> {
        Self::from_profile(&SAFARI_18, None)
    }

    /// Build a Safari 18-profiled requester with the selected mode.
    ///
    /// # Errors
    ///
    /// Returns [`ProfiledRequesterError::Build`] on construction failure.
    pub fn safari_mode(mode: ProfiledRequestMode) -> Result<Self, ProfiledRequesterError> {
        Self::from_profile_mode(&SAFARI_18, None, mode)
    }

    /// Build an Edge 131-profiled requester.
    ///
    /// # Errors
    ///
    /// Returns [`ProfiledRequesterError::Build`] on construction failure.
    pub fn edge() -> Result<Self, ProfiledRequesterError> {
        Self::from_profile(&EDGE_131, None)
    }

    /// Build an Edge 131-profiled requester with the selected mode.
    ///
    /// # Errors
    ///
    /// Returns [`ProfiledRequesterError::Build`] on construction failure.
    pub fn edge_mode(mode: ProfiledRequestMode) -> Result<Self, ProfiledRequesterError> {
        Self::from_profile_mode(&EDGE_131, None, mode)
    }

    /// Build a requester using a profile weighted by real-world browser market
    /// share (see [`TlsProfile::random_weighted`]).
    ///
    /// `seed` should differ across callers to get varied profiles.
    ///
    /// # Errors
    ///
    /// Returns [`ProfiledRequesterError::Build`] on construction failure.
    pub fn random_weighted(seed: u64) -> Result<Self, ProfiledRequesterError> {
        let profile = TlsProfile::random_weighted(seed);
        let client = build_profiled_client_preset(profile, None)?;
        Ok(Self { client, profile })
    }

    /// Build a weighted-random profile requester with the selected mode.
    ///
    /// `seed` should differ across callers to get varied profiles.
    ///
    /// # Errors
    ///
    /// Returns [`ProfiledRequesterError::Build`] on construction failure.
    pub fn random_weighted_mode(
        seed: u64,
        mode: ProfiledRequestMode,
    ) -> Result<Self, ProfiledRequesterError> {
        let profile = TlsProfile::random_weighted(seed);
        Self::from_profile_mode(profile, None, mode)
    }

    /// Borrow the underlying [`reqwest::Client`].
    #[must_use]
    pub const fn client(&self) -> &reqwest::Client {
        &self.client
    }

    /// Consume the requester and return the underlying [`reqwest::Client`].
    #[must_use]
    pub fn into_client(self) -> reqwest::Client {
        self.client
    }

    /// Return the static [`TlsProfile`] this requester was built from.
    #[must_use]
    pub const fn profile(&self) -> &'static TlsProfile {
        self.profile
    }

    /// The human-readable name of the TLS profile in use.
    #[must_use]
    pub fn profile_name(&self) -> &str {
        &self.profile.name
    }

    /// Return `true` if the profile negotiates HTTP/2 (h2 in ALPN).
    ///
    /// This is always `true` for the built-in Chrome, Firefox, Edge, and
    /// Safari profiles.
    #[must_use]
    pub fn supports_h2(&self) -> bool {
        // We can't query reqwest's ALPN config after construction, so we
        // derive it from the profile name as a best-effort hint. All four
        // built-in profiles include H2.
        !self.profile.name.contains("HTTP/1.1-only")
    }
}

// ─── tests ───────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn chrome_requester_builds() -> std::result::Result<(), Box<dyn std::error::Error>> {
        let r = ProfiledRequester::chrome()?;
        assert_eq!(r.profile_name(), "Chrome 131");
        Ok(())
    }

    #[test]
    fn chrome_mode_preset_builds() -> std::result::Result<(), Box<dyn std::error::Error>> {
        let r = ProfiledRequester::chrome_mode(ProfiledRequestMode::Preset)?;
        assert_eq!(r.profile_name(), "Chrome 131");
        Ok(())
    }

    #[test]
    fn firefox_requester_builds() -> std::result::Result<(), Box<dyn std::error::Error>> {
        let r = ProfiledRequester::firefox()?;
        assert_eq!(r.profile_name(), "Firefox 133");
        Ok(())
    }

    #[test]
    fn safari_requester_builds() -> std::result::Result<(), Box<dyn std::error::Error>> {
        let r = ProfiledRequester::safari()?;
        assert_eq!(r.profile_name(), "Safari 18");
        Ok(())
    }

    #[test]
    fn edge_requester_builds() -> std::result::Result<(), Box<dyn std::error::Error>> {
        let r = ProfiledRequester::edge()?;
        assert_eq!(r.profile_name(), "Edge 131");
        Ok(())
    }

    #[test]
    fn random_weighted_requester_varies() -> std::result::Result<(), Box<dyn std::error::Error>> {
        let a = ProfiledRequester::random_weighted(1)?;
        let b = ProfiledRequester::random_weighted(999_999)?;
        // Not guaranteed to differ, but the distribution should produce at
        // least two distinct profiles across a wider seed range.
        let _ = (a.profile_name(), b.profile_name()); // just ensure no panic
        Ok(())
    }

    #[test]
    fn random_weighted_mode_compatible_builds()
    -> std::result::Result<(), Box<dyn std::error::Error>> {
        let r = ProfiledRequester::random_weighted_mode(42, ProfiledRequestMode::Compatible)?;
        assert!(!r.profile_name().is_empty());
        Ok(())
    }

    #[test]
    fn from_profile_with_custom_gives_correct_name()
    -> std::result::Result<(), Box<dyn std::error::Error>> {
        let r = ProfiledRequester::from_profile(&CHROME_131, None)?;
        assert_eq!(r.profile_name(), "Chrome 131");
        Ok(())
    }

    #[test]
    fn from_profile_with_control_gives_correct_name()
    -> std::result::Result<(), Box<dyn std::error::Error>> {
        let r = ProfiledRequester::from_profile_with_control(
            &CHROME_131,
            None,
            TlsControl::compatible(),
        )?;
        assert_eq!(r.profile_name(), "Chrome 131");
        Ok(())
    }

    #[test]
    fn from_profile_mode_preset_gives_correct_name()
    -> std::result::Result<(), Box<dyn std::error::Error>> {
        let r =
            ProfiledRequester::from_profile_mode(&CHROME_131, None, ProfiledRequestMode::Preset)?;
        assert_eq!(r.profile_name(), "Chrome 131");
        Ok(())
    }

    #[test]
    fn from_profile_mode_compatible_gives_correct_name()
    -> std::result::Result<(), Box<dyn std::error::Error>> {
        let r = ProfiledRequester::from_profile_mode(
            &CHROME_131,
            None,
            ProfiledRequestMode::Compatible,
        )?;
        assert_eq!(r.profile_name(), "Chrome 131");
        Ok(())
    }

    #[test]
    fn strict_all_constructor_reports_unsupported_group_for_chrome()
    -> std::result::Result<(), Box<dyn std::error::Error>> {
        let err = ProfiledRequester::from_profile_strict_all(&CHROME_131, None)
            .err()
            .ok_or_else(|| {
                std::io::Error::other("strict_all should fail if a profile group is unsupported")
            })?;
        let msg = err.to_string();
        assert!(msg.contains("unsupported supported_group"), "{msg}");
        Ok(())
    }

    #[test]
    fn clone_is_shallow() -> std::result::Result<(), Box<dyn std::error::Error>> {
        let r = ProfiledRequester::chrome()?;
        let r2 = r.clone();
        assert_eq!(r.profile_name(), r2.profile_name());
        Ok(())
    }
}