/** * Copyright 2026 aphrody-code * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /** * Détection automatique des anti-bot providers. * * Utilisé par Browser.newPage({ profile: "auto", escalate: true }) * pour décider d'escalader vers un profil plus stealth. * * Sources : * - https://developers.cloudflare.com/waf/managed-challenge/ * - https://techdocs.akamai.com/bot-manager/docs * - https://docs.datadome.co/docs/bot-detection */ import { isGoogleDomain } from "../google/index.ts"; import { detectSoftBlock, type SoftBlockProvider, type SoftBlockResult, } from "../profiles/soft-block.ts"; export { detectSoftBlock, type SoftBlockInput, type SoftBlockReason, type SoftBlockResult, } from "../profiles/soft-block.ts"; export type ChallengeProvider = | "cloudflare-block" | "cloudflare-challenge" | "cloudflare-turnstile" | "akamai-bot-manager" | "perimeterx" | "datadome" | "kasada" | "incapsula" | "aws-waf" | "sucuri" /** 200 with an empty client-side app shell: needs a JS-capable profile. */ | "js-required" /** Denial page or challenge refresh without an identifiable vendor. */ | "soft-block" | "rate-limit" | "none"; export interface ChallengeDetection { provider: ChallengeProvider; confidence: "high" | "medium" | "low"; recommendation: "static" | "fast" | "stealth" | "max" | "wait" | "abort"; evidence: string[]; } export interface ChallengeContext { url: string; status: number; headers: Headers; body: string; } export function detectChallenge(ctx: ChallengeContext): ChallengeDetection { const evidence: string[] = []; const headerEntries: [string, string][] = []; ctx.headers.forEach((v, k) => headerEntries.push([k.toLowerCase(), v])); const headers = new Map(headerEntries); const body = ctx.body.slice(0, 50_000); const status = ctx.status; // --- Cloudflare --- const cfMitigated = headers.get("cf-mitigated"); const cfRay = headers.get("cf-ray"); const cfServer = headers.get("server")?.toLowerCase().includes("cloudflare"); if ( cfMitigated === "challenge" || /__cf_chl_opt|cf_chl_jschl|turnstile/i.test(body) ) { evidence.push("cf-mitigated:challenge OR turnstile script detected"); return { provider: "cloudflare-turnstile", confidence: "high", recommendation: "max", evidence, }; } if (cfMitigated === "block" || (cfServer && status === 403)) { evidence.push(`cf block (status=${status}, mitigated=${cfMitigated})`); return { provider: "cloudflare-block", confidence: "high", recommendation: "stealth", evidence, }; } if (cfRay && status >= 400 && status < 600) { evidence.push(`cf-ray=${cfRay} status=${status}`); return { provider: "cloudflare-challenge", confidence: "medium", recommendation: "stealth", evidence, }; } // --- Akamai Bot Manager --- if ( /]*id=["']datadome["']/i.test(body) || headers.get("x-dd-b") || /_dd[a-z]?=/.test(headers.get("set-cookie") ?? "") ) { evidence.push("datadome script/cookie/header"); return { provider: "datadome", confidence: "high", recommendation: "max", evidence, }; } // --- PerimeterX --- if ( /_pxhd=|_px[0-9]?=/.test(headers.get("set-cookie") ?? "") || /\/_px\//i.test(body) ) { evidence.push("perimeterx cookie/url"); return { provider: "perimeterx", confidence: "high", recommendation: "max", evidence, }; } // --- Kasada --- if ( /kasada|x-kpsdk-/.test(body) || headers.get("x-kpsdk-ct") || headers.get("x-kpsdk-st") ) { evidence.push("kasada markers"); return { provider: "kasada", confidence: "high", recommendation: "max", evidence, }; } // --- Imperva / Incapsula --- if ( /visid_incap|incap_ses/.test(headers.get("set-cookie") ?? "") || /X-Iinfo|X-CDN: Incapsula/i.test( Array.from(headers) .map(([k, v]) => `${k}: ${v}`) .join("\n"), ) ) { evidence.push("incapsula cookie/header"); return { provider: "incapsula", confidence: "high", recommendation: "stealth", evidence, }; } // --- Rate limit (any provider) --- if (status === 429) { evidence.push("status=429"); return { provider: "rate-limit", confidence: "high", recommendation: "wait", evidence, }; } // --- Soft blocks (vendor interstitials, JS shells, denial pages) --- const soft = detectSoftBlock({ url: ctx.url, status, headers: ctx.headers, html: ctx.body, }); if (soft.blocked) { return softBlockDetection(soft); } // --- No challenge detected --- return { provider: "none", confidence: "high", recommendation: "fast", evidence: ["no challenge markers found"], }; } const SOFT_BLOCK_PROVIDERS: Record< SoftBlockProvider, Pick > = { cloudflare: { provider: "cloudflare-challenge", recommendation: "max" }, akamai: { provider: "akamai-bot-manager", recommendation: "max" }, perimeterx: { provider: "perimeterx", recommendation: "max" }, datadome: { provider: "datadome", recommendation: "max" }, kasada: { provider: "kasada", recommendation: "max" }, imperva: { provider: "incapsula", recommendation: "stealth" }, "aws-waf": { provider: "aws-waf", recommendation: "stealth" }, sucuri: { provider: "sucuri", recommendation: "stealth" }, unknown: { provider: "soft-block", recommendation: "stealth" }, }; function softBlockDetection(soft: SoftBlockResult): ChallengeDetection { const mapped = soft.reason === "js_required" ? { provider: "js-required" as const, recommendation: "fast" as const } : SOFT_BLOCK_PROVIDERS[soft.provider ?? "unknown"]; return { ...mapped, confidence: soft.confidence >= 0.85 ? "high" : soft.confidence >= 0.65 ? "medium" : "low", evidence: [`soft-block:${soft.reason}`, ...(soft.evidence ? [soft.evidence] : [])], }; } /** Heuristique pour décider du profil initial sans avoir requête d'abord. */ export function suggestInitialProfile( url: string, ): "static" | "fast" | "stealth" | "max" { const u = new URL(url); const host = u.hostname.toLowerCase(); // Spécialisation Google if (isGoogleDomain(host)) { if (host.includes("gemini.google") || host.includes("geminicli.com")) { return "max"; } return "fast"; } // Liste curated de domaines connus pour Cloudflare full / Akamai const knownHardTargets = [ "linkedin.com", "twitter.com", "x.com", "facebook.com", "instagram.com", "tiktok.com", "amazon.com", "walmart.com", "bestbuy.com", "target.com", "nike.com", "adidas.com", "ticketmaster.com", "stubhub.com", "booking.com", "airbnb.com", "expedia.com", ]; if (knownHardTargets.some((d) => host === d || host.endsWith(`.${d}`))) { return "max"; } // Domaines connus simples const knownSimple = [ "google.com", "www.google.com", "developers.google.com", "design.google", ]; if (knownSimple.some((d) => host === d || host.endsWith(`.${d}`))) { return "fast"; } return "fast"; // par défaut, on tente fast et on escalade si challenge }