gestion par config.yml pour multiples updates

main
waazaa-fr 2024-11-11 19:06:04 +01:00
parent f739ee5f16
commit c452575b77
3 changed files with 90 additions and 38 deletions

View File

@ -1,15 +1,18 @@
FROM alpine:3.20.0 FROM alpine:3.20.0
ENV TZ=Europe/Paris ENV TZ=Europe/Paris
ENV DDNS_MYADDR_NO_UPDATE_LIMIT=30
ENV DDNS_MYADDR_IP_VERSION="ipv4" ENV DDNS_MYADDR_IP_VERSION="ipv4"
ENV DDNS_MYADDR_KEY=xxxxx ENV DDNS_MYADDR_KEY=xxxxx
RUN apk update && apk add --no-cache tzdata curl wget python3 py3-pip 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 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 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 RUN pip install requests pyyaml
COPY ./root / COPY ./root /
RUN chmod a+x /start/*.sh RUN chmod a+x /start/*.sh
VOLUME /config
CMD ["/start/start.sh"] CMD ["/start/start.sh"]

View File

@ -1,53 +1,95 @@
import requests import requests
import time import time
import os import os
import threading
import logging import logging
import yaml
from datetime import datetime, timedelta from datetime import datetime, timedelta
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
# Variables log_lock = threading.Lock() # Création d'un verrou pour les logs
ip_version = os.getenv('DDNS_MYADDR_IP_VERSION') # 'ipv4' ou 'ipv6'
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(): # Charge le fichier de configuration
if ip_version == 'ipv6': def load_config(file_path='/config/config.yml'):
response = requests.get('https://api64.ipify.org') # Appel pour IPv6 with open(file_path, 'r') as file:
else: return yaml.safe_load(file)
response = requests.get('https://api.ipify.org') # Appel pour IPv4
return response.text.strip()
def update_ip(ip): def get_public_ip(version):
url = 'https://myaddr.tools/update' if version == 'ipv4':
data = {'ip': ip, 'key': key} return requests.get('https://api.ipify.org').text.strip()
response = requests.post(url, data=data) elif version == 'ipv6':
return requests.get('https://api64.ipify.org').text.strip()
return None
def update_ip(api_key, ip):
url = 'https://myaddr.tools/update'
data = {'ip': ip, 'key': api_key}
response = requests.post(url, data=data)
return response.text return response.text
if __name__ == '__main__': def process_configuration(name, config):
try: last_ipv4 = None
last_ip = None last_ipv6 = None
last_update_time = datetime.now() last_update_ipv4_time = datetime.now() - timedelta(days=config['NO_UPDATE_LIMIT'])
last_update_ipv6_time = datetime.now() - timedelta(days=config['NO_UPDATE_LIMIT'])
api_key = config['KEY']
while True:
current_time = datetime.now()
while True: # Traitement de l'IPv4
public_ip = get_public_ip() ipv4_action = config['IPv4']
current_time = datetime.now() if ipv4_action == 'auto':
public_ipv4 = get_public_ip('ipv4')
if public_ipv4 != last_ipv4 and public_ipv4:
result = update_ip(api_key, public_ipv4)
with log_lock: # Utiliser le verrou pour synchroniser les logs
logging.info(f"{name} - IPv4 updated : {public_ipv4}, Response : {result}")
last_ipv4 = public_ipv4
last_update_ipv4_time = current_time
elif ipv4_action != 'none': # Privé une IPv4
if ipv4_action != last_ipv4 and current_time - last_update_ipv4_time > timedelta(days=config['NO_UPDATE_LIMIT']):
result = update_ip(api_key, ipv4_action)
with log_lock: # Utiliser le verrou pour synchroniser les logs
logging.info(f"{name} - IPv4 set to : {ipv4_action}, Response : {result}")
last_ipv4 = ipv4_action
last_update_ipv4_time = current_time
if public_ip != last_ip: # Vérifie si l'IP a changé # Traitement de l'IPv6
result = update_ip(public_ip) ipv6_action = config['IPv6']
logging.info(f"IP mise à jour : {public_ip}, Réponse : {result}") if ipv6_action == 'auto':
last_ip = public_ip # Met à jour l'IP public_ipv6 = get_public_ip('ipv6')
last_update_time = current_time # Réinitialise le temps if public_ipv6 != last_ipv6 and public_ipv6:
elif current_time - last_update_time > no_update_limit: # Vérifie si 1 mois sans mise à jour result = update_ip(api_key, public_ipv6)
result = update_ip(public_ip) with log_lock: # Utiliser le verrou pour synchroniser les logs
logging.info(f"Aucune mise à jour depuis un mois, IP envoyée : {public_ip}, Réponse : {result}") logging.info(f"{name} - IPv6 updated : {public_ipv6}, Response : {result}")
last_update_time = current_time # Réinitialise le temps last_ipv6 = public_ipv6
last_update_ipv6_time = current_time
else: elif ipv6_action != 'none': # Privé une IPv6
logging.info(f"Aucune mise à jour, l'IP reste la même : {public_ip}") if ipv6_action != last_ipv6 and current_time - last_update_ipv6_time > timedelta(days=config['NO_UPDATE_LIMIT']):
result = update_ip(api_key, ipv6_action)
with log_lock: # Utiliser le verrou pour synchroniser les logs
logging.info(f"{name} - IPv6 set to : {ipv6_action}, Response : {result}")
last_ipv6 = ipv6_action
last_update_ipv6_time = current_time
time.sleep(check_interval) # Pause de 15 minutes time.sleep(15 * 60) # Vérifie tous les 15 minutes
if __name__ == '__main__':
configurations = load_config()
threads = []
try:
for config_name, config in configurations.items():
thread = threading.Thread(target=process_configuration, args=(config_name, config))
thread.start()
threads.append(thread)
for thread in threads:
thread.join()
except KeyboardInterrupt: except KeyboardInterrupt:
logging.info("Arrêt en cours...") logging.info("Arrêt du script en cours...")
# Optionnel : tu peux ajouter une logique pour arrêter les threads proprement si nécessaire.
for thread in threads:
thread.join(timeout=1) # Attendre un peu pour que les threads finissent
logging.info("Script arrêté.")

View File

@ -1,4 +1,11 @@
#!/usr/bin/env ash #!/usr/bin/env ash
set -e set -e
config_file="/config/config.yml"
if [[ ! -f $config_file ]]; then
echo "Error : $config_file not found."
exit 1
fi
python3 /app/ddns-myaddr.py python3 /app/ddns-myaddr.py