HTTPS workflow

This commit is contained in:
Donald Zou 2024-12-12 16:25:54 +08:00
parent 907a142c8d
commit 9e93f8c2a5
2 changed files with 59 additions and 22 deletions

View File

@ -1,15 +1,15 @@
import dashboard import os.path
import dashboard, configparser
from datetime import datetime from datetime import datetime
global sqldb, cursor, DashboardConfig, WireguardConfigurations, AllPeerJobs, JobLogger global sqldb, cursor, DashboardConfig, WireguardConfigurations, AllPeerJobs, JobLogger
app_host, app_port = dashboard.gunicornConfig() app_host, app_port = dashboard.gunicornConfig()
date = datetime.today().strftime('%Y_%m_%d_%H_%M_%S') date = datetime.today().strftime('%Y_%m_%d_%H_%M_%S')
def post_worker_init(worker): def post_worker_init(worker):
dashboard.startThreads() dashboard.startThreads()
worker_class = 'gthread' worker_class = 'gthread'
workers = 1 workers = 1
threads = 1 threads = 1
@ -21,6 +21,19 @@ accesslog = f"./log/access_{date}.log"
log_level = "debug" log_level = "debug"
capture_output = True capture_output = True
errorlog = f"./log/error_{date}.log" errorlog = f"./log/error_{date}.log"
if os.path.exists("./ssl.ini"):
sslConfig = configparser.ConfigParser()
sslConfig.read_file(open('./ssl.ini', 'r'))
if sslConfig.has_section('SSL'):
cert = sslConfig.get('SSL', 'certificate_path')
pem = sslConfig.get('SSL', 'private_key_path')
if cert and pem and len(cert) > 0 and len(pem) > 0:
certfile = cert
keyfile = pem
print(f"[WGDashboard] HTTPS enable", flush=True)
print(f"[WGDashboard] WGDashboard w/ Gunicorn will be running on {bind}", flush=True) print(f"[WGDashboard] WGDashboard w/ Gunicorn will be running on {bind}", flush=True)
print(f"[WGDashboard] Access log file is at {accesslog}", flush=True) print(f"[WGDashboard] Access log file is at {accesslog}", flush=True)
print(f"[WGDashboard] Error log file is at {errorlog}", flush=True) print(f"[WGDashboard] Error log file is at {errorlog}", flush=True)

View File

