bunkerweb 1.4.0

This commit is contained in:
bunkerity
2022-06-03 17:24:14 +02:00
parent 3a078326c5
commit a9f886804a
5245 changed files with 1432051 additions and 27894 deletions

View File

@@ -0,0 +1,71 @@
#!/bin/bash
#
# A shell option that causes the shell to exit immediately if a command exits with a non-zero status.
set -e
# if directory /opt/bunkerweb/ exists then remove it
if [ -d /opt/bunkerweb/ ]; then
echo "Removing /opt/bunkerweb/ ..."
rm -rf /opt/bunkerweb/
echo "Done !"
fi
# if directory /etc/systemd/system/bunkerweb.service exists then remove it
if [ -f /etc/systemd/system/bunkerweb.service ]; then
echo "Removing /etc/systemd/system/bunkerweb.service ..."
rm -f /etc/systemd/system/bunkerweb.service
echo "Done !"
fi
# if directory /etc/systemd/system/bunkerweb-ui.service exists then remove it
if [ -f /etc/systemd/system/bunkerweb-ui.service ]; then
echo "Removing /etc/systemd/system/bunkerweb-ui.service ..."
rm -f /etc/systemd/system/bunkerweb-ui.service
echo "Done !"
fi
# Detect OS between Debian, Ubuntu, CentOS, Fedora
if [ -f /etc/debian_version ]; then
# Loop to erase all the files in /etc/nginx/
for i in $( ls /etc/nginx/ ); do
echo "Removing /etc/nginx/$i ..."
rm -rf /etc/nginx/$i
echo "Done !"
done
echo "If you want to reinstall nginx, please run the following command:"
echo "sudo apt-get install nginx"
elif [ -f /etc/centos-release ]; then
# Loop to erase all the files in /etc/nginx/
for i in $( ls /etc/nginx/ ); do
echo "Removing /etc/nginx/$i ..."
rm -rf /etc/nginx/$i
echo "Done !"
done
echo "If you want to reinstall nginx, please run the following command:"
echo "sudo yum install nginx"
elif [ -f /etc/fedora-release ]; then
# Loop to erase all the files in /etc/nginx/
for i in $( ls /etc/nginx/ ); do
echo "Removing /etc/nginx/$i ..."
rm -rf /etc/nginx/$i
echo "Done !"
done
echo "If you want to reinstall nginx, please run the following command:"
echo "sudo dnf install nginx"
elif [ -f /etc/lsb-release ]; then
# Loop to erase all the files in /etc/nginx/
for i in $( ls /etc/nginx/ ); do
echo "Removing /etc/nginx/$i ..."
rm -rf /etc/nginx/$i
echo "Done !"
done
echo "If you want to reinstall nginx, please run the following command:"
echo "sudo apt-get install nginx"
else
echo "Your OS is not supported"
exit 1
fi
# if previous command was successful then restart systemd unit
if [ $? -eq 0 ]; then
echo "Restarting systemd ..."
systemctl daemon-reload
echo "Done !"
fi

View File

@@ -0,0 +1,31 @@
#!/bin/bash
#
# A shell option that causes the shell to exit immediately if a command exits with a non-zero status.
set -e
#
# function to stop bunkerweb
function stopBunker()
{
echo "Stopping Bunkerweb service ..."
# Stop bunkerweb service
systemctl stop bunkerweb
}
function stopUI()
{
echo "Stopping bunkerweb-ui service ..."
# Stop flask server
systemctl stop bunkerweb-ui
echo "Done !"
}
#
# Check if bunkerweb service is running
if systemctl is-active --quiet bunkerweb; then
# Stop bunkerweb service
stopBunker
fi
# Check if bunkerweb-ui service is running
if systemctl is-active --quiet bunkerweb-ui; then
# Stop ui service
stopUI
fi

52
linux/scripts/bunkerweb-ui.sh Executable file
View File

