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.

171 lines
4.8 KiB

  1. #!/usr/bin/env sh
  2. #
  3. #PORKBUN_API_KEY="pk1_0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
  4. #PORKBUN_SECRET_API_KEY="sk1_0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
  5. PORKBUN_Api="https://porkbun.com/api/json/v3"
  6. ######## Public functions #####################
  7. #Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  8. dns_porkbun_add() {
  9. fulldomain=$1
  10. txtvalue=$2
  11. PORKBUN_API_KEY="${PORKBUN_API_KEY:-$(_readaccountconf_mutable PORKBUN_API_KEY)}"
  12. PORKBUN_SECRET_API_KEY="${PORKBUN_SECRET_API_KEY:-$(_readaccountconf_mutable PORKBUN_SECRET_API_KEY)}"
  13. if [ -z "$PORKBUN_API_KEY" ] || [ -z "$PORKBUN_SECRET_API_KEY" ]; then
  14. PORKBUN_API_KEY=''
  15. PORKBUN_SECRET_API_KEY=''
  16. _err "You didn't specify a Porkbun api key and secret api key yet."
  17. _err "You can get yours from here https://porkbun.com/account/api."
  18. return 1
  19. fi
  20. #save the credentials to the account conf file.
  21. _saveaccountconf_mutable PORKBUN_API_KEY "$PORKBUN_API_KEY"
  22. _saveaccountconf_mutable PORKBUN_SECRET_API_KEY "$PORKBUN_SECRET_API_KEY"
  23. _debug 'First detect the root zone'
  24. if ! _get_root "$fulldomain"; then
  25. return 1
  26. fi
  27. _debug _sub_domain "$_sub_domain"
  28. _debug _domain "$_domain"
  29. _debug "Getting txt records"
  30. _porkbun_rest POST "dns/retrieve/$_domain"
  31. if ! echo "$response" | tr -d " " | grep '\"status\":"SUCCESS"' >/dev/null; then
  32. _err "Error $response"
  33. return 1
  34. fi
  35. # For wildcard cert, the main root domain and the wildcard domain have the same txt subdomain name, so
  36. # we can not use updating anymore.
  37. # count=$(printf "%s\n" "$response" | _egrep_o "\"count\":[^,]*" | cut -d : -f 2)
  38. # _debug count "$count"
  39. # if [ "$count" = "0" ]; then
  40. _info "Adding record"
  41. if _porkbun_rest POST "dns/create/$_domain" "{\"name\":\"$_sub_domain\",\"type\":\"TXT\",\"content\":\"$txtvalue\",\"ttl\":120}"; then
  42. if _contains "$response" '\"status\":"SUCCESS"'; then
  43. _info "Added, OK"
  44. return 0
  45. elif _contains "$response" "The record already exists"; then
  46. _info "Already exists, OK"
  47. return 0
  48. else
  49. _err "Add txt record error. ($response)"
  50. return 1
  51. fi
  52. fi
  53. _err "Add txt record error."
  54. return 1
  55. }
  56. #fulldomain txtvalue
  57. dns_porkbun_rm() {
  58. fulldomain=$1
  59. txtvalue=$2
  60. PORKBUN_API_KEY="${PORKBUN_API_KEY:-$(_readaccountconf_mutable PORKBUN_API_KEY)}"
  61. PORKBUN_SECRET_API_KEY="${PORKBUN_SECRET_API_KEY:-$(_readaccountconf_mutable PORKBUN_SECRET_API_KEY)}"
  62. _debug 'First detect the root zone'
  63. if ! _get_root "$fulldomain"; then
  64. return 1
  65. fi
  66. _debug _sub_domain "$_sub_domain"
  67. _debug _domain "$_domain"
  68. _debug "Getting txt records"
  69. _porkbun_rest POST "dns/retrieve/$_domain"
  70. if ! echo "$response" | tr -d " " | grep '\"status\":"SUCCESS"' >/dev/null; then
  71. _err "Error: $response"
  72. return 1
  73. fi
  74. count=$(echo "$response" | _egrep_o "\"count\": *[^,]*" | cut -d : -f 2 | tr -d " ")
  75. _debug count "$count"
  76. if [ "$count" = "0" ]; then
  77. _info "Don't need to remove."
  78. else
  79. record_id=$(echo "$response" | tr '{' '\n' | grep "$txtvalue" | cut -d, -f1 | cut -d: -f2 | tr -d \")
  80. _debug "record_id" "$record_id"
  81. if [ -z "$record_id" ]; then
  82. _err "Can not get record id to remove."
  83. return 1
  84. fi
  85. if ! _porkbun_rest POST "dns/delete/$_domain/$record_id"; then
  86. _err "Delete record error."
  87. return 1
  88. fi
  89. echo "$response" | tr -d " " | grep '\"status\":"SUCCESS"' >/dev/null
  90. fi
  91. }
  92. #################### Private functions below ##################################
  93. #_acme-challenge.www.domain.com
  94. #returns
  95. # _sub_domain=_acme-challenge.www
  96. # _domain=domain.com
  97. _get_root() {
  98. domain=$1
  99. i=1
  100. while true; do
  101. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  102. _debug h "$h"
  103. if [ -z "$h" ]; then
  104. return 1
  105. fi
  106. if _porkbun_rest POST "dns/retrieve/$h"; then
  107. if _contains "$response" "\"status\":\"SUCCESS\""; then
  108. _sub_domain="$(echo "$fulldomain" | sed "s/\\.$_domain\$//")"
  109. _domain=$h
  110. return 0
  111. else
  112. _debug "Go to next level of $_domain"
  113. fi
  114. else
  115. _debug "Go to next level of $_domain"
  116. fi
  117. i=$(_math "$i" + 1)
  118. done
  119. return 1
  120. }
  121. _porkbun_rest() {
  122. m=$1
  123. ep="$2"
  124. data="$3"
  125. _debug "$ep"
  126. api_key_trimmed=$(echo "$PORKBUN_API_KEY" | tr -d '"')
  127. secret_api_key_trimmed=$(echo "$PORKBUN_SECRET_API_KEY" | tr -d '"')
  128. test -z "$data" && data="{" || data="$(echo $data | cut -d'}' -f1),"
  129. data="$data\"apikey\":\"$api_key_trimmed\",\"secretapikey\":\"$secret_api_key_trimmed\"}"
  130. export _H1="Content-Type: application/json"
  131. if [ "$m" != "GET" ]; then
  132. _debug data "$data"
  133. response="$(_post "$data" "$PORKBUN_Api/$ep" "" "$m")"
  134. else
  135. response="$(_get "$PORKBUN_Api/$ep")"
  136. fi
  137. if [ "$?" != "0" ]; then
  138. _err "error $ep"
  139. return 1
  140. fi
  141. _debug2 response "$response"
  142. return 0
  143. }