Skip to Content
DocumentationEdgeAWS Lambda@Edge

AWS Lambda@Edge

Interceptar el tráfico a nivel de CDN usando AWS Lambda@Edge es la forma más segura y económica de utilizar ProxyTracer. Al bloquear solicitudes maliciosas en la red perimetral de AWS, proteges completamente tus servidores de origen del tráfico de botnets y ahorras costos de computación.

La Implementación

Esta función se conecta al evento Viewer Request de CloudFront. Lee la IP del cliente directamente del objeto de evento, realiza una consulta asíncrona rápida a ProxyTracer y devuelve una respuesta 403 Forbidden construida directamente al usuario si se detecta un proxy.

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

Nota de Despliegue: Asegúrate de que esta función Lambda se despliegue en la región us-east-1, ya que CloudFront requiere que todas las funciones Lambda@Edge se inicien desde allí antes de replicarse globalmente.

Last updated on