pivpn/scripts/openvpn/pivpn.sh

132 lines
2.6 KiB
Bash
Raw Permalink Normal View History

2019-10-14 10:27:28 +00:00
#!/bin/bash
2022-07-26 13:20:35 +00:00
CHECK_PKG_INSTALLED='dpkg-query -s'
if grep -qsEe "^NAME\=['\"]?Alpine[a-zA-Z ]*['\"]?$" /etc/os-release; then
2022-07-27 12:53:36 +00:00
CHECK_PKG_INSTALLED='apk --no-cache info -e'
2022-07-26 13:20:35 +00:00
fi
2019-10-14 10:27:28 +00:00
# Must be root to use this tool
2022-07-27 12:53:36 +00:00
if [[ "${EUID}" -ne 0 ]]; then
if ${CHECK_PKG_INSTALLED} sudo &> /dev/null; then
export SUDO="sudo"
2019-10-14 10:27:28 +00:00
else
2022-07-27 12:53:36 +00:00
err "::: Please install sudo or run this as root."
2019-11-19 12:28:51 +00:00
exit 1
2019-10-14 10:27:28 +00:00
fi
fi
2020-05-05 20:43:20 +00:00
scriptDir="/opt/pivpn"
2020-04-28 22:44:56 +00:00
vpn="openvpn"
2022-07-27 12:53:36 +00:00
err() {
echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')]: $*" >&2
2019-10-14 10:27:28 +00:00
}
2022-07-27 12:53:36 +00:00
makeOVPNFunc() {
shift
${SUDO} "${scriptDir}/${vpn}/makeOVPN.sh" "$@"
exit "${?}"
2019-10-14 10:27:28 +00:00
}
2022-07-27 12:53:36 +00:00
listClientsFunc() {
shift
${SUDO} "${scriptDir}/${vpn}/clientStat.sh" "$@"
exit "${?}"
2019-10-14 10:27:28 +00:00
}
2022-07-27 12:53:36 +00:00
listOVPNFunc() {
${SUDO} "${scriptDir}/${vpn}/listOVPN.sh"
exit "${?}"
2019-10-14 10:27:28 +00:00
}
2022-07-27 12:53:36 +00:00
debugFunc() {
echo "::: Generating Debug Output"
${SUDO} "${scriptDir}/${vpn}/pivpnDebug.sh" | tee /tmp/debug.log
echo "::: "
echo "::: Debug output completed above."
echo "::: Copy saved to /tmp/debug.log"
echo "::: "
exit "${?}"
2019-10-14 10:27:28 +00:00
}
2022-07-27 12:53:36 +00:00
removeOVPNFunc() {
shift
${SUDO} "${scriptDir}/${vpn}/removeOVPN.sh" "$@"
exit "${?}"
2019-10-14 10:27:28 +00:00
}
2022-07-27 12:53:36 +00:00
uninstallFunc() {
${SUDO} "${scriptDir}/uninstall.sh" "${vpn}"
exit "${?}"
2019-10-14 10:27:28 +00:00
}
2022-07-27 12:53:36 +00:00
update() {
shift
${SUDO} "${scriptDir}/update.sh" "$@"
exit "${?}"
2019-10-14 10:27:28 +00:00
}
2022-07-27 12:53:36 +00:00
backup() {
${SUDO} "${scriptDir}/backup.sh" "${vpn}"
exit "${?}"
}
2019-10-14 10:27:28 +00:00
2022-07-27 12:53:36 +00:00
helpFunc() {
echo "::: Control all PiVPN specific functions!"
echo ":::"
echo "::: Usage: pivpn <command> [option]"
echo ":::"
echo "::: Commands:"
echo "::: -a, add [nopass] Create a client ovpn profile, optional nopass"
echo "::: -c, clients List any connected clients to the server"
echo "::: -d, debug Start a debugging session if having trouble"
echo "::: -l, list List all valid and revoked certificates"
echo "::: -r, revoke Revoke a client ovpn profile"
echo "::: -h, help Show this help dialog"
echo "::: -u, uninstall Uninstall PiVPN from your system!"
echo "::: -up, update Updates PiVPN Scripts"
echo "::: -bk, backup Backup Openvpn and ovpns dir"
exit 0
2019-10-14 10:27:28 +00:00
}
2022-07-27 12:53:36 +00:00
if [[ "$#" == 0 ]]; then
helpFunc
2019-10-14 10:27:28 +00:00
fi
# Handle redirecting to specific functions based on arguments
2022-07-27 12:53:36 +00:00
case "${1}" in
"-a" | "add")
makeOVPNFunc "$@"
;;
"-c" | "clients")
listClientsFunc "$@"
;;
"-d" | "debug")
debugFunc
;;
"-l" | "list")
listOVPNFunc
;;
"-r" | "revoke")
removeOVPNFunc "$@"
;;
"-h" | "help")
helpFunc
;;
"-u" | "uninstall")
uninstallFunc
;;
"-up" | "update")
update "$@"
;;
"-bk" | "backup")
backup
;;
*)
helpFunc
;;
2019-10-14 10:27:28 +00:00
esac