settings = $this->get_rate_limit_settings(); } /** * Get rate limit settings * * @return array */ private function get_rate_limit_settings() { $settings = SnagglySettings::get_instance(); return array( 'enabled' => $settings->is_rate_limiting_enabled(), 'requests_per_minute' => $settings->get('rate_limit_requests_per_minute', self::DEFAULT_LIMIT), 'burst_requests' => $settings->get('rate_limit_burst_requests', self::BURST_LIMIT), 'burst_window' => $settings->get('rate_limit_burst_window', self::BURST_WINDOW), 'block_duration' => $settings->get('rate_limit_block_duration', 300), // 5 minutes 'whitelist_ips' => $settings->get('rate_limit_whitelist_ips', array()), 'whitelist_users' => $settings->get('rate_limit_whitelist_users', array()) ); } /** * Check if request is rate limited * * @param string $endpoint AJAX endpoint name * @param int $user_id User ID (0 for non-logged-in users) * @param string $ip_address IP address * @return array Rate limit status */ public function check_rate_limit($endpoint, $user_id = 0, $ip_address = null) { if (!$this->settings['enabled']) { return array('allowed' => true); } if (null === $ip_address) { $ip_address = $this->get_client_ip(); } // Check whitelist if ($this->is_whitelisted($user_id, $ip_address)) { return array('allowed' => true); } // Check if currently blocked if ($this->is_blocked($endpoint, $user_id, $ip_address)) { return array( 'allowed' => false, 'reason' => 'blocked', 'retry_after' => $this->get_block_remaining_time($endpoint, $user_id, $ip_address) ); } // Check rate limits $rate_limit_status = $this->check_rate_limits($endpoint, $user_id, $ip_address); if (!$rate_limit_status['allowed']) { // Block the user if they exceed limits $this->block_user($endpoint, $user_id, $ip_address); return array( 'allowed' => false, 'reason' => $rate_limit_status['reason'], 'retry_after' => $this->settings['block_duration'] ); } // Record the request $this->record_request($endpoint, $user_id, $ip_address); return array('allowed' => true); } /** * Check if user/IP is whitelisted * * @param int $user_id * @param string $ip_address * @return bool */ private function is_whitelisted($user_id, $ip_address) { // Check IP whitelist if (in_array($ip_address, $this->settings['whitelist_ips'])) { return true; } // Check user whitelist if ($user_id > 0 && in_array($user_id, $this->settings['whitelist_users'])) { return true; } return false; } /** * Check if user is currently blocked * * @param string $endpoint * @param int $user_id * @param string $ip_address * @return bool */ private function is_blocked($endpoint, $user_id, $ip_address) { $block_key = $this->get_block_key($endpoint, $user_id, $ip_address); $blocked_until = get_transient($block_key); return $blocked_until !== false && $blocked_until > time(); } /** * Get remaining block time * * @param string $endpoint * @param int $user_id * @param string $ip_address * @return int */ private function get_block_remaining_time($endpoint, $user_id, $ip_address) { $block_key = $this->get_block_key($endpoint, $user_id, $ip_address); $blocked_until = get_transient($block_key); if ($blocked_until === false) { return 0; } return max(0, $blocked_until - time()); } /** * Block user for specified duration * * @param string $endpoint * @param int $user_id * @param string $ip_address */ private function block_user($endpoint, $user_id, $ip_address) { $block_key = $this->get_block_key($endpoint, $user_id, $ip_address); $block_until = time() + $this->settings['block_duration']; set_transient($block_key, $block_until, $this->settings['block_duration']); // Log the blocking $logger = SnagglyLogger::get_instance(); $logger->warning('User blocked due to rate limiting', array( 'endpoint' => $endpoint, 'user_id' => $user_id, 'ip_address' => $ip_address, 'block_duration' => $this->settings['block_duration'] )); } /** * Check rate limits * * @param string $endpoint * @param int $user_id * @param string $ip_address * @return array */ private function check_rate_limits($endpoint, $user_id, $ip_address) { $current_time = time(); // Check burst limit $burst_status = $this->check_burst_limit($endpoint, $user_id, $ip_address, $current_time); if (!$burst_status['allowed']) { return $burst_status; } // Check regular rate limit $rate_status = $this->check_regular_rate_limit($endpoint, $user_id, $ip_address, $current_time); if (!$rate_status['allowed']) { return $rate_status; } return array('allowed' => true); } /** * Check burst rate limit * * @param string $endpoint * @param int $user_id * @param string $ip_address * @param int $current_time * @return array */ private function check_burst_limit($endpoint, $user_id, $ip_address, $current_time) { $burst_key = $this->get_burst_key($endpoint, $user_id, $ip_address); $burst_requests = $this->get_window_count($burst_key, $this->settings['burst_window'], $current_time); if ($burst_requests === false) { return array('allowed' => true); } if ($burst_requests >= $this->settings['burst_requests']) { return array( 'allowed' => false, 'reason' => 'burst_limit_exceeded', 'limit' => $this->settings['burst_requests'], 'window' => $this->settings['burst_window'] ); } return array('allowed' => true); } /** * Check regular rate limit * * @param string $endpoint * @param int $user_id * @param string $ip_address * @param int $current_time * @return array */ private function check_regular_rate_limit($endpoint, $user_id, $ip_address, $current_time) { $rate_key = $this->get_rate_key($endpoint, $user_id, $ip_address); $rate_requests = $this->get_window_count($rate_key, 60, $current_time); if ($rate_requests === false) { return array('allowed' => true); } if ($rate_requests >= $this->settings['requests_per_minute']) { return array( 'allowed' => false, 'reason' => 'rate_limit_exceeded', 'limit' => $this->settings['requests_per_minute'], 'window' => 60 ); } return array('allowed' => true); } /** * Record a request * * @param string $endpoint * @param int $user_id * @param string $ip_address */ private function record_request($endpoint, $user_id, $ip_address) { $current_time = time(); $this->record_window_request($this->get_burst_key($endpoint, $user_id, $ip_address), $this->settings['burst_window'], $current_time); $this->record_window_request($this->get_rate_key($endpoint, $user_id, $ip_address), 60, $current_time); } /** A window starts with its first request and is never extended by later requests. */ private function get_window_count($key, $window, $now) { $state = get_transient($key); if (!is_array($state) || !isset($state['started_at'], $state['count']) || $state['started_at'] > $now || $now - $state['started_at'] >= $window) { return 0; } return (int) $state['count']; } /** Retain the original expiration so steady traffic cannot extend the window forever. */ private function record_window_request($key, $window, $now) { $count = $this->get_window_count($key, $window, $now); $state = $count ? get_transient($key) : array('started_at' => $now); $state['count'] = $count + 1; set_transient($key, $state, max(1, $window - ($now - $state['started_at']))); } /** * Get client IP address * * @return string */ private function get_client_ip() { $ip = isset($_SERVER['REMOTE_ADDR']) && is_string($_SERVER['REMOTE_ADDR']) ? sanitize_text_field(wp_unslash($_SERVER['REMOTE_ADDR'])) : ''; return filter_var($ip, FILTER_VALIDATE_IP) ? $ip : '0.0.0.0'; } /** * Get block key * * @param string $endpoint * @param int $user_id * @param string $ip_address * @return string */ private function get_block_key($endpoint, $user_id, $ip_address) { return self::STORAGE_PREFIX . 'block_' . md5($endpoint . '_' . ($user_id > 0 ? 'user_' . $user_id : 'ip_' . $ip_address)); } /** * Get burst key * * @param string $endpoint * @param int $user_id * @param string $ip_address * @return string */ private function get_burst_key($endpoint, $user_id, $ip_address) { return self::STORAGE_PREFIX . 'burst_' . md5($endpoint . '_' . ($user_id > 0 ? 'user_' . $user_id : 'ip_' . $ip_address)); } /** * Get rate key * * @param string $endpoint * @param int $user_id * @param string $ip_address * @return string */ private function get_rate_key($endpoint, $user_id, $ip_address) { return self::STORAGE_PREFIX . 'rate_' . md5($endpoint . '_' . ($user_id > 0 ? 'user_' . $user_id : 'ip_' . $ip_address)); } /** * Get rate limit statistics * * @param string $endpoint * @param int $user_id * @param string $ip_address * @return array */ public function get_rate_limit_stats($endpoint, $user_id = 0, $ip_address = null) { if (null === $ip_address) { $ip_address = $this->get_client_ip(); } $burst_key = $this->get_burst_key($endpoint, $user_id, $ip_address); $rate_key = $this->get_rate_key($endpoint, $user_id, $ip_address); $block_key = $this->get_block_key($endpoint, $user_id, $ip_address); return array( 'burst_requests' => $this->get_window_count($burst_key, $this->settings['burst_window'], time()), 'burst_limit' => $this->settings['burst_requests'], 'rate_requests' => $this->get_window_count($rate_key, 60, time()), 'rate_limit' => $this->settings['requests_per_minute'], 'is_blocked' => $this->is_blocked($endpoint, $user_id, $ip_address), 'block_remaining' => $this->get_block_remaining_time($endpoint, $user_id, $ip_address), 'is_whitelisted' => $this->is_whitelisted($user_id, $ip_address) ); } /** * Clear rate limit data for user * * @param int $user_id * @param string $ip_address */ public function clear_rate_limits($user_id = 0, $ip_address = null) { if (null === $ip_address) { $ip_address = $this->get_client_ip(); } $endpoints = array('scan', 'download', 'download_multiple', 'diagnostic'); foreach ($endpoints as $endpoint) { delete_transient($this->get_burst_key($endpoint, $user_id, $ip_address)); delete_transient($this->get_rate_key($endpoint, $user_id, $ip_address)); delete_transient($this->get_block_key($endpoint, $user_id, $ip_address)); } } /** * Get rate limit settings * * @return array */ public function get_settings() { return $this->settings; } }