add token-based auth

This commit is contained in:
2026-08-24 01:37:14 +02:00
parent 144da958dd
commit 442df674ba
2 changed files with 68 additions and 21 deletions
+34
View File
@@ -0,0 +1,34 @@
import sqlite3
#def __init__(self, path: str) -> None:
# self.db_connection = sqlite3.connect(path)
# cursor = self.db_connection.cursor()
# cursor.execute('''
# CREATE TABLE IF NOT EXISTS Tokens (
# TOKEN TEXT PRIMARY KEY NOT NULL,
# USER TEXT NOT NULL,
# NAME TEXT NOT NULL
# )
# ''')
#
# cursor.execute('''
# CREATE TABLE IF NOT EXISTS Domains (
# DOMAIN TEXT PRIMARY KEY NOT NULL,
# USER TEXT NOT NULL
# )
# ''')
#
# self.db_connection.commit()
# cursor.close()
def check_token_domain(db_path: str, token: str, domain: str) -> bool:
with sqlite3.connect(db_path) as connection:
cursor = connection.cursor()
cursor.execute('''
SELECT COUNT(*) FROM Tokens
JOIN Domains ON Tokens.user = Domains.user
WHERE token = ?
AND domain = ?
''', (token,domain))
return cursor.fetchone()[0] == 1
+21 -8
View File
@@ -1,10 +1,11 @@
from flask import Flask, jsonify, request
import auth
import re
HOSTS_PATH = "/app/hosts.d/izbi"
DB_PATH = "/app/sqlite.db"
app = Flask(__name__)
@@ -13,23 +14,23 @@ hosts = {}
def update_hosts(path: str, hosts: dict = {}): bool
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}")
print(f"ddns - updated {path}", flush=True)
return True
except Exception as e:
print(e)
print(e, flush=True)
return False
def validate_ip(ip: str): bool
exp = "^((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])$"
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)
@@ -42,6 +43,7 @@ def health():
@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']
@@ -53,15 +55,26 @@ def update_addr():
else:
ip = request.args['ip']
print(f"ddns - update request of [{domain}, {ip}] from {request.remote_addr}")
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", "An error occurred while processing request"}), 500
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:
return jsonify({"status": "500", "code": "internal-server-error", "comment": "An error occurred while processing request"}), 500