Terminal
  • Menu ▾
    • 关于
    • 博客
    • 项目
    • 🎬 影音
    • 🎵 音乐
    • 🧭 导航
  • 关于
  • 博客
  • 项目
  • 🎬 影音
  • 🎵 音乐
  • 🧭 导航

为什么需要透明代理?

科学上网工具(Clash、V2Ray 等)需要每台设备单独配置,手机、电视、IoT 设备不方便装软件。mihomo(原 Clash Meta) 作为透明代理网关,让全网设备自动走代理,无需单独配置。

实际效果:

  • ✅ 全网设备自动科学上网
  • ✅ 手机/电视/IoT 无需装软件
  • ✅ 智能分流(国内直连、国外代理)
  • ✅ 路由器 DNS 指向即可生效
  • ✅ RESTful API 管理

技术架构

设备请求国内域名 → mihomo 分流规则 → 直连 → 速度快
设备请求国外域名 → mihomo 分流规则 → 代理节点 → 科学上网

部署步骤

第一步:创建项目目录

mkdir -p ~/docker/mihomo/config
cd ~/docker/mihomo

第二步:编写 docker-compose.yml

services:
  mihomo:
    image: metacubex/mihomo:latest
    container_name: mihomo
    restart: unless-stopped
    ports:
      - "7890:7890"      # HTTP 代理
      - "7891:7891"      # SOCKS5 代理
      - "9090:9090"      # API 管理面板
    volumes:
      - ./config:/root/.config/mihomo    # 配置目录
    environment:
      - http_proxy=      # OrbStack 代理兼容
      - https_proxy=
      - ALL_PROXY=

第三步:准备配置文件

在 ./config/ 目录下创建 config.yaml:

# 基础设置
mixed-port: 7890          # HTTP 代理端口
socks-port: 7891          # SOCKS5 代理端口
port: 9090                # API 管理端口
allow-lan: true           # 允许局域网连接
mode: rule                # 规则模式
log-level: info           # 日志级别
external-controller: 0.0.0.0:9090   # API 监听地址

# 代理节点
proxies:
  - name: "节点1"
    type: ss
    server: example.com
    port: 443
    cipher: chacha20-ietf-poly1305
    password: "your-password"

  - name: "节点2"
    type: vmess
    server: example2.com
    port: 443
    uuid: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
    alterId: 0
    cipher: auto
    tls: true

# 代理组
proxy-groups:
  - name: "Proxy"
    type: select
    proxies:
      - 节点1
      - 节点2
      - DIRECT

# 分流规则
rules:
  # 国内直连
  - GEOIP,CN,DIRECT
  - DOMAIN-SUFFIX,cn,DIRECT
  - DOMAIN-SUFFIX,baidu.com,DIRECT
  - DOMAIN-SUFFIX,qq.com,DIRECT
  - DOMAIN-SUFFIX,taobao.com,DIRECT
  
  # 广告拦截
  - DOMAIN-SUFFIX,ads.example.com,REJECT
  
  # 其他走代理
  - MATCH,Proxy

第四步:启动容器

docker compose up -d

第五步:验证服务

# 测试 HTTP 代理
curl -x http://localhost:7890 https://www.google.com

# 测试 API
curl http://localhost:9090

客户端配置

方案一:系统代理(每台设备)

在设备的网络设置中配置代理:

  • HTTP 代理:http://NAS-IP:7890
  • SOCKS5 代理:socks5://NAS-IP:7891

方案二:路由器透明代理(全网)

  1. 在路由器中设置 DNS 为 mihomo 所在设备 IP
  2. 配置 iptables 规则(需要 OpenWrt 等第三方固件)
  3. 或使用 mihomo 的 TProxy 模式

方案三:WiFi 代理(手机)

手机 WiFi 设置中配置代理:

  • 服务器:NAS IP
  • 端口:7890
  • 类型:HTTP

订阅管理

自动更新订阅

在配置中添加订阅源:

proxy-providers:
  my-provider:
    type: http
    url: "https://你的机场.com/link/xxxxx"
    interval: 3600    # 每小时更新
    path: ./providers/my-provider.yaml
    health-check:
      enable: true
      interval: 600
      url: https://www.gstatic.com/generate_204

手动更新

# 下载订阅文件
curl -o ~/docker/mihomo/config/config.yaml "https://订阅链接"

# 重启容器
cd ~/docker/mihomo
docker compose restart

API 管理

mihomo 提供 RESTful API:

# 查看代理组
curl http://localhost:9090/proxies

# 切换代理组
curl -X PUT http://localhost:9090/proxies/Proxy -d '{"name": "节点1"}'

# 查看规则
curl http://localhost:9090/rules

# 查看连接
curl http://localhost:9090/connections

Web 管理面板

访问 http://NAS-IP:9090/ui 可以使用 Web 管理界面,或使用第三方面板:

  • Yacd:http://yacd.haishan.me
  • Metacubexd:http://metacubexd.pages.dev

连接地址填:http://NAS-IP:9090

分流规则详解

规则类型

类型说明示例
DOMAIN精确域名DOMAIN,google.com,Proxy
DOMAIN-SUFFIX域名后缀DOMAIN-SUFFIX,google.com,Proxy
DOMAIN-KEYWORD域名关键词DOMAIN-KEYWORD,google,Proxy
IP-CIDRIP 段IP-CIDR,8.8.8.8/32,Proxy
GEOIPGeoIP 数据库GEOIP,CN,DIRECT
MATCH兜底规则MATCH,Proxy

常用规则集

rule-providers:
  reject:
    type: http
    behavior: domain
    url: "https://cdn.jsdelivr.net/gh/Loyalsoldier/clash-rules@release/reject.txt"
    path: ./ruleset/reject.yaml
    interval: 86400

  direct:
    type: http
    behavior: domain
    url: "https://cdn.jsdelivr.net/gh/Loyalsoldier/clash-rules@release/direct.txt"
    path: ./ruleset/direct.yaml
    interval: 86400

rules:
  - RULE-SET,reject,REJECT
  - RULE-SET,direct,DIRECT
  - GEOIP,CN,DIRECT
  - MATCH,Proxy

性能优化

开启 TCP Fast Open

tcp-concurrent: true
tcp-fast-open: true

调整并发连接数

keep-alive-interval: 30

使用 Sniffer 嗅探

sniffer:
  enable: true
  sniff:
    TLS:
      ports: [443]
    HTTP:
      ports: [80]
      override-destination: true

坑

⚠️ OrbStack 代理注入:OrbStack 会注入 http_proxy=http://127.0.0.1:7897,必须在 compose 中显式清空代理环境变量,否则容器内网络异常。

⚠️ 端口冲突:如果 Mac 上有其他代理软件(如 ClashX),会占用 7890 端口,需要先关闭或改端口。

⚠️ 配置文件格式:YAML 对缩进敏感,必须用空格,不能用 Tab。

⚠️ 订阅更新:机场订阅链接有时效性,过期后需要重新获取。

验证

# 测试代理
curl -x http://localhost:7890 https://www.google.com

# 测试直连
curl -x http://localhost:7890 https://www.baidu.com

# 查看日志
docker logs mihomo
© 2026 Powered by Hugo :: Theme made by panr
--