1
0
mirror of https://github.com/donaldzou/WGDashboard.git synced 2024-11-06 07:50:13 +01:00
This commit is contained in:
Noxcis 2024-08-24 06:45:13 -05:00
parent 0c77823020
commit 8da0fde52a
8 changed files with 504 additions and 450 deletions

View File

@ -1,19 +1,20 @@
# Pull from small Debian stable image.
FROM alpine:latest
LABEL maintainer="dselen@nerthus.nl"
ENV PYTHONPATH="/usr/bin/python"
ENV PYTHONPATH="/usr/lib/python3.12/site-packages"
WORKDIR /opt/wireguarddashboard/src
RUN apk update && \
apk add --no-cache py3-bcrypt py3-psutil && \
apk add --no-cache sudo gcc musl-dev linux-headers && \
apk add --no-cache wireguard-tools && \
apk add --no-cache net-tools iproute2 iptables ip6tables && \
apk add --no-cache inotify-tools procps openresolv && \
apk add --no-cache iptables ip6tables && \
mkdir /opt/wireguarddashboard/src/master-key
COPY ./src /opt/wireguarddashboard/src/
COPY ./docker/wgd.sh /opt/wireguarddashboard/src/
COPY ./docker/requirements.txt /opt/wireguarddashboard/src/
COPY ./docker/alpine/entrypoint.sh /opt/wireguarddashboard/src/
#COPY ./docker/alpine/wgd.sh /opt/wireguarddashboard/src/
#COPY ./docker/alpine/requirements.txt /opt/wireguarddashboard/src/
RUN chmod u+x /opt/wireguarddashboard/src/entrypoint.sh

View File

@ -10,9 +10,9 @@ services:
environment:
- wg_net=10.0.0.1/24
- wg_port=51820
volumes:
- wgd_configs:/etc/wireguard
- wgd_app:/opt/wireguarddashboard/src
#volumes:
#- wgd_configs:/etc/wireguard
#- wgd_app:/opt/wireguarddashboard/src
ports:
- 10086:10086/tcp
- 51820:51820/udp
@ -21,6 +21,6 @@ services:
- net.ipv4.conf.all.src_valid_mark=1
volumes:
wgd_configs:
wgd_app:
#volumes:
#wgd_configs:
#wgd_app:

View File

@ -0,0 +1,41 @@
#!/bin/bash
echo "Starting the WireGuard Dashboard Docker container."
clean_up() {
# Cleaning out previous data such as the .pid file and starting the WireGuard Dashboard. Making sure to use the python venv.
echo "Looking for remains of previous instances..."
if [ -f "/opt/wireguarddashboard/app/src/gunicorn.pid" ]; then
echo "Found old .pid file, removing."
rm /opt/wireguarddashboard/app/src/gunicorn.pid
else
echo "No remains found, continuing."
fi
}
ensure_blocking() {
sleep 1s
echo "Ensuring container continuation."
# This function checks if the latest error log is created and tails it for docker logs uses.
if find "/opt/wireguarddashboard/src/log" -mindepth 1 -maxdepth 1 -type f | read -r; then
latestErrLog=$(find /opt/wireguarddashboard/src/log -name "error_*.log" | head -n 1)
latestAccLog=$(find /opt/wireguarddashboard/src/log -name "access_*.log" | head -n 1)
tail -f "${latestErrLog}" "${latestAccLog}"
fi
# Blocking command in case of erroring. So the container does not quit.
sleep infinity
}
# Execute functions for the WireGuard Dashboard services, then set the environment variables
clean_up
chmod u+x /opt/wireguarddashboard/src/wgd.sh
if [ ! -f "/opt/wireguarddashboard/src/wg-dashboard.ini" ]; then
/opt/wireguarddashboard/src/wgd.sh install
fi
/opt/wireguarddashboard/src/wgd.sh start
ensure_blocking

438
docker/alpine/wgd.sh Normal file
View File

