app/Plugin/AccessRateLimiter42/Event.php line 36

Open in your IDE?
  1. <?php
  2. namespace Plugin\AccessRateLimiter42;
  3. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  4. use Symfony\Component\HttpKernel\Event\RequestEvent;
  5. use Symfony\Component\HttpKernel\KernelEvents;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use Plugin\AccessRateLimiter42\Service\RateLimiterService;
  8. use Eccube\Event\TemplateEvent;
  9. use Symfony\Component\HttpFoundation\RequestStack;
  10. class Event implements EventSubscriberInterface{
  11.     /** @var RateLimiterService */
  12.     private $rateLimiterService;
  13.     /** @var RequestStack */
  14.     private $requestStack;
  15.     public function __construct(
  16.         RateLimiterService $rateLimiterService,
  17.         RequestStack $requestStack
  18.     ){
  19.         $this->rateLimiterService $rateLimiterService;
  20.         $this->requestStack $requestStack;
  21.     }
  22.     public static function getSubscribedEvents(){
  23.         return [
  24.             KernelEvents::REQUEST   => ['onKernelRequest'1024],
  25.             'index.twig'            => 'onRender',
  26.             'default_frame.twig'    => 'onRender',
  27.             'error.twig'            => 'onRender',
  28.         ];
  29.     }
  30.     public function onKernelRequest(RequestEvent $event){
  31.         if (!$event->isMainRequest()) {
  32.             return;
  33.         }
  34.         $request $event->getRequest();
  35.         $path $request->getPathInfo();
  36.         $adminRouteDir $request->server->get('ECCUBE_ADMIN_ROUTE');
  37.         if(strpos($path$adminRouteDir) !== false){
  38.             return;
  39.         }
  40.         $ip $this->getClientIp($request);
  41.         if ($this->rateLimiterService->checkRateLimit($ip)) {
  42.             $this->rateLimiterService->recordAccess($ip);
  43.             $this->rateLimiterService->recordBlockedIp($ip);
  44.             $template $this->rateLimiterService->getBlockTemplate();
  45.             $response = new Response(
  46.                 $template,
  47.                 429,
  48.                 ['Content-Type' => 'text/html; charset=UTF-8']
  49.             );
  50.             
  51.             $event->setResponse($response);
  52.             return;
  53.         }
  54.     }
  55.     public function onRender(TemplateEvent $event){
  56.         $request $this->requestStack->getCurrentRequest();
  57.         $ip $this->getClientIp($request);
  58.         $this->rateLimiterService->recordAccess($ip);
  59.         $this->rateLimiterService->cleanupOldLogs();
  60.     }
  61.     /**
  62.      * クライアントIPアドレス取得
  63.      */
  64.     private function getClientIp($request){
  65.         $ip $request->getClientIp();
  66.         
  67.         if ($request->headers->has('X-Forwarded-For')) {
  68.             $ips explode(','$request->headers->get('X-Forwarded-For'));
  69.             $ip trim($ips[0]);
  70.         }
  71.         
  72.         return $ip;
  73.     }
  74. }