2022-04-05 23:38:20 +00:00
|
|
|
#!/bin/bash
|
|
|
|
|
2022-07-15 16:03:20 +00:00
|
|
|
set -uo pipefail
|
|
|
|
|
|
|
|
trap 'trap " " SIGTERM; kill 0; wait' SIGTERM SIGQUIT SIGINT
|
|
|
|
|
|
|
|
echo "Starting Zerotier-One"
|
2022-04-05 23:38:20 +00:00
|
|
|
zerotier-one -d
|
|
|
|
|
2022-07-15 16:03:20 +00:00
|
|
|
echo "Wait for ZT service to come online before attempting queries..."
|
|
|
|
MAX_WAIT_SECS="${MAX_WAIT_SECS:-90}"
|
|
|
|
SLEEP_TIME="${SLEEP_TIME:-15}"
|
|
|
|
if [[ "$SLEEP_TIME" -le 0 ]]
|
|
|
|
then
|
|
|
|
SLEEP_TIME=1
|
|
|
|
fi
|
|
|
|
|
|
|
|
iterations=$((MAX_WAIT_SECS/SLEEP_TIME))
|
|
|
|
online=false
|
|
|
|
|
|
|
|
for ((s=0; s<=iterations; s++))
|
|
|
|
do
|
|
|
|
online="$(zerotier-cli -j info | jq '.online' 2>/dev/null)"
|
|
|
|
if [[ "$online" == "true" ]]
|
|
|
|
then
|
|
|
|
break
|
|
|
|
fi
|
|
|
|
sleep "$SLEEP_TIME"
|
|
|
|
echo " ."
|
|
|
|
done
|
|
|
|
|
|
|
|
if [[ "$online" != "true" ]]
|
|
|
|
then
|
|
|
|
echo "Waited $MAX_WAIT_SECS for zerotier-one to start, exiting." >&2
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
echo "done."
|
2022-04-05 23:38:20 +00:00
|
|
|
|
2022-07-15 16:03:20 +00:00
|
|
|
(
|
|
|
|
echo "Starting route helper"
|
2022-04-05 23:38:20 +00:00
|
|
|
while true
|
|
|
|
do
|
2022-07-15 16:03:20 +00:00
|
|
|
if ! NETWORK_LIST="$(zerotier-cli -j listnetworks)"
|
|
|
|
then
|
|
|
|
echo "Route helper: $NETWORK_LIST" >&2
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
NETWORK_COUNT="$(jq -r '. | length' <<< "$NETWORK_LIST")"
|
|
|
|
if [[ "$NETWORK_COUNT" -gt 0 ]]
|
|
|
|
then
|
2022-04-05 23:38:20 +00:00
|
|
|
for ((j=0; j<=$((NETWORK_COUNT-1)); j++))
|
|
|
|
do
|
2022-07-15 16:03:20 +00:00
|
|
|
ALLOW_DEFAULT="$(jq -r '.['$j'].allowDefault' <<< "$NETWORK_LIST")"
|
|
|
|
ROUTE_COUNT="$(jq -r '.['$j'].routes | length' <<< "$NETWORK_LIST")"
|
2022-04-05 23:38:20 +00:00
|
|
|
for ((k=0; k<=$((ROUTE_COUNT-1)); k++))
|
|
|
|
do
|
2022-07-15 16:03:20 +00:00
|
|
|
ROUTE="$(jq -r '.['$j'].routes['$k'].target' <<< "$NETWORK_LIST")"
|
2023-01-20 19:57:13 +00:00
|
|
|
VIA="$(jq -r '.['$j'].routes['$k'].via' <<< "$NETWORK_LIST")"
|
2022-07-15 16:03:20 +00:00
|
|
|
if [[ -n "$ROUTE" ]]
|
2022-04-05 23:38:20 +00:00
|
|
|
then
|
2022-07-15 16:03:20 +00:00
|
|
|
# check if route is default and allowDefault enabled for this network
|
|
|
|
if [[ "$ROUTE" == "0.0.0.0/0" && "$ALLOW_DEFAULT" == "false" ]]
|
|
|
|
then
|
|
|
|
continue
|
|
|
|
fi
|
|
|
|
EXIST="$(ip -o route show "$ROUTE")"
|
2023-01-20 19:57:13 +00:00
|
|
|
if [[ -z "${EXIST}" && "$VIA" == "null" ]]
|
2022-07-15 16:03:20 +00:00
|
|
|
then
|
|
|
|
IFNAME="$(jq -r '.['$j'] | .portDeviceName' <<< "$NETWORK_LIST")"
|
|
|
|
echo " Adding route $ROUTE to dev $IFNAME"
|
|
|
|
ip route add "$ROUTE" dev "$IFNAME"
|
|
|
|
# Routes will be deleted when ZT brings the interface down
|
|
|
|
fi
|
2022-04-05 23:38:20 +00:00
|
|
|
fi
|
|
|
|
done
|
|
|
|
done
|
|
|
|
fi
|
2022-07-15 16:03:20 +00:00
|
|
|
sleep 15
|
|
|
|
done ) &
|
|
|
|
|
|
|
|
wait
|
|
|
|
|