refactor(scripts): Structure code as per codestyle

Fix #1636
Refactor code according to code style
Constants, Functions, Script
This commit is contained in:
4s3ti 2022-10-08 01:11:10 +02:00
parent 79f7caf4d3
commit 71f7ca9b3b
No known key found for this signature in database
GPG key ID: AC2D3B898F96BC51
20 changed files with 377 additions and 342 deletions

View file

@ -1,15 +1,12 @@
#!/bin/bash #!/bin/bash
# PiVPN: Backup Script # PiVPN: Backup Script
### Constants
# 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}')"
columns="$(echo "${screen_size}" | awk '{print $2}')" 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. # Divide by two so the dialogs take up half of the screen, which looks nice.
r=$((rows / 2)) r=$((rows / 2))
c=$((columns / 2)) c=$((columns / 2))
@ -24,6 +21,49 @@ setupConfigDir="/etc/pivpn"
CHECK_PKG_INSTALLED='dpkg-query -s' 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}" ]] \ if [[ -r "${setupConfigDir}/wireguard/${setupVarsFile}" ]] \
&& [[ -r "${setupConfigDir}/openvpn/${setupVarsFile}" ]]; then && [[ -r "${setupConfigDir}/openvpn/${setupVarsFile}" ]]; then
# Two protocols have been installed, check if the script has passed # 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' CHECK_PKG_INSTALLED='apk --no-cache info -e'
fi 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 [[ "${EUID}" -ne 0 ]]; then
if ${CHECK_PKG_INSTALLED} sudo &> /dev/null; then if ${CHECK_PKG_INSTALLED} sudo &> /dev/null; then
export SUDO="sudo" export SUDO="sudo"

View file

@ -3,11 +3,6 @@
STATUS_LOG="/var/log/openvpn-status.log" STATUS_LOG="/var/log/openvpn-status.log"
if [[ ! -f "${STATUS_LOG}" ]]; then
err "The file: ${STATUS_LOG} was not found!"
exit 1
fi
err() { err() {
echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')]: $*" >&2 echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')]: $*" >&2
} }
@ -72,6 +67,11 @@ listClients() {
} | column -t -s $'\t' } | column -t -s $'\t'
} }
if [[ ! -f "${STATUS_LOG}" ]]; then
err "The file: ${STATUS_LOG} was not found!"
exit 1
fi
if [[ "$#" -eq 0 ]]; then if [[ "$#" -eq 0 ]]; then
HR=1 HR=1
listClients listClients

View file

@ -4,6 +4,7 @@
# Clean up Escape Seq -- psgoundar # Clean up Escape Seq -- psgoundar
INDEX="/etc/openvpn/easy-rsa/pki/index.txt" INDEX="/etc/openvpn/easy-rsa/pki/index.txt"
EASYRSA="/etc/openvpn/easy-rsa/easyrsa"
err() { err() {
echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')]: $*" >&2 echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')]: $*" >&2
@ -14,8 +15,6 @@ if [[ ! -f "${INDEX}" ]]; then
exit 1 exit 1
fi fi
EASYRSA="/etc/openvpn/easy-rsa/easyrsa"
if [[ ! -f "${EASYRSA}" ]]; then if [[ ! -f "${EASYRSA}" ]]; then
err "The file: ${EASYRSA} was not found!" err "The file: ${EASYRSA} was not found!"
exit 1 exit 1

View file

