Merge pull request #1060 from orazioedoardo/test

Improved dual VPN uninstallation, remove duplicate code/script
This commit is contained in:
Orazio 2020-05-29 15:21:17 +02:00 committed by GitHub
commit 2ba06af03d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 126 additions and 136 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
@ -999,6 +999,9 @@ getGitFiles(){
} }
cloneOrUpdateRepos(){ cloneOrUpdateRepos(){
# /usr/local should always exist, not sure about the src subfolder though
$SUDO mkdir -p /usr/local/src
# Get Git files # Get Git files
getGitFiles ${pivpnFilesDir} ${pivpnGitUrl} || \ getGitFiles ${pivpnFilesDir} ${pivpnGitUrl} || \
{ echo "!!! Unable to clone ${pivpnGitUrl} into ${pivpnFilesDir}, unable to continue."; \ { echo "!!! Unable to clone ${pivpnGitUrl} into ${pivpnFilesDir}, unable to continue."; \
@ -2224,43 +2227,37 @@ confUnattendedUpgrades(){
} }
installScripts(){ installScripts(){
# Install the scripts from /etc/.pivpn to their various locations # Ensure /opt exists (issue #607)
echo -n -e "::: Installing scripts to ${pivpnScriptDir}...\n" $SUDO mkdir -p /opt
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 if [[ ${VPN} == 'wireguard' ]]; then
othervpn='openvpn' othervpn='openvpn'
else else
othervpn='wireguard' othervpn='wireguard'
fi fi
if [ -r "${setupConfigDir}/${othervpn}/${setupVarsFile}" ]; then # Symlink scripts from /usr/local/src/pivpn to their various locations
# both are installed echo -n -e "::: Installing scripts to ${pivpnScriptDir}...\n"
# 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 the other protocol file exists it has been installed
if [ -r "${setupConfigDir}/${othervpn}/${setupVarsFile}" ]; then if [ -r "${setupConfigDir}/${othervpn}/${setupVarsFile}" ]; then
# both are installed, no bash completion, delete if already there # Both are installed, no bash completion, unlink if already there
$SUDO rm -f /etc/bash_completion.d/pivpn $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 else
# only one protocol is installed, put bash completion in place # Only one protocol is installed, symlink bash completion, the pivpn script
$SUDO cp "${pivpnFilesDir}/scripts/${VPN}/bash-completion" /etc/bash_completion.d/pivpn # and the script directory
$SUDO chown root:root /etc/bash_completion.d/pivpn $SUDO ln -s -T "${pivpnFilesDir}/scripts/${VPN}/bash-completion" /etc/bash_completion.d/pivpn
$SUDO chmod 755 /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 # shellcheck disable=SC1091
. /etc/bash_completion.d/pivpn . /etc/bash_completion.d/pivpn
fi 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) PKG_MANAGER="apt-get"
UPDATE_PKG_CACHE="${PKG_MANAGER} update"
dnsmasqConfig="/etc/dnsmasq.d/02-pivpn.conf"
setupVarsFile="setupVars.conf"
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 VPNChooseOptions=(WireGuard "" on
OpenVPN "" off) OpenVPN "" off)
if VPN=$("${chooseVPNCmd[@]}" "${VPNChooseOptions[@]}" 2>&1 >/dev/tty) ; then if VPN=$("${chooseVPNCmd[@]}" "${VPNChooseOptions[@]}" 2>&1 >/dev/tty) ; then
echo "::: Using VPN: $VPN" echo "::: Uninstalling VPN: $VPN"
VPN="${VPN,,}" VPN="${VPN,,}"
else else
echo "::: Cancel selected, exiting...." echo "::: Cancel selected, exiting...."
exit 1 exit 1
fi fi
fi
PKG_MANAGER="apt-get" setupVars="${setupConfigDir}/${VPN}/${setupVarsFile}"
UPDATE_PKG_CACHE="${PKG_MANAGER} update" else
dnsmasqConfig="/etc/dnsmasq.d/02-pivpn.conf" vpnStillExists=0
setupConfigDir="/etc/pivpn"
setupVarsFile="setupVars.conf" if [ -r "${setupConfigDir}/wireguard/${setupVarsFile}" ]; then
setupVars="${setupConfigDir}/${VPN}/${setupVarsFile}" 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,22 +121,8 @@ 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
@ -221,19 +205,33 @@ 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}"
unlink /usr/local/bin/pivpn
else else
if [[ ${VPN} == 'wireguard' ]]; then
othervpn='openvpn'
else
othervpn='wireguard'
fi
echo ":::" echo ":::"
echo "::: Other protocol still present, so not" echo "::: Other VPN ${othervpn} still present, so not"
echo "::: removing pivpn system files" echo "::: removing pivpn system files"
rm -f "${setupConfigDir}/${VPN}/${setupVarsFile}" 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 fi
echo ":::" echo ":::"

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}