Skip to Content
DocumentationCalcul EdgeCloudflare Workers

Cloudflare Workers

L'interception du trafic à l'Edge constitue la méthode la plus efficace pour exploiter ProxyTracer. En intégrant ce middleware au sein d'un Cloudflare Worker, les requêtes malveillantes sont rejetées directement au niveau du CDN avant d'exploiter les ressources de votre serveur d'origine.

L'Implémentation

Ce code extrait la véritable adresse IP du client en utilisant les en-têtes natifs de Cloudflare, interroge l'API ProxyTracer et renvoie un strict 403 Forbidden si un proxy est détecté.

export default { async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> { // 1. Extract the real IP (Cloudflare natively provides this) const ip = request.headers.get("cf-connecting-ip"); // Pass through if no IP is found (e.g., local development) if (!ip) { return fetch(request); } try { // 2. Query the ProxyTracer API const ptResponse = await fetch(`https://api.proxytracer.com/v1/check/${ip}`, { method: "GET", headers: { "Authorization": `Bearer ${env.PROXYTRACER_API_KEY}` } }); if (ptResponse.ok) { const data = await ptResponse.json(); // 3. Drop the connection if a proxy/VPN is detected if (data.proxy === true) { return new Response( JSON.stringify({ error: "Access Denied: VPN or Proxy detected." }), { status: 403, headers: { "Content-Type": "application/json" } } ); } } } catch (error) { // Fail open: If the API is unreachable, allow traffic to ensure uptime console.error("ProxyTracer API Error:", error); } // 4. Clean traffic proceeds to your origin server return fetch(request); } };

Note de Sécurité : Stockez systématiquement votre PROXYTRACER_API_KEY sous forme de Secret chiffré dans le tableau de bord Cloudflare Worker, jamais en texte clair.

Last updated on