diff --git a/wirego b/wirego index 16d6deb..0a67431 100755 --- a/wirego +++ b/wirego @@ -24,6 +24,19 @@ function list_profiles() { echo $profiles | xargs -n1 } +function output_message() { + echo "----------------------" + echo "Taking $1: $2" + echo "----------------------" +} + +function wg-quick-with-message() { + up_down=$1 + profile=$2 + output_message ${up_down^^} $profile + wg-quick $up_down $profile +} + # Detect Operating System if [ -f /etc/os-release ]; then source /etc/os-release @@ -55,13 +68,9 @@ fi active_profiles=`sudo wg | grep interface | awk '{print $2}'` # up selected -# check existnace of wg if [ "$1" == "up" ] ; then for up_profile in $up_profiles; do - echo "----------------------" - echo "Taking up: $up_profile" - wg-quick up $up_profile - echo "----------------------" + wg-quick-with-message up $up_profile # echo -n "press ENTER to continue"; read done exit @@ -100,15 +109,12 @@ fi if [ "$parameter" == "down" ] ; then if [ "$active_profiles" != "" ] ; then for active_profile in $active_profiles; do - echo "----------------------" - echo "Taking down: $active_profile" - wg-quick down $active_profile + wg-quick-with-message down $active_profile echo if [ "${DISTRO}" == "fedora" ]; then # restart resolved to remove wireguard DNS from your /etc/resolv.conf sudo systemctl restart systemd-resolved.service fi - echo "----------------------" done else echo "wg is already down" @@ -121,22 +127,20 @@ test_profile_existance=`echo $profiles | grep $parameter ` result=$? if [ $result -eq 0 ] ; then # turn down any other active wg + # including the requested one if [ "$active_profiles" != "" ] ; then - if [ "$active_profiles" == "$parameter" ] ; then - echo "Wireguard $parameter is already up, details:" - sudo wg - exit - else + for active_profile in $active_profiles ; do + if [ "$active_profile" == "$parameter" ] ; then + echo "Wireguard $parameter was already up, taking it down anyway" + fi # turn it down - wg-quick down $active_profiles - echo - wg-quick up $parameter - fi - else - wg-quick up $parameter + wg-quick-with-message down $active_profile + done fi + wg-quick-with-message up $parameter else echo "ERROR: profile not existing" echo list_profiles + exit 1 fi