oravil/laravel-guard 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

oravil/laravel-guard

Composer 安装命令:

composer require oravil/laravel-guard

包简介

laravel geo location, ip services, proxy and vpn detected

README 文档

README

Latest Version on Packagist GitHub Code Style Action Status Total Downloads Donate

Package Support

Providers Features

Provider Require Api Free Limit Paid Limit Support Languages Support Security Support Currenices Support Location
IpHub 1k/day up to 200k/day
IpRegistry 100k/once Per Paid Package
Ip-Api 45/minute unlimited
IpInfo 50k/month up to 2.5m/month
IpData 1.5k/day up to 100k/day
IpApi.com 1k/month up to 2m/month
ProxyCheck 1k/day up to 10m/day
GeoPlugin Unknown
IpGeoLocation 1k/day up to 20m/month

This package can be used to handle all ip services as a Laravel package. It Can Help With:

  1. Determine the geographical location of website visitors based on their IP addresses
  2. Detected Vpn, Proxy, Tor and Hosting Ip's.
  3. retrieve Language, Currencies and Location Data.
  4. Block connections via Connections Type Filters.
  5. Cache ip data in cache drivers
  6. Block Connections by country(dev).

Support us

buy me a coffee

Installation

You can install the package via composer:

composer require oravil/laravel-guard

You can publish the config file with:

php artisan vendor:publish --provider="Oravil\LaravelGuard\LaravelGuardServiceProvider"

This is the contents of the published config file:

