Coder Social home page Coder Social logo

lowerrocklabs / laravel-block-ip Goto Github PK

View Code? Open in Web Editor NEW

This project forked from michaelnabil230/laravel-block-ip

0.0 0.0 0.0 87 KB

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

Home Page: https://github.com/michaelnabil230/example-block-ip

License: MIT License

PHP 100.00%

laravel-block-ip's Introduction

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.

laravel-block-ip's People

Contributors

dependabot[bot] avatar github-actions[bot] avatar michaelnabil230 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.