@@ -0,0 +1,52 @@
#!/bin/bash
# function to start the UI
start_ui() {
export PYTHONPATH=/opt/bunkerweb/deps/python/
echo "Starting UI"
set -a
. /opt/bunkerweb/bunkerweb-ui.env
set +a
export FLASK_APP=/opt/bunkerweb/ui/main.py
python3 -m flask run --host=127.0.0.1 --port=7000
}
# function to stop the UI
stop_ui(){
echo "Stoping ui service ..."
# Check if pid file exist and remove it if so
PID_FILE_PATH="/opt/bunkerweb/tmp/ui.pid"
if [ -f "$PID_FILE_PATH" ];
then
var=$( cat $PID_FILE_PATH )
kill -SIGINT $var
echo "Killing : $var"
else
echo "File doesn't exist"
fi
}
# function reload the UI
reload_ui() {
stop_ui
# Wait for ui to stop
sleep 5
start_ui
# if previous command worked then exit with 0
exit 0
}
case "$1" in
start)
start_ui
;;
stop)
stop_ui
;;
reload)
reload_ui
;;
*)
echo "Usage: ./bunkerweb-ui.sh start|stop|reload"
;;
esac

View File

@@ -0,0 +1,16 @@
#!/bin/bash
# Stop nginx if it's running and remove the old config file if it exists
systemctl stop nginx
# Change the ownership of /opt/bunkerweb to nginx
chown -R nginx:nginx /opt/bunkerweb
# Change the ownership of bunkerweb.service to nginx
chown nginx:nginx /etc/systemd/system/bunkerweb.service
# Start bunkerweb service as nginx user and enable it to start on boot
systemctl enable bunkerweb
systemctl start bunkerweb
systemctl enable bunkerweb-ui
systemctl start bunkerweb-ui

19
linux/scripts/purge.sh Normal file
View File

@@ -0,0 +1,19 @@
#! /bin/sh
# A shell option that causes the shell to exit immediately if a command exits with a non-zero status.
set -e
# if purge is called, we want to remove the old config file from the system
# if it exists
if [ "$1" = "purge" ]; then
# purge bunkerweb
sudo systemctl stop bunkerweb
sudo systemctl disable bunkerweb
sudo rm -rf /opt/bunkerweb/
sudo rm -rf /etc/systemd/system/bunkerweb.service
sudo rm -rf /etc/systemd/system/bunkerweb-ui.service
# reload unit files
sudo systemctl daemon-reload
fi

234
linux/scripts/start.sh Normal file
View File

