This commit is contained in:
mathiaspalmqvist committed 2026-11-11 23:19:20 +01:00
commit 3ed6eb9d19
23 files changed
+1216

No files matched your search

+40
View File
@@ -0,0 +1,40 @@
<?php
namespace Mattitja\BadIpBlocker\Console;
use Illuminate\Console\Command;
use InvalidArgumentException;
use Mattitja\BadIpBlocker\BadIpStore;
class AddCommand extends Command
{
protected $signature = 'bad-ip-blocker:add
{ip : The IP address to block}
{--reason= : Why the IP is blocked}
{--ttl= : Seconds until the block expires (omit for permanent)}';
protected $description = 'Block an IP address';
public function handle(BadIpStore $store): int
{
$ttl = $this->option('ttl');
if ($ttl !== null && (! ctype_digit((string) $ttl) || (int) $ttl < 1)) {
$this->components->error('The --ttl option must be a positive number of seconds.');
return self::FAILURE;
}
try {
$store->block($this->argument('ip'), $this->option('reason'), $ttl !== null ? (int) $ttl : null);
} catch (InvalidArgumentException $e) {
$this->components->error($e->getMessage());
return self::FAILURE;
}
$this->components->info("Blocked [{$this->argument('ip')}].");
return self::SUCCESS;
}
}
+26
View File
@@ -0,0 +1,26 @@
<?php
namespace Mattitja\BadIpBlocker\Console;
use Illuminate\Console\Command;
use Mattitja\BadIpBlocker\BadIpStore;
class ClearCommand extends Command
{
protected $signature = 'bad-ip-blocker:clear {--force : Skip confirmation}';
protected $description = 'Remove all blocked IP addresses';
public function handle(BadIpStore $store): int
{
if (! $this->option('force') && ! $this->confirm('Remove all blocked IP addresses?')) {
return self::FAILURE;
}
$store->clear();
$this->components->info('All blocks removed.');
return self::SUCCESS;
}
}
+36
View File
@@ -0,0 +1,36 @@
<?php
namespace Mattitja\BadIpBlocker\Console;
use Illuminate\Console\Command;
use Mattitja\BadIpBlocker\BadIpStore;
class ListCommand extends Command
{
protected $signature = 'bad-ip-blocker:list';
protected $description = 'List blocked IP addresses';
public function handle(BadIpStore $store): int
{
$entries = $store->all();
if ($entries === []) {
$this->components->info('No blocked IP addresses.');
return self::SUCCESS;
}
$this->table(
['IP', 'Reason', 'Blocked at', 'Expires at'],
collect($entries)->map(fn (array $entry, string $ip) => [
$ip,
$entry['reason'] ?? '',
$entry['blocked_at'] ?? '',
$entry['expires_at'] ?? 'never',
])->values()->all(),
);
return self::SUCCESS;
}
}
+20
View File
@@ -0,0 +1,20 @@
<?php
namespace Mattitja\BadIpBlocker\Console;
use Illuminate\Console\Command;
use Mattitja\BadIpBlocker\BadIpStore;
class PruneCommand extends Command
{
protected $signature = 'bad-ip-blocker:prune';
protected $description = 'Remove expired IP blocks';
public function handle(BadIpStore $store): int
{
$this->components->info("Removed {$store->prune()} expired block(s).");
return self::SUCCESS;
}
}
+28
View File
@@ -0,0 +1,28 @@
<?php
namespace Mattitja\BadIpBlocker\Console;
use Illuminate\Console\Command;
use Mattitja\BadIpBlocker\BadIpStore;
class RemoveCommand extends Command
{
protected $signature = 'bad-ip-blocker:remove {ip : The IP address to unblock}';
protected $description = 'Unblock an IP address';
public function handle(BadIpStore $store): int
{
$ip = $this->argument('ip');
if (! $store->unblock($ip)) {
$this->components->warn("[{$ip}] was not blocked.");
return self::FAILURE;
}
$this->components->info("Unblocked [{$ip}].");
return self::SUCCESS;
}
}