-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdetectify
More file actions
executable file
·196 lines (183 loc) · 4.27 KB
/
detectify
File metadata and controls
executable file
·196 lines (183 loc) · 4.27 KB
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
#!/bin/bash
set -euo pipefail
die () {
echo >&2 "$@"
exit 1
}
which curl > /dev/null || die 'error: you need curl'
which jq > /dev/null || die 'error: you need jq'
which printf > /dev/null || die 'error: you need printf'
init () {
if [ "${_api_key}" == "" ]; then
die 'error: DETECTIFY_API_KEY env-variable required'
fi
}
_api_key="${DETECTIFY_API_KEY:-}"
_api_header="X-Detectify-Key: ${_api_key}"
_version="1.0"
_command="${1-help}"
_api_url="https://api.detectify.com/rest/v2"
domains_args () {
while [ $# -gt 1 ]; do
case "$2" in
--raw)
_raw="1"
;;
--status)
shift
_status="${2}"
;;
--status=*)
_status="${2#*=}"
;;
*)
;;
esac
shift
done
}
profiles_args () {
while [ $# -gt 1 ]; do
case "$2" in
--raw)
_raw="1"
;;
*)
if [ "${_client}" == "" ]; then
_client="$2"
fi
;;
esac
shift
done
}
status_args () {
while [ $# -gt 1 ]; do
case "$2" in
--raw)
_raw="1"
;;
*)
if [ "${_profile}" == "" ]; then
_profile="$2"
fi
;;
esac
shift
done
}
get_domain_token () {
_token=$(curl -s -H "${_api_header}" "${_api_url}/domains/" | jq -r --arg client "$1" '.[] | select(.name == $client) | .token')
}
get_profile_token () {
_token=$(curl -s -H "${_api_header}" "${_api_url}/profiles/" | jq -r --arg profile "$1" '.[] | select(.name == $profile) | .token')
}
doc () {
printf '%-30s \e[38;5;22m%s\e[m\n' "${1:-}" "${2:-}";
}
case ${_command} in
version)
echo "v${_version}"
;;
help)
echo ''
echo "detectify-cli v${_version}"
echo ''
doc 'help' 'this list of help'
doc 'version' 'show version of detectify-cli'
echo ''
doc '[--raw]' 'show raw JSON response'
echo ''
doc 'domains [--status=*]' 'list all domains, statuses: verified/unverified'
doc 'profiles' 'list all profiles'
doc 'profiles <domain>' 'list profiles for the domain'
doc 'start <profile>' 'start a scan on the profile'
doc 'stop <profile>' 'stop a scan on the profile'
doc 'status <profile>' 'status of the current scan'
echo ''
echo ''
;;
domains)
init
_raw=""
_status=""
domains_args $@
res=$(curl -s -H "${_api_header}" "${_api_url}/domains/")
_jq_params=".name"
if [ "${_raw}" == "1" ]; then
_jq_params="."
fi
if [ "${_status}" != "" ]; then
echo "${res}" | jq -r --arg status "${_status}" '.[] | select(.status==$status)' | jq -r "${_jq_params}"
else
echo "${res}" | jq -r '.[]' | jq -r "${_jq_params}"
fi
;;
profiles)
init
_raw=""
_client=""
_token=""
profiles_args $@
if [ "${_client}" != "" ]; then
get_domain_token "${_client}"
if [ "${_token}" == "" ]; then
die 'error: domain not found'
fi
_token="${_token}/"
fi
res=$(curl -s -H "${_api_header}" "${_api_url}/profiles/${_token}")
_jq_params=".name"
if [ "${_raw}" == "1" ]; then
_jq_params="."
fi
echo "${res}" | jq -r '.[]' | jq -r "${_jq_params}"
;;
status|start|stop)
_command="$1"
init
_raw=""
_profile=""
_token=""
status_args $@
_method="GET"
_get_code="0"
case "${_command}" in
start)
_method="POST"
_get_code="1"
;;
stop)
_method="DELETE"
_get_code="1"
;;
esac
if [ "${_profile}" != "" ]; then
get_profile_token "${_profile}"
if [ "${_token}" == "" ]; then
die 'error: domain not found'
fi
_token="${_token}/"
fi
if [ "${_get_code}" == "1" ]; then
res=$(curl --write-out "%{http_code}\n" --silent --output /dev/null -s -X "${_method}" -H "${_api_header}" "${_api_url}/scans/${_token}")
case "${res}" in
409) die 'error: conflict' ;;
404) die 'error: cannot stop, not started' ;;
423) die 'error: locked. domain not verified' ;;
200|202) echo 'success' ;;
*) die "error: ${res}" ;;
esac
else
_jq_params='. | (.status + ": " + .phase)'
if [ "${_raw}" == "1" ]; then
_jq_params="."
fi
res=$(curl -s -X "${_method}" -H "${_api_header}" "${_api_url}/scans/${_token}" | jq -r "${_jq_params}")
echo "${res}"
fi
;;
*)
die "Unknown command"
;;
esac