// config for Oravil/LaravelGuard config/guard.php
return [
    // laravel guard version
    'version' => '1.2',

    /*
    |-------------------------------------------------------------------------
    | Cache Driver
    | To use cache tags you should support one of cache drivers Redis / Memcached / Array
    |-------------------------------------------------------------------------
     *
     */

    'cache_enable' => false, // set true to enable cache

    'cache_tag_name' => 'lg-location', // cache tag name

    'cache_expires' => 30, // seconds

    /*
    |--------------------------------------------------------------------------
    | Provider
    |--------------------------------------------------------------------------
    |
    | The default provider you would like to use for geo ip retrieval.
    |
    */

    'provider' => 'ip-api',

    /*
    |--------------------------------------------------------------------------
    | Driver Fallbacks
    |--------------------------------------------------------------------------
    |
    | The providers you want to use to retrieve the users geo ip
    | if the above selected driver is unavailable.
    |
    | These will be called upon in order (first to last).
    |
    */

    'fallbacks' => [
        Oravil\LaravelGuard\Providers\IpRegistry::class,
        Oravil\LaravelGuard\Providers\IpApi::class,
    ],

    /*
    |--------------------------------------------------------------------------
    | Location
    |--------------------------------------------------------------------------
    |
    | Here you may configure the position instance that is created
    | and returned from the above drivers. The instance you
    | create must extend the built-in Position class.
    |
    */

    'location' => Oravil\LaravelGuard\Support\Location::class,

    /*
    |--------------------------------------------------------------------------
    | Localhost Testing
    |--------------------------------------------------------------------------
    |
    | If your running your website locally and want to test different
    | IP addresses to see location detection, set 'enabled' to true.
    |
    | The testing IP address is a Google host in the United-States.
    |
    */

    'testing' => [
        'enabled'  => env('GUARD_TESTING', false),
        'valid_ip' => '102.189.209.97',
        'cloud_ip' => '108.162.193.194',
        'proxy_ip' => '193.176.86.46',
        'tor_ip'   => '103.236.201.88',
        'bogon_ip' => '203.0.113.24',
    ],


    /*
    |--------------------------------------------------------------------------
    | Security Filters
    |--------------------------------------------------------------------------
    |
    |
    |
    */

    'security' => [
        'enabled' => env('GUARD_SECURITY', false),
        'middleware' => [
            //\Oravil\LaravelGuard\ShouldBlockMiddleware::class, copy to Http/kernel.php
            'enabled' => false,
            'block_message' => 'Your connection has been blocked, Our system has identified you as a threa',
            'abort_code' => 403
        ],
        'filters' => [
            'is_cloud' => true,
            'is_anonymous' => true,
            'is_threat' => true,
            'is_bogon' => true,
        ]
    ],

    /*
    |--------------------------------------------------------------------------
    | Providers List Configure
    |--------------------------------------------------------------------------
    |
    |
    |
    */

    'providers' => [
        'ipregistry' => [  // ip registry https://ipregistry.co/docs/
            'class' => \Oravil\LaravelGuard\Providers\IpRegistry::class, //provider class path
            'api_key' => env('IPREGISTRY_API_KEY', null), // api key
            'api_url' => 'https://api.ipregistry.co/', // api base url
            'currencies_enabled' => env('GUARD_CURRENCIES', true), // if you need currencies data
            'language_enabled' => env('GUARD_LANGUAGE', true), // if you need langauge data
            'security_enabled' => env('GUARD_SECURITY', true), //security status
        ],

        'iphub' => [ // ip hub https://iphub.info/
            'class' => \Oravil\LaravelGuard\Providers\IpHub::class, //provider class path
            'api_key' => env('IPHUB_API_KEY', null), // api key
            'api_url' => 'http://v2.api.iphub.info/ip/', // api base url
            'security_enabled' => env('GUARD_SECURITY', true), //security status
        ],

        'ip-api' => [ // ip api https://ip-api.com/
            'class' => \Oravil\LaravelGuard\Providers\IpApi::class, //provider class path
            'pro_api_url' => 'https://pro.ip-api.com/json/', // pro services
            'api_key' => env('IPAPI_API_KEY', null), // api key
            'api_url' => 'http://ip-api.com/json/', // api base url
            'currencies_enabled' => true, //  support currenices code only
            'security_enabled' => env('GUARD_SECURITY', true), //security status
        ],

        'proxycheck' => [ // ip api https://proxycheck.io/
            'class' => \Oravil\LaravelGuard\Providers\ProxyCheck::class, //provider class path
            'api_key' => env('PROXYCHECK_API_KEY', null), // api key
            'api_url' => 'http://proxycheck.io/v2/', // api base url
            'security_enabled' => env('GUARD_SECURITY', true), //security status
            'block_score' => 33
        ],

        'ipapicom' => [ // ip api https://ipapi.com/
            'class' => \Oravil\LaravelGuard\Providers\IpApiCom::class, //provider class path
            'api_key' => env('IPAPICOM_API_KEY', null), // api key
            'api_url' => 'http://api.ipapi.com/api/', // api base url
            'security_enabled' => env('GUARD_SECURITY', false), //security status
            'security_plan_enable' => false, // if you plan is BUSINESS PRO
            'currency_plan_enable' => false, // if you plan is STANDARD or above
        ],

        'ipdata' => [ // ip data https://ipdata.co/
            'class' => \Oravil\LaravelGuard\Providers\IpData::class, //provider class path
            'api_key' => env('IPDATA_API_KEY', null), // api key
            'api_url' => 'https://api.ipdata.co/', // api base url
            'currencies_enabled' => env('GUARD_CURRENCIES', true), // if you need currencies data
            'language_enabled' => env('GUARD_LANGUAGE', true), // if you need langauge data
            'security_enabled' => env('GUARD_SECURITY', true), //security status
        ],

        'ipinfo' => [ // ip data https://ipdata.co/
            'class' => \Oravil\LaravelGuard\Providers\IpInfo::class, //provider class path
            'api_key' => env('IPINFO_API_KEY', null), // api key
            'api_url' => '//ipinfo.io/', // api base url
            'security_enabled' => env('GUARD_SECURITY', true), //security status
        ],

        'geoplugin' => [ // ip data http://www.geoplugin.net
            'class' => \Oravil\LaravelGuard\Providers\GeoPlugin::class, //provider class path
            'api_url' => 'http://www.geoplugin.net/json.gp?ip', // api base url
            'currencies_enabled' => env('GUARD_CURRENCIES', true), // if you need currencies data
        ],

        'ipgeolocation' => [ // ip data https://ipgeolocation.io
            'class' => \Oravil\LaravelGuard\Providers\IpGeoLocation::class, //provider class path
            'api_key' => env('IP_GEO_LOCATION_API_KEY', null), // api key
            'api_url' => 'https://api.ipgeolocation.io/ipgeo', // api base url
            'currencies_enabled' => env('GUARD_CURRENCIES', true), // if you need currencies data
            'language_enabled' => env('GUARD_LANGUAGE', true), // if you need langauge data
        ],
    ],
];

