
天罡是一款自研可扩展的边缘 WAF 和反向代理,基于 Workerman + pfinal-asyncio 的高并发混合架构。 目标:提供企业级的自托管防护能力(规则管理、动态挑战、误报回放与审计),便于灰度启用与规则热更新。
边缘(TLS 终结)→ 天罡(数据平面)→ 后端(Webman/应用)
composer install
cp env.example .env
# 编辑 .env 文件配置数据库和 Redis(可选,支持离线模式)
php start.php start
# 访问登录页面
curl http://localhost:8787/admin/login
# 登录管理界面(需要认证)
curl http://localhost:8787/admin
# 访问API接口
curl http://localhost:8787/api/dashboard
# 健康检查
curl http://localhost:8787/health
访问 http://localhost:8787/admin/login 登录管理界面:
| 用户名 | 密码 | 说明 | 
|---|---|---|
| admin | admin123 | 管理员账户 | 
| waf | waf2024 | WAF管理员 | 
| tiangang | tiangang2024 | 天罡管理员 | 
# 运行单元测试
php tests/run_unit_tests.php
# 运行集成测试
php tests/run_all_tests.php
# 性能基准测试
php tests/performance/benchmark.php
# 生成测试报告
php tests/coverage_report.php
# 测试WAF规则
curl "http://localhost:8787/?test=<script>alert('xss')</script>"
# 测试SQL注入检测
curl "http://localhost:8787/?id=1' OR '1'='1"
# 测试频率限制
for i in {1..10}; do curl http://localhost:8787/; done
| 特性 | 同步核心 | 异步后台 | 混合架构 | 
|---|---|---|---|
| 稳定性 | ⭐⭐⭐⭐⭐ | ⭐⭐⭐ | ⭐⭐⭐⭐⭐ | 
| 性能 | ⭐⭐⭐ | ⭐⭐⭐⭐⭐ | ⭐⭐⭐⭐ | 
| 可维护性 | ⭐⭐⭐⭐⭐ | ⭐⭐ | ⭐⭐⭐⭐ | 
| 扩展性 | ⭐⭐ | ⭐⭐⭐⭐⭐ | ⭐⭐⭐⭐ | 
天罡WAF/
├──🚪入口层
│   ├──start.php              # 服务器启动入口
│   └──app/waf/               # WAF核心模块
│       └──TiangangGateway.php# 核心网关
│
├──🛡️WAF核心层
│   ├──app/waf/middleware/    # WAF中间件
│   │   └──WafMiddleware.php
│   ├──app/waf/detectors/     # 检测器
│   │   ├──QuickDetector.php# 快速检测
│   │   └──AsyncDetector.php# 异步检测
│   ├──app/waf/core/          # 核心组件
│   │   ├──WafResult.php      # 检测结果
│   │   ├──DecisionEngine.php# 决策引擎
│   │   └──CoroutinePool.php# 协程池
│   ├──app/waf/logging/       # 日志系统
│   │   ├──AsyncLogger.php    # 异步日志
│   │   └──LogCollector.php   # 日志收集
│   ├──app/waf/monitoring/    # 监控系统
│   │   └──MetricsCollector.php
│   ├──app/waf/performance/   # 性能分析
│   │   └──PerformanceDashboard.php
│   ├──app/waf/plugins/       # 插件管理
│   │   ├──PluginManager.php
│   │   └──WafPluginInterface.php
│   ├──app/waf/proxy/         # 代理转发
│   │   ├──ProxyHandler.php
│   │   └──BackendManager.php
│   ├──app/waf/config/        # 配置管理
│   │   └──ConfigManager.php
│   ├──app/waf/cache/         # 缓存管理
│   │   └──AsyncCacheManager.php
│   ├──app/waf/database/      # 数据库管理
│   │   └──AsyncDatabaseManager.php
│   └──app/waf/optimization/# 性能优化
│       └──PerformanceOptimizer.php
│
├──🌐管理界面层
│   ├──app/admin/controller/# 管理控制器
│   │   ├──AuthController.php     # 认证控制器
│   │   ├──DashboardController.php# 仪表板控制器
│   │   ├──WafController.php      # WAF管理控制器
│   │   ├──RuleController.php     # 规则管理控制器
│   │   └──LogController.php      # 日志管理控制器
│   ├──app/admin/routes/      # 管理路由
│   │   └──AdminRoutes.php
│   ├──app/admin/middleware/# 认证中间件
│   │   └──AuthMiddleware.php
│   └──app/admin/helpers/     # 辅助函数
│       └──helpers.php
│
├──🔌插件系统
│   └──plugins/waf/           # WAF规则插件
│       ├──SqlInjectionRule.php
│       ├──XssRule.php
│       ├──RateLimitRule.php
│       └──IpBlacklistRule.php
│
└──⚙️配置与测试
    ├──config/                # 配置文件
    ├──tests/                 # 测试代码
    └──docs/                  # 文档
GET /admin/login - 用户登录界面GET /admin - 管理控制台界面GET /dashboard - 仪表板页面GET /health - 系统健康状态POST /admin/auth/login - 用户登录GET /admin/auth/logout - 用户登出GET /api/dashboard - 获取仪表板数据GET /api/performance?period=1h - 获取性能报告GET /api/security?period=1d - 获取安全报告GET /api/export?type=dashboard&format=json - 导出数据{
  "success": true,
"data": {
    "overview": {
      "total_requests": 1500,
      "blocked_requests": 50,
      "block_rate": 3.3
    },
    "performance": {
      "avg_response_time": 120,
      "throughput": 250
    }
  },
"timestamp": 1640995200
}
# 启动所有服务
docker-compose up -d
# 查看服务状态
docker-compose ps
# 查看日志
docker-compose logs -f tiangang
# .env 文件配置
WAF_ENABLED=true
SERVER_HOST=0.0.0.0
SERVER_PORT=8787
BACKEND_URL=http://backend:8080
REDIS_HOST=redis
REDIS_PORT=6379
// 配置告警阈值
'monitoring' => [
    'alerts' => [
        'block_rate' => 0.1,      // 10% 拦截率告警
        'response_time' => 1000,  // 1秒响应时间告警
        'error_rate' => 0.05,     // 5% 错误率告警
    ]
]
# 克隆项目
git clone https://github.com/your-org/tiangang-waf.git
cd tiangang-waf
# 安装依赖
composer install
# 运行测试
php tests/run_all_tests.php
# 启动开发服务器
php start.php start