Compare commits

...

14 Commits

Author SHA1 Message Date
a1df4dcfdf Moved the web app to a separate directory. 2025-12-29 11:57:59 +01:00
af3d1fd3cb Update README and service configuration for Gunicorn usage and permissions 2025-12-28 21:33:34 +01:00
0989f51a55 Fix ExecStart command in dns-updater.service to properly handle environment variables 2025-12-28 21:30:49 +01:00
827ed9f83b Update dns-updater.service to run as root and change working directory 2025-12-28 21:27:52 +01:00
6d5dbee874 Add installation script for dns-updater service 2025-12-28 21:26:51 +01:00
63f67c7188 Add requirements.txt and update Dockerfile to install packages from it 2025-12-28 21:02:51 +01:00
bfebad5e5d Add .venv to .gitignore to prevent virtual environment files from being tracked 2025-12-28 21:02:46 +01:00
bc946e62e2 Updated the Dockerfile. 2025-12-28 21:01:34 +01:00
14d90d34aa Change container start-up procedure. 2025-12-28 21:00:53 +01:00
d77801788f Fix whitespace inconsistencies in delete-record.py 2025-12-28 18:15:21 +01:00
f77ade44d5 Fix whitespace inconsistencies in set-record.py 2025-12-28 18:15:03 +01:00
c5d6916fa6 Fix formatting inconsistencies in dns.py 2025-12-28 18:14:58 +01:00
1b2bb83eeb Add instructions for creating and using a self-signed SSL certificate in README.md 2025-12-28 18:14:53 +01:00
2884cd91a8 Add .pem files to .gitignore to prevent sensitive data from being tracked 2025-12-28 18:14:49 +01:00
14 changed files with 121 additions and 36 deletions

4
.gitignore vendored
View File

