areia-lab/laravel-traffic-control 问题修复 & 功能扩展

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

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

areia-lab/laravel-traffic-control

Composer 安装命令:

composer require areia-lab/laravel-traffic-control

包简介

Traffic control toolkit for Laravel — advanced throttling, blocking, bot detection, analytics, and dashboard.

README 文档

README

Packagist Version Packagist Downloads License

A full-featured traffic control & security toolkit for Laravel.
It provides rate limiting, IP black/whitelisting, bot detection, request quotas, alerts, logging, and a monitoring dashboard.

✨ Features

  • 🔒 Advanced Rate Limiting (per-IP, per-user, per-route, per-role, per-plan)
  • 🌍 IP Control: Whitelist, Blacklist, TOR/VPN blocking
  • 🤖 Bot & Crawler Detection using User-Agent patterns
  • 📊 Traffic Dashboard: Visualize blocked/allowed requests
  • 📑 Logging of suspicious/blocked requests with purge command
  • 🔔 Alerts (Slack, Email) on suspicious spikes
  • 🚦 API Quotas (daily, monthly, per-user, per-plan)
  • 🧩 Custom Rules: Extend with your own blocking logic (GeoIP, maintenance windows, etc.)
  • Request Queueing (optional future feature)

📦 Installation

Install via Composer:

composer require areia-lab/laravel-traffic-control

Publish configuration and migrations:

php artisan vendor:publish --provider="AreiaLab\TrafficControl\TrafficControlServiceProvider" --tag="traffic-config"
php artisan vendor:publish --provider="AreiaLab\TrafficControl\TrafficControlServiceProvider" --tag="traffic-migrations"
php artisan migrate

(Optional) Publish dashboard views:

php artisan vendor:publish --provider="AreiaLab\TrafficControl\TrafficControlServiceProvider" --tag="traffic-views"

📸 Screenshots

Dashboard Manage IP Settings

⚙️ Configuration

Edit config/traffic.php:

return [
    'enabled' => env('TRAFFIC_CONTROL_ENABLED', true), // Enable/disable globally
    'storage' => env('TRAFFIC_CONTROL_STORAGE', 'redis'), // redis | database | file

    'rate_limits' => [
        'default' => ['requests' => 60, 'per' => 60],
        'api' => ['requests' => 120, 'per' => 60],
    ],

    'ip' => [
        'block_tor' => true,
        'blacklist' => [],
        'whitelist' => [],
    ],

    'bot_detection' => [
        'enabled' => true,
        'user_agents' => ['bad-bot'],
    ],

    'alerts' => [
        'slack' => env('TRAFFIC_CONTROL_SLACK_WEBHOOK'),
        'email' => env('TRAFFIC_CONTROL_ALERT_EMAIL', 'admin@example.com'),
        'threshold' => env('TRAFFIC_CONTROL_ALERT_THRESHOLD', 1000),
    ],

    'dashboard' => [
        'enabled' => true,
        'prefix' => 'traffic-control',
        'middleware' => ['web'],
    ],

    'api_quota' => [
        'default' => 10000,
    ],

    'logging' => [
        'log_blocked' => true,
        'log_sample_rate' => 1,
    ],
];

🚀 Usage

Middleware

Apply globally in app/Http/Kernel.php or per route:

Route::middleware(['traffic.control'])->group(function () {
    Route::get('/api/data', [ApiController::class, 'index']);
});

Custom per-route limit:

Route::get('/heavy', [HeavyController::class, 'index'])
    ->middleware('traffic.control:200,60');

Role/Plan-Based Limits

if ($user = $request->user()) {
    $plan = $user->plan ?? 'free';
    $limit = config("traffic-control.rate_limits.$plan", config('traffic-control.rate_limits.default'));
}

API Quotas

use AreiaLab\TrafficControl\Facades\TrafficManager;

if (!TrafficManager::checkQuota($user->id)) {
    return response()->json(['error' => 'API quota exceeded'], 429);
}

Dashboard

Route::get('/admin/traffic', function () {
    $logs = \AreiaLab\TrafficControl\Models\TrafficLog::latest()->limit(50)->get();
    return view('vendor.traffic-control.dashboard', compact('logs'));
})->middleware(['web', 'auth'])->name('traffic-control.dashboard');

Alerts

Set .env variables:

TRAFFIC_CONTROL_SLACK_WEBHOOK=https://hooks.slack.com/services/XXXX
TRAFFIC_CONTROL_ALERT_EMAIL=admin@example.com

Purging Logs

php artisan traffic-control:purge               # Purge logs older than 30 days
php artisan traffic-control:purge --days=90 --force
php artisan traffic-control:purge --all
php artisan traffic-control:purge --all --force

🧩 Extending

Create custom rules under src/Rules/:

namespace AreiaLab\TrafficControl\Rules;

use Illuminate\Http\Request;

class GeoBlockRule
{
    public function handle(Request $request)
    {
        $country = $this->lookupCountry($request->ip());
        if (in_array($country, ['CN', 'RU'])) {
            return response('Not available in your region', 403);
        }
        return true;
    }

    protected function lookupCountry($ip)
    {
        return 'US';
    }
}

🛠 Roadmap

  • Redis sliding window / leaky bucket limiter
  • GeoIP/TOR/VPN detection integration
  • Charts for dashboard
  • Plan-based quota & billing hooks
  • AI anomaly detection

🤝 Contributing

PRs are welcome!

  • Follow PSR-12 coding style
  • Add unit/feature tests
  • Update README.md when adding new features

📜 License

MIT © AreiaLab

areia-lab/laravel-traffic-control 适用场景与选型建议

areia-lab/laravel-traffic-control 是一款 基于 Blade 开发的 Composer 扩展包,目前已累计 6 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 09 月 11 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 areia-lab/laravel-traffic-control 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: Blade

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-09-11