From b0562090f05b3d0890021c9b4e7a22fd15c60372 Mon Sep 17 00:00:00 2001 From: Franck Nijhof Date: Thu, 29 Aug 2019 12:55:05 +0200 Subject: [PATCH] :sparkles: Allow customize of WireGuard interface name --- wireguard/config.json | 1 + wireguard/rootfs/etc/cont-init.d/config.sh | 20 +++++++++++++------ wireguard/rootfs/etc/services.d/wireguard/run | 10 +++++++++- 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/wireguard/config.json b/wireguard/config.json index 7c1d392..7945c0f 100755 --- a/wireguard/config.json +++ b/wireguard/config.json @@ -56,6 +56,7 @@ "log_level": "match(^(trace|debug|info|notice|warning|error|fatal)$)?", "server": { "host": "str", + "interface": "match(^wg([0-9])+$)?", "addresses": ["str"], "dns": ["str"], "private_key": "str?", diff --git a/wireguard/rootfs/etc/cont-init.d/config.sh b/wireguard/rootfs/etc/cont-init.d/config.sh index 931e0c8..cbce687 100644 --- a/wireguard/rootfs/etc/cont-init.d/config.sh +++ b/wireguard/rootfs/etc/cont-init.d/config.sh @@ -13,6 +13,7 @@ declare endpoint declare filename declare fwmark declare host +declare interface declare keep_alive declare mtu declare name @@ -33,8 +34,15 @@ if ! bashio::fs.directory_exists '/ssl/wireguard'; then bashio::exit.nok "Could create wireguard storage folder!" fi +# Get interface and config file location +interface="wg0" +if bashio::config.has_value "server.interface"; then + interface=$(bashio::config "server.interface") +fi +config="/etc/wireguard/${interface}.conf" + # Start creation of configuration -echo "[Interface]" > "${CONFIG}" +echo "[Interface]" > "${config}" # Check if at least 1 address is specified if ! bashio::config.has_value 'server.addresses'; then @@ -44,17 +52,17 @@ fi # Add all server addresses to the configuration for address in $(bashio::config 'server.addresses'); do [[ "${address}" == *"/"* ]] || address="${address}/24" - echo "Address = ${address}" >> "${CONFIG}" + echo "Address = ${address}" >> "${config}" done # Add all server DNS addresses to the configuration if bashio::config.has_value 'server.dns'; then for dns in $(bashio::config 'server.dns'); do - echo "DNS = ${dns}" >> "${CONFIG}" + echo "DNS = ${dns}" >> "${config}" done else dns=$(bashio::dns.host) - echo "DNS = ${dns}" >> ${CONFIG} + echo "DNS = ${dns}" >> ${config} fi # Get the server's private key @@ -137,7 +145,7 @@ fi # End configuration file with an empty line echo "" -} >> "${CONFIG}" +} >> "${config}" # Get DNS for client configurations if bashio::config.has_value 'server.dns'; then @@ -226,7 +234,7 @@ for peer in $(bashio::config 'peers|keys'); do bashio::config.has_value "peers[${peer}].endpoint" \ && echo "Endpoint = ${endpoint}" echo "" - } >> "${CONFIG}" + } >> "${config}" # Generate client configuration diff --git a/wireguard/rootfs/etc/services.d/wireguard/run b/wireguard/rootfs/etc/services.d/wireguard/run index 44e24c0..f57e618 100644 --- a/wireguard/rootfs/etc/services.d/wireguard/run +++ b/wireguard/rootfs/etc/services.d/wireguard/run @@ -3,6 +3,8 @@ # Community Hass.io Add-ons: WireGuard # Runs WireGuard # ============================================================================== +declare interface + s6-svc -O /var/run/s6/services/wireguard bashio::log.info "Starting WireGuard..." @@ -11,5 +13,11 @@ bashio::log.info "Starting WireGuard..." # WireGuard we are OK to go. export WG_I_PREFER_BUGGY_USERSPACE_TO_POLISHED_KMOD=1 +# Get the interface +interface="wg0" +if bashio::config.has_value "server.interface"; then + interface=$(bashio::config "server.interface") +fi + # Run the WireGuard -exec wg-quick up wg0 +exec wg-quick up "${interface}"