mirror of
https://github.com/kiddin9/op-packages.git
synced 2026-08-01 23:19:33 +08:00
112 lines
3.3 KiB
Bash
Executable File
112 lines
3.3 KiB
Bash
Executable File
#!/bin/sh /etc/rc.common
|
|
|
|
USE_PROCD=1
|
|
START=98
|
|
script_path="/usr/share/natter2/natter.py"
|
|
|
|
start_service() {
|
|
local basic_list="enable tmp_path"
|
|
local instance_list="enable_instance id remark protocol enable_stun_server \
|
|
stun_server enable_keepalive_server keepalive_server \
|
|
interval enable_bonding bonding_interface bonding_port \
|
|
enable_forwarding forwarding_method target_address target_port \
|
|
enable_forwarding_retry enable_quit delay log_level enable_notify \
|
|
notify_path enable_upnp_service"
|
|
|
|
for i in $basic_list; do
|
|
local eval $i="$(uci_get_by_type base 0 $i)"
|
|
done ; unset i
|
|
|
|
if [ "$enable" == 1 ]; then
|
|
mkdir -p ${tmp_path}
|
|
mkdir -p /var/etc/natter2
|
|
|
|
for u in $(seq 0 $(($(uci show natter2 2> /dev/null | grep -E '@instances\[[0-9]\]+=instances' | wc -l) - 1)))
|
|
do
|
|
for i in $instance_list; do
|
|
local eval $i="$(uci_get_by_type instances $u $i)"
|
|
done ; unset i
|
|
|
|
[ "$enable_instance" != 1 ] && continue
|
|
script_command=""
|
|
|
|
[ "$protocol" == udp ] && script_command="$script_command -u"
|
|
|
|
if [ "$enable_stun_server" == 1 ]; then
|
|
for i in $stun_server; do
|
|
script_command="$script_command -s $i"
|
|
done ; unset i
|
|
fi
|
|
|
|
[ "$enable_keepalive_server" == 1 ] && script_command="$script_command -h $keepalive_server"
|
|
[ "$interval" ] && script_command="$script_command -k $interval"
|
|
[ "$enable_upnp_service" == 1 ] && script_command="$script_command -U"
|
|
|
|
if [ "$enable_bonding" == 1 ]; then
|
|
[ "$bonding_interface" ] && script_command="$script_command -i $bonding_interface"
|
|
[ "$bonding_port" ] && script_command="$script_command -b $bonding_port"
|
|
fi
|
|
|
|
if [ "$enable_forwarding" == 1 ]; then
|
|
[ "$forwarding_method" ] && script_command="$script_command -m $forwarding_method"
|
|
[ "$target_address" ] && script_command="$script_command -t $target_address"
|
|
[ "$target_port" ] && script_command="$script_command -p $target_port"
|
|
[ "$enable_forwarding_retry" == 1 ] && script_command="$script_command -r"
|
|
fi
|
|
|
|
[ "$enable_quit" == 1 ] && script_command="$script_command -q"
|
|
[ "$log_level" == "verbose" ] && script_command="$script_command -v"
|
|
|
|
if [ "$enable_notify" == 1 ]; then
|
|
echo "$notify_path" > /var/etc/natter2/${id}-notify
|
|
var_file=/var/etc/natter2/${id}-${u}-1
|
|
else
|
|
var_file=/var/etc/natter2/${id}-${u}-0
|
|
fi
|
|
|
|
cp -a /usr/share/luci-app-natter2/notify-base.sh $var_file
|
|
chmod +x $var_file
|
|
|
|
procd_open_instance "natter2_${id}"
|
|
procd_set_param command $(command -v python) "$script_path" $script_command -e $var_file
|
|
|
|
procd_set_param stdout 1
|
|
procd_set_param stderr 1
|
|
|
|
[ -n "$delay" ] && procd_set_param delay "$delay"
|
|
procd_set_param respawn
|
|
|
|
procd_close_instance
|
|
|
|
for i in $instance_list; do
|
|
unset $(echo $i)
|
|
done ; unset i
|
|
unset script_command
|
|
done ; unset u
|
|
else
|
|
stop_service
|
|
fi
|
|
}
|
|
|
|
stop_service() {
|
|
rm -rf "/var/etc/natter2" 2> /dev/null
|
|
for u in $(seq 0 $(($(uci show natter2 2> /dev/null | grep -E '@instances\[[0-9]\]+=instances' | wc -l) - 1)))
|
|
do
|
|
uci set natter2.@instances[$u].tmp_public_port=""
|
|
uci commit natter2
|
|
done
|
|
rm -f /tmp/natter2_nat_type
|
|
}
|
|
|
|
service_triggers() {
|
|
procd_add_reload_trigger "natter2"
|
|
}
|
|
|
|
uci_get_by_type() {
|
|
local ret=$(uci get natter2.@$1[$2].$3 2>/dev/null)
|
|
echo ${ret:=$4}
|
|
}
|
|
reload_service() {
|
|
restart
|
|
}
|