From 54054b4f6de1626b26692adb3c3b7870a32dcd2e Mon Sep 17 00:00:00 2001 From: mcinj <98779161+mcinj@users.noreply.github.com> Date: Sat, 14 May 2022 00:47:15 -0400 Subject: [PATCH] docker. basic auth. --- Dockerfile | 2 +- config/config.ini.example | 18 +++++++++++++++++- requirements.txt | 4 +++- src/ConfigReader.py | 7 ++++++- src/log.py | 2 +- src/run.py | 36 ++++++++++++++++++++++++++++++------ 6 files changed, 58 insertions(+), 11 deletions(-) diff --git a/Dockerfile b/Dockerfile index ebc1613..7835b0f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -3,7 +3,7 @@ FROM python:3.9-alpine RUN mkdir -p /app WORKDIR /app -RUN apk add tzdata --no-cache +RUN apk add tzdata build-base libffi-dev py3-cffi --no-cache ENV TZ=America/New_York ENV VIRTUAL_ENV=/app/env diff --git a/config/config.ini.example b/config/config.ini.example index 2fb7b1d..6d0fee8 100644 --- a/config/config.ini.example +++ b/config/config.ini.example @@ -31,4 +31,20 @@ pushover.enabled = false # your specific pushover token pushover.token = # your specific pushover user key -pushover.user_key = \ No newline at end of file +pushover.user_key = + +[WEB] +# should we enable the webserver which is just a simple, simple, simple webui to view the logs +web.enabled = false +# the port to run on +web.port = 9547 +# the app root / web folder / root / many other names . MUST contain a trailing '/' +web.app_root = / +# should this served up on http or https +web.ssl = true +# should we use basic auth +web.basic_auth = true +# basic auth username +web.basic_auth.username = admin +# basic auth password +web.basic_auth.password = admin \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 30e751c..ec416c3 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,4 +4,6 @@ urllib3==1.26.9 sqlalchemy==1.4.36 sqlalchemy_utils==0.38.2 python-dateutil==2.8.2 -Flask==2.1.2 \ No newline at end of file +Flask==2.1.2 +Flask-BasicAuth==0.2.0 +pyopenssl==22.0.0 \ No newline at end of file diff --git a/src/ConfigReader.py b/src/ConfigReader.py index 66e49ad..e69c3d7 100644 --- a/src/ConfigReader.py +++ b/src/ConfigReader.py @@ -75,7 +75,12 @@ class ConfigReader(ConfigParser): }, 'WEB': { 'web.enabled': 'false', - 'web.port': '9647' + 'web.app_root': '/', + 'web.port': '9647', + 'web.ssl': 'true', + 'web.basic_auth': 'true', + 'web.basic_auth.username': 'admin', + 'web.basic_auth.password': 'p@ssw0rd' } } deprecated_values = { diff --git a/src/log.py b/src/log.py index 562e677..36c702c 100644 --- a/src/log.py +++ b/src/log.py @@ -14,7 +14,7 @@ console_output.setLevel(logging.INFO) console_format = logging.Formatter(log_format) console_output.setFormatter(console_format) -info_log_file = RotatingFileHandler('../config/info.log', maxBytes=10000, backupCount=10) +info_log_file = RotatingFileHandler('../config/info.log', maxBytes=100000, backupCount=10) info_log_file.setLevel(logging.INFO) info_log_format = logging.Formatter(log_format) info_log_file.setFormatter(info_log_format) diff --git a/src/run.py b/src/run.py index 327b155..48c98c0 100644 --- a/src/run.py +++ b/src/run.py @@ -2,6 +2,8 @@ import threading from random import randint from time import sleep +from flask_basicauth import BasicAuth + import log from ConfigReader import ConfigReader, ConfigException from SteamGifts import SteamGifts, SteamGiftsException @@ -14,9 +16,17 @@ logger = log.get_logger(__name__) class WebServerThread(threading.Thread): - def __init__(self): + def __init__(self, config): Thread.__init__(self) self.exc = None + self.config = config + self.port = config['WEB'].getint('web.port') + self.ssl = config['WEB'].getboolean('web.ssl') + self.enabled = config['WEB'].getboolean('web.enabled') + self.app_root = config['WEB'].get('web.app_root') + self.basic_auth = config['WEB'].getboolean('web.basic_auth') + self.basic_auth_username = config['WEB'].get('web.basic_auth.username') + self.basic_auth_password = config['WEB'].get('web.basic_auth.password') def run_webserver(self): from flask import Flask @@ -24,17 +34,24 @@ class WebServerThread(threading.Thread): app = Flask(__name__) - @app.route("/") + if self.basic_auth: + app.config['BASIC_AUTH_USERNAME'] = self.basic_auth_username + app.config['BASIC_AUTH_PASSWORD'] = self.basic_auth_password + + app.config['BASIC_AUTH_FORCE'] = self.basic_auth + basic_auth = BasicAuth(app) + + @app.route(f"{self.app_root}") def config(): with open('../config/config.ini', 'r') as f: content = f.read() return render_template('configuration.html', config=content) - @app.route("/log") + @app.route(f"{self.app_root}log") def logs(): return render_template('log.html') - @app.route("/stream") + @app.route(f"{self.app_root}stream") def stream(): def generate(): with open('../config/info.log') as f: @@ -44,7 +61,14 @@ class WebServerThread(threading.Thread): return app.response_class(generate(), mimetype='text/plain') - app.run(port=9647, host="0.0.0.0") + if self.enabled: + logger.info("Webserver Enabled. Running") + if self.ssl: + app.run(port=self.port, host="0.0.0.0", ssl_context='adhoc') + else: + app.run(port=self.port, host="0.0.0.0") + else: + logger.info("Webserver NOT Enabled.") def run(self): # Variable that stores the exception, if raised by someFunction @@ -151,7 +175,7 @@ def run(): g.setName("Giveaway Enterer") g.start() - w = WebServerThread() + w = WebServerThread(config) w.setName("WebServer") # if the giveaway thread dies then this daemon thread will die by definition w.setDaemon(True)