Skip to content

Commit fdeaf86

Browse files
authored
Merge pull request #6197 from xvybihal/dns_active24v2
dns_active24: rewrite for supporting new v2 API
2 parents 8e032a2 + 8a4b436 commit fdeaf86

File tree

1 file changed

+109
-51
lines changed

1 file changed

+109
-51
lines changed

dnsapi/dns_active24.sh

Lines changed: 109 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
#!/usr/bin/env sh
22
# shellcheck disable=SC2034
3-
dns_active24_info='Active24.com
4-
Site: Active24.com
3+
dns_active24_info='Active24.cz
4+
Site: Active24.cz
55
Docs: github.com/acmesh-official/acme.sh/wiki/dnsapi#dns_active24
66
Options:
7-
ACTIVE24_Token API Token
7+
Active24_ApiKey API Key. Called "Identifier" in the Active24 Admin
8+
Active24_ApiSecret API Secret. Called "Secret key" in the Active24 Admin
89
Issues: github.com/acmesh-official/acme.sh/issues/2059
9-
Author: Milan Pála
1010
'
1111

12-
ACTIVE24_Api="https://api.active24.com"
13-
14-
######## Public functions #####################
12+
Active24_Api="https://rest.active24.cz"
13+
# export Active24_ApiKey=ak48l3h7-ak5d-qn4t-p8gc-b6fs8c3l
14+
# export Active24_ApiSecret=ajvkeo3y82ndsu2smvxy3o36496dcascksldncsq
1515

