first commit

main
waazaa-fr 2024-11-11 16:41:58 +01:00
commit b8cf309295
4 changed files with 68 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
.directory

14
Dockerfile Normal file
View File

@ -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"]

49
root/app/ddns-myaddr.py Normal file
View File

@ -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...")

4
root/start/start.sh Normal file
View File

@ -0,0 +1,4 @@
#!/usr/bin/env ash
set -e
python3 /app/ddns-myaddr.py