#! /bin/bash
conf=/etc/config/passwall
# 获取所有节点
function getNodes(){
f=$1;
ret=`cat $f | grep "config nodes" | awk '{print $3}' | sed -r "s/'//g"`;
echo $ret | tr " " " ";
}
# 移动旧的自动切换节点
function removeAutoSwitchNode(){
f=$1;
p=1;
stp=0;
while IFS= read -r line; do
if [[ $stp == "1" ]]; then
ret=`echo "$line" | grep 'list tcp_node'`;
if [[ $ret != "" ]]; then
continue;
fi
stp=0;
fi
echo $line >> "$f.$p";
ret=`echo "$line" | grep 'config auto_switch'`;
if [[ $ret != "" ]]; then
p=$((p+1));
stp=1;
fi
done < "$f"
mv $f "$f.bak"
cat "$f.1" > "$f";
rm "$f.1" -rf;
cat "$f.2" >> "$f";
rm "$f.2" -rf;
}
# 添加自动切换节点
function addAutoSwitchNode(){
f="$1";
n="$2";
mv $f "$f.bak1";
cat "$f.bak1" | sed -r "s/(config auto_switch)/\1\n list tcp_node '$n'/g" > $f;
rm "$f.bak1" -rf;
}
# 主入口程序
function main(){
f=$1;
nodes=$(getNodes "$f");
removeAutoSwitchNode "$f";
for n in ${nodes[@]}; do
addAutoSwitchNode "$f" "$n";
done
sh /usr/share/passwall/app.sh stop;
lua /usr/share/passwall/subscribe.lua start
sh /usr/share/passwall/app.sh start;
}
main "$conf"
将脚本上传到openwrt服务器上。然后通过corntab -e 定时启动