135 lines
3.7 KiB
Bash
135 lines
3.7 KiB
Bash
#!/bin/sh /etc/rc.common
|
|
|
|
START=90
|
|
STOP=15
|
|
USE_PROCD=1
|
|
NAME=xray
|
|
|
|
source /usr/share/xray/init.firewall
|
|
|
|
uci_get_by_type() {
|
|
local ret=$(uci get ${NAME}.@$1[0].$2 2> /dev/null)
|
|
echo ${ret:=$3}
|
|
}
|
|
|
|
log_procd_set_param() {
|
|
local type="$1"
|
|
shift
|
|
logger -st xray[$$] -p4 "Using procd_set_param $type" "$@"
|
|
}
|
|
|
|
start_xray() {
|
|
logger -st xray[$$] -p4 "Starting Xray from $1"
|
|
procd_open_instance
|
|
procd_set_param respawn 1 1 0
|
|
procd_set_param command $1
|
|
procd_append_param command run
|
|
procd_append_param command -confdir
|
|
procd_append_param command /var/etc/xray
|
|
|
|
local rlimit_nofile
|
|
if [ -s /usr/share/xray/rlimit_nofile ] ; then
|
|
rlimit_nofile="nofile=""$(cat /usr/share/xray/rlimit_nofile)"
|
|
fi
|
|
|
|
local rlimit_data
|
|
if [ -s /usr/share/xray/rlimit_data ] ; then
|
|
rlimit_data="data=""$(cat /usr/share/xray/rlimit_data)"
|
|
fi
|
|
|
|
# this param passing method is just so fucking weird
|
|
if [ -z "${rlimit_nofile}" ] ; then
|
|
if [ ! -z "${rlimit_data}" ]; then
|
|
log_procd_set_param limits "${rlimit_data}"
|
|
procd_set_param limits "${rlimit_data}"
|
|
fi
|
|
else
|
|
if [ -z "${rlimit_data}" ]; then
|
|
log_procd_set_param limits "${rlimit_nofile}"
|
|
procd_set_param limits "${rlimit_nofile}"
|
|
else
|
|
log_procd_set_param limits "${rlimit_data}" "${rlimit_nofile}"
|
|
procd_set_param limits "${rlimit_data}" "${rlimit_nofile}"
|
|
fi
|
|
fi
|
|
|
|
procd_set_param env XRAY_LOCATION_ASSET=/usr/share/xray
|
|
procd_set_param stdout 1
|
|
procd_set_param stderr 1
|
|
procd_set_param file /etc/config/xray
|
|
procd_set_param pidfile /var/run/xray.pid
|
|
procd_close_instance
|
|
}
|
|
|
|
gen_config_file() {
|
|
rm -f /var/etc/xray/*
|
|
if [ -s /usr/share/xray/infinite_retry ] ; then
|
|
while [ ! -s /var/etc/xray/config.json ] ; do
|
|
logger -st xray[$$] -p4 "(Re)generating Xray configuration files..."
|
|
impl_gen_config_file
|
|
done
|
|
else
|
|
logger -st xray[$$] -p4 "Generating Xray configuration files..."
|
|
impl_gen_config_file
|
|
fi
|
|
local custom_config=$(uci_get_by_type general custom_config)
|
|
[ ! "${#custom_config}" == "0" ] && echo ${custom_config} > /var/etc/xray/config_custom.json
|
|
}
|
|
|
|
setup_dnsmasq() {
|
|
local dns_port=$(uci_get_by_type general dns_port)
|
|
local dns_count=$(uci_get_by_type general dns_count 0)
|
|
|
|
echo "# Generated dnsmasq configurations by luci-app-xray" > /tmp/dnsmasq.d/xray.conf
|
|
echo "strict-order" >> /tmp/dnsmasq.d/xray.conf
|
|
echo "server=/#/127.0.0.1#${dns_port}" >> /tmp/dnsmasq.d/xray.conf
|
|
local cur_port
|
|
for cur_port in $(seq ${dns_port} $(expr ${dns_port} + ${dns_count})); do
|
|
echo "server=127.0.0.1#${cur_port}" >> /tmp/dnsmasq.d/xray.conf
|
|
done
|
|
|
|
logger -st xray[$$] -p4 $(cat /tmp/dnsmasq.d/xray.conf)
|
|
/etc/init.d/dnsmasq restart > /dev/null 2>&1
|
|
}
|
|
|
|
flush_dnsmasq() {
|
|
rm -f /tmp/dnsmasq.d/xray.conf
|
|
/etc/init.d/dnsmasq restart > /dev/null 2>&1
|
|
}
|
|
|
|
create_when_enable() {
|
|
[ "$(uci_get_by_type general transparent_proxy_enable)" == "1" ] || return 0
|
|
logger -st xray[$$] -p4 "Setting dnsmasq and firewall for transparent proxy..."
|
|
setup_dnsmasq
|
|
setup_firewall
|
|
}
|
|
|
|
flush_when_disable() {
|
|
logger -st xray[$$] -p4 "Resetting dnsmasq and firewall configurations..."
|
|
flush_dnsmasq
|
|
flush_firewall
|
|
}
|
|
|
|
start_service() {
|
|
config_load $NAME
|
|
mkdir -p /var/run /var/etc/xray
|
|
local xray_bin=$(uci_get_by_type general xray_bin)
|
|
command -v ${xray_bin} > /dev/null 2>&1 || return 1
|
|
gen_config_file
|
|
start_xray ${xray_bin}
|
|
create_when_enable || flush_when_disable
|
|
}
|
|
|
|
stop_service() {
|
|
flush_when_disable
|
|
}
|
|
|
|
reload_service() {
|
|
stop
|
|
start
|
|
}
|
|
|
|
service_triggers() {
|
|
procd_add_reload_trigger "xray"
|
|
}
|