Files
izbi-dns/app/main.py
T

85 lines
2.5 KiB
Python
Raw Normal View History

2026-08-21 23:19:27 +02:00
from flask import Flask, jsonify, request
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
app = Flask(__name__)
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"])
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)