Add ddns-cloudflare/README.md
This commit is contained in:
65
ddns-cloudflare/README.md
Normal file
65
ddns-cloudflare/README.md
Normal file
@@ -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)
|
||||
```
|
||||
Reference in New Issue
Block a user