-
Notifications
You must be signed in to change notification settings - Fork 0
/
freeSS.sh
executable file
·179 lines (165 loc) · 4.18 KB
/
freeSS.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
#!/bin/bash
#Functions
############################
USAGE(){
echo "Usage: $1 [params]"
echo " -b BASE: base dir, where to find configs and functions. default: ./"
echo " -t TIMEOUT: timeout time of check resource. default: 5s"
echo " -c COUNT: parallel count when check resources. default: 50"
echo " -p PORT: local listen port when check resources. default: 20000"
echo " -f FILE: save valid resources to this file."
echo " -M max_number: save up to [max_number] valid resources. default: 0(save all valid resources)"
echo " -P script: script to be executed after success get resource"
echo " -s: do not delete temp files"
echo " -v: more verbose output"
echo " -D: debug mode"
echo " -h: print this"
exit -1
}
function cleanup() {
LOG "Cleanup $WORK_DIR: $DELETE"
[ "$DELETE" != "0" ] && {
[ -d $WORK_DIR ] && rm -rf $WORK_DIR
}
}
SCRIPT=()
MAX=0
############################
while getopts ":b:t:c:P:p:f:M:svD" opt; do
case $opt in
b)
BASE=$OPTARG
;;
t)
TIMEOUT=$OPTARG
;;
c)
COUNT=$OPTARG
;;
p)
PORT=$OPTARG
;;
f)
FILE=$OPTARG
;;
M)
MAX=$OPTARG
;;
P)
SCRIPT+=("$OPTARG")
;;
s)
DELETE=0
#shift 1
;;
v)
LOG_LEVEL=$((LOG_LEVEL+1))
#shift 1
;;
D)
DEBUG=1
DELETE=0
LOG_LEVEL=100
COUNT=1
#shift 1
;;
*)
USAGE $0
;;
esac
done
EXEC=`basename $0`
PORT=${PORT:-20000}
COUNT=${COUNT:-50}
TIMEOUT=${TIMEOUT:-5}
#1: ERR; 2:ERR+WRN; 3:ERR+WRN+LOG
LOG_LEVEL=${LOG_LEVEL:-2}
DEBUG=${DEBUG:-0}
DELETE=${DELETE:-1}
export DEBUG
export DELETE
export LOG_LEVEL
#Dir && files
BASE=${BASE:-./}
SRC_DIR=${BASE}/sources
FUN_DIR=${BASE}/functions
CMD_DIR=${BASE}/commands
CFG_DIR=${BASE}/configs
PROTO_DIR=${BASE}/protocols
export SRC_DIR FUN_DIR CMD_DIR CFG_DIR PROTO_DIR
[ ! -f $FUN_DIR/common.sh ] && {
echo "Invalid setting! file \"$FUN_DIR/common.sh\" not exist"
return 1
}
. $FUN_DIR/common.sh
check_dirs $SRC_DIR $CMD_DIR $PROTO_DIR || ERR "Incomplete dirs"
check_files $CMD_DIR/GetResource.sh $CMD_DIR/ProcResource.sh || ERR "Incomplete scripts"
check_execs curl logger sed awk uniq jq sort wc base64 || ERR "Incomplete executes"
[ -n "$FILE" ] && {
mkdir -p `dirname $FILE` || ERR "Can not mkdir for dist file \"$FILE\""
touch $FILE || ERR "Can not create dist file: $FILE"
}
WORK_DIR=/tmp/$EXEC
SRC_FILE=$WORK_DIR/$$-resource.lst
VALID_FILE=$WORK_DIR/$$-valid.lst
mkdir -p $WORK_DIR
rm -rf $VALID_FILE $SRC_FILE
trap cleanup EXIT
list=`ls $SRC_DIR/*.cfg 2>/dev/null`
[ -z "$list" ] && ERR "Can not find source file @ dir \"./$SRC_DIR\""
childs=()
for src in $list;do
LOG "Processing $src"
$CMD_DIR/GetResource.sh -c "$src" -f "$SRC_FILE" &
childs+=("$!")
done
LOG "Waiting for child(s): ${childs[@]}"
wait_childs ${childs[@]}
LOG "Check if any resource ready"
[ -s ${SRC_FILE} ] || ERR "Fail: no resource find"
LOG "Sort contexts: ${SRC_FILE}"
cat ${SRC_FILE} | sort | uniq > ${SRC_FILE}.tmp
mv ${SRC_FILE}.tmp ${SRC_FILE}
#Check if resource valid
INF "Check resource(${SRC_FILE}). It may take long time"
count=0
port=$PORT
while read line;do
index=`expr $port "-" $PORT`
LOG "$CMD_DIR/ProcResource.sh -c -i $index -l $port -t $TIMEOUT -f $VALID_FILE \"$line\""
$CMD_DIR/ProcResource.sh -c -i $index -l $port -t $TIMEOUT -f $VALID_FILE "$line" &
count=`expr $count "+" 1`
[ "$count" -lt $COUNT ] && {
port=`expr $port "+" 1`
continue
} || {
wait -n
port=$?
port=`expr $PORT "+" $port`
}
done <$SRC_FILE
LOG "wait all jobs finished"
wait
LOG "check valid resource size"
[ -s $VALID_FILE ] || ERR "Fail: no proxy works in ${TIMEOUT}S"
LOG "Sort valid server by responce time"
[ "$MAX" -le 0 ] && MAX=999999 || LOG "Save max $MAX resources"
cat $VALID_FILE | head -n $MAX | sed 's/^[0-9]*\t//' | sort | uniq >$VALID_FILE.tmp
mv $VALID_FILE.tmp $VALID_FILE
LOG "Result file: $VALID_FILE"
[ -n "$FILE" ] && {
LOG "Save result to file: $FILE"
cp -f $VALID_FILE $FILE
}
[ "${#SCRIPT[@]}" -ge 1 ] && {
for script in ${SCRIPT[@]};do
LOG "Run post script: $script"
cmd=""
[ -x $CMD_DIR/$script ] && cmd=$CMD_DIR/$script
[ -x $BASE/$script ] && cmd=$BASE/$script
[ -x $script ] && cmd=$script
[ -z "$cmd" ] && ERR "Can not find script: $script"
$cmd -f $VALID_FILE
done
}
echo "Finished"