adler-core 0.10.0

Core engine for the Adler OSINT username-search tool.
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
//! Transport layer: how a single probe actually reaches a site.
//!
//! [`Client`](crate::Client) is the *router* — it owns cross-cutting
//! concerns (regex gate, robots, throttle, browser budget, retry) and
//! signal evaluation. A [`Fetcher`] owns only the transport: given a
//! [`FetchRequest`], produce a normalised [`FetchResponse`] (or a
//! [`FetchError`] carrying the [`UncertainReason`] the outcome should
//! report).
//!
//! Phase 1 ships two transports — [`HttpFetcher`] (raw `reqwest`, the
//! default) and [`BrowserFetcher`] (adapts a
//! [`BrowserBackend`](crate::browser::BrowserBackend)). The seam exists
//! so later phases can add fingerprint-impersonating and
//! challenge-solving transports, and an egress (proxy) dimension,
//! without growing the router into a monster.

use std::collections::BTreeMap;
use std::sync::Arc;
use std::time::Duration;

use async_trait::async_trait;

use crate::ban;
use crate::browser::BrowserBackend;
use crate::check::UncertainReason;
use crate::site::HttpMethod;

/// Per-fetch timeout for the browser transport. Browser fetches (JS
/// execution + waits) are inherently slower than raw HTTP, so this is
/// generous on purpose. Also used by `Client::fetch_for_doctor`.
pub(crate) const BROWSER_TIMEOUT: Duration = Duration::from_secs(60);

/// Everything a fetcher needs for one request. A superset: each
/// transport reads the subset it cares about (e.g. `want_body` and
/// `method` are HTTP-only; the browser transport uses `url` + `headers`).
pub(crate) struct FetchRequest<'a> {
    pub method: HttpMethod,
    pub url: &'a str,
    /// POST body (already `{username}`-substituted). HTTP-only.
    pub body: Option<&'a str>,
    /// Resolved User-Agent for this request (rotation handled upstream).
    pub user_agent: Option<&'a str>,
    /// Per-site extra headers. Applied by the browser transport today;
    /// the HTTP transport does not apply them yet (see Phase 2).
    pub headers: &'a BTreeMap<String, String>,
    /// Whether the response body is needed (signals / enrichment). When
    /// `false`, the HTTP transport may issue a HEAD.
    pub want_body: bool,
}

/// Normalised response, transport-agnostic. `body` is empty when the
/// caller didn't request it (HTTP HEAD path).
pub(crate) struct FetchResponse {
    pub status: u16,
    pub final_url: String,
    pub body: String,
}

/// A fetch that didn't yield a usable response. Wraps the
/// [`UncertainReason`] the resulting [`CheckOutcome`](crate::CheckOutcome)
/// should carry, so the router maps every error uniformly to
/// `Uncertain(reason)` — preserving the exact reason taxonomy the raw
/// HTTP / browser paths produced before this seam existed.
pub(crate) struct FetchError(pub UncertainReason);

#[async_trait]
pub(crate) trait Fetcher: Send + Sync {
    async fn fetch(&self, req: &FetchRequest<'_>) -> Result<FetchResponse, FetchError>;
}

/// Raw-HTTP transport over a `reqwest::Client`. Owns the client so a
/// later egress-pool phase can hold one fetcher per proxy.
pub(crate) struct HttpFetcher {
    inner: reqwest::Client,
}

impl HttpFetcher {
    pub(crate) fn new(inner: reqwest::Client) -> Self {
        Self { inner }
    }

