Hello, first of all, I want to tell you about the process I want to do.
First, it gets the IP address of the request and the country code of the IP address and checks whether this IP address is in the cache. If the IP address and country code are in the cache, it allows access to the page and returns “cachevvar”.
If the IP address and country code are not in the cache, it makes an API request to check the properties of the IP address. As a result of this request, IP addresses with features such as VPN, proxy, tor are detected. If the IP address has any of these properties, it does not allow access to the page and returns “exit”.
If the IP address does not have any of these features, it allows access to the page and returns “cachesave”. It also caches the IP address and country code and can use the cached response in the next request…
The error I received: CACHETTL
Saving to cache is successful, but the cache ttl feature does not work, that is, it does not delete the code from the cache in the time I want.
// Cache duration in seconds
const CACHE_DURATION = 6;
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
async function handleRequest(request) {
const ip = request.headers.get("CF-Connecting-IP");
const countryCode = request.headers.get("CF-IPCountry");
const cache = caches.default;
// Try to get the cached response for the IP and country code
const cacheKey = new Request(`https://gorevkutusu.net/${encodeURI(ip)}/${encodeURI(countryCode)}`);
let response = await cache.match(cacheKey);
if (response) {
// If the IP and country code are cached, allow access and return "cachevvar"
return new Response("cachevvar", {
headers: {
'content-type': 'text/plain;charset=UTF-8',
},
});
} else {
// If the IP and country code are not cached, make the API request to check for VPN, proxy, etc.
const url = `https://ipinfo.io/${ip}/?token=30dbac06c4dfa4`;
const apiResponse = await fetch(url);
const data = await apiResponse.json();
let allowed = true;
if (data.privacy.vpn || data.privacy.proxy || data.privacy.tor || data.privacy.relay || data.privacy.hosting) {
allowed = false;
}
// Cache the response for the IP and country code
const cacheResponse = new Response(allowed ? 'cachekayıt' : 'exit', {
headers: {
'content-type': 'text/plain;charset=UTF-8',
},
});
await cache.put(cacheKey, cacheResponse.clone(), {
// Set the cache duration
expirationTtl: CACHE_DURATION,
});
// If the IP is allowed, return "cachekayıt"
if (allowed) {
return new Response("cachekayıt", {
headers: {
'content-type': 'text/plain;charset=UTF-8',
},
});
} else {
// If the IP is not allowed, return "exit"
return new Response("exit", {
headers: {
'content-type': 'text/plain;charset=UTF-8',
},
});
}
}
}