forked from deppapparatus/better-cloudflare-ip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cf.sh
381 lines (381 loc) · 11.6 KB
/
cf.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
#!/bin/bash
# better-cloudflare-ip
declare -i bandwidth
declare -i speed
read -p "请设置期望到 CloudFlare 服务器的带宽大小(单位 Mbps):" bandwidth
speed=bandwidth*128*1024
starttime=`date +'%Y-%m-%d %H:%M:%S'`
while true
do
while true
do
declare -i m
declare -i n
declare -i per
rm -rf icmp temp data.txt meta.txt log.txt anycast.txt temp.txt speed.txt
mkdir icmp
while true
do
if [ -f "resolve.txt" ]
then
echo 指向解析获取CF节点IP
resolveip=$(cat resolve.txt)
while true
do
if [ ! -f "meta.txt" ]
then
curl --ipv4 --resolve speed.cloudflare.com:443:$resolveip --retry 3 -s https://speed.cloudflare.com/meta | sed -e 's/{//g' -e 's/}//g' -e 's/"//g' -e 's/,/\n/g'>meta.txt
else
asn=$(cat meta.txt | grep asn: | awk -F: '{print $2}')
city=$(cat meta.txt | grep city: | awk -F: '{print $2}')
latitude=$(cat meta.txt | grep latitude: | awk -F: '{print $2}')
longitude=$(cat meta.txt | grep longitude: | awk -F: '{print $2}')
curl --ipv4 --resolve service.udpfile.com:443:$resolveip --retry 3 "https://service.udpfile.com?asn="$asn"&city="$city"" -o data.txt -#
break
fi
done
else
echo DNS解析获取CF节点IP
while true
do
if [ ! -f "meta.txt" ]
then
curl --ipv4 --retry 3 -s https://speed.cloudflare.com/meta | sed -e 's/{//g' -e 's/}//g' -e 's/"//g' -e 's/,/\n/g'>meta.txt
else
asn=$(cat meta.txt | grep asn: | awk -F: '{print $2}')
city=$(cat meta.txt | grep city: | awk -F: '{print $2}')
latitude=$(cat meta.txt | grep latitude: | awk -F: '{print $2}')
longitude=$(cat meta.txt | grep longitude: | awk -F: '{print $2}')
curl --ipv4 --retry 3 "https://service.udpfile.com?asn="$asn"&city="$city"" -o data.txt -#
break
fi
done
fi
if [ -f "data.txt" ]
then
break
fi
done
domain=$(cat data.txt | grep domain= | cut -f 2- -d'=')
file=$(cat data.txt | grep file= | cut -f 2- -d'=')
url=$(cat data.txt | grep url= | cut -f 2- -d'=')
app=$(cat data.txt | grep app= | cut -f 2- -d'=')
if [ "$app" != "20210903" ]
then
echo 发现新版本程序: $app
echo 更新地址: $url
echo 更新后才可以使用
exit
fi
for i in `cat data.txt | sed '1,4d'`
do
echo $i>>anycast.txt
done
rm -rf meta.txt data.txt
n=0
m=$(cat anycast.txt | wc -l)
for i in `cat anycast.txt`
do
ping -c 20 -i 0.2 -n -q $i > icmp/$n.log&
n=$[$n+1]
per=$n*100/$m
while true
do
p=$(ps | grep ping | grep -v "grep" | wc -l)
if [ $p -ge 100 ]
then
echo 正在测试 ICMP 丢包率:进程数 $p,已完成 $per %
sleep 0.5
else
echo 正在测试 ICMP 丢包率:进程数 $p,已完成 $per %
break
fi
done
done
rm -rf anycast.txt
while true
do
p=$(ps -ef | grep ping | grep -v "grep" | wc -l)
if [ $p -ne 0 ]
then
echo 等待 ICMP 进程结束:剩余进程数 $p
sleep 1
else
echo ICMP 丢包率测试完成
break
fi
done
cat icmp/*.log | sed -n '3~5p;4~5p;5~5p' | sed 'N;N;s/\n/ /g' | awk -F, '{print $1,$3,$4}' | sort -t' ' -n -k 9 | awk -F/ '{print $1,$5}' | awk '{print $2,$9,$16}' | sort -t' ' -n -k 2 -k 3 | awk '{print $1}' | sed '21,$d' > ip.txt
rm -rf icmp
echo 选取20个丢包率最少的IP地址下载测速
mkdir temp
for i in `cat ip.txt`
do
echo $i 启动测速
curl --resolve $domain:443:$i https://$domain/$file -o temp/$i -s --connect-timeout 2 --max-time 10&
done
echo 等待测速进程结束,筛选出三个优选的IP
sleep 15
echo 测速完成
ls -S temp > ip.txt
rm -rf temp
n=$(wc -l ip.txt | awk '{print $1}')
if [ $n -ge 3 ]; then
first=$(sed -n '1p' ip.txt)
second=$(sed -n '2p' ip.txt)
third=$(sed -n '3p' ip.txt)
rm -rf ip.txt
echo 优选的IP地址为 $first - $second - $third
echo 第一次测试 $first
curl --resolve $domain:443:$first https://$domain/$file -o /dev/null --connect-timeout 5 --max-time 10 > log.txt 2>&1
cat log.txt | tr '\r' '\n' | awk '{print $NF}' | sed '1,3d;$d' | grep -v 'k\|M' >> speed.txt
for i in `cat log.txt | tr '\r' '\n' | awk '{print $NF}' | sed '1,3d;$d' | grep k | sed 's/k//g'`
do
declare -i k
k=$i
k=k*1024
echo $k >> speed.txt
done
for i in `cat log.txt | tr '\r' '\n' | awk '{print $NF}' | sed '1,3d;$d' | grep M | sed 's/M//g'`
do
i=$(echo | awk '{print '$i'*10 }')
declare -i M
M=$i
M=M*1024*1024/10
echo $M >> speed.txt
done
declare -i max
declare -i max1
max=0
for i in `cat speed.txt`
do
if [ $i -ge $max ]; then
max=$i
fi
done
rm -rf log.txt speed.txt
if [ $max -ge $speed ]; then
anycast=$first
break
fi
max=$[$max/1024]
max1=max
echo 峰值速度 $max kB/s
echo 第二次测试 $first
curl --resolve $domain:443:$first https://$domain/$file -o /dev/null --connect-timeout 5 --max-time 10 > log.txt 2>&1
cat log.txt | tr '\r' '\n' | awk '{print $NF}' | sed '1,3d;$d' | grep -v 'k\|M' >> speed.txt
for i in `cat log.txt | tr '\r' '\n' | awk '{print $NF}' | sed '1,3d;$d' | grep k | sed 's/k//g'`
do
declare -i k
k=$i
k=k*1024
echo $k >> speed.txt
done
for i in `cat log.txt | tr '\r' '\n' | awk '{print $NF}' | sed '1,3d;$d' | grep M | sed 's/M//g'`
do
i=$(echo | awk '{print '$i'*10 }')
declare -i M
M=$i
M=M*1024*1024/10
echo $M >> speed.txt
done
declare -i max
declare -i max2
max=0
for i in `cat speed.txt`
do
if [ $i -ge $max ]; then
max=$i
fi
done
rm -rf log.txt speed.txt
if [ $max -ge $speed ]; then
anycast=$first
break
fi
max=$[$max/1024]
max2=max
echo 峰值速度 $max kB/s
if [ $max1 -ge $max2 ]
then
curl --ipv4 --resolve service.udpfile.com:443:$first --retry 3 -s -X POST -d ''20210903-$first-$max1'' "https://service.udpfile.com?asn="$asn"&city="$city"" -o /dev/null --connect-timeout 5 --max-time 10
else
curl --ipv4 --resolve service.udpfile.com:443:$first --retry 3 -s -X POST -d ''20210903-$first-$max2'' "https://service.udpfile.com?asn="$asn"&city="$city"" -o /dev/null --connect-timeout 5 --max-time 10
fi
echo 第一次测试 $second
curl --resolve $domain:443:$second https://$domain/$file -o /dev/null --connect-timeout 5 --max-time 10 > log.txt 2>&1
cat log.txt | tr '\r' '\n' | awk '{print $NF}' | sed '1,3d;$d' | grep -v 'k\|M' >> speed.txt
for i in `cat log.txt | tr '\r' '\n' | awk '{print $NF}' | sed '1,3d;$d' | grep k | sed 's/k//g'`
do
declare -i k
k=$i
k=k*1024
echo $k >> speed.txt
done
for i in `cat log.txt | tr '\r' '\n' | awk '{print $NF}' | sed '1,3d;$d' | grep M | sed 's/M//g'`
do
i=$(echo | awk '{print '$i'*10 }')
declare -i M
M=$i
M=M*1024*1024/10
echo $M >> speed.txt
done
declare -i max
declare -i max1
max=0
for i in `cat speed.txt`
do
if [ $i -ge $max ]; then
max=$i
fi
done
rm -rf log.txt speed.txt
if [ $max -ge $speed ]; then
anycast=$second
break
fi
max=$[$max/1024]
max1=max
echo 峰值速度 $max kB/s
echo 第二次测试 $second
curl --resolve $domain:443:$second https://$domain/$file -o /dev/null --connect-timeout 5 --max-time 10 > log.txt 2>&1
cat log.txt | tr '\r' '\n' | awk '{print $NF}' | sed '1,3d;$d' | grep -v 'k\|M' >> speed.txt
for i in `cat log.txt | tr '\r' '\n' | awk '{print $NF}' | sed '1,3d;$d' | grep k | sed 's/k//g'`
do
declare -i k
k=$i
k=k*1024
echo $k >> speed.txt
done
for i in `cat log.txt | tr '\r' '\n' | awk '{print $NF}' | sed '1,3d;$d' | grep M | sed 's/M//g'`
do
i=$(echo | awk '{print '$i'*10 }')
declare -i M
M=$i
M=M*1024*1024/10
echo $M >> speed.txt
done
declare -i max
declare -i max2
max=0
for i in `cat speed.txt`
do
if [ $i -ge $max ]; then
max=$i
fi
done
rm -rf log.txt speed.txt
if [ $max -ge $speed ]; then
anycast=$second
break
fi
max=$[$max/1024]
max2=max
echo 峰值速度 $max kB/s
if [ $max1 -ge $max2 ]
then
curl --ipv4 --resolve service.udpfile.com:443:$second --retry 3 -s -X POST -d ''20210903-$second-$max1'' "https://service.udpfile.com?asn="$asn"&city="$city"" -o /dev/null --connect-timeout 5 --max-time 10
else
curl --ipv4 --resolve service.udpfile.com:443:$second --retry 3 -s -X POST -d ''20210903-$second-$max2'' "https://service.udpfile.com?asn="$asn"&city="$city"" -o /dev/null --connect-timeout 5 --max-time 10
fi
echo 第一次测试 $third
curl --resolve $domain:443:$third https://$domain/$file -o /dev/null --connect-timeout 5 --max-time 10 > log.txt 2>&1
cat log.txt | tr '\r' '\n' | awk '{print $NF}' | sed '1,3d;$d' | grep -v 'k\|M' >> speed.txt
for i in `cat log.txt | tr '\r' '\n' | awk '{print $NF}' | sed '1,3d;$d' | grep k | sed 's/k//g'`
do
declare -i k
k=$i
k=k*1024
echo $k >> speed.txt
done
for i in `cat log.txt | tr '\r' '\n' | awk '{print $NF}' | sed '1,3d;$d' | grep M | sed 's/M//g'`
do
i=$(echo | awk '{print '$i'*10 }')
declare -i M
M=$i
M=M*1024*1024/10
echo $M >> speed.txt
done
declare -i max
declare -i max1
max=0
for i in `cat speed.txt`
do
if [ $i -ge $max ]; then
max=$i
fi
done
rm -rf log.txt speed.txt
if [ $max -ge $speed ]; then
anycast=$third
break
fi
max=$[$max/1024]
max1=max
echo 峰值速度 $max kB/s
echo 第二次测试 $third
curl --resolve $domain:443:$third https://$domain/$file -o /dev/null --connect-timeout 5 --max-time 10 > log.txt 2>&1
cat log.txt | tr '\r' '\n' | awk '{print $NF}' | sed '1,3d;$d' | grep -v 'k\|M' >> speed.txt
for i in `cat log.txt | tr '\r' '\n' | awk '{print $NF}' | sed '1,3d;$d' | grep k | sed 's/k//g'`
do
declare -i k
k=$i
k=k*1024
echo $k >> speed.txt
done
for i in `cat log.txt | tr '\r' '\n' | awk '{print $NF}' | sed '1,3d;$d' | grep M | sed 's/M//g'`
do
i=$(echo | awk '{print '$i'*10 }')
declare -i M
M=$i
M=M*1024*1024/10
echo $M >> speed.txt
done
declare -i max
declare -i max2
max=0
for i in `cat speed.txt`
do
if [ $i -ge $max ]; then
max=$i
fi
done
rm -rf log.txt speed.txt
if [ $max -ge $speed ]; then
anycast=$third
break
fi
max=$[$max/1024]
max2=max
echo 峰值速度 $max kB/s
if [ $max1 -ge $max2 ]
then
curl --ipv4 --resolve service.udpfile.com:443:$third --retry 3 -s -X POST -d ''20210903-$third-$max1'' "https://service.udpfile.com?asn="$asn"&city="$city"" -o /dev/null --connect-timeout 5 --max-time 10
else
curl --ipv4 --resolve service.udpfile.com:443:$third --retry 3 -s -X POST -d ''20210903-$third-$max2'' "https://service.udpfile.com?asn="$asn"&city="$city"" -o /dev/null --connect-timeout 5 --max-time 10
fi
fi
done
break
done
max=$[$max/1024]
declare -i realbandwidth
realbandwidth=max/128
endtime=`date +'%Y-%m-%d %H:%M:%S'`
start_seconds=$(date --date="$starttime" +%s)
end_seconds=$(date --date="$endtime" +%s)
clear
curl --ipv4 --resolve service.udpfile.com:443:$anycast --retry 3 -s -X POST -d ''20210903-$anycast-$max'' "https://service.udpfile.com?asn="$asn"&city="$city"" -o temp.txt
publicip=$(cat temp.txt | grep publicip= | cut -f 2- -d'=')
colo=$(cat temp.txt | grep colo= | cut -f 2- -d'=')
rm -rf temp.txt
echo $anycast>resolve.txt
echo 优选IP $anycast 满足 $bandwidth Mbps带宽需求
echo 公网IP $publicip
echo 自治域 AS$asn
echo 经纬度 $longitude,$latitude
echo META城市 $city
echo 实测带宽 $realbandwidth Mbps
echo 峰值速度 $max kB/s
echo 数据中心 $colo
echo 总计用时 $((end_seconds-start_seconds)) 秒