-
-
Save vivisidea/380cfec33c458db29c15 to your computer and use it in GitHub Desktop.
simple script to choose the BEST shadowsocks server from serveral ips
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# simple script to choose the BEST shadowsocks server from serveral ips | |
# by wq.yang@2015-10-20 | |
# | |
TEST_RESOURCES="https://www.google.co.jp/webhp?hl=en&gws_rd=cr&ei=PEZHVoWJOeS9mgXD2ro4#hl=en&q=google \ | |
https://twitter.com/ \ | |
https://www.facebook.com/?_rdr=p | |
" | |
LOG_FILE="/tmp/ss.log" | |
RESULT_FILE=/tmp/ss-speed-test.txt | |
ARGS="--pid-file /tmp/ss-speed-test.pid --log-file $LOG_FILE \ | |
-b 127.0.0.1 -l 1080 -k YOUR_PASSWORD\ | |
-m rc4-md5 -t 600 --fast-open true \ | |
" | |
SERVER_PORTS=( | |
"YOUR_SERVER_IP:PORT" | |
"YOUR_SERVER_IP:PORT" | |
"YOUR_SERVER_IP:PORT" | |
"YOUR_SERVER_IP:PORT" | |
"YOUR_SERVER_IP:PORT" | |
"YOUR_SERVER_IP:PORT" | |
"YOUR_SERVER_IP:PORT" | |
"YOUR_SERVER_IP:PORT" | |
"YOUR_SERVER_IP:PORT" | |
"YOUR_SERVER_IP:PORT" | |
"YOUR_SERVER_IP:PORT" | |
"YOUR_SERVER_IP:PORT" | |
) | |
# simple log function | |
function log(){ | |
echo "`date "+%F %H:%M:%S"` - ${*}" >> ${LOG_FILE} | |
} | |
# if the local port is taken, try stop first | |
# use the fake server address and fake port, what the command need is the pid file | |
function stop_daemon(){ | |
lsof -i :1080 -s TCP:LISTEN -n > /dev/null 2>&1 | |
if [ $? -eq 0 ]; then | |
echo "stopping ss client ..." | |
sslocal -d stop -s 127.0.0.1 -p 65535 $ARGS | |
fi | |
} | |
# test resource access time | |
function test_resource(){ | |
local total_time=0 | |
local t=0 | |
for url in $TEST_RESOURCES; do | |
t=$(curl --connect-timeout 3 --max-time 5 --socks5-hostname 127.0.0.1:1080 "$url" -o /dev/null -s -w %{time_total}) | |
if [ $? -ne 0 ]; then # 10s for failed connect | |
t=10 | |
fi | |
total_time=$(echo "$total_time+$t" | bc) | |
log "url = $url, t=$t, total_time=$total_time" | |
done | |
echo -n $total_time | |
} | |
if [ "X$1" == "Xstop" ]; then | |
stop_daemon | |
exit $? | |
fi | |
cat /dev/null > $RESULT_FILE | |
for line in ${SERVER_PORTS[@]};do | |
stop_daemon | |
IFS=$':' read -r SERVER_IP SERVER_PORT <<< "$line" | |
sslocal -d start -s ${SERVER_IP} -p ${SERVER_PORT} $ARGS | |
log "checking $SERVER_IP ..." | |
TOTAL_TIME=$(test_resource) | |
echo "$SERVER_IP $SERVER_PORT $TOTAL_TIME" | tee -a $RESULT_FILE | |
done | |
stop_daemon | |
read -r BEST_IP BEST_PORT BEST_TIME <<< $(sort $RESULT_FILE -k 3 -n | head -n 1) | |
echo "choosing the best server $BEST_IP:$BEST_PORT ... best time: $BEST_TIME" | |
sslocal -d start -s ${BEST_IP} -p ${BEST_PORT} $ARGS |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment