From b8cf3092951f0496e3ada2f061c53d3014a042d9 Mon Sep 17 00:00:00 2001 From: waazaa-fr Date: Mon, 11 Nov 2024 16:41:58 +0100 Subject: [PATCH] first commit --- .gitignore | 1 + Dockerfile | 14 ++++++++++++ root/app/ddns-myaddr.py | 49 +++++++++++++++++++++++++++++++++++++++++ root/start/start.sh | 4 ++++ 4 files changed, 68 insertions(+) create mode 100644 .gitignore create mode 100644 Dockerfile create mode 100644 root/app/ddns-myaddr.py create mode 100644 root/start/start.sh diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..727bc05 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.directory diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..7cc0bf8 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,14 @@ +FROM alpine:3.20.0 + +ENV TZ=Europe/Paris +ENV DDNS_MYADDR_KEY=xxxxx + +RUN apk update && apk add --no-cache tzdata curl wget python3 py3-pip +RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone +RUN mkdir -m 0777 -p /root/.config/pip && echo "[global]" > /root/.config/pip/pip.conf && echo "break-system-packages = true" >> /root/.config/pip/pip.conf +RUN pip install requests + +COPY ./root / +RUN chmod a+x /start/*.sh + +CMD ["/start/start.sh"] \ No newline at end of file diff --git a/root/app/ddns-myaddr.py b/root/app/ddns-myaddr.py new file mode 100644 index 0000000..64be006 --- /dev/null +++ b/root/app/ddns-myaddr.py @@ -0,0 +1,49 @@ +import requests +import time +import os +import logging +from datetime import datetime, timedelta + +logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') + +# Variables +key = os.getenv('DDNS_MYADDR_KEY') # Récupère clé +check_interval = 15 * 60 # Intervalle de vérification de 15 minutes +no_update_limit = timedelta(days=30) # Limite de 30 jours + +def get_public_ip(): + response = requests.get('https://api.ipify.org') + return response.text.strip() + +def update_ip(ip): + url = 'https://myaddr.tools/update' + data = {'ip': ip, 'key': key} + response = requests.post(url, data=data) + return response.text + +if __name__ == '__main__': + try: + last_ip = None + last_update_time = datetime.now() + + while True: + public_ip = get_public_ip() + current_time = datetime.now() + + if public_ip != last_ip: # Vérifie si l'IP a changé + result = update_ip(public_ip) + logging.info(f"IP mise à jour : {public_ip}, Réponse : {result}") + last_ip = public_ip # Met à jour l'IP + last_update_time = current_time # Réinitialise le temps + elif current_time - last_update_time > no_update_limit: # Vérifie si 1 mois sans mise à jour + result = update_ip(public_ip) + logging.info(f"Aucune mise à jour depuis un mois, IP envoyée : {public_ip}, Réponse : {result}") + last_update_time = current_time # Réinitialise le temps + + else: + logging.info(f"Aucune mise à jour, l'IP reste la même : {public_ip}") + + time.sleep(check_interval) # Pause de 15 minutes + + except KeyboardInterrupt: + logging.info("Arrêt en cours...") \ No newline at end of file diff --git a/root/start/start.sh b/root/start/start.sh new file mode 100644 index 0000000..6872740 --- /dev/null +++ b/root/start/start.sh @@ -0,0 +1,4 @@ +#!/usr/bin/env ash +set -e + +python3 /app/ddns-myaddr.py \ No newline at end of file