2016-07-05 23:25:46 +02:00
|
|
|
#!/bin/sh
|
|
|
|
|
2018-07-29 12:36:36 +02:00
|
|
|
# Usage: vpn-select <path-to-hood-file>
|
|
|
|
|
2017-10-05 15:37:45 +02:00
|
|
|
. /usr/share/libubox/jshn.sh
|
|
|
|
|
2018-07-29 12:36:36 +02:00
|
|
|
hoodfile="$1"
|
|
|
|
|
2016-07-05 23:25:46 +02:00
|
|
|
make_config() {
|
|
|
|
# remove old config
|
2017-10-05 15:37:43 +02:00
|
|
|
rm /tmp/fastd_fff_peers/*
|
2017-10-05 15:37:45 +02:00
|
|
|
Index=1
|
2018-07-29 12:36:36 +02:00
|
|
|
json_load "$(cat "$hoodfile")"
|
2017-10-05 15:37:45 +02:00
|
|
|
json_select vpn
|
2016-07-05 23:25:46 +02:00
|
|
|
# get fastd peers
|
2017-10-05 15:37:45 +02:00
|
|
|
while json_select "$Index" > /dev/null
|
|
|
|
do
|
|
|
|
json_get_var protocol protocol
|
2018-07-26 19:22:22 +02:00
|
|
|
if [ "$protocol" = "fastd" ]; then
|
2017-10-05 15:37:45 +02:00
|
|
|
json_get_var servername name
|
|
|
|
filename="/etc/fastd/fff/peers/$servername"
|
|
|
|
echo "#name \"${servername}\";" > "$filename"
|
|
|
|
json_get_var key key
|
|
|
|
echo "key \"${key}\";" >> "$filename"
|
|
|
|
json_get_var address address
|
|
|
|
json_get_var port port
|
2018-11-10 23:17:02 +01:00
|
|
|
echo "remote \"${address}\" port ${port};" >> "$filename"
|
2017-10-05 15:37:45 +02:00
|
|
|
echo "" >> "$filename"
|
|
|
|
echo "float yes;" >> "$filename"
|
|
|
|
fi
|
|
|
|
json_select ".." # back to vpn
|
|
|
|
Index=$(( Index + 1 ))
|
2016-07-05 23:25:46 +02:00
|
|
|
done
|
2017-10-05 15:37:45 +02:00
|
|
|
json_select ".." # back to root
|
2016-07-05 23:25:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
# main
|
|
|
|
|
2018-07-29 12:36:36 +02:00
|
|
|
# Only do something if file is there and not empty; otherwise exit 1
|
|
|
|
if [ -s "$hoodfile" ]; then
|
2017-10-05 15:37:45 +02:00
|
|
|
if [ ! -d /tmp/fastd_fff_peers ]; then
|
|
|
|
# first run after reboot
|
|
|
|
mkdir /tmp/fastd_fff_peers
|
|
|
|
make_config
|
2019-06-27 14:44:25 +02:00
|
|
|
# start fastd only if there are some peers
|
2017-10-05 15:37:45 +02:00
|
|
|
[ "$(ls /etc/fastd/fff/peers/* 2>/dev/null)" ] && /etc/init.d/fastd start
|
|
|
|
else
|
|
|
|
make_config
|
|
|
|
/etc/init.d/fastd reload
|
2016-07-24 21:34:27 +02:00
|
|
|
|
2017-10-05 15:37:45 +02:00
|
|
|
# fastd start/stop for various situations
|
|
|
|
pidfile="/tmp/run/fastd.fff.pid"
|
|
|
|
if [ "$(ls /etc/fastd/fff/peers/* 2>/dev/null)" ]; then
|
|
|
|
([ -s "$pidfile" ] && [ -d "/proc/$(cat "$pidfile")" ]) || /etc/init.d/fastd start
|
|
|
|
else
|
|
|
|
([ -s "$pidfile" ] && [ -d "/proc/$(cat "$pidfile")" ]) && /etc/init.d/fastd stop
|
|
|
|
fi
|
|
|
|
fi
|
2018-07-29 12:36:36 +02:00
|
|
|
exit 0
|
|
|
|
else
|
|
|
|
echo "vpn-select: Hood file not found or empty!"
|
|
|
|
exit 1
|
2016-07-05 23:25:46 +02:00
|
|
|
fi
|