cf.sh 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #!/bin/bash
  2. red='\e[1;31m'
  3. green='\e[0;32m'
  4. NC='\e[0m'
  5. MYIP=$(wget -qO- https://icanhazip.com);
  6. echo "Checking VPS"
  7. apt install jq curl -y
  8. DOMAIN=aidan.my
  9. SUB_DOMAIN=vpn.aidan.my
  10. WILDCARD=*.vpn.aidan.my
  11. CF_ID=irwan@aidan.my
  12. CF_KEY=ff3d540666c202facbd936ba3d0d0110
  13. set -euo pipefail
  14. IP=$(wget -qO- https://icanhazip.com);
  15. echo "Updating DNS for ${SUB_DOMAIN}..."
  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=${SUB_DOMAIN}" \
  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":"'${SUB_DOMAIN}'","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":"'${SUB_DOMAIN}'","content":"'${IP}'","ttl":120,"proxied":false}')
  36. echo "Host : $SUB_DOMAIN"
  37. echo $SUB_DOMAIN > /root/domain
  38. sleep 5
  39. echo "Updating DNS for ${WILDCARD}..."
  40. ZONE=$(curl -sLX GET "https://api.cloudflare.com/client/v4/zones?name=${DOMAIN}&status=active" \
  41. -H "X-Auth-Email: ${CF_ID}" \
  42. -H "X-Auth-Key: ${CF_KEY}" \
  43. -H "Content-Type: application/json" | jq -r .result[0].id)
  44. RECORD=$(curl -sLX GET "https://api.cloudflare.com/client/v4/zones/${ZONE}/dns_records?name=${WILDCARD}" \
  45. -H "X-Auth-Email: ${CF_ID}" \
  46. -H "X-Auth-Key: ${CF_KEY}" \
  47. -H "Content-Type: application/json" | jq -r .result[0].id)
  48. if [[ "${#RECORD}" -le 10 ]]; then
  49. RECORD=$(curl -sLX POST "https://api.cloudflare.com/client/v4/zones/${ZONE}/dns_records" \
  50. -H "X-Auth-Email: ${CF_ID}" \
  51. -H "X-Auth-Key: ${CF_KEY}" \
  52. -H "Content-Type: application/json" \
  53. --data '{"type":"A","name":"'${WILDCARD}'","content":"'${IP}'","ttl":120,"proxied":false}' | jq -r .result.id)
  54. fi
  55. RESULT=$(curl -sLX PUT "https://api.cloudflare.com/client/v4/zones/${ZONE}/dns_records/${RECORD}" \
  56. -H "X-Auth-Email: ${CF_ID}" \
  57. -H "X-Auth-Key: ${CF_KEY}" \
  58. -H "Content-Type: application/json" \
  59. --data '{"type":"A","name":"'${WILDCARD}'","content":"'${IP}'","ttl":120,"proxied":false}')
  60. echo "Host : $WILDCARD"
  61. echo $WILDCARD > /home/wildcard
  62. rm -f /root/cf.sh