-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdnsme_sh.functions
266 lines (230 loc) · 6.56 KB
/
dnsme_sh.functions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#!/bin/bash
function dnsme_api () {
local api_url=https://api.dnsmadeeasy.com/V2.0
[[ x$sandbox == x1 ]] && api_url=https://api.sandbox.dnsmadeeasy.com/V2.0
dnsme_throttle # Checking that the request rate is not exceeded
request_date=$(
{ echo -n "$(date -u -R) " ; date -u +%Z ; } | cut -s -d' ' -f1-5,7
)
hmac=$(echo -n "$request_date" | openssl sha1 -hmac "$dnsme_secret_key" | sed 's/.*= //g')
# http headers
api_key_H="x-dnsme-apiKey:$dnsme_api_key"
hmac_H="x-dnsme-hmac:$hmac"
req_date_H="x-dnsme-requestDate:$request_date"
content_type_H="content-type:application/json"
accept_type_H="accept:application/json"
local url=$1
shift
url=${url#/} # Removing leading / from a partial url, if the user accidentally spelled it as /dns/managed
url=${url#*dnsmadeeasy.com/V2.0/} # backward compatibility for calls that use full url
url=$api_url/$url
local src args msgs
args=$(echo $url $*)
api_history=$(api_history)
res=$(curl -k -s -S -H "$api_key_H" -H "$hmac_H" -H "$req_date_H" -H "$content_type_H" -H "$accept_type_H" $url "$@" 2>&1 )
res_code=$?
if ! [[ x$res_code == x0 ]] ; then
res_code=$(( $? + 10 ))
echo -e " res code: $res_code \n src: $api_history \n args: $args \n msgs: Curl failed: $res" | error_red
return $res_code
fi
if [[ -z $res ]] ; then
res=$(echo -e " src: $src \n msgs: Sucess. No curl output" )
return $res_code
fi
# For some reason dnsme returns errors as jq filter instead of json
if jq -n -e "$(echo $res | cut -c 1-333) | .error" >/dev/zero 2>&1 ; then
local logic='
Reconstructing error can save you a lot of time troubleshooting'
res_code=1
echo -e " res code: $res_code \n src: $api_history \n args: $args \n msgs: $res" | error_red
return $res_code
fi
if ! echo $res | is_json ; then
res_code=2
echo -e "res_code: $res_code \n src: $api_history \n args: $args \n msgs: Request failed. Response is not json" | error_red
echo $res |
if echo $res | fgrep -q '</html>' ; then
lynx -dump --stdin 1>&2
else
cat
fi | error_red
return $res_code
fi
} # end function
function dnsme_throttle () {
local LIMIT=150 FRAME=5 # Request limit is 150 per 5 min
local reqs=$(find $dnsme_work_dir -name req_ts* | sort -r)
if (( $(echo $reqs | wc -w ) < 149 )) ; then
touch $dnsme_work_dir/req_ts.$(date +%s)
return 0
fi
echo $reqs | cut -d \ -f 150- | xargs rm -f
local start=$(echo $reqs | cut -d " " -f 149 | egrep -o [0-9]+$)
local frame_end=$(( start + FRAME*60 ))
if (( frame_end > $(date +%s) )) ; then
local secs_wait=$(( frame_end - $(date +%s) ))
echo -e " src: $(api_history) \n msg: Rate limit exceeded. Waiting $secs_wait seconds \n time: $(date)" | error_red
sleep $secs_wait
fi
touch $dnsme_work_dir/req_ts.$(date +%s)
}
function dnsme_domain_status () {
[[ -n $1 ]] || return 255
local id updated name domain=$1
read id updated name < <(dnsme domain id $domain | egrep -o ^[0-9]+)
if [[ x$id =~ ^x[0-9]+$ ]] ; then
dnsme api dns/managed/$id
fi || return
echo $res | jq .
} # end function
function dnsme_domain_id () {
res=$(cat $dnsme_cache |
jq -e --arg domain $1 '.data[] | select(.name==$domain or (.id|tostring)==$domain)| [.id,.updated,.name]') 2>/dev/zero
res_code=$?
if ! [[ x$res_code == x0 ]] ; then
echo -e " src: $(api_history) \n msgs: Domain ${1:-Null} not found" | error_red
return $res_code
fi
echo $res | json2table
}
function dnsme_purge () {
# purge deleted domains
local domains cached_domains
domains=$( dnsme ls | awk '{print $3}' | sed 1d )
cached_domains=$( find $dnsme_domains_dir/* | egrep -v 'dnsme.domains')
diff -Bb <( echo "$domains") <(echo "$cached_domains") --suppress-common-lines |
egrep '^>' | awk '{print $2}' | xargs -L 1 /bin/rm -fv
}
function dnsme_cfg () {
[[ x$1 == xreset ]] && unset dnsme_api_key dnsme_secret_key
[[ -z $dnsme_api_key ]] && read -p 'API Key: ' dnsme_api_key
[[ -z $dnsme_secret_key ]] && read -s -p 'API Secret Key: ' dnsme_secret_key && echo
} # end function dnsme_configure
function dnsme_url () {
local domain_id=$1
echo https://cp.dnsmadeeasy.com/dns/managed/$domain_id
}
function dnsme_updatedb () {
if [[ -z $1 ]] ; then
if dnsme_api dns/managed ; then
echo $res > $dnsme_cache
dnsme purge
fi
return $res_code
# Will return with error of dnsme_api, if dnsme_api fails
fi
local name updated id
read id updated name < <(dnsme domain id $1)
[[ x$id =~ ^x[0-9]+$ ]] || return 1
local domain_cache=$dnsme_domains_dir/$name
dnsme_api dns/managed/$id/records && echo $res > $domain_cache
}
function dnsme_cat () {
if [[ -z $1 ]] ; then
if ! cat $dnsme_cache | is_json ; then
dnsme updatedb
fi
res=$(<$dnsme_cache)
cat $dnsme_cache
return
fi
local domain=$1 id serial
shift
read id serial domain < <(dnsme domain id $domain)
[[ x$id =~ ^x[0-9]+$ ]] || return 1
local domain_cache=$dnsme_domains_dir/$domain
if ! cat $domain_cache | is_json ; then
dnsme updatedb $domain
fi
res=$(<$domain_cache)
if [[ -z $1 ]] ; then
cat $domain_cache
else
id=$1
cat $domain_cache |
jq -c ".data[] | select(.id==$id)"
fi
}
function dnsme_ll () {
dnsme api dns/template
local templs=$( echo $res |
jq '[ .data[] | {name,id} ]' )
dnsme cat | jq --argjson tmpls "$templs" '
.data[] | .templateId as $tId |
{ id, name,
templateId: ( [ $tmpls| .[] | select(.id==$tId) ] |
if length ==0 then
if $tId == null then
"-"
else
$tId
end
else
.[].name
end )
} ' | json2table
}
function dnsme_ls () {
if [[ -z $1 || x$1 == xdomains ]] ; then
{
toarray id updated name ;
dnsme cat |
jq -r '
.data |
sort_by("name") |
.[] | { id , updated, name }' ;
} |
json2table
return
fi
if [[ x$1 == xtemplates ]] ; then
dnsme api dns/template
echo $res |
jq -c '.data[] | { name, id, domainIds, publicTemplate }' |
json2table
return $res_code
fi
if [[ x$1 == xcache ]] ; then
grep -l 'type' $dnsme_domains_dir/*
return
fi
local domain
domain=$1
shift
dnsme cat $domain |
if [[ -z $1 ]] ; then
jq -r '.data | sort_by(.type) | .[]' | json2table
else
jq -c ".data[] | select(.id==$1) "' | if .name=="" then .name="@" else . end' | json2table
fi
} # end function
function dnsme () {
# export dnsme_debug=off
dnsme_work_dir=/dev/shm/$USER/dnsme
dnsme_domains_dir=/dev/shm/$USER/dnsme/domains
dnsme_cache=$dnsme_domains_dir/dnsme.domains
dnsme_work_dir=$dnsme_work_dir/run
mkdir -p $dnsme_work_dir $dnsme_domains_dir
jq_install
dnsme_cfg
local action=$1
shift
case x$action in
xdomain|xrecord|xcache)
action=${action}_$1
shift
dnsme_$action "$@"
# dnsme ls cache -> dnsme_ls_cache
# dnsme domain rm 12345 -> dnsme_domain_rm 12345
;;
x)
return
;;
*)
dnsme_$action "$@"
# dnsme ls -> dnsme_ls
# dnsme ls 12345 -> dnsme_ls 12345
;;
esac
} # end function dnsme