Cloudflare Workers
Interceptar el tráfico en el Edge es la forma más eficiente de utilizar ProxyTracer. Al implementar este middleware en un Cloudflare Worker, las solicitudes maliciosas se bloquean a nivel CDN antes de que consuman recursos de tu servidor de origen.
La Implementación
Este fragmento extrae la dirección IP real del cliente utilizando los encabezados nativos de Cloudflare, consulta la API de ProxyTracer y devuelve un estricto 403 Forbidden si se detecta un proxy.
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);
}
};Nota de Seguridad: Siempre almacena tu PROXYTRACER_API_KEY como un Secreto cifrado en el panel de Cloudflare Worker, nunca en texto plano.
Last updated on