Usage

#global functions
echo getIp();                                    // get location instance for client ip | testing ip if testing_enable => true
echo getIp('8.8.8.8');                           // LaravelGuard::get('8.8.8.8');
echo laravelGuard('8.8.8.8');                    // LaravelGuard::get('8.8.8.8');
echo laravelGuard()->echoApiResponse('8.8.8.8'); // LaravelGuard::echoApiResponse('8.8.8.8');
echo laravelGuard()->echoApiResponse();          // LaravelGuard::echoApiResponse('8.8.8.8');
echo laravelGuard()->testing('type');            // testing connection type(valid, proxy, vpn, tor, cloud, bogon), default: valid
laravelGuard()->flushCache();                    // flushed locations cache
//laravel facade
use Oravi/LaravelGuard/Facades/LaravelGuard;
echo LaravelGuard::get();                        // get location instance for client ip | testing ip if testing_enable => true
echo LaravelGuard::get('8.8.8.8');               // get location instance
echo LaravelGuard::echoApiResponse('8.8.8.8');   // get request from api for client ip
echo LaravelGuard::echoApiResponse();   // get request from api for client ip | testing ip if testing_enable => true
echo LaravelGuard::testing('type');              // testing connection type(valid, proxy, vpn, tor, cloud, bogon), default: valid
LaravelGuard::flushCache();                      // flushed locations cache

Security Filters

// app/http/kernel.php
protected $middleware = [
    ...
    \Oravil\LaravelGuard\ShouldBlockMiddleware::class
];

//config/guard.php
'security' => [
        'enabled' => env('GUARD_SECURITY', true), //set true to enable security filters
        'middleware' => [
            //\Oravil\LaravelGuard\ShouldBlockMiddleware::class, copy to Http/kernel.php
            'enabled' => true, // set true to enable middleware
            'block_message' => 'Your connection has been blocked, Our system has identified you as a threa', //edit block message
            'abort_code' => 403 //edit abort code
        ],
        'filters' => [
            'is_cloud' => true,
            'is_anonymous' => true,
            'is_threat' => true,
            'is_bogon' => true,
        ]
    ],

to retrive ip data from middleware without block connection

// set security true
'middleware' => [
    //\Oravil\LaravelGuard\ShouldBlockMiddleware::class, copy to Http/kernel.php
    'enabled' => false, // set false to disable block connections
    ....
]
// get ip data
dd(request()->ipGuard);
//check should block connection
dd(request()->ipGuard->shouldBlock);
// check block type
dd(request()->ipGuard->blockType);

Testing

// testing using ipregistry provider
return laravelGuard()->testing(); // or laravelGuard()->testing('valid');
//output: {"ip":"102.189.209.97","countryName":"Egypt","countryCode":"EG","regionCode":"EG-C","regionName":"Al Q\u0101hirah","cityName":"Cairo","zipCode":"09893","latitude":"30.07795","longitude":"31.28525","areaCode":1001450,"isEU":false,"currencyStatus":true,"currencyName":"Egyptian Pound","currencyCode":"EGP","currencySymbol":"EGP","langStatus":true,"langName":"Arabic","langNative":"\u0627\u0644\u0639\u0631\u0628\u064a\u0629","langCode":"ar","timeZone":"Africa\/Cairo","currentTime":"2021-11-05T06:57:33+02:00","securityStatus":true,"isCloudProvider":false,"isThreat":false,"isAnonymous":false,"isBogon":false,"provider":"IpRegistry","provider_class":"Oravil\\LaravelGuard\\Providers\\IpRegistry","isCached":null}

