Skip to content

Commit e218532

Browse files
committed
Did some refactor on wgd.sh
1 parent c012b8c commit e218532

File tree

1 file changed

+89
-103
lines changed

1 file changed

+89
-103
lines changed

src/wgd.sh

+89-103
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,6 @@ _determineOS(){
6565
OS=$ID
6666
elif [ -f /etc/redhat-release ]; then
6767
OS="redhat"
68-
# elif [ -f /etc/arch-release ]; then
69-
# OS="arch"
7068
else
7169
printf "[WGDashboard] %s Sorry, your OS is not supported. Currently the install script only support Debian-based, Red Hat-based OS." "$heavy_crossmark"
7270
printf "%s\n" "$helpMsg"
@@ -129,18 +127,6 @@ _installPythonVenv(){
129127
ubuntu|debian)
130128
{ sudo apt-get update; sudo apt-get install ${pythonExecutable}-venv; } &>> ./log/install.txt
131129
;;
132-
# centos|fedora|redhat|rhel)
133-
# if command -v dnf &> /dev/null; then
134-
# { sudo dnf install -y ${pythonExecutable}-virtualenv; printf "\n\n"; } >> ./log/install.txt
135-
# else
136-
# { sudo yum install -y ${pythonExecutable}-virtualenv; printf "\n\n"; } >> ./log/install.txt
137-
# fi
138-
# ;;
139-
# *)
140-
# printf "[WGDashboard] %s Sorry, your OS is not supported. Currently the install script only support Debian-based, Red Hat-based OS.\n" "$heavy_crossmark"
141-
# printf "%s\n" "$helpMsg"
142-
# kill $TOP_PID
143-
# ;;
144130
esac
145131
fi
146132

@@ -257,9 +243,9 @@ install_wgd(){
257243
_installPythonPip
258244

259245
if [ ! -d "db" ]
260-
then
261-
printf "[WGDashboard] Creating ./db folder\n"
262-
mkdir "db"
246+
then
247+
printf "[WGDashboard] Creating ./db folder\n"
248+
mkdir "db"
263249
fi
264250
_check_and_set_venv
265251
printf "[WGDashboard] Upgrading Python Package Manage (PIP)\n"
@@ -288,11 +274,11 @@ check_wgd_status(){
288274
}
289275

290276
certbot_create_ssl () {
291-
certbot certonly --config ./certbot.ini --email "$EMAIL" --work-dir $cb_work_dir --config-dir $cb_config_dir --domain "$SERVERURL"
277+
certbot certonly --config ./certbot.ini --email "$EMAIL" --work-dir $cb_work_dir --config-dir $cb_config_dir --domain "$SERVERURL"
292278
}
293279

294280
certbot_renew_ssl () {
295-
certbot renew --work-dir $cb_work_dir --config-dir $cb_config_dir
281+
certbot renew --work-dir $cb_work_dir --config-dir $cb_config_dir
296282
}
297283

298284
gunicorn_start () {
@@ -319,7 +305,7 @@ gunicorn_start () {
319305
}
320306

321307
gunicorn_stop () {
322-
sudo kill $(cat ./gunicorn.pid)
308+
sudo kill $(cat ./gunicorn.pid)
323309
}
324310

325311
start_wgd () {
@@ -328,40 +314,41 @@ start_wgd () {
328314
}
329315

330316
stop_wgd() {
331-
if test -f "$PID_FILE"; then
332-
gunicorn_stop
333-
else
334-
kill "$(ps aux | grep "[p]ython3 $app_name" | awk '{print $2}')"
335-
fi
317+
if test -f "$PID_FILE"; then
318+
gunicorn_stop
319+
else
320+
kill "$(ps aux | grep "[p]ython3 $app_name" | awk '{print $2}')"
321+
fi
336322
}
337323

338324
startwgd_docker() {
339325
_checkWireguard
340-
printf "[WGDashboard] WireGuard Config Started\n"
326+
printf "[WGDashboard][Docker] WireGuard configuration started\n"
341327
{ date; start_core ; printf "\n\n"; } >> ./log/install.txt
342328
gunicorn_start
343329
}
344-
start_core() {
345-
local iptable_dir="/opt/wireguarddashboard/src/iptable-rules"
346-
# Check if wg0.conf exists in /etc/wireguard
347-
if [[ ! -f /etc/wireguard/wg0.conf ]]; then
348-
echo "wg0.conf not found. Running Generate Configuration."
349-
newconf_wgd
350-
else
351-
echo "wg0.conf already exists. Skipping WireGuard Config Generation."
352-
fi
353-
# Re-assign config_files to ensure it includes any newly created configurations
354-
local config_files=$(find /etc/wireguard -type f -name "*.conf")
355-
356-
# Set file permissions
357-
find /etc/wireguard -type f -name "*.conf" -exec chmod 600 {} \;
358-
find "$iptable_dir" -type f -name "*.sh" -exec chmod +x {} \;
359330

360-
# Start WireGuard for each config file
361-
for file in $config_files; do
362-
config_name=$(basename "$file" ".conf")
363-
wg-quick up "$config_name"
364-
done
331+
start_core() {
332+
local iptable_dir="/opt/wireguarddashboard/src/iptable-rules"
333+
# Check if wg0.conf exists in /etc/wireguard
334+
if [[ ! -f /etc/wireguard/wg0.conf ]]; then
335+
echo "[WGDashboard][Docker] wg0.conf not found. Running generate configuration."
336+
newconf_wgd
337+
else
338+
echo "[WGDashboard][Docker] wg0.conf already exists. Skipping WireGuard configuration generation."
339+
fi
340+
# Re-assign config_files to ensure it includes any newly created configurations
341+
local config_files=$(find /etc/wireguard -type f -name "*.conf")
342+
343+
# Set file permissions
344+
find /etc/wireguard -type f -name "*.conf" -exec chmod 600 {} \;
345+
find "$iptable_dir" -type f -name "*.sh" -exec chmod +x {} \;
346+
347+
# Start WireGuard for each config file
348+
for file in $config_files; do
349+
config_name=$(basename "$file" ".conf")
350+
wg-quick up "$config_name"
351+
done
365352
}
366353

367354

@@ -383,15 +370,14 @@ EOF
383370
}
384371

385372
start_wgd_debug() {
386-
printf "%s\n" "$dashes"
387-
_checkWireguard
388-
printf "[WGDashboard] Starting WGDashboard in the foreground.\n"
389-
sudo "$venv_python" "$app_name"
390-
printf "%s\n" "$dashes"
373+
printf "%s\n" "$dashes"
374+
_checkWireguard
375+
printf "[WGDashboard] Starting WGDashboard in the foreground.\n"
376+
sudo "$venv_python" "$app_name"
377+
printf "%s\n" "$dashes"
391378
}
392379

393380
update_wgd() {
394-
395381
_determineOS
396382
if ! python3 --version > /dev/null 2>&1
397383
then
@@ -430,55 +416,55 @@ update_wgd() {
430416
}
431417

432418
if [ "$#" != 1 ];
433-
then
434-
help
435-
else
436-
if [ "$1" = "start" ]; then
437-
if check_wgd_status; then
438-
printf "%s\n" "$dashes"
439-
printf "[WGDashboard] WGDashboard is already running.\n"
440-
printf "%s\n" "$dashes"
441-
else
442-
start_wgd
443-
fi
444-
elif [ "$1" = "docker_start" ]; then
445-
printf "%s\n" "$dashes"
446-
startwgd_docker
447-
printf "%s\n" "$dashes"
448-
elif [ "$1" = "stop" ]; then
449-
if check_wgd_status; then
450-
printf "%s\n" "$dashes"
451-
stop_wgd
452-
printf "[WGDashboard] WGDashboard is stopped.\n"
453-
printf "%s\n" "$dashes"
454-
else
455-
printf "%s\n" "$dashes"
456-
printf "[WGDashboard] WGDashboard is not running.\n"
457-
printf "%s\n" "$dashes"
458-
fi
459-
elif [ "$1" = "update" ]; then
460-
update_wgd
461-
elif [ "$1" = "install" ]; then
462-
printf "%s\n" "$dashes"
463-
install_wgd
464-
printf "%s\n" "$dashes"
465-
elif [ "$1" = "restart" ]; then
466-
if check_wgd_status; then
467-
printf "%s\n" "$dashes"
468-
stop_wgd
469-
printf "| WGDashboard is stopped. |\n"
470-
sleep 4
471-
start_wgd
472-
else
473-
start_wgd
474-
fi
475-
elif [ "$1" = "debug" ]; then
476-
if check_wgd_status; then
477-
printf "| WGDashboard is already running. |\n"
478-
else
479-
start_wgd_debug
480-
fi
481-
else
482-
help
483-
fi
419+
then
420+
help
421+
else
422+
if [ "$1" = "start" ]; then
423+
if check_wgd_status; then
424+
printf "%s\n" "$dashes"
425+
printf "[WGDashboard] WGDashboard is already running.\n"
426+
printf "%s\n" "$dashes"
427+
else
428+
start_wgd
429+
fi
430+
elif [ "$1" = "docker_start" ]; then
431+
printf "%s\n" "$dashes"
432+
startwgd_docker
433+
printf "%s\n" "$dashes"
434+
elif [ "$1" = "stop" ]; then
435+
if check_wgd_status; then
436+
printf "%s\n" "$dashes"
437+
stop_wgd
438+
printf "[WGDashboard] WGDashboard is stopped.\n"
439+
printf "%s\n" "$dashes"
440+
else
441+
printf "%s\n" "$dashes"
442+
printf "[WGDashboard] WGDashboard is not running.\n"
443+
printf "%s\n" "$dashes"
444+
fi
445+
elif [ "$1" = "update" ]; then
446+
update_wgd
447+
elif [ "$1" = "install" ]; then
448+
printf "%s\n" "$dashes"
449+
install_wgd
450+
printf "%s\n" "$dashes"
451+
elif [ "$1" = "restart" ]; then
452+
if check_wgd_status; then
453+
printf "%s\n" "$dashes"
454+
stop_wgd
455+
printf "[WGDashboard] WGDashboard is stopped.\n"
456+
sleep 4
457+
start_wgd
458+
else
459+
start_wgd
460+
fi
461+
elif [ "$1" = "debug" ]; then
462+
if check_wgd_status; then
463+
printf "[WGDashboard] WGDashboard is already running.\n"
464+
else
465+
start_wgd_debug
466+
fi
467+
else
468+
help
469+
fi
484470
fi

0 commit comments

Comments
 (0)