From 2522f3dc5d1bf2bcf798e66a56910e1c907b5b4d Mon Sep 17 00:00:00 2001 From: Orkhan Ahmadov Date: Thu, 19 Sep 2019 11:06:20 +0200 Subject: [PATCH] README updated with "predefined_lists" usage --- README.md | 20 +++++++++++++++++++- config/config.php | 10 +++++----- src/Middleware.php | 11 +++++------ tests/BlacklistMiddlewareTest.php | 6 +++--- tests/WhitelistMiddlewareTest.php | 4 ++-- 5 files changed, 34 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 5fe5aa0..c2fd0dd 100755 --- a/README.md +++ b/README.md @@ -53,6 +53,23 @@ Route::middleware('ip_blacklist:3.3.3.3,4.4.4.4')->get('/', 'PostController@inde 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: + +```php +// 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'], +]; +``` + +```php +Route::middleware('ip_whitelist:my-list,my-list-2')->get('/', 'HomeController@index'); +// you can also mix predefined list with additional IP addresses +Route::middleware('ip_whitelist:my-list,my-list-2,5.5.5.5,6.6.6.6')->get('/', 'PostController@index'); +``` + ## Configuration Run this command to publish package config file: @@ -65,7 +82,8 @@ php artisan vendor:publish --provider="Orkhanahmadov\LaravelIpMiddleware\Laravel * `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 variable to look for IP address +* `custom_server_parameter` - Custom $_SERVER parameter to look for IP address +* `predefined_lists` - Predefined IP lists ### Testing diff --git a/config/config.php b/config/config.php index 218f485..7ee4da7 100755 --- a/config/config.php +++ b/config/config.php @@ -18,10 +18,10 @@ /* |-------------------------------------------------------------------------- - | Error code + | HTTP error code |-------------------------------------------------------------------------- | - | Error code when request gets rejected. + | HTTP error code when request gets rejected. | Default is 403 (Forbidden). | */ @@ -49,16 +49,16 @@ /* |-------------------------------------------------------------------------- - | Preconfigured IP list + | Predefined IP lists |-------------------------------------------------------------------------- | - | Here you can preconfigure IP list that middleware can use with list key name. + | Here you can predefine IP list that middleware can use with list key name. | You can use array list or comma separated string to set IP addresses. | Or you can use environment key. | */ - 'lists' => [ + 'predefined_lists' => [ // 'list-1' => ['1.1.1.1', '2.2.2.2'], // 'list-2' => '3.3.3.3,4.4.4.4', // 'list-3' => env('IP_WHITELIST'), // in .env file: IP_WHITELIST=5.5.5.5,6.6.6.6,7.7.7.7 diff --git a/src/Middleware.php b/src/Middleware.php index d331f11..816b9ad 100644 --- a/src/Middleware.php +++ b/src/Middleware.php @@ -61,18 +61,17 @@ protected function shouldCheck(): bool protected function ipList(array $list): array { $originalList = Arr::flatten($list); - $preconfiguredLists = $this->config->get('ip-middleware.lists'); + $predefinedLists = $this->config->get('ip-middleware.predefined_lists'); $finalList = []; - foreach ($originalList as $item) { - if (! isset($preconfiguredLists[$item])) { + if (! isset($predefinedLists[$item])) { $finalList[] = $item; } else { - if (is_array($preconfiguredLists[$item])) { - $ipAddresses = $preconfiguredLists[$item]; + if (is_array($predefinedLists[$item])) { + $ipAddresses = $predefinedLists[$item]; } else { - $ipAddresses = explode(',', $preconfiguredLists[$item]); + $ipAddresses = explode(',', $predefinedLists[$item]); } $finalList[] = $ipAddresses; } diff --git a/tests/BlacklistMiddlewareTest.php b/tests/BlacklistMiddlewareTest.php index ba14cc3..6ece179 100644 --- a/tests/BlacklistMiddlewareTest.php +++ b/tests/BlacklistMiddlewareTest.php @@ -58,16 +58,16 @@ public function testAllowsIfEnvironmentIsIgnored() $this->assertTrue($result); } - public function testBlocksWithPreconfiguredLists() + public function testBlocksWithPredefinedLists() { $this->expectException(HttpException::class); - config()->set('ip-middleware.lists', [ + config()->set('ip-middleware.predefined_lists', [ 'list-1' => '1.1.1.1,2.2.2.2', 'list-2' => ['3.3.3.3', '4.4.4.4'], 'list-3' => '5.5.5.5', ]); - $request = Request::create('/', 'GET', [], [], [], ['REMOTE_ADDR' => '4.4.4.4']); + $request = Request::create('/', 'GET', [], [], [], ['REMOTE_ADDR' => '2.2.2.2']); $this->middleware->handle($request, function () { }, 'list-1', 'list-2', 'list-3'); diff --git a/tests/WhitelistMiddlewareTest.php b/tests/WhitelistMiddlewareTest.php index a60cf46..9dd7ee8 100644 --- a/tests/WhitelistMiddlewareTest.php +++ b/tests/WhitelistMiddlewareTest.php @@ -72,9 +72,9 @@ public function testAllowsIfEnvironmentIsIgnored() $this->assertTrue($result); } - public function testAllowsWithPreconfiguredLists() + public function testAllowsWithPredefinedLists() { - config()->set('ip-middleware.lists', [ + config()->set('ip-middleware.predefined_lists', [ 'list-1' => '1.1.1.1,2.2.2.2', 'list-2' => ['3.3.3.3', '4.4.4.4'], 'list-3' => '5.5.5.5',