    /// Borrow the underlying client for non-probe diagnostics
    /// (`Client::fetch`).
    pub(crate) fn client(&self) -> &reqwest::Client {
        &self.inner
    }
}

#[async_trait]
impl Fetcher for HttpFetcher {
    async fn fetch(&self, req: &FetchRequest<'_>) -> Result<FetchResponse, FetchError> {
        // Method dispatch mirrors the pre-seam probe path: POST always
        // POST (carries the username in its body); GET reads the body
        // only when needed, otherwise HEAD with a transparent 405→GET
        // retry (some servers reject HEAD).
        let sent = match req.method {
            HttpMethod::Post => {
                send(
                    &self.inner,
                    reqwest::Method::POST,
                    req.url,
                    req.user_agent,
                    req.headers,
                    req.body,
                )
                .await
            }
            HttpMethod::Get if req.want_body => {
                send(
                    &self.inner,
                    reqwest::Method::GET,
                    req.url,
                    req.user_agent,
                    req.headers,
                    None,
                )
                .await
            }
            HttpMethod::Get => {
                match send(
                    &self.inner,
                    reqwest::Method::HEAD,
                    req.url,
                    req.user_agent,
                    req.headers,
                    None,
                )
                .await
                {
                    Ok(r) if r.status().as_u16() == 405 => {
                        send(
                            &self.inner,
                            reqwest::Method::GET,
                            req.url,
                            req.user_agent,
                            req.headers,
                            None,
                        )
                        .await
                    }
                    other => other,
                }
            }
        };

        let response = match sent {
            Ok(r) => r,
            Err(err) => {
                tracing::debug!(url = %req.url, error = %err, "request failed");
                return Err(FetchError(UncertainReason::Network(err.to_string())));
            }
        };

        let status = response.status().as_u16();
        let final_url = response.url().to_string();

        if let Some(reason) = ban::detect_pre_body(status, response.headers()) {
            tracing::warn!(url = %req.url, status, %reason, "ban-like response");
            return Err(FetchError(reason));
        }

        let body = if req.want_body {
            match response.text().await {
                Ok(b) => b,
                Err(err) => return Err(FetchError(UncertainReason::BodyRead(err.to_string()))),
            }
        } else {
            String::new()
        };

        if !body.is_empty() {
            if let Some(reason) = ban::detect_in_body(&body) {
                tracing::warn!(url = %req.url, %reason, "ban-like body");
                return Err(FetchError(reason));
            }
        }

        Ok(FetchResponse {
            status,
            final_url,
            body,
        })
    }
}

/// Browser transport: renders through a
/// [`BrowserBackend`](crate::browser::BrowserBackend) and normalises the
/// [`RenderedPage`](crate::browser::RenderedPage). Uses only `url` and
/// `headers` from the request (timeout is the fixed `BROWSER_TIMEOUT`) —
/// the backend always returns a full body and manages its own method /
/// User-Agent.
pub(crate) struct BrowserFetcher {
    backend: Arc<dyn BrowserBackend>,
}

impl BrowserFetcher {
    pub(crate) fn new(backend: Arc<dyn BrowserBackend>) -> Self {
        Self { backend }
    }
}

#[async_trait]
impl Fetcher for BrowserFetcher {
    async fn fetch(&self, req: &FetchRequest<'_>) -> Result<FetchResponse, FetchError> {
        let parsed = match url::Url::parse(req.url) {
            Ok(u) => u,
            Err(err) => {
                return Err(FetchError(UncertainReason::Other(format!(
                    "invalid url: {err}"
                ))));
            }
        };
        match self
            .backend
            .fetch(&parsed, req.headers, BROWSER_TIMEOUT)
            .await
        {
            Ok(page) => Ok(FetchResponse {
                status: page.status,
                final_url: page.final_url.as_str().to_owned(),
                body: page.body,
            }),
            Err(err) => {
                tracing::warn!(url = %req.url, error = %err, "browser fetch failed");
                Err(FetchError(UncertainReason::BrowserFailed(err.to_string())))
            }
        }
    }
}

/// Issue one request, applying the per-site / session `headers` and an
/// optional User-Agent override and body. A `User-Agent` in `headers`
/// wins over `ua`; a `Content-Type` in `headers` wins over the POST
/// default of `application/json`.
async fn send(
    client: &reqwest::Client,
    method: reqwest::Method,
    url: &str,
    ua: Option<&str>,
    headers: &BTreeMap<String, String>,
    body: Option<&str>,
) -> reqwest::Result<reqwest::Response> {
    let mut request = client.request(method, url);
    let has = |name: &str| headers.keys().any(|k| k.eq_ignore_ascii_case(name));
    // Rotation/default UA only when the headers don't set their own.
    if let Some(ua) = ua {
        if !has("user-agent") {
            request = request.header(reqwest::header::USER_AGENT, ua);
        }
    }
    for (k, v) in headers {
        request = request.header(k, v);
    }
    if let Some(b) = body {
        if !has("content-type") {
            request = request.header(reqwest::header::CONTENT_TYPE, "application/json");
        }
        request = request.body(b.to_owned());
    }
    request.send().await
}

#[cfg(feature = "impersonate")]
pub(crate) use impersonate::ImpersonateFetcher;

/// Browser-impersonating HTTP transport (`wreq` + `BoringSSL`), gated by
/// the `impersonate` Cargo feature. A site whose protection list is
/// *only* [`ProtectionKind::TlsFingerprint`](crate::ProtectionKind) is
/// routed here instead of the heavyweight browser backend — much
/// cheaper, since a real TLS handshake from `wreq` matches Chrome's
/// JA3/JA4 fingerprint without launching a browser process.
#[cfg(feature = "impersonate")]
mod impersonate {
    use super::{
        FetchError, FetchRequest, FetchResponse, Fetcher, HttpMethod, UncertainReason, ban,
    };
    use async_trait::async_trait;
    use std::collections::BTreeMap;

