Skip to content

This package is for controlling everything related to website visits using the Laravel framework

License

Notifications You must be signed in to change notification settings

LowerRockLabs/laravel-block-ip

 
 

Repository files navigation

This package is for controlling everything related to website visits using the Laravel framework

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

This is where your description should go. Limit it to a paragraph or two. Consider adding a small example.

Installation

You can install the package via composer:

composer require michaelnabil230/laravel-block-ip

You can publish all files and run the migrations with:

php artisan block-ip:install
php artisan migrate

You can publish and run the migrations with:

php artisan vendor:publish --tag="laravel-block-ip-migrations"
php artisan migrate

You can publish the config file with:

php artisan vendor:publish --tag="laravel-block-ip-config"

This is the contents of the published config file:

return [
    /*
     * Truest ips for you
     */

    'truest_ips' => [
        '127.0.0.1',
    ],

    /*
     * You can get notified when specific events occur. Out of the box you can use 'mail' and 'slack'.
     * For Slack you need to install laravel/slack-notification-channel.
     * 
     */

    'notifications' => [

        'channels' => ['mail'],

        /*
         * Here you can specify the notifiable to which the notifications should be sent. The default
         * notifiable will use the variables specified in this config file.
         */

        'notifiable' => \MichaelNabil230\BlockIp\Notifications\Notifiable::class,

        'mail' => [
            'to' => '[email protected]',

            'from' => [
                'address' => env('MAIL_FROM_ADDRESS', '[email protected]'),
                'name' => env('MAIL_FROM_NAME', 'Example'),
            ],
        ],

        'slack' => [
            'webhook_url' => '',

            /*
             * If this is set to null the default channel of the webhook will be used.
             */

            'channel' => null,

            'username' => null,

            'icon' => null,

        ],

        'discord' => [
            'webhook_url' => '',

            /*
             * If this is an empty string, the name field on the webhook will be used.
             */

            'username' => '',

            /*
             * If this is an empty string, the avatar on the webhook will be used.
             */

            'avatar_url' => '',
        ],
    ],

    'cache' => [

        /*
         * By default all block ips are cached for 24 hours to speed up performance.
         * When block ips are updated the cache is flushed automatically.
         */

        'expiration_time' => \DateInterval::createFromDateString('24 hours'),

        /*
         * The cache key used to store all block ips.
         */

        'key' => 'block-ips.cache.',

        /*
         * You may optionally indicate a specific cache driver to use for block ip
         * caching using any of the `store` drivers listed in the cache.php config
         * file. Using 'default' here means to use the `default` set in cache.php.
         */

        'store' => 'default',
    ],

    'webhook_cloud_flare' => [

        /**
         * Enable the webhook cloud flare work when user blocked.
         */
        'enable' => false,

        /**
         * Global API Key on the "My Profile > Api Tokens > API Keys" page.
         */
        'key' => env('CLOUDFLARE_KEY'),

        /**
         * Email address associated with your account.
         */
        'email' => env('CLOUDFLARE_EMAIL'),
    ],
];

Usage

You can add them inside your app/Providers/RouteServiceProvider.php file.

protected function configureRateLimiting()
{
    RateLimiter::for('api', function (Request $request) {
        return Limit::perMinute(60)->by($request->user()?->id ?: $request->ip());
    });

    \MichaelNabil230\BlockIp\BlockIpRegistrar::rateLimiter();
}

By default maxAttempts is 60 but if you want to change this number can make that.

\MichaelNabil230\BlockIp\BlockIpRegistrar::rateLimiter(100);

Package Middleware

This package comes with BlockIpMiddleware middleware. You can add them inside your app/Http/Kernel.php file.

protected $routeMiddleware = [
    // ...
    'block-ip' => \MichaelNabil230\BlockIp\Middleware\BlockIpMiddleware::class,
];

Check if this line is uncommented in your app/Http/Kernel.php file.

protected $routeMiddleware = [
    // ...
    'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
];

Then you can protect your routes using middleware rules:

Route::middleware(['block-ip', 'throttle:block-ip'])->group(function () {
    //
});

If you want to unblock all blocks for IPs can make that when running this command:

php artisan block-ip:unblock --all

Or pluck for IPs

php artisan block-ip:unblock --ips=127.0.0.1,127.0.0.2

If you want to add new IPs for the block can make that when running this command:

php artisan block-ip:block 127.0.0.1,127.0.0.2

Support

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.

About

This package is for controlling everything related to website visits using the Laravel framework

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • PHP 100.0%