]> git.proxmox.com Git - mirror_acme.sh.git/blob - dnsapi/dns_arvan.sh
Merge pull request #3099 from Alexilmarranen/dev
[mirror_acme.sh.git] / dnsapi / dns_arvan.sh
1 #!/usr/bin/env sh
2
3 #Arvan_Token="xxxx"
4
5 ARVAN_API_URL="https://napi.arvancloud.com/cdn/4.0/domains"
6
7 #Author: Ehsan Aliakbar
8 #Report Bugs here: https://github.com/Neilpang/acme.sh
9 #
10 ######## Public functions #####################
11
12 #Usage: dns_arvan_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
13 dns_arvan_add() {
14 fulldomain=$1
15 txtvalue=$2
16 _info "Using Arvan"
17
18 Arvan_Token="${Arvan_Token:-$(_readaccountconf_mutable Arvan_Token)}"
19
20 if [ -z "$Arvan_Token" ]; then
21 _err "You didn't specify \"Arvan_Token\" token yet."
22 _err "You can get yours from here https://npanel.arvancloud.com/profile/api-keys"
23 return 1
24 fi
25 #save the api token to the account conf file.
26 _saveaccountconf_mutable Arvan_Token "$Arvan_Token"
27
28 _debug "First detect the root zone"
29 if ! _get_root "$fulldomain"; then
30 _err "invalid domain"
31 return 1
32 fi
33
34 _debug _domain_id "$_domain_id"
35 _debug _sub_domain "$_sub_domain"
36 _debug _domain "$_domain"
37
38 _info "Adding record"
39 if _arvan_rest POST "$_domain/dns-records" "{\"type\":\"TXT\",\"name\":\"$_sub_domain\",\"value\":{\"text\":\"$txtvalue\"},\"ttl\":120}"; then
40 if _contains "$response" "$txtvalue"; then
41 _info "Added, OK"
42 return 0
43 elif _contains "$response" "Record Data is Duplicated"; 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 1
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 shorted_txtvalue=$(printf "%s" "$txtvalue" | cut -d "-" -d "_" -f1)
77 _arvan_rest GET "${_domain}/dns-records?search=$shorted_txtvalue"
78
79 if ! printf "%s" "$response" | grep \"current_page\":1 >/dev/null; then
80 _err "Error on Arvan Api"
81 _err "Please create a github issue with debbug log"
82 return 1
83 fi
84
85 count=$(printf "%s\n" "$response" | _egrep_o "\"total\":[^,]*" | cut -d : -f 2)
86 _debug count "$count"
87 if [ "$count" = "0" ]; then
88 _info "Don't need to remove."
89 else
90 record_id=$(printf "%s\n" "$response" | _egrep_o "\"id\":\"[^\"]*\"" | cut -d : -f 2 | tr -d \" | head -n 1)
91 _debug "record_id" "$record_id"
92 if [ -z "$record_id" ]; then
93 _err "Can not get record id to remove."
94 return 1
95 fi
96 if ! _arvan_rest "DELETE" "${_domain}/dns-records/$record_id"; then
97 _err "Delete record error."
98 return 1
99 fi
100 _debug "$response"
101 _contains "$response" 'dns record deleted'
102 fi
103 }
104
105 #################### Private functions below ##################################
106
107 #_acme-challenge.www.domain.com
108 #returns
109 # _sub_domain=_acme-challenge.www
110 # _domain=domain.com
111 # _domain_id=sdjkglgdfewsdfg
112 _get_root() {
113 domain=$1
114 i=1
115 p=1
116 while true; do
117 h=$(printf "%s" "$domain" | cut -d . -f $i-100)
118 _debug h "$h"
119 if [ -z "$h" ]; then
120 #not valid
121 return 1
122 fi
123
124 if ! _arvan_rest GET "?search=$h"; then
125 return 1
126 fi
127
128 if _contains "$response" "\"domain\":\"$h\"" || _contains "$response" '"total":1'; then
129 _domain_id=$(echo "$response" | _egrep_o "\[.\"id\":\"[^\"]*\"" | _head_n 1 | cut -d : -f 2 | tr -d \")
130 if [ "$_domain_id" ]; then
131 _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
132 _domain=$h
133 return 0
134 fi
135 return 1
136 fi
137 p=$i
138 i=$(_math "$i" + 1)
139 done
140 return 1
141 }
142
143 _arvan_rest() {
144 mtd="$1"
145 ep="$2"
146 data="$3"
147
148 token_trimmed=$(echo "$Arvan_Token" | tr -d '"')
149
150 export _H1="Authorization: $token_trimmed"
151
152 if [ "$mtd" = "DELETE" ]; then
153 #DELETE Request shouldn't have Content-Type
154 _debug data "$data"
155 response="$(_post "$data" "$ARVAN_API_URL/$ep" "" "$mtd")"
156 elif [ "$mtd" = "POST" ]; then
157 export _H2="Content-Type: application/json"
158 _debug data "$data"
159 response="$(_post "$data" "$ARVAN_API_URL/$ep" "" "$mtd")"
160 else
161 response="$(_get "$ARVAN_API_URL/$ep$data")"
162 fi
163 }