OPNsense配置clash(mihomo)透明代理教程

Clashmihomo)是一款功能强大的跨平台代理工具,主要用于管理代理规则和流量分流。支持多种代理协议(如 HTTP(S)、SOCKS5、Shadowsocks、VMess等)。提供灵活的规则分流功能,可根据域名、IP和GeoIP分流流量。支持YAML配置文件,易于配置和扩展。

MosDNS是一款高性能的DNS解析工具,专注于解决DNS劫持、分流和缓存问题。支持多上游DNS查询,按规则分流。提供DNS-over-HTTPS (DoH)、DNS-over-TLS (DoT) 等加密查询支持。内置缓存功能,提高DNS解析效率。支持复杂的过滤和规则匹配,用于屏蔽广告或定向解析。

Clash(mihomo)官方版本在freebsd平台上tun功能不可用。近期 Vincent-Loeng 大佬对mihomo代码进行了更新,让freebsd平台的mihomo有了tun功能。现在只需mihomo一个软件就可以在OPNsense上实现透明代理。同时配合使用MosDNS进行DNS精准分流,可以组成一个完整的代理与流量管理解决方案。

下面介绍在OPNsense上配置透明代理的方法。

测试环境:OPNsense 25.1.7。

前提条件

  • 测试有效的Clash配置文件
  • 防火墙基础网络连通性正常
  • 已启用 SSH 并完成系统备份

安装方法

为简化安装过程,使用一键脚本进行安装,可以轻松完成透明代理搭建过程。脚本将执行配置操作:

  • 添加系统菜单。
  • 添加系统服务项。
  • 添加开机启动项。
  • 添加升级脚本和订阅功能。
  • 添加tun接口,修改Unbound DNS端口。

插件安装以后在系统重置或升级时功将继续保留。

访问Clash for OPNsense项目,下载安装包并解压缩,使用finalShell软件将文件上传至pfSense的/root目录,进入安装目录,运行命令安装:

sh install.sh

安装过程如下图所示:

代理配置

1.修改配置

进入防火墙Web设置界面,导航到VPN>Proxy Suite 菜单,修改clash( proxies和rules部分) 内容并保存。根据日志信息排查配置错误。

2.添加接口

转到接口>分配,将tun_3000虚拟网卡添加为接口并启用,无需输入IPv4地址和网关。

3.DNS设置

为避免与clash使用的53端口冲突,将 Unbound DNS 端口修改为5355端口,并作为mosdns的默认上游DNS。

4.路由设置

clash配置文件中”auto-route”选项设置为true,开启自动路由。如下图所示:

tun:
  mtu: 9000
  enable: true
  stack: system
  device: tun_3000
  auto-route: true
  strict-route: true
  auto-detect-interface: true
  dns-hijack:
    - any:53
    - tcp://any:53

5.添加规则

在tun接口添加一条防火墙规则,允许tun子网互相访问。

6.检查测试

设置完成,客户端检查是否能正常进行透明代理,访问 ip111.cn,查看分流是否正常。

访问ip.skk.moe,检查是否可以正常访问国内外网站。

7.仪表盘

默认配置文件开启了clash api功能,可以访问http://lan_ip:9090/ui登录clash仪表盘查看代理连接信息。

8.订阅更新

安装脚本已集成clash订阅在线转换程序,除了在Web端手动更新外,也可以设置定时任务自动更新。转到系统>设置>任务,添加”Renew Clash Subsribe”任务项。如下图所示,表示每周六的凌晨3点自动更新订阅。

9.程序更新

安装包集成了mosdns所使用GeoIP数据和clash程序更新脚本,使用clash的socks5(7891端口)代理进行更新。添加”Proxy and GeoIP data updates”任务项。如下图所示,表示每周六的1点自动开始更新。

注意事项

  1. 卸载插件,运行sh uninstall.sh
  2. 如果无法正常分流或访问外网,请通过clash运行日志进行分析排查。
  3. 默认安装配置已针透明代理进行了优化,用户仅需修改clash的proxy和rule部分配置即可实现透明代理。

附件下载

相关文章:

