soapbucket/sbproxy
GitHub: soapbucket/sbproxy
一个高性能单二进制反向代理与 AI 网关,解决多 LLM 提供商统一接入与流量管控问题。
Stars: 16 | Forks: 0
# SBproxy
[](https://go.dev)
[](https://opensource.org/licenses/Apache-2.0)
[](https://github.com/soapbucket/sbproxy/releases)
[](https://github.com/soapbucket/sbproxy/actions/workflows/ci.yml)
一个高性能反向代理与 AI 网关。单二进制文件,单配置文件,低请求开销。
[网站](https://sbproxy.dev) | [文档](https://sbproxy.dev/docs) | [快速开始](https://sbproxy.dev/docs/quick-start) | [示例](examples/) | [端到端测试](e2e/) | [文档](docs/)
## 为什么选择 SBproxy
大多数团队为 HTTP 代理和 AI 流量分别运行独立的系统。SBproxy 在单个二进制文件中同时处理两者。一个 YAML 配置文件即可覆盖整个流量层:基于路径的路由、认证、速率限制、响应缓存、带有回退链的 AI 模型路由、内容安全护栏等。
- **单二进制文件**,零外部依赖。无需 Redis、数据库或 Sidecar 即可运行。
- **亚毫秒级开销**,通过编译后的处理链(无需每次请求查找配置)。
- **200+ 原生 LLM 提供商**,提供 OpenAI 兼容 API。详见 [docs/providers.md](docs/providers.md)。
- **热重载**,无需重启即可更新配置。
## 安装
```
# Homebrew
brew tap soapbucket/sbproxy && brew install sbproxy
# Go
go install github.com/soapbucket/sbproxy/cmd/sbproxy@latest
# Docker
docker pull ghcr.io/soapbucket/sbproxy:latest
# Script
curl -fsSL https://download.sbproxy.dev | sh
```
## 快速开始
### 反向代理
```
# sb.yml
proxy:
http_bind_port: 8080
origins:
"api.example.com":
action:
type: proxy
url: https://test.sbproxy.dev
```
```
sbproxy serve -f sb.yml
curl -H "Host: api.example.com" http://localhost:8080/echo
```
### AI 网关
```
# sb.yml
proxy:
http_bind_port: 8080
origins:
"ai.example.com":
action:
type: ai_proxy
providers:
- name: openai
api_key: ${OPENAI_API_KEY}
models: [gpt-4o, gpt-4o-mini]
default_model: gpt-4o-mini
routing:
strategy: fallback_chain
authentication:
type: api_key
api_keys: [my-key]
policies:
- type: rate_limiting
requests_per_minute: 60
algorithm: sliding_window
```
```
sbproxy serve -f sb.yml
curl -H "Host: ai.example.com" \
-H "X-API-Key: my-key" \
-H "Content-Type: application/json" \
http://localhost:8080/v1/chat/completions \
-d '{"model":"gpt-4o-mini","messages":[{"role":"user","content":"Hello"}]}'
```
无论哪个提供程序处理请求,都会返回 OpenAI 兼容的响应。
### 生产配置
请参考 [examples/16-full-production.yml](examples/16-full-production.yml) 获取结合 API 密钥认证、速率限制、WAF、响应缓存、压缩、CORS、安全头、基于路径的路由和自定义错误页面的生产级配置。
[examples/](examples/) 中的全部 16 个示例均使用 [test.sbproxy.dev](https://test.sbproxy.dev) 作为后端,开箱即用。
## 功能特性
### 路由
| 功能 | 描述 | 文档 |
|---|---|---|
| 反向代理 | 使用可配置的超时时间将请求转发到上游服务器 | [配置](https://sbproxy.dev/docs/config#proxy) |
| 基于路径的路由 | 通过转发规则将不同路径路由到不同的后端 | [配置](https://sbproxy.dev/docs/config#forward-rules) |
| 负载均衡 | 10 种算法(轮询、加权、最少连接、IP 哈希等)并支持健康检查 | [配置](https://sbproxy.dev/docs/config#loadbalancer) |
| AI 网关 | 支持 200+ 原生提供商的 OpenAI 兼容 API,支持模型路由与回退链 | [AI 文档](https://sbproxy.dev/docs/ai) |
| WebSocket | 全双工 WebSocket 代理 | [配置](https://sbproxy.dev/docs/config#websocket) |
| gRPC | gRPC 与 gRPC-Web 代理 | [配置](https://sbproxy.dev/docs/config#grpc) |
| GraphQL | 支持 GraphQL 的代理 | [配置](https://sbproxy.dev/docs/config#graphql) |
| MCP 服务器 | 模型上下文协议,用于 AI 工具调用 | [配置](https://sbproxy.dev/docs/config#mcp) |
| A2A 协议 | Google Agent-to-Agent 协议 | [配置](https://sbproxy.dev/docs/config#a2a) |
### 安全
| 功能 | 描述 | 文档 |
|---|---|---|
| 认证 | API 密钥、基本认证、Bearer 令牌、JWT(HS256/RS256/ES256)、转发认证、摘要认证 | [认证文档](https://sbproxy.dev/docs/auth) |
| WAF | 支持 OWASP 核心规则集(1-4 级),可自定义规则 | [安全](https://sbproxy.dev/docs/security#waf) |
| 速率限制 | 滑动窗口、固定窗口、令牌桶、漏桶,支持每个端点覆盖配置 | [配置](https://sbproxy.dev/docs/config#rate-limiting) |
| DDoS 防护 | 基于速率的自适应检测,支持阻断、限流或挑战响应 | [安全](https://sbproxy.dev/docs/security#ddos) |
| IP 过滤 | 支持 CIDR 和可信代理配置的允许/阻止列表 | [安全](https://sbproxy.dev/docs/security#ip-filtering) |
| CORS | 允许来源白名单、方法/头部控制、凭据支持、预检缓存 | [配置](https://sbproxy.dev/docs/config#cors) |
| 安全头 | HSTS、CSP、X-Frame-Options、Referrer-Policy、Permissions-Policy | [安全](https://sbproxy.dev/docs/security#headers) |
| CSRF 保护 | 基于令牌的身份验证,使用 Cookie + 头部校验 | [安全](https://sbproxy.dev/docs/security#csrf) |
| HTTP 签名 | RFC 9421 的入站验证与出站签名 | [安全](https://sbproxy.dev/docs/security#signatures) |
| 机器人检测 | 基于来源的机器人检测,支持允许/拒绝列表 | [安全](https://sbproxy.dev/docs/security#bot-detection) |
| CEL 表达式 | 使用通用表达式语言(CEL)进行自定义访问控制 | [脚本](https://sbproxy.dev/docs/scripting#cel) |
### 流量处理
| 功能 | 描述 | 文档 |
|---|---|---|
| 响应缓存 | TTL、stale-while-revalidate、stale-if-error、可自定义缓存键 | [配置](https://sbproxy.dev/docs/config#response-cache) |
| 压缩 | gzip、br、zstd,支持按内容类型排除 | [配置](https://sbproxy.dev/docs/config#compression) |
| 请求修改器 | 头部注入、URL 重写、使用模板变量的请求体替换 | [配置](https://sbproxy.dev/docs/config#modifiers) |
| 响应修改器 | 响应头部注入、状态码覆盖 | [配置](https://sbproxy.dev/docs/config#modifiers) |
| 转换 | JSON 投影、HTML、Markdown、Lua JSON、模板渲染,以及 15+ 其他功能 | [配置](https://sbproxy.dev/docs/config#transforms) |
| 错误页面 | 使用 Mustache 模板渲染的自定义错误页面 | [配置](https://sbproxy.dev/docs/config#error-pages) |
| Webhooks | 生命周期回调(on_load、on_request、on_response),支持同步与异步模式 | [Webhooks](https://sbproxy.dev/docs/webhooks) |
| 会话管理 | 基于 Cookie 的会话,支持可配置的策略 | [配置](https://sbproxy.dev/docs/config#session) |
| 功能标志 | 通过头部或配置覆盖每个来源的行为 | [配置](https://sbproxy.dev/docs/config#feature-flags) |
| Lua 脚本 | JSON 转换与自定义请求/响应逻辑 | [脚本](https://sbproxy.dev/docs/scripting#lua) |
### 可观测性
| 功能 | 描述 | 文档 |
|---|---|---|
| 结构化日志 | 支持多后端(标准错误、ClickHouse),包含 IP 掩码与采样 | [日志](https://sbproxy.dev/docs/logging) |
| Prometheus 指标 | 请求延迟、吞吐量、缓存命中率、错误率 | [指标](https://sbproxy.dev/docs/metrics) |
| OpenTelemetry | 支持 OTLP 导出的分布式追踪 | [追踪](https://sbproxy.dev/docs/tracing) |
| 事件 | 发布/订阅事件总线,支持生命周期与安全事件 | [事件](https://sbproxy.dev/docs/events) |
| PROXY 协议 | 支持 v1/v2,适用于 L4 负载均衡器部署 | [配置](https://sbproxy.dev/docs/config#proxy-protocol) |
### 协议支持
| 协议 | 状态 |
|---|---|
| HTTP/1.1 | 支持 |
| HTTP/2 | 支持(支持连接合并) |
| HTTP/3 (QUIC) | 支持 |
| WebSocket | 支持 |
| gRPC | 支持 |
| SSE | 支持(流式传输) |
## 架构
SBproxy 在启动时将每个来源配置编译为一个 18 层处理链。请求以函数调用序列执行预编译的处理链,无需映射查找或重新读取配置。
```
Request -> Global Middleware (14 layers) -> Host Routing (O(1) lookup) -> Origin Chain (18 layers) -> Action
```
来源处理链(执行顺序):
```
AllowedMethods -> ForceSSL -> ErrorPages -> TrafficCapture ->
MessageSignatures -> Session -> ThreatProtection -> BotDetection ->
RateLimitHeaders -> Policies -> OnRequest -> Auth ->
RequestModifiers -> ResponseModifiers -> OnResponse ->
ResponseCache -> Transforms -> Action
```
完整的启动流程、流程与包映射请参考 [docs/architecture.md](docs/architecture.md)。
## SBproxy Cloud
[cloud.sbproxy.dev](https://cloud.sbproxy.dev) 提供企业级功能、托管服务和配置仪表板。
企业能力包括金丝雀发布、流量镜像、API 版本控制、地理屏蔽、威胁检测、AI 护栏、语义缓存、预算强制执行、WASM 插件、OAuth 2.0、A/B 测试、编排流水线等。
## 文档
| 资源 | 链接 |
|---|---|
| 完整文档 | [sbproxy.dev/docs](https://sbproxy.dev/docs) |
| 配置参考 | [sbproxy.dev/docs/config](https://sbproxy.dev/docs/config) |
| AI 网关指南 | [sbproxy.dev/docs/ai](https://sbproxy.dev/docs/ai) |
| 安全指南 | [sbproxy.dev/docs/security](https://sbproxy.dev/docs/security) |
| 脚本(CEL + Lua) | [sbproxy.dev/docs/scripting](https://sbproxy.dev/docs/scripting) |
| 架构 | [docs/architecture.md](docs/architecture.md) |
| 示例 | [examples/](examples/) |
| 对比 | [docs/comparison.md](docs/comparison.md) |
## 许可证
Apache License 2.0。详细信息请参考 [LICENSE](LICENSE)。
SBproxy 是 [Soap Bucket LLC](https://www.soapbucket.org) 的项目。
标签:200+大语言模型, AI流量管理, AI网关, Apache 2.0许可证, Docker, EVTX分析, Go语言, Homebrew, HTTP代理, Naabu, OpenAI兼容, Python工具, rizin, SEO: AI网关, SEO: 单二进制, SEO: 反向代理, SEO: 大语言模型网关, YAML配置, 低延迟, 内容安全, 单二进制, 反向代理, 响应缓存, 威胁情报, 安全防御评估, 开发者工具, 故障转移, 日志审计, 模型路由, 热重载, 程序破解, 认证鉴权, 路径路由, 零依赖