使用 Mihomo (Clash Meta) + WARP 为 VPS 添加 IPv4/IPv6 支持

适用场景:

  • IPv6-only VPS 获取 IPv4 访问能力
  • IPv4-only VPS 获取 IPv6 访问能力
  • 通过 Mihomo 实现灵活的流量路由和代理

相关文档:

前置准备

1. 安装 Mihomo

下载 Mihomo

# 下载最新版本(Linux amd64)
wget -O /usr/local/bin/mihomo https://github.com/MetaCubeX/mihomo/releases/latest/download/mihomo-linux-amd64

# 或使用 curl
curl -fsSL https://github.com/MetaCubeX/mihomo/releases/latest/download/mihomo-linux-amd64 -o /usr/local/bin/mihomo

# 添加执行权限
chmod +x /usr/local/bin/mihomo

其他架构下载

  • Linux ARM64: mihomo-linux-arm64
  • Linux ARMv7: mihomo-linux-armv7
  • Linux 386: mihomo-linux-386

创建配置目录

# 创建 Mihomo 配置目录
mkdir -p /etc/mihomo

# 创建日志目录
mkdir -p /var/log/mihomo

2. 获取 WARP 配置信息

安装 wgcf 工具

# 下载 wgcf
wget -O /usr/local/bin/wgcf https://github.com/ViRb3/wgcf/releases/latest/download/wgcf_2.2.20_linux_amd64

# 添加执行权限
chmod +x /usr/local/bin/wgcf

注册 WARP 账户并生成配置

# 注册 WARP 账户
wgcf register

# 生成 WireGuard 配置文件
wgcf generate

查看生成的配置

cat wgcf-profile.conf

示例输出:

[Interface]
PrivateKey = YourPrivateKeyHere==
Address = 172.16.0.2/32, fd01:5ca1:ab1e:xxxx:xxxx:xxxx:xxxx:xxxx/128
DNS = 1.1.1.1

[Peer]
PublicKey = bmXOC+F1FxEMF9dyiK2H5/1SUtzH0JuVo51h2wPfgyo=
AllowedIPs = 0.0.0.0/0, ::/0
Endpoint = engage.cloudflareclient.com:2408

生成的配置文件包含以下关键信息:

  • PrivateKey: 客户端私钥
  • Address: 分配的 IPv4 和 IPv6 地址
  • PublicKey: WARP 服务端公钥

方案一:使用 TUN 模式(推荐)

TUN 模式是一种虚拟网卡技术,可以实现全局透明代理,无需手动设置系统代理或环境变量

IPv6-only VPS 获取 IPv4 访问

创建 /etc/mihomo/config.yaml

# Mihomo 基础配置
mixed-port: 7890
allow-lan: true
mode: rule
log-level: info
external-controller: 127.0.0.1:9090

# TUN 配置
tun:
  enable: true
  stack: mixed  # system/gvisor/mixed,推荐 mixed
  device: mihomo-tun
  auto-route: true
  auto-redirect: true
  auto-detect-interface: true
  dns-hijack:
    - any:53
  mtu: 1280

# DNS 配置
dns:
  enable: true
  listen: 0.0.0.0:53
  enhanced-mode: fake-ip
  fake-ip-range: 198.18.0.1/16
  nameserver:
    - 1.1.1.1
    - 8.8.8.8

# 代理配置
proxies:
  - name: "WARP-IPv4"
    type: wireguard
    server: 2606:4700:d0::a29f:c001  # IPv6-only 机器使用 IPv6 端点
    port: 2408
    ip: 172.16.0.2  # 从 wgcf-profile.conf 获取
    ipv6: fd01:5ca1:ab1e:xxxx:xxxx:xxxx:xxxx:xxxx  # 替换为实际地址
    private-key: "YourPrivateKeyHere=="  # 从 wgcf-profile.conf 获取
    public-key: "bmXOC+F1FxEMF9dyiK2H5/1SUtzH0JuVo51h2wPfgyo="
    allowed-ips:
      - 0.0.0.0/0  # 仅路由 IPv4 流量
    mtu: 1280
    udp: true

# 代理组
proxy-groups:
  - name: "PROXY"
    type: select
    proxies:
      - WARP-IPv4
      - DIRECT

