2019-08-03 00:02:17 +08:00
|
|
|
|
#!/bin/sh
|
|
|
|
|
|
|
|
|
|
|
|
CONFIG=passwall
|
2020-01-15 01:50:56 +08:00
|
|
|
|
LOG_FILE=/var/log/$CONFIG.log
|
2019-08-03 00:02:17 +08:00
|
|
|
|
|
2020-01-15 01:50:56 +08:00
|
|
|
|
echolog() {
|
2020-02-27 18:00:08 +08:00
|
|
|
|
local d="$(date "+%Y-%m-%d %H:%M:%S")"
|
2020-07-05 20:19:28 +08:00
|
|
|
|
#echo -e "$d: $1"
|
2020-02-27 18:00:08 +08:00
|
|
|
|
echo -e "$d: $1" >> $LOG_FILE
|
2020-01-15 01:50:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-03-04 12:18:23 +08:00
|
|
|
|
config_t_get() {
|
|
|
|
|
|
local index=0
|
|
|
|
|
|
[ -n "$4" ] && index=$4
|
|
|
|
|
|
local ret=$(uci -q get $CONFIG.@$1[$index].$2 2>/dev/null)
|
|
|
|
|
|
echo ${ret:=$3}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-10-26 07:59:56 +08:00
|
|
|
|
test_url() {
|
2020-03-04 12:18:23 +08:00
|
|
|
|
local url=$1
|
|
|
|
|
|
local try=1
|
|
|
|
|
|
[ -n "$2" ] && try=$2
|
|
|
|
|
|
local timeout=2
|
|
|
|
|
|
[ -n "$3" ] && timeout=$3
|
2020-07-05 20:19:28 +08:00
|
|
|
|
local extra_params=$4
|
|
|
|
|
|
status=$(/usr/bin/curl -I -o /dev/null -skL $extra_params --connect-timeout $timeout --retry $try -w %{http_code} "$url")
|
|
|
|
|
|
case "$status" in
|
|
|
|
|
|
204|\
|
|
|
|
|
|
200)
|
|
|
|
|
|
status=200
|
|
|
|
|
|
;;
|
|
|
|
|
|
esac
|
2019-08-03 00:02:17 +08:00
|
|
|
|
echo $status
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-10-26 07:59:56 +08:00
|
|
|
|
test_proxy() {
|
2020-07-05 20:19:28 +08:00
|
|
|
|
local try=3
|
2019-08-03 00:02:17 +08:00
|
|
|
|
result=0
|
2020-04-04 21:42:09 +08:00
|
|
|
|
status=$(test_url "https://www.google.com/generate_204" $try)
|
2019-10-26 07:59:56 +08:00
|
|
|
|
if [ "$status" = "200" ]; then
|
2019-08-03 00:02:17 +08:00
|
|
|
|
result=0
|
|
|
|
|
|
else
|
2020-03-04 12:18:23 +08:00
|
|
|
|
status2=$(test_url "https://www.baidu.com" $try)
|
2019-10-26 07:59:56 +08:00
|
|
|
|
if [ "$status2" = "200" ]; then
|
2019-08-03 00:02:17 +08:00
|
|
|
|
result=1
|
|
|
|
|
|
else
|
|
|
|
|
|
result=2
|
|
|
|
|
|
fi
|
|
|
|
|
|
fi
|
|
|
|
|
|
echo $result
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-10-26 07:59:56 +08:00
|
|
|
|
test_auto_switch() {
|
2020-03-04 12:18:23 +08:00
|
|
|
|
local type=$1
|
2020-04-10 01:07:41 +08:00
|
|
|
|
local index=$3
|
|
|
|
|
|
local b_tcp_nodes=$4
|
2020-03-04 12:18:23 +08:00
|
|
|
|
local now_node
|
|
|
|
|
|
if [ -f "/var/etc/$CONFIG/id/${type}_${index}" ]; then
|
|
|
|
|
|
now_node=$(cat /var/etc/$CONFIG/id/${type}_${index})
|
2019-08-03 00:02:17 +08:00
|
|
|
|
else
|
2020-03-04 12:18:23 +08:00
|
|
|
|
return 1
|
2019-08-03 00:02:17 +08:00
|
|
|
|
fi
|
2019-10-26 07:59:56 +08:00
|
|
|
|
|
2020-03-04 12:18:23 +08:00
|
|
|
|
status=$(test_proxy)
|
2020-04-10 01:07:41 +08:00
|
|
|
|
if [ "$status" == 0 ]; then
|
|
|
|
|
|
#echolog "自动切换检测:${type}_${index}节点正常。"
|
|
|
|
|
|
return 0
|
|
|
|
|
|
elif [ "$status" == 2 ]; then
|
2020-03-04 12:18:23 +08:00
|
|
|
|
echolog "自动切换检测:无法连接到网络,请检查网络是否正常!"
|
2020-04-10 01:07:41 +08:00
|
|
|
|
return 2
|
2020-03-04 12:18:23 +08:00
|
|
|
|
elif [ "$status" == 1 ]; then
|
|
|
|
|
|
echolog "自动切换检测:${type}_${index}节点异常,开始切换节点!"
|
2020-07-05 20:19:28 +08:00
|
|
|
|
|
|
|
|
|
|
#检测自动切换列表主节点是否能使用
|
|
|
|
|
|
local main_node=$(echo $b_tcp_nodes | awk -F ' ' '{print $1}')
|
|
|
|
|
|
if [ "$now_node" != "$main_node" ]; then
|
|
|
|
|
|
local tmp_port=$(/usr/share/passwall/app.sh get_new_port 61080 tcp)
|
|
|
|
|
|
/usr/share/passwall/app.sh run_socks "$main_node" "127.0.0.1" "$tmp_port" "/var/etc/passwall/auto_switch_main.json" "10"
|
|
|
|
|
|
proxy_status=$(test_url "https://www.google.com/generate_204" 3 3 "-x socks5h://127.0.0.1:$tmp_port")
|
|
|
|
|
|
ps -w | grep -v "grep" | grep "/var/etc/passwall/auto_switch_main.json" | awk '{print $1}' | xargs kill -9 >/dev/null 2>&1
|
|
|
|
|
|
if [ "$proxy_status" -eq 200 ]; then
|
|
|
|
|
|
#主节点正常,切换到主节点
|
|
|
|
|
|
echolog "自动切换检测:${type}_${index}主节点正常,切换到主节点!"
|
|
|
|
|
|
/usr/share/passwall/app.sh node_switch $type $2 $index $main_node
|
|
|
|
|
|
return 0
|
|
|
|
|
|
fi
|
|
|
|
|
|
fi
|
|
|
|
|
|
|
2020-03-05 19:03:11 +08:00
|
|
|
|
local new_node
|
2020-03-04 12:18:23 +08:00
|
|
|
|
in_backup_nodes=$(echo $b_tcp_nodes | grep $now_node)
|
|
|
|
|
|
# 判断当前节点是否存在于备用节点列表里
|
|
|
|
|
|
if [ -z "$in_backup_nodes" ]; then
|
|
|
|
|
|
# 如果不存在,设置第一次节点为新的节点
|
|
|
|
|
|
new_node=$(echo $b_tcp_nodes | awk -F ' ' '{print $1}')
|
|
|
|
|
|
else
|
|
|
|
|
|
# 如果存在,设置下一个备用节点为新的节点
|
2020-04-10 01:07:41 +08:00
|
|
|
|
#local count=$(expr $(echo $b_tcp_nodes | grep -o ' ' | wc -l) + 1)
|
2020-03-04 12:18:23 +08:00
|
|
|
|
local next_node=$(echo $b_tcp_nodes | awk -F "$now_node" '{print $2}' | awk -F " " '{print $1}')
|
|
|
|
|
|
if [ -z "$next_node" ]; then
|
|
|
|
|
|
new_node=$(echo $b_tcp_nodes | awk -F ' ' '{print $1}')
|
|
|
|
|
|
else
|
|
|
|
|
|
new_node=$next_node
|
|
|
|
|
|
fi
|
2019-08-03 00:02:17 +08:00
|
|
|
|
fi
|
2020-04-10 01:07:41 +08:00
|
|
|
|
/usr/share/passwall/app.sh node_switch $type $2 $index $new_node
|
|
|
|
|
|
sleep 10s
|
|
|
|
|
|
# 切换节点后等待10秒后再检测一次,如果还是不通继续切,直到可用为止
|
|
|
|
|
|
status2=$(test_proxy)
|
|
|
|
|
|
if [ "$status2" -eq 0 ]; then
|
|
|
|
|
|
echolog "自动切换检测:${type}_${index}节点切换完毕!"
|
|
|
|
|
|
return 0
|
|
|
|
|
|
elif [ "$status2" -eq 1 ]; then
|
|
|
|
|
|
test_auto_switch $1 $2 $3 "$4"
|
|
|
|
|
|
elif [ "$status2" -eq 2 ]; then
|
|
|
|
|
|
return 2
|
|
|
|
|
|
fi
|
2020-03-04 12:18:23 +08:00
|
|
|
|
fi
|
2019-08-03 00:02:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-12-14 18:01:26 +08:00
|
|
|
|
start() {
|
2020-07-05 20:19:28 +08:00
|
|
|
|
(
|
|
|
|
|
|
flock -xn 200
|
|
|
|
|
|
[ "$?" != "0" ] && exit 0
|
|
|
|
|
|
|
|
|
|
|
|
ENABLED=$(config_t_get global enabled 0)
|
|
|
|
|
|
[ "$ENABLED" != 1 ] && return 1
|
|
|
|
|
|
ENABLED=$(config_t_get auto_switch enable 0)
|
|
|
|
|
|
[ "$ENABLED" != 1 ] && return 1
|
2020-03-08 15:21:55 +08:00
|
|
|
|
delay=$(config_t_get auto_switch testing_time 1)
|
|
|
|
|
|
sleep ${delay}m
|
2020-07-05 20:19:28 +08:00
|
|
|
|
while [ "$ENABLED" -eq 1 ]
|
|
|
|
|
|
do
|
|
|
|
|
|
# TCP_NODE_NUM=$(config_t_get global_other tcp_node_num 1)
|
|
|
|
|
|
# 暂时只能检测TCP1
|
|
|
|
|
|
TCP_NODE_NUM=1
|
|
|
|
|
|
for i in $(seq 1 $TCP_NODE_NUM); do
|
|
|
|
|
|
eval TCP_NODE$i=\"$(config_t_get auto_switch tcp_node$i nil)\"
|
|
|
|
|
|
eval tmp=\$TCP_NODE$i
|
|
|
|
|
|
[ -n "$tmp" -a "$tmp" != "nil" ] && {
|
|
|
|
|
|
test_auto_switch TCP tcp $i "$tmp"
|
|
|
|
|
|
}
|
|
|
|
|
|
done
|
|
|
|
|
|
delay=$(config_t_get auto_switch testing_time 1)
|
|
|
|
|
|
sleep ${delay}m
|
|
|
|
|
|
done
|
|
|
|
|
|
) 200>"/var/lock/passwall_auto_switch.lock"
|
2019-12-14 18:01:26 +08:00
|
|
|
|
}
|
2019-08-03 00:02:17 +08:00
|
|
|
|
|
2019-12-14 18:01:26 +08:00
|
|
|
|
case $1 in
|
|
|
|
|
|
test_url)
|
|
|
|
|
|
test_url $2
|
|
|
|
|
|
;;
|
|
|
|
|
|
*)
|
|
|
|
|
|
start
|
|
|
|
|
|
;;
|
2020-04-04 21:42:09 +08:00
|
|
|
|
esac
|