fix(seo,performance): disallow bot media streaming, disable fastcgi buffering, and resolve real cloudflare client IPs

This commit is contained in:
Mustafa Yildiz
2026-08-21 14:02:31 +03:00
parent 19c8d496c7
commit c68c5a4c65
3 changed files with 62 additions and 11 deletions
+34 -11
View File
@@ -27,15 +27,17 @@ class BotDetector
// Bu IP prefix'leri için tüm kontroller atlanır (güvenilir crawler'lar)
private const TRUSTED_IP_PREFIXES = [
'66.249.', // Googlebot
'64.233.', // Google
'74.125.', // Google
'209.85.', // Google
'216.239.', // Google
'34.68.', // Google Cloud us-central1
'34.64.', // Google Cloud
'35.187.', // Google Cloud
'35.190.', // Google Cloud
'66.249.', // Googlebot IPv4
'64.233.', // Google
'74.125.', // Google
'209.85.', // Google
'216.239.', // Google
'34.68.', // Google Cloud us-central1
'34.64.', // Google Cloud
'35.187.', // Google Cloud
'35.190.', // Google Cloud
'2001:4860:', // Googlebot IPv6
'2a00:1450:', // Google IPv6
];
// İzin ver ama bot olarak işaretle (SEO crawler'lar)
@@ -80,7 +82,12 @@ class BotDetector
public function handle(Request $request, Closure $next)
{
$ip = $request->ip();
// Cloudflare / proxy arkasındaki gerçek istemci IP'sini tespit et
$cfIp = $request->header('CF-Connecting-IP')
?: ($request->header('True-Client-IP')
?: ($request->header('X-Forwarded-For') ? trim(explode(',', $request->header('X-Forwarded-For'))[0]) : null));
$ip = $cfIp ?: $request->ip();
$ua = strtolower($request->userAgent() ?? '');
$path = '/' . ltrim($request->path(), '/');
@@ -89,7 +96,23 @@ class BotDetector
$path = '/' . substr($path, 11);
}
// Skip paths
// Botların video parçalarını ve m3u8 playlistlerini boşuna taramasını engelle (CPU / FastCGI koruması)
$isBotCandidate = false;
if (!empty($ua)) {
foreach (array_merge(self::BAD_BOTS, self::GOOD_BOTS, self::GENERIC_BOTS) as $botPattern) {
if (str_contains($ua, $botPattern)) {
$isBotCandidate = true;
break;
}
}
}
if ($isBotCandidate && (str_starts_with($path, '/stream/') || str_starts_with($path, '/vtt-proxy'))) {
return response('No bot indexing allowed on media streams.', 403, [
'X-Robots-Tag' => 'noindex, nofollow, noarchive',
]);
}
// Skip paths (medya, tracking vb. insan trafiği için rate limit muafiyeti)
foreach (self::SKIP_PATHS as $skip) {
if (str_starts_with($path, $skip)) {
return $next($request);