You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

115 lines
2.8 KiB

  1. #!/usr/bin/env sh
  2. #
  3. #RAGE4_TOKEN="sdfsdfsdfljlbjkljlkjsdfoiwje"
  4. #
  5. #RAGE4_USERNAME="xxxx@sss.com"
  6. RAGE4_Api="https://rage4.com/rapi/"
  7. ######## Public functions #####################
  8. #Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  9. dns_rage4_add() {
  10. fulldomain=$1
  11. txtvalue=$2
  12. unquotedtxtvalue=$(echo "$txtvalue" | tr -d \")
  13. RAGE4_USERNAME="${RAGE4_USERNAME:-$(_readaccountconf_mutable RAGE4_USERNAME)}"
  14. RAGE4_TOKEN="${RAGE4_TOKEN:-$(_readaccountconf_mutable RAGE4_TOKEN)}"
  15. if [ -z "$RAGE4_USERNAME" ] || [ -z "$RAGE4_TOKEN" ]; then
  16. RAGE4_USERNAME=""
  17. RAGE4_TOKEN=""
  18. _err "You didn't specify a Rage4 api token and username yet."
  19. return 1
  20. fi
  21. #save the api key and email to the account conf file.
  22. _saveaccountconf_mutable RAGE4_USERNAME "$RAGE4_USERNAME"
  23. _saveaccountconf_mutable RAGE4_TOKEN "$RAGE4_TOKEN"
  24. _debug "First detect the root zone"
  25. if ! _get_root "$fulldomain"; then
  26. _err "invalid domain"
  27. return 1
  28. fi
  29. _debug _domain_id "$_domain_id"
  30. _rage4_rest "createrecord/?id=$_domain_id&name=$fulldomain&content=$unquotedtxtvalue&type=TXT&active=true&ttl=1"
  31. return 0
  32. }
  33. #fulldomain txtvalue
  34. dns_rage4_rm() {
  35. fulldomain=$1
  36. txtvalue=$2
  37. RAGE4_USERNAME="${RAGE4_USERNAME:-$(_readaccountconf_mutable RAGE4_USERNAME)}"
  38. RAGE4_TOKEN="${RAGE4_TOKEN:-$(_readaccountconf_mutable RAGE4_TOKEN)}"
  39. _debug "First detect the root zone"
  40. if ! _get_root "$fulldomain"; then
  41. _err "invalid domain"
  42. return 1
  43. fi
  44. _debug _domain_id "$_domain_id"
  45. _debug "Getting txt records"
  46. _rage4_rest "getrecords/?id=${_domain_id}"
  47. _record_id=$(echo "$response" | sed -rn 's/.*"id":([[:digit:]]+)[^\}]*'"$txtvalue"'.*/\1/p')
  48. _rage4_rest "deleterecord/?id=${_record_id}"
  49. return 0
  50. }
  51. #################### Private functions below ##################################
  52. #_acme-challenge.www.domain.com
  53. #returns
  54. # _domain=domain.com
  55. # _domain_id=sdjkglgdfewsdfg
  56. _get_root() {
  57. domain=$1
  58. if ! _rage4_rest "getdomains"; then
  59. return 1
  60. fi
  61. _debug _get_root_domain "$domain"
  62. for line in $(echo "$response" | tr '}' '\n'); do
  63. __domain=$(echo "$line" | sed -rn 's/.*"name":"([^"]*)",.*/\1/p')
  64. __domain_id=$(echo "$line" | sed -rn 's/.*"id":([^,]*),.*/\1/p')
  65. if [ "$domain" != "${domain%"$__domain"*}" ]; then
  66. _domain_id="$__domain_id"
  67. break
  68. fi
  69. done
  70. if [ -z "$_domain_id" ]; then
  71. return 1
  72. fi
  73. return 0
  74. }
  75. _rage4_rest() {
  76. ep="$1"
  77. _debug "$ep"
  78. username_trimmed=$(echo "$RAGE4_USERNAME" | tr -d '"')
  79. token_trimmed=$(echo "$RAGE4_TOKEN" | tr -d '"')
  80. auth=$(printf '%s:%s' "$username_trimmed" "$token_trimmed" | _base64)
  81. export _H1="Content-Type: application/json"
  82. export _H2="Authorization: Basic $auth"
  83. response="$(_get "$RAGE4_Api$ep")"
  84. if [ "$?" != "0" ]; then
  85. _err "error $ep"
  86. return 1
  87. fi
  88. _debug2 response "$response"
  89. return 0
  90. }