2026-08-24 17:24:08 +02:00
|
|
|
from flask import Flask, jsonify, request, render_template, redirect, session, flash
|
2026-08-24 01:37:14 +02:00
|
|
|
import auth
|
2026-08-21 23:19:27 +02:00
|
|
|
import re
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
HOSTS_PATH = "/app/hosts.d/izbi"
|
2026-08-24 01:37:14 +02:00
|
|
|
DB_PATH = "/app/sqlite.db"
|
2026-08-21 23:19:27 +02:00
|
|
|
|
|
|
|
|
|
2026-08-24 15:18:45 +02:00
|
|
|
|
2026-08-21 23:19:27 +02:00
|
|
|
app = Flask(__name__)
|
2026-08-24 17:24:08 +02:00
|
|
|
app.config["SESSION_PERMANENT"] = True
|
|
|
|
|
app.config["SESSION_TYPE"] = "memcached"
|
|
|
|
|
app.config["PERMANENT_SESSION_LIFETIME"] = 3600
|
|
|
|
|
|
|
|
|
|
app.config["SECRET_KEY"] = "dsadasda"
|
2026-08-21 23:19:27 +02:00
|
|
|
|
|
|
|
|
hosts = {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2026-08-24 01:37:14 +02:00
|
|
|
def update_hosts(path: str, hosts: dict = {}) -> bool:
|
2026-08-21 23:19:27 +02:00
|
|
|
try:
|
|
|
|
|
hosts_str = ""
|
|
|
|
|
for domain, ip in hosts.items():
|
|
|
|
|
hosts_str += f"{ip}\t{domain}\n"
|
|
|
|
|
with open(path, "w") as f:
|
|
|
|
|
f.write(hosts_str)
|
2026-08-24 01:37:14 +02:00
|
|
|
print(f"ddns - updated {path}", flush=True)
|
2026-08-21 23:19:27 +02:00
|
|
|
return True
|
|
|
|
|
except Exception as e:
|
2026-08-24 01:37:14 +02:00
|
|
|
print(e, flush=True)
|
2026-08-21 23:19:27 +02:00
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2026-08-24 01:37:14 +02:00
|
|
|
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])$"
|
2026-08-21 23:19:27 +02:00
|
|
|
return re.search(exp, ip)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.route('/', methods=["GET"])
|
2026-08-24 15:18:45 +02:00
|
|
|
def homepage():
|
2026-08-24 17:24:08 +02:00
|
|
|
if session.get("USERID") is not None:
|
|
|
|
|
return redirect("/dashboard")
|
2026-08-24 15:18:45 +02:00
|
|
|
return render_template("index.html")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.route('/login', methods=["POST"])
|
|
|
|
|
def login():
|
|
|
|
|
if "user" not in request.form or "pass" not in request.form:
|
|
|
|
|
return redirect("/"), 400
|
|
|
|
|
|
|
|
|
|
if request.form["user"].strip() == "" or request.form["pass"].strip() == "":
|
|
|
|
|
return redirect("/"), 400
|
|
|
|
|
|
2026-08-24 17:24:08 +02:00
|
|
|
userid = auth.user_login(DB_PATH, request.form["user"], request.form["pass"])
|
|
|
|
|
if userid is None:
|
2026-08-24 15:18:45 +02:00
|
|
|
return redirect("/")
|
|
|
|
|
|
2026-08-24 17:24:08 +02:00
|
|
|
session["USERID"] = userid
|
|
|
|
|
|
2026-08-24 15:18:45 +02:00
|
|
|
return redirect("/dashboard", code=302)
|
|
|
|
|
|
2026-08-24 17:24:08 +02:00
|
|
|
@app.route('/logout', methods=["GET"])
|
|
|
|
|
def logout():
|
|
|
|
|
session.clear()
|
|
|
|
|
return redirect("/")
|
2026-08-24 15:18:45 +02:00
|
|
|
|
|
|
|
|
|
2026-08-24 17:24:08 +02:00
|
|
|
|
|
|
|
|
@app.route('/dashboard', methods=["GET"])
|
|
|
|
|
def dashboard():
|
|
|
|
|
if session.get("USERID") is None:
|
|
|
|
|
return redirect("/")
|
|
|
|
|
username = auth.get_username(DB_PATH, session.get("USERID"))
|
|
|
|
|
domains = auth.get_user_domains(DB_PATH, session.get("USERID"))
|
|
|
|
|
tokens = auth.get_user_tokens(DB_PATH, session.get("USERID"))
|
|
|
|
|
|
|
|
|
|
return render_template("dashboard.html", username=username, domains=domains, tokens=tokens)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.route('/generate-token', methods=["POST"])
|
|
|
|
|
def generate_token():
|
|
|
|
|
if session.get("USERID") is None:
|
|
|
|
|
return redirect("/")
|
|
|
|
|
|
|
|
|
|
if "token" not in request.form:
|
|
|
|
|
return redirect("/dashboard")
|
|
|
|
|
if request.form["token"].strip() == "":
|
|
|
|
|
return redirect("/dashboard")
|
|
|
|
|
|
|
|
|
|
token = auth.generate_user_token(DB_PATH, session.get("USERID"), request.form["token"])
|
|
|
|
|
|
|
|
|
|
if token is not None:
|
|
|
|
|
flash(token)
|
|
|
|
|
|
|
|
|
|
return redirect("/dashboard")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.route('/revoke-token', methods=["POST"])
|
|
|
|
|
def revoke_token():
|
|
|
|
|
if session.get("USERID") is None:
|
|
|
|
|
return redirect("/")
|
|
|
|
|
|
|
|
|
|
if "token" not in request.form:
|
|
|
|
|
return redirect("/dashboard")
|
|
|
|
|
if request.form["token"].strip() == "":
|
|
|
|
|
return redirect("/dashboard")
|
|
|
|
|
|
|
|
|
|
auth.revoke_user_token(DB_PATH, session.get("USERID"), request.form["token"])
|
|
|
|
|
|
|
|
|
|
return redirect("/dashboard")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.route('/change-password', methods=["POST"])
|
|
|
|
|
def change_password():
|
|
|
|
|
if session.get("USERID") is None:
|
|
|
|
|
return redirect("/")
|
|
|
|
|
|
|
|
|
|
if "pass" not in request.form or "pass-new" not in request.form or "pass-rep" not in request.form:
|
|
|
|
|
flash("password change failed")
|
|
|
|
|
return reidrect("/dashboard")
|
|
|
|
|
if request.form["pass"] == "" or request.form["pass-new"] == "" or request.form["pass-rep"] == "":
|
|
|
|
|
flash("password change failed")
|
|
|
|
|
return redirect("/dashboard")
|
|
|
|
|
|
|
|
|
|
oldpass = request.form["pass"]
|
|
|
|
|
newpass = request.form["pass-new"]
|
|
|
|
|
|
|
|
|
|
if newpass != request.form["pass-rep"]:
|
|
|
|
|
flash("passwords do not match")
|
|
|
|
|
return redirect("/dashboard")
|
|
|
|
|
|
|
|
|
|
if not auth.change_user_password(DB_PATH, session.get("USERID"), oldpass, newpass):
|
|
|
|
|
flash("password change failed")
|
|
|
|
|
return redirect("/dashboard")
|
|
|
|
|
|
|
|
|
|
flash("password changed successfully")
|
|
|
|
|
return redirect("/dashboard")
|
|
|
|
|
|
2026-08-24 15:18:45 +02:00
|
|
|
@app.route('/health', methods=["GET"])
|
2026-08-21 23:19:27 +02:00
|
|
|
def health():
|
|
|
|
|
return jsonify({"status": "healthy", "comment": ""})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.route('/update', methods=["GET"])
|
|
|
|
|
def update_addr():
|
2026-08-24 01:37:14 +02:00
|
|
|
try:
|
|
|
|
|
if 'domain' not in request.args:
|
|
|
|
|
return jsonify({"status": "400", "code": "domain-error", "comment": "Missing or invalid domain"}), 400
|
|
|
|
|
domain = request.args['domain']
|
2026-08-21 23:19:27 +02:00
|
|
|
|
2026-08-24 01:37:14 +02:00
|
|
|
if 'ip' not in request.args:
|
|
|
|
|
ip = request.remote_addr
|
|
|
|
|
elif not validate_ip(request.args['ip']):
|
|
|
|
|
return jsonify({"status": "400", "code": "ip-error", "comment": "Invalid IP"}), 400
|
|
|
|
|
else:
|
|
|
|
|
ip = request.args['ip']
|
2026-08-21 23:19:27 +02:00
|
|
|
|
2026-08-24 01:37:14 +02:00
|
|
|
if 'token' not in request.args:
|
|
|
|
|
return jsonify({"status": "400", "code": "token-error", "comment": "Missing or invalid token"}), 400
|
|
|
|
|
token = request.args['token']
|
2026-08-21 23:19:27 +02:00
|
|
|
|
2026-08-24 01:37:14 +02:00
|
|
|
if not auth.check_token_domain(DB_PATH, token, domain):
|
|
|
|
|
print(f"ddns - unauthorized update attempt from {request.remote_addr} of {domain}", flush=True)
|
|
|
|
|
return jsonify({"status": "401", "code": "unauthorized", "comment": "This token is not allowed to update this domain"}), 401
|
2026-08-21 23:19:27 +02:00
|
|
|
|
2026-08-24 01:37:14 +02:00
|
|
|
success = True
|
2026-08-21 23:19:27 +02:00
|
|
|
|
2026-08-24 01:37:14 +02:00
|
|
|
if domain not in hosts or ip != hosts[domain]:
|
|
|
|
|
hosts[domain] = ip
|
|
|
|
|
success = update_hosts(HOSTS_PATH, hosts)
|
|
|
|
|
|
|
|
|
|
if not success:
|
|
|
|
|
return jsonify({"status": "500", "code": "internal-server-error", "comment": "An error occurred while processing request"}), 500
|
|
|
|
|
|
|
|
|
|
return jsonify({"status": "200", "code": "ok", "comment": "Updated successfully"})
|
2026-08-24 13:35:59 +02:00
|
|
|
except Exception as e:
|
|
|
|
|
print(e, flush=True)
|
2026-08-24 01:37:14 +02:00
|
|
|
return jsonify({"status": "500", "code": "internal-server-error", "comment": "An error occurred while processing request"}), 500
|
2026-08-21 23:19:27 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2026-08-24 13:35:59 +02:00
|
|
|
auth.init_db(DB_PATH)
|
2026-08-21 23:19:27 +02:00
|
|
|
app.run(host="0.0.0.0", port=8080, debug=True)
|