Skip to content

Commit ebc73b5

Browse files
author
viitorcloud-rushikesh
committed
Initial Commit
0 parents  commit ebc73b5

File tree

8 files changed

+327
-0
lines changed

8 files changed

+327
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
composer.phar
2+
composer.lock

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Changelog
2+
3+
All notable changes to `laravel-ip-gateway` will be documented in this file.
4+
5+
## 1.0.0 - 2018-01-10
6+
7+
- First release

CONTRIBUTING.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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**!

README.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# IP gateway for laravel
2+
3+
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE.md)
4+
5+
## Requirements
6+
7+
Laravel 5.4 >
8+
9+
## Features
10+
11+
* The Laravel Ip gateway package helps you to blacklist or whitelist IP to prevent unauthorized access.
12+
13+
* Since blacklists deny access to specific entities, they are best used when a limited number of items need to be denied access. When most entities need to be denied access, a whitelist approach is more efficient
14+
15+
## Installation
16+
17+
You can install the package via composer:
18+
19+
```bash
20+
composer require viitorcloud/laravel-ip-gateway
21+
```
22+
23+
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.
24+
25+
```bash
26+
php artisan vendor:publish --provider="LaravelIpGateway\IpGatewayProvider"
27+
```
28+
29+
### Config Usage
30+
31+
* `enable_package` is used for enable/disable access protection.
32+
33+
* `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`.
34+
35+
* You can authenticated IPs through register route groups in `middleware`.
36+
37+
* `redirect_route_to` will access URL, To redirect if denied.
38+
39+
* You can define all your whitelist or blacklist IP addresses inside `ip-list`.
40+
41+
### Changelog
42+
43+
Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.
44+
45+
## Contributing
46+
47+
Please see [CONTRIBUTING](CONTRIBUTING.md) for details.
48+
49+
## Security
50+
51+
If you discover any security-related issues, please email vishal@viitorcloud.com or ruchit.patel@viitor.cloud instead of using the issue tracker.
52+
53+
## Credits
54+
55+
- [All Contributors](../../contributors)
56+
57+
## License
58+
59+
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.
60+
61+
## Notes
62+
63+
**You can create as many whitelists or blacklist groups as you wish to protect access**

composer.json

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{
2+
"name": "vcian/laravel-ip-gateway",
3+
"description": "Blacklist or Whitelist IP for routes",
4+
"keywords": [
5+
"ip",
6+
"gateway",
7+
"whitelist",
8+
"blacklist",
9+
"laravel",
10+
"firewall",
11+
"prevent",
12+
"access",
13+
"authentication",
14+
"denied"
15+
],
16+
"license": "MIT",
17+
"authors": [
18+
{
19+
"name": "Rushikesh Soni",
20+
"email": "rushikesh.soni@viitor.cloud",
21+
"role": "Creator"
22+
}
23+
],
24+
"require": {
25+
"laravel/framework": ">5.4",
26+
"php": ">=5.4.0"
27+
},
28+
"autoload": {
29+
"psr-4": {
30+
"LaravelIpGateway\\": "src/"
31+
}
32+
},
33+
"extra": {
34+
"laravel": {
35+
"providers": [
36+
"LaravelIpGateway\\IpGatewayProvider"
37+
]
38+
}
39+
},
40+
"minimum-stability": "dev"
41+
}

publishable/config/ip-gateway.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
return [
4+
/*
5+
* Enable / disable package
6+
*
7+
* @type boolean
8+
*/
9+
'enable_package' => 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 ('auth')
25+
*
26+
* @var array
27+
*/
28+
'middleware' => [
29+
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.0',
46+
*
47+
* @type array
48+
*/
49+
'ip-list' => [
50+
51+
],
52+
];

src/IpGatewayProvider.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
namespace LaravelIpGateway;
4+
5+
use Illuminate\Support\ServiceProvider;
6+
use LaravelIpGateway\Middleware\IpGatewayMiddleware;
7+
8+
/**
9+
* Class IpGatewayProvider
10+
*
11+
* @package LaravelIpGateway
12+
*/
13+
class IpGatewayProvider extends ServiceProvider
14+
{
15+
/**
16+
* Bootstrap the application services.
17+
*
18+
* @return void
19+
*/
20+
public function boot()
21+
{
22+
$router = $this->app['router'];
23+
24+
if (config('ip-gateway')) {
25+
foreach (config('ip-gateway.middleware') as $middlewareName) {
26+
$router->pushMiddlewareToGroup($middlewareName, IpGatewayMiddleware::class);
27+
}
28+
}
29+
}
30+
31+
/**
32+
* Register the application services.
33+
*
34+
* @return void
35+
*/
36+
public function register()
37+
{
38+
$this->publishFiles();
39+
}
40+
41+
/**
42+
* Publish files
43+
*/
44+
public function publishFiles()
45+
{
46+
$publishableFiles = [
47+
__DIR__ . '/../publishable/config/ip-gateway.php' => config_path('ip-gateway.php'),
48+
];
49+
50+
foreach ($publishableFiles as $storedPath => $publishPath) {
51+
$this->publishes([$storedPath => $publishPath]);
52+
}
53+
54+
}
55+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
namespace LaravelIpGateway\Middleware;
4+
5+
use Closure;
6+
use Illuminate\Support\Facades\Log;
7+
8+
/**
9+
* Class IpGatewayMiddleware
10+
*
11+
* @package LaravelIpGateway\Middleware
12+
*/
13+
class IpGatewayMiddleware
14+
{
15+
protected $ipList;
16+
17+
/**
18+
* Handle an incoming request.
19+
*
20+
* @param \Illuminate\Http\Request $request
21+
* @param \Closure $next
22+
*
23+
* @return mixed
24+
*/
25+
public function handle($request, Closure $next)
26+
{
27+
$prohibitRequest = false;
28+
29+
if (config('ip-gateway')) {
30+
if (config('ip-gateway.enable_package') === true) {
31+
32+
if (config('ip-gateway.enable_blacklist') === true) {
33+
foreach ($request->getClientIps() as $ip) {
34+
if ($this->grantIpAddress($ip)) {
35+
$prohibitRequest = true;
36+
Log::warning($ip . ' IP address has tried to access.');
37+
}
38+
}
39+
}
40+
41+
if (config('ip-gateway.enable_blacklist') === false) {
42+
foreach ($request->getClientIps() as $ip) {
43+
if (!$this->grantIpAddress($ip)) {
44+
$prohibitRequest = true;
45+
Log::warning($ip . ' IP address has tried to access.');
46+
}
47+
}
48+
}
49+
}
50+
}
51+
52+
if ($prohibitRequest === false) {
53+
return $next($request);
54+
} else {
55+
if (config('ip-gateway.redirect_route_to') != '') {
56+
return redirect(config('ip-gateway.redirect_route_to'));
57+
} else {
58+
return redirect('/404');
59+
}
60+
}
61+
}
62+
63+
/**
64+
* Grant IP address
65+
*
66+
* @param $ip
67+
*
68+
* @return bool
69+
*/
70+
protected function grantIpAddress($ip)
71+
{
72+
$this->ipList = config('ip-gateway.ip-list');
73+
return in_array($ip, $this->ipList);
74+
}
75+
}

0 commit comments

Comments
 (0)