Initial commit
This commit is contained in:
+16
@@ -0,0 +1,16 @@
|
|||||||
|
FROM python:3.14.7-alpine3.24
|
||||||
|
|
||||||
|
# Install dnsmasq
|
||||||
|
RUN apk --update --no-cache add dnsmasq
|
||||||
|
|
||||||
|
# Install libraries
|
||||||
|
COPY ./requirements.txt /requirements.txt
|
||||||
|
RUN pip3 install -r /requirements.txt
|
||||||
|
|
||||||
|
# Copy source
|
||||||
|
COPY ./run.sh /run.sh
|
||||||
|
COPY ./app/ /app
|
||||||
|
|
||||||
|
RUN mkdir /app/hosts.d/
|
||||||
|
|
||||||
|
CMD ["/run.sh"]
|
||||||
+69
@@ -0,0 +1,69 @@
|
|||||||
|
from flask import Flask, jsonify, request
|
||||||
|
import re
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
HOSTS_PATH = "/app/hosts.d/izbi"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
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}")
|
||||||
|
return True
|
||||||
|
except Exception as e:
|
||||||
|
print(e)
|
||||||
|
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])$"
|
||||||
|
return re.search(exp, ip)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@app.route('/', methods=["GET"])
|
||||||
|
def health():
|
||||||
|
return jsonify({"status": "healthy", "comment": ""})
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@app.route('/update', methods=["GET"])
|
||||||
|
def update_addr():
|
||||||
|
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']
|
||||||
|
|
||||||
|
print(f"ddns - update request of [{domain}, {ip}] from {request.remote_addr}")
|
||||||
|
|
||||||
|
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": "200", "code": "ok", "comment": "Updated successfully"})
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
app.run(host="0.0.0.0", port=8080, debug=True)
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
services:
|
||||||
|
ddns:
|
||||||
|
image: izbi-dns
|
||||||
|
build: .
|
||||||
|
restart: unless-stopped
|
||||||
|
ports:
|
||||||
|
- "127.0.0.1:8080:8080"
|
||||||
|
- "127.0.0.1:55:5353/udp"
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
flask
|
||||||
Reference in New Issue
Block a user