mattitjaab/bad-ip-blocker

Block IPs based on a remote JSON file.

Installs: 133

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 0

Forks: 0

Open Issues: 0

pkg:composer/mattitjaab/bad-ip-blocker

v1.1.0 2025-11-23 07:09 UTC

This package is auto-updated.

Last update: 2025-12-23 07:17:36 UTC


README

Block incoming requests from known bad IP addresses using a centralized JSON feed.

This package fetches a list of bad IPs from a remote API hosted by Mattitja AB and caches them locally. Requests from matching IPs are immediately blocked with a 418 response.

Features

  • Automatically blocks known malicious IPs
  • Pulls IP list from a centralized JSON endpoint
  • Caches data locally to avoid repeated API calls
  • Designed to run globally across all routes

Installation

composer require mattitjaab/bad-ip-blocker

Usage

Register as global middleware in Laravel 12

Edit bootstrap/app.php:

use Mattitja\BadIpBlocker\Middleware\CheckBadIps;
use Illuminate\Foundation\Configuration\Middleware;

return Application::configure(basePath: dirname(__DIR__))
    ->withRouting(
        web: __DIR__.'/../routes/web.php',
        api: __DIR__.'/../routes/api.php',
        ...
    )
    ->withMiddleware(function (Middleware $middleware) {
        $middleware->append(CheckBadIps::class);
    })
    ->create();

This ensures the middleware runs globally on every incoming request.

How it works

  • On each request, the middleware checks if the client's IP exists in a cached JSON file.
  • If the cache is missing or older than one hour, it attempts to refresh from https://bad-ip.mattitja.cloud/api/json.
  • If the IP is found in the list, the request is blocked with a 418 response.

Cache location

Cached data is stored using Laravel's Cache facade (default: file driver):

storage/framework/cache/data/

To clear the cache and force a refresh:

php artisan cache:forget bad-ip-blocker

Or clear all cache:

php artisan cache:clear

Example response when blocked

HTTP/1.1 418 Blocked
Content-Type: text/plain

Blocked.

Maintained by Mattitja AB

This package is maintained by Mattitja AB and is intended for internal use across multiple projects.

License

MIT License © Mattitja AB