# 路由规则
rules:
  # IPv4 流量走 WARP
  - IP-CIDR,0.0.0.0/0,WARP-IPv4,no-resolve
  # IPv6 流量直连
  - IP-CIDR6,::/0,DIRECT,no-resolve

IPv4-only VPS 获取 IPv6 访问

创建 /etc/mihomo/config.yaml

# Mihomo 基础配置
mixed-port: 7890
allow-lan: true
mode: rule
log-level: info
external-controller: 127.0.0.1:9090

# TUN 配置
tun:
  enable: true
  stack: mixed
  device: mihomo-tun
  auto-route: true
  auto-redirect: true
  auto-detect-interface: true
  dns-hijack:
    - any:53
  mtu: 1280

# DNS 配置
dns:
  enable: true
  listen: 0.0.0.0:53
  enhanced-mode: fake-ip
  fake-ip-range: 198.18.0.1/16
  nameserver:
    - 1.1.1.1
    - 8.8.8.8

# 代理配置
proxies:
  - name: "WARP-IPv6"
    type: wireguard
    server: 162.159.192.1  # IPv4-only 机器使用 IPv4 端点
    port: 2408
    ip: 172.16.0.2
    ipv6: fd01:5ca1:ab1e:xxxx:xxxx:xxxx:xxxx:xxxx
    private-key: "YourPrivateKeyHere=="
    public-key: "bmXOC+F1FxEMF9dyiK2H5/1SUtzH0JuVo51h2wPfgyo="
    allowed-ips:
      - ::/0  # 仅路由 IPv6 流量
    mtu: 1280
    udp: true

# 代理组
proxy-groups:
  - name: "PROXY"
    type: select
    proxies:
      - WARP-IPv6
      - DIRECT

# 路由规则
rules:
  # IPv4 流量直连
  - IP-CIDR,0.0.0.0/0,DIRECT,no-resolve
  # IPv6 流量走 WARP
  - IP-CIDR6,::/0,WARP-IPv6,no-resolve

TUN 模式注意事项

  1. 权限要求: TUN 模式需要 root 权限运行
  2. 内核支持: 确保系统内核支持 TUN 设备
  3. 防火墙: 需要放行内核进程才能使用 system/mixed 栈
  4. DNS 劫持: dns-hijack 会拦截所有 DNS 请求

方案二:使用系统代理方式

如果不想使用 TUN 模式,可以通过设置系统代理或环境变量的方式使用 Mihomo。

IPv6-only VPS 获取 IPv4 访问

创建 /etc/mihomo/config.yaml

# Mihomo 基础配置
mixed-port: 7890
allow-lan: true
mode: rule
log-level: info
external-controller: 127.0.0.1:9090

# 代理配置
proxies:
  - name: "WARP-IPv4"
    type: wireguard
    server: 2606:4700:d0::a29f:c001  # IPv6-only 机器使用 IPv6 端点
    port: 2408
    ip: 172.16.0.2
    ipv6: fd01:5ca1:ab1e:xxxx:xxxx:xxxx:xxxx:xxxx
    private-key: "YourPrivateKeyHere=="
    public-key: "bmXOC+F1FxEMF9dyiK2H5/1SUtzH0JuVo51h2wPfgyo="
    allowed-ips:
      - 0.0.0.0/0
    mtu: 1280
    udp: true

# 代理组
proxy-groups:
  - name: "PROXY"
    type: select
    proxies:
      - WARP-IPv4
      - DIRECT

# 路由规则
rules:
  - IP-CIDR,0.0.0.0/0,WARP-IPv4,no-resolve
  - IP-CIDR6,::/0,DIRECT,no-resolve

配置系统代理

使用环境变量设置代理:

# 临时设置
export http_proxy=http://127.0.0.1:7890
export https_proxy=http://127.0.0.1:7890
export all_proxy=socks5://127.0.0.1:7890

# 永久设置(添加到 ~/.bashrc 或 /etc/profile)
echo 'export http_proxy=http://127.0.0.1:7890' >> ~/.bashrc
echo 'export https_proxy=http://127.0.0.1:7890' >> ~/.bashrc
echo 'export all_proxy=socks5://127.0.0.1:7890' >> ~/.bashrc
source ~/.bashrc

