Sign inSign up

36bian/openclash

By 36bian

•Updated about 1 year ago

全局透明代理,带webUI,支持 x64/arm64

Image
2

2.2K

36bian/openclash repository overview

⁠简介

适用于linux_x86服务器作为旁路由的全局clash透明代理,集成了webui(:9090/ui),黑名单模式。
参考进阶选项1使用 clash网关模式还是clash旁路由模式,参考进阶选项2使用 自动列表辅助程序

内核版本:https://github.com/MetaCubeX/mihomo/releases/tag/v1.19.9⁠
web版本:https://github.com/MetaCubeX/metacubexd/releases/tag/v1.186.1⁠

⁠使用

  1. 设置macvlan(enp2s0是网卡名)

    1. 开启网卡混杂模式 ip link set dev enp2s0 promisc on(自己找教程修改成永久)
    2. 创建macvlan网络。docker network create -d macvlan --subnet=192.168.0.0/24 --gateway=192.168.0.1 -o parent=enp2s0 macvlan_lan(注意修改子网掩码、网关、网卡名为你的实际情况)
  2. 创建文件夹compose-openclash在里边执行vi docker-compose.yml,将下列内容粘贴进去

    docker-compose.yml

    services:
      openclash:
        image: 36bian/openclash:1.19.9
        container_name: clash # 容器名
        restart: unless-stopped # 推荐使用 unless-stopped 或 always
        cap_add:
          - NET_ADMIN
          - NET_RAW
        devices:
          - /dev/net/tun:/dev/net/tun
        volumes:
          # 将宿主机的 ./config 目录挂载到容器配置路径
          - ./config:/root/.config
        networks:
          clash_net: # 服务内部对此网络的引用名
            ipv4_address: 192.168.0.2 # 使用一个不被占用的ip,按实际网络修改
    
    networks:
      clash_net: # 定义要使用的网络
        name: macvlan_lan # 先前创建的网络
        external: true
    
  3. 执行docker compose up -d启动(执行docker logs -f clash可以跟踪容器日志)

  4. 修改./config/config.yaml设置你的上网密码和分流规则(最好不要替换该文件,因为上边一些端口号什么调好的,替换容易出问题)

  5. 执行docker compose restart或者进入ui后台ip地址:9090/ui重载配置文件(用局域网ip地址端口号登录)

  6. 进入路由器(比如0.1),找到dhcp分配那里,把网关和dns解析都改成旁路由地址(比如说0.2)

  7. 新的设备再次连接网络后,由于指向了0.2,会自动实现翻墙功能,由此实现局域网全设备透明代理

⁠注意事项
  1. 这套方案无法作用于宿主机(宿主机无法透明上网),直接连是连不通的,这是macvlan属性。 所以需要宿主机网关和dns解析继续连接路由器,不要从dhcp获取。
  2. ⚠️如果这套旁路由挂了或者无法上网,进路由器把dhcp的网关什么改回来,重启路由恢复大家上网
⁠进阶选项1-部署模式选择

clash网关模式:上述部署方案(路由器网关设置为clash机器)兼容性最好,不挑路由器,但风险在于clash机器挂了局域网所有设备会无法联网,需要路由器改回dhcp中网关选项,也就是上边感叹号内容