@@ -1,2 +1,4 @@
.env
**/.venv
**/.env
**/__pycache__
**/*.pem

View File

@@ -1,13 +0,0 @@
FROM alpine:3.23
RUN apk add --no-cache python3 py3-pip
RUN pip3 install --break-system-packages flask flask-httpauth gunicorn requests jmespath
# Clean up apk cache
RUN rm -rf /var/cache/apk/*
WORKDIR /app
COPY app.py /app/app.py
EXPOSE 8080
CMD ["gunicorn", "--bind", "0.0.0.0:8080", "app:app"]

View File

@@ -20,7 +20,7 @@ The repository contains a Flask-based API proxy that allows Omada controller to
On an Ubuntu/Debian system, you can install the required packages using apt:
```bash
sudo apt install -y python3 python3-dotenv python3-flask python3-flask-httpauth python3-requests
sudo apt install -y python3 python3-dotenv python3-flask python3-flask-httpauth python3-requests gunicorn
```
Copy the `app.py` file to your desired location, and run it using Python:
@@ -28,3 +28,41 @@ Copy the `app.py` file to your desired location, and run it using Python:
```bash
flask run app.py
```
or use Gunicorn for production:
```bash
gunicorn --bind 0.0.0.0:8080 app:app
```
## Self-Signed SSL Certificate (Optional)
To run the Flask app with HTTPS, you can create a self-signed SSL certificate:
```bash
openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -days 365 -nodes -subj "/CN=localhost" -addext "subjectAltName=DNS:localhost"
```
Then run the Flask app with SSL context:
```bash
flask run --cert=cert.pem --key=key.pem
```
To use the Mail In A Box server's SSL certificate, use the following files:
- certificate: `/miab-data/ssl/ssl_certificate.pem`
- private key: `/miab-data/ssl/ssl_private_key.pem`
> **Note:** You have to run the web server as root to access the private key file.
## Service Installation
```
sudo mkdir -p /opt/dns-updater
sudo cp app.py /opt/dns-updater/
sudo cp dns-updater.service /etc/systemd/system/
sudo systemctl daemon-reload
sudo systemctl enable --now dns-updater.service
sudo systemctl status dns-updater.service
```

22
app/Dockerfile Normal file
View File

@@ -0,0 +1,22 @@
FROM alpine:3.23
RUN apk add --no-cache python3 py3-pip
# Copy requirements and install Python packages
COPY requirements.txt /tmp/requirements.txt
RUN pip3 install --break-system-packages -r /tmp/requirements.txt
# Clean up apk cache
RUN rm -rf /var/cache/apk/*
WORKDIR /app
COPY app.py /app/app.py
COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh
ENV LISTEN_ADDRESS=0.0.0.0
ENV LISTEN_PORT=8080
EXPOSE 8080
ENTRYPOINT [ "/entrypoint.sh" ]

View File

14
app/dns-updater.service Normal file
View File

@@ -0,0 +1,14 @@
[Unit]
Description=dns-updater
After=network.target
[Service]
User=root
Group=root
WorkingDirectory=/opt/dns-updater
Environment="MIAB_HOST=box.koszewscy.waw.pl"
ExecStart=/usr/bin/gunicorn --workers 4 --bind 0.0.0.0:8443 --certfile="/miab-data/ssl/ssl_certificate.pem" --keyfile="/miab-data/ssl/ssl_private_key.pem" app:app
Restart=always
[Install]
WantedBy=multi-user.target

14
app/entrypoint.sh Normal file
View File

@@ -0,0 +1,14 @@
#!/bin/sh
set -e
LISTEN_ADDRESS="${LISTEN_ADDRESS:-0.0.0.0}"
LISTEN_PORT="${LISTEN_PORT:-8080}"
cd /app
if [ -z "$CERT_FILE" ] || [ -z "$KEY_FILE" ]; then
exec gunicorn --bind ${LISTEN_ADDRESS}:${LISTEN_PORT} app:app
else
exec gunicorn --bind ${LISTEN_ADDRESS}:${LISTEN_PORT} --certfile=${CERT_FILE} --keyfile=${KEY_FILE} app:app
fi

13
app/install Executable file
View File

@@ -0,0 +1,13 @@
#!/usr/bin/bash
if [[ $(id -u) -ne 0 ]]; then
echo "This script must be run as root" >&2
exit 1
fi
mkdir -p /opt/dns-updater
cp app.py /opt/dns-updater/
cp dns-updater.service /etc/systemd/system/
systemctl daemon-reload
systemctl enable --now dns-updater.service
systemctl status dns-updater.service

5
app/requirements.txt Normal file
View File

@@ -0,0 +1,5 @@
flask
flask-httpauth
gunicorn
requests
jmespath

4
build Executable file
View File

@@ -0,0 +1,4 @@
#!/usr/bin/bash
# Build the Docker image
docker build -t skoszewski/omada-dyndns-miab-proxy app

View File

@@ -7,9 +7,9 @@ if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Delete a DNS record in Mail-in-a-Box")
parser.add_argument("name", type=str, help="The name of the DNS record to delete")
parser.add_argument("type", type=str, help="The type of the DNS record to delete (e.g., A, CNAME, MX)")
args = parser.parse_args()
miab = miab_dns.MailInABox()
response = miab.delete_record(name=args.name, type=args.type)
if response == "OK":

View File

@@ -1,14 +0,0 @@
[Unit]
Description=dns-updater
After=network.target
[Service]
User=www-data
Group=www-data
WorkingDirectory=/home/slawek/src/dns-updater
Environment="MIAB_HOST=box.koszewscy.waw.pl"
ExecStart=/usr/bin/gunicorn --workers 4 --bind 0.0.0.0:8080 app:app
Restart=always
[Install]
WantedBy=multi-user.target

View File

@@ -7,14 +7,14 @@ def list_records(record_type: str = None):
f"https://{MIAB_HOST}/admin/dns/custom",
headers={"Authorization": MIAB_AUTH_HEADER}
)
if response.status_code == 200:
records = response.json()
if record_type:
jmespath_expr = f"[?rtype=='{record_type.upper()}']"
else:
jmespath_expr = "[]"
return jmespath.search(jmespath_expr + ".{name: qname, type: rtype, value: value}", records)
else:
raise Exception(f"Failed to retrieve DNS records: {response.status_code} {response.text}")
@@ -45,4 +45,4 @@ def delete_record(name: str, type: str):
if response.status_code == 200:
return response.text
else:
raise Exception(f"Failed to delete DNS record: {response.status_code} {response.text}")
raise Exception(f"Failed to delete DNS record: {response.status_code} {response.text}")

View File

@@ -8,9 +8,9 @@ if __name__ == "__main__":
parser.add_argument("name", type=str, help="The name of the DNS record to set")
parser.add_argument("value", type=str, help="The value of the DNS record to set")
parser.add_argument("--type", type=str, default="A", help="The type of the DNS record (default: A)")
args = parser.parse_args()
miab = miab_dns.MailInABox()
response = miab.set_record(name=args.name, value=args.value, type=args.type)
if response == "OK":