@ -1,7 +1,7 @@
#!/bin/bash #!/bin/bash
# Create OVPN Client # Create OVPN Client
# Default Variable Declarations
### Constants
setupVars="/etc/pivpn/openvpn/setupVars.conf" setupVars="/etc/pivpn/openvpn/setupVars.conf"
DEFAULT="Default.txt" DEFAULT="Default.txt"
FILEEXT=".ovpn" FILEEXT=".ovpn"
@ -14,15 +14,11 @@ INDEX="/etc/openvpn/easy-rsa/pki/index.txt"
# shellcheck disable=SC1090 # shellcheck disable=SC1090
source "${setupVars}" source "${setupVars}"
## Functions
err() { err() {
echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')]: $*" >&2 echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')]: $*" >&2
} }
if [[ ! -f "${setupVars}" ]]; then
err "::: Missing setup vars file!"
exit 1
fi
helpFunc() { helpFunc() {
echo "::: Create a client ovpn profile, optional nopass" echo "::: Create a client ovpn profile, optional nopass"
echo ":::" echo ":::"
@ -67,7 +63,117 @@ checkName() {
err "::: You cannot leave the name blank." err "::: You cannot leave the name blank."
exit 1 exit 1
fi 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 if [[ -z "${HELP_SHOWN}" ]]; then
helpFunc helpFunc
@ -175,102 +281,6 @@ while [[ "$#" -gt 0 ]]; do
shift shift
done 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 #make sure ovpns dir exists
# Disabling warning for SC2154, var sourced externaly # Disabling warning for SC2154, var sourced externaly
# shellcheck disable=SC2154 # shellcheck disable=SC2154
@ -456,16 +466,6 @@ if [[ "${iOS}" == 1 ]]; then
printf "========================================================\n\n" printf "========================================================\n\n"
fi 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 #disabling SC2514, variable sourced externaly
# shellcheck disable=SC2154 # shellcheck disable=SC2154
NET_REDUCED="${pivpnNET::-2}" NET_REDUCED="${pivpnNET::-2}"

View file

@ -1,24 +1,16 @@
#!/bin/bash #!/bin/bash
### Constants
CHECK_PKG_INSTALLED='dpkg-query -s' CHECK_PKG_INSTALLED='dpkg-query -s'
if grep -qsEe "^NAME\=['\"]?Alpine[a-zA-Z ]*['\"]?$" /etc/os-release; then if grep -qsEe "^NAME\=['\"]?Alpine[a-zA-Z ]*['\"]?$" /etc/os-release; then
CHECK_PKG_INSTALLED='apk --no-cache info -e' CHECK_PKG_INSTALLED='apk --no-cache info -e'
fi 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" scriptDir="/opt/pivpn"
vpn="openvpn" vpn="openvpn"
### Functions
err() { err() {
echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')]: $*" >&2 echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')]: $*" >&2
} }
@ -92,6 +84,16 @@ helpFunc() {
exit 0 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 if [[ "$#" == 0 ]]; then
helpFunc helpFunc
fi fi

View file

@ -1,20 +1,22 @@
#!/bin/bash #!/bin/bash
# This scripts runs as root # This scripts runs as root
### Contants
setupVars="/etc/pivpn/openvpn/setupVars.conf" setupVars="/etc/pivpn/openvpn/setupVars.conf"
# shellcheck disable=SC1090
source "${setupVars}"
### Functions
err() { err() {
echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')]: $*" >&2 echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')]: $*" >&2
} }
### Script
if [[ ! -f "${setupVars}" ]]; then if [[ ! -f "${setupVars}" ]]; then
err "::: Missing setup vars file!" err "::: Missing setup vars file!"
exit 1 exit 1
fi fi
# shellcheck disable=SC1090
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 ::::"

View file

@ -1,17 +1,13 @@
#!/bin/bash #!/bin/bash
# PiVPN: revoke client script # PiVPN: revoke client script
### Constants
setupVars="/etc/pivpn/openvpn/setupVars.conf" setupVars="/etc/pivpn/openvpn/setupVars.conf"
INDEX="/etc/openvpn/easy-rsa/pki/index.txt" INDEX="/etc/openvpn/easy-rsa/pki/index.txt"
if [[ ! -f "${setupVars}" ]]; then
err "::: Missing setup vars file!"
exit 1
fi
# shellcheck disable=SC1090 # shellcheck disable=SC1090
source "${setupVars}" source "${setupVars}"
### Functions
err() { err() {
echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')]: $*" >&2 echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')]: $*" >&2
} }
@ -29,6 +25,12 @@ helpFunc() {
echo "::: -h,--help Show this help dialog" echo "::: -h,--help Show this help dialog"
} }
### Script
if [[ ! -f "${setupVars}" ]]; then
err "::: Missing setup vars file!"
exit 1
fi
# Parse input arguments # Parse input arguments
while [[ "$#" -gt 0 ]]; do while [[ "$#" -gt 0 ]]; do
_key="${1}" _key="${1}"