clash旁路由模式:为了解决这个问题,如果路由器支持(可能需要刷openwrt或者至少能ssh),可以做成dns智能分流方案(也就是路由器dhcp中网关保持不变,dns优先设置clash机器ip),这样clash机器挂了不影响国内域名访问,以下是具体方法,以openwrt为例

  1. 在最后一步设置路由器时,dhcp不要改网关,只需要第一dns设置成clash机器ip,第二dns选择一个随时可用的比如路由器ip。
    openwrt系统,位置网络-接口-lan编辑-dhcp服务器-高级设置-dhcp选项,填入6,192.168.0.2,192.168.1.1(根据你的实际情况修改,第一个clashdns,第二个备用dns)

  2. 路由器要开启fakeip地址转发,也就是198.18.0.0 255.254.0.0 clash机器ip,这样访问代理网站才会走clash所在机器
    openwrt系统

    1. 允许转发。位置,网络-防火墙-区域设置添加一条规则,从lan到lan口允许转发

    2. 将路由修改写入开机启动,位置系统-启动项-本地启动脚本

      # Put your custom commands here that should be executed once
      # the system init finished. By default this file does nothing.
      # fakeip路由到clash处理
      sleep 5
      ip rule add iif br-lan to 198.18.0.0/16 priority 10000 lookup 100
      ip route add default via 192.168.0.2 dev br-lan table 100
      exit 0
      
  3. 因为只解析dns,如果有需要翻墙的ip,则需要像第二条一样,手动在路由器中进行添加转发给clash机器。

  4. 设置ok时,客户端curl -v baidu.com返回真实ip和网站数据,curl -v google.com返回fakeip和网站数据。

⁠进阶选项2-自动列表辅助程序

安装了本项目的,推荐安装docker pull 36bian/openclashhelper:1.2,此程序可自动生成未被覆盖的被墙网站,详见36bian/openclashhelper项目介绍

⁠docker镜像构建脚本

Dockerfile

FROM alpine:3.21

WORKDIR /root

# 安装必要的包
RUN apk add --no-cache ca-certificates tzdata

# 创建存放默认配置的目录,并将默认配置文件复制进去
RUN mkdir -p /root/default_configs
COPY config.yaml /root/default_configs/config.yaml
# 下载地址 https://github.com/MetaCubeX/meta-rules-dat/releases/download/latest/country.mmdb
COPY country.mmdb /root/default_configs/country.mmdb
# 下载地址 https://github.com/MetaCubeX/meta-rules-dat/releases/download/latest/geosite.dat
COPY geosite.dat /root/default_configs/geosite.dat

# 复制 Mihomo 内核并赋予执行权限
COPY mihomo /root/mihomo
RUN chmod +x /root/mihomo

# 复制 Web UI 文件到 /root/ui
COPY dashboard_ui /root/ui

# 复制入口点脚本并赋予执行权限
COPY entrypoint.sh /root/entrypoint.sh
RUN chmod +x /root/entrypoint.sh

ENV TZ=Asia/Shanghai

# 容器启动时执行入口点脚本
ENTRYPOINT ["/root/entrypoint.sh"]
# 默认传递给入口点脚本的命令
CMD ["/root/mihomo", "-d", "/root/.config"]

entrypoint.sh。作用:挂载空目录时先反向复制一份,防止报错

#!/bin/sh
set -e

DEFAULT_CONFIG_DIR="/root/default_configs"
USER_CONFIG_DIR="/root/.config"  # 用户配置的挂载点

# 如果文件不存在,则从默认位置复制
if [ ! -f "${USER_CONFIG_DIR}/config.yaml" ]; then
  mkdir -p "${USER_CONFIG_DIR}"
  cp -a "${DEFAULT_CONFIG_DIR}/config.yaml" "${USER_CONFIG_DIR}/"
fi
if [ ! -f "${USER_CONFIG_DIR}/GeoSite.dat" ]; then
  mkdir -p "${USER_CONFIG_DIR}"
  cp -a "${DEFAULT_CONFIG_DIR}/GeoSite.dat" "${USER_CONFIG_DIR}/"
fi
if [ ! -f "${USER_CONFIG_DIR}/country.mmdb" ]; then
  mkdir -p "${USER_CONFIG_DIR}"
  cp -a "${DEFAULT_CONFIG_DIR}/country.mmdb" "${USER_CONFIG_DIR}/"
fi


echo "Starting Mihomo with configuration from ${USER_CONFIG_DIR}..."
# 执行传递给脚本的原始命令 (即 Dockerfile 中 CMD 的内容)
exec "$@"

Tag summary

Content type

Image

Digest

sha256:436d4090b…

Size

22 MB

Last updated

about 1 year ago

docker pull 36bian/openclash:1.19.9-arm64