Skip to content

Commit

Permalink
Fix max requests per minute
Browse files Browse the repository at this point in the history
  • Loading branch information
antonioribeiro committed Feb 13, 2023
1 parent ecbf63a commit b8a894b
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 13 deletions.
6 changes: 3 additions & 3 deletions src/Services/IpUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ trait IpUtils

public function getIpAddress(): string|null
{
if ($this->ipAddress !== null) {
if ($this->ipAddress !== null && filled($this->ipAddress)) {
return $this->ipAddress;
}

Expand All @@ -20,13 +20,13 @@ public function getIpAddress(): string|null
$this->ipAddress = $this->removePortFromIPv4($this->ipAddress);
}

if ($this->ipAddress !== null) {
if ($this->ipAddress !== null && filled($this->ipAddress)) {
return $this->ipAddress;
}

$this->ipAddress = request()->ip();

if ($this->ipAddress !== null) {
if ($this->ipAddress !== null && filled($this->ipAddress)) {
return $this->ipAddress;
}

Expand Down
15 changes: 9 additions & 6 deletions src/Services/Middleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,7 @@ public function blockAttackAttemps(): string

$rateLimitingKey = $this->rateLimitingKey();

$response = RateLimiter::attempt(
$rateLimitingKey,
$this->config('attacks.max-per-minute', 30),
fn() => 'allow',
);
$response = RateLimiter::attempt($rateLimitingKey, $this->maxRequestsPerMinute(), fn() => 'allow');

if ($response !== 'allow' && ($ipAddress = $this->getIpAddress()) !== null) {
$this->addIpAddressToBlockList($ipAddress);
Expand All @@ -175,7 +171,14 @@ public function addIpAddressToBlockList(string $ipAddress): void

$ipAddresses[] = $ipAddress;

$domain->block = implode("\n", $ipAddresses);
if (count($ipAddresses) > dd($this->config('attacks.max-blocked-ip-addresses', 500))) {
return;
}

$domain->block = collect($ipAddresses)
->unique()
->filter()
->implode("\n");

$domain->save();
}
Expand Down
2 changes: 1 addition & 1 deletion src/Services/Responder.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Responder
public function respond(array $data = []): mixed
{
if ($data['code'] === 200) {
return;
return null;
}

if ($data['should_abort']) {
Expand Down
7 changes: 6 additions & 1 deletion src/Services/TwillFirewall.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,19 @@ public function blockAttacks(bool $force = false): string|null

public function addBlockedToBlockList(bool $force = false): string|null
{
return $this->get('attacks.add_blocked_to_list', 'add_blocked_to_list', $force);
return $this->get('attacks.add-blocked-to-list', 'add_blocked_to_list', $force);
}

public function published(bool $force = false): string|null
{
return $this->get('enabled', 'published', $force);
}

public function maxRequestsPerMinute(bool $force = false): string|null
{
return $this->get('attacks.max_requests_per_minute', 'max_requests_per_minute', $force);
}

public function get(string $configKey, string $databaseColumn, bool $force = false): string|null
{
if (!$force && (!$this->isConfigured() || !$this->enabled())) {
Expand Down
5 changes: 3 additions & 2 deletions src/config/twill-firewall.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,9 @@

'attacks' => [
'block' => env('TWILL_BLOCK_ATTACKS_ENABLED', false),
'add_blocked_to_list' => env('TWILL_ADD_BLOCKED_TO_BLOCK_LIST', false),
'max-per-minute' => env('TWILL_BLOCK_ATTACKS_RATE_PER_MIUTE', 30),
'add-blocked-to-list' => env('TWILL_ADD_BLOCKED_TO_BLOCK_LIST', false),
'max-per-minute' => env('TWILL_BLOCK_ATTACKS_RATE_PER_MINUTE', 30),
'max-automatic-ip-addresses' => env('TWILL_BLOCK_ATTACKS_MAX_IPS', 1000),
],

'responses' => [
Expand Down

0 comments on commit b8a894b

Please sign in to comment.