pve lxc 环境下 安装配置 Clash Meta 作为透明网关
侧边栏壁纸
  • 累计撰写 16 篇文章
  • 累计收到 11 条评论

pve lxc 环境下 安装配置 Clash Meta 作为透明网关

坑了个飞
2024-02-01 / 0 评论 / 837 阅读 / 正在检测是否收录...
Clash Meta 已更名为 mihomo

环境说明:

  • 机器: J643 6 口小主机
  • pve 8.0.4
  • lxc 容器里装个 debian 12

一、lxc 容器开启 tun/tap 支持

编辑 /etc/pve/lxc/[ID].conf 文件,

在文件最后添加如下内容:

lxc.hook.autodev: sh -c "modprobe tun" 
lxc.cgroup.devices.allow: c 10:200 rwm 
lxc.cgroup2.devices.allow: c 10:200 rwm 
lxc.mount.entry: /dev/net dev/net none bind,create=dir

效果如下图:

image.png

保存修改后重启 lxc 容器, 重启后输入  ls -lh /dev/net  检查配置是否生效, 出现以下结果即为成功

image.png

安装配置 Clash-Meta

github 仓库: GitHub - MetaCubeX/mihomo: A simple Python Pydantic model for Honkai: Star Rail parsed data from the Mihomo API.
wiki 地址: 虚空终端 Docs

下载二进制包

wget https://github.com/MetaCubeX/mihomo/releases/download/v1.18.0/mihomo-linux-amd64-compatible-v1.18.0.gz

解压

gzip -d mihomo-linux-amd64-compatible-v1.18.0.gz

将解压出来的可执行文件重名名为 mihomo 并移动到 /usr/local/bin/

mv mihomo-linux-amd64-compatible-v1.18.0 /usr/local/bin/mihomo

添加可执行权限

chmod 755 /usr/local/bin/mihomo

开启 IP 转发

echo 'net.ipv4.ip_forward = 1' | tee -a /etc/sysctl.conf
echo 'net.ipv6.conf.all.forwarding = 1' | tee -a /etc/sysctl.conf

创建工作目录

mkdir /etc/mihomo

在  /etc/mihomo 下创建配置文件

#---------------------------------------------------#
## 配置文件需要放置在 $HOME/.config/clash.meta/*.yaml

## 这份文件是clashX的基础配置文件,请尽量新建配置文件进行修改。

## 如果您不知道如何操作,请参阅
## Clash Meta wiki  https://docs.metacubex.one
## Clash Meta wiki  https://clash-meta.wiki
## Clash Meta Github文档  https://github.com/MetaCubeX/Clash.Meta/blob/Alpha/README.md

#---------------------------------------------------#

######### 锚点 start #######
# 策略组相关
pr: &pr {type: select, proxies: [♻️ 默认,🇭🇰 香港,🇹🇼 台湾,🇯🇵 日本,🇸🇬 新加坡,🇺🇸 美国,🏴‍☠️ 其它地区,🏳️‍🌈 全部节点,🎌 自动选择,🟢 直连]}
pr2: &pr2 {type: select, proxies: [🟢 直连,♻️ 默认,🇭🇰 香港,🇹🇼 台湾,🇯🇵 日本,🇸🇬 新加坡,🇺🇸 美国,🏴‍☠️ 其它地区,🏳️‍🌈 全部节点,🎌 自动选择]}