return laravelGuard()->testing('cloud');
// output: {"ip":"108.162.193.194","countryName":"United States","countryCode":"US","regionCode":null,"regionName":null,"cityName":null,"zipCode":null,"latitude":"37.75096","longitude":"-97.822","areaCode":9629091,"isEU":false,"currencyStatus":true,"currencyName":"US Dollar","currencyCode":"USD","currencySymbol":"$","langStatus":true,"langName":"English","langNative":"English","langCode":"en","timeZone":"America\/Chicago","currentTime":"2021-11-04T23:59:59-05:00","securityStatus":true,"isCloudProvider":true,"isThreat":false,"isAnonymous":false,"isBogon":false,"provider":"IpRegistry","provider_class":"Oravil\\LaravelGuard\\Providers\\IpRegistry","isCached":null}

return laravelGuard()->testing('proxy'); // or laravelGuard()->testing('vpn');
//output: {"ip":"193.176.86.46","countryName":"Germany","countryCode":"DE","regionCode":"DE-BE","regionName":"Berlin","cityName":"Berlin","zipCode":"10178","latitude":"52.51965","longitude":"13.40687","areaCode":357021,"isEU":true,"currencyStatus":true,"currencyName":"Euro","currencyCode":"EUR","currencySymbol":"\u20ac","langStatus":true,"langName":"German","langNative":"Deutsch","langCode":"de","timeZone":"Europe\/Berlin","currentTime":"2021-11-05T06:00:43+01:00","securityStatus":true,"isCloudProvider":false,"isThreat":true,"isAnonymous":false,"isBogon":false,"provider":"IpRegistry","provider_class":"Oravil\\LaravelGuard\\Providers\\IpRegistry","isCached":null}

return laravelGuard()->testing('tor')
//output: {"ip":"103.236.201.88","countryName":"Indonesia","countryCode":"ID","regionCode":"ID-BT","regionName":"Banten","cityName":"Tangerang","zipCode":null,"latitude":"-6.17836","longitude":"106.63184","areaCode":1919440,"isEU":false,"currencyStatus":true,"currencyName":"Indonesian Rupiah","currencyCode":"IDR","currencySymbol":"IDR","langStatus":true,"langName":"Indonesian","langNative":"Indonesia","langCode":"id","timeZone":"Asia\/Jakarta","currentTime":"2021-11-05T12:02:37+07:00","securityStatus":true,"isCloudProvider":true,"isThreat":true,"isAnonymous":true,"isBogon":false,"provider":"IpRegistry","provider_class":"Oravil\\LaravelGuard\\Providers\\IpRegistry","isCached":null}

return laravelGuard()->testing('bogon')
//output: {"ip":"203.0.113.24","countryName":null,"countryCode":null,"regionCode":null,"regionName":null,"cityName":null,"zipCode":null,"latitude":"-4.0E-5","longitude":"4.0E-5","areaCode":0,"isEU":false,"currencyStatus":true,"currencyName":null,"currencyCode":null,"currencySymbol":null,"langStatus":true,"langName":null,"langNative":null,"langCode":null,"timeZone":"Africa\/Sao_Tome","currentTime":"2021-11-05T05:03:28Z","securityStatus":true,"isCloudProvider":false,"isThreat":true,"isAnonymous":false,"isBogon":true,"provider":"IpRegistry","provider_class":"Oravil\\LaravelGuard\\Providers\\IpRegistry","isCached":null}

Flushed locations cache

// console

php artisan guard:flush

//php

return laravelGuard()->flushCache();

Todo

  • add api service providers.
  • push to Github
  • push Pre-release
  • add to Packagist
  • add cache driver
  • add cache flush console command
  • add global functions
  • push version 1.1
  • block connections via filters
  • block connections via midlleware
  • push version 1.2
  • add create custom provider console command
  • push version 1.3
  • add fallbacks for limit requests
  • add github documentation
  • push version 2.0

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Thanks To:

Credits

License

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

oravil/laravel-guard 适用场景与选型建议

oravil/laravel-guard 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 934 次下载、GitHub Stars 达 4, 最近一次更新时间为 2021 年 11 月 02 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「geo」 「security」 「maxmind」 「proxy」 「laravel」 「location」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 oravil/laravel-guard 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 oravil/laravel-guard 我们能提供哪些服务?
定制开发 / 二次开发

基于 oravil/laravel-guard 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 934
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 4
  • 点击次数: 10
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 4
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-11-02