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.

157 lines
4.5 KiB

4 years ago
  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. # For wildcard cert, the main root domain and the wildcard domain have the same txt subdomain name, so
  30. # we can not use updating anymore.
  31. # count=$(printf "%s\n" "$response" | _egrep_o "\"count\":[^,]*" | cut -d : -f 2)
  32. # _debug count "$count"
  33. # if [ "$count" = "0" ]; then
  34. _info "Adding record"
  35. if _porkbun_rest POST "dns/create/$_domain" "{\"name\":\"$_sub_domain\",\"type\":\"TXT\",\"content\":\"$txtvalue\",\"ttl\":120}"; then
  36. if _contains "$response" '\"status\":"SUCCESS"'; then
  37. _info "Added, OK"
  38. return 0
  39. elif _contains "$response" "The record already exists"; then
  40. _info "Already exists, OK"
  41. return 0
  42. else
  43. _err "Add txt record error. ($response)"
  44. return 1
  45. fi
  46. fi
  47. _err "Add txt record error."
  48. return 1
  49. }
  50. #fulldomain txtvalue
  51. dns_porkbun_rm() {
  52. fulldomain=$1
  53. txtvalue=$2
  54. PORKBUN_API_KEY="${PORKBUN_API_KEY:-$(_readaccountconf_mutable PORKBUN_API_KEY)}"
  55. PORKBUN_SECRET_API_KEY="${PORKBUN_SECRET_API_KEY:-$(_readaccountconf_mutable PORKBUN_SECRET_API_KEY)}"
  56. _debug 'First detect the root zone'
  57. if ! _get_root "$fulldomain"; then
  58. return 1
  59. fi
  60. _debug _sub_domain "$_sub_domain"
  61. _debug _domain "$_domain"
  62. count=$(echo "$response" | _egrep_o "\"count\": *[^,]*" | cut -d : -f 2 | tr -d " ")
  63. _debug count "$count"
  64. if [ "$count" = "0" ]; then
  65. _info "Don't need to remove."
  66. else
  67. record_id=$(echo "$response" | tr '{' '\n' | grep -- "$txtvalue" | cut -d, -f1 | cut -d: -f2 | tr -d \")
  68. _debug "record_id" "$record_id"
  69. if [ -z "$record_id" ]; then
  70. _err "Can not get record id to remove."
  71. return 1
  72. fi
  73. if ! _porkbun_rest POST "dns/delete/$_domain/$record_id"; then
  74. _err "Delete record error."
  75. return 1
  76. fi
  77. echo "$response" | tr -d " " | grep '\"status\":"SUCCESS"' >/dev/null
  78. fi
  79. }
  80. #################### Private functions below ##################################
  81. #_acme-challenge.www.domain.com
  82. #returns
  83. # _sub_domain=_acme-challenge.www
  84. # _domain=domain.com
  85. _get_root() {
  86. domain=$1
  87. i=1
  88. while true; do
  89. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  90. _debug h "$h"
  91. if [ -z "$h" ]; then
  92. return 1
  93. fi
  94. if _porkbun_rest POST "dns/retrieve/$h"; then
  95. if _contains "$response" "\"status\":\"SUCCESS\""; then
  96. _sub_domain="$(echo "$fulldomain" | sed "s/\\.$_domain\$//")"
  97. _domain=$h
  98. return 0
  99. else
  100. _debug "Go to next level of $_domain"
  101. fi
  102. else
  103. _debug "Go to next level of $_domain"
  104. fi
  105. i=$(_math "$i" + 1)
  106. done
  107. return 1
  108. }
  109. _porkbun_rest() {
  110. m=$1
  111. ep="$2"
  112. data="$3"
  113. _debug "$ep"
  114. api_key_trimmed=$(echo "$PORKBUN_API_KEY" | tr -d '"')
  115. secret_api_key_trimmed=$(echo "$PORKBUN_SECRET_API_KEY" | tr -d '"')
  116. test -z "$data" && data="{" || data="$(echo $data | cut -d'}' -f1),"
  117. data="$data\"apikey\":\"$api_key_trimmed\",\"secretapikey\":\"$secret_api_key_trimmed\"}"
  118. export _H1="Content-Type: application/json"
  119. if [ "$m" != "GET" ]; then
  120. _debug data "$data"
  121. response="$(_post "$data" "$PORKBUN_Api/$ep" "" "$m")"
  122. else
  123. response="$(_get "$PORKBUN_Api/$ep")"
  124. fi
  125. _sleep 3 # prevent rate limit
  126. if [ "$?" != "0" ]; then
  127. _err "error $ep"
  128. return 1
  129. fi
  130. _debug2 response "$response"
  131. return 0
  132. }