From 055d5102890bd3299315960c9d1acc1abf520edd Mon Sep 17 00:00:00 2001 From: chunzhi Date: Thu, 8 Feb 2024 14:19:01 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20choose-cn-v2.sh?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- choose-cn-v2.sh | 90 +++++++++++++++++++++++++++++++++++++++++++++++++ choose-cn.sh | 41 ---------------------- 2 files changed, 90 insertions(+), 41 deletions(-) create mode 100644 choose-cn-v2.sh delete mode 100644 choose-cn.sh diff --git a/choose-cn-v2.sh b/choose-cn-v2.sh new file mode 100644 index 0000000..e2ebc54 --- /dev/null +++ b/choose-cn-v2.sh @@ -0,0 +1,90 @@ +#!/bin/bash + +# 测速脚本部分 +# 使用 dig 命令查询 DNS 记录并写入 ip.txt +dig cf.youoffer.net A +short > ip.txt + +# 创建一个新的文件 final_ips.txt +touch final_ips.txt + +# 读取 ip.txt 中的每个 IP 地址进行下载测试 +while IFS= read -r ip +do + echo "Starting download test for $ip" + # 使用 curl 命令进行下载测试 + start_time=$(date +%s.%N) + curl -s --resolve speed.cloudflare.com:443:"$ip" https://speed.cloudflare.com/__down?bytes=10000000 -o /dev/null --connect-timeout 5 --max-time 10 + curl_status=$? + end_time=$(date +%s.%N) + elapsed_time=$(echo "$end_time - $start_time" | bc) + + # 检查 curl 命令的返回状态 + if [ $curl_status -eq 0 ]; then + # 计算下载速度 + download_speed=$(echo "scale=2; (10000000*8)/(($elapsed_time)*1000000)" | bc) + + # 显示下载速度 + echo "$ip has a download speed of $download_speed Mbps" + + # 如果下载速度大于等于 50Mbps + if (( $(echo "$download_speed >= 50" | bc -l) )); then + # 将大于等于 50Mbps 的 IP 写入 final_ips.txt + echo "$ip" >> final_ips.txt + fi + else + echo "Download test for $ip failed with status $curl_status" + fi +done < ip.txt + +echo "Download tests completed. IPs with download speed >= 50Mbps are written to final_ips.txt" + +# Cloudflare DDNS 更新部分 +# 以下变量需要填写 +API_KEY="your_cloudflare_api_key" +EMAIL="your_email@example.com" +ZONE_ID="your_zone_id" +DOMAIN="your_domain.com" +SUBDOMAIN="sub.your_domain.com" +TTL=300 + +# 使用Cloudflare API获取子域名的所有A记录 +records=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/${ZONE_ID}/dns_records?type=A&name=${SUBDOMAIN}" \ +-H "X-Auth-Email: ${EMAIL}" \ +-H "X-Auth-Key: ${API_KEY}" \ +-H "Content-Type: application/json") + +# 从返回值中提取出所有A记录的ID +record_ids=$(echo "$records" | jq -r '.result[].id') + +# 遍历所有A记录的ID,并使用Cloudflare API删除DNS记录 +for record_id in $record_ids +do + response=$(curl -s -X DELETE "https://api.cloudflare.com/client/v4/zones/${ZONE_ID}/dns_records/${record_id}" \ + -H "X-Auth-Email: ${EMAIL}" \ + -H "X-Auth-Key: ${API_KEY}" \ + -H "Content-Type: application/json") + + if echo "$response" | grep -q '"success":true'; then + echo "DNS record with ID $record_id deleted successfully" + else + echo "Failed to delete DNS record with ID $record_id" + fi +done + +# 从final_ips.txt文件中读取IP地址,并使用Cloudflare API添加DNS记录 +while IFS= read -r ip +do + # 使用Cloudflare API添加DNS记录 + response=$(curl -s -X POST "https://api.cloudflare.com/client/v4/zones/${ZONE_ID}/dns_records" \ + -H "X-Auth-Email: ${EMAIL}" \ + -H "X-Auth-Key: ${API_KEY}" \ + -H "Content-Type: application/json" \ + --data '{"type":"A","name":"'${SUBDOMAIN}'","content":"'${ip}'","ttl":'${TTL}',"proxied":false}') + + # 检查返回值中的success字段是否为true + if echo "$response" | grep -q '"success":true'; then + echo "DNS record for IP $ip added successfully" + else + echo "Failed to add DNS record for IP $ip" + fi +done < final_ips.txt diff --git a/choose-cn.sh b/choose-cn.sh deleted file mode 100644 index 96f1ee1..0000000 --- a/choose-cn.sh +++ /dev/null @@ -1,41 +0,0 @@ -#!/bin/bash - -# 使用 dig 命令查询 DNS 记录并写入 ip.txt -dig cf.youoffer.net A +short > ip.txt - -# 创建一个新的文件 ip_use.txt -touch ip_use.txt - -# 读取 ip.txt 中的每个 IP 地址进行下载测试 -while IFS= read -r ip -do - echo "Starting download test for $ip" - # 使用 curl 命令进行下载测试 - start_time=$(date +%s.%N) - curl -s --resolve speed.cloudflare.com:443:"$ip" https://speed.cloudflare.com/__down?bytes=10000000 -o /dev/null --connect-timeout 5 --max-time 10 - curl_status=$? - end_time=$(date +%s.%N) - elapsed_time=$(echo "$end_time - $start_time" | bc) - - # 检查 curl 命令的返回状态 - if [ $curl_status -eq 0 ]; then - # 计算下载速度 - download_speed=$(echo "scale=2; (10000000*8)/(($elapsed_time)*1000000)" | bc) - - # 显示下载速度 - echo "$ip has a download speed of $download_speed Mbps" - - # 如果下载速度大于等于 50Mbps - if (( $(echo "$download_speed >= 50" | bc -l) )); then - # 将大于等于 50Mbps 的 IP 写入 ip_use.txt - echo "$ip" >> ip_use.txt - fi - else - echo "Download test for $ip failed with status $curl_status" - fi -done < ip.txt - -# 重命名 ip_use.txt 为 final_ips.txt,因为 ip_use.txt 在循环中被touch过,可能包含旧数据 -mv ip_use.txt final_ips.txt - -echo "Download tests completed. IPs with download speed >= 50Mbps are written to final_ips.txt"