-
Notifications
You must be signed in to change notification settings - Fork 14
/
run
executable file
·299 lines (254 loc) · 7.06 KB
/
run
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
#!/bin/bash
# <command> <stub(file)> <conf> <ca> <curpath> <stub(printable name)> <timeout>
#init-------------------------------------------------------------
curpath=$0
if [[ ! $curpath == \/* ]]; then
curpath=${curpath#./} ##remove ./
curpath=`pwd`/$curpath ##add .../..
fi
if [[ $curpath =~ "/" ]]; then
curpath=${curpath%/*} ##remove /run..
else
curpath=""
fi
command=$1
#execcommand='exec'
if [[ $command == "_" ]]; then
command=""
extracommand='./'
fi
stubspath=$8
if [[ ! $stubspath ]]; then
stubspath=$curpath"/../../../../stubs"
fi
filename=$2
if [[ ! $filename =~ '/' ]]; then
if [[ $filename =~ ":" ]]; then
path=$stubspath/${filename%:*}
cd $path
filename=${filename##*:}
else
cd $stubspath/$filename
filename=`find -name "[Rr]un*"`' Run run'
fi
filename=${filename##*/}
fi
status=""
for f in $filename
do
#echo `pwd` - $command $extracommand''$f google.com 443
valid=`$command $extracommand''$f google.com 443`
if [[ $(echo $valid | grep "REJECT") || $(echo $valid | grep "ACCEPT") || $(echo $valid | grep "UNSUPPORTED") ]]; then
filename=$f #this is propably the correct file, if that is not the case, well then you will see some errors
status="ok"
break
fi
done
if [[ ! $status ]]; then #no correct file found or might be wrong command?
exit 1
fi
confpath=$3
if [[ ! $confpath =~ '/' ]]; then
confpath=$curpath"/conf/"$confpath
fi
certspath=$4
FIS=IFS; IFS=+ read certspath cabundle <<< $certspath; IFS=FIS
if [[ "$certspath" ]]; then
if [[ "$certspath" != '_' ]]; then
if [[ $certspath == "--" ]]; then
certspath=$curpath/certs
else
certspath=${certspath//[[:blank:]]/}
fi
else
certspath=""
fi
if [[ "$5" ]]; then
if [[ $5 != "_" ]]; then
curpath=$5
fi
fi
fi
stub=$6
if [[ ! $stub ]]; then
stub="${filename##*\/}"
fi
filepath=${curpath%/*}
timeout=$7
if [[ ! $timeout ]]; then
timeout=15
fi
#read info
infopath=$curpath/info
if [[ -f "$infopath/info" ]]; then
info=`cat $infopath/info`
parallel=`echo $info | grep 'parallel'`
printerror=`echo $info | grep 'print-error'`
printcontext=`echo $info | grep 'print-context'`
fi
#functions-------------------------------------------------------------
#echo timeout: $timeout
checkreturnvalue()
{
if [[ $1 ]]; then
if [[ $1 == "BREAK" ]]; then
return 1
fi
fi
return 0
}
handleresponse()
{
#echo "hello world"
start=0
process=$1
while [[ $process ]]; do
if ps -p $process >> /dev/null 2>&1
then
(( start++ ))
sleep 1
if [[ $start -gt $timeout ]]; then
kill $process >> /dev/null 2>&1
echo "TIMED OUT"
return
fi
else
return
fi
done
}
connect()
{
port=$1
msg=$2
expected=$3
crt=$4
hostname=$5
extra=$6
returnvalue=0
if [[ "$crt" == "certs/test.crt" ]]; then
response=`$command $extracommand''$filename $hostname $port $curpath/$crt 2>&1 & handleresponse $!`
elif [[ "$crt" == "_" ]]; then
if [[ $cabundle ]]; then
response=`$command $extracommand''$filename $hostname $port $cabundle 2>&1 & handleresponse $!`
elif [[ "$certspath" =~ "." ]]; then
response=`$command $extracommand''$filename $hostname $port $certspath 2>&1 & handleresponse $!`
else
response=`$command $extracommand''$filename $hostname $port 2>&1 & handleresponse $!`
fi
elif [[ "$certspath" ]]; then
if [[ "$certspath" != "-k" ]]; then
#echo $command $filename $hostname $port "$certspath"/"$crt"
response=`$command $extracommand''$filename $hostname $port $certspath/$crt 2>&1 & handleresponse $!`
else
response=`$command $extracommand''$filename $hostname $port $certspath 2>&1 & handleresponse $!`
fi
else
response=`$command $extracommand''$filename $hostname $port 2>&1 & handleresponse $!`
fi
if [[ $printcontext ]]; then
context="`echo -e $response | sed -e '$d'`"
fi
if [[ $printerror ]]; then
error=$response
fi
response=`echo -e $response | sed -e '$!d'`
#choose color
if [[ "$response" == "UNSUPPORTED" ]]; then
color='\e[0;37m' #allright
response='[ OK? ][ UNSUPPORTED ]'
#msg=$filename
elif [[ "$response" == "TIMED OUT" ]]; then
color='\e[0;40m' #most likely the connection was succesfull, but can be sure
response="[ ][ TIMED OUT ]"
`checkreturnvalue $extra`; returnvalue=$?
elif [[ "$expected" == "REJECT" ]]; then
if [[ "$response" == "REJECT" ]]; then
color='\e[0;32m' #green, success
response='[ PASS ]'[$response]
else
color='\e[0;41m' #red, failure(either error or verify did succeed)
if [[ "$response" == "ACCEPT" ]]; then
response='[ FAIL ]'[$response]
`checkreturnvalue $extra`; returnvalue=$?
else
response='[ ERROR][------]'
if [[ $printerror ]]; then
context=$error
fi
`checkreturnvalue $extra`; returnvalue=$?
fi
fi
elif [[ "$expected" == "ACCEPT" ]]; then
if [[ "$response" == "ACCEPT" ]]; then
color='\e[0;36m' #green, success
response="[ PASS ][$response]"
else
color='\e[0;41m' #red, failure(either error or verify did not succeed)
if [[ "$response" == "REJECT" ]]; then
response="[ FAIL ][$response]"
`checkreturnvalue $extra`; returnvalue=$?
else
response='[ ERROR][------]'
if [[ $printerror ]]; then
context=$error
fi
`checkreturnvalue $extra`; returnvalue=$?
fi
fi
else
color='\e[0;37m' #allright
if [[ "$response" == "ACCEPT" || "$response" == "REJECT" ]]; then
response="[ OK? ][$response]"
else
response='[ ERROR][------]'
if [[ $printerror ]]; then
context=$error
fi
`checkreturnvalue $extra`; returnvalue=$?
fi
fi
if [[ $returnvalue != 0 ]]; then
response=$response"[-> SKIP $hostname (till CONTINUE) ]"
msg=""
fi
if [[ $printcontext && $context ]]; then
echo -e "$color[$stub]$response[$msg]\e[0m[$hostname]\n"$context
else
echo -e "$color[$stub]$response[$msg]\e[0m[$hostname]"
fi
return $returnvalue
}
#main-------------------------------------------------------------
FIS=$IFS
IFS='&'
trytoconnect=true
while read -r port msg expected crt hostname extra; do
port=${port//[[:blank:]]/}
#msg=${msg//[[:blank:]]/}
expected=${expected//[[:blank:]]/}
crt=${crt//[[:blank:]]/}
hostname=${hostname//[[:blank:]]/}
extra=${extra//[[:blank:]]/}
#echo "port: " .$port.
#echo "msg: " .$msg.
#echo "crt: " .$crt.
#echo "expected: " .$expected.
#echo "hostname: " .$hostname.
#echo "extra:"$extra"-"
#echo $filename
#echo $confpath
#echo $certspath
#try to connect
if [ $trytoconnect = true ]; then
if [[ $parallel && $extra != "BREAK" ]]; then
connect $port $msg $expected $crt $hostname &
else
connect $port $msg $expected $crt $hostname $extra || trytoconnect=false #should i skip the rest of this backend?
fi
elif [[ $extra == "CONTINUE" ]]; then #resume control
trytoconnect=true
fi
done < "$confpath"
IFS=$FIS
exit 0