-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
96 additions
and
3 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,4 +31,5 @@ jobs: | |
- name: Run autoddns script | ||
run: | | ||
python autoddns.py | ||
python autoddns.py | ||
python autoddnsv6.py |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import os | ||
import requests | ||
|
||
# 从环境变量获取 Cloudflare API 配置信息 | ||
API_KEY = os.getenv("CLOUDFLARE_API_KEY") | ||
EMAIL = os.getenv("CLOUDFLARE_EMAIL") | ||
ZONE_ID = os.getenv("CLOUDFLARE_ZONE_ID") | ||
|
||
# 确保从环境变量中获取到了这些信息 | ||
if not all([API_KEY, EMAIL, ZONE_ID]): | ||
print("缺少 Cloudflare 配置信息,请确保在 GitHub Secrets 中设置了 CLOUDFLARE_API_KEY, CLOUDFLARE_EMAIL 和 CLOUDFLARE_ZONE_ID。") | ||
exit(1) | ||
|
||
# 域名与标记映射关系 | ||
LOCATION_TO_DOMAIN = { | ||
# 示例映射(可根据实际需求调整) | ||
"SJC": "us.616049.xyz", # 圣何塞 | ||
"NRT": "jp.616049.xyz", # 东京成田 | ||
"HKG": "hk.616049.xyz", # 香港国际机场 | ||
"SIN": "sg.616049.xyz", # 新加坡 | ||
} | ||
|
||
# 从 cfipv6.txt 文件中读取前十个 IPv6 和标记 | ||
def get_ips_from_file(file_path, limit=10): | ||
ip_data = [] | ||
try: | ||
with open(file_path, "r") as file: | ||
for line in file: | ||
if "#" in line: | ||
ip, location = line.strip().split("#") | ||
ip_data.append((ip.strip(), location.strip())) | ||
if len(ip_data) >= limit: | ||
break | ||
return ip_data | ||
except FileNotFoundError: | ||
print(f"文件未找到: {file_path}") | ||
return [] | ||
|
||
# 检查是否已有相同记录 | ||
def check_existing_record(domain, ip): | ||
url = f"https://api.cloudflare.com/client/v4/zones/{ZONE_ID}/dns_records" | ||
headers = { | ||
"X-Auth-Email": EMAIL, | ||
"X-Auth-Key": API_KEY, | ||
"Content-Type": "application/json" | ||
} | ||
response = requests.get(url, headers=headers) | ||
if response.status_code == 200: | ||
records = response.json().get("result", []) | ||
return any(record["name"] == domain and record["content"] == ip for record in records) | ||
print(f"检查记录失败: {response.status_code}, {response.text}") | ||
return False | ||
|
||
# 添加新的 DNS 记录 | ||
def add_dns_record(domain, ip): | ||
url = f"https://api.cloudflare.com/client/v4/zones/{ZONE_ID}/dns_records" | ||
data = { | ||
"type": "AAAA", # IPv6 记录类型 | ||
"name": domain, | ||
"content": ip, | ||
"ttl": 1, | ||
"proxied": False | ||
} | ||
headers = { | ||
"X-Auth-Email": EMAIL, | ||
"X-Auth-Key": API_KEY, | ||
"Content-Type": "application/json" | ||
} | ||
response = requests.post(url, headers=headers, json=data) | ||
if response.status_code == 200: | ||
print(f"添加成功: {domain} -> {ip}") | ||
elif response.status_code == 409: | ||
print(f"记录已存在: {domain} -> {ip}") | ||
else: | ||
print(f"添加失败: {domain} -> {ip}, 错误信息: {response.status_code}, {response.text}") | ||
|
||
# 主程序 | ||
if __name__ == "__main__": | ||
ip_data = get_ips_from_file("cfipv6.txt") # 改为读取 cfipv6.txt | ||
if not ip_data: | ||
print("未读取到 IP 数据,请检查 cfipv6.txt 文件格式是否正确。") | ||
else: | ||
for ip, location in ip_data: | ||
domain = LOCATION_TO_DOMAIN.get(location) | ||
if domain: | ||
print(f"为域名 {domain} 添加 IP: {ip}") | ||
if not check_existing_record(domain, ip): | ||
add_dns_record(domain, ip) | ||
else: | ||
print(f"记录已存在,跳过: {domain} -> {ip}") | ||
else: | ||
print(f"未找到标记 {location} 对应的域名映射,跳过。") |
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
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