mirror of
https://github.com/pivpn/pivpn.git
synced 2025-04-25 08:40:10 +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
|
@ -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 ":::"
|
||||
|
@ -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}"
|
||||
|
|
|
@ -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}"
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue