Skip to content

Commit

Permalink
cfst: Add support for IPv6.
Browse files Browse the repository at this point in the history
  • Loading branch information
laityts committed Dec 27, 2024
1 parent a7cf4b8 commit f97024d
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 3 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/run_autoddns.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,5 @@ jobs:
- name: Run autoddns script
run: |
python autoddns.py
python autoddns.py
python autoddnsv6.py
92 changes: 92 additions & 0 deletions autoddnsv6.py
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} 对应的域名映射,跳过。")
2 changes: 1 addition & 1 deletion cfst.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down
2 changes: 1 addition & 1 deletion cfstv6.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down

0 comments on commit f97024d

Please sign in to comment.