[OpenWrt-Devel] [PATCH 1/2] wireguard.sh: batch file writes

Rosen Penev rosenp at gmail.com
Sun Jan 5 21:34:56 EST 2020


Speeds up config generation.

Signed-off-by: Rosen Penev <rosenp at gmail.com>
---
 v3: Fix title
 v2: Added PKG_RELEASE bump.
 .../utils/wireguard-tools/files/wireguard.sh  | 64 +++++++++----------
 1 file changed, 30 insertions(+), 34 deletions(-)

diff --git a/package/network/utils/wireguard-tools/files/wireguard.sh b/package/network/utils/wireguard-tools/files/wireguard.sh
index 4c811c6ba9..4bea67e033 100644
--- a/package/network/utils/wireguard-tools/files/wireguard.sh
+++ b/package/network/utils/wireguard-tools/files/wireguard.sh
@@ -42,33 +42,31 @@ proto_wireguard_setup_peer() {
 	config_get endpoint_port "${peer_config}" "endpoint_port"
 	config_get persistent_keepalive "${peer_config}" "persistent_keepalive"
 
-	echo "[Peer]" >> "${wg_cfg}"
-	echo "PublicKey=${public_key}" >> "${wg_cfg}"
-	if [ "${preshared_key}" ]; then
-		echo "PresharedKey=${preshared_key}" >> "${wg_cfg}"
-	fi
-	for allowed_ip in $allowed_ips; do
-		echo "AllowedIPs=${allowed_ip}" >> "${wg_cfg}"
-	done
-	if [ "${endpoint_host}" ]; then
-		case "${endpoint_host}" in
-			*:*)
-				endpoint="[${endpoint_host}]"
-				;;
-			*)
-				endpoint="${endpoint_host}"
-				;;
-		esac
-		if [ "${endpoint_port}" ]; then
-			endpoint="${endpoint}:${endpoint_port}"
-		else
-			endpoint="${endpoint}:51820"
+	{
+		echo "[Peer]"
+		echo "PublicKey=${public_key}"
+		[ "${preshared_key}" ] && echo "PresharedKey=${preshared_key}"
+		for allowed_ip in $allowed_ips; do
+			echo "AllowedIPs=${allowed_ip}"
+		done
+		if [ "${endpoint_host}" ]; then
+			case "${endpoint_host}" in
+				*:*)
+					endpoint="[${endpoint_host}]"
+					;;
+				*)
+					endpoint="${endpoint_host}"
+					;;
+			esac
+			if [ "${endpoint_port}" ]; then
+				endpoint="${endpoint}:${endpoint_port}"
+			else
+				endpoint="${endpoint}:51820"
+			fi
+			echo "Endpoint=${endpoint}"
 		fi
-		echo "Endpoint=${endpoint}" >> "${wg_cfg}"
-	fi
-	if [ "${persistent_keepalive}" ]; then
-		echo "PersistentKeepalive=${persistent_keepalive}" >> "${wg_cfg}"
-	fi
+		[ "${persistent_keepalive}" ] && echo "PersistentKeepalive=${persistent_keepalive}"
+	} >> "${wg_cfg}"
 
 	if [ ${route_allowed_ips} -ne 0 ]; then
 		for allowed_ip in ${allowed_ips}; do
@@ -119,14 +117,12 @@ proto_wireguard_setup() {
 
 	umask 077
 	mkdir -p "${wg_dir}"
-	echo "[Interface]" > "${wg_cfg}"
-	echo "PrivateKey=${private_key}" >> "${wg_cfg}"
-	if [ "${listen_port}" ]; then
-		echo "ListenPort=${listen_port}" >> "${wg_cfg}"
-	fi
-	if [ "${fwmark}" ]; then
-		echo "FwMark=${fwmark}" >> "${wg_cfg}"
-	fi
+	{
+		echo "[Interface]"
+		echo "PrivateKey=${private_key}"
+		[ "${listen_port}" ] && echo "ListenPort=${listen_port}"
+		[ "${fwmark}" ] && echo "FwMark=${fwmark}"
+	} > "${wg_cfg}"
 	config_foreach proto_wireguard_setup_peer "wireguard_${config}"
 
 	# apply configuration file
-- 
2.24.1


_______________________________________________
openwrt-devel mailing list
openwrt-devel at lists.openwrt.org
https://lists.openwrt.org/mailman/listinfo/openwrt-devel



More information about the openwrt-devel mailing list