Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
viitorcloud-rushikesh committed Jan 11, 2019
0 parents commit ebc73b5
Show file tree
Hide file tree
Showing 8 changed files with 327 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
composer.phar
composer.lock
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Changelog

All notable changes to `laravel-ip-gateway` will be documented in this file.

## 1.0.0 - 2018-01-10

- First release
32 changes: 32 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Contributing

Contributions are **welcome** and will be fully **credited**.

We accept contributions via Pull Requests on [Github](https://github.com/viitorcloudtechnologies/laravel-word-refiner).


## Pull Requests

- **[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).

- **Add tests!** - Your patch won't be accepted if it doesn't have tests.

- **Document any change in behaviour** - Make sure the `README.md` and any other relevant documentation are kept up-to-date.

- **Consider our release cycle** - We try to follow [SemVer v2.0.0](http://semver.org/). Randomly breaking public APIs is not an option.

- **Create feature branches** - Don't ask us to pull from your master branch.

- **One pull request per feature** - If you want to do more than one thing, send multiple pull requests.

- **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.


## Running Tests

``` bash
$ composer test
```


**Happy coding**!
63 changes: 63 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# IP gateway for laravel

[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE.md)

## Requirements

Laravel 5.4 >

## Features

* The Laravel Ip gateway package helps you to blacklist or whitelist IP to prevent unauthorized access.

* 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

## Installation

You can install the package via composer:

```bash
composer require viitorcloud/laravel-ip-gateway
```

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.

```bash
php artisan vendor:publish --provider="LaravelIpGateway\IpGatewayProvider"
```

### Config Usage

* `enable_package` is used for enable/disable access protection.

* `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`.

* You can authenticated IPs through register route groups in `middleware`.

* `redirect_route_to` will access URL, To redirect if denied.

* You can define all your whitelist or blacklist IP addresses inside `ip-list`.

### Changelog

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

## Contributing

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

## Security

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

## Credits

- [All Contributors](../../contributors)

## License

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

## Notes

**You can create as many whitelists or blacklist groups as you wish to protect access**
41 changes: 41 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"name": "vcian/laravel-ip-gateway",
"description": "Blacklist or Whitelist IP for routes",
"keywords": [
"ip",
"gateway",
"whitelist",
"blacklist",
"laravel",
"firewall",
"prevent",
"access",
"authentication",
"denied"
],
"license": "MIT",
"authors": [
{
"name": "Rushikesh Soni",
"email": "[email protected]",
"role": "Creator"
}
],
"require": {
"laravel/framework": ">5.4",
"php": ">=5.4.0"
},
"autoload": {
"psr-4": {
"LaravelIpGateway\\": "src/"
}
},
"extra": {
"laravel": {
"providers": [
"LaravelIpGateway\\IpGatewayProvider"
]
}
},
"minimum-stability": "dev"
}
52 changes: 52 additions & 0 deletions publishable/config/ip-gateway.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

return [
/*
* Enable / disable package
*
* @type boolean
*/
'enable_package' => true,

/*
* Enable / disable firewall
*
* Enable will block Blacklist IP
* Disable will allow only Whitelist IP
*
* @type boolean
*/
'enable_blacklist' => true,

/*
* Enable IP detection for middleware
*
* You can use middleware name ('auth')
*
* @var array
*/
'middleware' => [

],

/*
* Url to redirect if blocked
*
* You can use route url (/404);
*
* @type string
*/
'redirect_route_to' => '',

/*
* Whitelisted and blacklisted IP addresses
*
* Examples of IP address
* '127.0.0.0',
*
* @type array
*/
'ip-list' => [

],
];
55 changes: 55 additions & 0 deletions src/IpGatewayProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace LaravelIpGateway;

use Illuminate\Support\ServiceProvider;
use LaravelIpGateway\Middleware\IpGatewayMiddleware;

/**
* Class IpGatewayProvider
*
* @package LaravelIpGateway
*/
class IpGatewayProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*
* @return void
*/
public function boot()
{
$router = $this->app['router'];

if (config('ip-gateway')) {
foreach (config('ip-gateway.middleware') as $middlewareName) {
$router->pushMiddlewareToGroup($middlewareName, IpGatewayMiddleware::class);
}
}
}

/**
* Register the application services.
*
* @return void
*/
public function register()
{
$this->publishFiles();
}

/**
* Publish files
*/
public function publishFiles()
{
$publishableFiles = [
__DIR__ . '/../publishable/config/ip-gateway.php' => config_path('ip-gateway.php'),
];

foreach ($publishableFiles as $storedPath => $publishPath) {
$this->publishes([$storedPath => $publishPath]);
}

}
}
75 changes: 75 additions & 0 deletions src/middleware/IpGatewayMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

namespace LaravelIpGateway\Middleware;

use Closure;
use Illuminate\Support\Facades\Log;

/**
* Class IpGatewayMiddleware
*
* @package LaravelIpGateway\Middleware
*/
class IpGatewayMiddleware
{
protected $ipList;

/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
*
* @return mixed
*/
public function handle($request, Closure $next)
{
$prohibitRequest = false;

if (config('ip-gateway')) {
if (config('ip-gateway.enable_package') === true) {

if (config('ip-gateway.enable_blacklist') === true) {
foreach ($request->getClientIps() as $ip) {
if ($this->grantIpAddress($ip)) {
$prohibitRequest = true;
Log::warning($ip . ' IP address has tried to access.');
}
}
}

if (config('ip-gateway.enable_blacklist') === false) {
foreach ($request->getClientIps() as $ip) {
if (!$this->grantIpAddress($ip)) {
$prohibitRequest = true;
Log::warning($ip . ' IP address has tried to access.');
}
}
}
}
}

if ($prohibitRequest === false) {
return $next($request);
} else {
if (config('ip-gateway.redirect_route_to') != '') {
return redirect(config('ip-gateway.redirect_route_to'));
} else {
return redirect('/404');
}
}
}

/**
* Grant IP address
*
* @param $ip
*
* @return bool
*/
protected function grantIpAddress($ip)
{
$this->ipList = config('ip-gateway.ip-list');
return in_array($ip, $this->ipList);
}
}

0 comments on commit ebc73b5

Please sign in to comment.