[v2,1/2] fff-vpn-select: make vpn-select modular

Submitted by Robert Langhammer on June 18, 2020, 10:53 p.m.

Details

Message ID 20200618225324.29501-1-rlanghammer@web.de
State Rejected
Headers show

Commit Message

Robert Langhammer June 18, 2020, 10:53 p.m.
vpn-select is an old relic and did not reflect the opportunities of our hoodfile.
This rewrite makes vpn-select modular to easely add new vpn-protocols.

The stuff dependent on the vpn-protocol is outsourced to files in /etc/vpn-select.d and comes in with the respective vpn package.

Signed-off-by: Robert Langhammer <rlanghammer@web.de>

---
Changes in v2

* learn supported protocols. This way, the ugly grepping can be thrown away.
* add function ${protocol}_clear to remove old config.
* rename function _config to ${protocol}_addpeer.
* add errormessage when protocol is unknown.
---
 .../fff-vpn-select/files/usr/sbin/vpn-select  | 91 +++++++------------
 1 file changed, 35 insertions(+), 56 deletions(-)

--
2.20.1

Patch hide | download patch | download mbox

diff --git a/src/packages/fff/fff-vpn-select/files/usr/sbin/vpn-select b/src/packages/fff/fff-vpn-select/files/usr/sbin/vpn-select
index 30883f5..29fea38 100755
--- a/src/packages/fff/fff-vpn-select/files/usr/sbin/vpn-select
+++ b/src/packages/fff/fff-vpn-select/files/usr/sbin/vpn-select
@@ -1,65 +1,44 @@ 
 #!/bin/sh

 # Usage: vpn-select <path-to-hood-file>
+# To add a new protocol, put a file with three functions to /etc/vpn-select.d/ .
+# The file must start with protocol=name. It is most important to use the same name here and in hoodfile.
+# The function ${protocol}_addpeer() is called for every peer in hoodfile.
+# The second function ${protocol}_start_stop() is called once per installed protocol.
+# The old config can be cleared in function ${protocol}_clear() .

 . /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
+[ -s "$hoodfile" ] || exit 1
+
+# source functions
+for file in /etc/vpn-select.d/*; do
+	. "$file"
+	supported_protocols="$supported_protocols $protocol"
+done
+
+# clear old config
+for protocol in $supported_protocols; do
+    "${protocol}_clear"
+done
+
+# load hoodfile
+json_load "$(cat "$hoodfile")"
+json_select vpn
+
+# configure vpn
+index=1
+while json_select "$index" > /dev/null ; do
+	json_get_var protocol protocol
+	"${protocol}_addpeer" || echo "protocol $protocol unknown"
+	json_select ".." # back to vpn
+	index=$(( index + 1 ))
+done
+
+# start/restart/stop vpnservices
+for protocol in $supported_protocols; do
+	"${protocol}_start_stop"
+done

-		# 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