Skip to content

Commit

Permalink
README updated with "predefined_lists" usage
Browse files Browse the repository at this point in the history
  • Loading branch information
orkhanahmadov committed Sep 19, 2019
1 parent 7d859f6 commit 2522f3d
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 17 deletions.
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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

Expand Down
10 changes: 5 additions & 5 deletions config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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).
|
*/
Expand Down Expand Up @@ -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
Expand Down
11 changes: 5 additions & 6 deletions src/Middleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
6 changes: 3 additions & 3 deletions tests/BlacklistMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
4 changes: 2 additions & 2 deletions tests/WhitelistMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit 2522f3d

Please sign in to comment.