4 #PORKBUN_API_KEY="pk1_0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
5 #PORKBUN_SECRET_API_KEY="sk1_0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
7 PORKBUN_Api
="https://porkbun.com/api/json/v3"
9 ######## Public functions #####################
11 #Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
16 PORKBUN_API_KEY
="${PORKBUN_API_KEY:-$(_readaccountconf_mutable PORKBUN_API_KEY)}"
17 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
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."
27 #save the credentials to the account conf file.
28 _saveaccountconf_mutable PORKBUN_API_KEY
"$PORKBUN_API_KEY"
29 _saveaccountconf_mutable PORKBUN_SECRET_API_KEY
"$PORKBUN_SECRET_API_KEY"
31 _debug
'First detect the root zone'
32 if ! _get_root
"$fulldomain"; then
35 _debug _sub_domain
"$_sub_domain"
36 _debug _domain
"$_domain"
38 # For wildcard cert, the main root domain and the wildcard domain have the same txt subdomain name, so
39 # we can not use updating anymore.
40 # count=$(printf "%s\n" "$response" | _egrep_o "\"count\":[^,]*" | cut -d : -f 2)
41 # _debug count "$count"
42 # if [ "$count" = "0" ]; then
44 if _porkbun_rest POST
"dns/create/$_domain" "{\"name\":\"$_sub_domain\",\"type\":\"TXT\",\"content\":\"$txtvalue\",\"ttl\":120}"; then
45 if _contains
"$response" '\"status\":"SUCCESS"'; then
48 elif _contains
"$response" "The record already exists"; then
49 _info
"Already exists, OK"
52 _err
"Add txt record error. ($response)"
56 _err
"Add txt record error."
66 PORKBUN_API_KEY
="${PORKBUN_API_KEY:-$(_readaccountconf_mutable PORKBUN_API_KEY)}"
67 PORKBUN_SECRET_API_KEY
="${PORKBUN_SECRET_API_KEY:-$(_readaccountconf_mutable PORKBUN_SECRET_API_KEY)}"
69 _debug
'First detect the root zone'
70 if ! _get_root
"$fulldomain"; then
73 _debug _sub_domain
"$_sub_domain"
74 _debug _domain
"$_domain"
76 count
=$
(echo "$response" | _egrep_o
"\"count\": *[^,]*" | cut
-d : -f 2 |
tr -d " ")
78 if [ "$count" = "0" ]; then
79 _info
"Don't need to remove."
81 record_id
=$
(echo "$response" |
tr '{' '\n' |
grep -- "$txtvalue" | cut
-d, -f1 | cut
-d: -f2 |
tr -d \")
82 _debug
"record_id" "$record_id"
83 if [ -z "$record_id" ]; then
84 _err
"Can not get record id to remove."
87 if ! _porkbun_rest POST
"dns/delete/$_domain/$record_id"; then
88 _err
"Delete record error."
91 echo "$response" |
tr -d " " |
grep '\"status\":"SUCCESS"' >/dev
/null
96 #################### Private functions below ##################################
97 #_acme-challenge.www.domain.com
99 # _sub_domain=_acme-challenge.www
105 h
=$
(printf "%s" "$domain" | cut
-d .
-f $i-100)
111 if _porkbun_rest POST
"dns/retrieve/$h"; then
112 if _contains
"$response" "\"status\":\"SUCCESS\""; then
114 _sub_domain
="$(echo "$fulldomain" | sed "s
/\\.
$_domain\$
//")"
117 _debug
"Go to next level of $_domain"
120 _debug
"Go to next level of $_domain"
134 api_key_trimmed
=$
(echo "$PORKBUN_API_KEY" |
tr -d '"')
135 secret_api_key_trimmed
=$
(echo "$PORKBUN_SECRET_API_KEY" |
tr -d '"')
137 test -z "$data" && data
="{" || data
="$(echo $data | cut -d'}' -f1),"
138 data
="$data\"apikey\":\"$api_key_trimmed\",\"secretapikey\":\"$secret_api_key_trimmed\"}"
140 export _H1
="Content-Type: application/json"
142 if [ "$m" != "GET" ]; then
144 response
="$(_post "$data" "$PORKBUN_Api/$ep" "" "$m")"
146 response
="$(_get "$PORKBUN_Api/$ep")"
149 _sleep
3 # prevent rate limit
151 if [ "$?" != "0" ]; then
155 _debug2 response
"$response"