diff --git a/.github/workflows/run_autoddns.yml b/.github/workflows/run_autoddns.yml index 033f1c40..e57b37c1 100644 --- a/.github/workflows/run_autoddns.yml +++ b/.github/workflows/run_autoddns.yml @@ -31,4 +31,5 @@ jobs: - name: Run autoddns script run: | - python autoddns.py \ No newline at end of file + python autoddns.py + python autoddnsv6.py \ No newline at end of file diff --git a/autoddnsv6.py b/autoddnsv6.py new file mode 100644 index 00000000..bcff5fef --- /dev/null +++ b/autoddnsv6.py @@ -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} 对应的域名映射,跳过。") \ No newline at end of file diff --git a/cfst.py b/cfst.py index b077e3d9..32bc578d 100755 --- a/cfst.py +++ b/cfst.py @@ -112,7 +112,7 @@ def get_colo(ip_address): remove_file(log_file) # 执行 cfst 命令,使用变量传递 cfcolo -subprocess.run(["./cfst", "-httping", "-cfcolo", cfcolo, "-tl", "200", "-tll", "40", "-tp", "443", "-sl", "5", "-dn", "10"], check=True) +subprocess.run(["./cfst", "-httping", "-cfcolo", cfcolo, "-tl", "200", "-tll", "20", "-tp", "443", "-sl", "5", "-dn", "10"], check=True) # 提取 IP 地址并保存到 cfip.txt ip_addresses = [] diff --git a/cfstv6.py b/cfstv6.py index 478eb15c..91540af0 100755 --- a/cfstv6.py +++ b/cfstv6.py @@ -112,7 +112,7 @@ def get_colo(ip_address): remove_file(log_file) # 执行 cfst 命令,使用变量传递 cfcolo -subprocess.run(["./cfst", "-f", "ipv6.txt", "-o", "resultv6.csv", "-httping", "-cfcolo", cfcolo, "-tl", "100", "-tll", "40", "-tp", "443", "-sl", "5", "-dn", "10"], check=True) +subprocess.run(["./cfst", "-f", "ipv6.txt", "-o", "resultv6.csv", "-httping", "-cfcolo", cfcolo, "-tl", "100", "-tll", "20", "-tp", "443", "-sl", "5", "-dn", "10"], check=True) # 提取 IP 地址并保存到 cfip.txt ip_addresses = []