Code Monkey home page Code Monkey logo

laravel-route-blocker's Introduction

Laravel Route Blocker

Block routes by IP

(inspired on Laravel Firewall)


Requirements

Laravel 5.x, 6.x, 7.x and 8.x


Installation

  1. To install through composer, run the following command from terminal:

    $ composer require skydiver/laravel-route-blocker
  2. Publish the config file:

    Run the following command to publish the package config file:

    $ php artisan vendor:publish --tag=LaravelRouteBlocker
Still using Laravel 5.4 or below?

Please add service provider to your config/app.php providers array:

'providers' => [
    ...
    Skydiver\LaravelRouteBlocker\LaravelRouteBlockerServiceProvider::class,
]

Usage

  1. Register middlewares in app/Http/Kernel.php on $routeMiddleware array:
        'blacklist' => \Skydiver\LaravelRouteBlocker\Middleware\BlacklistMiddleware::class,
        'whitelist' => \Skydiver\LaravelRouteBlocker\Middleware\WhitelistMiddleware::class,
    
  • Blacklist allows all traffic except matching rules.
  • Whitelist blocks all traffic except matching rules.
  • You can register both or just a single middleware.
  1. Create a config group on config/laravel-route-blocker.php and insert your allowed/blocked IPs:

        'whitelist' => [
            'my_group' => [
                '127.0.0.1',
                '192.168.17.0',
                '10.0.1.*'
            ],
            'another_group' => [
                '8.8.8.*'
            ],
        ],
        'blacklist' => [
            'blocked_ips' => [
                '127.0.0.1',
                '192.168.100.0',
            ],
            'blocked_ips2' => [
                '8.8.8.8',
            ],
        ],
    
    • You can create as many blacklist/whitelists groups as you wish and protect differents set of routes with differents IPs

    Also, you can configure to throw an HTTP status code or redirect to a custom URL:

    'redirect_to'      => '',   // URL TO REDIRECT IF BLOCKED (LEAVE BLANK TO THROW STATUS)
    'response_status'  => 403,  // STATUS CODE (403, 404 ...)
    'response_message' => ''    // MESSAGE (COMBINED WITH STATUS CODE)
    
  2. Put your protected routes inside a group and specify the whitelist parameter:

        // Only IPs matched on "my_group" will be allowed to access route
        Route::group(['middleware' => 'whitelist:my_group'], function() {
    
            Route::get('/demo', function () {
                return "DEMO";
            });
    
        });
    
        // Only IPs matched on "my_group" will be blocked to access route
        Route::group(['middleware' => 'blacklist:blocked_ips'], function() {
    
            Route::get('/private', function () {
                return "Private Page";
            });
    
        });
    

Artisan Commands

  • To get a list of current IPs groups run:

        $ php artisan route:blocks:groups
    
        +-----------------+--------------+-----------+
        | Group           | IP           | Type      |
        +-----------------+--------------+-----------+
        | allowed_group_1 | 127.0.0.1    | whitelist |
        | allowed_group_1 | 127.0.0.2    | whitelist |
        | allowed_group_1 | 192.168.17.0 | whitelist |
        | allowed_group_1 | 10.0.0.*     | whitelist |
        | allowed_group_2 | 8.8.8.8      | whitelist |
        | allowed_group_2 | 8.8.8.*      | whitelist |
        | allowed_group_2 | 8.8.4.4      | whitelist |
        | blocked_ips_1   | 127.0.0.1    | blacklist |
        | blocked_ips_1   | 127.0.0.2    | blacklist |
        | blocked_ips_1   | 192.168.17.0 | blacklist |
        | blocked_ips_1   | 10.0.0.*     | blacklist |
        | blocked_ips_2   | 8.8.8.8      | blacklist |
        | blocked_ips_2   | 8.8.8.*      | blacklist |
        | blocked_ips_2   | 8.8.4.4      | blacklist |
        +-----------------+--------------+-----------+
    

Testing

To manually run test suite:

vendor/bin/phpunit --verbose

Test files inside tests/Feature should be run by GitHub action only.

laravel-route-blocker's People

Contributors

bogvsdev avatar itbm avatar skydiver avatar tai1030 avatar theschwag avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

laravel-route-blocker's Issues

Possibility of opposite? Block by blacklist?

Hey,

Looks like a great package! I haven't check it out yet, but it wasn't clear by the docs, is it possible to block groups of IPs, and allow everything else?

I like laravel-firewall but it's a bit too complex for my needs, this seems perfect.

Cheers

Country block?

Hi,
How do you block total country ip?
Example: I don't want anyone from x country have access to my website. Or yet better i want only put one country in my whitelist and the rest of the world be banned?

question: do you handle different types of IP addresses?

I saw this was inspired by antonioribeiro/firewall package.
Can you use IP addresses like that package:
'blacklist' => array(
'127.0.0.1',
'192.168.17.0/24'
'127.0.0.1/255.255.255.255'
'10.0.0.1-10.0.0.255'
'172.17..'
),

Im particularly interested in the 192.168.17.0/24 format since thats how the network admins usually send whitelists/blacklist ip ranges to me.

Also Im curious, in your opinion, what is the advantages and disadvantages of using this plugin vs antonioribeiro/firewall package?

php artisan route:blocks:groups: In BoundMethod.php line 135

To fix the BoundMehtod error:

php artisan route:blocks:groups

In BoundMethod.php line 135:
                                                                                   
  Method Skydiver\LaravelRouteBlocker\Console\GroupsList::handle() does not exist 

Add in /vendor/skydiver/laravel-route-blocker/src/Console/GroupList.php

public function handle()
        {
                return $this->fire();
        } 

laravel-route-blocker seems not compatible with laravel 6.6.2

conflicts with laravel/framework[v6.6.2]

- don't install illuminate/support v5.8.34|don't install laravel/framework v6.6.2
- don't install illuminate/support v5.8.35|don't install laravel/framework v6.6.2
- don't install illuminate/support v5.8.36|don't install laravel/framework v6.6.2
- don't install illuminate/support v5.8.4|don't install laravel/framework v6.6.2
- don't install illuminate/support v5.8.8|don't install laravel/framework v6.6.2
- don't install illuminate/support v5.8.9|don't install laravel/framework v6.6.2
- Installation request for laravel/framework (locked at v6.6.2, required as ^6.2) -> satisfiable by laravel/framework[v6.6.2].

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.