cf.sh 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #!/bin/bash
  2. red='\e[1;31m'
  3. green='\e[0;32m'
  4. NC='\e[0m'
  5. MYIP=$(wget -qO- ipinfo.io/ip);
  6. echo "Checking VPS"
  7. apt install jq curl -y
  8. DOMAIN=$host
  9. sub=$(</dev/urandom tr -dc a-z0-9 | head -c4)
  10. SUB_DOMAIN=${sub}$host
  11. CF_ID=$CF_ID
  12. CF_KEY=$CF_KEY
  13. set -euo pipefail
  14. IP=$(wget -qO- ipinfo.io/ip);
  15. echo "Updating DNS for $host..."
  16. ZONE=$(curl -sLX GET "https://api.cloudflare.com/client/v4/zones?name=${DOMAIN}&status=active" \
  17. -H "X-Auth-Email: ${CF_ID}" \
  18. -H "X-Auth-Key: ${CF_KEY}" \
  19. -H "Content-Type: application/json" | jq -r .result[0].id)
  20. RECORD=$(curl -sLX GET "https://api.cloudflare.com/client/v4/zones/${ZONE}/dns_records?name=${host}" \
  21. -H "X-Auth-Email: ${CF_ID}" \
  22. -H "X-Auth-Key: ${CF_KEY}" \
  23. -H "Content-Type: application/json" | jq -r .result[0].id)
  24. if [[ "${#RECORD}" -le 10 ]]; then
  25. RECORD=$(curl -sLX POST "https://api.cloudflare.com/client/v4/zones/${ZONE}/dns_records" \
  26. -H "X-Auth-Email: ${CF_ID}" \
  27. -H "X-Auth-Key: ${CF_KEY}" \
  28. -H "Content-Type: application/json" \
  29. --data '{"type":"A","name":"'${host}'","content":"'${IP}'","ttl":120,"proxied":false}' | jq -r .result.id)
  30. fi
  31. RESULT=$(curl -sLX PUT "https://api.cloudflare.com/client/v4/zones/${ZONE}/dns_records/${RECORD}" \
  32. -H "X-Auth-Email: ${CF_ID}" \
  33. -H "X-Auth-Key: ${CF_KEY}" \
  34. -H "Content-Type: application/json" \
  35. --data '{"type":"A","name":"'${host}'","content":"'${IP}'","ttl":120,"proxied":false}')
  36. echo "Host : $host"
  37. echo $host > /root/domain
  38. rm -f /root/cf.sh