Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions rate-limiter.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
'id' => getenv('RATE_LIMIT_ID'),
'interval' => getenv('RATE_LIMIT_INTERVAL'),
'host' => 'localhost',
'auth' => ['redis-username', 'redis-password'],
'db' => 0,
'prefix' => 'waf:ratelimit:',
]);

Expand All @@ -33,15 +35,23 @@ function rate_limit(array $options): void
}

$key = preg_replace('/[^a-zA-Z0-9_\-\.]/', '_', $key);

try {
$redis = new Redis();
if (!$redis->connect($host, 6379)) {
error_log('Failed to connect to Redis server.');
return;
}
if (!$redis->auth($options['auth'])) {
error_log('Failed to authenticate to Redis server.');
return;
}
if (!$redis->select($options['db'])) {
error_log('Failed to select Redis database.');
return;
}
} catch (\RedisException $e) {
error_log('Redis connection failed: ' . $e->getMessage());
error_log('Redis connection error: ' . $e->getMessage());
return;
}

Expand Down