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

48 lines
1.3 KiB
Bash
Executable File

#!/bin/sh
# Usage: vpn-select <path-to-hood-file>
# To add a new protocol, put a file with three functions to /usr/lib/vpn-select.d/ .
# The file must start with protocol=name. It is most important to use the same name here and in hoodfile.
# The old config can be cleared in function ${protocol}_clear(). It is called first once per installed protocol.
# The function ${protocol}_addpeer() is called for every selected peer in hoodfile.
# The function ${protocol}_start_stop() is called at the end once per installed protocol.
. /usr/share/libubox/jshn.sh
hoodfile="$1"
# source functions
for file in /usr/lib/vpn-select.d/*; do
[ -f $file ] && . "$file"
supported_protocols="$supported_protocols $protocol"
done
# clear old config
for protocol in $supported_protocols; do
"${protocol}_clear"
done
# configure vpn
if [ "$0" != "/usr/sbin/vpn-stop" ] ; then
if [ -n "$hoodfile" ] && [ -s "$hoodfile" ] ; then
json_load "$(cat "$hoodfile")"
json_select hood
json_get_var id id
json_select ".."
json_select vpn
json_get_keys vpn_keys
for key in $vpn_keys; do
json_select $key
json_get_var protocol protocol
"${protocol}_addpeer"
json_select ".." # back to vpn
done
fi
fi
# start/restart/stop vpnservices
for protocol in $supported_protocols; do
"${protocol}_start_stop"
done