@ -11,8 +11,9 @@ venv_python="./venv/bin/python3"
venv_gunicorn="./venv/bin/gunicorn" venv_gunicorn="./venv/bin/gunicorn"
pythonExecutable="python3" pythonExecutable="python3"
heavy_checkmark=$(printf "\xE2\x9C\x94") heavy_checkmark=$(printf "\xE2\x9C\x85")
heavy_crossmark=$(printf "\xE2\x9C\x97") heavy_crossmark=$(printf "\xE2\x9D\x8C")
install=$(printf "\xF0\x9F\x92\xBF")
PID_FILE=./gunicorn.pid PID_FILE=./gunicorn.pid
environment=$(if [[ $ENVIRONMENT ]]; then echo $ENVIRONMENT; else echo 'develop'; fi) environment=$(if [[ $ENVIRONMENT ]]; then echo $ENVIRONMENT; else echo 'develop'; fi)
@ -46,7 +47,7 @@ help () {
_check_and_set_venv(){ _check_and_set_venv(){
VIRTUAL_ENV="./venv" VIRTUAL_ENV="./venv"
if [ ! -d $VIRTUAL_ENV ]; then if [ ! -d $VIRTUAL_ENV ]; then
printf "[WGDashboard] Creating Python Virtual Environment under ./venv\n" printf "[WGDashboard] %s Creating Python Virtual Environment under ./venv\n" "$install"
{ $pythonExecutable -m venv $VIRTUAL_ENV; } >> ./log/install.txt { $pythonExecutable -m venv $VIRTUAL_ENV; } >> ./log/install.txt
fi fi
@ -74,9 +75,8 @@ _determineOS(){
} }
_installPython(){ _installPython(){
{ printf "\n\n [Installing Python] [%s] \n\n""$(date)"; } >> ./log/install.txt { printf "\n\n [Installing Python] [%s] \n\n""$(date)"; } >> ./log/install.txt
printf "[WGDashboard] %s Installing Python\n" "$install"
case "$OS" in case "$OS" in
ubuntu|debian) ubuntu|debian)
{ sudo apt update ; sudo apt-get install -y python3 net-tools; printf "\n\n"; } >> ./log/install.txt { sudo apt update ; sudo apt-get install -y python3 net-tools; printf "\n\n"; } >> ./log/install.txt
@ -105,6 +105,7 @@ _installPython(){
_installPythonVenv(){ _installPythonVenv(){
{ printf "\n\n [Installing Python Venv] [%s] \n\n""$(date)"; } >> ./log/install.txt { printf "\n\n [Installing Python Venv] [%s] \n\n""$(date)"; } >> ./log/install.txt
printf "[WGDashboard] %s Installing Python Virtual Environment\n" "$install"
if [ "$pythonExecutable" = "python3" ]; then if [ "$pythonExecutable" = "python3" ]; then
case "$OS" in case "$OS" in
ubuntu|debian) ubuntu|debian)
@ -145,8 +146,10 @@ _installPythonVenv(){
_installPythonPip(){ _installPythonPip(){
{ printf "\n\n [Installing Python Pip] [%s] \n\n""$(date)"; } >> ./log/install.txt { printf "\n\n [Installing Python Pip] [%s] \n\n""$(date)"; } >> ./log/install.txt
if ! $pythonExecutable -m pip -h > /dev/null 2>&1 if ! $pythonExecutable -m pip -h > /dev/null 2>&1
then then
printf "[WGDashboard] %s Installing Python Package Manager (PIP)\n" "$install"
case "$OS" in case "$OS" in
ubuntu|debian) ubuntu|debian)
if [ "$pythonExecutable" = "python3" ]; then if [ "$pythonExecutable" = "python3" ]; then
@ -186,24 +189,28 @@ _installPythonPip(){
_checkWireguard(){ _checkWireguard(){
if ! command -v wg > /dev/null 2>&1 || ! command -v wg-quick > /dev/null 2>&1 if ! command -v wg > /dev/null 2>&1 || ! command -v wg-quick > /dev/null 2>&1
then then
printf "[WGDashboard] %s Installing WireGuard\n" "$install"
case "$OS" in case "$OS" in
ubuntu|debian) ubuntu|debian)
{ {
sudo apt update && sudo apt-get install -y wireguard; sudo apt update && sudo apt-get install -y wireguard;
printf "\n[WGDashboard] WireGuard installed on %s.\n\n" "$OS"; printf "\n[WGDashboard] WireGuard installed on %s.\n\n" "$OS";
} &>> ./log/install.txt } &>> ./log/install.txt
printf "[WGDashboard] %s WireGuard is successfully installed.\n" "$heavy_checkmark"
;; ;;
centos|fedora|redhat|rhel|almalinux|rocky) centos|fedora|redhat|rhel|almalinux|rocky)
{ {
sudo dnf install -y wireguard-tools; sudo dnf install -y wireguard-tools;
printf "\n[WGDashboard] WireGuard installed on %s.\n\n" "$OS"; printf "\n[WGDashboard] WireGuard installed on %s.\n\n" "$OS";
} &>> ./log/install.txt } &>> ./log/install.txt
printf "[WGDashboard] %s WireGuard is successfully installed.\n" "$heavy_checkmark"
;; ;;
alpine) alpine)
{ {
sudo apk update && sudo apk add wireguard-tools --no-cache; sudo apk update && sudo apk add wireguard-tools --no-cache;
printf "\n[WGDashboard] WireGuard installed on %s.\n\n" "$OS"; printf "\n[WGDashboard] WireGuard installed on %s.\n\n" "$OS";
} &>> ./log/install.txt } &>> ./log/install.txt
printf "[WGDashboard] %s WireGuard is successfully installed.\n" "$heavy_checkmark"
;; ;;
*) *)
printf "[WGDashboard] %s Sorry, your OS is not supported. Currently, the install script only supports Debian-based, Red Hat-based, and Alpine Linux.\n" "$heavy_crossmark" printf "[WGDashboard] %s Sorry, your OS is not supported. Currently, the install script only supports Debian-based, Red Hat-based, and Alpine Linux.\n" "$heavy_crossmark"
@ -212,7 +219,7 @@ _checkWireguard(){
;; ;;
esac esac
else else
printf "[WGDashboard] WireGuard is already installed.\n" printf "[WGDashboard] %s WireGuard is already installed.\n" "$heavy_checkmark"
fi fi
} }
@ -224,6 +231,7 @@ _checkPythonVersion(){
version=$($pythonExecutable --version) version=$($pythonExecutable --version)
if [ $version_pass == "1" ] if [ $version_pass == "1" ]
then then
printf "[WGDashboard] %s Found compatible version of Python. Will be using %s to install WGDashboard.\n" "$heavy_checkmark" "$($pythonExecutable --version)"
return; return;
elif python3.10 --version > /dev/null 2>&1 elif python3.10 --version > /dev/null 2>&1
then then
@ -245,7 +253,7 @@ _checkPythonVersion(){
} }
_determinePypiMirror(){ _determinePypiMirror(){
printf "[WGDashboard] Pinging list of recommended Python Package Index mirror\n" printf "[WGDashboard] %s Pinging list of recommended Python Package Index mirror\n" "$install"
urls=( urls=(
"https://pypi.org/simple/" "https://pypi.org/simple/"
"https://pypi.tuna.tsinghua.edu.cn/simple/" "https://pypi.tuna.tsinghua.edu.cn/simple/"
@ -256,6 +264,7 @@ _determinePypiMirror(){
# Function to extract hostname and ping it # Function to extract hostname and ping it
index=1 index=1
printf " ---------------------------------------------------------\n"
for url in "${urls[@]}"; do for url in "${urls[@]}"; do
# Extract the hostname from the URL # Extract the hostname from the URL
hostname=$(echo "$url" | awk -F/ '{print $3}') hostname=$(echo "$url" | awk -F/ '{print $3}')
@ -264,15 +273,20 @@ _determinePypiMirror(){
# Handle cases where the hostname is not reachable # Handle cases where the hostname is not reachable
if [ -z "$rtt" ]; then if [ -z "$rtt" ]; then
rtt="9999" rtt="9999"
printf "\t [%i] [FAILED] %s\n" "$index" "$url" printf " [%i] [FAILED] %s\n" "$index" "$url"
else else
printf "\t [%i] %sms %s\n" "$index" "$rtt" "$url" printf " [%i] %sms %s\n" "$index" "$rtt" "$url"
fi fi
index=$((index+1)) index=$((index+1))
done done
read -p "[WGDashboard] Enter the number of the Python Package Index mirror you would like to use (Hit Enter to skip and use default mirror): " choice printf "\n"
printf " Which mirror you would like to use (Hit enter to use default): "
read -r choice
printf " ---------------------------------------------------------\n"
if [[ "$choice" =~ ^[0-9]+$ ]] && (( choice >= 1 && choice <= ${#urls[@]} )); then if [[ "$choice" =~ ^[0-9]+$ ]] && (( choice >= 1 && choice <= ${#urls[@]} )); then
selected_url="${urls[choice-1]}" selected_url="${urls[choice-1]}"
@ -313,18 +327,28 @@ install_wgd(){
_checkWireguard _checkWireguard
sudo chmod -R 755 /etc/wireguard/ sudo chmod -R 755 /etc/wireguard/
if [ ! -d "db" ]
then
printf "[WGDashboard] Creating ./db folder\n"
mkdir "db"
fi
_check_and_set_venv _check_and_set_venv
printf "[WGDashboard] Upgrading Python Package Manage (PIP)\n" printf "[WGDashboard] %s Upgrading Python Package Manage (PIP)\n" "$install"
{ date; python3 -m ensurepip --upgrade; printf "\n\n"; } >> ./log/install.txt { date; python3 -m ensurepip --upgrade; printf "\n\n"; } >> ./log/install.txt
{ date; python3 -m pip install --upgrade pip -i "$selected_url"; printf "\n\n"; } >> ./log/install.txt { date; python3 -m pip install --upgrade pip -i "$selected_url"; printf "\n\n"; } >> ./log/install.txt
printf "[WGDashboard] Installing latest Python dependencies\n" printf "[WGDashboard] %s Installing latest Python dependencies\n" "$install"
{ date; python3 -m pip install -r requirements.txt -i "$selected_url"; printf "\n\n"; } >> ./log/install.txt #This all works on the default installation. { date; python3 -m pip install -r requirements.txt -i "$selected_url"; printf "\n\n"; } >> ./log/install.txt #This all works on the default installation.
printf "[WGDashboard] WGDashboard installed successfully!\n"
if [ ! -d "db" ]
then
mkdir "db"
printf "[WGDashboard] %s Created ./db folder\n" "$heavy_checkmark"
else
printf "[WGDashboard] %s Found existing ./db folder\n" "$heavy_checkmark"
fi
if [ ! -f "ssl.ini" ]
then
printf "[SSL]\ncertificate_path = \nprivate_key_path = \n" >> ssl.ini
printf "[WGDashboard] %s Created ssl.ini\n" "$heavy_checkmark"
else
printf "[WGDashboard] %s Found existing ssl.ini\n" "$heavy_checkmark"
fi
printf "[WGDashboard] %s WGDashboard installed successfully!\n" "$heavy_checkmark"
printf "[WGDashboard] Enter ./wgd.sh start to start the dashboard\n" printf "[WGDashboard] Enter ./wgd.sh start to start the dashboard\n"
} }