OpenLoadBalancer/olb

GitHub: OpenLoadBalancer/olb

一款用Go编写的高性能零依赖L4/L7负载均衡器,集成WAF防护和AI驱动的MCP管理能力。

Stars: 16 | Forks: 3

# OpenLoadBalancer

OpenLoadBalancer

Website Go License Release Tests Coverage Zero Deps

## 快速开始 ``` curl -sSL https://openloadbalancer.dev/install.sh | sh ``` 创建 `olb.yaml`: ``` admin: address: "127.0.0.1:8081" listeners: - name: http address: ":80" routes: - path: / pool: web pools: - name: web algorithm: round_robin backends: - address: "10.0.1.10:8080" - address: "10.0.1.11:8080" health_check: type: http path: /health interval: 10s ``` ``` olb start --config olb.yaml ``` 完成。HTTP 代理在 `:80`,admin API 在 `:8081`,每 10 秒进行健康检查,在两个后端之间进行轮询。 ## 安装 ``` # Binary curl -sSL https://openloadbalancer.dev/install.sh | sh # Docker docker pull ghcr.io/openloadbalancer/olb:latest docker run -d -p 80:80 -p 8081:8081 -v ./olb.yaml:/etc/olb/configs/olb.yaml ghcr.io/openloadbalancer/olb:latest # Homebrew brew tap openloadbalancer/olb && brew install olb # 从源码构建 git clone https://github.com/openloadbalancer/olb.git && cd olb && make build ``` 需要 Go 1.25+。无其他依赖。 ## 功能特性 **代理:** HTTP/HTTPS, WebSocket, gRPC, SSE, TCP (L4), UDP (L4), SNI 路由, PROXY protocol v1/v2, 请求影子化/镜像 **负载均衡:** 14 种算法 — Round Robin, Weighted RR, Least Connections, Weighted Least Connections, Least Response Time, Weighted Least Response Time, IP Hash, Consistent Hash (Ketama), Maglev, Ring Hash, Power of Two, Random, Weighted Random, Sticky Sessions **Geo-DNS 路由:** 基于地理位置的流量路由(国家、地区、城市) **安全性:** TLS 终结 + SNI, ACME/Let's Encrypt, mTLS, OCSP stapling, 6 层 WAF (IP ACL, 限速, 请求清洗, 带有 SQLi/XSS/路径遍历/CMDi/XXE/SSRF 的检测引擎, 带有 JA3 指纹识别的机器人检测, 带有安全标头 + 数据脱敏的响应保护), 断路器 **中间件:** 16 个组件 — Recovery, body limit, WAF (6 层流水线), IP filter, real IP, request ID, timeout, rate limit, circuit breaker, CORS, headers, compression (gzip), retry, cache, metrics, access log **可观测性:** Web UI 仪表板(8 个页面), TUI (`olb top`), Prometheus 指标, 结构化 JSON 日志, admin REST API (15+ 端点), Grafana 仪表板 **运维:** 热配置重载 (SIGHUP 或 API), Raft 集群 + SWIM gossip, 服务发现 (Static/DNS/Consul/Docker/File), 用于 AI 集成的 MCP server, 插件系统, 30+ CLI 命令, 分布式限速, 请求影子化/镜像 ## MCP 集成(AI 驱动管理) OpenLoadBalancer 包含一个内置的 [Model Context Protocol](https://modelcontextprotocol.io/) (MCP) server,使 AI 代理(Claude, GPT, Copilot)能够监控、诊断和管理负载均衡器。 ### 传输 - **SSE** (Server-Sent Events): `GET /sse` 用于流式传输 + `POST /message` 用于命令 — 符合 MCP 规范 - **HTTP POST**: `POST /mcp` 用于简单的请求/响应(向后兼容) - **Stdio**: 通过 stdin/stdout 的行分隔 JSON-RPC,用于本地 CLI 工具 ### 认证 ``` admin: mcp_address: ":8082" mcp_token: "your-secret-token" # Bearer token auth mcp_audit: true # Log all tool calls ``` ### 17 个 MCP 工具 | 类别 | 工具 | |----------|-------| | **指标** | `olb_query_metrics` — RPS, 延迟, 错误率, 连接数 | | **后端** | `olb_list_backends`, `olb_modify_backend` — 添加, 移除, 排空, 启用/禁用 | | **路由** | `olb_modify_route` — 添加, 更新, 移除路由及流量分配 | | **诊断** | `olb_diagnose` — 自动化错误/延迟/容量/健康分析 | | **配置** | `olb_get_config`, `olb_get_logs`, `olb_cluster_status` | | **WAF** | `waf_status`, `waf_add_whitelist`, `waf_add_blacklist`, `waf_remove_whitelist`, `waf_remove_blacklist`, `waf_list_rules`, `waf_get_stats`, `waf_get_top_blocked_ips`, `waf_get_attack_timeline` | ### 从 Claude Desktop 连接 ``` { "mcpServers": { "olb": { "url": "http://localhost:8082/sse", "headers": { "Authorization": "Bearer your-secret-token" } } } } ``` ## 性能 在 AMD Ryzen 9 9950X3D 上基准测试: | 指标 | 结果 | |--------|--------| | 峰值 RPS | **15,480** (10 并发, round_robin) | | 代理开销 | **137µs** (直连: 87µs → 代理: 223µs) | | RoundRobin.Next | **3.5 ns/op**, 0 次分配 | | 中间件开销 | **< 3%** (完整栈 vs 无) | | WAF 开销 (6 层) | **~35μs** 每请求, 在代理规模下 **< 3%** | | 二进制大小 | **9 MB** | | P99 延迟 (50 并发) | **22ms** | | 成功率 | **100%** 跨所有测试 |
算法比较(1000 请求, 50 并发) | 算法 | RPS | 平均延迟 | 分布 | |-----------|-----|-------------|-------------| | random | 12,913 | 3.5ms | 32/34/34% | | maglev | 11,597 | 3.8ms | 68/2/30% | | ip_hash | 11,062 | 4.0ms | 75/12/13% | | power_of_two | 10,708 | 4.0ms | 34/33/33% | | least_connections | 10,119 | 4.4ms | 33/33/34% | | consistent_hash | 8,897 | 4.6ms | 0/0/100% | | weighted_rr | 8,042 | 5.6ms | 33/33/34% | | round_robin | 7,320 | 6.3ms | 35/33/32% |
完整基准测试报告 参见 [docs/benchmark-report.md](docs/benchmark-report.md) 获取完整报告,包括并发扩展、后端延迟影响和中间件开销测量。
## E2E 验证 56 个端到端测试证明每个功能在真实代理场景中均有效: | 类别 | 已验证 | |----------|----------| | **代理** | HTTP, HTTPS/TLS, WebSocket, SSE, TCP, UDP | | **算法** | RR, WRR, LC, IPHash, CH, Maglev, P2C, Random, RingHash | | **中间件** | 限速 (429), CORS, gzip (98% 压缩率), WAF 6 层 (SQLi/XSS/CMDi/路径遍历 → 403, 限速 → 429, 监控模式, 安全标头, 机器人检测, IP ACL, 数据脱敏), IP 过滤, 断路器, 缓存 (HIT/MISS), 标头, 重试 | | **运维** | 健康检查 (宕机/恢复), 配置重载, 加权分配, 会话亲和, 优雅故障转移 (0 宕机时间) | | **基础设施** | Admin API, Web UI, Prometheus, MCP server, 多监听器 | | **性能** | 15K RPS, 137µs 代理开销, 100% 成功率 | ## 算法 | 算法 | 配置名称 | 用例 | |-----------|------------|----------| | Round Robin | `round_robin` | 默认,后端同等 | | Weighted Round Robin | `weighted_round_robin` | 后端容量不均 | | Least Connections | `least_connections` | 长连接 | | Least Response Time | `least_response_time` | 延迟敏感 | | IP Hash | `ip_hash` | 按 IP 的会话亲和 | | Consistent Hash | `consistent_hash` | 缓存局部性 | | Maglev | `maglev` | Google 风格哈希 | | Ring Hash | `ring_hash` | 带虚拟节点的一致性哈希 | | Power of Two | `power_of_two` | 平衡随机 | | Random | `random` | 简单,无状态 | ## 配置 支持 **YAML**, **JSON**, **TOML**, 和 **HCL**,并支持 `${ENV_VAR}` 替换。 ``` admin: address: "127.0.0.1:8081" middleware: rate_limit: enabled: true requests_per_second: 1000 cors: enabled: true allowed_origins: ["*"] compression: enabled: true waf: enabled: true mode: enforce detection: enabled: true threshold: {block: 50, log: 25} bot_detection: {enabled: true, mode: monitor} response: security_headers: {enabled: true} listeners: - name: http address: ":8080" routes: - path: /api pool: api-pool - path: / pool: web-pool pools: - name: web-pool algorithm: round_robin backends: - address: "10.0.1.10:8080" - address: "10.0.1.11:8080" health_check: type: http path: /health interval: 5s - name: api-pool algorithm: least_connections backends: - address: "10.0.2.10:8080" weight: 3 - address: "10.0.2.11:8080" weight: 2 ``` 参见 [docs/configuration.md](docs/configuration.md) 获取所有选项。 ### Geo-DNS 路由示例 ``` geodns: enabled: true default_pool: default-pool rules: - id: us-traffic country: US pool: us-pool fallback: default-pool - id: eu-traffic country: EU pool: eu-pool - id: asia-traffic country: JP region: Tokyo pool: asia-pool ``` ### 请求影子化示例 ``` shadow: enabled: true percentage: 10.0 # Mirror 10% of traffic copy_headers: true copy_body: false timeout: 30s targets: - pool: staging-pool percentage: 100.0 ``` ### 分布式限速示例 ``` waf: enabled: true rate_limit: enabled: true store: type: redis address: "localhost:6379" database: 0 rules: - id: per-ip scope: ip limit: 1000 window: 1m ``` ## CLI ``` olb start --config olb.yaml # Start proxy olb stop # Graceful shutdown olb reload # Hot-reload config olb status # Server status olb top # Live TUI dashboard olb backend list # List backends olb backend drain web-pool 10.0.1.10:8080 olb health show # Health check status olb config validate olb.yaml # Validate config olb cluster status # Cluster info ``` ## 架构 ``` ┌─────────────────────────────────────────────────┐ │ OpenLoadBalancer │ Clients ─────────┤ │ HTTP/S, WS, │ Listeners → Middleware → Router → Balancer → Backends gRPC, TCP, UDP │ (L4/L7) (16 types) (trie) (14 algos) │ │ │ │ WAF (6 layers) │ TLS │ Cluster │ MCP │ Web UI │ │ GeoDNS │ Shadow │ Discovery │ Prometheus │ └─────────────────────────────────────────────────┘ ``` ## 文档 | 指南 | 描述 | |-------|-------------| | [Getting Started](docs/getting-started.md) | 5 分钟快速开始 | | [Configuration](docs/configuration.md) | 所有配置选项 | | [Production Deployment](docs/production-deployment.md) | 生产部署指南 | | [Troubleshooting](docs/troubleshooting.md) | 故障排除手册 | | [Algorithms](docs/algorithms.md) | 算法详情 | | [API Reference](docs/api.md) | Admin REST API | | [Clustering](docs/clustering.md) | 多节点设置 | | [WAF](docs/waf.md) | Web Application Firewall (6 层防御) | | [MCP / AI](docs/mcp.md) | AI 集成 | | [Benchmarks](docs/benchmark-report.md) | 性能数据 | | [Specification](docs/SPECIFICATION.md) | 技术规范 | ## 贡献 参见 [CONTRIBUTING.md](CONTRIBUTING.md)。关键规则: 1. **零外部依赖** — 仅限 stdlib 2. **必须包含测试** — 90% 覆盖率,不要降低它 3. **所有功能已连接** — engine.go 中无死代码 4. **gofmt + go vet** — CI 强制执行 ## 许可证 Apache 2.0 — [LICENSE](LICENSE)
标签:AI集成, Docker, EVTX分析, Go语言, HTTP代理, L4负载均衡, L7负载均衡, MCP, Python工具, Web管理界面, 健康检查, 单二进制, 反向代理, 安全防御评估, 密码管理, 开源, 日志审计, 流量管理, 程序破解, 系统工具, 网络基础设施, 自定义请求头, 请求拦截, 负载均衡, 轮询算法, 配置错误, 集群, 零依赖