Skip to content

Commit

Permalink
Update result.csv and ip.txt
Browse files Browse the repository at this point in the history
  • Loading branch information
laityts committed Jan 19, 2025
1 parent 7b528e1 commit 1c6b219
Show file tree
Hide file tree
Showing 13 changed files with 256 additions and 326 deletions.
60 changes: 29 additions & 31 deletions autoddns.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ def emit(self, record):
"☁️CF优选": "cf.616049.xyz" # CF优选
}

# 从 cfip.txt 文件中读取前十个 IP 和标记
def get_ips_from_file(file_path, limit=10):
# 从 ip.txt 文件中读取前200个 IP 和标记
def get_ips_from_file(file_path, limit=200):
ip_data = []
try:
with open(file_path, "r") as file:
Expand All @@ -119,7 +119,7 @@ def get_ips_from_file(file_path, limit=10):
logging.error(f"文件未找到: {file_path}")
return []

# 删除相同前缀的所有 DNS 记录(完全匹配),但至少保留一条
# 删除相同前缀的所有 DNS 记录,但保留最后一条
def delete_dns_records_with_prefix(prefix):
try:
url = f"https://api.cloudflare.com/client/v4/zones/{ZONE_ID}/dns_records"
Expand All @@ -131,14 +131,14 @@ def delete_dns_records_with_prefix(prefix):
response = requests.get(url, headers=headers)
response.raise_for_status()
records = response.json().get("result", [])
logging.info(f"找到 {len(records)} 条 DNS 记录,开始删除与 {prefix} 完全匹配的记录...")
logging.info(f"找到 {len(records)} 条 DNS 记录,开始删除与 {prefix} 完全匹配的记录(保留最后一条)...")

# 过滤出与给定前缀匹配的记录
matching_records = [record for record in records if record["name"].split(".")[0] == prefix]
# 过滤出与给定前缀完全匹配的记录(例如 proxy.us)
matching_records = [record for record in records if record["name"].startswith(prefix + ".")]

# 如果匹配的记录多于一条,则删除多余的记录
# 如果匹配的记录数量大于 1,则删除除最后一条之外的所有记录
if len(matching_records) > 1:
for record in matching_records[1:]: # 保留第一条记录
for record in matching_records[:-1]: # 保留最后一条,删除其他
record_id = record["id"]
delete_url = f"{url}/{record_id}"
delete_response = requests.delete(delete_url, headers=headers)
Expand All @@ -147,42 +147,40 @@ def delete_dns_records_with_prefix(prefix):
else:
logging.error(f"删除失败: {record['name']} -> {record['content']}, 错误信息: {delete_response.status_code}, {delete_response.text}")
else:
logging.info(f"前缀 {prefix} 的记录数量不超过一条,无需删除。")
logging.info(f"没有需要删除的记录,{prefix} 前缀的记录数量为 {len(matching_records)}")
except requests.exceptions.RequestException as e:
logging.error(f"请求失败: {e}")
sys.exit(1)

# 批量添加 DNS 记录,每个前缀最多添加 5 条记录
# 批量添加 DNS 记录
def add_dns_records_bulk(ip_data):
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"
}

# 记录每个前缀已添加的记录数量
prefix_count = {}

# 记录已经删除过哪些前缀
deleted_prefixes = set()
# 记录每个前缀已经添加的记录数量
prefix_counters = {}

for ip, location in ip_data:
domain = LOCATION_TO_DOMAIN.get(location)
if domain:
# 提取前缀(例如 "us.616049.xyz" 的前缀是 "us")
prefix = domain.split(".")[0]

# 初始化前缀计数
if prefix not in prefix_count:
prefix_count[prefix] = 0

