From 572cc0dc890f28fc823e28381e2b36f5ba751871 Mon Sep 17 00:00:00 2001 From: Navratan Gupta Date: Sat, 6 Jun 2020 15:49:09 +0530 Subject: [PATCH] Added uninstallation steps WireGuard can be uninstalled by running bash wireguard-install.sh uninstall --- wireguard-install.sh | 108 ++++++++++++++++++++++++++++++++++++------- 1 file changed, 91 insertions(+), 17 deletions(-) diff --git a/wireguard-install.sh b/wireguard-install.sh index f6768fb..d8fda20 100644 --- a/wireguard-install.sh +++ b/wireguard-install.sh @@ -1,5 +1,23 @@ #!/bin/bash +function checkOS() { + # Check OS version + if [[ -e /etc/debian_version ]]; then + source /etc/os-release + OS=$ID # debian or ubuntu + elif [[ -e /etc/fedora-release ]]; then + source /etc/os-release + OS=$ID + elif [[ -e /etc/centos-release ]]; then + OS=centos + elif [[ -e /etc/arch-release ]]; then + OS=arch + else + echo "Looks like you aren't running this installer on a Debian, Ubuntu, Fedora, CentOS or Arch Linux system" + exit 1 + fi +} + function addClient() { # Load params source /etc/wireguard/params @@ -62,6 +80,69 @@ AllowedIPs = $CLIENT_WG_IPV4/32,$CLIENT_WG_IPV6/128" >>"/etc/wireguard/$SERVER_W echo "It is also available in $HOME/$SERVER_WG_NIC-client-$CLIENT_NAME.conf" } +function uninstall() { + +## To uninstall wireguard and its setup ## + + +#Check OS type +checkOS + +#get WireGuard interface name +SERVER_WG_NIC="wg0" +read -rp "WireGuard interface name: " -e -i "$SERVER_WG_NIC" SERVER_WG_NIC + +ip link set down "$SERVER_WG_NIC" #stop wireguard interface + +#Remove wireguard tool and qrencode +if [[ $OS == 'ubuntu' ]]; then + apt-get remove --purge -y wireguard qrencode + add-apt-repository -y -r ppa:wireguard/wireguard + apt-get autoremove -y +elif [[ $OS == 'debian' ]]; then + apt-get remove --purge -y wireguard qrencode + apt-get autoremove -y +elif [[ $OS == 'fedora' ]]; then + dnf remove -y wireguard-tools qrencode + if [[ $VERSION_ID -lt 32 ]]; then + dnf remove -y wireguard-dkms + dnf copr disable -y jdoss/wireguard + fi + dnf autoremove -y +elif [[ $OS == 'centos' ]]; then + yum -y remove wireguard-dkms wireguard-tools qrencode + yum -y autoremove +elif [[ $OS == 'arch' ]]; then + pacman -Rs --noconfirm wireguard-tools wireguard-arch qrencode +fi + +#Delete /etc/wireguard +rm -rfv /etc/wireguard > /dev/null 2>&1 + +if [[ -e /etc/sysctl.d/wg.conf ]]; then + #Delete wg.conf + rm -fv /etc/sysctl.d/wg.conf +fi + +sysctl --system + +systemctl stop "wg-quick@$SERVER_WG_NIC" +systemctl disable "wg-quick@$SERVER_WG_NIC" +ip link delete "$SERVER_WG_NIC" #delete wireguard interface +# Check if WireGuard is running +systemctl is-active --quiet "wg-quick@$SERVER_WG_NIC" +WG_RUNNING=$? + +if [[ $WG_RUNNING -ne 0 ]]; then + echo "WireGuard failed to uninstall properly." + exit 1 +else + echo "WireGuard uninstalled successfully." + exit 0 +fi + +} + if [ "$EUID" -ne 0 ]; then echo "You need to run this script as root" exit 1 @@ -89,27 +170,20 @@ if [[ $1 == "add-client" ]]; then echo "Please install WireGuard first." exit 1 fi +elif [[ $1 == "uninstall" ]]; then + if [[ -e /etc/wireguard ]]; then + uninstall + exit 0 + else + echo "WireGuard is not installed." + exit 1 + fi elif [[ -e /etc/wireguard ]]; then - echo "WireGuard is already installed. Run with 'add-client' to add a client." - exit 1 -fi - -# Check OS version -if [[ -e /etc/debian_version ]]; then - source /etc/os-release - OS=$ID # debian or ubuntu -elif [[ -e /etc/fedora-release ]]; then - source /etc/os-release - OS=$ID -elif [[ -e /etc/centos-release ]]; then - OS=centos -elif [[ -e /etc/arch-release ]]; then - OS=arch -else - echo "Looks like you aren't running this installer on a Debian, Ubuntu, Fedora, CentOS or Arch Linux system" + echo "WireGuard is already installed. Run with 'add-client' to add a client or 'uninstall' to remove." exit 1 fi +checkOS # Detect public IPv4 address and pre-fill for the user SERVER_PUB_IPV4=$(ip addr | grep 'inet' | grep -v inet6 | grep -vE '127\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' | grep -oE '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' | head -1) read -rp "IPv4 or IPv6 public address: " -e -i "$SERVER_PUB_IPV4" SERVER_PUB_IP