Created
August 18, 2024 05:38
-
-
Save shibeta/c333233cf136adc24b4e73da0ef16459 to your computer and use it in GitHub Desktop.
简单的cloudflare ddns脚本,无须使用ipv6一堆毛病的docker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import ipaddress | |
proxy = { | |
"http": None, | |
"https": None | |
} | |
# 配置 | |
api_token = '{{API_HERE}}' # 替换为你的 Cloudflare API token | |
# 域名和id只填一种即可,都填优先使用id,不使用域名 | |
domain = 'example.com' # 替换为你要更新的域名 | |
record_name = 'record.example.com' # 替换为你要更新的记录名称 | |
# 推荐填id,速度快 | |
zone_id = '' # 替换为你要更新的 Zone ID | |
record_id = '' # 替换为你要更新的 Record ID | |
# 获取 Zone ID | |
def get_zone_id(domain): | |
url = f'https://api.cloudflare.com/client/v4/zones?name={domain}' | |
headers = { | |
'Authorization': f'Bearer {api_token}', | |
'Content-Type': 'application/json' | |
} | |
response = requests.get(url, headers=headers, proxies=proxy) | |
response_data = response.json() | |
if response_data['success']: | |
return response_data['result'][0]['id'] | |
else: | |
raise Exception('无法获取 Zone ID') | |
# 获取 Record ID | |
def get_record_id(zone_id, record_name): | |
url = f'https://api.cloudflare.com/client/v4/zones/{zone_id}/dns_records?name={record_name}' | |
headers = { | |
'Authorization': f'Bearer {api_token}', | |
'Content-Type': 'application/json' | |
} | |
response = requests.get(url, headers=headers, proxies=proxy) | |
response_data = response.json() | |
if response_data['success']: | |
return response_data['result'][0]['id'] | |
else: | |
raise Exception('无法获取 Record ID') | |
def get_record_ip(zone_id, record_id): | |
url = f'https://api.cloudflare.com/client/v4/zones/{zone_id}/dns_records/{record_id}' | |
headers = { | |
'Authorization': f'Bearer {api_token}', | |
'Content-Type': 'application/json' | |
} | |
response = requests.get(url, headers=headers, proxies=proxy) | |
response_data = response.json() | |
if response_data['success']: | |
return response_data['result']['content'] | |
else: | |
raise Exception('无法获取 Record IP') | |
# 获取本机的 IPv6 地址 | |
def get_ipv6_address(): | |
url = 'https://6.ipw.cn' | |
response = requests.get(url) | |
return response.text.strip() | |
# 更新 DNS 记录 | |
def update_dns_record(zone_id, record_id, ipv6_address): | |
url = f'https://api.cloudflare.com/client/v4/zones/{zone_id}/dns_records/{record_id}' | |
headers = { | |
'Authorization': f'Bearer {api_token}', | |
'Content-Type': 'application/json' | |
} | |
data = { | |
'type': 'AAAA', | |
'name': record_name, | |
'content': ipv6_address, | |
'ttl': 1, # 设置 TTL 为 1,表示自动 | |
'proxied': False # 是否启用 Cloudflare 的代理 | |
} | |
response = requests.put(url, headers=headers, json=data, proxies=proxy) | |
response_data = response.json() | |
if response_data['success']: | |
print('DNS 记录更新成功') | |
else: | |
raise Exception('DNS 记录更新失败') | |
if __name__ == '__main__': | |
try: | |
if not zone_id or not record_id: | |
# 如果没有 Zone ID 或 Record ID,则获取 | |
zone_id = get_zone_id(domain) | |
record_id = get_record_id(zone_id, record_name) | |
local_address = ipaddress.ip_address(get_ipv6_address()) | |
record_address = ipaddress.ip_address(get_record_ip(zone_id, record_id)) | |
if local_address == record_address: | |
# 如果本地 IP 和 Cloudflare 的记录 IP 相同,则不更新 DNS 记录 | |
print(f'本地 IP 和 Cloudflare 的记录 IP 相同,无需更新({local_address})') | |
else: | |
# 如果本地 IP 和 Cloudflare 的记录 IP 不相同,则更新 DNS 记录 | |
print(f'本地 IP 和 Cloudflare 的记录 IP 不相同,开始更新({record_address} -> {local_address})') | |
update_dns_record(zone_id, record_id, str(local_address)) | |
except Exception as e: | |
print(f'发生错误: {e}') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment