oravil/laravel-guard

laravel 地理位置服务,IP 服务,代理和 VPN 检测

1.2.1 2021-11-06 09:05 UTC

This package is auto-updated.

Last update: 2024-09-06 14:59:28 UTC


README

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

包支持

提供商功能

此包可以作为 Laravel 包处理所有 IP 服务。它可以帮助:

  1. 根据访客的 IP 地址确定网站访客的地理位置
  2. 检测 VPN、代理、Tor 和托管 IP。
  3. 检索语言、货币和位置数据。
  4. 通过连接类型过滤器阻止连接。
  5. 在缓存驱动器中缓存 IP 数据
  6. 按国家(开发)阻止连接

支持我们

买我一杯咖啡

安装

您可以通过 composer 安装此包

composer require oravil/laravel-guard

您可以使用以下命令发布配置文件

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

这是已发布的配置文件的内容

// 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
        ],
    ],
];

用法

#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

安全过滤器

// 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,
        ]
    ],

从中间件中检索 IP 数据而不阻止连接

// 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 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}

刷新位置缓存

// 控制台

php artisan guard:flush

// PHP

return laravelGuard()->flushCache();

待办事项

  • 添加 API 服务提供商。
  • 推送到 Github
  • 推送预发布版本
  • 添加到 Packagist
  • 添加缓存驱动器
  • 添加缓存刷新控制台命令
  • 添加全局函数
  • 推送版本 1.1
  • 通过过滤器阻止连接
  • 通过中间件阻止连接
  • 推送版本 1.2
  • 添加创建自定义提供者的控制台命令
  • 推送版本 1.3
  • 添加对限制请求的回退
  • 添加 GitHub 文档
  • 推送版本 2.0

贡献

请参阅 CONTRIBUTING 以获取详细信息。

安全漏洞

请查看 我们的安全策略 了解如何报告安全漏洞。

感谢

致谢

许可证

MIT 许可证 (MIT)。请参阅 许可证文件 了解更多信息。