    /// Chrome version we impersonate. Picked from
    /// [`wreq_util::Emulation`]; bump as Chrome moves so the JA3/JA4
    /// fingerprint stays "current Chrome" — old emulations get filtered
    /// out by WAFs over time.
    const EMULATION: wreq_util::Emulation = wreq_util::Emulation::Chrome134;

    pub(crate) struct ImpersonateFetcher {
        inner: wreq::Client,
    }

    impl ImpersonateFetcher {
        pub(crate) fn new() -> crate::error::Result<Self> {
            let inner = wreq::Client::builder()
                .emulation(EMULATION)
                .build()
                .map_err(|e| crate::error::Error::HttpSetup {
                    message: format!("wreq client init: {e}"),
                })?;
            Ok(Self { inner })
        }
    }

    #[async_trait]
    impl Fetcher for ImpersonateFetcher {
        async fn fetch(&self, req: &FetchRequest<'_>) -> Result<FetchResponse, FetchError> {
            // Method dispatch mirrors `HttpFetcher`: POST always POST;
            // GET reads the body only when needed, otherwise HEAD with
            // a transparent 405 → GET retry.
            let sent = match req.method {
                HttpMethod::Post => {
                    send(
                        &self.inner,
                        wreq::Method::POST,
                        req.url,
                        req.user_agent,
                        req.headers,
                        req.body,
                    )
                    .await
                }
                HttpMethod::Get if req.want_body => {
                    send(
                        &self.inner,
                        wreq::Method::GET,
                        req.url,
                        req.user_agent,
                        req.headers,
                        None,
                    )
                    .await
                }
                HttpMethod::Get => {
                    match send(
                        &self.inner,
                        wreq::Method::HEAD,
                        req.url,
                        req.user_agent,
                        req.headers,
                        None,
                    )
                    .await
                    {
                        Ok(r) if r.status().as_u16() == 405 => {
                            send(
                                &self.inner,
                                wreq::Method::GET,
                                req.url,
                                req.user_agent,
                                req.headers,
                                None,
                            )
                            .await
                        }
                        other => other,
                    }
                }
            };

            let response = match sent {
                Ok(r) => r,
                Err(err) => {
                    tracing::debug!(url = %req.url, error = %err, "impersonate request failed");
                    return Err(FetchError(UncertainReason::Network(err.to_string())));
                }
            };

            let status = response.status().as_u16();
            let final_url = response.url().to_string();

            if let Some(reason) = ban::detect_pre_body(status, response.headers()) {
                tracing::warn!(url = %req.url, status, %reason, "ban-like response");
                return Err(FetchError(reason));
            }

            let body = if req.want_body {
                match response.text().await {
                    Ok(b) => b,
                    Err(err) => {
                        return Err(FetchError(UncertainReason::BodyRead(err.to_string())));
                    }
                }
            } else {
                String::new()
            };

            if !body.is_empty() {
                if let Some(reason) = ban::detect_in_body(&body) {
                    tracing::warn!(url = %req.url, %reason, "ban-like body");
                    return Err(FetchError(reason));
                }
            }

            Ok(FetchResponse {
                status,
                final_url,
                body,
            })
        }
    }

    async fn send(
        client: &wreq::Client,
        method: wreq::Method,
        url: &str,
        ua: Option<&str>,
        headers: &BTreeMap<String, String>,
        body: Option<&str>,
    ) -> wreq::Result<wreq::Response> {
        let mut request = client.request(method, url);
        let has = |name: &str| headers.keys().any(|k| k.eq_ignore_ascii_case(name));
        // wreq's emulation already sets a Chrome User-Agent on every
        // request; only override when we have a rotation UA AND the
        // caller hasn't put their own UA in headers.
        if let Some(ua) = ua {
            if !has("user-agent") {
                request = request.header(wreq::header::USER_AGENT, ua);
            }
        }
        for (k, v) in headers {
            request = request.header(k, v);
        }
        if let Some(b) = body {
            if !has("content-type") {
                request = request.header(wreq::header::CONTENT_TYPE, "application/json");
            }
            request = request.body(b.to_owned());
        }
        request.send().await
    }
}