diff --git a/src/certbot.ini b/src/certbot.ini new file mode 100644 index 0000000..cf9b7a2 --- /dev/null +++ b/src/certbot.ini @@ -0,0 +1,4 @@ +authenticator = standalone +noninteractive = true +agree-tos = true +rsa-key-size = 2048 \ No newline at end of file diff --git a/src/dashboard.py b/src/dashboard.py index 0cbddf9..2cc8844 100644 --- a/src/dashboard.py +++ b/src/dashboard.py @@ -1670,13 +1670,37 @@ def check_update(): return result +""" +Configure DashBoard before start web-server +""" +def run_dashboard(): + init_dashboard() + global update + update = check_update() + global config + config = configparser.ConfigParser(strict=False) + config.read('wg-dashboard.ini') + global app_ip + app_ip = config.get("Server", "app_ip") + global app_port + app_port = config.get("Server", "app_port") + global wg_conf_path + wg_conf_path = config.get("Server", "wg_conf_path") + config.clear() + return app + +""" +Get host and port for web-server +""" +def get_host_bind(): + init_dashboard() + config = configparser.ConfigParser(strict=False) + config.read('wg-dashboard.ini') + app_ip = config.get("Server", "app_ip") + app_port = config.get("Server", "app_port") + + return app_ip, app_port if __name__ == "__main__": - init_dashboard() - UPDATE = check_update() - configuration_settings = get_dashboard_conf() - app_ip = configuration_settings.get("Server", "app_ip") - app_port = int(configuration_settings.get("Server", "app_port")) - WG_CONF_PATH = configuration_settings.get("Server", "wg_conf_path") - configuration_settings.clear() + run_dashboard() app.run(host=app_ip, debug=False, port=app_port) diff --git a/src/gunicorn.conf.py b/src/gunicorn.conf.py new file mode 100644 index 0000000..56b82ce --- /dev/null +++ b/src/gunicorn.conf.py @@ -0,0 +1,11 @@ +import multiprocessing +import dashboard + +app_host, app_port = dashboard.get_host_bind() + +worker_class = 'gthread' +workers = multiprocessing.cpu_count() * 2 + 1 +threads = 4 +bind = f"{app_host}:{app_port}" +daemon = True +pidfile = './gunicorn.pid' diff --git a/src/requirements.txt b/src/requirements.txt index b590667..6bf8c79 100644 --- a/src/requirements.txt +++ b/src/requirements.txt @@ -2,4 +2,6 @@ Flask tinydb==4.5.2 ifcfg icmplib -flask-qrcode \ No newline at end of file +flask-qrcode +gunicorn +certbot \ No newline at end of file diff --git a/src/wgd.sh b/src/wgd.sh index 15632fb..3f4b21c 100755 --- a/src/wgd.sh +++ b/src/wgd.sh @@ -4,6 +4,15 @@ # Under Apache-2.0 License app_name="dashboard.py" app_official_name="WGDashboard" +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='============================================================' help () { @@ -57,28 +66,85 @@ install_wgd(){ check_wgd_status(){ - if ps aux | grep '[p]ython3 '$app_name > /dev/null; - then + if [[ $environment == 'production' ]]; then + if ps aux | grep -v grep | grep $(cat ./gunicorn.pid) > /dev/null; then return 0 - else - return 1 + 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 } -start_wgd () { - printf "%s\n" "$dashes" - printf "| Starting WGDashboard in the background. |\n" - if [ ! -d "log" ] - then mkdir "log" +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 () { + if [[ $SSL ]]; then + if [ ! -d $cb_config_dir ]; then + certbot_create_ssl + else + certbot_renew_ssl + fi + fi + printf "%s\n" "$dashes" + printf "| Starting WGDashboard in the background. |\n" + if [ ! -d "log" ]; then + mkdir "log" + fi + d=$(date '+%Y%m%d%H%M%S') + if [[ $USER == root ]]; then + export PATH=$PATH:/usr/local/bin:$HOME/.local/bin + fi + if [[ $SSL ]]; then + gunicorn --certfile $cb_config_dir/live/"$SERVERURL"/cert.pem \ + --keyfile $cb_config_dir/live/"$SERVERURL"/privkey.pem \ + --access-logfile log/access_"$d".log \ + --error-logfile log/error_"$d".log 'dashboard:run_dashboard()' + else + gunicorn --access-logfile log/access_"$d".log \ + --error-logfile log/error_"$d".log 'dashboard:run_dashboard()' + fi + printf "| Log files is under log/ |\n" + printf "%s\n" "$dashes" +} + +gunicorn_stop () { + kill $(cat ./gunicorn.pid) +} + +start_wgd () { + if [[ $environment == 'production' ]]; then + gunicorn_start + else + printf "%s\n" "$dashes" + printf "| Starting WGDashboard in the background. |\n" + if [ ! -d "log" ] + then mkdir "log" + fi + d=$(date '+%Y%m%d%H%M%S') + python3 "$app_name" > log/"$d".txt 2>&1 & + printf "| Log files is under log/ |\n" + printf "%s\n" "$dashes" fi - d=$(date '+%Y%m%d%H%M%S') - python3 "$app_name" > log/"$d".txt 2>&1 & - printf "| Log files is under log/ |\n" - printf "%s\n" "$dashes" } stop_wgd() { - kill "$(ps aux | grep "[p]ython3 $app_name" | awk '{print $2}')" + if [[ $environment == 'production' ]]; then + gunicorn_stop + else + kill "$(ps aux | grep "[p]ython3 $app_name" | awk '{print $2}')" + fi } start_wgd_debug() {