Skip to content

Commit

Permalink
Add ServiceProvider for ratelimiter
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelfolaron committed Jun 10, 2024
1 parent 778a9e7 commit 0559ac7
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 10 deletions.
1 change: 1 addition & 0 deletions app/Core/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ protected function registerBaseServiceProviders()

$this->register(new \Leantime\Core\Providers\Auth($this));

$this->register(new \Leantime\Core\Providers\RateLimiter($this));
$this->register(new \Leantime\Core\Providers\Db($this));
$this->register(new \Leantime\Core\Providers\Language($this));
}
Expand Down
9 changes: 8 additions & 1 deletion app/Core/IncomingRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ protected function setRequestDest(?string $requestUri = null): void
break;
};

$this->query->set('act', $act);
$this->query->set('act', $act);
isset($id) && $this->query->set('id', $id);
isset($request_parts) && $this->query->set('request_parts', $request_parts);
}
Expand Down Expand Up @@ -138,6 +138,13 @@ public function getRequestParams(string $method = null): array
};
}

/**
* Set the Laravel session instance.
*
* @param \Illuminate\Contracts\Session\Session $session The Laravel session instance.
*
* @return void
*/
public function setLaravelSession(\Illuminate\Contracts\Session\Session $session)
{
$this->session = new SymfonySessionDecorator($session);
Expand Down
26 changes: 17 additions & 9 deletions app/Core/Middleware/RequestRateLimiter.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@
use Leantime\Core\Eventhelpers;
use Leantime\Core\Frontcontroller;
use Leantime\Core\IncomingRequest;
use Leantime\Core\Language;
use Leantime\Core\Middleware\Request;
use Leantime\Domain\Api\Services\Api;
use Leantime\Domain\Setting\Services\Setting;
use Symfony\Component\HttpFoundation\Response;

/**
Expand All @@ -25,6 +27,7 @@ class RequestRateLimiter
use Eventhelpers;

protected RateLimiter $limiter;
protected Environment $config;

/**
* __construct
Expand All @@ -33,34 +36,39 @@ class RequestRateLimiter
* @param RateLimiter $limiter The RateLimiter object to be initialized.
* @return void.
*/
public function __construct()
public function __construct(Environment $config, RateLimiter $limiter)
{
app()->singleton(RateLimiter::class, fn($app)=> new RateLimiter(Cache::store("installation")));
$this->limiter = app()->make(RateLimiter::class);
$this->limiter = $limiter;
$this->config = $config;
}

/**
* Handle the incoming request.
*
* @param IncomingRequest $request The incoming request object.
* @param Closure $next The next middleware closure.
* @param Closure $next The next middleware closure.
* @return Response The response object.
* @throws BindingResolutionException
*/
public function handle(IncomingRequest $request, Closure $next): Response
{

if(!session("isInstalled")) {
return $next($request);
}

//Configurable rate limits
$rateLimitGeneral = app()->make(Environment::class)->get('LEAN_RATELIMIT_GENERAL') ?? 1000;
$rateLimitApi = app()->make(Environment::class)->get('LEAN_RATELIMIT_API') ?? 10;
$rateLimitAuth = app()->make(Environment::class)->get('LEAN_RATELIMIT_AUTH') ?? 20;
$rateLimitGeneral = $this->config->ratelimitGeneral ?? 2000;
$rateLimitApi = $this->config->ratelimitApi ?? 10;
$rateLimitAuth = $this->config->rateLimitAuth ?? 20;

//Key
$keyModifier = "-1";
if(session()->exists("userdata")){
if (session()->exists("userdata")) {
$keyModifier = session("userdata.id");
}

$key = $request->getClientIp()."-".$keyModifier;
$key = $request->getClientIp() . "-" . $keyModifier;

//General Limit per minute
$limit = $rateLimitGeneral;
Expand Down
28 changes: 28 additions & 0 deletions app/Core/Providers/RateLimiter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Leantime\Core\Providers;

use Illuminate\Cache\MemcachedConnector;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\ServiceProvider;
use Leantime\Core\CliRequest;
use Leantime\Core\Events;
use Leantime\Core\IncomingRequest;
use Leantime\Domain\Auth\Services\Auth as AuthService;
use Leantime\Domain\Oidc\Services\Oidc as OidcService;
use Leantime\Domain\Setting\Services\Setting as SettingsService;

class RateLimiter extends ServiceProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
$this->app->singleton(\Illuminate\Cache\RateLimiter::class, function ($app) {
return new \Illuminate\Cache\RateLimiter(Cache::store("installation"));
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
9999999999a:0:{}

0 comments on commit 0559ac7

Please sign in to comment.