一个基于 V2ray 服务的 Sing-box FreeBSD rc 服务

很早以前就把这个配置文件捣鼓出来了,但是由于拖延症现在才放到这里

#
# v2ray_enable (bool):          Set to "NO" by default.
#                               Set it to "YES" to enable v2ray
# v2ray_config (path):          Set to "/usr/local/etc/v2ray/config.json" by default
#                               Set it to the v2ray server config
# v2ray_logdir (path):          Set to "/var/log/v2ray" by default.
#                               Set it to the directory of v2ray log files
# v2ray_env (str):              Set to "" by default.
#                               Set it to the desired environment variables

. /etc/rc.subr

name="sing_box"
rcvar="${name}_enable"

: ${sing_box_enable="NO"}
: ${sing_box_config="/usr/local/etc/sing-box/config.json"}
: ${sing_box_logdir="/var/log/sing-box"}
: ${sing_box_env=""}
: ${sing_box_user="v2ray"}
: ${sing_box_group="v2ray"}

pidfile="/var/run/$name.pid"
procname="/usr/local/bin/sing-box"
command="/usr/sbin/daemon"
command_args="-c -p ${pidfile} /usr/bin/env ${sing_box_env} ${procname} run --config ${sing_box_config}"
required_files="${sing_box_config}"

start_precmd="sing_box_startprecmd"

sing_box_startprecmd() {
        touch "${pidfile}"
        chown ${sing_box_user}:${sing_box_group} "${pidfile}"
        mkdir -p "${sing_box_logdir}"
        chown -R ${sing_box_user}:${sing_box_group} "${sing_box_logdir}"
}

load_rc_config "$name"
run_rc_command "$1"

由于懒得做新用户创建的工作,所以直接使用的是 v2ray 生成的用户;实际使用过程中需要先安装 v2ray 创建用户,或者改一下 sing_box_user 和 sing_box_group 这两项

自启动需要修改 sing_box_enable 这一项为 YES