]> git.proxmox.com Git - mirror_acme.sh.git/blob - dnsapi/dns_arvan.sh
Merge pull request #4531 from NCDGHA/bugfix/issue_4530_fix_http_status_503
[mirror_acme.sh.git] / dnsapi / dns_arvan.sh
1 #!/usr/bin/env sh
2
3 # Arvan_Token="Apikey xxxx"
4
5 ARVAN_API_URL="https://napi.arvancloud.ir/cdn/4.0/domains"
6 # Author: Vahid Fardi
7 # Report Bugs here: https://github.com/Neilpang/acme.sh
8 #
9 ######## Public functions #####################
10
11 #Usage: dns_arvan_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
12 dns_arvan_add() {
13 fulldomain=$1
14 txtvalue=$2
15 _info "Using Arvan"
16
17 Arvan_Token="${Arvan_Token:-$(_readaccountconf_mutable Arvan_Token)}"
18
19 if [ -z "$Arvan_Token" ]; then
20 _err "You didn't specify \"Arvan_Token\" token yet."
21 _err "You can get yours from here https://npanel.arvancloud.ir/profile/api-keys"
22 return 1
23 fi
24 #save the api token to the account conf file.
25 _saveaccountconf_mutable Arvan_Token "$Arvan_Token"
26
27 _debug "First detect the root zone"
28 if ! _get_root "$fulldomain"; then
29 _err "invalid domain"
30 return 1
31 fi
32
33 _debug _domain_id "$_domain_id"
34 _debug _sub_domain "$_sub_domain"
35 _debug _domain "$_domain"
36
37 _info "Adding record"
38 if _arvan_rest POST "$_domain/dns-records" "{\"type\":\"TXT\",\"name\":\"$_sub_domain\",\"value\":{\"text\":\"$txtvalue\"},\"ttl\":120}"; then
39 if _contains "$response" "$txtvalue"; then
40 _info "response id is $response"
41 _info "Added, OK"
42 return 0
43 elif _contains "$response" "Record Data is duplicate"; then
44 _info "Already exists, OK"
45 return 0
46 else
47 _err "Add txt record error."
48 return 1
49 fi
50 fi
51 _err "Add txt record error."
52 return 0
53 }
54
55 #Usage: fulldomain txtvalue
56 #Remove the txt record after validation.
57 dns_arvan_rm() {
58 fulldomain=$1
59 txtvalue=$2
60 _info "Using Arvan"
61 _debug fulldomain "$fulldomain"
62 _debug txtvalue "$txtvalue"
63
64 Arvan_Token="${Arvan_Token:-$(_readaccountconf_mutable Arvan_Token)}"
65
66 _debug "First detect the root zone"
67 if ! _get_root "$fulldomain"; then
68 _err "invalid domain"
69 return 1
70 fi
71 _debug _domain_id "$_domain_id"
72 _debug _sub_domain "$_sub_domain"
73 _debug _domain "$_domain"
74
75 _debug "Getting txt records"
76 _arvan_rest GET "${_domain}/dns-records"
77 if ! printf "%s" "$response" | grep \"current_page\":1 >/dev/null; then
78 _err "Error on Arvan Api"
79 _err "Please create a github issue with debbug log"
80 return 1
81 fi
82
83 _record_id=$(echo "$response" | _egrep_o ".\"id\":\"[^\"]*\",\"type\":\"txt\",\"name\":\"_acme-challenge\",\"value\":{\"text\":\"$txtvalue\"}" | cut -d : -f 2 | cut -d , -f 1 | tr -d \")
84 if ! _arvan_rest "DELETE" "${_domain}/dns-records/${_record_id}"; then
85 _err "Error on Arvan Api"
86 return 1
87 fi
88 _debug "$response"
89 _contains "$response" 'dns record deleted'
90 return 0
91 }
92
93 #################### Private functions below ##################################
94
95 #_acme-challenge.www.domain.com
96 #returns
97 # _sub_domain=_acme-challenge.www
98 # _domain=domain.com
99 # _domain_id=sdjkglgdfewsdfg
100 _get_root() {
101 domain=$1
102 i=2
103 p=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 #not valid
109 return 1
110 fi
111
112 if ! _arvan_rest GET "$h"; then
113 return 1
114 fi
115 if _contains "$response" "\"domain\":\"$h\""; then
116 _domain_id=$(echo "$response" | cut -d : -f 3 | cut -d , -f 1 | tr -d \")
117 if [ "$_domain_id" ]; then
118 _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
119 _domain=$h
120 return 0
121 fi
122 return 1
123 fi
124 p=$i
125 i=$(_math "$i" + 1)
126 done
127 return 1
128 }
129
130 _arvan_rest() {
131 mtd="$1"
132 ep="$2"
133 data="$3"
134
135 token_trimmed=$(echo "$Arvan_Token" | tr -d '"')
136 export _H1="Authorization: $token_trimmed"
137
138 if [ "$mtd" = "DELETE" ]; then
139 #DELETE Request shouldn't have Content-Type
140 _debug data "$data"
141 response="$(_post "$data" "$ARVAN_API_URL/$ep" "" "$mtd")"
142 elif [ "$mtd" = "POST" ]; then
143 export _H2="Content-Type: application/json"
144 export _H3="Accept: application/json"
145 _debug data "$data"
146 response="$(_post "$data" "$ARVAN_API_URL/$ep" "" "$mtd")"
147 else
148 response="$(_get "$ARVAN_API_URL/$ep$data")"
149 fi
150 return 0
151 }