mirror of
https://github.com/Ysurac/openmptcprouter-feeds.git
synced 2025-03-09 15:40:03 +00:00
163 lines
3.9 KiB
Bash
Executable file
163 lines
3.9 KiB
Bash
Executable file
#!/bin/sh /etc/rc.common
|
|
|
|
START=90
|
|
|
|
USE_PROCD=1
|
|
|
|
PROG=/usr/bin/zerotier-one
|
|
CONFIG_PATH=/var/lib/zerotier-one
|
|
|
|
section_enabled() {
|
|
config_get_bool enabled "$1" 'enabled' 0
|
|
[ $enabled -gt 0 ]
|
|
}
|
|
service_triggers() {
|
|
procd_add_reload_trigger zerotier
|
|
}
|
|
start_instance() {
|
|
local cfg="$1"
|
|
local port secret config_path
|
|
local ARGS=""
|
|
|
|
if ! section_enabled "$cfg"; then
|
|
echo "disabled in config"
|
|
return 1
|
|
fi
|
|
|
|
[ -d /etc/config/zero ] || mkdir -p /etc/config/zero
|
|
config_path=/etc/config/zero
|
|
|
|
config_get_bool port $cfg 'port'
|
|
config_get secret $cfg 'secret'
|
|
|
|
# Remove existing link or folder
|
|
rm -rf $CONFIG_PATH
|
|
|
|
# Create link from CONFIG_PATH to config_path
|
|
if [ -n "$config_path" -a "$config_path" != $CONFIG_PATH ]; then
|
|
if [ ! -d "$config_path" ]; then
|
|
echo "ZeroTier config_path does not exist: $config_path"
|
|
return
|
|
fi
|
|
|
|
ln -s $config_path $CONFIG_PATH
|
|
fi
|
|
|
|
mkdir -p $CONFIG_PATH/networks.d
|
|
|
|
if [ -n "$port" ]; then
|
|
ARGS="$ARGS -p$port"
|
|
fi
|
|
|
|
if [ "$secret" = "generate" ]; then
|
|
echo "Generate secret - please wait..."
|
|
local sf="/tmp/zt.$cfg.secret"
|
|
|
|
zerotier-idtool generate "$sf" > /dev/null
|
|
[ $? -ne 0 ] && return 1
|
|
|
|
secret="$(cat $sf)"
|
|
rm "$sf"
|
|
|
|
uci set zerotier.$cfg.secret="$secret"
|
|
uci commit zerotier
|
|
fi
|
|
|
|
if [ -n "$secret" ]; then
|
|
echo "$secret" > $CONFIG_PATH/identity.secret
|
|
# make sure there is not previous identity.public
|
|
rm -f $CONFIG_PATH/identity.public
|
|
fi
|
|
|
|
add_join() {
|
|
# an (empty) config file will cause ZT to join a network
|
|
touch $CONFIG_PATH/networks.d/$1.conf
|
|
}
|
|
|
|
config_list_foreach $cfg 'join' add_join
|
|
|
|
procd_open_instance
|
|
procd_set_param command $PROG $ARGS $CONFIG_PATH
|
|
procd_set_param stderr 1
|
|
procd_close_instance
|
|
(
|
|
[ -f "/var/run/zerotier.wait.pid" ] && return
|
|
touch /var/run/zerotier.wait.pid
|
|
while [ "$(ifconfig | grep zt | awk '{print $1}')" = "" ]
|
|
do
|
|
sleep 1
|
|
done
|
|
zt0=$(ifconfig | grep zt | awk '{print $1}')
|
|
echo "zt interface $zt0 is started!"
|
|
if [ -z "$(uci get network.zerotier)" ]; then
|
|
uci set network.zerotier=interface
|
|
uci set network.zerotier.proto='static'
|
|
fi
|
|
config_get nat $cfg 'nat'
|
|
if [ "$nat" == "1" ]; then
|
|
if [ -z "$(uci get firewall.ztzone)" ]; then
|
|
uci set firewall.ztzone=zone
|
|
uci set firewall.ztzone.input='ACCEPT'
|
|
uci set firewall.ztzone.output='ACCEPT'
|
|
uci set firewall.ztzone.forward='REJECT'
|
|
uci set firewall.ztzone.masq='1'
|
|
uci set firewall.ztzone.name='zerotier'
|
|
uci set firewall.ztzone.network='zerotier'
|
|
fi
|
|
else
|
|
uci delete firewall.ztzone
|
|
fi
|
|
config_get access $cfg 'access'
|
|
if [ "${access//ztfwlan/}" != "$access" ]; then
|
|
uci set firewall.ztfwlan=forwarding
|
|
uci set firewall.ztfwlan.dest='lan'
|
|
uci set firewall.ztfwlan.src='zerotier'
|
|
else
|
|
uci delete firewall.ztfwlan
|
|
fi
|
|
if [ "${access//ztfwwan/}" != "$access" ]; then
|
|
uci set firewall.ztfwwan=forwarding
|
|
uci set firewall.ztfwwan.dest='wan'
|
|
uci set firewall.ztfwwan.src='zerotier'
|
|
else
|
|
uci delete firewall.ztfwwan
|
|
fi
|
|
if [ "${access//lanfwzt/}" != "$access" ]; then
|
|
uci set firewall.lanfwzt=forwarding
|
|
uci set firewall.lanfwzt.dest='zerotier'
|
|
uci set firewall.lanfwzt.src='lan'
|
|
else
|
|
uci delete firewall.lanfwzt
|
|
fi
|
|
if [ "${access//wanfwzt/}" != "$access" ]; then
|
|
uci set firewall.wanfwzt=forwarding
|
|
uci set firewall.wanfwzt.dest='zerotier'
|
|
uci set firewall.wanfwzt.src='wan'
|
|
else
|
|
uci delete firewall.wanfwzt
|
|
fi
|
|
uci set network.zerotier.ifname="$zt0"
|
|
[ -n "$(uci changes network)" ] && uci commit network && /etc/init.d/network reload
|
|
[ -n "$(uci changes firewall)" ] && uci commit firewall && /etc/init.d/firewall reload
|
|
rm /var/run/zerotier.wait.pid
|
|
) &
|
|
}
|
|
|
|
start_service() {
|
|
config_load 'zerotier'
|
|
config_foreach start_instance 'zerotier'
|
|
}
|
|
|
|
stop_instance() {
|
|
rm -f /tmp/zero.log
|
|
local cfg="$1"
|
|
|
|
# Remove existing link or folder
|
|
rm -rf $CONFIG_PATH
|
|
}
|
|
|
|
stop_service() {
|
|
config_load 'zerotier'
|
|
config_foreach stop_instance 'zerotier'
|
|
}
|
|
|