]> git.proxmox.com Git - mirror_acme.sh.git/blob - dnsapi/dns_vscale.sh
Merge pull request #4658 from Justman10000/master
[mirror_acme.sh.git] / dnsapi / dns_vscale.sh
1 #!/usr/bin/env sh
2
3 #This is the vscale.io api wrapper for acme.sh
4 #
5 #Author: Alex Loban
6 #Report Bugs here: https://github.com/LAV45/acme.sh
7
8 #VSCALE_API_KEY="sdfsdfsdfljlbjkljlkjsdfoiwje"
9 VSCALE_API_URL="https://api.vscale.io/v1"
10
11 ######## Public functions #####################
12
13 #Usage: dns_myapi_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
14 dns_vscale_add() {
15 fulldomain=$1
16 txtvalue=$2
17
18 if [ -z "$VSCALE_API_KEY" ]; then
19 VSCALE_API_KEY=""
20 _err "You didn't specify the VSCALE api key yet."
21 _err "Please create you key and try again."
22 return 1
23 fi
24
25 _saveaccountconf VSCALE_API_KEY "$VSCALE_API_KEY"
26
27 _debug "First detect the root zone"
28 if ! _get_root "$fulldomain"; then
29 _err "invalid domain"
30 return 1
31 fi
32 _debug _domain_id "$_domain_id"
33 _debug _sub_domain "$_sub_domain"
34 _debug _domain "$_domain"
35
36 _vscale_tmpl_json="{\"type\":\"TXT\",\"name\":\"$_sub_domain.$_domain\",\"content\":\"$txtvalue\"}"
37
38 if _vscale_rest POST "domains/$_domain_id/records/" "$_vscale_tmpl_json"; then
39 response=$(printf "%s\n" "$response" | _egrep_o "{\"error\": \".+\"" | cut -d : -f 2)
40 if [ -z "$response" ]; then
41 _info "txt record updated success."
42 return 0
43 fi
44 fi
45
46 return 1
47 }
48
49 #fulldomain txtvalue
50 dns_vscale_rm() {
51 fulldomain=$1
52 txtvalue=$2
53
54 _debug "First detect the root zone"
55 if ! _get_root "$fulldomain"; then
56 _err "invalid domain"
57 return 1
58 fi
59 _debug _domain_id "$_domain_id"
60 _debug _sub_domain "$_sub_domain"
61 _debug _domain "$_domain"
62
63 _debug "Getting txt records"
64 _vscale_rest GET "domains/$_domain_id/records/"
65
66 if [ -n "$response" ]; then
67 record_id=$(printf "%s\n" "$response" | _egrep_o "\"TXT\", \"id\": [0-9]+, \"name\": \"$_sub_domain.$_domain\"" | cut -d : -f 2 | tr -d ", \"name\"")
68 _debug record_id "$record_id"
69 if [ -z "$record_id" ]; then
70 _err "Can not get record id to remove."
71 return 1
72 fi
73 if _vscale_rest DELETE "domains/$_domain_id/records/$record_id" && [ -z "$response" ]; then
74 _info "txt record deleted success."
75 return 0
76 fi
77 _debug response "$response"
78 return 1
79 fi
80
81 return 1
82 }
83
84 #################### Private functions below ##################################
85 #_acme-challenge.www.domain.com
86 #returns
87 # _sub_domain=_acme-challenge.www
88 # _domain=domain.com
89 # _domain_id=12345
90 _get_root() {
91 domain=$1
92 i=2
93 p=1
94
95 if _vscale_rest GET "domains/"; then
96 response="$(echo "$response" | tr -d "\n" | sed 's/{/\n&/g')"
97 while true; do
98 h=$(printf "%s" "$domain" | cut -d . -f $i-100)
99 _debug h "$h"
100 if [ -z "$h" ]; then
101 #not valid
102 return 1
103 fi
104
105 hostedzone="$(echo "$response" | tr "{" "\n" | _egrep_o "\"name\":\s*\"$h\".*}")"
106 if [ "$hostedzone" ]; then
107 _domain_id=$(printf "%s\n" "$hostedzone" | _egrep_o "\"id\":\s*[0-9]+" | _head_n 1 | cut -d : -f 2 | tr -d \ )
108 if [ "$_domain_id" ]; then
109 _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
110 _domain=$h
111 return 0
112 fi
113 return 1
114 fi
115 p=$i
116 i=$(_math "$i" + 1)
117 done
118 fi
119 return 1
120 }
121
122 #method uri qstr data
123 _vscale_rest() {
124 mtd="$1"
125 ep="$2"
126 data="$3"
127
128 _debug mtd "$mtd"
129 _debug ep "$ep"
130
131 export _H1="Accept: application/json"
132 export _H2="Content-Type: application/json"
133 export _H3="X-Token: ${VSCALE_API_KEY}"
134
135 if [ "$mtd" != "GET" ]; then
136 # both POST and DELETE.
137 _debug data "$data"
138 response="$(_post "$data" "$VSCALE_API_URL/$ep" "" "$mtd")"
139 else
140 response="$(_get "$VSCALE_API_URL/$ep")"
141 fi
142
143 if [ "$?" != "0" ]; then
144 _err "error $ep"
145 return 1
146 fi
147 _debug2 response "$response"
148 return 0
149 }