]>
Commit | Line | Data |
---|---|---|
475e6e28 NL |
1 | #!/usr/bin/env sh |
2 | ||
3 | #This is the Internet.BS api wrapper for acme.sh | |
4 | # | |
5 | #Author: <alexey@nelexa.ru> Ne-Lexa | |
6 | #Report Bugs here: https://github.com/Ne-Lexa/acme.sh | |
7 | ||
8 | #INTERNETBS_API_KEY="sdfsdfsdfljlbjkljlkjsdfoiwje" | |
9 | #INTERNETBS_API_PASSWORD="sdfsdfsdfljlbjkljlkjsdfoiwje" | |
b7b94e38 | 10 | |
475e6e28 NL |
11 | INTERNETBS_API_URL="https://api.internet.bs" |
12 | ||
13 | ######## Public functions ##################### | |
14 | ||
15 | #Usage: dns_myapi_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs" | |
16 | dns_internetbs_add() { | |
17 | fulldomain=$1 | |
18 | txtvalue=$2 | |
19 | ||
b7b94e38 NL |
20 | INTERNETBS_API_KEY="${INTERNETBS_API_KEY:-$(_readaccountconf_mutable INTERNETBS_API_KEY)}" |
21 | INTERNETBS_API_PASSWORD="${INTERNETBS_API_PASSWORD:-$(_readaccountconf_mutable INTERNETBS_API_PASSWORD)}" | |
22 | ||
475e6e28 NL |
23 | if [ -z "$INTERNETBS_API_KEY" ] || [ -z "$INTERNETBS_API_PASSWORD" ]; then |
24 | INTERNETBS_API_KEY="" | |
25 | INTERNETBS_API_PASSWORD="" | |
26 | _err "You didn't specify the INTERNET.BS api key and password yet." | |
27 | _err "Please create you key and try again." | |
28 | return 1 | |
29 | fi | |
30 | ||
b7b94e38 NL |
31 | _saveaccountconf_mutable INTERNETBS_API_KEY "$INTERNETBS_API_KEY" |
32 | _saveaccountconf_mutable INTERNETBS_API_PASSWORD "$INTERNETBS_API_PASSWORD" | |
475e6e28 NL |
33 | |
34 | _debug "First detect the root zone" | |
35 | if ! _get_root "$fulldomain"; then | |
36 | _err "invalid domain" | |
37 | return 1 | |
38 | fi | |
39 | ||
40 | _debug _sub_domain "$_sub_domain" | |
41 | _debug _domain "$_domain" | |
42 | ||
43 | # https://testapi.internet.bs/Domain/DnsRecord/Add?ApiKey=testapi&Password=testpass&FullRecordName=w3.test-api-domain7.net&Type=CNAME&Value=www.internet.bs%&ResponseFormat=json | |
44 | if _internetbs_rest POST "Domain/DnsRecord/Add" "FullRecordName=${_sub_domain}.${_domain}&Type=TXT&Value=${txtvalue}&ResponseFormat=json"; then | |
45 | if ! _contains "$response" "\"status\":\"SUCCESS\""; then | |
46 | _err "ERROR add TXT record" | |
47 | _err "$response" | |
475e6e28 NL |
48 | return 1 |
49 | fi | |
50 | ||
51 | _info "txt record add success." | |
52 | return 0 | |
53 | fi | |
54 | ||
55 | return 1 | |
56 | } | |
57 | ||
58 | #Usage: fulldomain txtvalue | |
59 | #Remove the txt record after validation. | |
60 | dns_internetbs_rm() { | |
61 | fulldomain=$1 | |
62 | txtvalue=$2 | |
63 | ||
b7b94e38 NL |
64 | INTERNETBS_API_KEY="${INTERNETBS_API_KEY:-$(_readaccountconf_mutable INTERNETBS_API_KEY)}" |
65 | INTERNETBS_API_PASSWORD="${INTERNETBS_API_PASSWORD:-$(_readaccountconf_mutable INTERNETBS_API_PASSWORD)}" | |
66 | ||
67 | if [ -z "$INTERNETBS_API_KEY" ] || [ -z "$INTERNETBS_API_PASSWORD" ]; then | |
68 | INTERNETBS_API_KEY="" | |
69 | INTERNETBS_API_PASSWORD="" | |
70 | _err "You didn't specify the INTERNET.BS api key and password yet." | |
71 | _err "Please create you key and try again." | |
72 | return 1 | |
73 | fi | |
74 | ||
475e6e28 NL |
75 | _debug "First detect the root zone" |
76 | if ! _get_root "$fulldomain"; then | |
77 | _err "invalid domain" | |
78 | return 1 | |
79 | fi | |
80 | ||
81 | _debug _sub_domain "$_sub_domain" | |
82 | _debug _domain "$_domain" | |
83 | ||
84 | _debug "Getting txt records" | |
fdb9d93b | 85 | # https://testapi.internet.bs/Domain/DnsRecord/List?ApiKey=testapi&Password=testpass&Domain=test-api-domain7.net&FilterType=CNAME&ResponseFormat=json |
475e6e28 NL |
86 | _internetbs_rest POST "Domain/DnsRecord/List" "Domain=$_domain&FilterType=TXT&ResponseFormat=json" |
87 | ||
88 | if ! _contains "$response" "\"status\":\"SUCCESS\""; then | |
89 | _err "ERROR list dns records" | |
90 | _err "$response" | |
475e6e28 NL |
91 | return 1 |
92 | fi | |
93 | ||
94 | if _contains "$response" "\name\":\"${_sub_domain}.${_domain}\""; then | |
95 | _info "txt record find." | |
96 | ||
97 | # https://testapi.internet.bs/Domain/DnsRecord/Remove?ApiKey=testapi&Password=testpass&FullRecordName=www.test-api-domain7.net&Type=cname&ResponseFormat=json | |
98 | _internetbs_rest POST "Domain/DnsRecord/Remove" "FullRecordName=${_sub_domain}.${_domain}&Type=TXT&ResponseFormat=json" | |
99 | ||
100 | if ! _contains "$response" "\"status\":\"SUCCESS\""; then | |
101 | _err "ERROR remove dns record" | |
102 | _err "$response" | |
475e6e28 NL |
103 | return 1 |
104 | fi | |
105 | ||
106 | _info "txt record deleted success." | |
107 | return 0 | |
108 | fi | |
109 | ||
110 | return 1 | |
111 | } | |
112 | ||
113 | #################### Private functions below ################################## | |
114 | #_acme-challenge.www.domain.com | |
115 | #returns | |
116 | # _sub_domain=_acme-challenge.www | |
117 | # _domain=domain.com | |
118 | # _domain_id=12345 | |
119 | _get_root() { | |
120 | domain=$1 | |
121 | i=2 | |
122 | p=1 | |
123 | ||
124 | # https://testapi.internet.bs/Domain/List?ApiKey=testapi&Password=testpass&CompactList=yes&ResponseFormat=json | |
125 | if _internetbs_rest POST "Domain/List" "CompactList=yes&ResponseFormat=json"; then | |
126 | ||
127 | if ! _contains "$response" "\"status\":\"SUCCESS\""; then | |
128 | _err "ERROR fetch domain list" | |
129 | _err "$response" | |
475e6e28 NL |
130 | return 1 |
131 | fi | |
132 | ||
133 | while true; do | |
134 | h=$(printf "%s" "$domain" | cut -d . -f ${i}-100) | |
135 | _debug h "$h" | |
136 | if [ -z "$h" ]; then | |
137 | #not valid | |
138 | return 1 | |
139 | fi | |
140 | ||
141 | if _contains "$response" "\"$h\""; then | |
142 | _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-${p}) | |
143 | _domain=${h} | |
144 | return 0 | |
145 | fi | |
146 | ||
a2071998 | 147 | p=${i} |
475e6e28 NL |
148 | i=$(_math "$i" + 1) |
149 | done | |
150 | fi | |
151 | return 1 | |
152 | } | |
153 | ||
154 | #Usage: method URI data | |
155 | _internetbs_rest() { | |
156 | m="$1" | |
157 | ep="$2" | |
158 | data="$3" | |
159 | url="${INTERNETBS_API_URL}/${ep}" | |
160 | ||
161 | _debug url "$url" | |
162 | ||
163 | apiKey="$(printf "%s" "${INTERNETBS_API_KEY}" | _url_encode)" | |
164 | password="$(printf "%s" "${INTERNETBS_API_PASSWORD}" | _url_encode)" | |
165 | ||
166 | if [ "$m" = "GET" ]; then | |
167 | response="$(_get "${url}?ApiKey=${apiKey}&Password=${password}&${data}" | tr -d '\r')" | |
168 | else | |
169 | _debug2 data "$data" | |
170 | response="$(_post "$data" "${url}?ApiKey=${apiKey}&Password=${password}" | tr -d '\r')" | |
171 | fi | |
172 | ||
173 | if [ "$?" != "0" ]; then | |
174 | _err "error $ep" | |
175 | return 1 | |
176 | fi | |
177 | ||
178 | _debug2 response "$response" | |
179 | return 0 | |
180 | } |