mirror of
https://github.com/pivpn/pivpn.git
synced 2024-12-19 03:10:16 +00:00
refactor(scripts): Structure code as per codestyle
Fix #1636 Refactor code according to code style Constants, Functions, Script
This commit is contained in:
parent
79f7caf4d3
commit
71f7ca9b3b
20 changed files with 377 additions and 342 deletions
|
@ -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"
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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 ":::"
|
||||
|
@ -69,6 +65,116 @@ checkName() {
|
|||
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
|
||||
echo
|
||||
|
@ -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}"
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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 ::::"
|
||||
|
|
|
@ -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}"
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)"
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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}"
|
||||
|
|
|
@ -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}"
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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 ":::"
|
||||
|
@ -57,6 +54,12 @@ checkName() {
|
|||
fi
|
||||
}
|
||||
|
||||
### Script
|
||||
if [[ ! -f "${setupVars}" ]]; then
|
||||
err "::: Missing setup vars file!"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Parse input arguments
|
||||
while [[ "$#" -gt 0 ]]; do
|
||||
_key="${1}"
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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 ::::"
|
||||
|
|
|
@ -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}"
|
||||
|
|
|
@ -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}"
|
||||
|
|
Loading…
Reference in a new issue