10.3 配置 Mihomo(Clash)

mihomo 已在 ports 中提供。

安装 mihomo

  • 使用 pkg 安装:

# pkg install net/mihomo
  • 或者使用 Ports 安装:

# cd /usr/ports/net/mihomo/ 
# make install clean

如果觉得配置较为麻烦,也可以在 Linux 兼容层中使用,FreeBSD 的网络同样可以受其控制。

RC 脚本

已向 Ports 维护者发出请求合并(Bug 291295 - net/mihomo: Add rc.conf and some Post-installation),但目前尚未收到回应。

RC 脚本

将以下脚本保存为名为 mihomo 的文本文件,并放置到路径 /usr/local/etc/rc.d/ 下。最后使用 root 账户为该文件赋予可执行权限:chmod +x /usr/local/etc/rc.d/mihomo

#!/bin/sh

. /etc/rc.subr   # 引入 rc.d 脚本框架

name="mihomo"    # 定义服务名
desc="mihomo server"    # 服务描述
rcvar="mihomo_enable"    # 服务开关变量

: ${mihomo_datadir:="/var/run/mihomo"}
: ${mihomo_user:="root"}    # 默认用户;如使用其他用户,请确保 /etc/mihomo 目录及 $pidfile 和 log 文件可写
: ${mihomo_extra_flags:=""}	# mihomo 的额外参数

procname="/usr/local/bin/mihomo"    # 与 pidfile 配合,用于检测服务进程
pidfile="${mihomo_datadir}/mihomo.pid"    # 用于检测服务进程
logfile="${mihomo_datadir}/mihomo.log"
start_cmd="mihomo_start"    # 设置 start 命令调用 mihomo_start 函数,stop 等命令由 rc.d 框架默认实现
extra_commands="init reconfig regeoip"    # 设置其他的自定义命令
reconfig_cmd="mihomo_reconfig"    # 指定 reconfig 命令调用 mihomo_reconfig 函数,用于下载 config.yaml 文件
regeoip_cmd="mihomo_regeoip"    # 指定 regeoip 命令调用 mihomo_regeoip 函数,用于下载 geoip.dat 文件,可通过 mihomo_extra_flags="-m" 指定使用该文件
init_cmd="mihomo_init"    # 指定 init 命令调用 mihomo_init 函数。创建数据文件目录,指定属主,避免普通用户身份执行时的读写权限问题

mihomo_start()
{    # 使用 daemon 启动 mihomo,指定 -p 参数使用 pidfile,使 mihomo 成为 daemon 的子进程,由 daemon 自动管理 pidfile,从而在 mihomo 进程退出时自动清理 pidfile
	daemon -u ${mihomo_user} -p "$pidfile" -o "${logfile}" $procname -d "${mihomo_datadir}" -f "${mihomo_datadir}/config.yaml" ${mihomo_extra_flags}
}
mihomo_reconfig()
{
	startmsg "begin to refresh config.yaml"
	startmsg "config.yaml : ${mihomo_config}"
	if ( fetch -o ${mihomo_datadir}/config.yaml.new "${mihomo_config}" );then
		mv ${mihomo_datadir}/config.yaml.new ${mihomo_datadir}/config.yaml    # 下载成功将覆盖原有配置,下载失败保留原有配置   
        startmsg "rename config.yaml.new to config.yaml"
	else
		err "fetch config.yaml failed! check $$mihomo_config!"
	fi
}
mihomo_regeoip()
{
	startmsg "begin to refresh geoip.dat"
	startmsg "geoip.dat : $mihomo_geoip"
	if ( fetch -o ${mihomo_datadir}/geoip.new "${mihomo_geoip}" );then
		mv ${mihomo_datadir}/geoip.new ${mihomo_datadir}/geoip.dat
        startmsg "rename geoip.new to geoip.dat"
	else
		err "fetch geoip.dat failed! check $$mihomo_geoip"
	fi
}
mihomo_init()
{
	startmsg "begin init"
	install -d -m 0700 -o ${mihomo_user} ${mihomo_datadir}
    startmsg "all data is in ${mihomo_datadir}"
    startmsg "remember reconfig/regeoip before start"
}

load_rc_config $name
run_rc_command "$1"

可用参数及选项

以下是一些命令行参数,这些命令会直接将配置写入 /etc/rc.conf,如配置有误,可直接修改对应行。

  • 启用 mihomo 服务项及开机自启

  • 立刻启用 mihomo

  • 立刻停用 mihomo

  • 查看 mihomo 状态

  • 在此指定你的订阅链接地址(示例地址仅用于演示,需自行替换为有效链接)

  • 用于地理位置判断的 GeoIP 数据,主要用于根据 IP 地址的地理归属进行流量分流或规则匹配。

  • 指定其他 mihomo 参数

  • -m:指定 mihomo 的 geoip.dat 文件路径,具体说明参见 rc 脚本。

  • 指定调用 mihomo 的用户

  • 指定 mihomo 的数据路径

  • 初始化(创建)mihomo 的数据目录

  • 更新订阅。start 前先更新即可使用最新的订阅信息

  • 更新 regeoip

最小 RC 示例

以下是一个最简配置示例,可在理解其含义并按需修改后写入 /etc/rc.conf 文件:

读者可根据自身需求自行调整配置。

未竟事项

  • 如何实现“直连”、“代理”、“全局”的分流?

  • 如何实现 TUN 虚拟网卡代理(是否可能)?

  • 如何根据订阅链接进行测速?

  • 如何指定订阅链接中的代理组中的某个代理?(比如仅使用位于美国的某个代理 A)

Clash for FreeBSD

警告

目前该项目尚不支持某些订阅类型。

会报错:

如果读者遇到类似问题,建议使用其他解决方案。

下载项目

克隆 Clash for FreeBSD 仓库到本地:

启动程序

直接运行脚本文件 start.sh 即可启动程序。

  • 进入项目目录

  • 编辑 .env 文件,修改变量 CLASH_URL 的值(即把你的 Clash 订阅链接放进去)。

注意:

.env 文件中的变量 CLASH_SECRET 为自定义 Clash 的 Secret,当值为空时,脚本将自动生成随机字符串。

  • 安装 bash,并将其设置为 root 用户及当前登录用户的 shell,具体步骤请参见相关文档。

  • 运行启动脚本

  • 加载环境变量并启用系统代理

  • 列出本地监听的端口中包含 9090 或 789 的套接字:

  • 查看环境中设置的 HTTP 和 HTTPS 代理变量:

如果以上步骤的输出结果类似,且能够正常访问 https://Google.com,则说明 Clash 程序已成功启动。

重启程序

如果需要对 Clash 配置进行修改,请修改 conf/config.yaml 文件。然后运行 restart.sh 脚本进行重启。

注意:

重启脚本 restart.sh 不会更新订阅信息。

访问 Web 端

访问 http://127.0.0.1:9090/ui 在网页中输入上面输出的 Secret 值即可。

停止程序

  • 进入项目目录

  • 关闭服务

随后检查程序端口、进程以及环境变量 http_proxy|https_proxy,若均不存在,则说明服务已正常关闭。

参考资料

wenyinos/clash-for-freebsd

未竟事宜

  • 需要更新支持所有类型的订阅链接。

  • 与 bash 解耦合,支持默认的 sh。

最后更新于

这有帮助吗?