107 lines
3.1 KiB
Python
107 lines
3.1 KiB
Python
from flask import Flask, jsonify, request, render_template, redirect
|
|
import auth
|
|
import re
|
|
|
|
|
|
|
|
HOSTS_PATH = "/app/hosts.d/izbi"
|
|
DB_PATH = "/app/sqlite.db"
|
|
|
|
|
|
|
|
app = Flask(__name__)
|
|
|
|
hosts = {}
|
|
|
|
|
|
|
|
def update_hosts(path: str, hosts: dict = {}) -> bool:
|
|
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)
|
|
print(f"ddns - updated {path}", flush=True)
|
|
return True
|
|
except Exception as e:
|
|
print(e, flush=True)
|
|
return False
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
@app.route('/', methods=["GET"])
|
|
def homepage():
|
|
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
|
|
|
|
if not auth.user_login(DB_PATH, request.form["user"], request.form["pass"]):
|
|
return redirect("/")
|
|
|
|
return redirect("/dashboard", code=302)
|
|
|
|
|
|
|
|
@app.route('/health', methods=["GET"])
|
|
def health():
|
|
return jsonify({"status": "healthy", "comment": ""})
|
|
|
|
|
|
|
|
@app.route('/update', methods=["GET"])
|
|
def update_addr():
|
|
try:
|
|
if 'domain' not in request.args:
|
|
return jsonify({"status": "400", "code": "domain-error", "comment": "Missing or invalid domain"}), 400
|
|
domain = request.args['domain']
|
|
|
|
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']
|
|
|
|
if 'token' not in request.args:
|
|
return jsonify({"status": "400", "code": "token-error", "comment": "Missing or invalid token"}), 400
|
|
token = request.args['token']
|
|
|
|
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
|
|
|
|
success = True
|
|
|
|
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"})
|
|
except Exception as e:
|
|
print(e, flush=True)
|
|
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=True)
|