Skip to content

Auto Switch Servers

Jian Chang edited this page Oct 17, 2018 · 5 revisions

1.准备工作

opkg update
opkg install wget ca-bundle ca-certificates

2.保存以下内容为 shell 脚本文件

#!/bin/sh

NAME=shadowsocks

wget --spider --quiet --timeout=10 https://www.facebook.com/
if [ "$?" != "0" ]; then
    wget --spider --quiet --timeout=10 https://www.baidu.com/
    if [ "$?" = "0" ]; then
        servers=$(uci -n export $NAME | awk '$2=/servers/{print substr($3,2,9)}' | awk 'NR>2 && NR<14{print $0}')
        next_server=$(uci get $NAME.@transparent_proxy[0].main_server | awk -v servers="$servers" '{split(servers,V);for (i in V) {if (V[i] == $1) {print V[i % 11 + 1];break}}}')
        if [ -n "$next_server" ]; then
            uci delete $NAME.@transparent_proxy[0].main_server
            for c in $(seq 2); do
                uci add_list $NAME.@transparent_proxy[0].main_server=$next_server
            done
            uci commit $NAME
            /etc/init.d/$NAME restart
        fi
    fi
fi

3.添加以下计划任务

*      *     *     *     *     /path/to/shell-script >/dev/null 2>&1