@ -0,0 +1,438 @@
#!/bin/bash
# wgd.sh - Copyright(C) 2024 Donald Zou [https://github.com/donaldzou]
# Under Apache-2.0 License
#trap "kill $TOP_PID"
export TOP_PID=$$
app_name="dashboard.py"
app_official_name="WGDashboard"
venv_python="./venv/bin/python3"
venv_gunicorn="./venv/bin/gunicorn"
pythonExecutable="python3"
heavy_checkmark=$(printf "\xE2\x9C\x94")
heavy_crossmark=$(printf "\xE2\x9C\x97")
PID_FILE=./gunicorn.pid
environment=$(if [[ $ENVIRONMENT ]]; then echo $ENVIRONMENT; else echo 'develop'; fi)
if [[ $CONFIGURATION_PATH ]]; then
cb_work_dir=$CONFIGURATION_PATH/letsencrypt/work-dir
cb_config_dir=$CONFIGURATION_PATH/letsencrypt/config-dir
else
cb_work_dir=/etc/letsencrypt
cb_config_dir=/var/lib/letsencrypt
fi
dashes='------------------------------------------------------------'
equals='============================================================'
helpMsg="[WGDashboard] Please check ./log/install.txt for more details. For further assistance, please open a ticket on https://github.com/donaldzou/WGDashboard/issues/new/choose, I'm more than happy to help :)"
help () {
printf "=================================================================================\n"
printf "+ <WGDashboard> by Donald Zou - https://github.com/donaldzou +\n"
printf "=================================================================================\n"
printf "| Usage: ./wgd.sh <option> |\n"
printf "| |\n"
printf "| Available options: |\n"
printf "| start: To start WGDashboard. |\n"
printf "| stop: To stop WGDashboard. |\n"
printf "| debug: To start WGDashboard in debug mode (i.e run in foreground). |\n"
printf "| update: To update WGDashboard to the newest version from GitHub. |\n"
printf "| install: To install WGDashboard. |\n"
printf "| Thank you for using! Your support is my motivation ;) |\n"
printf "=================================================================================\n"
}
_check_and_set_venv(){
VIRTUAL_ENV="./venv"
if [ ! -d $VIRTUAL_ENV ]; then
printf "[WGDashboard] Creating Python Virtual Environment under ./venv\n"
{ $pythonExecutable -m venv $VIRTUAL_ENV; } >> ./log/install.txt
fi
if ! $venv_python --version > /dev/null 2>&1
then
printf "[WGDashboard] %s Python Virtual Environment under ./venv failed to create. Halting now.\n" "$heavy_crossmark"
kill $TOP_PID
fi
. ${VIRTUAL_ENV}/bin/activate
}
_determineOS(){
if [ -f /etc/os-release ]; then
. /etc/os-release
OS=$ID
elif [ -f /etc/redhat-release ]; then
OS="redhat"
# elif [ -f /etc/arch-release ]; then
# OS="arch"
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 "%s\n" "$helpMsg"
kill $TOP_PID
fi
printf "[WGDashboard] OS: %s\n" "$OS"
}
_installPython(){
case "$OS" in
ubuntu|debian)
{ sudo apt update ; sudo apt-get install -y python3 net-tools; printf "\n\n"; } &>> ./log/install.txt
;;
centos|fedora|redhat|rehl)
if command -v dnf &> /dev/null; then
{ sudo dnf install -y python3 net-tools; printf "\n\n"; } >> ./log/install.txt
else
{ sudo yum install -y python3 net-tools ; printf "\n\n"; } >> ./log/install.txt
fi
;;
alpine)
{ apk update; apk add python3 net-tools py3-bcrypt py3-psutil; printf "\n\n"; } &>> ./log/install.txt
;;
esac
if ! python3 --version > /dev/null 2>&1
then
printf "[WGDashboard] %s Python is still not installed, halting script now.\n" "$heavy_crossmark"
printf "%s\n" "$helpMsg"
kill $TOP_PID
else
printf "[WGDashboard] %s Python is installed\n" "$heavy_checkmark"
fi
}
_installPythonVenv(){
if [ "$pythonExecutable" = "python3" ]; then
case "$OS" in
ubuntu|debian)
{ sudo apt update ; sudo apt-get install -y python3-venv; printf "\n\n"; } &>> ./log/install.txt
;;
centos|fedora|redhat|rhel)
if command -v dnf &> /dev/null; then
{ sudo dnf install -y python3-virtualenv; printf "\n\n"; } >> ./log/install.txt
else
{ sudo yum install -y python3-virtualenv; printf "\n\n"; } >> ./log/install.txt
fi
;;
alpine)
{ apk add python3 py3-virtualenv; printf "\n\n"; } &>> ./log/install.txt
;;
*)
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
else
case "$OS" in
ubuntu|debian)
{ 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
fi
if ! $pythonExecutable -m venv -h > /dev/null 2>&1
then
printf "[WGDashboard] %s Python Virtual Environment is still not installed, halting script now.\n" "$heavy_crossmark"
printf "%s\n" "$helpMsg"
else
printf "[WGDashboard] %s Python Virtual Environment is installed\n" "$heavy_checkmark"
fi
}
_installPythonPip(){
if ! $pythonExecutable -m pip -h > /dev/null 2>&1
then
case "$OS" in
ubuntu|debian)
if [ "$pythonExecutable" = "python3" ]; then
{ sudo apt update ; sudo apt-get install -y python3-pip; printf "\n\n"; } &>> ./log/install.txt
else
{ sudo apt update ; sudo apt-get install -y ${pythonExecutable}-distutil python3-pip; printf "\n\n"; } &>> ./log/install.txt
fi
;;
centos|fedora|redhat|rhel)
if [ "$pythonExecutable" = "python3" ]; then
{ sudo dnf install -y python3-pip; printf "\n\n"; } >> ./log/install.txt
else
{ sudo dnf install -y ${pythonExecutable}-pip; printf "\n\n"; } >> ./log/install.txt
fi
;;
alpine)
{ apk add py3-pip; printf "\n\n"; } &>> ./log/install.txt
;;
*)
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
fi
if ! $pythonExecutable -m pip -h > /dev/null 2>&1
then
printf "[WGDashboard] %s Python Package Manager (PIP) is still not installed, halting script now.\n" "$heavy_crossmark"
printf "%s\n" "$helpMsg"
kill $TOP_PID
else
printf "[WGDashboard] %s Python Package Manager (PIP) is installed\n" "$heavy_checkmark"
fi
}
_checkWireguard(){
if ! wg -h > /dev/null 2>&1
then
printf "[WGDashboard] %s WireGuard is not installed. Please follow instruction on https://www.wireguard.com/install/ to install. \n" "$heavy_crossmark"
kill $TOP_PID
fi
if ! wg-quick -h > /dev/null 2>&1
then
printf "[WGDashboard] %s WireGuard is not installed. Please follow instruction on https://www.wireguard.com/install/ to install. \n" "$heavy_crossmark"
kill $TOP_PID
fi
}
_checkPythonVersion(){
version_pass=$($pythonExecutable -c 'import sys; print("1") if (sys.version_info.major == 3 and sys.version_info.minor >= 10) else print("0");')
version=$($pythonExecutable --version)
if [ $version_pass == "1" ]
then
return;
elif python3.10 --version > /dev/null 2>&1
then
printf "[WGDashboard] %s Found Python 3.10. Will be using [python3.10] to install WGDashboard.\n" "$heavy_checkmark"
pythonExecutable="python3.10"
elif python3.11 --version > /dev/null 2>&1
then
printf "[WGDashboard] %s Found Python 3.11. Will be using [python3.11] to install WGDashboard.\n" "$heavy_checkmark"
pythonExecutable="python3.11"
elif python3.12 --version > /dev/null 2>&1
then
printf "[WGDashboard] %s Found Python 3.12. Will be using [python3.12] to install WGDashboard.\n" "$heavy_checkmark"
pythonExecutable="python3.12"
else
printf "[WGDashboard] %s Could not find a compatible version of Python. Current Python is %s.\n" "$heavy_crossmark" "$version"
printf "[WGDashboard] WGDashboard required Python 3.10, 3.11 or 3.12. Halting install now.\n"
kill $TOP_PID
fi
}
install_wgd(){
printf "[WGDashboard] Starting to install WGDashboard\n"
_checkWireguard
sudo chmod -R 755 /etc/wireguard/
if [ ! -d "log" ]
then
printf "[WGDashboard] Creating ./log folder\n"
mkdir "log"
fi
_determineOS
if ! python3 --version > /dev/null 2>&1
then
printf "[WGDashboard] Python is not installed, trying to install now\n"
_installPython
else
printf "[WGDashboard] %s Python is installed\n" "$heavy_checkmark"
fi
_checkPythonVersion
_installPythonVenv
_installPythonPip
if [ ! -d "db" ]
then
printf "[WGDashboard] Creating ./db folder\n"
mkdir "db"
fi
_check_and_set_venv
printf "[WGDashboard] Upgrading Python Package Manage (PIP)\n"
{ date; python3 -m pip install --upgrade pip; printf "\n\n"; } >> ./log/install.txt
printf "[WGDashboard] Installing latest Python dependencies\n"
{ date; python3 -m pip install -r requirements.txt ; printf "\n\n"; } >> ./log/install.txt
printf "[WGDashboard] WGDashboard installed successfully!\n"
printf "[WGDashboard] Enter ./wgd.sh start to start the dashboard\n"
}
check_wgd_status(){
if test -f "$PID_FILE"; then
if ps aux | grep -v grep | grep $(cat ./gunicorn.pid) > /dev/null; then
return 0
else
return 1
fi
else
if ps aux | grep -v grep | grep '[p]ython3 '$app_name > /dev/null; then
return 0
else
return 1
fi
fi
}
certbot_create_ssl () {
certbot certonly --config ./certbot.ini --email "$EMAIL" --work-dir $cb_work_dir --config-dir $cb_config_dir --domain "$SERVERURL"
}
certbot_renew_ssl () {
certbot renew --work-dir $cb_work_dir --config-dir $cb_config_dir
}
gunicorn_start () {
printf "%s\n" "$dashes"
printf "[WGDashboard] Starting WGDashboard with Gunicorn in the background.\n"
d=$(date '+%Y%m%d%H%M%S')
if [[ $USER == root ]]; then
export PATH=$PATH:/usr/local/bin:$HOME/.local/bin
fi
_check_and_set_venv
sudo "$venv_gunicorn" --config ./gunicorn.conf.py
#sudo gunicorn -c ./gunicorn.conf.py
sleep 5
checkPIDExist=0
while [ $checkPIDExist -eq 0 ]
do
if test -f './gunicorn.pid'; then
checkPIDExist=1
printf "[WGDashboard] Checking if WGDashboard w/ Gunicorn started successfully\n"
fi
sleep 2
done
printf "[WGDashboard] WGDashboard w/ Gunicorn started successfully\n"
printf "%s\n" "$dashes"
}
gunicorn_stop () {
sudo kill $(cat ./gunicorn.pid)
}
start_wgd () {
_checkWireguard
gunicorn_start
}
stop_wgd() {
if test -f "$PID_FILE"; then
gunicorn_stop
else
kill "$(ps aux | grep "[p]ython3 $app_name" | awk '{print $2}')"
fi
}
start_wgd_debug() {
printf "%s\n" "$dashes"
_checkWireguard
printf "[WGDashboard] Starting WGDashboard in the foreground.\n"
sudo "$venv_python" "$app_name"
printf "%s\n" "$dashes"
}
update_wgd() {
_determineOS
if ! python3 --version > /dev/null 2>&1
then
printf "[WGDashboard] Python is not installed, trying to install now\n"
_installPython
else
printf "[WGDashboard] %s Python is installed\n" "$heavy_checkmark"
fi
_checkPythonVersion
_installPythonVenv
_installPythonPip
new_ver=$($venv_python -c "import json; import urllib.request; data = urllib.request.urlopen('https://api.github.com/repos/donaldzou/WGDashboard/releases/latest').read(); output = json.loads(data);print(output['tag_name'])")
printf "%s\n" "$dashes"
printf "[WGDashboard] Are you sure you want to update to the %s? (Y/N): " "$new_ver"
read up
if [ "$up" = "Y" ] || [ "$up" = "y" ]; then
printf "[WGDashboard] Shutting down WGDashboard\n"
if check_wgd_status; then
stop_wgd
fi
mv wgd.sh wgd.sh.old
printf "[WGDashboard] Downloading %s from GitHub..." "$new_ver"
{ date; git stash; git pull https://github.com/donaldzou/WGDashboard.git $new_ver --force; } >> ./log/update.txt
chmod +x ./wgd.sh
sudo ./wgd.sh install
printf "[WGDashboard] Update completed!\n"
printf "%s\n" "$dashes"
rm wgd.sh.old
else
printf "%s\n" "$dashes"
printf "[WGDashboard] Update Canceled.\n"
printf "%s\n" "$dashes"
fi
}
if [ "$#" != 1 ];
then
help
else
if [ "$1" = "start" ]; then
if check_wgd_status; then
printf "%s\n" "$dashes"
printf "[WGDashboard] WGDashboard is already running.\n"
printf "%s\n" "$dashes"
else
start_wgd
fi
elif [ "$1" = "stop" ]; then
if check_wgd_status; then
printf "%s\n" "$dashes"
stop_wgd
printf "[WGDashboard] WGDashboard is stopped.\n"
printf "%s\n" "$dashes"
else
printf "%s\n" "$dashes"
printf "[WGDashboard] WGDashboard is not running.\n"
printf "%s\n" "$dashes"
fi
elif [ "$1" = "update" ]; then
update_wgd
elif [ "$1" = "install" ]; then
printf "%s\n" "$dashes"
install_wgd
printf "%s\n" "$dashes"
elif [ "$1" = "restart" ]; then
if check_wgd_status; then
printf "%s\n" "$dashes"
stop_wgd
printf "| WGDashboard is stopped. |\n"
sleep 4
start_wgd
else
start_wgd
fi
elif [ "$1" = "debug" ]; then
if check_wgd_status; then
printf "| WGDashboard is already running. |\n"
else
start_wgd_debug
fi
else
help
fi
fi

View File

@ -1,427 +0,0 @@
#!/bin/bash
# wgd.sh - Copyright(C) 2024 Donald Zou [https://github.com/donaldzou]
# Under Apache-2.0 License
app_name="dashboard.py"
app_official_name="WGDashboard"
venv_python="./venv/bin/python3"
venv_gunicorn="./venv/bin/gunicorn"
PID_FILE=./gunicorn.pid
environment=$(if [[ $ENVIRONMENT ]]; then echo $ENVIRONMENT; else echo 'develop'; fi)
if [[ $CONFIGURATION_PATH ]]; then
cb_work_dir=$CONFIGURATION_PATH/letsencrypt/work-dir
cb_config_dir=$CONFIGURATION_PATH/letsencrypt/config-dir
else
cb_work_dir=/etc/letsencrypt
cb_config_dir=/var/lib/letsencrypt
fi
dashes='------------------------------------------------------------'
equals='============================================================'
helpMsg="[WGDashboard] Please check ./log/install.txt for more details. For further assistance, please open a ticket on https://github.com/donaldzou/WGDashboard/issues/new/choose, I'm more than happy to help :)"
help () {
printf "=================================================================================\n"
printf "+ <WGDashboard> by Donald Zou - https://github.com/donaldzou +\n"
printf "=================================================================================\n"
printf "| Usage: ./wgd.sh <option> |\n"
printf "| |\n"
printf "| Available options: |\n"
printf "| start: To start WGDashboard. |\n"
printf "| stop: To stop WGDashboard. |\n"
printf "| debug: To start WGDashboard in debug mode (i.e run in foreground). |\n"
printf "| update: To update WGDashboard to the newest version from GitHub. |\n"
printf "| install: To install WGDashboard. |\n"
printf "| Thank you for using! Your support is my motivation ;) |\n"
printf "=================================================================================\n"
}
_check_and_set_venv(){
VIRTUAL_ENV="./venv"
if [ ! -d $VIRTUAL_ENV ]; then
printf "[WGDashboard] Creating Python Virtual Environment under ./venv\n"
{ python3 -m venv --system-site-packages $VIRTUAL_ENV; } >> ./log/install.txt
fi
printf "[WGDashboard] Activate Python Virtual Environment under ./venv\n"
. ${VIRTUAL_ENV}/bin/activate
}
_determineOS() {
if [ -f /etc/os-release ]; then
. /etc/os-release
OS=$ID
elif [ -f /etc/redhat-release ]; then
OS="redhat"
elif [ -f /etc/alpine-release ]; then
OS="alpine"
else
printf "[WGDashboard] Sorry, your OS is not supported. Currently, the install script only supports Debian-based, Red Hat-based, and Alpine OS.\n"
printf "%s\n" "$helpMsg"
exit 1
fi
}
_installPython() {
case "$OS" in
ubuntu|debian)
{ apt update; apt-get install -y python3; printf "\n\n"; } &>> ./log/install.txt
;;
centos|fedora|redhat)
if command -v dnf &> /dev/null; then
{ dnf install -y python3; printf "\n\n"; } >> ./log/install.txt
else
{ yum install -y python3; printf "\n\n"; } >> ./log/install.txt
fi
;;
alpine)
{ apk add --no-cache python3; printf "\n\n"; } &>> ./log/install.txt
;;
*)
printf "[WGDashboard] Sorry, your OS is not supported. Currently, the install script only supports Debian-based, Red Hat-based, and Alpine OS.\n"
printf "%s\n" "$helpMsg"
exit 1
;;
esac
if ! python3 --version > /dev/null 2>&1; then
printf "[WGDashboard] Python is still not installed, halting script now.\n"
printf "%s\n" "$helpMsg"
exit 1
else
printf "[WGDashboard] \xE2\x9C\x94 Python is installed\n"
fi
}
_installPythonVenv(){
case "$OS" in
ubuntu|debian)
{
apt update
apt-get install -y python3-venv
printf "\n\n"
} &>> ./log/install.txt
;;
centos|fedora|redhat)
if command -v dnf &> /dev/null; then
{
dnf install -y python3-virtualenv
printf "\n\n"
} >> ./log/install.txt
else
{
yum install -y python3-virtualenv
printf "\n\n"
} >> ./log/install.txt
fi
;;
alpine)
{
apk update
apk add --no-cache python3 py3-virtualenv
printf "\n\n"
} >> ./log/install.txt
;;
*)
printf "[WGDashboard] Sorry, your OS is not supported. Currently the install script only supports Debian-based, Red Hat-based, and Alpine OS.\n"
printf "%s\n" "$helpMsg"
exit 1
;;
esac
if ! python3 -m venv -h > /dev/null 2>&1
then
printf "[WGDashboard] Python Virtual Environment is still not installed, halting script now.\n"
printf "%s\n" "$helpMsg"
exit 1
else
printf "[WGDashboard] \xE2\x9C\x94 Python Virtual Environment is installed\n"
fi
}
_installPythonPip() {
case "$OS" in
ubuntu|debian)
{ apt update; apt-get install -y python3-pip; printf "\n\n"; } &>> ./log/install.txt
;;
centos|fedora|redhat)
if command -v dnf &> /dev/null; then
{ dnf install -y python3-pip; printf "\n\n"; } >> ./log/install.txt
else
{ yum install -y python3-pip; printf "\n\n"; } >> ./log/install.txt
fi
;;
alpine)
{ apk add --no-cache py3-pip; printf "\n\n"; } &>> ./log/install.txt
;;
*)
printf "[WGDashboard] Sorry, your OS is not supported for auto install. Currently, the install script only supports Debian-based, Red Hat-based, and Alpine OS.\n"
printf "%s\n" "$helpMsg"
exit 1
;;
esac
if ! python3 -m pip -h > /dev/null 2>&1; then
printf "[WGDashboard] Python Package Manager (PIP) is still not installed, halting script now.\n"
printf "%s\n" "$helpMsg"
exit 1
else
printf "[WGDashboard] \xE2\x9C\x94 Python Package Manager (PIP) is installed\n"
fi
}
_checkWireguard(){
if ! wg -h > /dev/null 2>&1
then
printf "[WGDashboard] WireGuard is not installed. Please follow instruction on https://www.wireguard.com/install/ to install. \n"
exit 1
fi
if ! wg-quick -h > /dev/null 2>&1
then
printf "[WGDashboard] WireGuard is not installed. Please follow instruction on https://www.wireguard.com/install/ to install. \n"
exit 1
fi
}
install_wgd(){
printf "[WGDashboard] Starting to install WGDashboard\n"
_checkWireguard
chmod -R 755 /etc/wireguard/
if [ ! -d "log" ]
then
printf "[WGDashboard] Creating ./log folder\n"
mkdir "log"
fi
_determineOS
if ! python3 --version > /dev/null 2>&1
then
printf "[WGDashboard] Python is not installed, trying to install now\n"
_installPython
else
printf "[WGDashboard] \xE2\x9C\x94 Python is installed\n"
fi
_installPythonVenv
_installPythonPip
version_pass=$(python3 -c 'import sys; print("1") if (sys.version_info.major == 3 and sys.version_info.minor >= 10) else print("0");')
if [ $version_pass == "0" ]
then
printf "[WGDashboard] WGDashboard required Python 3.7 or above\n"
exit 1
fi
if [ ! -d "db" ]
then
printf "[WGDashboard] Creating ./db folder\n"
mkdir "db"
fi
_check_and_set_venv
printf "[WGDashboard] Upgrading Python Package Manage (PIP)\n"
{ date; python3 -m pip install --upgrade pip; printf "\n\n"; } >> ./log/install.txt
printf "[WGDashboard] Installing latest Python dependencies\n"
{ date; python3 -m pip install -r requirements.txt ; printf "\n\n"; } >> ./log/install.txt
printf "[WGDashboard] WGDashboard installed successfully!\n"
printf "[WGDashboard] Enter ./wgd.sh start to start the dashboard\n"
}
check_wgd_status(){
if test -f "$PID_FILE"; then
if ps aux | grep -v grep | grep $(cat ./gunicorn.pid) > /dev/null; then
return 0
else
return 1
fi
else
if ps aux | grep -v grep | grep '[p]ython3 '$app_name > /dev/null; then
return 0
else
return 1
fi
fi
}
certbot_create_ssl () {
certbot certonly --config ./certbot.ini --email "$EMAIL" --work-dir $cb_work_dir --config-dir $cb_config_dir --domain "$SERVERURL"
}
certbot_renew_ssl () {
certbot renew --work-dir $cb_work_dir --config-dir $cb_config_dir
}
gunicorn_start () {
printf "%s\n" "$dashes"
printf "[WGDashboard] Starting WGDashboard with Gunicorn in the background.\n"
d=$(date '+%Y%m%d%H%M%S')
is_alpine() {
grep -q "Alpine" /etc/os-release 2>/dev/null
}
if [[ $USER == root ]]; then
if is_alpine; then
export PATH=$PATH:/usr/local/bin:$HOME/.local/bin:/sbin:/usr/sbin
else
export PATH=$PATH:/usr/local/bin:$HOME/.local/bin
fi
fi
_check_and_set_venv
if [ ! -x "$venv_gunicorn" ]; then
printf "[ERROR] Gunicorn executable not found or not executable.\n"
return 1
fi
start_core
gunicorn -c ./gunicorn.conf.py
# line below exsits after execution when using docker
#"$venv_gunicorn" --config ./gunicorn.conf.py &
sleep 5
checkPIDExist=0
while [ $checkPIDExist -eq 0 ]; do
if test -f './gunicorn.pid'; then
checkPIDExist=1
printf "[WGDashboard] Checking if WGDashboard w/ Gunicorn started successfully\n"
else
printf "[INFO] Waiting for Gunicorn PID file to be created...\n"
fi
sleep 2
done
printf "[WGDashboard] WGDashboard w/ Gunicorn started successfully\n"
printf "%s\n" "$dashes"
}
gunicorn_stop () {
kill $(cat ./gunicorn.pid)
}
start_wgd () {
_checkWireguard
gunicorn_start
}
stop_wgd() {
if test -f "$PID_FILE"; then
gunicorn_stop
else
kill "$(ps aux | grep "[p]ython3 $app_name" | awk '{print $2}')"
fi
}
start_wgd_debug() {
printf "%s\n" "$dashes"
_checkWireguard
printf "[WGDashboard] Starting WGDashboard in the foreground.\n"
"$venv_python" "$app_name"
printf "%s\n" "$dashes"
}
update_wgd() {
new_ver=$(python3 -c "import json; import urllib.request; data = urllib.request.urlopen('https://api.github.com/repos/donaldzou/WGDashboard/releases/latest').read(); output = json.loads(data);print(output['tag_name'])")
printf "%s\n" "$dashes"
printf "[WGDashboard] Are you sure you want to update to the %s? (Y/N): " "$new_ver"
read up
if [ "$up" = "Y" ] || [ "$up" = "y" ]; then
printf "[WGDashboard] Shutting down WGDashboard\n"
if check_wgd_status; then
stop_wgd
fi
mv wgd.sh wgd.sh.old
printf "[WGDashboard] Downloading %s from GitHub..." "$new_ver"
{ date; git stash; git pull https://github.com/donaldzou/WGDashboard.git $new_ver --force; } >> ./log/update.txt
chmod +x ./wgd.sh
./wgd.sh install
printf "[WGDashboard] Update completed!\n"
printf "%s\n" "$dashes"
rm wgd.sh.old
else
printf "%s\n" "$dashes"
printf "| Update Canceled. |\n"
printf "%s\n" "$dashes"
fi
}
start_core () {
local config_files=$(find /etc/wireguard -type f -name "*.conf")
local iptable_dir="/opt/wireguarddashboard/src/iptable-rules"
newconf_wgd
find /etc/wireguard -type f -name "*.conf" -exec chmod 600 {} \;
find "$iptable_dir" -type f -name "*.sh" -exec chmod +x {} \;
for file in $config_files; do
config_name=$(basename "$file" ".conf")
{ date; wg-quick up "$config_name"; printf "\n\n"; } >> /opt/wireguarddashboard/src/log/install.txt 2>&1
done
}
newconf_wgd() {
local wg_port_listen=$wg_port
local wg_addr_range=$wg_net
private_key=$(wg genkey)
public_key=$(echo "$private_key" | wg pubkey)
cat <<EOF >"/etc/wireguard/wg0.conf"
[Interface]
PrivateKey = $private_key
Address = $wg_addr_range
ListenPort = $wg_port_listen
SaveConfig = true
PostUp = /opt/wireguarddashboard/src/iptable-rules/postup.sh
PreDown = /opt/wireguarddashboard/src/iptable-rules/postdown.sh
EOF
}
if [ "$#" != 1 ];
then
help
else
if [ "$1" = "start" ]; then
if check_wgd_status; then
printf "%s\n" "$dashes"
printf "[WGDashboard] WGDashboard is already running.\n"
printf "%s\n" "$dashes"
else
start_wgd
fi
elif [ "$1" = "stop" ]; then
if check_wgd_status; then
printf "%s\n" "$dashes"
stop_wgd
printf "[WGDashboard] WGDashboard is stopped.\n"
printf "%s\n" "$dashes"
else
printf "%s\n" "$dashes"
printf "[WGDashboard] WGDashboard is not running.\n"
printf "%s\n" "$dashes"
fi
elif [ "$1" = "update" ]; then
update_wgd
elif [ "$1" = "install" ]; then
printf "%s\n" "$dashes"
install_wgd
printf "%s\n" "$dashes"
elif [ "$1" = "restart" ]; then
if check_wgd_status; then
printf "%s\n" "$dashes"
stop_wgd
printf "| WGDashboard is stopped. |\n"
sleep 4
start_wgd
else
start_wgd
fi
elif [ "$1" = "debug" ]; then
if check_wgd_status; then
printf "| WGDashboard is already running. |\n"
else
start_wgd_debug
fi
else
help
fi
fi

View File

@ -1073,17 +1073,9 @@ def regex_match(regex, text):
return pattern.search(text) is not None
def iPv46RegexCheck(ip):
pattern = r'(^\s*((([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))\s*$|' \
r'^\s*((([0-9a-f]{1,4}:){7}([0-9a-f]{1,4}|:))|(([0-9a-f]{1,4}:){6}(:[0-9a-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)' \
r'(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9a-f]{1,4}:){5}(((:[0-9a-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)' \
r'(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9a-f]{1,4}:){4}(((:[0-9a-f]{1,4}){1,3})|((:[0-9a-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)' \
r'(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9a-f]{1,4}:){3}(((:[0-9a-f]{1,4}){1,4})|((:[0-9a-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)' \
r'(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9a-f]{1,4}:){2}(((:[0-9a-f]{1,4}){1,5})|((:[0-9a-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)' \
r'(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9a-f]{1,4}:){1}(((:[0-9a-f]{1,4}){1,6})|((:[0-9a-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)' \
r'(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9a-f]{1,4}){1,7})|((:[0-9a-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)' \
r'(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*$)'
return re.match(pattern, ip)
return re.match(
r'((^\s*((([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))\s*$)|(^\s*((([0-9a-f]{1,4}:){7}([0-9a-f]{1,4}|:))|(([0-9a-f]{1,4}:){6}(:[0-9a-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9a-f]{1,4}:){5}(((:[0-9a-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9a-f]{1,4}:){4}(((:[0-9a-f]{1,4}){1,3})|((:[0-9a-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9a-f]{1,4}:){3}(((:[0-9a-f]{1,4}){1,4})|((:[0-9a-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9a-f]{1,4}:){2}(((:[0-9a-f]{1,4}){1,5})|((:[0-9a-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9a-f]{1,4}:){1}(((:[0-9a-f]{1,4}){1,6})|((:[0-9a-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9a-f]{1,4}){1,7})|((:[0-9a-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*$))',
ip)
class DashboardAPIKey:
def __init__(self, Key: str, CreatedAt: str, ExpiredAt: str):

View File

@ -87,6 +87,9 @@ _installPython(){
{ sudo yum install -y python3 net-tools ; printf "\n\n"; } >> ./log/install.txt
fi
;;
alpine)
{ apk update; apk add python3 net-tools python3-dev; printf "\n\n"; } &>> ./log/install.txt
;;
esac
if ! python3 --version > /dev/null 2>&1
@ -112,6 +115,9 @@ _installPythonVenv(){
{ sudo yum install -y python3-virtualenv; printf "\n\n"; } >> ./log/install.txt
fi
;;
alpine)
{ apk add python3 py3-virtualenv; printf "\n\n"; } &>> ./log/install.txt
;;
*)
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"
@ -166,6 +172,9 @@ _installPythonPip(){
{ sudo dnf install -y ${pythonExecutable}-pip; printf "\n\n"; } >> ./log/install.txt
fi
;;
alpine)
{ apk add py3-pip; printf "\n\n"; } &>> ./log/install.txt
;;
*)
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"