定制 olssonm/blockip 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

olssonm/blockip

Composer 安装命令:

composer require olssonm/blockip

包简介

Block requests from specified IPs in Laravel 5.

README 文档

README

Latest Version on Packagist Software License Build Status

Block requests from specified IPs quick and easy in Laravel. Highly customizable.

Version Compatibility

Laravel blockip
5.3.x 1.x.x
5.4.x 2.x.x
5.5/5.6/5.7/5.8 3.x.x
^6.0 4.x.x
^7.0 5.x.x
^8.0 6.x.x

Installation

Via Composer

$ composer require olssonm/blockip

Add the service provider to the providers array in config/app.php (auto-detection also available in newer Laravel-versions).

<?php

    'providers' => [
        Olssonm\Blockip\BlockipServiceProvider::class
    ]

Usage

This backage sets up the blockip-middleware for use in your application. All routes that uses the middlareware is protected from unwanted requests.

Use in a group

<?php

    Route::group(['middleware' => 'blockip'], function() {
        Route::get('/', ['as' => 'start', 'uses' => 'StartController@index']);
        Route::get('/page', ['as' => 'page', 'uses' => 'StartController@page']);
    });

Singe route

<?php

    Route::get('/', [
        'as' => 'start',
        'uses' => 'StartController@index',
        'middleware' => 'blockip'
    ]);

Configuration

Run the command $ php artisan vendor:publish --provider="Olssonm\Blockip\BlockipServiceProvider" to publish the packages configuration. In config/blockip.php you can edit your settings:

<?php

return [

    // IPs to block
    'ips' => [
        '37.123.187.245',   // an example of a single IP
        '23.20.0.0/14'      // an example of an IP-range with CIDR-notation
    ],

    // Message for blocked requests
    'error_message'     => '401 Unauthorized.',

    // Uncomment to use a view instead of plaintext message
    // 'error_view'     => 'blockip::default',

    // Environments where the middleware is active
    'envs'              => [
        'testing',
        'development',
        'production'
    ],

    // Main handler for the getIp(), getIpsToBlock() and getError-methods().
    // Check the documentation on how to customize this to your liking.
    'handler'           => Olssonm\Blockip\Handlers\BlockipHandler::class,

];

Everything here is pretty much self explanatory, but because the blockip-handler is customizable you can pretty much change every aspect of the middleware.

If you want to write your own handler, you should implement the Olssonm\Blockip\Handlers\BaseHandler-interface, like so:

<?php

use Olssonm\Blockip\Handlers\BaseHandler;

class MyHandler implements BaseHandler {

    /**
     * @return string
     */
    public function getIp() {
        // Method to retrieve the request IP
    }

    /**
     * @return array
     */
    public function getIpsToBlock() {
        // Method to set what IPs to be blocked
    }

    /**
     * @return response
     */
    public function getError() {
        // Method to set the response
    }
}

Using the system you have the ability to for example make your getIpsToBlock()-method check IPs from an API, your getError() return a JSON-response etc. etc.

Note: the default handler already checks for the special HTTP_CF_CONNECTING_IP-header when using the Cloudflare CDN.

Testing

$ composer test

or

$ phpunit

Laravel always runs in the "testing" environment while running tests. Make sure that testing is set in the envs-array in blockip.php.

License

The MIT License (MIT). Please see License File for more information.

© 2021 Marcus Olsson.

统计信息

  • 总下载量: 241
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 6
  • 点击次数: 2
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 6
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-11-28

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固