Skip to Content
DocumentationEdge-ComputingCloudflare Workers

Cloudflare Workers

Das Abfangen von Datenverkehr am Edge ist der effizienteste Weg, ProxyTracer zu nutzen. Durch die Implementierung dieser Middleware in einem Cloudflare Worker werden schädliche Anfragen direkt auf CDN-Ebene abgewiesen, bevor sie Serverressourcen verbrauchen.

Die Implementierung

Dieses Snippet extrahiert die tatsächliche Client-IP über native Cloudflare-Header, fragt die ProxyTracer-API ab und gibt bei Erkennung eines Proxys sofort 403 Forbidden zurück.

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); } };

Sicherheitshinweis: Speichern Sie Ihren PROXYTRACER_API_KEY immer als verschlüsseltes Secret im Cloudflare Worker-Dashboard und niemals als Klartext.

Last updated on