Skip to content

Commit

Permalink
fixing multiple down when one profile was already up
Browse files Browse the repository at this point in the history
  • Loading branch information
francescor committed Nov 28, 2021
1 parent 1a329d3 commit a711d19
Showing 1 changed file with 24 additions and 20 deletions.
44 changes: 24 additions & 20 deletions wirego
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand All @@ -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

0 comments on commit a711d19

Please sign in to comment.