#!/bin/sh
. /etc/rc.subr # Include the rc.d script framework, the standard prerequisite for FreeBSD service scripts
name="mihomo" # Define the service name, used to identify and manage this service
desc="mihomo server" # Service description
rcvar="mihomo_enable" # Service toggle variable, controls whether the service starts automatically at boot
: ${mihomo_datadir:="/var/run/mihomo"}
: ${mihomo_user:="root"} # Default user; if using another user, ensure the mihomo_datadir directory and $pidfile and log files are writable
: ${mihomo_extra_flags:=""} # Extra parameters for mihomo, used to pass custom startup options
procname="/usr/local/bin/mihomo" # Used with pidfile to detect the service process
pidfile="${mihomo_datadir}/mihomo.pid" # Stores the Process ID (PID) of the main process, used with procname to detect the service process
logfile="${mihomo_datadir}/mihomo.log"
start_cmd="mihomo_start" # Set the start command to call the mihomo_start function; stop and other commands are implemented by default by the rc.d framework
extra_commands="init reconfig regeoip" # Register additional custom commands, extending the rc.d framework's standard command set
reconfig_cmd="mihomo_reconfig" # Specify the reconfig command to call the mihomo_reconfig function, used to download the config.yaml file
regeoip_cmd="mihomo_regeoip" # Specify the regeoip command to call the mihomo_regeoip function, used to download the geoip.dat file; can specify using this file via mihomo_extra_flags="-m"
init_cmd="mihomo_init" # Specify the init command to call the mihomo_init function. Creates the data file directory, sets ownership, to avoid read/write permission issues when running as a non-root user
mihomo_start()
{ # Use daemon to start mihomo; the -p parameter makes daemon automatically manage the pidfile, cleaning it up when the process exits
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 # On successful download, overwrite the original config; on failure, keep the original config
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"