Improved dual VPN uninstallation, remove duplicate code/script

- Allow using 'pivpn vpn -u' to directly uninstall VPN 'vpn'
  - Also allow using 'pivpn -u' with two VPNs (will present a dialog).
  - During uninstall, ask which VPN to remove only if there are two VPNs
  - PiVPN git repo will be downloaded to '/usr/local/src/pivpn'. All scripts
    in /opt/pivpn, the main pivpn script and the bash completion file,
    are now just symbolic links. Resolves issue #695.
  - Remove unused call to updateWireGuard().
This commit is contained in:
Orazio 2020-05-27 16:36:26 +02:00
parent 1dc10e7d54
commit 823afa3fbb
8 changed files with 121 additions and 137 deletions

View file

@ -18,7 +18,7 @@ pivpnGitUrl="https://github.com/pivpn/pivpn.git"
setupVarsFile="setupVars.conf" setupVarsFile="setupVars.conf"
setupConfigDir="/etc/pivpn" setupConfigDir="/etc/pivpn"
tempsetupVarsFile="/tmp/setupVars.conf" tempsetupVarsFile="/tmp/setupVars.conf"
pivpnFilesDir="/etc/.pivpn" pivpnFilesDir="/usr/local/src/pivpn"
pivpnScriptDir="/opt/pivpn" pivpnScriptDir="/opt/pivpn"
piholeSetupVars="/etc/pihole/setupVars.conf" piholeSetupVars="/etc/pihole/setupVars.conf"
@ -950,11 +950,11 @@ updateRepo(){
### FIXME: Never call rm -rf with a plain variable. Never again as SU! ### FIXME: Never call rm -rf with a plain variable. Never again as SU!
#$SUDO rm -rf "${1}" #$SUDO rm -rf "${1}"
if test -n "$1"; then if test -n "$1"; then
$SUDO rm -rf "$(dirname "$1")/.pivpn" $SUDO rm -rf "$(dirname "$1")/pivpn"
fi fi
# Go back to /etc otherwise git will complain when the current working # Go back to /usr/local/src otherwise git will complain when the current working
# directory has just been deleted (/etc/.pivpn). # directory has just been deleted (/usr/local/src/pivpn).
cd /etc && \ cd /usr/local/src && \
$SUDO git clone -q --depth 1 --no-single-branch "${2}" "${1}" > /dev/null & spinner $! $SUDO git clone -q --depth 1 --no-single-branch "${2}" "${1}" > /dev/null & spinner $!
cd "${1}" || exit 1 cd "${1}" || exit 1
if [ -z "${TESTING+x}" ]; then if [ -z "${TESTING+x}" ]; then
@ -972,11 +972,11 @@ makeRepo(){
### FIXME: Never call rm -rf with a plain variable. Never again as SU! ### FIXME: Never call rm -rf with a plain variable. Never again as SU!
#$SUDO rm -rf "${1}" #$SUDO rm -rf "${1}"
if test -n "$1"; then if test -n "$1"; then
$SUDO rm -rf "$(dirname "$1")/.pivpn" $SUDO rm -rf "$(dirname "$1")/pivpn"
fi fi
# Go back to /etc otherwhise git will complain when the current working # Go back to /usr/local/src otherwhise git will complain when the current working
# directory has just been deleted (/etc/.pivpn). # directory has just been deleted (/usr/local/src/pivpn).
cd /etc && \ cd /usr/local/src && \
$SUDO git clone -q --depth 1 --no-single-branch "${2}" "${1}" > /dev/null & spinner $! $SUDO git clone -q --depth 1 --no-single-branch "${2}" "${1}" > /dev/null & spinner $!
cd "${1}" || exit 1 cd "${1}" || exit 1
if [ -z "${TESTING+x}" ]; then if [ -z "${TESTING+x}" ]; then
@ -2224,43 +2224,34 @@ confUnattendedUpgrades(){
} }
installScripts(){ installScripts(){
# Install the scripts from /etc/.pivpn to their various locations if [[ ${VPN} == 'wireguard' ]]; then
echo -n -e "::: Installing scripts to ${pivpnScriptDir}...\n" othervpn='openvpn'
if [ ! -d "${pivpnScriptDir}/${VPN}" ]; then
$SUDO install -m 0755 -o root -d ${pivpnScriptDir}/${VPN}
fi
$SUDO install -m 755 -t ${pivpnScriptDir} ${pivpnFilesDir}/scripts/*.sh
$SUDO install -m 755 -t ${pivpnScriptDir}/${VPN} ${pivpnFilesDir}/scripts/${VPN}/*.sh
# make a link for a single command being installed
# may already exist if installing the second protocol
if [ ! -e "/usr/local/bin/pivpn" ]; then
$SUDO ln -s -T ${pivpnScriptDir}/${VPN}/pivpn.sh /usr/local/bin/pivpn
fi
# if the other protocol file exists it has been installed
if [[ ${VPN} == 'wireguard' ]]; then
othervpn='openvpn'
else
othervpn='wireguard'
fi
if [ -r "${setupConfigDir}/${othervpn}/${setupVarsFile}" ]; then
# both are installed
# dont need a link, copy the common script to the location instead
$SUDO rm -f /usr/local/bin/pivpn
$SUDO install -m 755 -t /usr/local/bin /${pivpnFilesDir}/scripts/pivpn
fi
if [ -r "${setupConfigDir}/${othervpn}/${setupVarsFile}" ]; then
# both are installed, no bash completion, delete if already there
$SUDO rm -f /etc/bash_completion.d/pivpn
else else
# only one protocol is installed, put bash completion in place othervpn='wireguard'
$SUDO cp "${pivpnFilesDir}/scripts/${VPN}/bash-completion" /etc/bash_completion.d/pivpn fi
$SUDO chown root:root /etc/bash_completion.d/pivpn
$SUDO chmod 755 /etc/bash_completion.d/pivpn # Symlink scripts from /usr/local/src/pivpn to their various locations
# shellcheck disable=SC1091 echo -n -e "::: Installing scripts to ${pivpnScriptDir}...\n"
. /etc/bash_completion.d/pivpn
fi # if the other protocol file exists it has been installed
if [ -r "${setupConfigDir}/${othervpn}/${setupVarsFile}" ]; then
# Both are installed, no bash completion, unlink if already there
$SUDO unlink /etc/bash_completion.d/pivpn
# Unlink the protocol specific pivpn script and symlink the common
# script to the location instead
$SUDO unlink /usr/local/bin/pivpn
$SUDO ln -s -T "${pivpnFilesDir}/scripts/pivpn" /usr/local/bin/pivpn
else
# Only one protocol is installed, symlink bash completion, the pivpn script
# and the script directory
$SUDO ln -s -T "${pivpnFilesDir}/scripts/${VPN}/bash-completion" /etc/bash_completion.d/pivpn
$SUDO ln -s -T "${pivpnFilesDir}/scripts/${VPN}/pivpn.sh" /usr/local/bin/pivpn
$SUDO ln -s "${pivpnFilesDir}/scripts/" "${pivpnScriptDir}"
# shellcheck disable=SC1091
. /etc/bash_completion.d/pivpn
fi
echo " done." echo " done."
} }

View file

@ -47,30 +47,19 @@ function removeOVPNFunc {
} }
function uninstallFunc { function uninstallFunc {
$SUDO ${scriptDir}/uninstall.sh $SUDO ${scriptDir}/uninstall.sh "${vpn}"
exit 0 exit 0
} }
function versionFunc {
printf "\e[1mVersion 1.9\e[0m\n"
}
function update { function update {
shift shift
# $SUDO ${scriptDir}/update.sh "$@" $SUDO ${scriptDir}/update.sh "$@"
echo "::: The updating functionality for PiVPN scripts is temporarily disabled"
echo "::: To keep the VPN (and the system) up to date, use 'apt update' and 'apt upgrade'"
exit 0 exit 0
} }
function backup { function backup {
$SUDO ${scriptDir}/backup.sh $SUDO ${scriptDir}/backup.sh
exit 0 exit 0
} }
@ -105,7 +94,6 @@ case "$1" in
"-r" | "revoke" ) removeOVPNFunc "$@";; "-r" | "revoke" ) removeOVPNFunc "$@";;
"-h" | "help" ) helpFunc;; "-h" | "help" ) helpFunc;;
"-u" | "uninstall" ) uninstallFunc;; "-u" | "uninstall" ) uninstallFunc;;
"-v" ) versionFunc;;
"-up"| "update" ) update "$@" ;; "-up"| "update" ) update "$@" ;;
"-bk"| "backup" ) backup;; "-bk"| "backup" ) backup;;
* ) helpFunc;; * ) helpFunc;;

View file

@ -13,7 +13,7 @@ source "${setupVars}"
echo -e "::::\t\t\e[4mPiVPN debug\e[0m\t\t ::::" echo -e "::::\t\t\e[4mPiVPN debug\e[0m\t\t ::::"
printf "=============================================\n" printf "=============================================\n"
echo -e "::::\t\t\e[4mLatest commit\e[0m\t\t ::::" echo -e "::::\t\t\e[4mLatest commit\e[0m\t\t ::::"
git --git-dir /etc/.pivpn/.git log -n 1 git --git-dir /usr/local/src/pivpn/.git log -n 1
printf "=============================================\n" printf "=============================================\n"
echo -e "::::\t \e[4mInstallation settings\e[0m \t ::::" echo -e "::::\t \e[4mInstallation settings\e[0m \t ::::"
sed "s/$pivpnHOST/REDACTED/" < ${setupVars} sed "s/$pivpnHOST/REDACTED/" < ${setupVars}

View file

@ -11,7 +11,11 @@ if [ $EUID -ne 0 ];then
fi fi
scriptDir="/opt/pivpn" scriptDir="/opt/pivpn"
vpn="wireguard"
uninstallServer(){
$SUDO ${scriptDir}/uninstall.sh
exit 0
}
showHelp(){ showHelp(){
echo "::: To pass off to the pivpn command for each protocol" echo "::: To pass off to the pivpn command for each protocol"
@ -20,6 +24,7 @@ showHelp(){
echo "::: Usage: pivpn ovpn <command> [option]" echo "::: Usage: pivpn ovpn <command> [option]"
echo ":::" echo ":::"
echo "::: -h, help Show this help dialog" echo "::: -h, help Show this help dialog"
echo "::: -u, uninstall Uninstall pivpn from your system!"
exit 0 exit 0
} }
@ -32,5 +37,6 @@ case "$1" in
wg ) "${scriptDir}/wireguard/pivpn.sh" "${@:2}";; wg ) "${scriptDir}/wireguard/pivpn.sh" "${@:2}";;
ovpn ) "${scriptDir}/openvpn/pivpn.sh" "${@:2}";; ovpn ) "${scriptDir}/openvpn/pivpn.sh" "${@:2}";;
"-h" | "help" ) showHelp;; "-h" | "help" ) showHelp;;
"-u" | "uninstall" ) uninstallServer;;
* ) showHelp;; * ) showHelp;;
esac esac

View file

@ -4,24 +4,6 @@
### FIXME: global: config storage, refactor all scripts to adhere to the storage ### FIXME: global: config storage, refactor all scripts to adhere to the storage
### FIXME: use variables where appropriate, reduce magic numbers by 99.9%, at least. ### FIXME: use variables where appropriate, reduce magic numbers by 99.9%, at least.
# what is already installed?
setupVars="/etc/pivpn/openvpn/setupVars.conf"
foundins=''
if [ -f "${setupVars}" ]; then
foundins="openvpn"
fi
setupVars="/etc/pivpn/wireguard/setupVars.conf"
if [ -f "${setupVars}" ]; then
foundins="${foundins} wireguard"
fi
if [ -z ${foundins} ]; then
foundins="nothing found"
fi
# Find the rows and columns. Will default to 80x24 if it can not be detected. # Find the rows and columns. Will default to 80x24 if it can not be detected.
screen_size=$(stty size 2>/dev/null || echo 24 80) screen_size=$(stty size 2>/dev/null || echo 24 80)
rows=$(echo "$screen_size" | awk '{print $1}') rows=$(echo "$screen_size" | awk '{print $1}')
@ -34,24 +16,46 @@ c=$(( columns / 2 ))
r=$(( r < 20 ? 20 : r )) r=$(( r < 20 ? 20 : r ))
c=$(( c < 70 ? 70 : c )) c=$(( c < 70 ? 70 : c ))
chooseVPNCmd=(whiptail --backtitle "Setup PiVPN" --title "Installation mode" --separate-output --radiolist "WireGuard is a new kind of VPN that provides near-instantaneous connection speed, high performance, and modern cryptography.\\n\\nIt's the recommended choice especially if you use mobile devices where WireGuard is easier on battery than OpenVPN.\\n\\nOpenVPN is still available if you need the traditional, flexible, trusted VPN protocol or if you need features like TCP and custom search domain.\\n\\nChoose a VPN (${foundins}) to uninstall (press space to select):" "${r}" "${c}" 2)
VPNChooseOptions=(WireGuard "" on
OpenVPN "" off)
if VPN=$("${chooseVPNCmd[@]}" "${VPNChooseOptions[@]}" 2>&1 >/dev/tty) ; then
echo "::: Using VPN: $VPN"
VPN="${VPN,,}"
else
echo "::: Cancel selected, exiting...."
exit 1
fi
PKG_MANAGER="apt-get" PKG_MANAGER="apt-get"
UPDATE_PKG_CACHE="${PKG_MANAGER} update" UPDATE_PKG_CACHE="${PKG_MANAGER} update"
dnsmasqConfig="/etc/dnsmasq.d/02-pivpn.conf" dnsmasqConfig="/etc/dnsmasq.d/02-pivpn.conf"
setupConfigDir="/etc/pivpn"
setupVarsFile="setupVars.conf" setupVarsFile="setupVars.conf"
setupVars="${setupConfigDir}/${VPN}/${setupVarsFile}" setupConfigDir="/etc/pivpn"
pivpnFilesDir="/usr/local/src/pivpn"
pivpnScriptDir="/opt/pivpn"
if [ -r "${setupConfigDir}/wireguard/${setupVarsFile}" ] && [ -r "${setupConfigDir}/openvpn/${setupVarsFile}" ]; then
vpnStillExists=1
# Two protocols have been installed, check if the script has passed
# an argument, otherwise ask the user which one he wants to remove
if [ $# -ge 1 ]; then
VPN="$1"
echo "::: Uninstalling VPN: $VPN"
else
chooseVPNCmd=(whiptail --backtitle "Setup PiVPN" --title "Uninstall" --separate-output --radiolist "Both OpenVPN and WireGuard are installed, choose a VPN to uninstall (press space to select):" "${r}" "${c}" 2)
VPNChooseOptions=(WireGuard "" on
OpenVPN "" off)
if VPN=$("${chooseVPNCmd[@]}" "${VPNChooseOptions[@]}" 2>&1 >/dev/tty) ; then
echo "::: Uninstalling VPN: $VPN"
VPN="${VPN,,}"
else
echo "::: Cancel selected, exiting...."
exit 1
fi
fi
setupVars="${setupConfigDir}/${VPN}/${setupVarsFile}"
else
vpnStillExists=0
if [ -r "${setupConfigDir}/wireguard/${setupVarsFile}" ]; then
setupVars="${setupConfigDir}/wireguard/${setupVarsFile}"
elif [ -r "${setupConfigDir}/openvpn/${setupVarsFile}" ]; then
setupVars="${setupConfigDir}/openvpn/${setupVarsFile}"
fi
fi
if [ ! -f "${setupVars}" ]; then if [ ! -f "${setupVars}" ]; then
echo "::: Missing setup vars file!" echo "::: Missing setup vars file!"
@ -61,12 +65,6 @@ fi
# shellcheck disable=SC1090 # shellcheck disable=SC1090
source "${setupVars}" source "${setupVars}"
if [[ ${VPN} == 'wireguard' ]]; then
othervpn='openvpn'
else
othervpn='wireguard'
fi
### FIXME: introduce global lib ### FIXME: introduce global lib
spinner(){ spinner(){
local pid=$1 local pid=$1
@ -123,25 +121,11 @@ removeAll(){
fi fi
vpnStillExists='no'
if [ -r "${setupConfigDir}/${othervpn}/${setupVarsFile}" ]; then
vpnStillExists='yes'
$SUDO rm -f /usr/local/bin/pivpn
$SUDO ln -s -T /opt/pivpn/${othervpn}/pivpn.sh /usr/local/bin/pivpn
echo ":::"
echo "::: Two VPN protocols exist, you should remove ${othervpn} too"
echo ":::"
else
rm -f /etc/bash_completion.d/pivpn
fi
# Disable IPv4 forwarding # Disable IPv4 forwarding
if [ ${vpnStillExists} == 'no' ]; then if [ "${vpnStillExists}" -eq 0 ]; then
sed -i '/net.ipv4.ip_forward=1/c\#net.ipv4.ip_forward=1' /etc/sysctl.conf sed -i '/net.ipv4.ip_forward=1/c\#net.ipv4.ip_forward=1' /etc/sysctl.conf
sysctl -p sysctl -p
fi fi
# Purge dependencies # Purge dependencies
echo "::: Purge dependencies..." echo "::: Purge dependencies..."
@ -221,20 +205,34 @@ removeAll(){
rm -rf "$install_home/ovpns" rm -rf "$install_home/ovpns"
fi fi
if [ ${vpnStillExists} == 'no' ]; then if [ "${vpnStillExists}" -eq 0 ]; then
echo ":::" echo ":::"
echo "::: Removing pivpn system files..." echo "::: Removing pivpn system files..."
rm -rf /etc/.pivpn rm -rf "${setupConfigDir}"
rm -rf /etc/pivpn rm -rf "${pivpnFilesDir}"
rm -f /var/log/*pivpn* rm -f /var/log/*pivpn*
rm -rf /opt/pivpn rm -f /etc/bash_completion.d/pivpn
rm -f /usr/local/bin/pivpn unlink "${pivpnScriptDir}"
else unlink /usr/local/bin/pivpn
echo ":::" else
echo "::: Other protocol still present, so not" if [[ ${VPN} == 'wireguard' ]]; then
echo "::: removing pivpn system files" othervpn='openvpn'
rm -f "${setupConfigDir}/${VPN}/${setupVarsFile}" else
fi othervpn='wireguard'
fi
echo ":::"
echo "::: Other VPN ${othervpn} still present, so not"
echo "::: removing pivpn system files"
rm -f "${setupConfigDir}/${VPN}/${setupVarsFile}"
# Restore single pivpn script and bash completion for the remaining VPN
$SUDO unlink /usr/local/bin/pivpn
$SUDO ln -s -T "${pivpnFilesDir}/scripts/${othervpn}/pivpn.sh" /usr/local/bin/pivpn
$SUDO ln -s -T "${pivpnFilesDir}/scripts/${othervpn}/bash-completion" /etc/bash_completion.d/pivpn
# shellcheck disable=SC1091
. /etc/bash_completion.d/pivpn
fi
echo ":::" echo ":::"
printf "::: Finished removing PiVPN from your system.\\n" printf "::: Finished removing PiVPN from your system.\\n"

View file

@ -19,6 +19,10 @@ c=$(( columns / 2 ))
r=$(( r < 20 ? 20 : r )) r=$(( r < 20 ? 20 : r ))
c=$(( c < 70 ? 70 : c )) c=$(( c < 70 ? 70 : c ))
echo "::: The updating functionality for PiVPN scripts is temporarily disabled"
echo "::: To keep the VPN (and the system) up to date, use 'apt update' and 'apt upgrade'"
exit 0
chooseVPNCmd=(whiptail --backtitle "Setup PiVPN" --title "Installation mode" --separate-output --radiolist "Choose a VPN to update (press space to select):" "${r}" "${c}" 2) chooseVPNCmd=(whiptail --backtitle "Setup PiVPN" --title "Installation mode" --separate-output --radiolist "Choose a VPN to update (press space to select):" "${r}" "${c}" 2)
VPNChooseOptions=(WireGuard "" on VPNChooseOptions=(WireGuard "" on
OpenVPN "" off) OpenVPN "" off)

View file

@ -48,15 +48,13 @@ removeClient(){
} }
uninstallServer(){ uninstallServer(){
$SUDO ${scriptdir}/uninstall.sh $SUDO ${scriptdir}/uninstall.sh "${vpn}"
exit 0 exit 0
} }
updateScripts(){ updateScripts(){
shift shift
# $SUDO ${scriptdir}/update.sh "$@" $SUDO ${scriptdir}/update.sh "$@"
echo "::: The updating functionality for PiVPN scripts is temporarily disabled"
echo "::: To keep the VPN (and the system) up to date, use 'apt update' and 'apt upgrade'"
exit 0 exit 0
} }
@ -98,7 +96,6 @@ case "$1" in
"-h" | "help" ) showHelp;; "-h" | "help" ) showHelp;;
"-u" | "uninstall" ) uninstallServer;; "-u" | "uninstall" ) uninstallServer;;
"-up" | "update" ) updateScripts "$@" ;; "-up" | "update" ) updateScripts "$@" ;;
"-wg" | "wgupdate" ) updateWireGuard ;;
"-bk" | "backup" ) backup ;; "-bk" | "backup" ) backup ;;
* ) showHelp;; * ) showHelp;;
esac esac

View file

@ -13,7 +13,7 @@ source "${setupVars}"
echo -e "::::\t\t\e[4mPiVPN debug\e[0m\t\t ::::" echo -e "::::\t\t\e[4mPiVPN debug\e[0m\t\t ::::"
printf "=============================================\n" printf "=============================================\n"
echo -e "::::\t\t\e[4mLatest commit\e[0m\t\t ::::" echo -e "::::\t\t\e[4mLatest commit\e[0m\t\t ::::"
git --git-dir /etc/.pivpn/.git log -n 1 git --git-dir /usr/local/src/pivpn/.git log -n 1
printf "=============================================\n" printf "=============================================\n"
echo -e "::::\t \e[4mInstallation settings\e[0m \t ::::" echo -e "::::\t \e[4mInstallation settings\e[0m \t ::::"
sed "s/$pivpnHOST/REDACTED/" < ${setupVars} sed "s/$pivpnHOST/REDACTED/" < ${setupVars}