1616
# Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
1717
# Used to add txt record
@@ -22,15 +22,16 @@ dns_active24_add() {
2222
_active24_init
2323

2424
_info "Adding txt record"
25-
if _active24_rest POST "dns/$_domain/txt/v1" "{\"name\":\"$_sub_domain\",\"text\":\"$txtvalue\",\"ttl\":0}"; then
26-
if _contains "$response" "errors"; then
25+
if _active24_rest POST "/v2/service/$_service_id/dns/record" "{\"type\":\"TXT\",\"name\":\"$_sub_domain\",\"content\":\"$txtvalue\",\"ttl\":300}"; then
26+
if _contains "$response" "error"; then
2727
_err "Add txt record error."
2828
return 1
2929
else
3030
_info "Added, OK"
3131
return 0
3232
fi
3333
fi
34+
3435
_err "Add txt record error."
3536
return 1
3637
}
@@ -44,19 +45,25 @@ dns_active24_rm() {
4445
_active24_init
4546

4647
_debug "Getting txt records"
47-
_active24_rest GET "dns/$_domain/records/v1"
48+
# The API needs to send data in body in order the filter to work
49+
# TODO: web can also add content $txtvalue to filter and then get the id from response
50+
_active24_rest GET "/v2/service/$_service_id/dns/record" "{\"page\":1,\"descending\":true,\"sortBy\":\"name\",\"rowsPerPage\":100,\"totalRecords\":0,\"filters\":{\"type\":[\"TXT\"],\"name\":\"${_sub_domain}\"}}"
51+
#_active24_rest GET "/v2/service/$_service_id/dns/record?rowsPerPage=100"
4852

49-
if _contains "$response" "errors"; then
53+
if _contains "$response" "error"; then
5054
_err "Error"
5155
return 1
5256
fi
5357

54-
hash_ids=$(echo "$response" | _egrep_o "[^{]+${txtvalue}[^}]+" | _egrep_o "hashId\":\"[^\"]+" | cut -c10-)
58+
# Note: it might never be more than one record actually, NEEDS more INVESTIGATION
59+
record_ids=$(printf "%s" "$response" | _egrep_o "[^{]+${txtvalue}[^}]+" | _egrep_o '"id" *: *[^,]+' | cut -d ':' -f 2)
60+
_debug2 record_ids "$record_ids"
5561

56-
for hash_id in $hash_ids; do
57-
_debug "Removing hash_id" "$hash_id"
58-
if _active24_rest DELETE "dns/$_domain/$hash_id/v1" ""; then
59-
if _contains "$response" "errors"; then
62+
for redord_id in $record_ids; do
63+
_debug "Removing record_id" "$redord_id"
64+
_debug "txtvalue" "$txtvalue"
65+
if _active24_rest DELETE "/v2/service/$_service_id/dns/record/$redord_id" ""; then
66+
if _contains "$response" "error"; then
6067
_err "Unable to remove txt record."
6168
return 1
6269
else
@@ -70,21 +77,15 @@ dns_active24_rm() {
7077
return 1
7178
}
7279

73-
#################### Private functions below ##################################
74-
#_acme-challenge.www.domain.com
75-
#returns
76-
# _sub_domain=_acme-challenge.www
77-
# _domain=domain.com
78-
# _domain_id=sdjkglgdfewsdfg
7980
_get_root() {
8081
domain=$1
82+
i=1
83+
p=1
8184

82-
if ! _active24_rest GET "dns/domains/v1"; then
85+
if ! _active24_rest GET "/v1/user/self/service"; then
8386
return 1
8487
fi
8588

86-
i=1
87-
p=1
8889
while true; do
8990
h=$(printf "%s" "$domain" | cut -d . -f "$i"-100)
9091
_debug "h" "$h"
@@ -104,45 +105,102 @@ _get_root() {
104105
return 1
105106
}
106107

107-
_active24_rest() {
108-
m=$1
109-
ep="$2"
110-
data="$3"
111-
_debug "$ep"
112-
113-
export _H1="Authorization: Bearer $ACTIVE24_Token"
114-
115-
if [ "$m" != "GET" ]; then
116-
_debug "data" "$data"
117-
response="$(_post "$data" "$ACTIVE24_Api/$ep" "" "$m" "application/json")"
118-
else
119-
response="$(_get "$ACTIVE24_Api/$ep")"
108+
_active24_init() {
109+
Active24_ApiKey="${Active24_ApiKey:-$(_readaccountconf_mutable Active24_ApiKey)}"
110+
Active24_ApiSecret="${Active24_ApiSecret:-$(_readaccountconf_mutable Active24_ApiSecret)}"
111+
#Active24_ServiceId="${Active24_ServiceId:-$(_readaccountconf_mutable Active24_ServiceId)}"
112+
113+
if [ -z "$Active24_ApiKey" ] || [ -z "$Active24_ApiSecret" ]; then
114+
Active24_ApiKey=""
115+
Active24_ApiSecret=""
116+
_err "You don't specify Active24 api key and ApiSecret yet."
117+
_err "Please create your key and try again."
118+
return 1
120119
fi
121120

122-
if [ "$?" != "0" ]; then
123-
_err "error $ep"
121+
#save the credentials to the account conf file.
122+
_saveaccountconf_mutable Active24_ApiKey "$Active24_ApiKey"
123+
_saveaccountconf_mutable Active24_ApiSecret "$Active24_ApiSecret"
124+
125+
_debug "A24 API CHECK"
126+
if ! _active24_rest GET "/v2/check"; then
127+
_err "A24 API check failed with: $response"
124128
return 1
125129
fi
126-
_debug2 response "$response"
127-
return 0
128-
}
129130

130-
_active24_init() {
131-
ACTIVE24_Token="${ACTIVE24_Token:-$(_readaccountconf_mutable ACTIVE24_Token)}"
132-
if [ -z "$ACTIVE24_Token" ]; then
133-
ACTIVE24_Token=""
134-
_err "You didn't specify a Active24 api token yet."
135-
_err "Please create the token and try again."
131+
if ! echo "$response" | tr -d " " | grep \"verified\":true >/dev/null; then
132+
_err "A24 API check failed with: $response"
136133
return 1
137134
fi
138135

139-
_saveaccountconf_mutable ACTIVE24_Token "$ACTIVE24_Token"
140-
141136
_debug "First detect the root zone"
142137
if ! _get_root "$fulldomain"; then
143138
_err "invalid domain"
144139
return 1
145140
fi
141+
146142
_debug _sub_domain "$_sub_domain"
147143
_debug _domain "$_domain"
144+
_active24_get_service_id "$_domain"
145+
_debug _service_id "$_service_id"
146+
}
147+
148+
_active24_get_service_id() {
149+
_d=$1
150+
if ! _active24_rest GET "/v1/user/self/zone/${_d}"; then
151+
return 1
152+
else
153+
response=$(echo "$response" | _json_decode)
154+
_service_id=$(echo "$response" | _egrep_o '"id" *: *[^,]+' | cut -d ':' -f 2)
155+
fi
156+
}
157+
158+
_active24_rest() {
159+
m=$1
160+
ep_qs=$2 # with query string
161+
# ep=$2
162+
ep=$(printf "%s" "$ep_qs" | cut -d '?' -f1) # no query string
163+
data="$3"
164+
165+
_debug "A24 $ep"
166+
_debug "A24 $Active24_ApiKey"
167+
_debug "A24 $Active24_ApiSecret"
168+
169+
timestamp=$(_time)
170+
datez=$(date -u +"%Y%m%dT%H%M%SZ")
171+
canonicalRequest="${m} ${ep} ${timestamp}"
172+
signature=$(printf "%s" "$canonicalRequest" | _hmac sha1 "$(printf "%s" "$Active24_ApiSecret" | _hex_dump | tr -d " ")" hex)
173+
authorization64="$(printf "%s:%s" "$Active24_ApiKey" "$signature" | _base64)"
174+
175+
export _H1="Date: ${datez}"
176+
export _H2="Accept: application/json"
177+
export _H3="Content-Type: application/json"
178+
export _H4="Authorization: Basic ${authorization64}"
179+
180+
_debug2 H1 "$_H1"
181+
_debug2 H2 "$_H2"
182+
_debug2 H3 "$_H3"
183+
_debug2 H4 "$_H4"
184+
185+
# _sleep 1
186+
187+
if [ "$m" != "GET" ]; then
188+
_debug2 "${m} $Active24_Api${ep_qs}"
189+
_debug "data" "$data"
190+
response="$(_post "$data" "$Active24_Api${ep_qs}" "" "$m" "application/json")"
191+
else
192+
if [ -z "$data" ]; then
193+
_debug2 "GET $Active24_Api${ep_qs}"
194+
response="$(_get "$Active24_Api${ep_qs}")"
195+
else
196+
_debug2 "GET $Active24_Api${ep_qs} with data: ${data}"
197+
response="$(_post "$data" "$Active24_Api${ep_qs}" "" "$m" "application/json")"
198+
fi
199+
fi
200+
if [ "$?" != "0" ]; then
201+
_err "error $ep"
202+
return 1
203+
fi
204+
_debug2 response "$response"
205+
return 0
148206
}

0 commit comments

Comments
 (0)