-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspider.sh
executable file
·493 lines (472 loc) · 14.4 KB
/
spider.sh
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
#!/bin/bash
# -*- coding: UTF8 -*-
# ---------------------------------------------
# @author: Guillaume Seren
# @since: 17/06/2015
# source: https://github.com/GuillaumeSeren/spider
# file: spider.sh
# Licence: GPLv3
# ---------------------------------------------
# TaskList {{{1
# @TODO:Add a urlTested to map all ressources already tested 1 time
# @TODO Add support for login (cookie / session)
# @TODO Add a way to define a route to simulate a user
# @TODO Add stress test option (ab) to simulate a group.
# @TODO Add parallel process for web & prober (perf)
# @TODO Discover recursively all ressources, too slow actually (v8 perf)
# @TODO Generate a dependency graph to represent the webapp (niceToHave)
# @TODO Add a DB to store the graph, and request it (niceToHave)
# @TODO Add a sorting option (by speed, by http code niceToHave)
# Error Codes {{{1
# 0 - Ok
# 1 - Output error, the input did not match the pattern
# 2 - unknown content-type in prober
# 3 - Prober getUrlContentTypeSimple unknown content-type
# 4 - Prober Unknown option in mode
# 5 - Spider filter arg unknown
# 6 - Prober option unknown
# 7 - Error in getOutputConfigured args
# 8 - Error in addUniqInArray
# 9 - Error in deleteInArray
# Default variables {{{1
flagGetOpts=0
args="${*}"
declare -a urlActualRessources
declare -a urlVisited
declare -a urlUnVisited
declare -a urlUnVisitedOutput
# usage() {{{1
# Return the helping message for the use.
function usage()
{
cat << DOC
usage: "$0" options
This script explore a webapp and test ressources state.
OPTIONS:
-h Show this message.
-u url (needed)
-l recursivity level :
n: default 1
0: infinite
-d Define a target domain to focus.
-o Define the output : (comma separated, default add everything)
timer Add the timer in the output
http_code Add the http_code return in the output
url Add the url to the output
! You need at least 1 arg when configuring the output !
-f Filter on content-type, select one of:
html | script | media | style
Sample:
Test a simple url
"$0" -u guillaumeseren.com
Find all linked ressources
"$0" -u guillaumeseren.com -l 0 -d guillaumeseren.com
"$0" -u guillaumeseren.com -o http_code
DOC
}
# GETOPTS {{{1
# Get the param of the script.
while getopts ":u:l:d:o:f:h" OPTION
do
flagGetOpts=1
case $OPTION in
h)
usage
exit 1
;;
u)
cmdUrl="$OPTARG"
;;
l)
cmdLevel="$OPTARG"
;;
d)
cmdDomain="$OPTARG"
;;
o)
cmdOutput="$OPTARG"
# We need to check if there is a , in the string input explode-like
# if yes we split into an array and test each entry for each
# if no there should be only one entry.
if [[ "$cmdOutput" =~ "timer" ]]; then
bOutputTimer=1
fi
if [[ "$cmdOutput" =~ "http_code" ]]; then
bOutputHttp=1
fi
if [[ "$cmdOutput" =~ "url" ]]; then
bOutputUrl=1
fi
# global validation
if [[ "$bOutputTimer" != 1 && "$bOutputHttp" != 1 && "$bOutputUrl" != 1 ]]; then
echo "Output error, the input did not match the pattern"
usage
exit 1
fi
;;
f)
# @FIXME: validate available filter directly in the prober
# @FIXME: Add multiple option instead of just one
if [[ "$OPTARG" != 'html' && "$OPTARG" != 'script' && "$OPTARG" != 'media' && "$OPTARG" != 'style' ]]; then
echo "Spider unknown filter: $OPTARG"
usage
exit 5
fi
cmdFilter="$OPTARG"
;;
?)
echo "commande $1 inconnue"
usage
exit
;;
esac
done
# We check if getopts did not find no any param
if [ "$flagGetOpts" == 0 ]; then
echo 'This script cannot be launched without options.'
usage
exit 1
fi
# function getOutputConfigured() {{{1
function getOutputConfigured() {
local timeTask
local urlProber
local url
# $1 is the timer
# $2 is the probe status
# $3 is the url
# Let's break if either of the arg is not here
if [[ -z "$1" || -z "$2" || -z "$3" ]]; then
echo "Spider getOutputConfigured a arg is missing"
echo "#1: $1"
echo "#2: $2"
echo "#3: $3"
exit 7
fi
if [[ ! -z $bOutputTimer || -z $cmdOutput ]]; then
timeTask="$1:"
fi
if [[ ! -z $bOutputHttp || -z $cmdOutput ]]; then
urlProber="$2:"
fi
if [[ ! -z $bOutputUrl || -z $cmdOutput ]]; then
url="$3"
fi
echo "$timeTask$urlProber$url"
}
# function getDuperNumberInArray() {{{1
function getDupeNumberInArray() {
name=$1[@]
a=("${!name}")
dupeTotal=0
declare -a sortedArray
for ((i=0; i<${#a[@]}; i++));
do
isInArray=0
for u in "${sortedArray[@]}"
do
if [[ "${u}" == ${a[$i]} ]]; then
isInArray=1
fi
done
if [[ "$isInArray" -eq 0 ]]; then
# Each value must be tested only once
sortedArray=(${sortedArray[@]} ${a[$i]})
# echo "sortedArray: ${sortedArray[@]}"
# echo "searching:${i} - ${a[$i]}"
dupeCount=0
for ((j=0; j<${#a[@]}; j++));
do
if [[ "${a[$i]}" == "${a[$j]}" ]]; then
# echo "There is a match on ${a[$i]}"
dupeCount=$((dupeCount+1))
# echo "dupecount: ${dupeCount}"
fi
done
if [[ ${dupeCount} -ge 2 ]]; then
# echo "there is more than 1 dupe"
dupeTotal=$(( dupeTotal + dupeCount -1))
# echo "dupeTotal: ${dupeTotal}"
fi
fi
done
echo "${dupeTotal}"
}
# function setDedupeInArray() {{{1
function setDedupeInArray() {
echo "setDedupeInArray"
name=$1[@]
a=("${!name}")
declare -a outputArray
local i j
for i in "${a[@]}"
do
dupe=0
# echo "testing $i"
for j in "${outputArray[@]}"
do
if [[ "$i" == "$j" ]]; then
# echo "there is a match with $j"
# already in the array
dupe=1
fi
done
if [[ "${dupe}" -eq 0 ]]; then
# Not in the array
# echo "$i not matched adding"
outputArray=(${outputArray[@]} ${i})
# echo "${outputArray[@]}"
fi
done
echo "${outputArray[@]}"
}
# function valueIsInArray() {{{1
function valueIsInArray() {
if [[ -n "$1" || -n "$2" ]]; then
# Do the stuff
name=$1[@]
array=("${!name}")
value="$2"
match=0
for item in "${array[@]}"
do
if [[ "$item" == "$value" ]]; then
match=1
fi
done
echo "${match}"
else
# One of the needed param is missing
# inform the user and die
echo "valueIsInArray: one of the var is empty"
echo "\$1"
fi
}
# function addUniqInArray() {{{1
# # first arg is array name
# # second arg is value
function addUniqInArray() {
# local name value arrayItem valueFound
local value arrayItem valueFound
if [[ -n "$1" && -n "$2" ]]; then
valueFound=0
# Do the stuff
name=$1[@]
array=("${!name}")
# declare -a array=("{!$1}")
value="$2"
for arrayItem in "${array[@]}"
do
if [[ "${arrayItem}" == "${value}" ]]; then
valueFound=1
fi
done
if [[ "${valueFound}" == 0 ]]; then
# Add value to the array
array=(${array[@]} ${value})
fi
echo "${array[@]}"
else
# One of the needed param is missing
# inform the user and die
echo "addUniqInArray: one of the var is empty"
echo "\$1: $1"
echo "\$2: $2"
exit 8
fi
}
# function deleteInArray() {{{1
# # first arg is array name
# # second arg is value
function deleteInArray() {
local name value array
if [[ -n "$1" && -n "$2" ]]; then
name=$1[@]
array=("${!name}")
value="$2"
for ((i=0; i<${#array[@]}; i++));
do
if [[ "${array[$i]}" == "$value" ]]; then
unset array[$i]
fi
done
echo "${array[@]}"
else
# One of the needed param is missing
# inform the user and die
echo "deleteInArray: one of the var is empty"
echo "\$1: $1"
echo "\$2: $2"
exit 9
fi
}
# Function isValueNotInArray() {{{1
function isValueNotInArray() {
local name value array valueInArray
if [[ -n "$1" && -n "$2" ]]; then
name=$1[@]
array=("${!name}")
value="$2"
valueInArray=0
# echo "isValueNotInArray: testing $value"
for ((i=0; i<${#array[@]}; i++));
do
# echo "isValueNotInArray: ${array[$i]} == $value"
if [[ "${array[$i]}" == "$value" ]]; then
valueInArray=1
fi
done
echo "$valueInArray"
else
# One of the needed param is missing
# inform the user and die
echo "isValueNotInArray: one of the var is empty"
echo "\$1: $1"
echo "\$2: $2"
exit 9
fi
}
# function main() {{{1
function main() {
# @FIXME: Maybe export to global variables
local timeBegin
local timeEnd
local timeTask
local urlProber
local urlHttp
# Add cmdUrl to urlUnVisited
urlUnVisited=($(addUniqInArray urlUnVisited "${cmdUrl}"))
# @FIXME Move that message to a debug / verbose mode
echo "call web on: ${args}"
# Now cmdLevel is not given to wget call but the number of loop below
# We need to count the size of array to output to user
urlActualRessources=($(bash ./web.sh -u "$cmdUrl" -l "1" -d "$cmdDomain" ))
# @FIXME: Refactor the duplication test + deduplication into a function
# -> Begin of deduplication {{{2
local dupeRessource
dupeRessource=$(getDupeNumberInArray "urlActualRessources")
# check ressources array for duplicate
if [[ ${dupeRessource} -gt 0 ]]; then
echo "dupe count: ${dupeRessource}"
echo "deduplication"
echo "${urlActualRessources[@]}"
urlActualRessources=($(setDedupeInArray "urlActualRessources"))
dupeRessource=$(getDupeNumberInArray "urlActualRessources")
echo "After dedupeplication count: ${dupeRessource}"
fi
# <- End of test + deduplication 2}}}
# @FIXME: Refactor the ressources probing into a function
# -> Begin ressources probing
# @FIXME Move that message to a debug / verbose mode
echo "testing: ${urlUnVisited[0]}"
echo "Number of ressources of the page: ${#urlActualRessources[@]}"
echo "time:page state:URL"
# This loop call prober (curl on each ressources of urlActualRessources)
for i in "${urlActualRessources[@]}"
do
timeBegin="$(date +%s.%N)"
if [[ -n "$cmdFilter" ]]; then
# get the available content-type
# @FIXME: export in a function
content_type=( $(bash ./prober.sh -l simple) )
urlProber="$(bash ./prober.sh -m 'content-type' -u "$i")"
timeEnd="$(date +%s.%N)"
if [[ "$(valueIsInArray content_type $urlProber)" == 0 ]]; then
echo "Prober unknown content-type of $i"
echo "content-type: ${urlProber}"
exit 3
fi
urlHttp="${urlProber}"
else
urlProber="$(bash ./prober.sh -m 'http' -u "$i")"
timeEnd="$(date +%s.%N)"
# If not test it (one more curl call)
urlHttp="$(bash ./prober.sh -m 'content-type' -u "$i")"
fi
timeTask=$(echo "$timeEnd - $timeBegin" | bc)
timeTask="${timeTask:0:4}"
# Does it need filtering ?
if [[ -n "$cmdFilter" && "$cmdFilter" == "$urlProber" ]]; then
getOutputConfigured "$timeTask" "$urlProber" "$i"
elif [[ -z "$cmdFilter" && "$cmdFilter" == '' ]]; then
getOutputConfigured "$timeTask" "$urlProber" "$i"
fi
# Now if the ressource is a html type add it to unvisited array
if [[ "${urlHttp}" == "html" ]]; then
# if not already in the array we add it
# We need to test that value is not in visited already
if [[ "$(isValueNotInArray urlVisited ${i})" -eq '0' ]]; then
urlUnVisited=($(addUniqInArray urlUnVisited "${i}"))
fi
fi
done
# -> End ressources probing
# Add it to the visited
urlVisited=($(addUniqInArray urlVisited "${cmdUrl}"))
# Delete from the unvisited
urlUnVisited=($(deleteInArray urlUnVisited "${cmdUrl}"))
# END Single pass
# Do we need to continue iterating ?
# basically just compare recursive parm if given with size of visited
while [[ "${cmdLevel}" -gt "${#urlVisited[@]}" || "${cmdLevel}" == 0 ]] && [[ "${#urlUnVisited[@]}" -gt 0 ]];
do
# Diplay size of visited and unvisited to track progress
echo "Visiting the page number ${#urlVisited[@]}"
echo "Remaining page(s) to visit ${#urlUnVisited[@]}"
echo "let's have a look at the next in unVisited"
echo "testing: ${urlUnVisited[0]}"
# 1 check if not already in visited (might be a default)
# 2 send web to get the urlActualRessources array
echo "webing on ${urlUnVisited[0]}"
urlActualRessources=($(bash ./web.sh -u "${urlUnVisited[0]}" -l "1" -d "$cmdDomain" ))
echo "Number of ressources of the page: ${#urlActualRessources[@]}"
# echo "size of urlActualRessources ${#urlActualRessources[@]}"
# 3 loop on the ressources and probe the state
for i in "${urlActualRessources[@]}"
do
timeBegin="$(date +%s.%N)"
if [[ -n "$cmdFilter" ]]; then
# get the available content-type
# @FIXME: export in a function
content_type=( $(bash ./prober.sh -l simple) )
urlProber="$(bash ./prober.sh -m 'content-type' -u "$i")"
timeEnd="$(date +%s.%N)"
if [[ "$(valueIsInArray content_type $urlProber)" == 0 ]]; then
echo "Prober unknown content-type of $i"
echo "content-type: ${urlProber}"
exit 3
fi
urlHttp="${urlProber}"
else
urlProber="$(bash ./prober.sh -m 'http' -u "$i")"
timeEnd="$(date +%s.%N)"
# If not test it (one more curl call)
urlHttp="$(bash ./prober.sh -m 'content-type' -u "$i")"
fi
timeTask=$(echo "$timeEnd - $timeBegin" | bc)
timeTask="${timeTask:0:4}"
# Does it need filtering ?
if [[ -n "$cmdFilter" && "$cmdFilter" == "$urlProber" ]]; then
getOutputConfigured "$timeTask" "$urlProber" "$i"
elif [[ -z "$cmdFilter" && "$cmdFilter" == '' ]]; then
getOutputConfigured "$timeTask" "$urlProber" "$i"
fi
# Now if the ressource is a html type add it to unvisited array
if [[ "${urlHttp}" == "html" ]]; then
# if not already in the array we add it
# We need to test that value is not in visited already
if [[ "$(isValueNotInArray urlVisited ${i})" -eq '0' ]]; then
urlUnVisited=($(addUniqInArray urlUnVisited "${i}"))
fi
fi
done
# 4 display to the user
# 5 Add the html in unVisited if not already in there
# Add it to the visited
urlVisited=($(addUniqInArray urlVisited "${urlUnVisited[0]}"))
# Delete from the unvisited
urlUnVisited=($(deleteInArray urlUnVisited "${urlUnVisited[0]}"))
done
}
main
# vim: set ft=sh ts=2 sw=2 tw=80 foldmethod=marker et :