Bifrost — a proxy bridge between worlds. Route traffic through upstream proxies or let it pass directly, with per-rule control and local auth.
Bifrost — 连接两界的代理之桥。按规则决定流量走上游代理还是直连,支持本地认证与精细管控。
名称取自北欧神话中的 Bifrost(彩虹桥)——连接人间(Midgard)与神域(Asgard)的通道。正如彩虹桥由守护者海姆达尔把控通行,Bifrost 代理桥也为每一条流量做出裁决:经由上游代理通往外部世界,或直连目的地。
前往 Releases 页面下载对应平台的二进制文件,解压后即可使用。
需要 Go 1.24+:
git clone https://cnb.cool/debug.icu/bifrost.git
cd bifrost
make build
构建产物位于 bin/bifrost。
make build-all
生成所有平台的二进制文件(Linux/macOS/Windows,amd64/arm64)。
1. 启动服务
bifrost start
首次启动时,Bifrost 会在 ~/.bifrost/config.yaml 创建默认配置文件。如果启用了认证且未指定密码,会自动生成一个随机密码并输出到日志。
2. 配置系统代理
将系统或应用的 HTTP 代理指向 Bifrost:
http://bifrost:<password>@127.0.0.1:7890
3. 停止服务
bifrost stop
配置文件默认位于 ~/.bifrost/config.yaml,使用 --config / -c 可指定其他路径。
upstream:
addr: "127.0.0.1:1080" # 上游代理地址(可通过环境变量覆盖)
username: "" # 上游代理用户名(可选)
password: "" # 上游代理密码(可选)
local:
port: 7890 # 本地监听端口
listen: "127.0.0.1" # 监听地址(127.0.0.1 或 0.0.0.0)
auth:
enabled: true # 是否启用本地认证
username: "bifrost" # 本地认证用户名
password: "" # 留空则启动时自动生成
routing:
default: proxy # 默认策略:proxy(走代理)或 direct(直连)
rules:
- match: "*.cn"
action: direct
- match: "192.168.0.0/16"
action: direct
- match: "localhost"
action: direct
上游代理支持通过环境变量配置,环境变量优先级高于配置文件。
| 环境变量 | 说明 |
|---|---|
BIFROST_UPSTREAM | 完整代理 URL,如 http://user:pass@host:port |
BIFROST_UPSTREAM_ADDR | 上游代理地址(host:port) |
BIFROST_UPSTREAM_USERNAME | 上游代理用户名 |
BIFROST_UPSTREAM_PASSWORD | 上游代理密码 |
BIFROST_UPSTREAM 设置时,会同时解析地址和认证信息,优先级高于三个独立变量。
# 方式一:完整 URL(推荐)
export BIFROST_UPSTREAM="http://user:pass@10.0.0.1:8080"
bifrost start
# 方式二:独立变量
export BIFROST_UPSTREAM_ADDR="10.0.0.1:8080"
export BIFROST_UPSTREAM_USERNAME="user"
export BIFROST_UPSTREAM_PASSWORD="pass"
bifrost start
每条规则包含一个匹配条件(match)和一个动作(action)。规则按顺序匹配,首条命中即生效;无匹配则使用默认策略。
| 动作 | 说明 |
|---|---|
proxy | 经上游代理转发 |
direct | 直连目标,绕过上游代理 |
Bifrost 根据 match 字段的格式自动判断匹配类型:
| 格式 | 类型 | 示例 | 说明 |
|---|---|---|---|
*.xxx | 域名通配符 | *.cn | 匹配该域名下所有子域名 |
x.x.x.x/n | CIDR | 192.168.0.0/16 | 匹配 IP 段(目标为 IP 时生效) |
x.x.x.x | IP | 10.0.0.1 | 精确匹配 IP 地址 |
a.b.c | 域名精确 | example.com | 精确匹配域名(含 .) |
| 其他 | 关键词 | localhost | 域名中包含该字符串即匹配 |
routing:
default: proxy
rules:
# 国内域名直连
- match: "*.cn"
action: direct
# 内网 IP 直连
- match: "10.0.0.0/8"
action: direct
- match: "192.168.0.0/16"
action: direct
# 本地地址直连
- match: "localhost"
action: direct
- match: "127.0.0.1"
action: direct
# 指定域名走代理
- match: "*.google.com"
action: proxy
bifrost start 启动代理服务(前台运行) bifrost stop 停止服务 bifrost status 查看运行状态 bifrost reload 热重载配置文件 bifrost gen-password 生成随机强密码 bifrost rules list 查看当前规则列表 bifrost rules add <匹配> <动作> 添加规则 bifrost rules remove <匹配> 删除规则 bifrost version 查看版本号
bifrost start --log-level debug # 日志级别:debug / info / warn / error
bifrost start -c /path/to/config # 指定配置文件
# 查看规则
bifrost rules list
# 添加规则
bifrost rules add "*.github.com" proxy
bifrost rules add "172.16.0.0/12" direct
# 删除规则
bifrost rules remove "*.github.com"
规则变更会写入配置文件。如果服务正在运行,执行 bifrost reload 使其生效。
bifrost gen-password # 默认 32 位
bifrost gen-password -l 64 # 指定长度
MIT