View file

@ -1,27 +1,14 @@
#!/bin/bash #!/bin/bash
### Constants
CHECK_PKG_INSTALLED='dpkg-query -s' CHECK_PKG_INSTALLED='dpkg-query -s'
scriptDir="/opt/pivpn"
### Functions
err() { err() {
echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')]: $*" >&2 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() { uninstallServer() {
${SUDO} "${scriptDir}/uninstall.sh" ${SUDO} "${scriptDir}/uninstall.sh"
exit "${?}" exit "${?}"
@ -44,6 +31,21 @@ showHelp() {
exit 0 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 if [[ "$#" == 0 ]]; then
showHelp showHelp
fi fi

View file

@ -1,5 +1,6 @@
#!/bin/bash #!/bin/bash
### Constants
PLAT="$(grep -sEe '^NAME\=' /etc/os-release \ PLAT="$(grep -sEe '^NAME\=' /etc/os-release \
| sed -E -e "s/NAME\=[\'\"]?([^ ]*).*/\1/")" | sed -E -e "s/NAME\=[\'\"]?([^ ]*).*/\1/")"
@ -8,10 +9,12 @@ VPN="${1}"
setupVars="/etc/pivpn/${VPN}/setupVars.conf" setupVars="/etc/pivpn/${VPN}/setupVars.conf"
ERR=0 ERR=0
### Functions
err() { err() {
echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')]: $*" >&2 echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')]: $*" >&2
} }
### Script
if [[ ! -f "${setupVars}" ]]; then if [[ ! -f "${setupVars}" ]]; then
err "::: Missing setup vars file!" err "::: Missing setup vars file!"
exit 1 exit 1

View file

@ -1,11 +1,7 @@
#!/bin/bash #!/bin/bash
# PiVPN: Uninstall Script # PiVPN: Uninstall Script
### FIXME: ### Constants
### global: config storage, refactor all scripts to adhere to the storage
### FIXME:
### use variables where appropriate, reduce magic numbers by 99.9%, at least.
# 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}')"
@ -25,70 +21,15 @@ setupVarsFile="setupVars.conf"
setupConfigDir="/etc/pivpn" setupConfigDir="/etc/pivpn"
pivpnFilesDir="/usr/local/src/pivpn" pivpnFilesDir="/usr/local/src/pivpn"
pivpnScriptDir="/opt/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() { err() {
echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')]: $*" >&2 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 ### FIXME: introduce global lib
spinner() { spinner() {
local pid="${1}" local pid="${1}"
@ -391,7 +332,61 @@ askreboot() {
fi 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 -n "::: Preparing to remove packages, be sure that each may be safely "
echo "removed depending on your operating system." echo "removed depending on your operating system."
echo "::: (SAFE TO REMOVE ALL ON RASPBIAN)" echo "::: (SAFE TO REMOVE ALL ON RASPBIAN)"

View file

@ -1,7 +1,18 @@
#!/bin/bash #!/bin/bash
### Updates pivpn scripts (Not PiVPN) ### 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" pivpnrepo="https://github.com/pivpn/pivpn.git"
pivpnlocalpath="/etc/.pivpn" pivpnlocalpath="/etc/.pivpn"
pivpnscripts="/opt/pivpn/" pivpnscripts="/opt/pivpn/"
@ -19,18 +30,6 @@ c=$((columns / 2))
r=$((r < 20 ? 20 : r)) r=$((r < 20 ? 20 : r))
c=$((c < 70 ? 70 : c)) 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 chooseVPNCmd=(whiptail
--backtitle "Setup PiVPN" --backtitle "Setup PiVPN"
--title "Installation mode" --title "Installation mode"
@ -50,14 +49,10 @@ fi
setupVars="/etc/pivpn/${VPN}/setupVars.conf" setupVars="/etc/pivpn/${VPN}/setupVars.conf"
if [[ ! -f "${setupVars}" ]]; then
err "::: Missing setup vars file!"
exit 1
fi
# shellcheck disable=SC1090 # shellcheck disable=SC1090
source "${setupVars}" source "${setupVars}"
### Functions
# TODO: Uncomment this function when the updating functionality # TODO: Uncomment this function when the updating functionality
# will be re-enabled # will be re-enabled
#err() { #err() {
@ -75,8 +70,6 @@ scriptusage() {
echo "::: -h, help Show this usage dialog" echo "::: -h, help Show this usage dialog"
} }
### Functions
## Updates scripts
updatepivpnscripts() { updatepivpnscripts() {
local branch local branch
branch="${1}" branch="${1}"
@ -126,6 +119,10 @@ cloneandupdate() {
} }
## SCRIPT ## SCRIPT
if [[ ! -f "${setupVars}" ]]; then
err "::: Missing setup vars file!"
exit 1
fi
if [[ "$#" -eq 0 ]]; then if [[ "$#" -eq 0 ]]; then
updatepivpnscripts updatepivpnscripts

View file

@ -1,17 +1,14 @@
#!/bin/bash #!/bin/bash
# PiVPN: client status script # PiVPN: client status script
### Constants
CLIENTS_FILE="/etc/wireguard/configs/clients.txt" CLIENTS_FILE="/etc/wireguard/configs/clients.txt"
### Functions
err() { err() {
echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')]: $*" >&2 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() { scriptusage() {
echo "::: List any connected clients to the server" echo "::: List any connected clients to the server"
echo ":::" echo ":::"
@ -83,6 +80,12 @@ listClients() {
grep '\[disabled\] ### begin' wg0.conf | sed 's/#//g; s/begin//' 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 if [[ "$#" -eq 0 ]]; then
HR=1 HR=1
listClients listClients

View file

@ -1,19 +1,15 @@
#!/bin/bash #!/bin/bash
### Constants
setupVars="/etc/pivpn/wireguard/setupVars.conf" setupVars="/etc/pivpn/wireguard/setupVars.conf"
# shellcheck disable=SC1090
source "${setupVars}"
### Funcions
err() { err() {
echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')]: $*" >&2 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() { helpFunc() {
echo "::: Disable client conf profiles" echo "::: Disable client conf profiles"
echo ":::" echo ":::"
@ -28,6 +24,12 @@ helpFunc() {
echo "::: -h,--help Show this help dialog" echo "::: -h,--help Show this help dialog"
} }
### Script
if [[ ! -f "${setupVars}" ]]; then
err "::: Missing setup vars file!"
exit 1
fi
# Parse input arguments # Parse input arguments
while [[ "$#" -gt 0 ]]; do while [[ "$#" -gt 0 ]]; do
_key="${1}" _key="${1}"

View file

@ -1,19 +1,16 @@
#!/bin/bash #!/bin/bash
### Constants
setupVars="/etc/pivpn/wireguard/setupVars.conf" 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 # shellcheck disable=SC1090
source "${setupVars}" source "${setupVars}"
### Functions
err() {
echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')]: $*" >&2
}
helpFunc() { helpFunc() {
echo "::: Enables client conf profiles" echo "::: Enables client conf profiles"
echo ":::" echo ":::"
@ -28,6 +25,12 @@ helpFunc() {
echo "::: -h,--help Show this help dialog" echo "::: -h,--help Show this help dialog"
} }
### Script
if [[ ! -f "${setupVars}" ]]; then
err "::: Missing setup vars file!"
exit 1
fi
# Parse input arguments # Parse input arguments
while [[ "$#" -gt 0 ]]; do while [[ "$#" -gt 0 ]]; do
_key="${1}" _key="${1}"

View file

@ -1,9 +1,12 @@
#!/bin/bash #!/bin/bash
### Constants
### Funcions
err() { err() {
echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')]: $*" >&2 echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')]: $*" >&2
} }
### Script
cd /etc/wireguard/configs || exit cd /etc/wireguard/configs || exit
if [[ ! -s clients.txt ]]; then if [[ ! -s clients.txt ]]; then

View file

@ -1,5 +1,6 @@
#!/bin/bash #!/bin/bash
### Constantss
# Some vars that might be empty but need to be defined for checks # Some vars that might be empty but need to be defined for checks
pivpnPERSISTENTKEEPALIVE="" pivpnPERSISTENTKEEPALIVE=""
pivpnDNS2="" pivpnDNS2=""
@ -8,18 +9,14 @@ setupVars="/etc/pivpn/wireguard/setupVars.conf"
# shellcheck disable=SC2154 # shellcheck disable=SC2154
userGroup="${install_user}:${install_user}" userGroup="${install_user}:${install_user}"
# shellcheck disable=SC1090
source "${setupVars}"
### Functions
err() { err() {
echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')]: $*" >&2 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() { helpFunc() {
echo "::: Create a client conf profile" echo "::: Create a client conf profile"
echo ":::" echo ":::"
@ -55,7 +52,13 @@ checkName() {
err "::: A client with this name already exists" err "::: A client with this name already exists"
exit 1 exit 1
fi fi
} }
### Script
if [[ ! -f "${setupVars}" ]]; then
err "::: Missing setup vars file!"
exit 1
fi
# Parse input arguments # Parse input arguments
while [[ "$#" -gt 0 ]]; do while [[ "$#" -gt 0 ]]; do

View file

@ -1,24 +1,16 @@
#!/bin/bash #!/bin/bash
### Constants
CHECK_PKG_INSTALLED='dpkg-query -s' CHECK_PKG_INSTALLED='dpkg-query -s'
scriptdir="/opt/pivpn"
vpn="wireguard"
if grep -qsEe "^NAME\=['\"]?Alpine[a-zA-Z ]*['\"]?$" /etc/os-release; then if grep -qsEe "^NAME\=['\"]?Alpine[a-zA-Z ]*['\"]?$" /etc/os-release; then
CHECK_PKG_INSTALLED='apk --no-cache info -e' CHECK_PKG_INSTALLED='apk --no-cache info -e'
fi fi
# Must be root to use this tool ### Functions
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"
err() { err() {
echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')]: $*" >&2 echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')]: $*" >&2
} }
@ -114,6 +106,17 @@ showHelp() {
exit 0 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 if [[ "$#" == 0 ]]; then
showHelp showHelp
fi fi

View file

@ -1,20 +1,26 @@
#!/bin/bash #!/bin/bash
# This scripts runs as root
### Constants
setupVars="/etc/pivpn/wireguard/setupVars.conf" setupVars="/etc/pivpn/wireguard/setupVars.conf"
# shellcheck disable=SC1090
source "${setupVars}"
### Funcions
err() { err() {
echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')]: $*" >&2 echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')]: $*" >&2
} }
### Script
# This scripts runs as root
if [[ ! -f "${setupVars}" ]]; then if [[ ! -f "${setupVars}" ]]; then
err "::: Missing setup vars file!" err "::: Missing setup vars file!"
exit 1 exit 1
fi fi
# shellcheck disable=SC1090
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 ::::"

View file

@ -1,5 +1,9 @@
#!/bin/bash #!/bin/bash
### Constants
encoding="ansiutf8"
### Functions
err() { err() {
echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')]: $*" >&2 echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')]: $*" >&2
} }
@ -18,8 +22,8 @@ helpFunc() {
echo "::: -h,--help Show this help dialog" echo "::: -h,--help Show this help dialog"
} }
### Script
# Parse input arguments # Parse input arguments
encoding="ansiutf8"
while [[ "$#" -gt 0 ]]; do while [[ "$#" -gt 0 ]]; do
_key="${1}" _key="${1}"

View file

@ -1,19 +1,16 @@
#!/bin/bash #!/bin/bash
### Constants
setupVars="/etc/pivpn/wireguard/setupVars.conf" 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 # shellcheck disable=SC1090
source "${setupVars}" source "${setupVars}"
### Functions
err() {
echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')]: $*" >&2
}
helpFunc() { helpFunc() {
echo "::: Remove a client conf profile" echo "::: Remove a client conf profile"
echo ":::" echo ":::"
@ -27,6 +24,12 @@ helpFunc() {
echo "::: -h,--help Show this help dialog" echo "::: -h,--help Show this help dialog"
} }
### Script
if [[ ! -f "${setupVars}" ]]; then
err "::: Missing setup vars file!"
exit 1
fi
# Parse input arguments # Parse input arguments
while [[ "$#" -gt 0 ]]; do while [[ "$#" -gt 0 ]]; do
_key="${1}" _key="${1}"