@@ -0,0 +1,234 @@
#!/bin/bash
#############################################################
# Help #
#############################################################
Help()
{
# Dispaly Help
echo "Usage of this script"
echo
echo "Syntax: start [start|stop|reload]"
echo "options:"
echo "start Create the configurations file and run all the jobs needed throught the bunkerweb service."
echo "stop Stop the bunkerweb service."
echo "reload Reload the bunkerweb service"
echo
}
export PYTHONPATH=/opt/bunkerweb/deps/python/
# Add nginx to sudoers
if [ ! -f /etc/sudoers.d/nginx ]; then
echo "Adding nginx user to sudoers file ..."
echo "nginx ALL=(ALL) NOPASSWD: /bin/systemctl restart bunkerweb" >> /etc/sudoers.d/nginx
echo "Done !"
fi
#############################################################
# Checker #
#############################################################
function check_ok() {
if [ $? -eq 0 ]; then
result="$?"
else
result="$?"
fi
}
#############################################################
# Start #
#############################################################
function start() {
#############################################
# STEP1 #
# Generate variables.env files to /tmp/ #
#############################################
printf "HTTP_PORT=80\nSERVER_NAME=example.com\nTEMP_NGINX=yes\nUSE_BUNKERNET=no" > "/tmp/variables.env"
# Test if command worked
check_ok
# Exit if failed
if [ $result -ne 0 ];
then
echo "Your command exited with non-zero status $result"
exit 1
fi
echo "Generate variables.env files to /tmp/"
echo "OK !"
#############################################
# STEP2 #
# Generate first temporary config #
#############################################
/opt/bunkerweb/gen/main.py --settings /opt/bunkerweb/settings.json --templates /opt/bunkerweb/confs --output /etc/nginx --variables /tmp/variables.env
# Test if command worked
check_ok
# Exit if failed
if [ $result -ne 0 ];
then
echo "Your command exited with non-zero status $result"
exit 1
fi
echo "Generate first temporary config"
echo "OK !"
#############################################
# STEP3 #
# Execute nginx #
#############################################
nginx
# Test if command worked
check_ok
# Exit if failed
if [ $result -ne 0 ];
then
echo "Your command exited with non-zero status $result"
exit 1
fi
echo "Execute nginx"
echo "OK !"
#############################################
# STEP4 #
# Run jobs script #
#############################################
/opt/bunkerweb/job/main.py --variables /etc/nginx/variables.env --run
# Test if command worked
check_ok
# Exit if failed
if [ $result -ne 0 ];
then
echo "Your command exited with non-zero status $result"
exit 1
fi
echo "Run jobs script"
echo "OK !"
#############################################
# STEP5 #
# Quit nginx #
#############################################
nginx -s quit
# Test if command worked
check_ok
# Exit if failed
if [ $result -ne 0 ];
then
echo "Your command exited with non-zero status $result"
exit 1
fi
echo "Quit nginx"
echo "OK !"
#############################################
# STEP6 #
# Generate final confs #
#############################################
/opt/bunkerweb/gen/main.py --settings /opt/bunkerweb/settings.json --templates /opt/bunkerweb/confs --output /etc/nginx --variables /opt/bunkerweb/variables.env
# Test if command worked
check_ok
# Exit if failed
if [ $result -ne 0 ];
then
echo "Your command exited with non-zero status $result"
exit 1
fi
echo "Generate final confs"
echo "OK !"
#############################################
# STEP7 #
# Run jobs infinite #
#############################################
/opt/bunkerweb/job/main.py --variables /etc/nginx/variables.env &
# Test if command worked
check_ok
# Exit if failed
if [ $result -ne 0 ];
then
echo "Your command exited with non-zero status $result"
exit 1
fi
echo "Run jobs infinite"
echo "OK !"
#############################################
# STEP8 #
# Start nginx #
#############################################
nginx -g "daemon off; user nginx;"
# Test if command worked
check_ok
# Exit if failed
if [ $result -ne 0 ];
then
echo "Your command exited with non-zero status $result"
exit 1
fi
echo "Start nginx"
echo "OK !"
}
function stop()
{
echo "Stoping Bunkerweb service ..."
# Check if pid file exist and remove it if so
PID_FILE_PATH="/opt/bunkerweb/tmp/scheduler.pid"
if [ -f "$PID_FILE_PATH" ];
then
var=$( cat $PID_FILE_PATH )
kill -SIGINT $var
echo "Killing : $var"
else
echo "File doesn't exist"
fi
# Check if nginx running and if so, stop it
SERVICE="nginx"
if pgrep -x "$SERVICE" >/dev/null
then
echo "Stopping $SERVICE service"
nginx -s stop
else
echo "$SERVICE already stopped"
fi
echo "Done !"
}
function reload()
{
echo "Reloading Bunkerweb service ..."
# Check if pid file exist and remove it if so
PID_FILE_PATH="/opt/bunkerweb/tmp/scheduler.pid"
if [ -f "$PID_FILE_PATH" ];
then
var=$( cat $PID_FILE_PATH )
kill -SIGHUP $var
echo "Reloading : $var"
else
echo "File doesn't exist"
fi
# Check if nginx running and if so, reload it
SERVICE="nginx"
if pgrep -x "$SERVICE" >/dev/null
then
echo "Reloading $SERVICE service"
nginx -s reload
else
echo "$SERVICE already stopped"
fi
echo "Done !"
}
# List of differents args
case $1 in
"start")
start
;;
"stop")
stop
;;
"reload")
reload
;;
*)
echo "Invalid option!"
echo "List of option available:"
Help
esac