Speeds up config generation.

Signed-off-by: Rosen Penev <ros...@gmail.com>
---
 .../utils/wireguard-tools/files/wireguard.sh  | 32 ++++++++-----------
 1 file changed, 14 insertions(+), 18 deletions(-)

diff --git a/package/network/utils/wireguard-tools/files/wireguard.sh 
b/package/network/utils/wireguard-tools/files/wireguard.sh
index 4c811c6ba9..1e424e7959 100644
--- a/package/network/utils/wireguard-tools/files/wireguard.sh
+++ b/package/network/utils/wireguard-tools/files/wireguard.sh
@@ -42,13 +42,12 @@ 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
+       {
+       echo "[Peer]"
+       echo "PublicKey=${public_key}"
+       [ "${preshared_key}" ] && echo "PresharedKey=${preshared_key}"
        for allowed_ip in $allowed_ips; do
-               echo "AllowedIPs=${allowed_ip}" >> "${wg_cfg}"
+               echo "AllowedIPs=${allowed_ip}"
        done
        if [ "${endpoint_host}" ]; then
                case "${endpoint_host}" in
@@ -64,11 +63,10 @@ proto_wireguard_setup_peer() {
                else
                        endpoint="${endpoint}:51820"
                fi
-               echo "Endpoint=${endpoint}" >> "${wg_cfg}"
-       fi
-       if [ "${persistent_keepalive}" ]; then
-               echo "PersistentKeepalive=${persistent_keepalive}" >> 
"${wg_cfg}"
+               echo "Endpoint=${endpoint}"
        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@lists.openwrt.org
https://lists.openwrt.org/mailman/listinfo/openwrt-devel

Reply via email to