]>
Commit | Line | Data |
---|---|---|
fb08b53f AA |
1 | #!/usr/bin/env sh |
2 | ||
3 | # | |
4 | # NEODIGIT_API_TOKEN="jasdfhklsjadhflnhsausdfas" | |
5 | ||
6 | # This is Neodigit.net api wrapper for acme.sh | |
7 | # | |
8 | # Author: Adrian Almenar | |
9 | # Report Bugs here: https://github.com/tecnocratica/acme.sh | |
10 | # | |
11 | NEODIGIT_API_URL="https://api.neodigit.net/v1" | |
12 | # | |
13 | ######## Public functions ##################### | |
14 | ||
15 | # Usage: dns_myapi_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs" | |
16 | dns_neodigit_add() { | |
17 | fulldomain=$1 | |
18 | txtvalue=$2 | |
19 | ||
842f0303 | 20 | NEODIGIT_API_TOKEN="${NEODIGIT_API_TOKEN:-$(_readaccountconf_mutable NEODIGIT_API_TOKEN)}" |
fb08b53f AA |
21 | if [ -z "$NEODIGIT_API_TOKEN" ]; then |
22 | NEODIGIT_API_TOKEN="" | |
23 | _err "You haven't specified a Token api key." | |
24 | _err "Please create the key and try again." | |
25 | return 1 | |
26 | fi | |
27 | ||
28 | #save the api key and email to the account conf file. | |
29 | _saveaccountconf_mutable NEODIGIT_API_TOKEN "$NEODIGIT_API_TOKEN" | |
30 | ||
31 | _debug "First detect the root zone" | |
32 | if ! _get_root "$fulldomain"; then | |
33 | _err "invalid domain" | |
34 | return 1 | |
35 | fi | |
36 | ||
37 | _debug fulldomain "$fulldomain" | |
38 | _debug txtvalue "$txtvalue" | |
39 | _debug domain "$_domain" | |
40 | _debug sub_domain "$_sub_domain" | |
41 | ||
42 | _debug "Getting txt records" | |
43 | _neo_rest GET "dns/zones/${_domain_id}/records?type=TXT&name=$fulldomain" | |
44 | ||
45 | _debug _code "$_code" | |
46 | ||
47 | if [ "$_code" != "200" ]; then | |
48 | _err "error retrieving data!" | |
49 | return 1 | |
50 | fi | |
51 | ||
52 | _debug fulldomain "$fulldomain" | |
53 | _debug txtvalue "$txtvalue" | |
54 | _debug domain "$_domain" | |
55 | _debug sub_domain "$_sub_domain" | |
56 | ||
57 | _info "Adding record" | |
58 | if _neo_rest POST "dns/zones/$_domain_id/records" "{\"record\":{\"type\":\"TXT\",\"name\":\"$_sub_domain\",\"content\":\"$txtvalue\",\"ttl\":60}}"; then | |
59 | if printf -- "%s" "$response" | grep "$_sub_domain" >/dev/null; then | |
60 | _info "Added, OK" | |
61 | return 0 | |
62 | else | |
63 | _err "Add txt record error." | |
64 | return 1 | |
65 | fi | |
66 | fi | |
67 | _err "Add txt record error." | |
68 | return 1 | |
69 | } | |
70 | ||
71 | #fulldomain txtvalue | |
72 | dns_neodigit_rm() { | |
73 | fulldomain=$1 | |
74 | txtvalue=$2 | |
75 | ||
842f0303 | 76 | NEODIGIT_API_TOKEN="${NEODIGIT_API_TOKEN:-$(_readaccountconf_mutable NEODIGIT_API_TOKEN)}" |
fb08b53f AA |
77 | if [ -z "$NEODIGIT_API_TOKEN" ]; then |
78 | NEODIGIT_API_TOKEN="" | |
79 | _err "You haven't specified a Token api key." | |
80 | _err "Please create the key and try again." | |
81 | return 1 | |
82 | fi | |
83 | ||
84 | #save the api key and email to the account conf file. | |
85 | _saveaccountconf_mutable NEODIGIT_API_TOKEN "$NEODIGIT_API_TOKEN" | |
86 | ||
87 | _debug "First detect the root zone" | |
88 | if ! _get_root "$fulldomain"; then | |
89 | _err "invalid domain" | |
90 | return 1 | |
91 | fi | |
92 | ||
93 | _debug _domain_id "$_domain_id" | |
94 | _debug _sub_domain "$_sub_domain" | |
95 | _debug _domain "$_domain" | |
96 | ||
97 | _debug "Getting txt records" | |
98 | _neo_rest GET "dns/zones/${_domain_id}/records?type=TXT&name=$fulldomain&content=$txtvalue" | |
99 | ||
100 | if [ "$_code" != "200" ]; then | |
101 | _err "error retrieving data!" | |
102 | return 1 | |
103 | fi | |
104 | ||
f4ad42bb | 105 | record_id=$(echo "$response" | _egrep_o "\"id\":\s*[0-9]+" | _head_n 1 | cut -d: -f2 | cut -d, -f1) |
fb08b53f AA |
106 | _debug "record_id" "$record_id" |
107 | if [ -z "$record_id" ]; then | |
108 | _err "Can not get record id to remove." | |
109 | return 1 | |
110 | fi | |
111 | if ! _neo_rest DELETE "dns/zones/$_domain_id/records/$record_id"; then | |
112 | _err "Delete record error." | |
113 | return 1 | |
114 | fi | |
115 | ||
116 | } | |
117 | ||
118 | #################### Private functions below ################################## | |
119 | #_acme-challenge.www.domain.com | |
120 | #returns | |
121 | # _sub_domain=_acme-challenge.www | |
122 | # _domain=domain.com | |
123 | # _domain_id=dasfdsafsadg5ythd | |
124 | _get_root() { | |
125 | domain=$1 | |
126 | i=2 | |
127 | p=1 | |
128 | while true; do | |
129 | h=$(printf "%s" "$domain" | cut -d . -f $i-100) | |
130 | _debug h "$h" | |
131 | if [ -z "$h" ]; then | |
132 | #not valid | |
133 | return 1 | |
134 | fi | |
135 | ||
136 | if ! _neo_rest GET "dns/zones?name=$h"; then | |
137 | return 1 | |
138 | fi | |
139 | ||
140 | _debug p "$p" | |
141 | ||
142 | if _contains "$response" "\"name\":\"$h\"" >/dev/null; then | |
f4ad42bb | 143 | _domain_id=$(echo "$response" | _egrep_o "\"id\":\s*[0-9]+" | _head_n 1 | cut -d: -f2 | cut -d, -f1) |
fb08b53f AA |
144 | if [ "$_domain_id" ]; then |
145 | _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p) | |
146 | _domain=$h | |
147 | return 0 | |
148 | fi | |
149 | return 1 | |
150 | fi | |
151 | p=$i | |
152 | i=$(_math "$i" + 1) | |
153 | done | |
154 | return 1 | |
155 | } | |
156 | ||
157 | _neo_rest() { | |
158 | m=$1 | |
159 | ep="$2" | |
160 | data="$3" | |
161 | _debug "$ep" | |
162 | ||
163 | export _H1="X-TCPanel-Token: $NEODIGIT_API_TOKEN" | |
164 | export _H2="Content-Type: application/json" | |
165 | ||
166 | if [ "$m" != "GET" ]; then | |
167 | _debug data "$data" | |
168 | response="$(_post "$data" "$NEODIGIT_API_URL/$ep" "" "$m")" | |
169 | else | |
170 | response="$(_get "$NEODIGIT_API_URL/$ep")" | |
171 | fi | |
172 | ||
173 | _code="$(grep "^HTTP" "$HTTP_HEADER" | _tail_n 1 | cut -d " " -f 2 | tr -d "\\r\\n")" | |
174 | ||
175 | if [ "$?" != "0" ]; then | |
176 | _err "error $ep" | |
177 | return 1 | |
178 | fi | |
179 | _debug2 response "$response" | |
180 | return 0 | |
181 | } |