Add comments

This commit is contained in:
2026-08-25 01:48:03 +02:00
parent 07ff654baa
commit 1b5ea7be3a
2 changed files with 69 additions and 16 deletions
+20 -14
View File
@@ -4,22 +4,19 @@ import re
from os import environ, urandom
HOSTS_PATH = ( environ["HOSTS"] if "HOSTS" in environ else"/app/data/hosts.d/" ) + "/izbi"
DB_PATH = environ["DB_PATH"] if "DB_PATH" in environ else "/app/data/sqlite.db"
app = Flask(__name__)
app.config["SESSION_PERMANENT"] = True
app.config["SESSION_TYPE"] = "memcached"
app.config["PERMANENT_SESSION_LIFETIME"] = 3600
app.config["SECRET_KEY"] = environ["SECRET_KEY"] if "SECRET_KEY" in environ else urandom(32).hex()
hosts = {}
# Update hosts file
def update_hosts(path: str, hosts: dict = {}) -> bool:
try:
hosts_str = ""
@@ -34,13 +31,13 @@ def update_hosts(path: str, hosts: dict = {}) -> bool:
return False
# check if string is a valid IP
def validate_ip(ip: str) -> bool:
exp = r"^((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.){3}(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])$"
return re.search(exp, ip)
# Serve homepage
@app.route('/', methods=["GET"])
def homepage():
if session.get("USERID") is not None:
@@ -48,7 +45,7 @@ def homepage():
return render_template("index.html")
# Handle login
@app.route('/login', methods=["POST"])
def login():
if "user" not in request.form or "pass" not in request.form:
@@ -65,13 +62,15 @@ def login():
return redirect("/dashboard", code=302)
# Log out by clearing session data
@app.route('/logout', methods=["GET"])
def logout():
session.clear()
return redirect("/")
# Display dashboard for logged in users
@app.route('/dashboard', methods=["GET"])
def dashboard():
if session.get("USERID") is None:
@@ -81,13 +80,15 @@ def dashboard():
domains = auth.get_user_domains(DB_PATH, userid)
tokens = auth.get_user_tokens(DB_PATH, userid)
# If user is admin, display additional options
if auth.is_admin(DB_PATH, userid):
users = auth.get_users(DB_PATH)
return render_template("dashboard.html", username=username, domains=domains, tokens=tokens, is_admin=True, users=users)
return render_template("dashboard.html", username=username, domains=domains, tokens=tokens, is_admin=False)
# Generate new access token with a name
@app.route('/generate-token', methods=["POST"])
def generate_token():
if session.get("USERID") is None:
@@ -106,6 +107,7 @@ def generate_token():
return redirect("/dashboard")
# Revoke token by name
@app.route('/revoke-token', methods=["POST"])
def revoke_token():
if session.get("USERID") is None:
@@ -121,6 +123,7 @@ def revoke_token():
return redirect("/dashboard")
# Create new user (by admin)
@app.route('/create-user', methods=["POST"])
def create_user():
if session.get("USERID") is None:
@@ -138,6 +141,7 @@ def create_user():
if domains is None:
domains = []
else:
# Collapse whitespaces
collapse_exp = re.compile(r'\s+')
domains = collapse_exp.sub(' ', domains.strip())
domains = domains.split(" ")
@@ -149,6 +153,7 @@ def create_user():
return redirect("/dashboard")
# Update user's field (by admin)
@app.route('/update-user', methods=["POST"])
def update_user():
if session.get("USERID") is None:
@@ -186,7 +191,7 @@ def update_user():
return redirect("/dashboard")
# Delete user (by admin)
@app.route('/delete-user', methods=["POST"])
def delete_user():
if session.get("USERID") is None:
@@ -207,8 +212,7 @@ def delete_user():
return redirect("/dashboard")
# Change password (by user)
@app.route('/change-password', methods=["POST"])
def change_password():
if session.get("USERID") is None:
@@ -235,12 +239,14 @@ def change_password():
flash("password changed successfully")
return redirect("/dashboard")
# API health endpoint ???
@app.route('/health', methods=["GET"])
def health():
return jsonify({"status": "healthy", "comment": ""})
# API for updating domain's IP
@app.route('/update', methods=["GET"])
def update_addr():
try:
@@ -265,6 +271,7 @@ def update_addr():
success = True
# Update hosts file if something changed
if domain not in hosts or ip != hosts[domain]:
hosts[domain] = ip
success = update_hosts(HOSTS_PATH, hosts)
@@ -278,7 +285,6 @@ def update_addr():
return jsonify({"status": "500", "code": "internal-server-error", "comment": "An error occurred while processing request"}), 500
if __name__ == '__main__':
auth.init_db(DB_PATH)
app.run(host="0.0.0.0", port=8080, debug=False)