为 Ollama 添加 API Key 鉴权的原生 macOS 应用 + 一键 Cloudflare Tunnel 公网暴露
原生 Swift · 零依赖 · 轻量 · 磨砂玻璃 UI · 一键公网
中文 · English
- 🖥️ 原生 macOS 应用 — SwiftUI + 磨砂玻璃 UI,零外部依赖
- 🔐 API Key 鉴权 — Bearer Token 认证,保护你的 Ollama 服务
- 🌐 Cloudflare Tunnel — 一键暴露公网,自带 HTTPS,内置 cloudflared 自动下载
- 📊 实时 Dashboard — 请求统计、延迟监控、成功率、活动图表、错误状态提示
- 🔄 透明代理 — 完整支持流式响应(SSE),兼容所有 Ollama API(含 Cloud 模型)
- 🌙 明暗主题 — 深色/浅色/跟随系统,全局磨砂玻璃高级感
- 🌍 中英双语 — 默认中文,一键切换
- 📌 状态栏常驻 — 左键显示窗口,右键弹出菜单
- 🚀 开机自启 — 系统级 Login Item 支持
- 🔄 自动更新 — 自动检测 GitHub 新版本,一键下载安装
- 🙈 隐藏 Dock — 可选隐藏 Dock 图标,仅保留状态栏
- ☁️ 自动 Tunnel — 可选随服务自动启动 Cloudflare Tunnel
- 💾 导出日志 — 请求日志导出为 CSV 文件
- 🐳 Docker 可选 — 同时提供 Python FastAPI 版本用于服务器部署
前往 Releases 下载对应架构的 .dmg:
| 芯片 | 文件 |
|---|---|
| Apple Silicon (M1/M2/M3/M4) | OllamaGateway-vX.X.X-arm64.dmg |
| Intel | OllamaGateway-vX.X.X-x86_64.dmg |
打开 DMG → 拖入 Applications → 首次启动右键"打开"。
⚠️ macOS 签名提示:因应用未签名,首次打开可能被 macOS 拦截。在终端运行:xattr -cr /Applications/OllamaGateway.app之后双击即可正常启动。
git clone https://github.com/RayYiHang/OllamaGateway.git
cd OllamaGateway
bash scripts/build.sh release
open build/OllamaGateway.app需要 macOS 13+ 和 Xcode Command Line Tools
- 打开应用 → 进入 Settings 标签页
- 配置 Ollama 地址 → 默认
http://localhost:11434 - 添加 API Key → 点击"生成随机密钥"或手动输入
- 启动服务 → 点击左下角「启动」按钮
- 使用网关 →
# 健康检查
curl http://localhost:8000/
# 对话(需鉴权)
curl http://localhost:8000/api/chat \
-H "Authorization: Bearer sk-your-key" \
-H "Content-Type: application/json" \
-d '{"model": "qwen2.5", "messages": [{"role": "user", "content": "hello"}]}'- 应用 → 设置 → Cloudflare Tunnel → 点击「启动」
- 首次使用会自动下载 cloudflared(无需手动安装)
- 获得一个
*.trycloudflare.comHTTPS 地址 - 分享此地址即可远程使用你的 Ollama 服务
💡 在设置中开启"自动启动 Tunnel",随服务一起自动开启。
如果遇到连接问题或 Cloud 模型 404 错误,请配置 Ollama 环境变量:
# 允许所有来源访问(推荐在使用反向代理时设置)
launchctl setenv OLLAMA_ORIGINS "*"
# 如需允许远程访问 Ollama
launchctl setenv OLLAMA_HOST "0.0.0.0:11434"设置后需重启 Ollama 应用。网关已内置 Host header 转发,确保与 Ollama 的 Host 校验兼容。
Client → Ollama Gateway (:8000) → Ollama (:11434)
│
API Key 验证
Host Header 转发
请求日志记录
流式响应转发
│
Cloudflare Tunnel (可选)
│
公网 HTTPS 访问
技术栈:纯 Swift + SwiftUI + Network.framework + URLSession
- HTTP 服务器:
NWListener(零依赖,系统框架) - HTTP 客户端:
URLSession(原生流式传输) - GUI:SwiftUI(磨砂玻璃 Material Design)
- 隧道:内置
cloudflared自动下载管理 - 无任何第三方库
同时提供 Python FastAPI 版本,适用于 Linux 服务器 / Docker 部署:
# 配置环境变量
echo "API_KEYS=sk-your-key-1,sk-your-key-2" > .env
echo "OLLAMA_BASE_URL=http://localhost:11434" >> .env
# Docker 运行
docker build -t ollama-gateway .
docker run -d --name ollama-gateway \
--env-file .env \
--network host \
-p 8000:8000 \
ollama-gateway直接运行(不用 Docker)
pip install -r requirements.txt
uvicorn main:app --host 0.0.0.0 --port 8000├── Package.swift # Swift 项目(零依赖)
├── Sources/OllamaGateway/
│ ├── OllamaGatewayApp.swift # 应用入口 + StatusBar
│ ├── ProxyServer.swift # HTTP 反向代理(含 Host Header 转发)
│ ├── AppState.swift # 数据模型 + 状态管理
│ ├── Services.swift # 健康检测 + 更新检查
│ ├── CloudflareTunnel.swift # Cloudflare Tunnel(自动下载)
│ ├── MainView.swift # 主窗口布局
│ ├── DashboardView.swift # 仪表盘
│ ├── SettingsView.swift # 设置面板
│ ├── Components.swift # UI 组件库
│ ├── Theme.swift # 主题系统
│ └── Localization.swift # 中英文本地化
├── scripts/ # 构建 & 打包脚本
├── .github/workflows/release.yml # CI/CD(自动构建双架构)
├── main.py # Python 版本(Docker 用)
├── Dockerfile
└── docs/PLAN.md
| 项目 | 默认值 | 说明 |
|---|---|---|
| Ollama Base URL | http://localhost:11434 |
Ollama 服务地址 |
| Server Port | 8000 |
网关监听(反代)端口 |
| API Keys | — | 鉴权密钥(支持多个) |
| CF Tunnel | 关闭 | 一键 Cloudflare 公网暴露 |
| 隐藏 Dock | 关闭 | 仅保留状态栏图标 |
| 自动 Tunnel | 关闭 | 随服务自动启动 Tunnel |
所有配置通过应用 GUI 管理,持久化在 UserDefaults 中。
# 克隆
git clone https://github.com/RayYiHang/OllamaGateway.git && cd OllamaGateway
# 开发构建
swift build
# Release 构建 + 打包 DMG
bash scripts/build.sh release
bash scripts/create-dmg.sh
# 发布新版本
git tag 1.0.0 && git push origin 1.0.0
# → GitHub Actions 自动构建 arm64 + x86_64 DMG| 端点 | 鉴权 | 说明 |
|---|---|---|
GET / |
✗ | 健康检查 |
/{path} |
✓ Bearer Token | 透明代理到 Ollama |
兼容所有 OpenAI 格式客户端(Cursor、Continue、Open WebUI 等)。
欢迎提交 PR 和 Issue!中英文均可。
