From bbbe0b9ee91b363ddc83c277f85f7c8d9a1fe11e Mon Sep 17 00:00:00 2001 From: jay817 Date: Fri, 23 Aug 2024 09:38:12 -0400 Subject: [PATCH] Add ddns-cloudflare/README.md --- ddns-cloudflare/README.md | 65 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 ddns-cloudflare/README.md diff --git a/ddns-cloudflare/README.md b/ddns-cloudflare/README.md new file mode 100644 index 0000000..a9acd66 --- /dev/null +++ b/ddns-cloudflare/README.md @@ -0,0 +1,65 @@ +## `docker-compose.yml` +```yml +services: + ddns: + environment: + - API_TOKEN= + - ZONE_ID= + - RECORD_ID= + - DOMAIN_NAME= + image: python:slim + restart: unless-stopped + container_name: ddns + entrypoint: python -c + command: + - | + import subprocess + subprocess.run('pip install requests', shell=True, capture_output=True) + import json, time, requests, os + + def get_current_ip(): + return requests.get('https://api64.ipify.org').text + + def update_dns(ip): + zone_id = os.environ['ZONE_ID'] + record_id = os.environ['RECORD_ID'] + api_token = os.environ['API_TOKEN'] + domain_name = os.environ['DOMAIN_NAME'] + + url = f"https://api.cloudflare.com/client/v4/zones/{zone_id}/dns_records/{record_id}" + headers = { + 'Authorization': f'Bearer {api_token}' + } + + data = { + 'type': 'A', + 'name': domain_name, + 'content': ip, + 'ttl': 1, + 'proxied': False + } + + response = requests.put(url, headers=headers, json=data) + + if response.status_code == 200: + print(f"DNS 레코드가 성공적으로 업데이트되었습니다. 새 IP: {ip}", flush=True) + else: + print("DNS 레코드 업데이트에 실패했습니다.", flush=True) + print(response.text, flush=True) + + subprocess.run('pip install requests', shell=True, capture_output=True) + + last_ip = None + check_interval = 6 * 3600 # 6시간 + + while True: + current_ip = get_current_ip() + if current_ip != last_ip: + print(f"IP 변경 감지: {last_ip} -> {current_ip}", flush=True) + update_dns(current_ip) + last_ip = current_ip + else: + print(f"IP 변경 없음: {current_ip}", flush=True) + + time.sleep(check_interval) +``` \ No newline at end of file