#!/bin/bash # 检查并安装依赖项 jq 和 bc if ! command -v jq &> /dev/null then echo "jq could not be found, installing now..." sudo apt-get update && sudo apt-get install -y jq fi if ! command -v bc &> /dev/null then echo "bc could not be found, installing now..." sudo apt-get update && sudo apt-get install -y bc fi # 输出设置 crontab 的命令 echo "To run this script every day at 3 AM, add the following line to your crontab:" echo "0 3 * * * /path/to/this/script.sh" # 测速脚本部分 # 使用 dig 命令查询 DNS 记录并写入 ip.txt dig cf.youoffer.net A +short > ip.txt # 创建一个新的文件 final_ips.txt touch final_ips.txt #延迟检测部分 while IFS= read -r ip do echo "Checking latency for $ip" # 使用 ping 命令检查延迟 ping_result=$(ping -c 1 -W 1 "$ip" 2>&1) ping_status=$? # 如果 ping 成功 if [ $ping_status -eq 0 ]; then # 提取延迟时间 ping_time=$(echo "$ping_result" | grep time= | cut -d'=' -f 4 | cut -d' ' -f 1) # 显示延迟时间 echo "Latency for $ip is $ping_time ms" # 如果延迟时间大于 200ms if (( $(echo "$ping_time > 200" | bc -l) )); then echo "Latency for $ip is too high, discarding." # 从 ip.txt 删除延迟时间大于 200ms 的 IP sed -i "/$ip/d" ip.txt fi else echo "Ping to $ip failed or timed out." # 从 ip.txt 删除无法 ping 通的 IP sed -i "/$ip/d" ip.txt fi done < ip.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" # 如果下载速度大于等于 8Mbps if (( $(echo "$download_speed >= 8" | bc -l) )); then # 将大于等于 8Mbps 的 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 >= 8Mbps are written to final_ips.txt" # WebSocket 检测部分 IP_USE_FILE="ip_use.txt" touch "$IP_USE_FILE" # 读取 final_ips.txt 文件中的每个 IP 地址,并进行 WebSocket 测试 while IFS= read -r IP_ADDRESS; do # 忽略空行 if [[ -z "$IP_ADDRESS" ]]; then continue fi echo "Testing WebSocket connection for IP address: $IP_ADDRESS" # 使用 curl 测试 WebSocket 连接 HTTP_RESPONSE=$(curl --include \ --no-buffer \ --max-time 5 \ --resolve speed.cloudflare.com:443:"$IP_ADDRESS" \ --header "Connection: Upgrade" \ --header "Upgrade: websocket" \ --header "Host: speed.cloudflare.com" \ --header "Origin: https://speed.cloudflare.com" \ --header "Sec-WebSocket-Key: SGVsbG8sIHdvcmxkIQ==" \ --header "Sec-WebSocket-Version: 13" \ "https://speed.cloudflare.com/" --silent --output /dev/null --write-out "%{http_code}") # 检查 HTTP 响应码 if [[ "$HTTP_RESPONSE" == "101" ]]; then echo "$IP_ADDRESS" >> "$IP_USE_FILE" echo "WebSocket connection success: HTTP 101 Switching Protocols" else echo "WebSocket connection failed: HTTP response was $HTTP_RESPONSE or connection timed out" # 从 final_ips.txt 删除无法建立 WebSocket 连接的 IP sed -i "/$IP_ADDRESS/d" final_ips.txt fi done < final_ips.txt echo "WebSocket tests completed. IPs with successful WebSocket connections are written to $IP_USE_FILE" # Cloudflare DDNS 更新部分 # 以下变量需要填写 API_KEY="your_cloudflare_global_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 < ip_use.txt # 删除临时文件 rm -rf ip.txt final_ips.txt ip_use.txt