Did some refactor on wgd.sh

This commit is contained in:
Donald Zou 2024-08-25 16:26:36 +08:00
parent c012b8c4a5
commit e21853286e

View file

@ -65,8 +65,6 @@ _determineOS(){
OS=$ID OS=$ID
elif [ -f /etc/redhat-release ]; then elif [ -f /etc/redhat-release ]; then
OS="redhat" OS="redhat"
# elif [ -f /etc/arch-release ]; then
# OS="arch"
else else
printf "[WGDashboard] %s Sorry, your OS is not supported. Currently the install script only support Debian-based, Red Hat-based OS." "$heavy_crossmark" printf "[WGDashboard] %s Sorry, your OS is not supported. Currently the install script only support Debian-based, Red Hat-based OS." "$heavy_crossmark"
printf "%s\n" "$helpMsg" printf "%s\n" "$helpMsg"
@ -129,18 +127,6 @@ _installPythonVenv(){
ubuntu|debian) ubuntu|debian)
{ sudo apt-get update; sudo apt-get install ${pythonExecutable}-venv; } &>> ./log/install.txt { sudo apt-get update; sudo apt-get install ${pythonExecutable}-venv; } &>> ./log/install.txt
;; ;;
# centos|fedora|redhat|rhel)
# if command -v dnf &> /dev/null; then
# { sudo dnf install -y ${pythonExecutable}-virtualenv; printf "\n\n"; } >> ./log/install.txt
# else
# { sudo yum install -y ${pythonExecutable}-virtualenv; printf "\n\n"; } >> ./log/install.txt
# fi
# ;;
# *)
# printf "[WGDashboard] %s Sorry, your OS is not supported. Currently the install script only support Debian-based, Red Hat-based OS.\n" "$heavy_crossmark"
# printf "%s\n" "$helpMsg"
# kill $TOP_PID
# ;;
esac esac
fi fi
@ -257,9 +243,9 @@ install_wgd(){
_installPythonPip _installPythonPip
if [ ! -d "db" ] if [ ! -d "db" ]
then then
printf "[WGDashboard] Creating ./db folder\n" printf "[WGDashboard] Creating ./db folder\n"
mkdir "db" mkdir "db"
fi fi
_check_and_set_venv _check_and_set_venv
printf "[WGDashboard] Upgrading Python Package Manage (PIP)\n" printf "[WGDashboard] Upgrading Python Package Manage (PIP)\n"
@ -288,11 +274,11 @@ check_wgd_status(){
} }
certbot_create_ssl () { certbot_create_ssl () {
certbot certonly --config ./certbot.ini --email "$EMAIL" --work-dir $cb_work_dir --config-dir $cb_config_dir --domain "$SERVERURL" certbot certonly --config ./certbot.ini --email "$EMAIL" --work-dir $cb_work_dir --config-dir $cb_config_dir --domain "$SERVERURL"
} }
certbot_renew_ssl () { certbot_renew_ssl () {
certbot renew --work-dir $cb_work_dir --config-dir $cb_config_dir certbot renew --work-dir $cb_work_dir --config-dir $cb_config_dir
} }
gunicorn_start () { gunicorn_start () {
@ -319,7 +305,7 @@ gunicorn_start () {
} }
gunicorn_stop () { gunicorn_stop () {
sudo kill $(cat ./gunicorn.pid) sudo kill $(cat ./gunicorn.pid)
} }
start_wgd () { start_wgd () {
@ -328,40 +314,41 @@ start_wgd () {
} }
stop_wgd() { stop_wgd() {
if test -f "$PID_FILE"; then if test -f "$PID_FILE"; then
gunicorn_stop gunicorn_stop
else else
kill "$(ps aux | grep "[p]ython3 $app_name" | awk '{print $2}')" kill "$(ps aux | grep "[p]ython3 $app_name" | awk '{print $2}')"
fi fi
} }
startwgd_docker() { startwgd_docker() {
_checkWireguard _checkWireguard
printf "[WGDashboard] WireGuard Config Started\n" printf "[WGDashboard][Docker] WireGuard configuration started\n"
{ date; start_core ; printf "\n\n"; } >> ./log/install.txt { date; start_core ; printf "\n\n"; } >> ./log/install.txt
gunicorn_start gunicorn_start
} }
start_core() { start_core() {
local iptable_dir="/opt/wireguarddashboard/src/iptable-rules" local iptable_dir="/opt/wireguarddashboard/src/iptable-rules"
# Check if wg0.conf exists in /etc/wireguard # Check if wg0.conf exists in /etc/wireguard
if [[ ! -f /etc/wireguard/wg0.conf ]]; then if [[ ! -f /etc/wireguard/wg0.conf ]]; then
echo "wg0.conf not found. Running Generate Configuration." echo "[WGDashboard][Docker] wg0.conf not found. Running generate configuration."
newconf_wgd newconf_wgd
else else
echo "wg0.conf already exists. Skipping WireGuard Config Generation." echo "[WGDashboard][Docker] wg0.conf already exists. Skipping WireGuard configuration generation."
fi fi
# Re-assign config_files to ensure it includes any newly created configurations # Re-assign config_files to ensure it includes any newly created configurations
local config_files=$(find /etc/wireguard -type f -name "*.conf") local config_files=$(find /etc/wireguard -type f -name "*.conf")
# Set file permissions # Set file permissions
find /etc/wireguard -type f -name "*.conf" -exec chmod 600 {} \; find /etc/wireguard -type f -name "*.conf" -exec chmod 600 {} \;
find "$iptable_dir" -type f -name "*.sh" -exec chmod +x {} \; find "$iptable_dir" -type f -name "*.sh" -exec chmod +x {} \;
# Start WireGuard for each config file # Start WireGuard for each config file
for file in $config_files; do for file in $config_files; do
config_name=$(basename "$file" ".conf") config_name=$(basename "$file" ".conf")
wg-quick up "$config_name" wg-quick up "$config_name"
done done
} }
@ -383,15 +370,14 @@ EOF
} }
start_wgd_debug() { start_wgd_debug() {
printf "%s\n" "$dashes" printf "%s\n" "$dashes"
_checkWireguard _checkWireguard
printf "[WGDashboard] Starting WGDashboard in the foreground.\n" printf "[WGDashboard] Starting WGDashboard in the foreground.\n"
sudo "$venv_python" "$app_name" sudo "$venv_python" "$app_name"
printf "%s\n" "$dashes" printf "%s\n" "$dashes"
} }
update_wgd() { update_wgd() {
_determineOS _determineOS
if ! python3 --version > /dev/null 2>&1 if ! python3 --version > /dev/null 2>&1
then then
@ -430,55 +416,55 @@ update_wgd() {
} }
if [ "$#" != 1 ]; if [ "$#" != 1 ];
then then
help help
else else
if [ "$1" = "start" ]; then if [ "$1" = "start" ]; then
if check_wgd_status; then if check_wgd_status; then
printf "%s\n" "$dashes" printf "%s\n" "$dashes"
printf "[WGDashboard] WGDashboard is already running.\n" printf "[WGDashboard] WGDashboard is already running.\n"
printf "%s\n" "$dashes" printf "%s\n" "$dashes"
else else
start_wgd start_wgd
fi fi
elif [ "$1" = "docker_start" ]; then elif [ "$1" = "docker_start" ]; then
printf "%s\n" "$dashes" printf "%s\n" "$dashes"
startwgd_docker startwgd_docker
printf "%s\n" "$dashes" printf "%s\n" "$dashes"
elif [ "$1" = "stop" ]; then elif [ "$1" = "stop" ]; then
if check_wgd_status; then if check_wgd_status; then
printf "%s\n" "$dashes" printf "%s\n" "$dashes"
stop_wgd stop_wgd
printf "[WGDashboard] WGDashboard is stopped.\n" printf "[WGDashboard] WGDashboard is stopped.\n"
printf "%s\n" "$dashes" printf "%s\n" "$dashes"
else else
printf "%s\n" "$dashes" printf "%s\n" "$dashes"
printf "[WGDashboard] WGDashboard is not running.\n" printf "[WGDashboard] WGDashboard is not running.\n"
printf "%s\n" "$dashes" printf "%s\n" "$dashes"
fi fi
elif [ "$1" = "update" ]; then elif [ "$1" = "update" ]; then
update_wgd update_wgd
elif [ "$1" = "install" ]; then elif [ "$1" = "install" ]; then
printf "%s\n" "$dashes" printf "%s\n" "$dashes"
install_wgd install_wgd
printf "%s\n" "$dashes" printf "%s\n" "$dashes"
elif [ "$1" = "restart" ]; then elif [ "$1" = "restart" ]; then
if check_wgd_status; then if check_wgd_status; then
printf "%s\n" "$dashes" printf "%s\n" "$dashes"
stop_wgd stop_wgd
printf "| WGDashboard is stopped. |\n" printf "[WGDashboard] WGDashboard is stopped.\n"
sleep 4 sleep 4
start_wgd start_wgd
else else
start_wgd start_wgd
fi fi
elif [ "$1" = "debug" ]; then elif [ "$1" = "debug" ]; then
if check_wgd_status; then if check_wgd_status; then
printf "| WGDashboard is already running. |\n" printf "[WGDashboard] WGDashboard is already running.\n"
else else
start_wgd_debug start_wgd_debug
fi fi
else else
help help
fi fi
fi fi