cfst_dnspod.sh 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #!/bin/bash
  2. # --------------------------------------------------------------
  3. # 项目: CloudflareSpeedTest 自动更新Dnspod优选解析
  4. # 版本: 1.0.0
  5. # 作者: imashen
  6. # --------------------------------------------------------------
  7. # 清理历史残留
  8. rm -f result4.csv result6.csv
  9. # DNSPod API 凭据
  10. dnspod_token="${API_TOKEN}"
  11. dnspod_domain="${DOMAIN}"
  12. dnspod_record="${SUB_DOMAIN}"
  13. # DNSPod API URL
  14. dnspod_api_url="https://dnsapi.cn"
  15. # 获取记录 ID
  16. get_record_id() {
  17. local record_type=$1
  18. local response
  19. response=$(curl -s -X POST -d "login_token=$dnspod_token&format=json&domain=$dnspod_domain&record_type=$record_type" "$dnspod_api_url/Record.List")
  20. local record_id
  21. record_id=$(echo "$response" | jq -r --arg type "$record_type" '.records[] | select(.type == $type) | .id')
  22. echo "$record_id"
  23. }
  24. # 创建 DNS 记录
  25. create_dns_record() {
  26. local record_type=$1
  27. local ip_address=$2
  28. local response
  29. response=$(curl -s -X POST -d "login_token=$dnspod_token&format=json&domain=$dnspod_domain&sub_domain=$dnspod_record&record_type=$record_type&record_line=默认&value=$ip_address" "$dnspod_api_url/Record.Create")
  30. local record_id
  31. record_id=$(echo "$response" | jq -r '.record.id')
  32. echo "$record_id"
  33. }
  34. # 更新 DNS 记录
  35. update_dns_record() {
  36. local record_id=$1
  37. local record_type=$2
  38. local ip_address=$3
  39. curl -s -X POST -d "login_token=$dnspod_token&format=json&domain=$dnspod_domain&record_id=$record_id&sub_domain=$dnspod_record&record_type=$record_type&record_line=默认&value=$ip_address" "$dnspod_api_url/Record.Modify"
  40. }
  41. # 运行 CFST v4
  42. ./cfst -f ip.txt -n 500 -o result4.csv
  43. # 读取 CSV 文件并提取优选 IPv4 地址
  44. preferred_ipv4=$(awk -F, 'NR==2 {print $1}' result4.csv)
  45. # 检查是否获取到了 IPv4 地址
  46. if [ -z "$preferred_ipv4" ]; then
  47. echo "Failed to get the preferred IPv4 address."
  48. else
  49. echo "BETTER IPv4: $preferred_ipv4"
  50. # 获取 IPv4 记录 ID
  51. ipv4_record_id=$(get_record_id "A")
  52. if [ -n "$ipv4_record_id" ]; then
  53. # 更新 IPv4 记录
  54. update_dns_record "$ipv4_record_id" "A" "$preferred_ipv4"
  55. echo "Updated DNSPod record with IPv4: $preferred_ipv4"
  56. else
  57. # 创建 IPv4 记录
  58. new_ipv4_record_id=$(create_dns_record "A" "$preferred_ipv4")
  59. if [ -n "$new_ipv4_record_id" ]; then
  60. echo "Created DNSPod record with IPv4: $preferred_ipv4"
  61. else
  62. echo "Failed to create DNSPod record with IPv4."
  63. fi
  64. fi
  65. fi
  66. # 运行 CFST v6
  67. ./cfst -f ipv6.txt -n 500 -o result6.csv
  68. # 读取 CSV 文件并提取优选 IPv6 地址
  69. preferred_ipv6=$(awk -F, 'NR==2 {print $1}' result6.csv)
  70. # 检查是否获取到了 IPv6 地址
  71. if [ -z "$preferred_ipv6" ]; then
  72. echo "Failed to get the preferred IPv6 address."
  73. else
  74. echo "BETTER IPv6: $preferred_ipv6"
  75. # 获取 IPv6 记录 ID
  76. ipv6_record_id=$(get_record_id "AAAA")
  77. if [ -n "$ipv6_record_id" ]; then
  78. # 更新 IPv6 记录
  79. update_dns_record "$ipv6_record_id" "AAAA" "$preferred_ipv6"
  80. echo "Updated DNSPod record with IPv6: $preferred_ipv6"
  81. else
  82. # 创建 IPv6 记录
  83. new_ipv6_record_id=$(create_dns_record "AAAA" "$preferred_ipv6")
  84. if [ -n "$new_ipv6_record_id" ]; then
  85. echo "Created DNSPod record with IPv6: $preferred_ipv6"
  86. else
  87. echo "Failed to create DNSPod record with IPv6."
  88. fi
  89. fi
  90. fi