├── LICENSE ├── README.md ├── composer.json ├── config └── ipchecker.php ├── database └── migrations │ └── 2019_11_27_085806_create_ip_lists_table.php ├── doc ├── iplist.PNG ├── json.PNG └── view.PNG ├── resources └── views │ ├── error.blade.php │ └── index.blade.php ├── routes └── web.php └── src ├── Contracts └── IpCheckerInterface.php ├── DBDriver.php ├── FileDriver.php ├── Http ├── Controllers │ └── IpCheckerController.php └── Middleware │ └── IpChecker.php ├── IpCheckerServiceProvider.php ├── IpList.php └── lang ├── en └── messages.php └── tr └── messages.php /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Hayri Can BARÇIN 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. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Laravel IP Checker 2 | [![Latest Stable Version](https://poser.pugx.org/hayrican/ipchecker/version)](https://packagist.org/packages/hayrican/ipchecker) 3 | [![Total Downloads](https://poser.pugx.org/hayrican/ipchecker/downloads)](https://packagist.org/packages/hayrican/ipchecker) 4 | [![Latest Unstable Version](https://poser.pugx.org/hayrican/ipchecker/v/unstable)](//packagist.org/packages/hayrican/ipchecker) 5 | [![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/HayriCan/laravel-ip-checker/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/HayriCan/laravel-ip-checker/?branch=master) 6 | [![Codacy Badge](https://api.codacy.com/project/badge/Grade/1d1eae86dc6549728e6ce5b7f0660f49)](https://www.codacy.com/manual/HayriCan/laravel-ip-checker?utm_source=github.com&utm_medium=referral&utm_content=HayriCan/laravel-ip-checker&utm_campaign=Badge_Grade) 7 | [![Build Status](https://scrutinizer-ci.com/g/HayriCan/laravel-ip-checker/badges/build.png?b=master)](https://scrutinizer-ci.com/g/HayriCan/laravel-ip-checker/build-status/master) 8 | [![Code Intelligence Status](https://scrutinizer-ci.com/g/HayriCan/laravel-ip-checker/badges/code-intelligence.svg?b=master)](https://scrutinizer-ci.com/code-intelligence) 9 | [![License](https://poser.pugx.org/hayrican/ipchecker/license)](https://packagist.org/packages/hayrican/ipchecker) 10 | 11 | This package provides restricted access via IP Address to your application. 12 | 13 | ## Installation 14 | To get started, you should add the `hayrican/ipchecker` Composer dependency to your project: 15 | ``` 16 | composer require hayrican/ipchecker 17 | ``` 18 | 19 | #### Service Provider (Laravel Older 5.5) 20 | 21 | ##### If you are using later version of Laravel 5.5, you can skip this step. 22 | 23 | Register provider on your `config/app.php` file. 24 | ```php 25 | 'providers' => [ 26 | ..., 27 | HayriCan\IpChecker\IpCheckerServiceProvider::class, 28 | ] 29 | ``` 30 | 31 | ## Configuration 32 | You should publish vendor for configuration file. 33 | ```bash 34 | $ php artisan vendor:publish --tag="ipchecker" 35 | ``` 36 | 37 | ##### Driver 38 | The config file is called *ipchecker.php*. Currently supported drivers are `db` and `file` 39 | 40 | Default driver is `file` and ipchecker will use file to record ip addresses. But if you want to use Database for records, migrate table by using 41 | 42 | ```bash 43 | php artisan migrate 44 | ``` 45 | You have to change driver to `db` before migrate. Otherwise it will not migrate the table. 46 | 47 | ##### Route Group Middleware 48 | ```php 49 | 'api_middleware'=>'api', 50 | 'web_middleware'=>'web', 51 | ``` 52 | If your routes has different middleware then these default values you can change them in here. 53 | These route middleware need for filtering response of denial access. 54 | 55 | ## Localization 56 | When you call ``php artisan vendor:publish --tag="ipchecker"`` command, it will also publish `lang` file to your `resources/lang` directory. You can change all fields as you desire. 57 | 58 | 59 | # Package Usage 60 | ## 1.Add Middleware 61 | Add middleware named `ipchecker` to the route or controller you want to log data 62 | 63 | ```php 64 | // in app.php or web.php 65 | 66 | Route::group(['middleware' => ['ipchecker']], function () { 67 | Route::get('test',function (){ 68 | dd('Test API'); 69 | }); 70 | }); 71 | ``` 72 | When try to access this route it will check your IP Address. If you ip address is not on the list you will get response 73 | ```php 74 | { 75 | "success": false, 76 | "code": 250, 77 | "message": "Your IP Address not in the list." 78 | } 79 | ``` 80 | Otherwise you will access to the route. 81 | 82 | ## 2.Add IP Address 83 | 84 | Up to default config dashboard can be accessible via ***yourdomain.com/iplists*** but it is configurable from config file `ipchecker.php` 85 | 86 | ```php 87 | { 88 | "settings"=>[ 89 | "auth" => false, 90 | "admin_id"=>[], 91 | "route_prefix"=> "", 92 | ], 93 | } 94 | ``` 95 | 96 | If you want to guard this page just change `"auth"` to `true` and it require `"auth"` middleware. 97 | 98 | When you enabled auth you could add admin users id to ``"admin_id"`` array. 99 | If leave ``"admin_id"`` array empty, all users can has access to IP Checker dashboard 100 | ```php 101 | { 102 | "settings"=>[ 103 | "auth"=> true, 104 | "admin_id"=>[2,5], 105 | "route_prefix"=> "", 106 | ], 107 | } 108 | ``` 109 | 110 | Also you can change the route prefix of this dashboard. If you change `"route_prefix"` to `"foo"` your dashboard will be accessible via ***yourdomain.com/foo/iplists***. 111 | 112 | 113 | 114 | ![Screencast1](doc/iplist.PNG) 115 | 116 | You can add IP Address in here. 117 | 118 | ## Author 119 | 120 | [Hayri Can BARÇIN] 121 | Email: [Contact Me] 122 | 123 | ## License 124 | 125 | This project is licensed under the MIT License - see the [License File](LICENSE) for details 126 | 127 | 128 | 129 | [//]: # (These are reference links used in the body of this note and get stripped out when the markdown processor does its job. There is no need to format nicely because it shouldn't be seen. Thanks SO - http://stackoverflow.com/questions/4823468/store-comments-in-markdown-syntax) 130 | [Hayri Can BARÇIN]: 131 | [Contact Me]: 132 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "hayrican/ipchecker", 3 | "description": "Laravel package for providing restricted access by IP Address to your application", 4 | "homepage": "https://github.com/HayriCan/laravel-ip-checker", 5 | "keywords": [ 6 | "laravel", 7 | "ip address", 8 | "restricted", 9 | "access", 10 | "helper", 11 | "php7" 12 | ], 13 | "type": "library", 14 | "license": "MIT", 15 | "authors": [ 16 | { 17 | "name": "hayrican", 18 | "email": "hayricanbarcin@gmail.com" 19 | } 20 | ], 21 | "minimum-stability": "dev", 22 | "require": { 23 | "ext-json": "*", 24 | "ext-simplexml": "*" 25 | }, 26 | "autoload": { 27 | "psr-4": { 28 | "HayriCan\\IpChecker\\": "src/" 29 | } 30 | }, 31 | "extra": { 32 | "laravel": { 33 | "providers": [ 34 | "HayriCan\\IpChecker\\IpCheckerServiceProvider" 35 | ] 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /config/ipchecker.php: -------------------------------------------------------------------------------- 1 | 7 | * @license http://www.opensource.org/licenses/mit-license.php MIT 8 | * @link https://github.com/HayriCan/laravel-ip-checker 9 | */ 10 | 11 | return [ 12 | 13 | /* 14 | |-------------------------------------------------------------------------- 15 | | Driver 16 | |-------------------------------------------------------------------------- 17 | | Driver option currently supports "db" and "file" 18 | | 19 | */ 20 | 21 | "driver" => "file", 22 | 23 | /* 24 | |-------------------------------------------------------------------------- 25 | | File Path and File Name 26 | |-------------------------------------------------------------------------- 27 | | If you switch driver to "file" it will write Allowed Ip Addresses on this path 28 | | 29 | */ 30 | 31 | "filepath"=>"ipchecker", 32 | "filename" => "iplist.php", 33 | 34 | 35 | /* 36 | |-------------------------------------------------------------------------- 37 | | Dashboard Settings 38 | |-------------------------------------------------------------------------- 39 | | If you change "auth" to true your IpChecker dashboard will require "auth" middleware 40 | | When you enabled auth you could add admin users id to "admin_id" array. 41 | | If leave "admin_id" array empty, all users can has access to IP Checker dashboard 42 | | You can change IpChecker dashboard route prefix from "route_prefix" 43 | | 44 | */ 45 | 46 | "settings"=>[ 47 | "auth"=> false, 48 | "admin_id"=>[], 49 | "route_prefix"=> "", 50 | ], 51 | 52 | /* 53 | |-------------------------------------------------------------------------- 54 | | Route Group Middleware 55 | |-------------------------------------------------------------------------- 56 | | You can specify your "api.php" and "web.php" route group middleware for filtering response type. 57 | | 58 | | 59 | */ 60 | 61 | "api_middleware"=>"api", 62 | "web_middleware"=>"web", 63 | 64 | ]; -------------------------------------------------------------------------------- /database/migrations/2019_11_27_085806_create_ip_lists_table.php: -------------------------------------------------------------------------------- 1 | 7 | * @license http://www.opensource.org/licenses/mit-license.php MIT 8 | * @link https://github.com/HayriCan/laravel-ip-checker 9 | */ 10 | 11 | use Illuminate\Support\Facades\Schema; 12 | use Illuminate\Database\Schema\Blueprint; 13 | use Illuminate\Database\Migrations\Migration; 14 | 15 | class CreateIpListsTable extends Migration 16 | { 17 | /** 18 | * Run the migrations. 19 | * 20 | * @return void 21 | */ 22 | public function up() 23 | { 24 | Schema::create('ip_lists', function (Blueprint $table) { 25 | $table->bigIncrements('id'); 26 | $table->string('group'); 27 | $table->string('definition'); 28 | $table->string('ip'); 29 | $table->timestamps(); 30 | }); 31 | } 32 | 33 | /** 34 | * Reverse the migrations. 35 | * 36 | * @return void 37 | */ 38 | public function down() 39 | { 40 | Schema::dropIfExists('ip_lists'); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /doc/iplist.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HayriCan/laravel-ip-checker/cc58cc772db34bb4901602f63ac94606beb63916/doc/iplist.PNG -------------------------------------------------------------------------------- /doc/json.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HayriCan/laravel-ip-checker/cc58cc772db34bb4901602f63ac94606beb63916/doc/json.PNG -------------------------------------------------------------------------------- /doc/view.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HayriCan/laravel-ip-checker/cc58cc772db34bb4901602f63ac94606beb63916/doc/view.PNG -------------------------------------------------------------------------------- /resources/views/error.blade.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | @yield('title') 8 | 9 | 10 | 11 | 12 | 13 | 49 | 50 | 51 |
52 |
53 | {{$code}} 54 |
55 | 56 |
57 | {{$message}} 58 |
59 |
60 | 61 | 62 | -------------------------------------------------------------------------------- /resources/views/index.blade.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | {{ config('app.name', 'APILogger') }} 9 | 10 | 11 | 12 | 13 | 14 | 15 | 137 | 138 | 139 |
140 | 147 | 148 |
149 |
150 |
151 |

{{trans('ipchecker::messages.ip_list')}}

152 |
153 | 157 |
158 |
159 | @if ($message = \Session::get('success')) 160 |
161 | 162 | {{ $message }} 163 |
164 | @endif 165 | 166 | @if ($message = \Session::get('error')) 167 |
168 | 169 | {{ $message }} 170 |
171 | @endif 172 | 173 | @if ($message = \Session::get('info')) 174 |
175 | 176 | {{ $message }} 177 |
178 | @endif 179 | 180 | @if ($errors->any()) 181 |
182 |
    183 | @foreach ($errors->all() as $error) 184 |
  • {{ $error }}
  • 185 | @endforeach 186 |
187 |
188 | @endif 189 |
190 | @forelse ($iplist as $key => $ip) 191 |
192 |
193 |
{{trans('ipchecker::messages.group')}} : {{$ip->group}}
194 |
{{trans('ipchecker::messages.definition')}} : {{$ip->definition}}
195 |
IP : {{$ip->ip}}
196 | 197 | 201 | 202 |
203 |
204 | @empty 205 |
206 | {{trans('ipchecker::messages.no_record')}} 207 |
208 | @endforelse 209 |
210 |
211 |
212 | 213 | 242 | 243 | 269 |
270 | 271 | 272 | 273 | 279 | 280 | 281 | 282 | 283 | -------------------------------------------------------------------------------- /routes/web.php: -------------------------------------------------------------------------------- 1 | 7 | * @license http://www.opensource.org/licenses/mit-license.php MIT 8 | * @link https://github.com/HayriCan/laravel-ip-checker 9 | */ 10 | 11 | Route::group(['prefix'=>config('ipchecker.settings.route_prefix')], function () { 12 | Route::get('/iplists', 'HayriCan\IpChecker\Http\Controllers\IpCheckerController@index')->name('iplist.index'); 13 | Route::post('/ip-add', 'HayriCan\IpChecker\Http\Controllers\IpCheckerController@add')->name('iplist.add'); 14 | Route::delete('/ip-delete', 'HayriCan\IpChecker\Http\Controllers\IpCheckerController@delete')->name('iplist.delete'); 15 | }); 16 | -------------------------------------------------------------------------------- /src/Contracts/IpCheckerInterface.php: -------------------------------------------------------------------------------- 1 | 9 | * @license http://www.opensource.org/licenses/mit-license.php MIT 10 | * @link https://github.com/HayriCan/laravel-ip-checker 11 | */ 12 | interface IpCheckerInterface{ 13 | 14 | public function getIpArray(); 15 | /** 16 | * @return mixed 17 | */ 18 | public function getIpList(); 19 | 20 | public function saveIp($array); 21 | 22 | public function deleteIp($ipAddress); 23 | } -------------------------------------------------------------------------------- /src/DBDriver.php: -------------------------------------------------------------------------------- 1 | 9 | * @license http://www.opensource.org/licenses/mit-license.php MIT 10 | * @link https://github.com/HayriCan/laravel-ip-checker 11 | */ 12 | 13 | use HayriCan\IpChecker\Contracts\IpCheckerInterface; 14 | 15 | class DBDriver implements IpCheckerInterface{ 16 | 17 | /** 18 | * Model for saving logs 19 | * 20 | * @var [type] 21 | */ 22 | protected $model; 23 | protected $ipList = []; 24 | 25 | public function __construct(IpList $ipList) 26 | { 27 | $this->model = $ipList; 28 | } 29 | 30 | /** 31 | * @return array 32 | */ 33 | public function getIpArray() 34 | { 35 | foreach ($this->model->all() as $record){ 36 | array_push($this->ipList,$record->ip); 37 | } 38 | $ipList = $this->ipList ?? []; 39 | 40 | return $ipList; 41 | } 42 | 43 | /** 44 | * @return IpList[]|\Illuminate\Database\Eloquent\Collection|mixed 45 | */ 46 | public function getIpList() 47 | { 48 | return $this->model->all(); 49 | } 50 | 51 | /** 52 | * @param $array 53 | * @return bool 54 | */ 55 | public function saveIp($array) 56 | { 57 | $result = IpList::create($array); 58 | if (!$result){ 59 | return false; 60 | } 61 | 62 | return true; 63 | } 64 | 65 | /** 66 | * @param $ipAddress 67 | * @return bool 68 | */ 69 | public function deleteIp($ipAddress) 70 | { 71 | $response = false; 72 | $ipList = IpList::where('ip',$ipAddress)->first(); 73 | if ($ipList){ 74 | IpList::where('id',$ipList->id)->delete(); 75 | $response = true; 76 | } 77 | 78 | return $response; 79 | } 80 | } -------------------------------------------------------------------------------- /src/FileDriver.php: -------------------------------------------------------------------------------- 1 | 9 | * @license http://www.opensource.org/licenses/mit-license.php MIT 10 | * @link https://github.com/HayriCan/laravel-ip-checker 11 | */ 12 | 13 | use Carbon\Carbon; 14 | use Illuminate\Support\Facades\File; 15 | use HayriCan\IpChecker\Contracts\IpCheckerInterface; 16 | 17 | class FileDriver implements IpCheckerInterface 18 | { 19 | 20 | /** 21 | * file path to save the logs 22 | */ 23 | protected $path; 24 | protected $ipList = []; 25 | 26 | public function __construct() 27 | { 28 | $this->path = storage_path(config('ipchecker.filepath')); 29 | } 30 | 31 | /** 32 | * @return array 33 | */ 34 | public function getIpArray() 35 | { 36 | if (is_dir($this->path)) { 37 | $files = scandir($this->path); 38 | 39 | foreach ($files as $file) { 40 | if (!is_dir($file)) { 41 | $lines = file($this->path.DIRECTORY_SEPARATOR.$file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); 42 | foreach ($lines as $line) { 43 | $contentarr = explode(";", $line); 44 | array_push($this->ipList, $contentarr[2]); 45 | } 46 | } 47 | } 48 | $ipList = $this->ipList ?? []; 49 | 50 | return $ipList; 51 | } 52 | 53 | return []; 54 | } 55 | 56 | /** 57 | * @return array|\Illuminate\Support\Collection|mixed 58 | */ 59 | public function getIpList() 60 | { 61 | if (is_dir($this->path)) { 62 | $files = scandir($this->path); 63 | 64 | foreach ($files as $file) { 65 | if (!is_dir($file)) { 66 | $lines = file($this->path.DIRECTORY_SEPARATOR.$file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); 67 | foreach ($lines as $line) { 68 | $contentarr = explode(";", $line); 69 | array_push($this->ipList, $this->mapArrayToModel($contentarr)); 70 | } 71 | } 72 | } 73 | return collect($this->ipList); 74 | } 75 | 76 | return []; 77 | } 78 | 79 | /** 80 | * @param $array 81 | * @return bool 82 | */ 83 | public function saveIp($array) 84 | { 85 | $array['created_at']=Carbon::now()->toDateTimeString(); 86 | $filename = $this->getFilename(); 87 | 88 | $contents = implode(";", $array); 89 | 90 | File::makeDirectory($this->path, 0777, true, true); 91 | 92 | File::append(($this->path.DIRECTORY_SEPARATOR.$filename), $contents.PHP_EOL); 93 | 94 | return true; 95 | } 96 | 97 | /** 98 | * @param $ipAddress 99 | * @return bool 100 | */ 101 | public function deleteIp($ipAddress) 102 | { 103 | if (is_dir($this->path)) { 104 | $files = scandir($this->path); 105 | 106 | foreach ($files as $file) { 107 | if (!is_dir($file)) { 108 | $lines = file($this->path.DIRECTORY_SEPARATOR.$file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); 109 | foreach ($lines as $line) { 110 | if (strpos($line, $ipAddress)){ 111 | $contents = file_get_contents($this->path.DIRECTORY_SEPARATOR.$file); 112 | $contents = str_replace($line,'',$contents); 113 | file_put_contents($this->path.DIRECTORY_SEPARATOR.$file,$contents); 114 | return true; 115 | } 116 | } 117 | } 118 | } 119 | return false; 120 | } 121 | 122 | return false; 123 | } 124 | 125 | /** 126 | * Helper method for mapping array into models 127 | * 128 | * @param array $data 129 | * @return IpList 130 | */ 131 | public function mapArrayToModel(array $data){ 132 | $model = new IpList(); 133 | $model->group = $data[0]; 134 | $model->definition = $data[1]; 135 | $model->ip = $data[2]; 136 | $model->created_at = Carbon::make($data[3]); 137 | return $model; 138 | } 139 | 140 | /** 141 | * get log file if defined in constants 142 | * 143 | * @return string 144 | */ 145 | public function getFilename() 146 | { 147 | $filename = 'iplist.php'; 148 | if (config('ipchecker.filename')){ 149 | $filename = config('ipchecker.filename'); 150 | } 151 | 152 | return $filename; 153 | } 154 | } 155 | -------------------------------------------------------------------------------- /src/Http/Controllers/IpCheckerController.php: -------------------------------------------------------------------------------- 1 | 9 | * @license http://www.opensource.org/licenses/mit-license.php MIT 10 | * @link https://github.com/HayriCan/laravel-ip-checker 11 | */ 12 | 13 | use HayriCan\IpChecker\Contracts\IpCheckerInterface; 14 | use App\Http\Controllers\Controller; 15 | use Illuminate\Http\Request; 16 | use Illuminate\Support\Facades\Auth; 17 | use Illuminate\Support\Facades\Validator; 18 | 19 | class IpCheckerController extends Controller 20 | { 21 | /** 22 | * Create a new controller instance. 23 | * 24 | * @return void 25 | */ 26 | public function __construct() 27 | { 28 | $this->middleware(['web']); 29 | if (config('ipchecker.settings.auth')) { 30 | $this->middleware(['web','auth']); 31 | $this->middleware(function ($request, $next) { 32 | if (!empty(config('ipchecker.settings.admin_id'))){ 33 | if (!in_array(Auth::id(),config('ipchecker.settings.admin_id'))){ 34 | return abort(404); 35 | } 36 | } 37 | 38 | return $next($request); 39 | }); 40 | } 41 | } 42 | 43 | /** 44 | * Show the application dashboard. 45 | * 46 | * @param IpCheckerInterface $ipchecker 47 | * @return \Illuminate\Contracts\Support\Renderable 48 | */ 49 | public function index(IpCheckerInterface $ipchecker) 50 | { 51 | $iplist = $ipchecker->getIpList(); 52 | 53 | if(count($iplist)>0){ 54 | $iplist = $iplist->sortByDesc('created_at'); 55 | } 56 | 57 | return view('ipchecker::index',compact('iplist')); 58 | } 59 | 60 | /** 61 | * @param Request $request 62 | * @param IpCheckerInterface $ipchecker 63 | * @return \Illuminate\Http\RedirectResponse 64 | */ 65 | public function add(Request $request,IpCheckerInterface $ipchecker) 66 | { 67 | $request_data = $request->all(); 68 | $request_validation = array( 69 | 'group'=>'required|string', 70 | 'definition'=>'required|string', 71 | 'ip'=>'required|ip', 72 | ); 73 | $validator = Validator::make($request_data, $request_validation); 74 | if ($validator->fails()) { 75 | return redirect()->back()->withErrors($validator->errors()); 76 | } 77 | 78 | if (!in_array($request->input('ip'),$ipchecker->getIpArray())){ 79 | $ipchecker->saveIp(array( 80 | 'group'=>$request->input('group'), 81 | 'definition'=>$request->input('definition'), 82 | 'ip'=>$request->input('ip'), 83 | )); 84 | 85 | return redirect()->back()->with('success',trans('ipchecker::messages.ip_success')); 86 | } 87 | 88 | return redirect()->back()->with('error',trans('ipchecker::messages.ip_error')); 89 | } 90 | 91 | /** 92 | * @param Request $request 93 | * @param IpCheckerInterface $ipchecker 94 | * @return \Illuminate\Http\RedirectResponse 95 | */ 96 | public function delete(Request $request,IpCheckerInterface $ipchecker) 97 | { 98 | $ipchecker->deleteIp($request->input('ipAddress')); 99 | 100 | return redirect()->back()->with('info',trans('ipchecker::messages.ip_delete')); 101 | } 102 | } 103 | -------------------------------------------------------------------------------- /src/Http/Middleware/IpChecker.php: -------------------------------------------------------------------------------- 1 | 9 | * @license http://www.opensource.org/licenses/mit-license.php MIT 10 | * @link https://github.com/HayriCan/laravel-ip-checker 11 | */ 12 | 13 | use HayriCan\IpChecker\Contracts\IpCheckerInterface; 14 | use Closure; 15 | 16 | class IpChecker 17 | { 18 | protected $allowedIps; 19 | 20 | public function __construct(IpCheckerInterface $ipChecker) 21 | { 22 | $this->allowedIps = $ipChecker; 23 | } 24 | 25 | /** 26 | * Handle an incoming request. 27 | * 28 | * @param $request 29 | * @param Closure $next 30 | * @return \Illuminate\Http\JsonResponse|mixed 31 | */ 32 | public function handle($request, Closure $next) 33 | { 34 | if (!in_array($request->ip(), $this->allowedIps->getIpArray())) { 35 | $code = trans('ipchecker::messages.denied_access.code'); 36 | $message = trans('ipchecker::messages.denied_access.message'); 37 | 38 | if (in_array(config('ipchecker.api_middleware'),$request->route()->gatherMiddleware())){ 39 | $return_array = [ 40 | 'success'=>false, 41 | 'code'=>$code, 42 | 'message'=>$message, 43 | ]; 44 | 45 | return response()->json($return_array,200,[],JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES); 46 | } 47 | 48 | return response()->view('ipchecker::error',compact('message','code')); 49 | } 50 | 51 | return $next($request); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /src/IpCheckerServiceProvider.php: -------------------------------------------------------------------------------- 1 | 9 | * @license http://www.opensource.org/licenses/mit-license.php MIT 10 | * @link https://github.com/HayriCan/laravel-ip-checker 11 | */ 12 | 13 | use Exception; 14 | use HayriCan\IpChecker\Contracts\IpCheckerInterface; 15 | use HayriCan\IpChecker\Http\Middleware\IpChecker; 16 | use Illuminate\Support\ServiceProvider; 17 | use HayriCan\IpChecker\DBDriver; 18 | use HayriCan\IpChecker\FileDriver; 19 | 20 | class IpCheckerServiceProvider extends ServiceProvider 21 | { 22 | /** 23 | * Register services. 24 | * 25 | * @return void 26 | * @throws Exception 27 | */ 28 | public function register() 29 | { 30 | $this->mergeConfigFrom( 31 | __DIR__ . '/../config/ipchecker.php', 'ipchecker' 32 | ); 33 | $this->bindServices(); 34 | } 35 | public function boot() 36 | { 37 | $this->loadConfig(); 38 | $this->loadRoutes(); 39 | $this->loadViews(); 40 | if (config('ipchecker.driver') === 'db'){ 41 | $this->loadMigrations(); 42 | } 43 | $this->loadTranslation(); 44 | } 45 | 46 | public function bindServices(){ 47 | $driver = config('ipchecker.driver'); 48 | switch ($driver) { 49 | case 'file': 50 | $instance = FileDriver::class; 51 | break; 52 | case 'db': 53 | $instance = DBDriver::class; 54 | break; 55 | default: 56 | throw new Exception("Unsupported Driver"); 57 | break; 58 | } 59 | $this->app->singleton(IpCheckerInterface::class,$instance); 60 | 61 | $this->app->singleton('ipchecker', function ($app) use ($instance){ 62 | return new IpChecker($app->make($instance)); 63 | }); 64 | } 65 | 66 | public function loadConfig(){ 67 | $this->publishes([ 68 | __DIR__ . '/../config/ipchecker.php' => config_path('ipchecker.php') 69 | ], 'ipchecker'); 70 | } 71 | 72 | public function loadRoutes(){ 73 | $this->loadRoutesFrom(__DIR__ . '/../routes/web.php'); 74 | } 75 | 76 | public function loadViews(){ 77 | $this->loadViewsFrom(__DIR__ . '/../resources/views', 'ipchecker'); 78 | } 79 | 80 | public function loadMigrations(){ 81 | $this->loadMigrationsFrom(__DIR__ . '/../database/migrations'); 82 | } 83 | 84 | public function loadTranslation(){ 85 | $this->loadTranslationsFrom(__DIR__ . '/lang', 'ipchecker'); 86 | 87 | $this->publishes([ 88 | __DIR__ . '/lang' => resource_path('lang/vendor/ipchecker'), 89 | ], 'ipchecker'); 90 | } 91 | 92 | } -------------------------------------------------------------------------------- /src/IpList.php: -------------------------------------------------------------------------------- 1 | 9 | * @license http://www.opensource.org/licenses/mit-license.php MIT 10 | * @link https://github.com/HayriCan/laravel-ip-checker 11 | */ 12 | 13 | use Illuminate\Database\Eloquent\Model; 14 | 15 | class IpList extends Model 16 | { 17 | protected $guarded = []; 18 | } 19 | -------------------------------------------------------------------------------- /src/lang/en/messages.php: -------------------------------------------------------------------------------- 1 | 7 | * @license http://www.opensource.org/licenses/mit-license.php MIT 8 | * @link https://github.com/HayriCan/laravel-ip-checker 9 | */ 10 | 11 | return [ 12 | /* 13 | |-------------------------------------------------------------------------- 14 | | Dashboard Translation 15 | |-------------------------------------------------------------------------- 16 | | 17 | */ 18 | 'ip_list'=>'IP List', 19 | 'add_ip'=>'Add IP Address', 20 | 'group'=>'Group', 21 | 'definition'=>'Definition', 22 | 'ip_address'=>'IP Address', 23 | 'delete_title'=>'Are you sure?', 24 | 'delete_body'=>'Do you really want to delete these ip address? This process cannot be undone!', 25 | 'cancel'=>'Cancel', 26 | 'save'=>'Save', 27 | 'delete'=>'Delete', 28 | 'no_record'=>'No Records', 29 | 30 | 31 | /* 32 | |-------------------------------------------------------------------------- 33 | | Flash Messages 34 | |-------------------------------------------------------------------------- 35 | | 36 | */ 37 | 'ip_success'=>'IP Address added successfully!', 38 | 'ip_error'=>'IP Address is already in the list!', 39 | 'ip_delete'=>'IP Address has been removed!', 40 | 41 | /* 42 | |-------------------------------------------------------------------------- 43 | | Denied Access Response 44 | |-------------------------------------------------------------------------- 45 | | You can change the response code and message from here 46 | | 47 | */ 48 | 'denied_access'=>[ 49 | 'code'=>'250', 50 | 'message'=>'Your IP Address is not defined in the system!', 51 | ], 52 | 53 | 54 | ]; -------------------------------------------------------------------------------- /src/lang/tr/messages.php: -------------------------------------------------------------------------------- 1 | 7 | * @license http://www.opensource.org/licenses/mit-license.php MIT 8 | * @link https://github.com/HayriCan/laravel-ip-checker 9 | */ 10 | 11 | return [ 12 | /* 13 | |-------------------------------------------------------------------------- 14 | | Dashboard Translation 15 | |-------------------------------------------------------------------------- 16 | | 17 | */ 18 | 'ip_list'=>'IP Listesi', 19 | 'add_ip'=>'IP Adresi Ekle', 20 | 'group'=>'Grup', 21 | 'definition'=>'Tanım', 22 | 'ip_address'=>'IP Adresi', 23 | 'delete_title'=>'Emin misiniz?', 24 | 'delete_body'=>'Bu ip adresini silmek istediğinize emin misiniz? Bu işlem geri alınamaz!', 25 | 'cancel'=>'İptal', 26 | 'save'=>'Kaydet', 27 | 'delete'=>'Sil', 28 | 'no_record'=>'Kayıt Bulunamadı!', 29 | 30 | 31 | /* 32 | |-------------------------------------------------------------------------- 33 | | Flash Messages 34 | |-------------------------------------------------------------------------- 35 | | 36 | */ 37 | 'ip_success'=>'IP Adresi başarı ile eklendi!', 38 | 'ip_error'=>'IP Adresi zaten kayıtlı!', 39 | 'ip_delete'=>'IP Adresi kaldırıldı!', 40 | 41 | /* 42 | |-------------------------------------------------------------------------- 43 | | Denied Access Response 44 | |-------------------------------------------------------------------------- 45 | | You can change the response code and message from here 46 | | 47 | */ 48 | 'denied_access'=>[ 49 | 'code'=>'250', 50 | 'message'=>'IP Adresiniz sistemde tanımlı değil!', 51 | ], 52 | 53 | 54 | ]; --------------------------------------------------------------------------------