# 如果该前缀的记录数量已达到 5 条,则跳过
if prefix_count[prefix] >= 5:
logging.info(f"前缀 {prefix} 的记录数量已达到 5 条,跳过添加。")
# 提取前缀(例如 "proxy.us.616049.xyz" 的前缀是 "proxy.us")
prefix = ".".join(domain.split(".")[:1]) # 提取前两部分(proxy.us)
# 如果该前缀没有被删除过,则删除该前缀的所有 DNS 记录(保留最后一条)
if prefix not in deleted_prefixes:
delete_dns_records_with_prefix(prefix)
deleted_prefixes.add(prefix) # 标记该前缀已删除
prefix_counters[prefix] = 1 # 初始化计数器(因为删除后至少保留一条)

# 如果该前缀的记录数量已经达到 5 条,则跳过
if prefix_counters.get(prefix, 0) >= 5:
logging.info(f"前缀 {prefix} 的记录数量已达到 5 条,跳过添加: {domain} -> {ip}")
continue

# 删除多余记录(至少保留一条)
delete_dns_records_with_prefix(prefix)

# 添加新记录

data = {
"type": "A",
"name": domain,
Expand All @@ -194,7 +192,7 @@ def add_dns_records_bulk(ip_data):
response = requests.post(url, headers=headers, json=data)
if response.status_code == 200:
logging.info(f"添加成功: {domain} -> {ip}")
prefix_count[prefix] += 1 # 更新前缀计数
prefix_counters[prefix] = prefix_counters.get(prefix, 0) + 1 # 增加计数器
elif response.status_code == 409:
logging.info(f"记录已存在: {domain} -> {ip}")
else:
Expand All @@ -209,6 +207,6 @@ def add_dns_records_bulk(ip_data):
# 添加新的 DNS 记录
ip_data = get_ips_from_file("cfip/ip.txt")
if not ip_data:
logging.error("未读取到 IP 数据,请检查 cfip.txt 文件格式是否正确。")
logging.error("未读取到 IP 数据,请检查 ip.txt 文件格式是否正确。")
else:
add_dns_records_bulk(ip_data)
4 changes: 2 additions & 2 deletions autoddnsfd.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ def emit(self, record):
"Proxy": "proxy.616049.xyz" # Proxy
}

# 从 cfipfd.txt 文件中读取前二十个 IP 和标记
def get_ips_from_file(file_path, limit=100):
# 从 cfipfd.txt 文件中读取前200个 IP 和标记
def get_ips_from_file(file_path, limit=200):
ip_data = []
try:
with open(file_path, "r") as file:
Expand Down
45 changes: 29 additions & 16 deletions autoddnsv6.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ def emit(self, record):
"☁️CFIPV6优选": "cfv6.616049.xyz" # CF优选
}

# 从 cfipv6.txt 文件中读取前十个 IPv6 和标记
def get_ips_from_file(file_path, limit=10):
# 从 ip.txt 文件中读取前200个 IP 和标记
def get_ips_from_file(file_path, limit=200):
ip_data = []
try:
with open(file_path, "r") as file:
Expand All @@ -120,7 +120,7 @@ def get_ips_from_file(file_path, limit=10):
logging.error(f"文件未找到: {file_path}")
return []

# 删除相同前缀的所有 DNS 记录(完全匹配)
# 删除相同前缀的所有 DNS 记录,但保留最后一条
def delete_dns_records_with_prefix(prefix):
try:
url = f"https://api.cloudflare.com/client/v4/zones/{ZONE_ID}/dns_records"
Expand All @@ -132,21 +132,23 @@ def delete_dns_records_with_prefix(prefix):
response = requests.get(url, headers=headers)
response.raise_for_status()
records = response.json().get("result", [])
logging.info(f"找到 {len(records)} 条 DNS 记录,开始删除与 {prefix} 完全匹配的记录...")
for record in records:
# 提取记录名称的前缀(例如 "proxy.us.616049.xyz" 的前缀是 "proxy"
record_prefix = record["name"].split(".")[0]
# 仅删除与给定前缀完全匹配的记录
if record_prefix == prefix:
# 打印即将删除的 DNS 记录的详细信息
logging.info(f"即将删除记录: 名称={record['name']}, 类型={record['type']}, 内容={record['content']}, TTL={record['ttl']}, 代理={record['proxied']}")
logging.info(f"找到 {len(records)} 条 DNS 记录,开始删除与 {prefix} 完全匹配的记录(保留最后一条)...")

