From 71f7ca9b3b927b440fd424cfa9a5bf78943b5e04 Mon Sep 17 00:00:00 2001 From: 4s3ti <4s3ti@4s3ti.net> Date: Sat, 8 Oct 2022 01:11:10 +0200 Subject: [PATCH] refactor(scripts): Structure code as per codestyle Fix #1636 Refactor code according to code style Constants, Functions, Script --- scripts/backup.sh | 85 ++++++------ scripts/openvpn/clientStat.sh | 10 +- scripts/openvpn/listOVPN.sh | 3 +- scripts/openvpn/makeOVPN.sh | 228 +++++++++++++++---------------- scripts/openvpn/pivpn.sh | 22 +-- scripts/openvpn/pivpnDebug.sh | 10 +- scripts/openvpn/removeOVPN.sh | 14 +- scripts/pivpn | 34 ++--- scripts/self_check.sh | 3 + scripts/uninstall.sh | 125 ++++++++--------- scripts/update.sh | 39 +++--- scripts/wireguard/clientSTAT.sh | 13 +- scripts/wireguard/disableCONF.sh | 18 +-- scripts/wireguard/enableCONF.sh | 21 +-- scripts/wireguard/listCONF.sh | 3 + scripts/wireguard/makeCONF.sh | 21 +-- scripts/wireguard/pivpn.sh | 29 ++-- scripts/wireguard/pivpnDEBUG.sh | 14 +- scripts/wireguard/qrcodeCONF.sh | 6 +- scripts/wireguard/removeCONF.sh | 21 +-- 20 files changed, 377 insertions(+), 342 deletions(-) diff --git a/scripts/backup.sh b/scripts/backup.sh index 23c2377..7f7f61f 100755 --- a/scripts/backup.sh +++ b/scripts/backup.sh @@ -1,15 +1,12 @@ #!/bin/bash # PiVPN: Backup Script +### Constants # 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)" rows="$(echo "${screen_size}" | awk '{print $1}')" columns="$(echo "${screen_size}" | awk '{print $2}')" -err() { - echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')]: $*" >&2 -} - # Divide by two so the dialogs take up half of the screen, which looks nice. r=$((rows / 2)) c=$((columns / 2)) @@ -24,6 +21,49 @@ setupConfigDir="/etc/pivpn" CHECK_PKG_INSTALLED='dpkg-query -s' +### Functions +err() { + echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')]: $*" >&2 +} + +checkbackupdir() { + # Disabling shellcheck error $install_home sourced from $setupVars + # shellcheck disable=SC2154 + mkdir -p "${install_home}/${backupdir}" +} + +backup_openvpn() { + openvpndir=/etc/openvpn + ovpnsdir="${install_home}/ovpns" + backupzip="${date}-pivpnovpnbackup.tgz" + + checkbackupdir + # shellcheck disable=SC2210 + tar czpf "${install_home}/${backupdir}/${backupzip}" "${openvpndir}" \ + "${ovpnsdir}" > /dev/null 2>&1 + + echo -e "Backup created in ${install_home}/${backupdir}/${backupzip} " + echo -e "To restore the backup, follow instructions at:" + echo -ne "https://docs.pivpn.io/openvpn/" + echo -e "#migrating-pivpn-openvpn" +} + +backup_wireguard() { + wireguarddir=/etc/wireguard + configsdir="${install_home}/configs" + backupzip="${date}-pivpnwgbackup.tgz" + + checkbackupdir + tar czpf "${install_home}/${backupdir}/${backupzip}" "${wireguarddir}" \ + "${configsdir}" > /dev/null 2>&1 + + echo -e "Backup created in ${install_home}/${backupdir}/${backupzip} " + echo -e "To restore the backup, follow instructions at:" + echo -ne "https://docs.pivpn.io/openvpn/" + echo -e "wireguard/#migrating-pivpn-wireguard" +} + +### Script if [[ -r "${setupConfigDir}/wireguard/${setupVarsFile}" ]] \ && [[ -r "${setupConfigDir}/openvpn/${setupVarsFile}" ]]; then # Two protocols have been installed, check if the script has passed @@ -73,43 +113,6 @@ if [[ "${PLAT}" == 'Alpine' ]]; then CHECK_PKG_INSTALLED='apk --no-cache info -e' fi -checkbackupdir() { - # Disabling shellcheck error $install_home sourced from $setupVars - # shellcheck disable=SC2154 - mkdir -p "${install_home}/${backupdir}" -} - -backup_openvpn() { - openvpndir=/etc/openvpn - ovpnsdir="${install_home}/ovpns" - backupzip="${date}-pivpnovpnbackup.tgz" - - checkbackupdir - # shellcheck disable=SC2210 - tar czpf "${install_home}/${backupdir}/${backupzip}" "${openvpndir}" \ - "${ovpnsdir}" > /dev/null 2>&1 - - echo -e "Backup created in ${install_home}/${backupdir}/${backupzip} " - echo -e "To restore the backup, follow instructions at:" - echo -ne "https://docs.pivpn.io/openvpn/" - echo -e "#migrating-pivpn-openvpn" -} - -backup_wireguard() { - wireguarddir=/etc/wireguard - configsdir="${install_home}/configs" - backupzip="${date}-pivpnwgbackup.tgz" - - checkbackupdir - tar czpf "${install_home}/${backupdir}/${backupzip}" "${wireguarddir}" \ - "${configsdir}" > /dev/null 2>&1 - - echo -e "Backup created in ${install_home}/${backupdir}/${backupzip} " - echo -e "To restore the backup, follow instructions at:" - echo -ne "https://docs.pivpn.io/openvpn/" - echo -e "wireguard/#migrating-pivpn-wireguard" -} - if [[ "${EUID}" -ne 0 ]]; then if ${CHECK_PKG_INSTALLED} sudo &> /dev/null; then export SUDO="sudo" diff --git a/scripts/openvpn/clientStat.sh b/scripts/openvpn/clientStat.sh index b2da6f8..e7f6827 100755 --- a/scripts/openvpn/clientStat.sh +++ b/scripts/openvpn/clientStat.sh @@ -3,11 +3,6 @@ STATUS_LOG="/var/log/openvpn-status.log" -if [[ ! -f "${STATUS_LOG}" ]]; then - err "The file: ${STATUS_LOG} was not found!" - exit 1 -fi - err() { echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')]: $*" >&2 } @@ -72,6 +67,11 @@ listClients() { } | column -t -s $'\t' } +if [[ ! -f "${STATUS_LOG}" ]]; then + err "The file: ${STATUS_LOG} was not found!" + exit 1 +fi + if [[ "$#" -eq 0 ]]; then HR=1 listClients diff --git a/scripts/openvpn/listOVPN.sh b/scripts/openvpn/listOVPN.sh index ccba5d3..96af300 100755 --- a/scripts/openvpn/listOVPN.sh +++ b/scripts/openvpn/listOVPN.sh @@ -4,6 +4,7 @@ # Clean up Escape Seq -- psgoundar INDEX="/etc/openvpn/easy-rsa/pki/index.txt" +EASYRSA="/etc/openvpn/easy-rsa/easyrsa" err() { echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')]: $*" >&2 @@ -14,8 +15,6 @@ if [[ ! -f "${INDEX}" ]]; then exit 1 fi -EASYRSA="/etc/openvpn/easy-rsa/easyrsa" - if [[ ! -f "${EASYRSA}" ]]; then err "The file: ${EASYRSA} was not found!" exit 1 diff --git a/scripts/openvpn/makeOVPN.sh b/scripts/openvpn/makeOVPN.sh index dc27999..811f88e 100755 --- a/scripts/openvpn/makeOVPN.sh +++ b/scripts/openvpn/makeOVPN.sh @@ -1,7 +1,7 @@ #!/bin/bash - # Create OVPN Client -# Default Variable Declarations + +### Constants setupVars="/etc/pivpn/openvpn/setupVars.conf" DEFAULT="Default.txt" FILEEXT=".ovpn" @@ -14,15 +14,11 @@ INDEX="/etc/openvpn/easy-rsa/pki/index.txt" # shellcheck disable=SC1090 source "${setupVars}" +## Functions err() { echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')]: $*" >&2 } -if [[ ! -f "${setupVars}" ]]; then - err "::: Missing setup vars file!" - exit 1 -fi - helpFunc() { echo "::: Create a client ovpn profile, optional nopass" echo ":::" @@ -67,7 +63,117 @@ checkName() { err "::: You cannot leave the name blank." exit 1 fi -} +} + +keynoPASS() { + # Build the client key + export EASYRSA_CERT_EXPIRE="${DAYS}" + ./easyrsa build-client-full "${NAME}" nopass + cd pki || exit +} + +useBitwarden() { + # login and unlock vault + printf "****Bitwarden Login****" + printf "\n" + + SESSION_KEY="$(bw login --raw)" + export BW_SESSION="${SESSION_KEY}" + + printf "Successfully Logged in!" + printf "\n" + + # ask user for username + printf "Enter the username: " + read -r NAME + + #check name + checkName + + # ask user for length of password + printf "Please enter the length of characters you want your password to be " + printf "(minimum 12): " + read -r LENGTH + + # check length + until [[ "${LENGTH}" -gt 11 ]] && [[ "${LENGTH}" -lt 129 ]]; do + echo "Password must be between from 12 to 128 characters, please try again." + # ask user for length of password + printf "Please enter the length of characters you want your password to be " + printf "(minimum 12): " + read -r LENGTH + done + + printf "Creating a PiVPN item for your vault..." + printf "\n" + + # create a new item for your PiVPN Password + PASSWD="$(bw generate -usln --length "${LENGTH}")" + bw get template item \ + | jq '.login.type = "1"' \ + | jq '.name = "PiVPN"' \ + | jq -r --arg NAME "${NAME}" '.login.username = $NAME' \ + | jq -r --arg PASSWD "${PASSWD}" '.login.password = $PASSWD' \ + | bw encode \ + | bw create item + bw logout +} + +keyPASS() { + if [[ -z "${PASSWD}" ]]; then + stty -echo + + while true; do + printf "Enter the password for the client: " + read -r PASSWD + printf "\n" + printf "Enter the password again to verify: " + read -r PASSWD2 + printf "\n" + + [[ "${PASSWD}" == "${PASSWD2}" ]] && break + + printf "Passwords do not match! Please try again.\n" + done + + stty echo + + if [[ -z "${PASSWD}" ]]; then + err "You left the password blank" + err "If you don't want a password, please run:" + err "pivpn add nopass" + exit 1 + fi + fi + + if [[ "${#PASSWD}" -lt 4 ]] || [[ "${#PASSWD}" -gt 1024 ]]; then + err "Password must be between from 4 to 1024 characters" + exit 1 + fi + + export EASYRSA_CERT_EXPIRE="${DAYS}" + ./easyrsa --passin=pass:"${PASSWD}" \ + --passout=pass:"${PASSWD}" \ + build-client-full "${NAME}" + + cd pki || exit +} + +cidrToMask() { + # Source: https://stackoverflow.com/a/20767392 + set -- $((5 - (${1} / 8))) \ + 255 255 255 255 \ + $(((255 << (8 - (${1} % 8))) & 255)) \ + 0 0 0 + shift "${1}" + echo "${1-0}.${2-0}.${3-0}.${4-0}" +} + +### Script +if [[ ! -f "${setupVars}" ]]; then + err "::: Missing setup vars file!" + exit 1 +fi if [[ -z "${HELP_SHOWN}" ]]; then helpFunc @@ -175,102 +281,6 @@ while [[ "$#" -gt 0 ]]; do shift done -# Functions def - -keynoPASS() { - # Build the client key - export EASYRSA_CERT_EXPIRE="${DAYS}" - ./easyrsa build-client-full "${NAME}" nopass - cd pki || exit -} - -useBitwarden() { - # login and unlock vault - printf "****Bitwarden Login****" - printf "\n" - - SESSION_KEY="$(bw login --raw)" - export BW_SESSION="${SESSION_KEY}" - - printf "Successfully Logged in!" - printf "\n" - - # ask user for username - printf "Enter the username: " - read -r NAME - - #check name - checkName - - # ask user for length of password - printf "Please enter the length of characters you want your password to be " - printf "(minimum 12): " - read -r LENGTH - - # check length - until [[ "${LENGTH}" -gt 11 ]] && [[ "${LENGTH}" -lt 129 ]]; do - echo "Password must be between from 12 to 128 characters, please try again." - # ask user for length of password - printf "Please enter the length of characters you want your password to be " - printf "(minimum 12): " - read -r LENGTH - done - - printf "Creating a PiVPN item for your vault..." - printf "\n" - - # create a new item for your PiVPN Password - PASSWD="$(bw generate -usln --length "${LENGTH}")" - bw get template item \ - | jq '.login.type = "1"' \ - | jq '.name = "PiVPN"' \ - | jq -r --arg NAME "${NAME}" '.login.username = $NAME' \ - | jq -r --arg PASSWD "${PASSWD}" '.login.password = $PASSWD' \ - | bw encode \ - | bw create item - bw logout -} - -keyPASS() { - if [[ -z "${PASSWD}" ]]; then - stty -echo - - while true; do - printf "Enter the password for the client: " - read -r PASSWD - printf "\n" - printf "Enter the password again to verify: " - read -r PASSWD2 - printf "\n" - - [[ "${PASSWD}" == "${PASSWD2}" ]] && break - - printf "Passwords do not match! Please try again.\n" - done - - stty echo - - if [[ -z "${PASSWD}" ]]; then - err "You left the password blank" - err "If you don't want a password, please run:" - err "pivpn add nopass" - exit 1 - fi - fi - - if [[ "${#PASSWD}" -lt 4 ]] || [[ "${#PASSWD}" -gt 1024 ]]; then - err "Password must be between from 4 to 1024 characters" - exit 1 - fi - - export EASYRSA_CERT_EXPIRE="${DAYS}" - ./easyrsa --passin=pass:"${PASSWD}" \ - --passout=pass:"${PASSWD}" \ - build-client-full "${NAME}" - - cd pki || exit -} - #make sure ovpns dir exists # Disabling warning for SC2154, var sourced externaly # shellcheck disable=SC2154 @@ -456,16 +466,6 @@ if [[ "${iOS}" == 1 ]]; then printf "========================================================\n\n" fi -cidrToMask() { - # Source: https://stackoverflow.com/a/20767392 - set -- $((5 - (${1} / 8))) \ - 255 255 255 255 \ - $(((255 << (8 - (${1} % 8))) & 255)) \ - 0 0 0 - shift "${1}" - echo "${1-0}.${2-0}.${3-0}.${4-0}" -} - #disabling SC2514, variable sourced externaly # shellcheck disable=SC2154 NET_REDUCED="${pivpnNET::-2}" diff --git a/scripts/openvpn/pivpn.sh b/scripts/openvpn/pivpn.sh index b67321a..0b85bd8 100755 --- a/scripts/openvpn/pivpn.sh +++ b/scripts/openvpn/pivpn.sh @@ -1,24 +1,16 @@ #!/bin/bash +### Constants CHECK_PKG_INSTALLED='dpkg-query -s' if grep -qsEe "^NAME\=['\"]?Alpine[a-zA-Z ]*['\"]?$" /etc/os-release; then CHECK_PKG_INSTALLED='apk --no-cache info -e' fi -# Must be root to use this tool -if [[ "${EUID}" -ne 0 ]]; then - if ${CHECK_PKG_INSTALLED} sudo &> /dev/null; then - export SUDO="sudo" - else - err "::: Please install sudo or run this as root." - exit 1 - fi -fi - scriptDir="/opt/pivpn" vpn="openvpn" +### Functions err() { echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')]: $*" >&2 } @@ -92,6 +84,16 @@ helpFunc() { exit 0 } +# Must be root to use this tool +if [[ "${EUID}" -ne 0 ]]; then + if ${CHECK_PKG_INSTALLED} sudo &> /dev/null; then + export SUDO="sudo" + else + err "::: Please install sudo or run this as root." + exit 1 + fi +fi + if [[ "$#" == 0 ]]; then helpFunc fi diff --git a/scripts/openvpn/pivpnDebug.sh b/scripts/openvpn/pivpnDebug.sh index bff2b8f..509401e 100755 --- a/scripts/openvpn/pivpnDebug.sh +++ b/scripts/openvpn/pivpnDebug.sh @@ -1,20 +1,22 @@ #!/bin/bash # This scripts runs as root - +### Contants setupVars="/etc/pivpn/openvpn/setupVars.conf" +# shellcheck disable=SC1090 +source "${setupVars}" + +### Functions err() { echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')]: $*" >&2 } +### Script if [[ ! -f "${setupVars}" ]]; then err "::: Missing setup vars file!" exit 1 fi -# shellcheck disable=SC1090 -source "${setupVars}" - echo -e "::::\t\t\e[4mPiVPN debug\e[0m\t\t ::::" printf "=============================================\n" echo -e "::::\t\t\e[4mLatest commit\e[0m\t\t ::::" diff --git a/scripts/openvpn/removeOVPN.sh b/scripts/openvpn/removeOVPN.sh index 168a53b..f6ce765 100755 --- a/scripts/openvpn/removeOVPN.sh +++ b/scripts/openvpn/removeOVPN.sh @@ -1,17 +1,13 @@ #!/bin/bash # PiVPN: revoke client script +### Constants setupVars="/etc/pivpn/openvpn/setupVars.conf" INDEX="/etc/openvpn/easy-rsa/pki/index.txt" - -if [[ ! -f "${setupVars}" ]]; then - err "::: Missing setup vars file!" - exit 1 -fi - # shellcheck disable=SC1090 source "${setupVars}" +### Functions err() { echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')]: $*" >&2 } @@ -29,6 +25,12 @@ helpFunc() { echo "::: -h,--help Show this help dialog" } +### Script +if [[ ! -f "${setupVars}" ]]; then + err "::: Missing setup vars file!" + exit 1 +fi + # Parse input arguments while [[ "$#" -gt 0 ]]; do _key="${1}" diff --git a/scripts/pivpn b/scripts/pivpn index 4f3b367..077a59b 100755 --- a/scripts/pivpn +++ b/scripts/pivpn @@ -1,27 +1,14 @@ #!/bin/bash +### Constants CHECK_PKG_INSTALLED='dpkg-query -s' +scriptDir="/opt/pivpn" +### Functions err() { echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')]: $*" >&2 } -if grep -qsEe "^NAME\=['\"]?Alpine[a-zA-Z ]*['\"]?$" /etc/os-release; then - CHECK_PKG_INSTALLED='apk --no-cache info -e' -fi - -# Must be root to use this tool -if [[ "${EUID}" -ne 0 ]]; then - if ${CHECK_PKG_INSTALLED} sudo &> /dev/null; then - export SUDO="sudo" - else - err "::: Please install sudo or run this as root." - exit 1 - fi -fi - -scriptDir="/opt/pivpn" - uninstallServer() { ${SUDO} "${scriptDir}/uninstall.sh" exit "${?}" @@ -44,6 +31,21 @@ showHelp() { exit 0 } +### Script +# Must be root to use this tool +if [[ "${EUID}" -ne 0 ]]; then + if ${CHECK_PKG_INSTALLED} sudo &> /dev/null; then + export SUDO="sudo" + else + err "::: Please install sudo or run this as root." + exit 1 + fi +fi + +if grep -qsEe "^NAME\=['\"]?Alpine[a-zA-Z ]*['\"]?$" /etc/os-release; then + CHECK_PKG_INSTALLED='apk --no-cache info -e' +fi + if [[ "$#" == 0 ]]; then showHelp fi diff --git a/scripts/self_check.sh b/scripts/self_check.sh index 6b0ed1f..28c1c2a 100755 --- a/scripts/self_check.sh +++ b/scripts/self_check.sh @@ -1,5 +1,6 @@ #!/bin/bash +### Constants PLAT="$(grep -sEe '^NAME\=' /etc/os-release \ | sed -E -e "s/NAME\=[\'\"]?([^ ]*).*/\1/")" @@ -8,10 +9,12 @@ VPN="${1}" setupVars="/etc/pivpn/${VPN}/setupVars.conf" ERR=0 +### Functions err() { echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')]: $*" >&2 } +### Script if [[ ! -f "${setupVars}" ]]; then err "::: Missing setup vars file!" exit 1 diff --git a/scripts/uninstall.sh b/scripts/uninstall.sh index af6c51f..e6e3f90 100755 --- a/scripts/uninstall.sh +++ b/scripts/uninstall.sh @@ -1,11 +1,7 @@ #!/bin/bash # PiVPN: Uninstall Script -### 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. - +### Constants # 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)" rows="$(echo "${screen_size}" | awk '{print $1}')" @@ -25,70 +21,15 @@ setupVarsFile="setupVars.conf" setupConfigDir="/etc/pivpn" pivpnFilesDir="/usr/local/src/pivpn" pivpnScriptDir="/opt/pivpn" +PLAT="$(grep -sEe '^NAME\=' /etc/os-release \ + | sed -E -e "s/NAME\=[\'\"]?([^ ]*).*/\1/")" +UPDATE_PKG_CACHE="${PKG_MANAGER} update" +### Functions err() { echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')]: $*" >&2 } -PLAT="$(grep -sEe '^NAME\=' /etc/os-release \ - | sed -E -e "s/NAME\=[\'\"]?([^ ]*).*/\1/")" - -if [[ "${PLAT}" == 'Alpine' ]]; then - PKG_MANAGER='apk' - PKG_REMOVE="${PKG_MANAGER} --no-cache --purge del -r" -fi - -UPDATE_PKG_CACHE="${PKG_MANAGER} update" - -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 - err "::: 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 - err "::: Missing setup vars file!" - exit 1 -fi - -# shellcheck disable=SC1090 -source "${setupVars}" - ### FIXME: introduce global lib spinner() { local pid="${1}" @@ -391,7 +332,61 @@ askreboot() { fi } -######### SCRIPT ########### +### Script +if [[ "${PLAT}" == 'Alpine' ]]; then + PKG_MANAGER='apk' + PKG_REMOVE="${PKG_MANAGER} --no-cache --purge del -r" +fi + +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 + err "::: 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 + err "::: Missing setup vars file!" + exit 1 +fi + +# shellcheck disable=SC1090 +source "${setupVars}" + echo -n "::: Preparing to remove packages, be sure that each may be safely " echo "removed depending on your operating system." echo "::: (SAFE TO REMOVE ALL ON RASPBIAN)" diff --git a/scripts/update.sh b/scripts/update.sh index 32709bc..f8efe39 100755 --- a/scripts/update.sh +++ b/scripts/update.sh @@ -1,7 +1,18 @@ #!/bin/bash - ### Updates pivpn scripts (Not PiVPN) -### Main Vars +# TODO: Delete this section when the updating functionality will be re-enabled +### +err() { + echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')]: $*" >&2 +} + +err "::: The updating functionality for PiVPN scripts is temporarily disabled" +err "::: To keep the VPN (and the system) up to date, use:" +err " apt update; apt upgrade" +exit 0 +### END SECTION ### + +### Constants pivpnrepo="https://github.com/pivpn/pivpn.git" pivpnlocalpath="/etc/.pivpn" pivpnscripts="/opt/pivpn/" @@ -19,18 +30,6 @@ c=$((columns / 2)) r=$((r < 20 ? 20 : r)) c=$((c < 70 ? 70 : c)) -# TODO: Delete this section when the updating functionality will be re-enabled -### -err() { - echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')]: $*" >&2 -} - -err "::: The updating functionality for PiVPN scripts is temporarily disabled" -err "::: To keep the VPN (and the system) up to date, use:" -err " apt update; apt upgrade" -exit 0 -### - chooseVPNCmd=(whiptail --backtitle "Setup PiVPN" --title "Installation mode" @@ -50,14 +49,10 @@ fi setupVars="/etc/pivpn/${VPN}/setupVars.conf" -if [[ ! -f "${setupVars}" ]]; then - err "::: Missing setup vars file!" - exit 1 -fi - # shellcheck disable=SC1090 source "${setupVars}" +### Functions # TODO: Uncomment this function when the updating functionality # will be re-enabled #err() { @@ -75,8 +70,6 @@ scriptusage() { echo "::: -h, help Show this usage dialog" } -### Functions -## Updates scripts updatepivpnscripts() { local branch branch="${1}" @@ -126,6 +119,10 @@ cloneandupdate() { } ## SCRIPT +if [[ ! -f "${setupVars}" ]]; then + err "::: Missing setup vars file!" + exit 1 +fi if [[ "$#" -eq 0 ]]; then updatepivpnscripts diff --git a/scripts/wireguard/clientSTAT.sh b/scripts/wireguard/clientSTAT.sh index f72f265..a7b4320 100755 --- a/scripts/wireguard/clientSTAT.sh +++ b/scripts/wireguard/clientSTAT.sh @@ -1,17 +1,14 @@ #!/bin/bash # PiVPN: client status script +### Constants CLIENTS_FILE="/etc/wireguard/configs/clients.txt" +### Functions err() { echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')]: $*" >&2 } -if [[ ! -s "${CLIENTS_FILE}" ]]; then - err "::: There are no clients to list" - exit 0 -fi - scriptusage() { echo "::: List any connected clients to the server" echo ":::" @@ -83,6 +80,12 @@ listClients() { grep '\[disabled\] ### begin' wg0.conf | sed 's/#//g; s/begin//' } +### Script +if [[ ! -s "${CLIENTS_FILE}" ]]; then + err "::: There are no clients to list" + exit 0 +fi + if [[ "$#" -eq 0 ]]; then HR=1 listClients diff --git a/scripts/wireguard/disableCONF.sh b/scripts/wireguard/disableCONF.sh index 8043581..bd1220c 100755 --- a/scripts/wireguard/disableCONF.sh +++ b/scripts/wireguard/disableCONF.sh @@ -1,19 +1,15 @@ #!/bin/bash +### Constants setupVars="/etc/pivpn/wireguard/setupVars.conf" +# shellcheck disable=SC1090 +source "${setupVars}" +### Funcions err() { echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')]: $*" >&2 } -if [[ ! -f "${setupVars}" ]]; then - err "::: Missing setup vars file!" - exit 1 -fi - -# shellcheck disable=SC1090 -source "${setupVars}" - helpFunc() { echo "::: Disable client conf profiles" echo ":::" @@ -28,6 +24,12 @@ helpFunc() { echo "::: -h,--help Show this help dialog" } +### Script +if [[ ! -f "${setupVars}" ]]; then + err "::: Missing setup vars file!" + exit 1 +fi + # Parse input arguments while [[ "$#" -gt 0 ]]; do _key="${1}" diff --git a/scripts/wireguard/enableCONF.sh b/scripts/wireguard/enableCONF.sh index a7176d9..9d1e555 100755 --- a/scripts/wireguard/enableCONF.sh +++ b/scripts/wireguard/enableCONF.sh @@ -1,19 +1,16 @@ #!/bin/bash +### Constants setupVars="/etc/pivpn/wireguard/setupVars.conf" -err() { - echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')]: $*" >&2 -} - -if [[ ! -f "${setupVars}" ]]; then - err "::: Missing setup vars file!" - exit 1 -fi - # shellcheck disable=SC1090 source "${setupVars}" +### Functions +err() { + echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')]: $*" >&2 +} + helpFunc() { echo "::: Enables client conf profiles" echo ":::" @@ -28,6 +25,12 @@ helpFunc() { echo "::: -h,--help Show this help dialog" } +### Script +if [[ ! -f "${setupVars}" ]]; then + err "::: Missing setup vars file!" + exit 1 +fi + # Parse input arguments while [[ "$#" -gt 0 ]]; do _key="${1}" diff --git a/scripts/wireguard/listCONF.sh b/scripts/wireguard/listCONF.sh index da37b87..3da53f8 100755 --- a/scripts/wireguard/listCONF.sh +++ b/scripts/wireguard/listCONF.sh @@ -1,9 +1,12 @@ #!/bin/bash +### Constants +### Funcions err() { echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')]: $*" >&2 } +### Script cd /etc/wireguard/configs || exit if [[ ! -s clients.txt ]]; then diff --git a/scripts/wireguard/makeCONF.sh b/scripts/wireguard/makeCONF.sh index 627b3b8..f6488d1 100755 --- a/scripts/wireguard/makeCONF.sh +++ b/scripts/wireguard/makeCONF.sh @@ -1,5 +1,6 @@ #!/bin/bash +### Constantss # Some vars that might be empty but need to be defined for checks pivpnPERSISTENTKEEPALIVE="" pivpnDNS2="" @@ -8,18 +9,14 @@ setupVars="/etc/pivpn/wireguard/setupVars.conf" # shellcheck disable=SC2154 userGroup="${install_user}:${install_user}" +# shellcheck disable=SC1090 +source "${setupVars}" + +### Functions err() { echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')]: $*" >&2 } -if [[ ! -f "${setupVars}" ]]; then - err "::: Missing setup vars file!" - exit 1 -fi - -# shellcheck disable=SC1090 -source "${setupVars}" - helpFunc() { echo "::: Create a client conf profile" echo ":::" @@ -55,7 +52,13 @@ checkName() { err "::: A client with this name already exists" exit 1 fi -} +} + +### Script +if [[ ! -f "${setupVars}" ]]; then + err "::: Missing setup vars file!" + exit 1 +fi # Parse input arguments while [[ "$#" -gt 0 ]]; do diff --git a/scripts/wireguard/pivpn.sh b/scripts/wireguard/pivpn.sh index 894caeb..53960c3 100755 --- a/scripts/wireguard/pivpn.sh +++ b/scripts/wireguard/pivpn.sh @@ -1,24 +1,16 @@ #!/bin/bash +### Constants + CHECK_PKG_INSTALLED='dpkg-query -s' +scriptdir="/opt/pivpn" +vpn="wireguard" if grep -qsEe "^NAME\=['\"]?Alpine[a-zA-Z ]*['\"]?$" /etc/os-release; then CHECK_PKG_INSTALLED='apk --no-cache info -e' fi -# Must be root to use this tool -if [[ "${EUID}" -ne 0 ]]; then - if ${CHECK_PKG_INSTALLED} sudo &> /dev/null; then - export SUDO="sudo" - else - err "::: Please install sudo or run this as root." - exit 1 - fi -fi - -scriptdir="/opt/pivpn" -vpn="wireguard" - +### Functions err() { echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')]: $*" >&2 } @@ -114,6 +106,17 @@ showHelp() { exit 0 } +### Script +# Must be root to use this tool +if [[ "${EUID}" -ne 0 ]]; then + if ${CHECK_PKG_INSTALLED} sudo &> /dev/null; then + export SUDO="sudo" + else + err "::: Please install sudo or run this as root." + exit 1 + fi +fi + if [[ "$#" == 0 ]]; then showHelp fi diff --git a/scripts/wireguard/pivpnDEBUG.sh b/scripts/wireguard/pivpnDEBUG.sh index 28e44f3..4f77aaf 100755 --- a/scripts/wireguard/pivpnDEBUG.sh +++ b/scripts/wireguard/pivpnDEBUG.sh @@ -1,20 +1,26 @@ #!/bin/bash -# This scripts runs as root + +### Constants setupVars="/etc/pivpn/wireguard/setupVars.conf" +# shellcheck disable=SC1090 +source "${setupVars}" + +### Funcions + err() { echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')]: $*" >&2 } +### Script + +# This scripts runs as root if [[ ! -f "${setupVars}" ]]; then err "::: Missing setup vars file!" exit 1 fi -# shellcheck disable=SC1090 -source "${setupVars}" - echo -e "::::\t\t\e[4mPiVPN debug\e[0m\t\t ::::" printf "=============================================\n" echo -e "::::\t\t\e[4mLatest commit\e[0m\t\t ::::" diff --git a/scripts/wireguard/qrcodeCONF.sh b/scripts/wireguard/qrcodeCONF.sh index 1f7bbd8..e81f319 100755 --- a/scripts/wireguard/qrcodeCONF.sh +++ b/scripts/wireguard/qrcodeCONF.sh @@ -1,5 +1,9 @@ #!/bin/bash +### Constants +encoding="ansiutf8" + +### Functions err() { echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')]: $*" >&2 } @@ -18,8 +22,8 @@ helpFunc() { echo "::: -h,--help Show this help dialog" } +### Script # Parse input arguments -encoding="ansiutf8" while [[ "$#" -gt 0 ]]; do _key="${1}" diff --git a/scripts/wireguard/removeCONF.sh b/scripts/wireguard/removeCONF.sh index 56572b2..fd7e66f 100755 --- a/scripts/wireguard/removeCONF.sh +++ b/scripts/wireguard/removeCONF.sh @@ -1,19 +1,16 @@ #!/bin/bash +### Constants setupVars="/etc/pivpn/wireguard/setupVars.conf" -err() { - echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')]: $*" >&2 -} - -if [[ ! -f "${setupVars}" ]]; then - err "::: Missing setup vars file!" - exit 1 -fi - # shellcheck disable=SC1090 source "${setupVars}" +### Functions +err() { + echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')]: $*" >&2 +} + helpFunc() { echo "::: Remove a client conf profile" echo ":::" @@ -27,6 +24,12 @@ helpFunc() { echo "::: -h,--help Show this help dialog" } +### Script +if [[ ! -f "${setupVars}" ]]; then + err "::: Missing setup vars file!" + exit 1 +fi + # Parse input arguments while [[ "$#" -gt 0 ]]; do _key="${1}"