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.

162 lines
4.5 KiB

1 month ago
4 years ago
1 month ago
  1. #!/usr/bin/env sh
  2. # shellcheck disable=SC2034
  3. dns_porkbun_info='Porkbun.com
  4. Site: Porkbun.com
  5. Docs: github.com/acmesh-official/acme.sh/wiki/dnsapi2#dns_porkbun
  6. Options:
  7. PORKBUN_API_KEY API Key
  8. PORKBUN_SECRET_API_KEY API Secret
  9. Issues: github.com/acmesh-official/acme.sh/issues/3450
  10. '
  11. PORKBUN_Api="https://api.porkbun.com/api/json/v3"
  12. ######## Public functions #####################
  13. #Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  14. dns_porkbun_add() {
  15. fulldomain=$1
  16. txtvalue=$2
  17. PORKBUN_API_KEY="${PORKBUN_API_KEY:-$(_readaccountconf_mutable PORKBUN_API_KEY)}"
  18. PORKBUN_SECRET_API_KEY="${PORKBUN_SECRET_API_KEY:-$(_readaccountconf_mutable PORKBUN_SECRET_API_KEY)}"
  19. if [ -z "$PORKBUN_API_KEY" ] || [ -z "$PORKBUN_SECRET_API_KEY" ]; then
  20. PORKBUN_API_KEY=''
  21. PORKBUN_SECRET_API_KEY=''
  22. _err "You didn't specify a Porkbun api key and secret api key yet."
  23. _err "You can get yours from here https://porkbun.com/account/api."
  24. return 1
  25. fi
  26. #save the credentials to the account conf file.
  27. _saveaccountconf_mutable PORKBUN_API_KEY "$PORKBUN_API_KEY"
  28. _saveaccountconf_mutable PORKBUN_SECRET_API_KEY "$PORKBUN_SECRET_API_KEY"
  29. _debug 'First detect the root zone'
  30. if ! _get_root "$fulldomain"; then
  31. return 1
  32. fi
  33. _debug _sub_domain "$_sub_domain"
  34. _debug _domain "$_domain"
  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. count=$(echo "$response" | _egrep_o "\"count\": *[^,]*" | cut -d : -f 2 | tr -d " ")
  69. _debug count "$count"
  70. if [ "$count" = "0" ]; then
  71. _info "Don't need to remove."
  72. else
  73. record_id=$(echo "$response" | tr '{' '\n' | grep -- "$txtvalue" | cut -d, -f1 | cut -d: -f2 | tr -d \")
  74. _debug "record_id" "$record_id"
  75. if [ -z "$record_id" ]; then
  76. _err "Can not get record id to remove."
  77. return 1
  78. fi
  79. if ! _porkbun_rest POST "dns/delete/$_domain/$record_id"; then
  80. _err "Delete record error."
  81. return 1
  82. fi
  83. echo "$response" | tr -d " " | grep '"status":"SUCCESS"' >/dev/null
  84. fi
  85. }
  86. #################### Private functions below ##################################
  87. #_acme-challenge.www.domain.com
  88. #returns
  89. # _sub_domain=_acme-challenge.www
  90. # _domain=domain.com
  91. _get_root() {
  92. domain=$1
  93. i=1
  94. while true; do
  95. h=$(printf "%s" "$domain" | cut -d . -f "$i"-100)
  96. _debug h "$h"
  97. if [ -z "$h" ]; then
  98. return 1
  99. fi
  100. if _porkbun_rest POST "dns/retrieve/$h"; then
  101. if _contains "$response" "\"status\":\"SUCCESS\""; then
  102. _domain=$h
  103. _sub_domain="$(echo "$fulldomain" | sed "s/\\.$_domain\$//")"
  104. return 0
  105. else
  106. _debug "Go to next level of $_domain"
  107. fi
  108. else
  109. _debug "Go to next level of $_domain"
  110. fi
  111. i=$(_math "$i" + 1)
  112. done
  113. return 1
  114. }
  115. _porkbun_rest() {
  116. m=$1
  117. ep="$2"
  118. data="$3"
  119. _debug "$ep"
  120. api_key_trimmed=$(echo "$PORKBUN_API_KEY" | tr -d '"')
  121. secret_api_key_trimmed=$(echo "$PORKBUN_SECRET_API_KEY" | tr -d '"')
  122. test -z "$data" && data="{" || data="$(echo "$data" | cut -d'}' -f1),"
  123. data="$data\"apikey\":\"$api_key_trimmed\",\"secretapikey\":\"$secret_api_key_trimmed\"}"
  124. export _H1="Content-Type: application/json"
  125. if [ "$m" != "GET" ]; then
  126. _debug data "$data"
  127. response="$(_post "$data" "$PORKBUN_Api/$ep" "" "$m")"
  128. else
  129. response="$(_get "$PORKBUN_Api/$ep")"
  130. fi
  131. _sleep 3 # prevent rate limit
  132. if [ "$?" != "0" ]; then
  133. _err "error $ep"
  134. return 1
  135. fi
  136. _debug2 response "$response"
  137. return 0
  138. }