# 过滤出与给定前缀完全匹配的记录(例如 proxy.us)
matching_records = [record for record in records if record["name"].startswith(prefix + ".")]

# 如果匹配的记录数量大于 1,则删除除最后一条之外的所有记录
if len(matching_records) > 1:
for record in matching_records[:-1]: # 保留最后一条,删除其他
record_id = record["id"]
delete_url = f"{url}/{record_id}"
delete_response = requests.delete(delete_url, headers=headers)
if delete_response.status_code == 200:
logging.info(f"已删除记录: {record['name']} -> {record['content']}")
else:
logging.error(f"删除失败: {record['name']} -> {record['content']}, 错误信息: {delete_response.status_code}, {delete_response.text}")
else:
logging.info(f"没有需要删除的记录,{prefix} 前缀的记录数量为 {len(matching_records)}")
except requests.exceptions.RequestException as e:
logging.error(f"请求失败: {e}")
sys.exit(1)
Expand All @@ -161,17 +163,27 @@ def add_dns_records_bulk(ip_data):
}
# 记录已经删除过哪些前缀
deleted_prefixes = set()
# 记录每个前缀已经添加的记录数量
prefix_counters = {}

for ip, location in ip_data:
domain = LOCATION_TO_DOMAIN.get(location)
if domain:
# 提取前缀(例如 "usv6.616049.xyz" 的前缀是 "usv6")
prefix = domain.split(".")[0]
# 如果该前缀没有被删除过,则删除该前缀的所有 DNS 记录
# 提取前缀(例如 "proxy.us.616049.xyz" 的前缀是 "proxy.us")
prefix = ".".join(domain.split(".")[:1]) # 提取前两部分(proxy.us)
# 如果该前缀没有被删除过,则删除该前缀的所有 DNS 记录(保留最后一条)
if prefix not in deleted_prefixes:
delete_dns_records_with_prefix(prefix)
deleted_prefixes.add(prefix) # 标记该前缀已删除
prefix_counters[prefix] = 1 # 初始化计数器(因为删除后至少保留一条)

# 如果该前缀的记录数量已经达到 5 条,则跳过
if prefix_counters.get(prefix, 0) >= 5:
logging.info(f"前缀 {prefix} 的记录数量已达到 5 条,跳过添加: {domain} -> {ip}")
continue

