Main Funcion cleanup

General code cleanup
Moved loose code into funcions
Removed unecessary coments
Moved some comments to their funcions
This commit is contained in:
4s3ti 2021-11-24 22:04:33 +01:00
parent d4e3a63522
commit e458cba36a

View file

@ -34,7 +34,7 @@ PKG_INSTALL="${PKG_MANAGER} --yes --no-install-recommends install"
PKG_COUNT="${PKG_MANAGER} -s -o Debug::NoLocking=true upgrade | grep -c ^Inst || true" PKG_COUNT="${PKG_MANAGER} -s -o Debug::NoLocking=true upgrade | grep -c ^Inst || true"
# Dependencies that are required by the script, regardless of the VPN protocol chosen # Dependencies that are required by the script, regardless of the VPN protocol chosen
BASE_DEPS=(git tar curl grep dnsutils whiptail net-tools bsdmainutils) BASE_DEPS=(git tar curl grep dnsutils whiptail net-tools bsdmainutils bash-completion)
# Dependencies that where actually installed by the script. For example if the script requires # Dependencies that where actually installed by the script. For example if the script requires
# grep and dnsutils but dnsutils is already installed, we save grep here. This way when uninstalling # grep and dnsutils but dnsutils is already installed, we save grep here. This way when uninstalling
@ -72,7 +72,56 @@ export LC_ALL=C
shopt -s globstar shopt -s globstar
main(){ main(){
# Pre install checks and configs
rootCheck
flagsCheck "$@"
unattendedCheck
checkExistingInstall "$@"
distroCheck
checkHostname
# Verify there is enough disk space for the install
if [[ "${skipSpaceCheck}" == true ]]; then
echo "::: --skip-space-check passed to script, skipping free disk space verification!"
else
verifyFreeDiskSpace
fi
updatePackageCache
notifyPackageUpdatesAvailable
preconfigurePackages
installDependentPackages BASE_DEPS[@]
welcomeDialogs
chooseInterface
if [ "$PLAT" != "Raspbian" ]; then
avoidStaticIPv4Ubuntu
else
getStaticIPv4Settings
if [ -z "$dhcpReserv" ] || [ "$dhcpReserv" -ne 1 ]; then
setStaticIPv4
fi
fi
chooseUser
cloneOrUpdateRepos
# Install
if installPiVPN; then
echo "::: Install Complete..."
else
exit 1
fi
restartServices
# Ask if unattended-upgrades will be enabled
askUnattendedUpgrades
if [ "$UNATTUPG" -eq 1 ]; then
confUnattendedUpgrades
fi
writeConfigFiles
installScripts
displayFinalMessage
echo ":::"
}
####### FUNCTIONS ##########
rootCheck(){
######## FIRST CHECK ######## ######## FIRST CHECK ########
# Must be root to install # Must be root to install
echo ":::" echo ":::"
@ -90,7 +139,9 @@ main(){
exit 1 exit 1
fi fi
fi fi
}
flagsCheck(){
# Check arguments for the undocumented flags # Check arguments for the undocumented flags
for ((i=1; i <= "$#"; i++)); do for ((i=1; i <= "$#"; i++)); do
j="$((i+1))" j="$((i+1))"
@ -102,6 +153,9 @@ main(){
esac esac
done done
}
unattendedCheck(){
if [[ "${runUnattended}" == true ]]; then if [[ "${runUnattended}" == true ]]; then
echo "::: --unattended passed to install script, no whiptail dialogs will be displayed" echo "::: --unattended passed to install script, no whiptail dialogs will be displayed"
if [ -z "$unattendedConfig" ]; then if [ -z "$unattendedConfig" ]; then
@ -117,7 +171,9 @@ main(){
fi fi
fi fi
fi fi
}
checkExistingInstall(){
# see which setup already exists # see which setup already exists
if [ -r "${setupConfigDir}/wireguard/${setupVarsFile}" ]; then if [ -r "${setupConfigDir}/wireguard/${setupVarsFile}" ]; then
setupVars="${setupConfigDir}/wireguard/${setupVarsFile}" setupVars="${setupConfigDir}/wireguard/${setupVarsFile}"
@ -147,87 +203,8 @@ main(){
source "$setupVars" source "$setupVars"
runUnattended=true runUnattended=true
fi fi
# Check for supported distribution
distroCheck
# Checks for hostname Length
checkHostname
# Start the installer
# Verify there is enough disk space for the install
if [[ "${skipSpaceCheck}" == true ]]; then
echo "::: --skip-space-check passed to script, skipping free disk space verification!"
else
verifyFreeDiskSpace
fi
updatePackageCache
# Notify user of package availability
notifyPackageUpdatesAvailable
# Install packages used by this installation script
preconfigurePackages
installDependentPackages BASE_DEPS[@]
# Display welcome dialogs
welcomeDialogs
# Find interfaces and let the user choose one
chooseInterface
if [ "$PLAT" != "Raspbian" ]; then
avoidStaticIPv4Ubuntu
else
getStaticIPv4Settings
if [ -z "$dhcpReserv" ] || [ "$dhcpReserv" -ne 1 ]; then
setStaticIPv4
fi
fi
# Choose the user for the ovpns
chooseUser
# Clone/Update the repos
cloneOrUpdateRepos
# Install
if installPiVPN; then
echo "::: Install Complete..."
else
exit 1
fi
# Start services
restartServices
# Ask if unattended-upgrades will be enabled
askUnattendedUpgrades
if [ "$UNATTUPG" -eq 1 ]; then
confUnattendedUpgrades
fi
# Save installation setting to the final location
echo "INSTALLED_PACKAGES=(${INSTALLED_PACKAGES[*]})" >> ${tempsetupVarsFile}
echo "::: Setupfiles copied to ${setupConfigDir}/${VPN}/${setupVarsFile}"
$SUDO mkdir -p "${setupConfigDir}/${VPN}/"
$SUDO cp ${tempsetupVarsFile} "${setupConfigDir}/${VPN}/${setupVarsFile}"
installScripts
# Ensure that cached writes reach persistent storage
echo "::: Flushing writes to disk..."
sync
echo "::: done."
displayFinalMessage
echo ":::"
} }
####### FUNCTIONS ##########
askAboutExistingInstall(){ askAboutExistingInstall(){
opt1a="Update" opt1a="Update"
opt1b="Get the latest PiVPN scripts" opt1b="Get the latest PiVPN scripts"
@ -248,9 +225,10 @@ askAboutExistingInstall(){
} }
# Compatibility, functions to check for supported OS
# distroCheck, maybeOSSupport, noOSSupport
distroCheck(){ distroCheck(){
# Check for supported distribution
# Compatibility, functions to check for supported OS
# distroCheck, maybeOSSupport, noOSSupport
# if lsb_release command is on their system # if lsb_release command is on their system
if command -v lsb_release > /dev/null; then if command -v lsb_release > /dev/null; then
@ -322,7 +300,7 @@ Would you like to continue anyway?" ${r} ${c}) then
checkHostname(){ checkHostname(){
###Checks for hostname size # Checks for hostname Length
host_name=$(hostname -s) host_name=$(hostname -s)
if [[ ! ${#host_name} -le 28 ]]; then if [[ ! ${#host_name} -le 28 ]]; then
if [ "${runUnattended}" = 'true' ]; then if [ "${runUnattended}" = 'true' ]; then
@ -423,6 +401,7 @@ notifyPackageUpdatesAvailable(){
} }
preconfigurePackages(){ preconfigurePackages(){
# Install packages used by this installation script
# If apt is older than 1.5 we need to install an additional package to add # If apt is older than 1.5 we need to install an additional package to add
# support for https repositories that will be used later on # support for https repositories that will be used later on
if [[ -f /etc/apt/sources.list ]]; then if [[ -f /etc/apt/sources.list ]]; then
@ -519,10 +498,12 @@ preconfigurePackages(){
} }
installDependentPackages(){ installDependentPackages(){
declare -a TO_INSTALL=() declare -a TO_INSTALL=()
# Install packages passed in via argument array # Install packages passed via argument array
# No spinner - conflicts with set -e # No spinner - conflicts with set -e
declare -a argArray1=("${!1}") declare -a argArray1=("${!1}")
for i in "${argArray1[@]}"; do for i in "${argArray1[@]}"; do
@ -584,6 +565,8 @@ In the next section, you can choose to use your current network settings (DHCP)
} }
chooseInterface(){ chooseInterface(){
# Find interfaces and let the user choose one
# Turn the available interfaces into an array so it can be used with a whiptail dialog # Turn the available interfaces into an array so it can be used with a whiptail dialog
local interfacesArray=() local interfacesArray=()
# Number of available interfaces # Number of available interfaces
@ -905,6 +888,7 @@ setStaticIPv4(){
} }
chooseUser(){ chooseUser(){
# Choose the user for the ovpns
if [ "${runUnattended}" = 'true' ]; then if [ "${runUnattended}" = 'true' ]; then
if [ -z "$install_user" ]; then if [ -z "$install_user" ]; then
if [ "$(awk -F':' 'BEGIN {count=0} $3>=1000 && $3<=60000 { count++ } END{ print count }' /etc/passwd)" -eq 1 ]; then if [ "$(awk -F':' 'BEGIN {count=0} $3>=1000 && $3<=60000 { count++ } END{ print count }' /etc/passwd)" -eq 1 ]; then
@ -1047,6 +1031,7 @@ getGitFiles(){
} }
cloneOrUpdateRepos(){ cloneOrUpdateRepos(){
# Clone/Update the repos
# /usr/local should always exist, not sure about the src subfolder though # /usr/local should always exist, not sure about the src subfolder though
$SUDO mkdir -p /usr/local/src $SUDO mkdir -p /usr/local/src
@ -1060,21 +1045,11 @@ cloneOrUpdateRepos(){
installPiVPN(){ installPiVPN(){
$SUDO mkdir -p /etc/pivpn/ $SUDO mkdir -p /etc/pivpn/
askWhichVPN askWhichVPN
setVPNDefaultVars
# Allow custom subnetClass via unattend setupVARs file. Use default if not provided.
if [ -z "$subnetClass" ]; then
subnetClass="24"
fi
if [ "$VPN" = "openvpn" ]; then if [ "$VPN" = "openvpn" ]; then
pivpnDEV="tun0" setOpenVPNDefaultVars
# Allow custom NET via unattend setupVARs file. Use default if not provided.
if [ -z "$pivpnNET" ]; then
pivpnNET="10.8.0.0"
fi
vpnGw="${pivpnNET/.0.0/.0.1}"
askAboutCustomizing askAboutCustomizing
installOpenVPN installOpenVPN
askCustomProto askCustomProto
@ -1090,6 +1065,36 @@ installPiVPN(){
elif [ "$VPN" = "wireguard" ]; then elif [ "$VPN" = "wireguard" ]; then
setWireguardDefaultVars
installWireGuard
askCustomPort
askClientDNS
askPublicIPOrDNS
confWireGuard
confNetwork
writeWireguardTempVarsFile
fi
writeVPNTempVarsFile
}
setVPNDefaultVars(){
# Allow custom subnetClass via unattend setupVARs file. Use default if not provided.
if [ -z "$subnetClass" ]; then
subnetClass="24"
fi
}
setOpenVPNDefaultVars(){
pivpnDEV="tun0"
# Allow custom NET via unattend setupVARs file. Use default if not provided.
if [ -z "$pivpnNET" ]; then
pivpnNET="10.8.0.0"
fi
vpnGw="${pivpnNET/.0.0/.0.1}"
}
setWireguardDefaultVars(){
# Since WireGuard only uses UDP, askCustomProto() is never called so we # Since WireGuard only uses UDP, askCustomProto() is never called so we
# set the protocol here. # set the protocol here.
pivpnPROTO="udp" pivpnPROTO="udp"
@ -1113,14 +1118,18 @@ installPiVPN(){
fi fi
CUSTOMIZE=0 CUSTOMIZE=0
}
installWireGuard writeVPNTempVarsFile(){
askCustomPort {
askClientDNS echo "pivpnDEV=${pivpnDEV}"
askPublicIPOrDNS echo "pivpnNET=${pivpnNET}"
confWireGuard echo "subnetClass=${subnetClass}"
confNetwork echo "ALLOWED_IPS=\"${ALLOWED_IPS}\""
} >> ${tempsetupVarsFile}
}
writeWireguardTempVarsFile(){
echo "pivpnPROTO=${pivpnPROTO}" >> ${tempsetupVarsFile} echo "pivpnPROTO=${pivpnPROTO}" >> ${tempsetupVarsFile}
echo "pivpnMTU=${pivpnMTU}" >> ${tempsetupVarsFile} echo "pivpnMTU=${pivpnMTU}" >> ${tempsetupVarsFile}
@ -1130,15 +1139,6 @@ installPiVPN(){
if [ "$pivpnPERSISTENTKEEPALIVE" ]; then if [ "$pivpnPERSISTENTKEEPALIVE" ]; then
echo "pivpnPERSISTENTKEEPALIVE=${pivpnPERSISTENTKEEPALIVE}" >> ${tempsetupVarsFile} echo "pivpnPERSISTENTKEEPALIVE=${pivpnPERSISTENTKEEPALIVE}" >> ${tempsetupVarsFile}
fi fi
fi
{
echo "pivpnDEV=${pivpnDEV}"
echo "pivpnNET=${pivpnNET}"
echo "subnetClass=${subnetClass}"
echo "ALLOWED_IPS=\"${ALLOWED_IPS}\""
} >> ${tempsetupVarsFile}
} }
askWhichVPN(){ askWhichVPN(){
@ -2182,6 +2182,7 @@ if \$programname == 'ovpn-server' then stop" | $SUDO tee /etc/rsyslog.d/30-openv
restartServices(){ restartServices(){
# Start services
echo "::: Restarting services..." echo "::: Restarting services..."
case ${PLAT} in case ${PLAT} in
Debian|Raspbian|Ubuntu) Debian|Raspbian|Ubuntu)
@ -2266,6 +2267,14 @@ confUnattendedUpgrades(){
fi fi
} }
writeConfigFiles(){
# Save installation setting to the final location
echo "INSTALLED_PACKAGES=(${INSTALLED_PACKAGES[*]})" >> ${tempsetupVarsFile}
echo "::: Setupfiles copied to ${setupConfigDir}/${VPN}/${setupVarsFile}"
$SUDO mkdir -p "${setupConfigDir}/${VPN}/"
$SUDO cp ${tempsetupVarsFile} "${setupConfigDir}/${VPN}/${setupVarsFile}"
}
installScripts(){ installScripts(){
# Ensure /opt exists (issue #607) # Ensure /opt exists (issue #607)
$SUDO mkdir -p /opt $SUDO mkdir -p /opt
@ -2289,6 +2298,10 @@ installScripts(){
$SUDO unlink /usr/local/bin/pivpn $SUDO unlink /usr/local/bin/pivpn
$SUDO ln -sf -T "${pivpnFilesDir}/scripts/pivpn" /usr/local/bin/pivpn $SUDO ln -sf -T "${pivpnFilesDir}/scripts/pivpn" /usr/local/bin/pivpn
else else
# Check if bash_completion scripts dir exists and creates it if not
if [ ! -d "/etc/bash_completion.d" ]; then
mkdir -p /etc/bash_copletion.d
fi
# Only one protocol is installed, symlink bash completion, the pivpn script # Only one protocol is installed, symlink bash completion, the pivpn script
# and the script directory # and the script directory
$SUDO ln -sf -T "${pivpnFilesDir}/scripts/${VPN}/bash-completion" /etc/bash_completion.d/pivpn $SUDO ln -sf -T "${pivpnFilesDir}/scripts/${VPN}/bash-completion" /etc/bash_completion.d/pivpn
@ -2302,6 +2315,11 @@ installScripts(){
} }
displayFinalMessage(){ displayFinalMessage(){
# Ensure that cached writes reach persistent storage
echo "::: Flushing writes to disk..."
sync
echo "::: done."
if [ "${runUnattended}" = 'true' ]; then if [ "${runUnattended}" = 'true' ]; then
echo "::: Installation Complete!" echo "::: Installation Complete!"
echo "::: Now run 'pivpn add' to create the client profiles." echo "::: Now run 'pivpn add' to create the client profiles."