启动和管理 Mihomo

手动启动

# 前台运行(用于测试)
mihomo -d /etc/mihomo

# 后台运行
nohup mihomo -d /etc/mihomo > /var/log/mihomo/mihomo.log 2>&1 &

创建 systemd 服务

创建 /etc/systemd/system/mihomo.service

[Unit]
Description=Mihomo Service
After=network.target

[Service]
Type=simple
User=root
ExecStart=/usr/local/bin/mihomo -d /etc/mihomo
Restart=on-failure
RestartSec=5s

[Install]
WantedBy=multi-user.target

管理服务

# 重载 systemd 配置
systemctl daemon-reload

# 启动服务
systemctl start mihomo

# 查看服务状态
systemctl status mihomo

# 启用开机自启
systemctl enable mihomo

# 重启服务
systemctl restart mihomo

# 停止服务
systemctl stop mihomo

# 查看日志
journalctl -u mihomo -f

验证配置

测试 IPv4 连接

# 通过代理测试 IPv4
curl -x http://127.0.0.1:7890 -4 ip.p3terx.com
curl -x http://127.0.0.1:7890 -4 ifconfig.me

# 或使用环境变量
export http_proxy=http://127.0.0.1:7890
curl -4 ip.p3terx.com

测试 IPv6 连接

# 通过代理测试 IPv6
curl -x http://127.0.0.1:7890 -6 ip.p3terx.com
curl -x http://127.0.0.1:7890 -6 ifconfig.me

# 或使用环境变量
export http_proxy=http://127.0.0.1:7890
curl -6 ip.p3terx.com

故障排查

1. Mihomo 无法启动

# 检查配置文件语法
mihomo -t -d /etc/mihomo

# 查看详细错误日志
mihomo -d /etc/mihomo

# 检查端口占用
netstat -tlnp | grep 7890
lsof -i :7890

2. 无法连接到 WARP

# 检查 WireGuard 密钥是否正确
cat wgcf-profile.conf

# 测试端点连通性(IPv4 机器)
ping -c 4 162.159.192.1

# 测试端点连通性(IPv6 机器)
ping6 -c 4 2606:4700:d0::a29f:c001

# 检查防火墙
iptables -L -n -v
ufw status

配置参数说明

端点地址选择规则

IPv4-only 机器(获取 IPv6):

  • 使用 IPv4 端点:162.159.192.1162.159.193.1
  • 配置 allowed-ips: ["::/0"] 仅路由 IPv6 流量

IPv6-only 机器(获取 IPv4):

  • 使用 IPv6 端点:2606:4700:d0::a29f:c0012606:4700:d0::a29f:c101
  • 配置 allowed-ips: ["0.0.0.0/0"] 仅路由 IPv4 流量

双栈机器:

  • 可使用任意端点
  • 配置 allowed-ips: ["0.0.0.0/0", "::/0"] 路由所有流量

使用场景示例

场景 1:仅 ChatGPT 走 WARP

rules:
  - DOMAIN-SUFFIX,openai.com,WARP-IPv4
  - DOMAIN-SUFFIX,chatgpt.com,WARP-IPv4
  - DOMAIN-KEYWORD,openai,WARP-IPv4
  - MATCH,DIRECT

场景 2:仅流媒体走 WARP

rules:
  - DOMAIN-SUFFIX,netflix.com,WARP-IPv4
  - DOMAIN-SUFFIX,hulu.com,WARP-IPv4
  - DOMAIN-SUFFIX,disneyplus.com,WARP-IPv4
  - MATCH,DIRECT

注意事项

  1. 端点地址选择: IPv4 机器使用 IPv4 端点(162.159.192.1),IPv6 机器使用 IPv6 端点(2606:4700:d0::a29f:c001)
  2. 密钥安全: 妥善保管 private-key,不要泄露
  3. 配置备份: 定期备份 wgcf-account.toml 和配置文件
  4. 合规使用: 遵守 Cloudflare 服务条款,避免滥用
  5. 性能影响: WARP 会增加延迟,不适合低延迟场景
  6. IP 限制: WARP IP 可能被部分服务封禁

相关资源

官方文档

工具项目

相关教程


本文档整理自 Mihomo 官方文档和社区实践,仅供学习和参考使用。