haxneeraj/laraguard-ip

A Laravel middleware to guard your application via IP and country-level control.

v1.0.0 2025-04-15 06:06 UTC

This package is auto-updated.

Last update: 2025-04-16 11:24:05 UTC


README

laraguard-ip Logo

๐Ÿšซ Laraguard-IP

A powerful Laravel package to protect your application using IP-based, country-based, and spam-blacklist-based middleware.

Total Downloads Latest Stable Version License

Created with โค๏ธ by Neeraj Saini (opens in new tab)

โœจ Features

  • โœ… IP Whitelist Only Mode
  • ๐ŸŒ Country Whitelist Mode (GeoIP-based) (COMING SOON)
  • โŒ Spam Protection using blocklists (like AbuseIPDB)
  • ๐Ÿง  IP compression & JSON-based caching
  • ๐Ÿ›ก๏ธ Middleware-based access control
  • ๐Ÿ› ๏ธ Artisan command to fetch & cache spam IPs

๐Ÿ“ฆ Installation

composer require haxneeraj/laraguard-ip

๐Ÿ›  Configuration

1. Add the Service Provider

Add the service provider to the config/app.php file (this step may be optional if you're using Laravel's package auto-discovery):

For Laravel 10 and below:

    'providers' => [
        // Other service providers...
        Haxneeraj\LaraguardIp\Providers\LaraguardIpServiceProvider::class,
    ],

For Laravel 11: Add the service provider to the bootstrap/providers.php file (this step may be optional if you're using Laravel's package auto-discovery):

    return [
        // Other service providers...
        Haxneeraj\LaraguardIp\Providers\LaraguardIpServiceProvider::class,
    ];

2. Publish the config file

php artisan vendor:publish --provider="Haxneeraj\LaraguardIp\Providers\LaraguardIpServiceProvider" --tag=config

This will create a config file at:

config/laraguard-ip.php

๐Ÿงน Middleware Usage

The middleware is automatically registered via the package's service provider.
You can directly use it in your routes like this:

Apply to Routes

Route::middleware(['laraguard-ip'])->group(function () {
    // Protected routes
});

โš™๏ธ Configuration Options

Here's a summary of available config values:

Key Description Default
laraguardip_spam_protection Enable blocklist protection false
whitelist_only_access Only allow IPs in whitelist_ips false
country_whitelist_only Only allow from country_whitelist false
whitelist_ips Safe IPs ['127.0.0.1']
country_whitelist Allowed ISO country codes ['IN', 'US']
sources Blocklist sources (URL) AbuseIPDB
path Blocklist storage path storage/framework/cache/laraguard-ip.json
compress Enable IP compression true

๐Ÿ” Fetch & Cache Blacklist IPs

You can fetch and store the latest spam IPs from the defined sources:

php artisan laraguardip:update
  • Downloads IPs from config('laraguard-ip.sources')
  • Saves to laraguard-ip.json
  • Caches the list forever using Laravel's Cache

โ“ How It Works

  • โ›” Denies access if IP is not whitelisted (if enabled)
  • ๐ŸŒ Blocks requests from unwanted countries (if enabled)
  • โŒ Checks IPs against blacklist
  • ๐Ÿ“‚ Cache and config driven โ€“ no DB required

๐Ÿงช Example .env

LARAGUARDIP_SPAM_PROTECTION=true
LARAGUARDIP_WHITELIST_ONLY=false
LARAGUARDIP_COUNTRY_WHITELIST_ONLY=false
LARAGUARDIP_STORAGE_PATH=framework/cache/laraguard-ip.json
LARAGUARDIP_STORAGE_COMPRESS=true

๐Ÿงช Tests

This package comes with feature and unit tests to ensure everything works smoothly.

๐Ÿƒ Run Tests

composer test

๐Ÿง  Want to Contribute?

Pull requests are welcome! Feel free to fork, raise issues or drop ideas.

๐Ÿ“„ License

MIT ยฉ Neeraj Saini (opens in new tab)