about summary refs log tree commit diff
diff options
context:
space:
mode:
authorKevin Mandura <webmaster@kevin-mandura.de>2024-03-23 19:18:43 +0100
committerKevin Mandura <webmaster@kevin-mandura.de>2024-03-23 19:18:43 +0100
commit73f3ca7742fe010095eced0720a94eb1a91e4704 (patch)
tree40ef3f01eefd5d1aa5dfcab3e5d8964eaf10ade4
parentd95b4071482d08ee72382d08f77198ba4286f286 (diff)
downloaddwm-status-sh-73f3ca7742fe010095eced0720a94eb1a91e4704.tar.gz
dwm-status-sh-73f3ca7742fe010095eced0720a94eb1a91e4704.zip
Add VPN status module
-rw-r--r--dwm-status.sh40
1 files changed, 40 insertions, 0 deletions
diff --git a/dwm-status.sh b/dwm-status.sh
index 827719e..a755c2a 100644
--- a/dwm-status.sh
+++ b/dwm-status.sh
@@ -65,6 +65,10 @@ CONFIG_CACHE_DIR="${XDG_CACHE_HOME:-$HOME/.cache}/dwm-status-sh"
 # Use »ip a« to list all recognized network interfaces on your system.
 CONFIG_ETHERNET_INTERFACE_PREFIX="eth"
 
+# The prefix that any VPN interface has in its name.
+# Use »ip a« to list all recognized network interfaces on your system.
+CONFIG_VPN_INTERFACE_PREFIX="tun"
+
 # The prefix that any WLAN interface has in its name.
 # Use »ip a« to list all recognized network interfaces on your system.
 CONFIG_WLAN_INTERFACE_PREFIX="wlan"
@@ -89,6 +93,7 @@ CONFIG_BATTERY_PREFIX="BAT"
 readonly CACHE_NEW_MAILS_FILE="${CONFIG_CACHE_DIR}/new-mails.txt"
 readonly CACHE_ETHERNET_INTERFACE_FILE="${CONFIG_CACHE_DIR}/ethernet-interface.txt"
 readonly CACHE_WLAN_INTERFACE_FILE="${CONFIG_CACHE_DIR}/wlan-interface.txt"
+readonly CACHE_VPN_INTERFACE_FILE="${CONFIG_CACHE_DIR}/vpn-interface.txt"
 readonly CACHE_CPU_TEMPERATURE_FILE="${CONFIG_CACHE_DIR}/cpu-temperature.txt"
 readonly CACHE_CPU_USAGE_FILE="${CONFIG_CACHE_DIR}/cpu-usage.txt"
 readonly CACHE_AUDIO_VOLUME_FILE="${CONFIG_CACHE_DIR}/audio-volume.txt"
@@ -152,6 +157,39 @@ module_mail() {
     fi
 }
 
+module_vpn_interface() {
+    # Abort, if the network interfaces directory variable is empty or
+    # the specified directory doesn't exist.
+    [ -n "$CONFIG_NETWORK_INTERFACES_DIR" ] || return
+    [ -d "$CONFIG_NETWORK_INTERFACES_DIR" ] || return
+
+    if [ "$1" = "force" ]; then
+        # Check, if there is any active VPN interface.
+        vpn_status=0
+        for vpn_interface in "$CONFIG_NETWORK_INTERFACES_DIR"/"${CONFIG_VPN_INTERFACE_PREFIX}"*; do
+            # Check the operstate file to determine, whether the VPN
+            # interface is active.
+            vpn_interface_operstate_file="${vpn_interface}/operstate"
+            if [ -f "$vpn_interface_operstate_file" ]; then
+                vpn_interface_operstate=$(cat "$vpn_interface_operstate_file")
+                [ "$vpn_interface_operstate" = "up" ] && vpn_status=1; break
+            fi
+        done
+
+        # Cache the VPN status into a file.
+        printf "%s" "$vpn_status" > "$CACHE_VPN_INTERFACE_FILE"
+    else
+        # Only read latest information stored inside the cache file.
+        vpn_status=$(cat "$CACHE_VPN_INTERFACE_FILE")
+    fi
+
+    # Append to status bar string, if there is any active VPN
+    # interface.
+    if [ "$vpn_status" = 1 ]; then
+        status_bar_string="${status_bar_string}VPN${CONFIG_DELIM_STRING}"
+    fi
+}
+
 module_wlan_interface() {
     # Abort, if the network interfaces directory variable is empty or
     # the specified directory doesn't exist.
@@ -379,6 +417,7 @@ refresh_status_bar() {
     if [ "$status_bar_refresh_iteration_count" = 0 ]; then
         # It is time to update all status bar modules.
         module_mail force
+        module_vpn_interface force
         module_wlan_interface force
         module_ethernet_interface force
         module_cpu_temperature force
@@ -394,6 +433,7 @@ refresh_status_bar() {
     else
         # Only update prioritized status bar modules.
         module_mail
+        module_vpn_interface force
         module_wlan_interface force
         module_ethernet_interface force
         module_cpu_temperature force