-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_clash.sh
executable file
·385 lines (385 loc) · 14.7 KB
/
get_clash.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
#!/bin/bash
#定时执行任务
#0 15 * * *
#对于raw.githubusercontent.com无法下载的使用下面加速地址
#https://ghproxy.com/
#------------------------------------------------------------------------------------------------------
get_arch=`arch`
#设置只提取平均速度大于多少M的节点,例如:10M*1024*1024=10485760,只能设置整数
#
#speed_gt=1
#let avg_speed=$speed_gt*1024*1024
avg_speed=100
#模板文件路径
parm_path=$(cd `dirname $0`; pwd)
date=$(date "+%Y-%m-%d %H:%M:%S")
echo --$date-- "-----------------------开始更新CLASH配置文件----------------------" |tee -a /tmp/clash_run_log.log
#删除排序文件
cd /tmp
rm -rf line.log node_config.yaml config_cl.yaml
rm -rf /mnt/updateClashToGithub/node
cp $parm_path/formwork /tmp/newconfig
year=$(date "+%Y")
month=$(date "+%m")
day=$(date "+%d")
merge=$(date "+%Y%m%d")
#以下列表可插入能直接下载的配置文件网址
urllist=(
"https://sub.789.st/sub?target=clash&url=https://clashnode.com/wp-content/uploads/$year/$month/$merge.txt"
)
for i in ${urllist[@]}
do
a=${i#*//}
b=${a%%.*}
let c++
get=`aria2c -s 3 -d /run -o $b$c.yaml $i`
echo $get >> /tmp/clash_run_log.log
if echo "$get" | grep -e "download completed";then
date=$(date "+%Y-%m-%d %H:%M:%S")
echo --$date-- "下载完成!" |tee -a /tmp/clash_run_log.log
else
echo --$date-- "下载失败!" |tee -a /tmp/clash_run_log.log
let err++
if [ $err -eq "${#urllist[@]}" ];then
echo --$date-- "当前共:""${#urllist[@]}""个订阅地址,并且全部下载出错,无可用信息,巳结束运行!!" |tee -a /tmp/clash_run_log.log
exit 1
fi
fi
done
echo "------------------------------------------please wait!----------------------------------------------------"
echo "----------------------------------------------------------------------------------------------------------"
echo "----------------------------------------------------------------------------------------------------------"
sleep 3
echo --$date-- "准备获取全部节点列表"|tee -a |tee -a /tmp/line.log
for filename in $(ls /run/*.yaml)
do
while read line
do
line=`echo $line | sed 's/\"//g'`
if echo $line | grep "{name:" | grep -v "中国";then
# a=${line#*, }
# random=`head /dev/urandom | tr -dc A-Za-z0-9 | head -c 15`
# echo "- {name: 吉祥|"$random"|, "$a >> /mnt/updateClashToGithub/node
echo $line >> /mnt/updateClashToGithub/node
fi
if echo $line | grep -q "proxy-groups:";then
break
fi
done < $filename
done
rm -rf /run/*.yaml
#-------------------------------------------------------------------------------------------------
echo --$date-- "节点服务器去重"|tee -a /tmp/clash_run_log.log
for filename in $(ls /mnt/updateClashToGithub/node)
do
while read line
do
if echo $line | grep "{name:";then
a=${line#*, }
b=${a%%,*} #server:$b
c=${a#*port:}
d=${c%%,*} #port:$d
echo "$b," port:"$d" >>/tmp/samenodeserver
fi
done < $filename
done
temp1=`sort /tmp/samenodeserver|uniq -d`
if [[ "$temp1" != "" ]];then
echo "$temp1" >>/tmp/same
for filename in $(ls /tmp/same)
do
while read line
do
s=`cat /mnt/updateClashToGithub/node |grep -n "$line"|cut -f1 -d:`
delnum=($s)
del=$["${#delnum[@]}" - 1]
for ((i=0; i<$del; i++));do
n="${delnum[$i]}"
#给要删除的行做标记
sed -i "${n},${n}s/- {name/mark-del/g" /mnt/updateClashToGithub/node
done
done < $filename
done
#删除重复节点的数量
for filename in $(ls /mnt/updateClashToGithub/node);do
while read line;do
if echo $line | grep "mark-del";then
let x++
fi
done < $filename
done
date=$(date "+%Y-%m-%d %H:%M:%S")
echo --$date-- "需要从可用节点中删除重复服务器节点的数量为:"$x |tee -a /tmp/clash_run_log.log
sed -i -e "/mark-del/d" /mnt/updateClashToGithub/node #删除所有标记的重复行
sed -i -e "/vless,/d" /mnt/updateClashToGithub/node #删除不可识别的代理类型
echo --$date-- "可用节点服务器完成去重"|tee -a /tmp/clash_run_log.log
rm -rf /tmp/same
else
echo --$date-- "没有重复的节点服务器" |tee -a /tmp/clash_run_log.log
fi
rm -rf /tmp/samenodeserver
#---------------------------------------------------------------------------------------------
#提取代理与模板文件进行合并并生成新的配置文件
date=$(date "+%Y-%m-%d %H:%M:%S")
echo --$date-- "开始合并订阅文件" |tee -a /tmp/clash_run_log.log
for filename in $(ls /mnt/updateClashToGithub/node)
do
while read line
do
if echo $line | grep "{name:" ;then
sed -i '/以上为代理地址请插入/i\'" $line"'' /tmp/newconfig
a=${line#*:}
b=${a%%,*}
sed -i '/以上为节点选择组的自动选择代理请插入/i\'" -$b"'' /tmp/newconfig
sed -i '/以上为自动选择组下面的代理地址请插入/i\'" -$b"'' /tmp/newconfig
sed -i '/以上为负载均衡组下面的代理地址请插入/i\'" -$b"'' /tmp/newconfig
sed -i '/以上为故障转移组下面的代理地址请插入/i\'" -$b"'' /tmp/newconfig
sed -i '/以上为漏网之鱼组下面的代理地址请插入/i\'" -$b"'' /tmp/newconfig
fi
if echo $line | grep -q "proxy-groups:";then
break
fi
done < $filename
done
date=$(date "+%Y-%m-%d %H:%M:%S")
echo --$date-- "订阅文件合并完成!" |tee -a /tmp/clash_run_log.log
mv /tmp/newconfig /tmp/allnode_config.yaml
#----------------------------------------------------------------------------------------------
date=$(date "+%Y-%m-%d %H:%M:%S")
echo --$date-- "开始测速!" |tee -a /tmp/clash_run_log.log
cd $parm_path
if [[ $get_arch =~ "x86_64" ]];then
echo "第统类型:x86_64"
./lite-linux-amd64 --config ./config.json --test /tmp/allnode_config.yaml
elif [[ $get_arch =~ "armv7l" ]];then
echo "第统类型:armv7l"
./lite-linux-armv7 --config ./config.json --test /tmp/allnode_config.yaml
else
echo "系统类型不支持!"
fi
date=$(date "+%Y-%m-%d %H:%M:%S")
echo --$date-- "测速完成!" |tee -a /tmp/clash_run_log.log
#----------------------------------------------------------------------------------------------
for filename in $(ls /tmp/allnode_config.yaml)
do
while read line
do
if echo $line | grep "{name:";then
echo $line >> /tmp/node_config.yaml
fi
if echo $line | grep -q "proxy-groups:";then
break
fi
done < $filename
done
#----------------------------------------------------------------------------------------------
#多线程筛选可用节点部分
linksCount=$(cat $parm_path/output.json | jq -r '.linksCount')
args1=$[$linksCount/4]
chuthree=$[$[$linksCount-$args1]/3]
args2=$[$args1+$chuthree]
chutwo=$[$[$linksCount-$args1-$chuthree]/2]
args3=$[$args1+$chuthree+$chutwo]
args4=$linksCount
start_time=`date +%s` #定义脚本运行的开始时间
getNode(){
for ((i=$1; i<$2; i++))
do
var=$(cat $parm_path/output.json | jq -r '.nodes['$i'].avg_speed')
if [ $var -lt $avg_speed ];then
invalid=`cat ./output.json | jq -r '.nodes['$i'].remarks'|sed 's/+/ /g'`
echo "$invalid" >>/tmp/invalidnode.mp
else
valid=`cat ./output.json | jq -r '.nodes['$i'].remarks'|sed 's/+/ /g'`
echo "$valid" >>/tmp/validnode.mp
fi
done
}
getNode 0 $args1 &
getNode $args1 $args2 &
getNode $args2 $args3 &
getNode $args3 $args4 &
wait
stop_time=`date +%s` # 定义脚本运行的结束时间
date=$(date "+%Y-%m-%d %H:%M:%S")
echo --$date-- "多线程查询可用节点用时:`expr $stop_time - $start_time`s" |tee -a /tmp/clash_run_log.log
#----------------------------------------------------------------------------------------------
for filename in $(ls /tmp/invalidnode.mp)
do
while read line
do
remarks_array[$l]=$line
let l++
done < $filename
done
if [[ -e "/tmp/validnode.mp" ]];then
for filename in $(ls /tmp/validnode.mp)
do
while read line
do
normal_num[$y]=$line
let y++
done < $filename
done
else
echo --$date-- "不好意思,一个可用的节点都没有,程序退出!"|tee -a /tmp/clash_run_log.log
rm -rf /tmp/*.mp #清理线程临时文件
exit 1
fi
rm -rf /tmp/*.mp #清理线程临时文件
#----------------------------------------------------------------------------------
date=$(date "+%Y-%m-%d %H:%M:%S")
echo --$date-- "正常可用的节点数量:""${#normal_num[*]}"|tee -a /tmp/clash_run_log.log
echo --$date-- "正常可用的节点数量:""${#normal_num[*]}" |tee -a /tmp/line.log
for ((i=0; i<"${#normal_num[*]}";i++))
do
let o++
str_num=`echo "${normal_num[$i]}"|awk '{print length($0)}'`
echo "第:"$o"节点----""${normal_num[$i]}" "字符:"$str_num |tee -a /tmp/line.log
done
date=$(date "+%Y-%m-%d %H:%M:%S")
echo --$date-- "待删除的节点数量:""${#remarks_array[*]}" |tee -a /tmp/clash_run_log.log
echo --$date-- "待删除的节点数量:""${#remarks_array[*]}" |tee -a /tmp/line.log
#----------------------------------------------------------------------------------------------
echo --$date-- "准备排序待删除的:""${#remarks_array[*]}""个节点......"|tee -a /tmp/clash_run_log.log
sleep 5
start_time=`date +%s` #定义脚本运行的开始时间
MAX=`echo "${remarks_array[0]}" | awk '{print length($0)}'`
for I in ${!remarks_array[@]};do
MIN=`echo "${remarks_array[$I]}"|awk '{print length($0)}'`
allnum[$I]=$MIN
if [[ ${MAX} -le ${MIN} ]];then
MAX=${MIN}
fi
done
echo "MAX:" $MAX
for (( l=0; l<=$MAX; l++ ));do
for (( s=0; s<"${#allnum[@]}"; s++ ));do
m="${allnum[$s]}"
if [ $m -eq $l ];then
jump[$l]=$l
fi
done
done
for (( l=0; l<=$MAX; l++ ));do
if [ "${jump[$l]}" != "" ];then
jumpnum[$w]=$l
let w++
fi
done
for (( s="${#jumpnum[@]}"; s>=0; s-- ))
do
for (( i=0; i<"${#remarks_array[*]}"; i++ ))
do
num2=`echo "${remarks_array[$i]}" | awk '{print length($0)}'`
num3="${jumpnum[$s]}"
if [[ $num2 -eq $num3 ]];then
echo "${remarks_array[$i]}" "字符:" $num2
echo "${remarks_array[$i]}" >> /tmp/node.ss
fi
done
done
unset remarks_array
for filename in $(ls /tmp/node.ss)
do
while read line
do
remarks_array[$r]=$line
let r++
done < $filename
done
rm /tmp/node.ss
stop_time=`date +%s` # 定义脚本运行的结束时间
echo --$date-- "排序用时:`expr $stop_time - $start_time`s" |tee -a /tmp/clash_run_log.log
date=$(date "+%Y-%m-%d %H:%M:%S")
echo --$date-- "节点排序完成" |tee -a /tmp/clash_run_log.log
#----------------------------------------------------------------------------------------------
echo --$date-- "准备删除低速节点" |tee -a /tmp/line.log
echo --$date-- "开始清理低速节点"|tee -a /tmp/clash_run_log.log
for ((i=0; i<"${#remarks_array[*]}";i++))
do
let j++
str_num=`echo "${remarks_array[$i]}"|awk '{print length($0)}'`
echo "删除第"$j"个节点:""${remarks_array[$i]}" "字符:"$str_num |tee -a /tmp/line.log
line=`cat /tmp/node_config.yaml | grep -n "${remarks_array[$i]}" | sed 's/:/d;:/g' |awk -F ":" '{print $1}' | xargs |sed 's/[[:space:]]*//g'`
dellaststr=`echo $line |sed 's/.$//'`
array=(${line//d;/ })
if [ "${#array[*]}" -gt 1 ];then
for ((k=0; k<"${#array[*]}"; k++))
do
str=`sed -n "${array[$k]}"p /tmp/node_config.yaml`
names=`echo ${str#*name:}`
cutnames=`echo ${names%%,*}`
if [ "$cutnames" = "${remarks_array[$i]}" ];then
echo "该行疑似包含其它可用节点,将删除该行" |tee -a /tmp/line.log
echo "截取到该行内容包含:"$cutnames |tee -a /tmp/line.log
sed -i "${array[$k]}"d /tmp/node_config.yaml
echo "${#array[$k]}" |tee -a /tmp/line.log
fi
done
else
sed -i "${dellaststr}" /tmp/node_config.yaml
echo $dellaststr |tee -a /tmp/line.log
fi
done
date=$(date "+%Y-%m-%d %H:%M:%S")
echo --$date-- "低速节点清理完成"|tee -a /tmp/clash_run_log.log
echo --$date-- "低速节点删除完成"|tee -a /tmp/line.log
#----------------------------------------------------------------------------------------------
#开始整理config.yaml配置文件并提取代理合并入文件
echo --$date-- "开始整理配置文件并提取代理" |tee -a /tmp/clash_run_log.log
cp $parm_path/formwork /tmp/newconfig
for filename in $(ls /tmp/node_config.yaml)
do
while read line
do
if echo $line | grep "{name:";then
sed -i '/以上为代理地址请插入/i\'" $line"'' /tmp/newconfig
a=${line#*:}
b=${a%%,*}
sed -i '/以上为节点选择组的自动选择代理请插入/i\'" -$b"'' /tmp/newconfig
sed -i '/以上为自动选择组下面的代理地址请插入/i\'" -$b"'' /tmp/newconfig
sed -i '/以上为负载均衡组下面的代理地址请插入/i\'" -$b"'' /tmp/newconfig
sed -i '/以上为故障转移组下面的代理地址请插入/i\'" -$b"'' /tmp/newconfig
sed -i '/以上为漏网之鱼组下面的代理地址请插入/i\'" -$b"'' /tmp/newconfig
fi
done < $filename
done
echo --$date-- "代理提取完成并成功生成配置文件" |tee -a /tmp/clash_run_log.log
mv /tmp/newconfig /tmp/config_cl.yaml
echo --$date-- "配置文件以生成到:/tmp/config_cl.yaml,更新完成!" |tee -a /tmp/clash_run_log.log
restart_clash(){
date=$(date "+%Y-%m-%d %H:%M:%S")
echo --$date-- "------------------------重启CLASH-----------------------------" |tee -a /tmp/clash_run_log.log
rm /usr/local/clash/config.yaml
cp /tmp/config_cl.yaml /usr/local/clash/config.yaml
systemctl restart clash.service
date=$(date "+%Y-%m-%d %H:%M:%S")
echo --$date-- "------------------------重启CLASH完成-------------------------" |tee -a /tmp/clash_run_log.log
}
restart_clash #cp配置文件到clash配置目录并重启clash
date=$(date "+%Y-%m-%d %H:%M:%S")
#执行github同步脚本
$parm_path/Creategithubfile.sh
#清除日志内容
a=$(grep -c "" /tmp/clash_run_log.log)
if [ $a -gt 106 ]; then
rm /tmp/clash_run_log.log
fi
#附节点订阅地址
#https://github.com/freefq/free freefq/free, 节点数量: 34
#https://github.com/oslook/clash-freenode oslook/clash-freenode, 节点数量: 42
#https://github.com/ssrsub/ssr ssrsub/ssr, 节点数量: 82
#https://github.com/Leon406/SubCrawler Leon406/SubCrawler, 节点数量: 3272
#https://github.com/iwxf/free-v2ray iwxf/free-v2ray, 节点数量: 39
#https://github.com/Jsnzkpg/Jsnzkpg Jsnzkpg/Jsnzkpg, 节点数量: 80
#https://github.com/ermaozi/get_subscribe ermaozi/get_subscribe, 节点数量: 38
#https://github.com/wrfree/free wrfree/free, 节点数量: 34
#https://github.com/GreenFishStudio/GreenFish GreenFishStudio/GreenFish, 节点数量: 56
#https://github.com/anaer/Sub anaer/Sub, 节点数量: 246
#https://github.com/mhmhone/shadowrocket-free-subscribe mhmhone/shadowrocket-free-subscribe, 节点数量: 32
#https://github.com/aiboboxx/v2rayfree aiboboxx/v2rayfree, 节点数量: 46
#https://github.com/moneyfly1/sublist moneyfly1/sublist, 节点数量: 15
#https://github.com/Pawdroid/Free-servers Pawdroid/Free-servers, 节点数量: 15
#https://github.com/Fukki-Z/nodefree Nodefree.org, 节点数量: 50
#https://github.com/snakem982/proxypool