评论

  1. 月亮之城
    11 月前
    2025-1-13 12:25:15

    最近也在弄opnsense 在家里搞着玩 一直在看你的教程

    • Avatar photo
      博主
      月亮之城
      11 月前
      2025-1-14 8:26:29

      希望对你有所帮助,也欢迎提出意见!

  2. Jim
    11 月前
    2025-1-18 21:01:05

    Netbird出了opnsense插件,可以出个使用教程
    https://github.com/netbirdio/netbird/issues/2200

  3. zjahon
    10 月前
    2025-2-07 8:43:53

    之前按博主另外一个教程装了后台带订阅版clash,怎么把他清除干净呢?

    • Avatar photo
      博主
      zjahon
      10 月前
      2025-2-07 9:29:23

      删除安装目录即可,其他的会自动覆盖。

  4. inkflaw
    9 月前
    2025-2-28 19:01:02

    我给 heiher/hev-socks5-tunnel 的流程在 FreeBSD 上跑通了,但是在 OpnSense 上还不行
    看起来它的性能比 tun2socks 好很多,大佬可以帮忙看看嘛|´・ω・)ノ
    https://github.com/heiher/hev-socks5-tunnel/issues/210#issuecomment-2690314268

    • Avatar photo
      博主
      inkflaw
      已编辑
      9 月前
      2025-3-02 12:30:38

      在freebsd上用gmake命令编译了程序,分别在pfSense、OPNsense上进行了测试。在pfSense上运行以后,可以在接口>分配处发现tun3000虚拟网卡的,但是与clash配合,分流测试不成功。在OPNsense上运行以后,不能在接口>分配处找到tun3000虚拟网卡,更改虚拟网卡名称并添加默认路由后,进行分流测试也不成功。只能等作者完善了。

      • inkflaw
        鉄血男兒
        已编辑
        9 月前
        2025-3-12 20:58:46

        已经跟开发者一起修好了这个 bug 了
        https://github.com/heiher/hev-socks5-tunnel/issues/210
        附上我的配置文件和 init

        tunnel:
          # Interface name
          name: tun_3000
          # Interface MTU
          mtu: 9000
          # Multi-queue, FreeBSD no support
          multi-queue: false
          # IPv4 address
          ipv4: 198.18.0.1
          # IPv6 address
          ipv6: 'fc00::1'
          # Post up script
        # post-up-script: up.sh
          # Pre down script
        # pre-down-script: down.sh
        
        socks5:
          # Socks5 server port
          port: 7891
          # Socks5 server address (ipv4/ipv6)
          address: 127.0.0.1
          # Socks5 UDP relay mode (tcp|udp)
          udp: 'udp'
          # Socks5 handshake using pipeline mode
        # pipeline: false
          # Socks5 server username
        # username: 'username'
          # Socks5 server password
        # password: 'password'
          # Socket mark
        # mark: 0
        
        #misc:
           # task stack size (bytes)
        #  task-stack-size: 86016
        #  task-stack-size: 36864 # 20480 + tcp-buffer-size
           # tcp buffer size (bytes)
        #  tcp-buffer-size: 65536
        #  tcp-buffer-size: 16384 # 16*1024k
           # connect timeout (ms)
        #  connect-timeout: 5000
           # read-write timeout (ms)
        #  read-write-timeout: 60000
           # stdout, stderr or file-path
        #  log-file: stderr
           # debug, info, warn or error
        #  log-level: warn
           # If present, run as a daemon with this pid file
        #  pid-file: /run/hev-socks5-tunnel.pid
           # If present, set rlimit nofile; else use default value
        #  limit-nofile: 65535
        #!/bin/sh
        
        # PROVIDE: hevsocks5tunnel
        # REQUIRE: NETWORKING
        # KEYWORD: shutdown
        
        . /etc/rc.subr
        
        name="hevsocks5tunnel"
        rcvar="hevsocks5tunnel_enable"
        pidfile="/var/run/${name}.pid"
        logfile="/var/log/${name}.log"
        command="/usr/local/bin/hev-socks5-tunnel"
        command_args="/usr/local/etc/hevsocks5tunnel/config.yaml"
        
        start_cmd="${name}_start"
        stop_cmd="${name}_stop"
        restart_cmd="${name}_restart"
        status_cmd="${name}_status"
        
        
        hevsocks5tunnel_start() {
            if [ -f "$pidfile" ] && kill -0 $(cat "$pidfile") 2>/dev/null; then
                echo "${name} is already running." | tee -a "$logfile"
                return 1
            fi
            ifconfig tun_3000 destroy || true
            ifconfig tun3000 destroy || true
            ifconfig tun3000 create name tun_3000
            #ifconfig hevsocks5 destroy || true
            echo "$(date '+%Y-%m-%d %H:%M:%S') - Starting ${name}..." | tee -a "$logfile"
            nohup ${command} ${command_args} >> "$logfile" 2>&1 &
            echo $! > "$pidfile"
            echo "$(date '+%Y-%m-%d %H:%M:%S') - ${name} started successfully." | tee -a "$logfile"
        }
        
        hevsocks5tunnel_stop() {
            if [ ! -f "$pidfile" ] || ! kill -0 $(cat "$pidfile") 2>/dev/null; then
                echo "${name} is not running." | tee -a "$logfile"
                return 1
            fi
            echo "$(date '+%Y-%m-%d %H:%M:%S') - Stopping ${name}..." | tee -a "$logfile"
            kill $(cat "$pidfile") && rm -f "$pidfile"
            echo "$(date '+%Y-%m-%d %H:%M:%S') - ${name} has stopped." | tee -a "$logfile"
            ifconfig tun_3000 destroy || true
            ifconfig tun3000 destroy || true
        }
        
        hevsocks5tunnel_restart() {
            echo "$(date '+%Y-%m-%d %H:%M:%S') - Restarting ${name}..." | tee -a "$logfile"
            hevsocks5tunnel_stop
            sleep 1
            hevsocks5tunnel_start
        }
        
        hevsocks5tunnel_status() {
            if [ -f "$pidfile" ] && kill -0 $(cat "$pidfile") 2>/dev/null; then
                echo "hevsocks5tunnel is running (PID: $(cat "$pidfile"))."
            else
                echo "hevsocks5tunnel is not running."
            fi
        }
        load_rc_config $name
        run_rc_command "$1"
        • Avatar photo
          博主
          inkflaw
          已编辑
          9 月前
          2025-3-12 23:00:08

          配合clash测试成功。安装配置教程:https://pfchina.org/?p=14551

  5. Avatar photo
    博主
    已编辑
    7 月前
    2025-5-01 11:39:00

    以后发布的代理全家桶使用hev-socks-tunnel代替tun2socks。

  6. 白色流星
    7 月前
    2025-5-04 11:32:11

    請問如何完全卸載呢?

    • Avatar photo
      博主
      白色流星
      已编辑
      7 月前
      2025-5-04 16:27:59

      已更新,添加了卸载脚本。只需运行sh uninstall.sh即可。

      • 白色流星
        鉄血男兒
        7 月前
        2025-5-06 14:17:09

        多謝

  7. imsw93
    4 月前
    2025-7-29 11:04:05

    访问控制台IP:9090/ui提示404,查询/usr/local/etc/clash文件夹中只有sub文件夹,没有ui文件夹,这是啥情况?

    • Avatar photo
      博主
      imsw93
      已编辑
      4 月前
      2025-7-29 14:19:54

      文件漏放了,后续更新的版本已经添加进去了。

  8. imsw93
    4 月前
    2025-8-08 17:08:48

    能否让部分域名不解析到fake-ip?我服务器都是用域名访问,现在全解析过去导致服务器也没法访问了

    • Avatar photo
      博主
      imsw93
      4 月前
      2025-8-08 21:49:52

      可以在clash的配置文件当中设置,将不用解析的域名或IP进行排除,具体请参考mihomo配置方法。

  9. Solomon
    已编辑
    2 月前
    2025-10-17 15:55:06

    你好,目前外网和内网都可以正常分流,但是ipv6访问异常了,单独使用外网的ipv6地址是能ping通,ping域名则提示如下问题,是不是dns这块有问题?
    ping: http://www.baidu.com: Address family for hostname not supported

    • Avatar photo
      博主
      Solomon
      2 月前
      2025-10-18 8:57:12

      建议关掉ipv6,除非你自己有解决问题的能力。

  10. gordon
    5 天前
    2025-12-04 16:17:08

    能不能只做tun接口,目前防火墙策略和dns我是自己维护的,不想弄太多外来配置污染我的规则

    • Avatar photo
      博主
      gordon
      5 天前
      2025-12-04 16:42:21

      这是目前经测试可以完美跑起来的方法,你也可以自己尝试一下其他方法,比如不使用mosdns,使用系统DNS等。

    • Vincent
      gordon
      5 天前
      2025-12-04 23:31:02

      关闭自动路由便不会修改路由规则和DNS

      • Avatar photo
        博主
        Vincent
        4 天前
        2025-12-05 9:39:20

        啊!大佬来亲自回复了。

      • gordon
        Vincent
        4 天前
        2025-12-05 10:45:10

        那可以,我研究一下

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