data = {
"type": "AAAA", # IPv6 记录类型
"type": "AAAA",
"name": domain,
"content": ip,
"ttl": 1,
Expand All @@ -181,6 +193,7 @@ def add_dns_records_bulk(ip_data):
response = requests.post(url, headers=headers, json=data)
if response.status_code == 200:
logging.info(f"添加成功: {domain} -> {ip}")
prefix_counters[prefix] = prefix_counters.get(prefix, 0) + 1 # 增加计数器
elif response.status_code == 409:
logging.info(f"记录已存在: {domain} -> {ip}")
else:
Expand All @@ -195,6 +208,6 @@ def add_dns_records_bulk(ip_data):
# 添加新的 DNS 记录
ip_data = get_ips_from_file("cfip/ipv6.txt")
if not ip_data:
logging.error("未读取到 IP 数据,请检查 cfipv6.txt 文件格式是否正确。")
logging.error("未读取到 IP 数据,请检查 ip.txt 文件格式是否正确。")
else:
add_dns_records_bulk(ip_data)
78 changes: 40 additions & 38 deletions cfip/ip.txt
Original file line number Diff line number Diff line change
@@ -1,38 +1,40 @@
104.16.12.237#🇭🇰HKG
104.18.68.196#🇭🇰HKG
104.16.15.107#🇭🇰HKG
104.16.14.6#🇭🇰HKG
104.16.1.234#🇭🇰HKG
162.159.60.4#🇭🇰HKG
104.18.69.15#🇭🇰HKG
104.16.3.233#🇭🇰HKG
162.159.42.162#🇭🇰HKG
104.17.133.236#🇭🇰HKG
104.17.36.44#🇺🇸SJC
104.16.233.106#🇺🇸SJC
104.21.37.201#🇺🇸SJC
104.17.40.40#🇺🇸SJC
104.16.80.248#🇺🇸SJC
172.67.219.82#🇺🇸SJC
104.21.68.142#🇺🇸SJC
104.22.19.57#🇺🇸SJC
104.21.33.150#🇺🇸SJC
104.22.31.238#🇺🇸SJC
104.18.1.154#🇺🇸SEA
190.93.246.136#🇺🇸SEA
172.67.10.134#🇺🇸SEA
104.22.58.201#🇺🇸SEA
104.18.150.228#🇺🇸SEA
104.18.128.103#🇺🇸SEA
104.26.7.159#🇺🇸SEA
162.159.60.134#🇺🇸SEA
104.16.124.106#🇺🇸SEA
104.18.115.121#🇺🇸SEA
162.159.160.54#🇩🇪FRA
104.20.51.108#🇩🇪FRA
104.20.63.0#🇩🇪FRA
104.20.57.72#🇩🇪FRA
104.20.55.34#🇩🇪FRA
104.20.50.1#🇩🇪FRA
104.20.59.10#🇩🇪FRA
104.20.48.126#🇩🇪FRA
104.16.15.209#🇭🇰HKG
104.17.197.170#🇭🇰HKG
104.16.9.129#🇭🇰HKG
104.17.199.94#🇭🇰HKG
104.17.203.109#🇭🇰HKG
104.17.206.155#🇭🇰HKG
104.17.132.177#🇭🇰HKG
104.18.159.35#🇭🇰HKG
104.18.236.109#🇭🇰HKG
104.18.66.217#🇭🇰HKG
104.16.224.65#🇺🇸SJC
104.18.97.117#🇺🇸SJC
104.17.76.170#🇺🇸SJC
104.16.177.111#🇺🇸SJC
104.18.137.26#🇺🇸SJC
104.16.17.245#🇺🇸SJC
104.16.176.2#🇺🇸SJC
104.18.100.184#🇺🇸SJC
104.17.77.205#🇺🇸SJC
104.17.86.91#🇺🇸SJC
104.16.78.213#🇺🇸SEA
104.19.233.81#🇺🇸SEA
104.19.255.186#🇺🇸SEA
172.67.218.139#🇺🇸SEA
104.16.143.223#🇺🇸SEA
104.17.71.161#🇺🇸SEA
104.19.237.4#🇺🇸SEA
104.16.75.154#🇺🇸SEA
104.17.79.74#🇺🇸SEA
104.17.35.2#🇺🇸SEA
104.18.34.10#🇩🇪FRA
104.20.48.11#🇩🇪FRA
104.20.60.188#🇩🇪FRA
104.20.59.211#🇩🇪FRA
104.20.53.140#🇩🇪FRA
104.20.62.25#🇩🇪FRA
104.20.52.119#🇩🇪FRA
104.20.50.52#🇩🇪FRA
104.20.63.252#🇩🇪FRA
162.159.160.247#🇩🇪FRA
18 changes: 0 additions & 18 deletions cfip/ipv6.txt

This file was deleted.

1 change: 0 additions & 1 deletion cfst.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ def remove_file(file_path):
port_txt = "port/ipport.txt"
log_file = "log/log.txt"
output_cf_txt = "speed/ip.txt"
proxy_txt = "cfip/proxy.txt"
iplog_file = "log/iplog.txt"
commit_message = "Update result.csv and ip.txt"
download_url = "https://github.com/XIU2/CloudflareSpeedTest/releases/download/v2.2.5/CloudflareST_linux_arm64.tar.gz"
Expand Down
Loading

0 comments on commit 1c6b219

Please sign in to comment.