├── .github └── workflows │ └── update-changelog.yml ├── .gitignore ├── CHANGELOG.md ├── CONTRIBUTING.md ├── LICENSE ├── README.md ├── composer.json ├── publishable └── config │ └── ip-gateway.php └── src ├── IpGatewayProvider.php └── Middleware └── IpGatewayMiddleware.php /.github/workflows/update-changelog.yml: -------------------------------------------------------------------------------- 1 | name: "Update Changelog" 2 | 3 | on: 4 | release: 5 | types: [released] 6 | 7 | jobs: 8 | update: 9 | runs-on: ubuntu-latest 10 | 11 | steps: 12 | - name: Checkout code 13 | uses: actions/checkout@v4 14 | with: 15 | ref: main 16 | 17 | - name: Update Changelog 18 | uses: stefanzweifel/changelog-updater-action@v1 19 | with: 20 | latest-version: ${{ github.event.release.name }} 21 | release-notes: ${{ github.event.release.body }} 22 | 23 | - name: Commit updated CHANGELOG 24 | uses: stefanzweifel/git-auto-commit-action@v5 25 | with: 26 | branch: main 27 | commit_message: Update CHANGELOG 28 | file_pattern: CHANGELOG.md 29 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | composer.phar 2 | composer.lock 3 | .idea/ 4 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | All notable changes to `laravel-ip-gateway` will be documented in this file. 4 | 5 | ## v2.2.0 - 2024-08-23 6 | 7 | ### What's Changed 8 | 9 | * Merge pull request #1 from vcian/main by @ruchit288 in https://github.com/vcian/laravel-ip-gateway/pull/8 10 | * Update php version in composer. by @ruchit288 in https://github.com/vcian/laravel-ip-gateway/pull/9 11 | 12 | **Full Changelog**: https://github.com/vcian/laravel-ip-gateway/compare/v2.1.1...v2.2.0 13 | 14 | ## 2.1.0 - 2023-06-19 15 | 16 | - Code refactoring - remove unnecessary conditions. 17 | 18 | ## 2.0.0 - 2023-04-12 19 | 20 | - Code refactoring and version upgrade. 21 | 22 | ## 1.0.0 - 2018-01-10 23 | 24 | - First release 25 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | 3 | Contributions are **welcome** and will be fully **credited**. 4 | 5 | We accept contributions via Pull Requests on [Github](https://github.com/viitorcloudtechnologies/laravel-word-refiner). 6 | 7 | 8 | ## Pull Requests 9 | 10 | - **[PSR-2 Coding Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md)** - The easiest way to apply the conventions is to install [PHP Code Sniffer](http://pear.php.net/package/PHP_CodeSniffer). 11 | 12 | - **Add tests!** - Your patch won't be accepted if it doesn't have tests. 13 | 14 | - **Document any change in behaviour** - Make sure the `README.md` and any other relevant documentation are kept up-to-date. 15 | 16 | - **Consider our release cycle** - We try to follow [SemVer v2.0.0](http://semver.org/). Randomly breaking public APIs is not an option. 17 | 18 | - **Create feature branches** - Don't ask us to pull from your master branch. 19 | 20 | - **One pull request per feature** - If you want to do more than one thing, send multiple pull requests. 21 | 22 | - **Send coherent history** - Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please [squash them](http://www.git-scm.com/book/en/v2/Git-Tools-Rewriting-History#Changing-Multiple-Commit-Messages) before submitting. 23 | 24 | 25 | ## Running Tests 26 | 27 | ``` bash 28 | $ composer test 29 | ``` 30 | 31 | 32 | **Happy coding**! 33 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Viitor Cloud Technologies 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # IP gateway for laravel 2 | 3 | [![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=for-the-badge)](LICENSE.md) 4 | [![Total Downloads](https://img.shields.io/packagist/dt/vcian/laravel-ip-gateway?style=for-the-badge)](https://packagist.org/packages/vcian/laravel-ip-gateway) 5 | 6 | ## Features 7 | 8 | * The Laravel Ip gateway package helps you to blacklist or whitelist IP to prevent unauthorized access to your application. 9 | 10 | * Since blacklists deny access to specific entities, they are best used when a limited number of items need to be denied access. 11 | When most entities need to be denied access, a whitelist approach is more efficient. 12 | 13 | ## Requirements 14 | 15 | PHP ^8.0 16 | 17 | ## Installation 18 | 19 | You can install the package via composer: 20 | 21 | ```bash 22 | composer require vcian/laravel-ip-gateway 23 | ``` 24 | 25 | After installation, You need to publish the config file for this package. This will add the file `config/ip-gateway.php`, where you can configure this package. 26 | 27 | ```bash 28 | php artisan vendor:publish 29 | ``` 30 | 31 | ### Config Usage (`ip-gateway.php`) 32 | 33 | * `enable_package` is used for enable/disable access protection. 34 | 35 | * `enable_blacklist` when its true that means, It will denied access for registered ips in `ip-list`, false means, It will allow accessing for registered ips in `ip-list`. 36 | 37 | * You can authenticated IPs through register route groups in `middleware`. 38 | 39 | * `redirect_route_to` will access URL, To redirect if denied. 40 | 41 | * You can define all your whitelist or blacklist IP addresses inside `ip-list`. 42 | 43 | * If you want to add IP gateway middleware in you custom middleware group then just define your custom middleware group inside `middleware` 44 | ### Changelog 45 | 46 | Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently. 47 | 48 | ## Contributing 49 | 50 | Please see [CONTRIBUTING](CONTRIBUTING.md) for details. 51 | 52 | ## Security 53 | 54 | If you discover any security-related issues, please email ruchit.patel@viitor.cloud instead of using the issue tracker. 55 | 56 | ## License 57 | 58 | The MIT License (MIT). Please see [License File](LICENSE.md) for more information. 59 | 60 | ## Notes 61 | 62 | **You can create as many whitelists or blacklist groups as you wish to protect access** 63 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "vcian/laravel-ip-gateway", 3 | "description": "Blacklist or Whitelist IP of your application routes.", 4 | "keywords": [ 5 | "ip", 6 | "ipgateway", 7 | "whitelist", 8 | "blacklist", 9 | "laravel", 10 | "IP Filtering", 11 | "IP Restriction Laravel", 12 | "Laravel IP Security" 13 | ], 14 | "license": "MIT", 15 | "authors": [ 16 | { 17 | "name": "Vcian - Viitorcloud", 18 | "email": "ruchit.patel@viitor.cloud", 19 | "role": "Creator" 20 | } 21 | ], 22 | "require": { 23 | "php": "^8.2" 24 | }, 25 | "autoload": { 26 | "psr-4": { 27 | "Vcian\\LaravelIpGateway\\": "src/" 28 | } 29 | }, 30 | "extra": { 31 | "laravel": { 32 | "providers": [ 33 | "Vcian\\LaravelIpGateway\\IpGatewayProvider" 34 | ] 35 | } 36 | }, 37 | "minimum-stability": "dev", 38 | "prefer-stable": true 39 | } 40 | -------------------------------------------------------------------------------- /publishable/config/ip-gateway.php: -------------------------------------------------------------------------------- 1 | true, 10 | 11 | /* 12 | * Enable / disable firewall 13 | * 14 | * Enable will block Blacklist IP 15 | * Disable will allow only Whitelist IP 16 | * 17 | * @type boolean 18 | */ 19 | 'enable_blacklist' => true, 20 | 21 | /* 22 | * Enable IP detection for Middleware 23 | * 24 | * You can use Middleware name like 'auth','web','api' 25 | * 26 | * @var array 27 | */ 28 | 'middleware' => [ 29 | 'web' // Default Middleware Group 30 | ], 31 | 32 | /* 33 | * Url to redirect if blocked 34 | * 35 | * You can use route url (/404); 36 | * 37 | * @type string 38 | */ 39 | 'redirect_route_to' => '', 40 | 41 | /* 42 | * Whitelisted and blacklisted IP addresses 43 | * 44 | * Examples of IP address 45 | * '127.0.0.1', 46 | * 47 | * @type array 48 | */ 49 | 'ip-list' => [ 50 | 51 | ], 52 | ]; 53 | -------------------------------------------------------------------------------- /src/IpGatewayProvider.php: -------------------------------------------------------------------------------- 1 | app['router']; 23 | if(config('ip-gateway')){ 24 | foreach (config('ip-gateway.middleware') as $middlewareName) { 25 | $router->pushMiddlewareToGroup($middlewareName, IpGatewayMiddleware::class); 26 | } 27 | } 28 | } 29 | 30 | /** 31 | * Register the application services. 32 | * 33 | * @return void 34 | */ 35 | public function register() 36 | { 37 | $this->publishFiles(); 38 | } 39 | 40 | /** 41 | * Publish files 42 | */ 43 | public function publishFiles() 44 | { 45 | $publishableFiles = [ 46 | __DIR__ . '/../publishable/config/ip-gateway.php' => config_path('ip-gateway.php'), 47 | ]; 48 | 49 | foreach ($publishableFiles as $storedPath => $publishPath) { 50 | $this->publishes([$storedPath => $publishPath]); 51 | } 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /src/Middleware/IpGatewayMiddleware.php: -------------------------------------------------------------------------------- 1 | getClientIps(); 32 | $enableBlacklist = config('ip-gateway.enable_blacklist') === true; 33 | $redirectRoute = config('ip-gateway.redirect_route_to'); 34 | 35 | if (config('ip-gateway.enable_package') === true) { // Check if package status is enable. 36 | foreach ($getClientIps as $ip) { 37 | if ($enableBlacklist && $this->grantIpAddress($ip)) { // Its check blacklisted ip-addresses from ip-config file. 38 | $prohibitRequest = true; 39 | Log::warning($ip . ' IP address has tried to access.'); 40 | } elseif (!$enableBlacklist && !$this->grantIpAddress($ip)) { // Its check whitelisted ip-addresses from ip-config file. 41 | $prohibitRequest = true; 42 | Log::warning($ip . ' IP address has tried to access.'); 43 | } 44 | } 45 | 46 | if ($prohibitRequest) { 47 | return redirect($redirectRoute != '' ? $redirectRoute : '/404'); 48 | } 49 | } 50 | 51 | return $next($request); 52 | 53 | } catch (\Exception $ex) { 54 | Log::error('Problem occurred while handle an incoming request '.$ex->getMessage()); 55 | return redirect('/404'); 56 | } 57 | } 58 | 59 | /** 60 | * Grant IP address 61 | * 62 | * @param string $ip 63 | * @return bool 64 | */ 65 | protected function grantIpAddress(string $ip) : bool 66 | { 67 | return in_array($ip, config('ip-gateway.ip-list')); 68 | } 69 | } 70 | --------------------------------------------------------------------------------