Giter VIP home page Giter VIP logo

laravel-ip-middleware's Introduction

Laravel IP middleware

Latest Stable Version Latest Unstable Version Total Downloads License

Build Status Test Coverage Maintainability Quality Score StyleCI

Laravel middleware for whitelisting/blacklisting client IP addresses

Requirements

  • PHP 7.3 or higher
  • Laravel 6 or higher

Installation

You can install the package via composer:

composer require orkhanahmadov/laravel-ip-middleware

Usage

Packages comes with WhitelistMiddleware and BlacklistMiddleware middlewares. You can register any or both of them in $routeMiddleware in app/Http/Kernel.php file:

protected $routeMiddleware = [
    // ...
    'ip_whitelist' => \Orkhanahmadov\LaravelIpMiddleware\WhitelistMiddleware::class,
    'ip_blacklist' => \Orkhanahmadov\LaravelIpMiddleware\BlacklistMiddleware::class,
];

Use middlewares in any of your routes and pass IP addresses.

Route::middleware('ip_whitelist:1.1.1.1')->get('/', 'HomeController@index');
Route::middleware('ip_blacklist:3.3.3.3')->get('/', 'PostController@index');
  • ip_whitelist middleware will block any requests where client IP not matching with whitelisted IPs.
  • ip_blacklist middleware will block requests coming from blacklisted IPs.

You can also pass multiple IP addresses separated with comma:

Route::middleware('ip_whitelist:1.1.1.1,2.2.2.2')->get('/', 'HomeController@index');

This will block all requests where client IP not matching whitelisted IP list.

Package also allows setting predefine IP list in config and use them with name:

// config/ip-middleware.php

'predefined_lists' = [
    'my-list-1' => ['1.1.1.1', '2.2.2.2'],
    'my-list-2' => ['3.3.3.3', '4.4.4.4'],
];
Route::middleware('ip_whitelist:my-list-1,my-list-2')->get('/', 'HomeController@index');
// you can also mix predefined list with additional IP addresses
Route::middleware('ip_whitelist:my-list-1,my-list-2,5.5.5.5,6.6.6.6')->get('/', 'PostController@index');

Configuration

Run this command to publish package config file:

php artisan vendor:publish --provider="Orkhanahmadov\LaravelIpMiddleware\LaravelIpMiddlewareServiceProvider"

ip-middleware.php config file contains following settings:

  • ignore_environments - Middleware ignores IP checking when application is running in listed environments.
  • error_code - HTTP code that shown when request gets rejected.
  • custom_server_parameter - Custom $_SERVER parameter to look for IP address
  • predefined_lists - Predefined IP lists

Testing

composer test

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

Credits

License

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

laravel-ip-middleware's People

Contributors

orkhanahmadov avatar kosmonowt avatar sc-creed 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.