]>
Commit | Line | Data |
---|---|---|
8eda5f36 QD |
1 | #!/usr/bin/env sh |
2 | ||
3 | # | |
4 | #PORKBUN_API_KEY="pk1_0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" | |
5 | #PORKBUN_SECRET_API_KEY="sk1_0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" | |
6 | ||
7 | PORKBUN_Api="https://porkbun.com/api/json/v3" | |
8 | ||
9 | ######## Public functions ##################### | |
10 | ||
11 | #Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs" | |
12 | dns_porkbun_add() { | |
13 | fulldomain=$1 | |
14 | txtvalue=$2 | |
15 | ||
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)}" | |
18 | ||
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 | ||
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" | |
30 | ||
31 | _debug 'First detect the root zone' | |
32 | if ! _get_root "$fulldomain"; then | |
33 | return 1 | |
34 | fi | |
35 | _debug _sub_domain "$_sub_domain" | |
36 | _debug _domain "$_domain" | |
37 | ||
8eda5f36 QD |
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 | |
43 | _info "Adding record" | |
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 | |
46 | _info "Added, OK" | |
47 | return 0 | |
48 | elif _contains "$response" "The record already exists"; then | |
49 | _info "Already exists, OK" | |
50 | return 0 | |
51 | else | |
52 | _err "Add txt record error. ($response)" | |
53 | return 1 | |
54 | fi | |
55 | fi | |
56 | _err "Add txt record error." | |
57 | return 1 | |
58 | ||
59 | } | |
60 | ||
61 | #fulldomain txtvalue | |
62 | dns_porkbun_rm() { | |
63 | fulldomain=$1 | |
64 | txtvalue=$2 | |
65 | ||
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)}" | |
68 | ||
69 | _debug 'First detect the root zone' | |
70 | if ! _get_root "$fulldomain"; then | |
71 | return 1 | |
72 | fi | |
73 | _debug _sub_domain "$_sub_domain" | |
74 | _debug _domain "$_domain" | |
75 | ||
8eda5f36 QD |
76 | count=$(echo "$response" | _egrep_o "\"count\": *[^,]*" | cut -d : -f 2 | tr -d " ") |
77 | _debug count "$count" | |
78 | if [ "$count" = "0" ]; then | |
79 | _info "Don't need to remove." | |
80 | else | |
46180435 | 81 | record_id=$(echo "$response" | tr '{' '\n' | grep -- "$txtvalue" | cut -d, -f1 | cut -d: -f2 | tr -d \") |
8eda5f36 QD |
82 | _debug "record_id" "$record_id" |
83 | if [ -z "$record_id" ]; then | |
84 | _err "Can not get record id to remove." | |
85 | return 1 | |
86 | fi | |
87 | if ! _porkbun_rest POST "dns/delete/$_domain/$record_id"; then | |
88 | _err "Delete record error." | |
89 | return 1 | |
90 | fi | |
91 | echo "$response" | tr -d " " | grep '\"status\":"SUCCESS"' >/dev/null | |
92 | fi | |
93 | ||
94 | } | |
95 | ||
96 | #################### Private functions below ################################## | |
97 | #_acme-challenge.www.domain.com | |
98 | #returns | |
99 | # _sub_domain=_acme-challenge.www | |
100 | # _domain=domain.com | |
101 | _get_root() { | |
102 | domain=$1 | |
103 | i=1 | |
104 | while true; do | |
105 | h=$(printf "%s" "$domain" | cut -d . -f $i-100) | |
106 | _debug h "$h" | |
107 | if [ -z "$h" ]; then | |
108 | return 1 | |
109 | fi | |
110 | ||
111 | if _porkbun_rest POST "dns/retrieve/$h"; then | |
112 | if _contains "$response" "\"status\":\"SUCCESS\""; then | |
8eda5f36 | 113 | _domain=$h |
1e5e3353 | 114 | _sub_domain="$(echo "$fulldomain" | sed "s/\\.$_domain\$//")" |
8eda5f36 QD |
115 | return 0 |
116 | else | |
117 | _debug "Go to next level of $_domain" | |
118 | fi | |
119 | else | |
120 | _debug "Go to next level of $_domain" | |
121 | fi | |
122 | i=$(_math "$i" + 1) | |
123 | done | |
124 | ||
125 | return 1 | |
126 | } | |
127 | ||
128 | _porkbun_rest() { | |
129 | m=$1 | |
130 | ep="$2" | |
131 | data="$3" | |
132 | _debug "$ep" | |
133 | ||
134 | api_key_trimmed=$(echo "$PORKBUN_API_KEY" | tr -d '"') | |
135 | secret_api_key_trimmed=$(echo "$PORKBUN_SECRET_API_KEY" | tr -d '"') | |
136 | ||
137 | test -z "$data" && data="{" || data="$(echo $data | cut -d'}' -f1)," | |
138 | data="$data\"apikey\":\"$api_key_trimmed\",\"secretapikey\":\"$secret_api_key_trimmed\"}" | |
139 | ||
140 | export _H1="Content-Type: application/json" | |
141 | ||
142 | if [ "$m" != "GET" ]; then | |
143 | _debug data "$data" | |
144 | response="$(_post "$data" "$PORKBUN_Api/$ep" "" "$m")" | |
145 | else | |
146 | response="$(_get "$PORKBUN_Api/$ep")" | |
147 | fi | |
148 | ||
2e34e11b | 149 | _sleep 3 # prevent rate limit |
150 | ||
8eda5f36 QD |
151 | if [ "$?" != "0" ]; then |
152 | _err "error $ep" | |
153 | return 1 | |
154 | fi | |
155 | _debug2 response "$response" | |
156 | return 0 | |
157 | } |