Esempio n. 1
0
 /**
  * Incoming log entries from the filter chain
  *
  * @param LogEntriesEvent $event
  */
 public function onProcess(LogEntriesEvent $event)
 {
     $endpointPattern = $this->getEndpointPattern();
     $event->setLogEntries(array_filter($event->getLogEntries(), function (LogEntry $logEntry) use($endpointPattern) {
         $matches = [];
         preg_match($endpointPattern, $logEntry->getRequest(), $matches);
         return count($matches) > 0;
     }));
 }
Esempio n. 2
0
 /**
  * Incoming log entries from the filter chain
  *
  * @param LogEntriesEvent $event object containing the log entries
  */
 public function onProcess(LogEntriesEvent $event)
 {
     $window = $this->window;
     $latestTimestamp = 0;
     // loop through the new log entries (normally just one)
     foreach ($event->getLogEntries() as $logEntry) {
         $blocked = false;
         $latestTimestamp = $logEntry->getTimestamp() > $latestTimestamp ? $logEntry->getTimestamp() : $latestTimestamp;
         foreach ($this->blockedLogEntries as $blockedLogEntry) {
             if ($blockedLogEntry->getHost() == $logEntry->getHost()) {
                 $blocked = true;
                 break;
             }
         }
         if (!$blocked) {
             $this->withheldLogEntries[] = $logEntry;
             $logEntries = array_filter($this->withheldLogEntries, function (LogEntry $withheld) use($logEntry, $window) {
                 return $withheld->getHost() == $logEntry->getHost() && $withheld->getTimestamp() > $logEntry->getTimestamp() - $window;
             });
             if (count($logEntries) > $this->maxHits) {
                 $this->blockHost($logEntry);
             }
         }
     }
     // decide which log entries get passed along the filter chain, and which will be withheld
     $passOnLogEntries = [];
     $newWithheldLogEntries = [];
     if ($event->shouldFlush()) {
         $passOnLogEntries = $this->withheldLogEntries;
     } else {
         foreach ($this->withheldLogEntries as $logEntry) {
             if ($logEntry->getTimestamp() > $latestTimestamp - $window) {
                 $newWithheldLogEntries[] = $logEntry;
             } else {
                 $passOnLogEntries[] = $logEntry;
             }
         }
     }
     $this->withheldLogEntries = $newWithheldLogEntries;
     $event->setLogEntries($passOnLogEntries);
 }