├── 345673.py ├── README.md ├── ymyuuubestcf.py └── ymyuuuproxy.py /345673.py: -------------------------------------------------------------------------------- 1 | import requests 2 | import csv 3 | 4 | # Cloudflare API参数 5 | api_token = "****" 6 | zone_id = "****" 7 | domain = "****" # 您的二级域名 8 | 9 | # Cloudflare API端点 10 | api_url = f"https://api.cloudflare.com/client/v4/zones/{zone_id}/dns_records" 11 | 12 | # 请求标头 13 | headers = { 14 | "Authorization": f"Bearer {api_token}", 15 | "Content-Type": "application/json" 16 | } 17 | 18 | # 删除指定二级域名下的所有 DNS 记录 19 | def delete_all_dns_records(): 20 | response = requests.get(api_url, headers=headers, params={"name": domain}) 21 | if response.status_code == 200: 22 | result = response.json() 23 | dns_records = result["result"] 24 | dns_record_ids = [record["id"] for record in dns_records] 25 | 26 | for record_id in dns_record_ids: 27 | delete_response = requests.delete(f"{api_url}/{record_id}", headers=headers) 28 | if delete_response.status_code == 200: 29 | print(f"已删除 DNS 记录: {record_id}") 30 | else: 31 | print(f"删除 DNS 记录时出错:{delete_response.text}") 32 | else: 33 | print("获取 DNS 记录时出错:", response.text) 34 | 35 | # 获取优选 IP 数据并筛选延迟最低的 3 个数据 36 | def fetch_and_filter_ips(): 37 | url = "https://api.345673.xyz/get_data" 38 | key = "o1zrmHAF" 39 | data = {"key": key} 40 | 41 | response = requests.post(url, json=data) 42 | if response.status_code == 200: 43 | result = response.json() 44 | if result["code"] == 200: 45 | info = result["info"] 46 | 47 | ip_list = [] 48 | for category_name, ips in info.items(): 49 | for ip_info in ips: 50 | delay_str = ip_info["delay"].replace("ms", "") 51 | try: 52 | delay = float(delay_str) 53 | ip_info["delay"] = delay 54 | ip_info["category"] = category_name # 添加 category 信息 55 | ip_list.append(ip_info) 56 | except ValueError: 57 | print("无法转换延迟值为浮点数:{}".format(ip_info['delay'])) 58 | 59 | # 按延迟排序并取前 3 个 60 | ip_list.sort(key=lambda x: x["delay"]) 61 | top_3_ips = ip_list[:3] 62 | 63 | with open('ip.csv', 'w', newline='', encoding='utf-8') as csvfile: 64 | fieldnames = ["节点类别", "IP地址", "线路", "节点", "延迟", "下载速度", "时间"] 65 | writer = csv.DictWriter(csvfile, fieldnames=fieldnames) 66 | writer.writeheader() 67 | 68 | for ip_info in top_3_ips: 69 | writer.writerow({ 70 | "节点类别": ip_info["category"], 71 | "IP地址": ip_info["ip"], 72 | "线路": ip_info["line"], 73 | "节点": ip_info["node"], 74 | "延迟": ip_info["delay"], 75 | "下载速度": ip_info["downloadspeed"], 76 | "时间": ip_info["time"] 77 | }) 78 | 79 | return [ip_info["ip"] for ip_info in top_3_ips] 80 | else: 81 | print("请求失败,错误信息:{}".format(result['info'])) 82 | else: 83 | print("请求失败,状态码:{}".format(response.status_code)) 84 | return [] 85 | 86 | # 将筛选后的 IP 地址解析到 Cloudflare 域名下 87 | def add_dns_records(ip_addresses): 88 | for ip_address in ip_addresses: 89 | data = { 90 | "type": "A", 91 | "name": domain, 92 | "content": ip_address, 93 | "ttl": 1, 94 | "proxied": False 95 | } 96 | response = requests.post(api_url, headers=headers, json=data) 97 | if response.status_code == 200: 98 | print(f"IP地址 {ip_address} 已成功解析到 Cloudflare 域名下") 99 | else: 100 | print(f"解析IP地址 {ip_address} 时出错:{response.text}") 101 | 102 | # 主函数 103 | def main(): 104 | delete_all_dns_records() 105 | ip_addresses = fetch_and_filter_ips() 106 | if ip_addresses: 107 | add_dns_records(ip_addresses) 108 | print("所有操作已完成") 109 | else: 110 | print("没有符合条件的 IP 地址") 111 | 112 | if __name__ == "__main__": 113 | main() 114 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 1,先安装pythone3 2 | 3 | sudo apt update 4 | 5 | sudo apt install python3 6 | 7 | sudo apt install python3-pip 8 | 9 | 2,上传py文件到服务器,windows,NAS,OPENWRT,DOCKER 或者家用小主机支持py的都可以 10 | 11 | 只需填写CF的api token,zone id,和解析域名 12 | 13 | 3,python3运行***.py文件 14 | 15 | 4,设置自动运行 16 | 17 | crontab -e 18 | 19 | 0 */6 * * * /usr/bin/python3 /root/*****.py #6小时运行一次 20 | 21 | 22 | 23 | 对接的api为https://api.345673.xyz/get_data 24 | ![image](https://github.com/dockkkk/api-cfcdn/assets/102992310/f99c5628-d88f-4e65-8e58-2185786ed142) 25 | 26 | 27 | 感谢isJielin大佬 ymyuuu大佬 提供的免费优选 28 | -------------------------------------------------------------------------------- /ymyuuubestcf.py: -------------------------------------------------------------------------------- 1 | import requests 2 | import csv 3 | 4 | # Cloudflare API参数 5 | api_token = "*********" 6 | zone_id = "*****" 7 | subdomain = "*****" # 您的二级域名 8 | 9 | # Cloudflare API端点 10 | api_url = f"https://api.cloudflare.com/client/v4/zones/{zone_id}/dns_records" 11 | 12 | # 请求标头 13 | headers = { 14 | "Authorization": f"Bearer {api_token}", 15 | "Content-Type": "application/json" 16 | } 17 | 18 | # 发送GET请求以获取所有指定二级域名下的DNS记录 19 | response = requests.get(api_url, headers=headers, params={"name": subdomain}) 20 | 21 | # 检查响应状态码 22 | if response.status_code == 200: 23 | # 解析JSON响应 24 | data = response.json() 25 | 26 | # 遍历每个DNS记录并删除它们 27 | for record in data['result']: 28 | record_id = record['id'] 29 | 30 | # 构造删除记录的API端点 31 | delete_url = f"{api_url}/{record_id}" 32 | 33 | # 发送DELETE请求以删除记录 34 | delete_response = requests.delete(delete_url, headers=headers) 35 | 36 | # 检查删除记录的响应状态码 37 | if delete_response.status_code == 200: 38 | print(f"已成功删除DNS记录:{record['name']} - {record['content']}") 39 | else: 40 | print(f"删除DNS记录时出错:{delete_response.text}") 41 | 42 | # 删除完毕后,您可以继续添加新的记录 43 | # 定义文件URL和保存路径 44 | url = "https://raw.githubusercontent.com/ymyuuu/IPDB/main/bestcf.txt" 45 | save_path = "result.csv" 46 | 47 | # 发送GET请求到URL 48 | response = requests.get(url) 49 | 50 | # 检查请求是否成功(状态码为200) 51 | if response.status_code == 200: 52 | # 解码响应内容为文本 53 | content = response.text 54 | 55 | # 将内容分割成行 56 | lines = content.split('\n') 57 | 58 | # 以写入模式打开CSV文件 59 | with open(save_path, 'w', newline='', encoding='utf-8') as csvfile: 60 | # 创建CSV写入器对象 61 | csv_writer = csv.writer(csvfile) 62 | 63 | # 遍历每一行 64 | for line in lines: 65 | # 通过空格分割行 66 | data = line.split() 67 | 68 | # 将数据写入CSV文件 69 | csv_writer.writerow(data) 70 | 71 | print("数据已保存到", save_path) 72 | 73 | # 从CSV文件中读取IP地址并解析到Cloudflare域名下 74 | with open(save_path, 'r', newline='', encoding='utf-8') as csvfile: 75 | # 创建CSV读取器对象 76 | csv_reader = csv.reader(csvfile) 77 | 78 | # 跳过标题行(如果有的话) 79 | next(csv_reader) 80 | 81 | # 读取每一行数据 82 | for row in csv_reader: 83 | ip_address = row[0] # 假设IP地址在第一列 84 | 85 | # Cloudflare API端点 86 | api_url = f"https://api.cloudflare.com/client/v4/zones/{zone_id}/dns_records" 87 | 88 | # 请求主体 89 | data = { 90 | "type": "A", 91 | "name": subdomain, 92 | "content": ip_address, 93 | "ttl": 1, # TTL(生存时间),以秒为单位 94 | "proxied": False # 关闭Cloudflare代理 95 | } 96 | 97 | # 发送POST请求以创建DNS记录 98 | response = requests.post(api_url, headers=headers, json=data) 99 | 100 | # 检查响应状态码 101 | if response.status_code == 200: 102 | print(f"IP地址 {ip_address} 已成功解析到 Cloudflare 域名下") 103 | else: 104 | print(f"解析IP地址 {ip_address} 时出错:", response.text) 105 | else: 106 | print("无法从URL检索数据") 107 | else: 108 | print("获取DNS记录时出错:", response.text) 109 | -------------------------------------------------------------------------------- /ymyuuuproxy.py: -------------------------------------------------------------------------------- 1 | import requests 2 | import csv 3 | 4 | # Cloudflare API参数 5 | api_token = "****" 6 | zone_id = "****" 7 | subdomain = "****" # 您的二级域名 8 | 9 | # Cloudflare API端点 10 | api_url = f"https://api.cloudflare.com/client/v4/zones/{zone_id}/dns_records" 11 | 12 | # 请求标头 13 | headers = { 14 | "Authorization": f"Bearer {api_token}", 15 | "Content-Type": "application/json" 16 | } 17 | 18 | # 发送GET请求以获取所有指定二级域名下的DNS记录 19 | response = requests.get(api_url, headers=headers, params={"name": subdomain}) 20 | 21 | # 检查响应状态码 22 | if response.status_code == 200: 23 | # 解析JSON响应 24 | data = response.json() 25 | 26 | # 遍历每个DNS记录并删除它们 27 | for record in data['result']: 28 | record_id = record['id'] 29 | 30 | # 构造删除记录的API端点 31 | delete_url = f"{api_url}/{record_id}" 32 | 33 | # 发送DELETE请求以删除记录 34 | delete_response = requests.delete(delete_url, headers=headers) 35 | 36 | # 检查删除记录的响应状态码 37 | if delete_response.status_code == 200: 38 | print(f"已成功删除DNS记录:{record['name']} - {record['content']}") 39 | else: 40 | print(f"删除DNS记录时出错:{delete_response.text}") 41 | 42 | # 删除完毕后,您可以继续添加新的记录 43 | # 定义文件URL和保存路径 44 | url = "https://raw.githubusercontent.com/ymyuuu/IPDB/main/bestproxy.txt" 45 | save_path = "result.csv" 46 | 47 | # 发送GET请求到URL 48 | response = requests.get(url) 49 | 50 | # 检查请求是否成功(状态码为200) 51 | if response.status_code == 200: 52 | # 解码响应内容为文本 53 | content = response.text 54 | 55 | # 将内容分割成行 56 | lines = content.split('\n') 57 | 58 | # 以写入模式打开CSV文件 59 | with open(save_path, 'w', newline='', encoding='utf-8') as csvfile: 60 | # 创建CSV写入器对象 61 | csv_writer = csv.writer(csvfile) 62 | 63 | # 遍历每一行 64 | for line in lines: 65 | # 通过空格分割行 66 | data = line.split() 67 | 68 | # 将数据写入CSV文件 69 | csv_writer.writerow(data) 70 | 71 | print("数据已保存到", save_path) 72 | 73 | # 从CSV文件中读取IP地址并解析到Cloudflare域名下 74 | with open(save_path, 'r', newline='', encoding='utf-8') as csvfile: 75 | # 创建CSV读取器对象 76 | csv_reader = csv.reader(csvfile) 77 | 78 | # 跳过标题行(如果有的话) 79 | next(csv_reader, None) 80 | 81 | # 读取每一行数据 82 | for row in csv_reader: 83 | if row: # 检查行是否为空 84 | ip_address = row[0] # 假设IP地址在第一列 85 | 86 | # 请求主体 87 | data = { 88 | "type": "A", 89 | "name": subdomain, 90 | "content": ip_address, 91 | "ttl": 1, # TTL(生存时间),以秒为单位 92 | "proxied": False # 关闭 Cloudflare 代理 93 | } 94 | 95 | # 发送POST请求以创建DNS记录 96 | response = requests.post(api_url, headers=headers, json=data) 97 | 98 | # 检查响应状态码 99 | if response.status_code == 200: 100 | print(f"IP地址 {ip_address} 已成功解析到 Cloudflare 域名下") 101 | else: 102 | print(f"解析IP地址 {ip_address} 时出错:", response.text) 103 | else: 104 | print("无法从URL检索数据") 105 | else: 106 | print("获取DNS记录时出错:", response.text) 107 | --------------------------------------------------------------------------------