Skip to Content
DocumentationEdge-ComputingAWS Lambda@Edge

AWS Lambda@Edge

Das Abfangen von Datenverkehr auf CDN-Ebene mittels AWS Lambda@Edge ist die sicherste und kostengünstigste Methode zur Nutzung von ProxyTracer. Indem bösartige Anfragen direkt am AWS-Edge blockiert werden, schützen Sie Ihre Ursprungsserver vollständig vor Botnetz-Verkehr und sparen Rechenkosten.

Die Implementierung

Diese Funktion hakt sich in das CloudFront Viewer Request-Ereignis ein. Sie liest die Client-IP direkt aus dem Ereignisobjekt aus, führt eine schnelle asynchrone Abfrage an ProxyTracer durch und gibt bei Erkennung eines Proxys direkt eine maßgeschneiderte 403 Forbidden-Antwort an den Nutzer zurück.

export const handler = async (event) => { const request = event.Records[0].cf.request; // 1. Safely extract the client IP directly from CloudFront const clientIp = request.clientIp; // Pass through if local testing or no IP found if (!clientIp) { return request; } try { // 2. Query ProxyTracer API (Lambda Node 18+ supports native fetch) const ptResponse = await fetch(`https://api.proxytracer.com/v1/check/${clientIp}`, { method: 'GET', headers: { 'Authorization': `Bearer ${process.env.PROXYTRACER_API_KEY}` }, // Using an AbortController to enforce a strict 500ms timeout signal: AbortSignal.timeout(500) }); if (ptResponse.ok) { const data = await ptResponse.json(); // 3. Drop the connection immediately if a proxy is detected if (data.proxy === true) { // Returning a custom response short-circuits the CloudFront request return { status: '403', statusDescription: 'Forbidden', headers: { 'content-type': [{ key: 'Content-Type', value: 'application/json' }] }, body: JSON.stringify({ error: "Access Denied: VPN or Proxy detected." }) }; } } } catch (error) { // Fail open: If the API times out, allow traffic to ensure uptime console.error('ProxyTracer API Error:', error); } // 4. Traffic is clean, return the request object to continue routing to origin return request; };

Deployment-Hinweis: Stellen Sie sicher, dass diese Lambda-Funktion in der Region us-east-1 bereitgestellt wird, da CloudFront erfordert, dass alle Lambda@Edge-Funktionen von dort aus initiiert werden, bevor sie weltweit veranschaulicht und repliziert werden.

Last updated on