#这里是订阅更新和延迟测试相关的
p: &p {type: http, interval: 3600, health-check: {enable: true, url: https://www.gstatic.com/generate_204, interval: 300}}

######### 锚点 end #######

# url 里填写自己的订阅,名称不能重复
proxy-providers:
  provider1:
    <<: *p
    url: "你机场的订阅地址"

# port: 7890 # HTTP(S) 代理服务器端口
# socks-port: 7891 # SOCKS5 代理端口
mixed-port: 10801 # HTTP(S) 和 SOCKS 代理混合端口
# redir-port: 7892 # 透明代理端口,用于 Linux 和 MacOS

# Linux 透明代理服务器端口(TProxy TCP 和 TProxy UDP)
tproxy-port: 7894

allow-lan: true # 允许局域网连接
bind-address: "*" # 绑定 IP 地址,仅作用于 allow-lan 为 true,'*'表示所有地址

#  find-process-mode has 3 values:always, strict, off
#  - always, 开启,强制匹配所有进程
#  - strict, 默认,由 clash 判断是否开启
#  - off, 不匹配进程,推荐在路由器上使用此模式
find-process-mode: strict

# 规则匹配模型(rule/global/direct)
mode: rule

# 开启 IPv6 总开关,关闭阻断所有 IPv6 链接和屏蔽 DNS 请求 AAAA 记录
ipv6: true

# 统一延迟, 更换延迟计算方式,去除握手等额外延迟
unified-delay: false
# TCP 并发连接所有 IP, 将使用最快握手的 TCP
tcp-concurrent: true
# RESTful API 监听地址
external-controller: 0.0.0.0:9090
# API 的访问密钥
secret: '123456'
# 配置 WEB UI 目录,使用 http://{{external-controller}}/ui 访问
external-ui: ui
external-ui-url: "https://github.com/MetaCubeX/metacubexd/archive/refs/heads/gh-pages.zip"

# GEO数据模式(更改geoip使用文件,mmdb或者dat,可选true / flase,true为dat,此项有默认值flase)
geodata-mode: true
# 自定义 geodata url
geox-url:
  geoip: "https://mirror.ghproxy.com/https://github.com/MetaCubeX/meta-rules-dat/releases/download/latest/geoip-lite.dat"
  geosite: "https://mirror.ghproxy.com/https://github.com/MetaCubeX/meta-rules-dat/releases/download/latest/geosite.dat"
  mmdb: "https://mirror.ghproxy.com/https://github.com/MetaCubeX/meta-rules-dat/releases/download/latest/country-lite.mmdb"

# 全局 TLS 指纹,优先低于 proxy 内的 client-fingerprint
# 可选: "chrome","firefox","safari","ios","random","none" options.
# Utls is currently support TLS transport in TCP/grpc/WS/HTTP for VLESS/Vmess and trojan.
global-client-fingerprint: chrome

# 缓存
profile:
  # 储存 API 对策略组的选择,以供下次启动时使用
  store-selected: true
  # 储存 fakeip 映射表,域名再次发生连接时,使用原有映射地址
  store-fake-ip: true

sniffer:
  enable: true
  sniff:
    HTTP:
      ports: [80, 8080-8880]
      override-destination: true
    TLS:
      ports: [443, 8443]
    QUIC:
      ports: [443, 8443]
  skip-domain:
    - "Mijia Cloud"

tun:
  # 是否启用 tun 模式来路由全局流量。 
  enable: true
  # tun 模式堆栈,如无使用问题,建议使用 mixed栈,默认 gvisor;MacOS 用户推荐 gvisor 栈
  stack: mixed
  # dns 劫持,一般设置为 any:53 即可, 即劫持所有 53 端口的 udp 流量
  dns-hijack:
    - "any:53"
  # 自动设置全局路由,可以自动将全局流量路由进入 tun 网卡。
  auto-route: true
  # 自动选择流量出口接口,多出口网卡同时连接的设备建议手动指定出口网卡
  auto-detect-interface: true

# 使用系统的dns, 也就是dhcp服务器下发的dns服务地址(使用我自建的dns服务来分流)
dns:
  # 关闭将使用系统 DNS
  enable: true
  # 开启 DoH 支持 HTTP/3,将并发尝试
  prefer-h3: true   
  # 开启 DNS 服务器监听                    
  listen: :53
  # false 将返回 AAAA 的空结果 
  ipv6: true
  # fake-ip or redir-host
  enhanced-mode: redir-host
  # fake-ip 池设置
  fake-ip-range: 28.0.0.1/8
  # 以下匹配项不返回 fake ip          
  fake-ip-filter:
    - "+.lan"
    - "+.local"
  nameserver:
    - https://doh.pub/dns-query
    - https://dns.alidns.com/dns-query
  proxy-server-nameserver:
    - https://doh.pub/dns-query
  nameserver-policy:
    'geosite:cn': https://doh.pub/dns-query
  fallback:
    - "https://dns.cloudflare.com/dns-query#dns"
    - "https://dns.google/dns-query#dns"
  fallback-filter:
    geoip: true
    geoip-code: CN
    geosite:
      - gfw
      - geolocation-!cn
    domain:
      - '+.google.com'
      - '+.facebook.com'
      - '+.youtube.com'
proxies:
  - name: "🟢 直连"
    type: direct
    udp: true
proxy-groups:
  - {name: ♻️ 默认, type: select, proxies: [🎌 自动选择, 🟢 直连, 🇭🇰 香港, 🇹🇼 台湾, 🇯🇵 日本, 🇸🇬 新加坡, 🇺🇸 美国, 🏴‍☠️ 其它地区, 🏳️‍🌈 全部节点]}
  - {name: dns, type: select, proxies: [🎌 自动选择, ♻️ 默认, 🇭🇰 香港, 🇹🇼 台湾, 🇯🇵 日本, 🇸🇬 新加坡, 🇺🇸 美国, 🏴‍☠️ 其它地区, 🏳️‍🌈 全部节点]}
  - {name: 📺 哔哩东南亚, <<: *pr}
  - {name: 📺 巴哈姆特, <<: *pr}
  - {name: 📺 YouTube, <<: *pr}
  - {name: 📺 NETFLIX, <<: *pr}
  - {name: 🧰 Github, <<: *pr}
  - {name: "🍎 苹果服务", <<: *pr}
  - {name: "🎮 xbox", <<: *pr}
  - {name: "🎮 playstation", <<: *pr2}
  - {name: 🇨🇳 国内, <<: *pr2}
  - {name: 🐟 漏网之鱼, <<: *pr}
  #分隔,下面是地区分组
  - {name: 🇭🇰 香港, type: select , include-all-providers: true, filter: "(?i)港|hk|hongkong|hong kong", use: [provider1]}
  - {name: 🇹🇼 台湾, type: select , include-all-providers: true, filter: "(?i)台|tw|taiwan", use: [provider1]}
  - {name: 🇯🇵 日本, type: select , include-all-providers: true, filter: "(?i)日|jp|japan", use: [provider1]}
  - {name: 🇺🇸 美国, type: select , include-all-providers: true, filter: "(?i)美|us|unitedstates|united states", use: [provider1]}
  - {name: 🇸🇬 新加坡, type: select , include-all-providers: true, filter: "(?i)(新|sg|singapore)", use: [provider1]}
  - {name: 🏴‍☠️ 其它地区, type: select , include-all-providers: true, filter: "(?i)^(?!.*(?:🇭🇰|🇯🇵|🇺🇸|🇸🇬|🇨🇳|港|hk|hongkong|台|tw|taiwan|日|jp|japan|新|sg|singapore|美|us|unitedstates)).*", use: [provider1]}
  - {name: 🏳️‍🌈 全部节点, type: select , include-all-providers: true, use: [provider1]}
  - {name: 🎌 自动选择, type: url-test, include-all-providers: true, tolerance: 10, use: [provider1]}

# 游戏规则集下载地址: https://github.com/FQrabbit/SSTap-Rule/
# 其它第三方规则:
# 1. https://github.com/DivineEngine/Profiles/tree/master
# 2. https://github.com/dler-io/Rules
# 3. https://github.com/ACL4SSR/ACL4SSR/tree/master
rule-providers:
  # 自定义直连规则
  custom-direct:
    type: http
    behavior: classical
    #【Meta专属】URL可根据rule设定匹配对应的策略,方便更新provider
    url: "https://fastly.jsdelivr.net/gh/fillpit/script@main/clash/rules/custom-direct.yaml"
    interval: 600
  # 自定义代理规则
  custom-proxy:
    type: http
    behavior: classical
    #【Meta专属】URL可根据rule设定匹配对应的策略,方便更新provider
    url: "https://fastly.jsdelivr.net/gh/fillpit/script@main/clash/rules/custom-proxy.yaml"
    interval: 600

rules:
  # 自己的规则放前面
  - RULE-SET,custom-direct,DIRECT
  - RULE-SET,custom-proxy,♻️ 默认

  # 常用局域网 ip 段 直连
  - GEOIP,lan,🟢 直连,no-resolve

  # ******** 自定义 规则区域 start ********
  # ****** 默认 非国内的域名和ip都走代理 ******
  # ****** 也就是说分流规则这里主要关注 需要直连的地址 和需要屏蔽的地址 (没匹配上的都走代理) ******

    # 测速网站 直连
  - GEOSITE,speedtest,DIRECT
  # 私有网络专用域名
  - GEOSITE,private,DIRECT

  - GEOSITE,github,🧰 Github
    # rsshub
  - GEOSITE,rsshub,♻️ 默认
  - GEOSITE,youtube,📺 YouTube

  - GEOSITE,biliintl,📺 哔哩东南亚
  - GEOSITE,bilibili,🟢 直连

  - GEOSITE,netflix,📺 NETFLIX
  - GEOIP,netflix,📺 NETFLIX

    # xbox
  - GEOSITE,xbox,🎮 xbox
  # 索尼
  - GEOSITE,playstation,🎮 playstation

  # dev
  - GEOSITE,category-dev,♻️ 默认
  # apple 的服务
  - GEOSITE,apple,🍎 苹果服务
   # game
  - GEOSITE,category-games,♻️ 默认
  # 娱乐类
  - GEOSITE,category-entertainment,♻️ 默认

  # 非国内地址
  - GEOSITE,geolocation-!cn,🐟 漏网之鱼

  # 国内域名
  - GEOSITE,CN,🇨🇳 国内
  # 国内 ip
  - GEOIP,CN,🇨🇳 国内
  # 没匹配上的  
  - DST-PORT,80,🐟 漏网之鱼
  - DST-PORT,443,🐟 漏网之鱼
  - DST-PORT,22,🐟 漏网之鱼
  - MATCH,DIRECT
注意, 你需要把此处的改成你自己的机场订阅地址

image.png

创建 systemd 配置文件 /etc/systemd/system/mihomo.service:

[Unit]
Description=mihomo Daemon, Another Clash Kernel.
After=network.target NetworkManager.service systemd-networkd.service iwd.service

[Service]
Type=simple
LimitNPROC=500
LimitNOFILE=1000000
CapabilityBoundingSet=CAP_NET_ADMIN CAP_NET_RAW CAP_NET_BIND_SERVICE CAP_SYS_TIME CAP_SYS_PTRACE CAP_DAC_READ_SEARCH
AmbientCapabilities=CAP_NET_ADMIN CAP_NET_RAW CAP_NET_BIND_SERVICE CAP_SYS_TIME CAP_SYS_PTRACE CAP_DAC_READ_SEARCH
Restart=always
ExecStartPre=/usr/bin/sleep 1s
ExecStart=/usr/local/bin/mihomo -d /etc/mihomo
ExecReload=/bin/kill -HUP $MAINPID

[Install]
WantedBy=multi-user.target

设置开机自动启动,并启动该服务

# 开机启动
systemctl enable mihomo 
# 马上启动服务 
systemctl start mihomo

查看服务运行状态

systemctl status mihomo

如下所示即启动成功

image.png

结尾

现在只需要把你需要代理的设备的网关设置成这台服务器的IP即可

需要注意的事

现在这个版本无法自动下载 web面板 面板, 需要我们手动下载

web面板仓库: GitHub - MetaCubeX/metacubexd: Mihomo Dashboard, The Official One, XD

下载 web面板

wget https://github.com/MetaCubeX/metacubexd/archive/refs/heads/gh-pages.zip

解压文件到 /etc/mihomo 目录

unzip -d /etc/mihomo gh-pages.zip

重命名目录为 ui

mv /etc/mihomo/metacubexd-gh-pages /etc/mihomo/ui

通过 http://IP:9090/ui 访问 web 面板, 没有问题的话就能看到如下页面了, 密码是 123456

image.png

0

评论 (0)

取消