firmware/src/packages/fff/fff-vpn-select/files/usr/sbin/vpn-select

66 lines
1.6 KiB
Bash
Executable File

#!/bin/sh
# Usage: vpn-select <path-to-hood-file>
. /usr/share/libubox/jshn.sh
hoodfile="$1"
make_config() {
# remove old config
rm /tmp/fastd_fff_peers/*
# prepare
Index=1
json_load "$(cat "$hoodfile")"
json_select vpn
# get fastd peers
while json_select "$Index" > /dev/null
do
json_get_var protocol protocol
if [ "$protocol" = "fastd" ]; then
# set up fastd
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
echo "remote \"${address}\" port ${port};" >> "$filename"
echo "" >> "$filename"
echo "float yes;" >> "$filename"
fi
json_select ".." # back to vpn
Index=$(( Index + 1 ))
done
json_select ".." # back to root
}
# Only do something if file is there and not empty; otherwise exit 1
if [ -s "$hoodfile" ]; then
if [ ! -d /tmp/fastd_fff_peers ]; then
# first run after reboot
mkdir /tmp/fastd_fff_peers
make_config
# start fastd only if there are some peers
[ "$(ls /etc/fastd/fff/peers/* 2>/dev/null)" ] && /etc/init.d/fastd start
else
make_config
/etc/init.d/fastd reload
# 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
exit 0
else
echo "vpn-select: Hood file not found or empty!"
exit 1
fi