Free ShadowSocks Config Generator

从网上找到几个免费ShadowSocks账号, 因为每6小时更换一次密码, 所以编写脚本定期自动化完成.

workflow

  • 从网页中提取ss账号
    • wget下载页面
    • sed/cut提取数据
    • here-doc模版输出
  • 生成自动化shell脚本
    • uci批处理
    • 重启ss服务
  • 检查shell是否发生变化
    • 是: 执行脚本
    • 否: 不执行脚本

screenshot

# Firefox ▷ Tools ▷ Web Developer ▷ Developer Toolbar
$ screenshot iss.png --selector "#free>.container"

Choose the fastest server:

  • [ ] us1.iss.tf (192.653 ms)
  • [ ] us2.iss.tf (185.275 ms)
  • [x] jp3.iss.tf (71.759 ms)

/root/iss.sh

#!/bin/sh
#
# free shadowsocks config generator
#

cd /tmp

hostip() {  
  nslookup $1 2>/dev/null | awk 'END{print $3}'
}

{
echo 'uci batch << _EOF_'  
wget -qO- 'http://www.ishadowsocks.org/' |  
  sed -n -e '/<section id="free"/,/section>/{/font/d;/<h4>/s/\s*<\/\?h4>\s*//gp}' |
    cut -d : -f 2 |
      sed -e 'N;N;N;s/\n/,/g' |
        while IFS=, read server server_port password method
        do
          cfg=${server%%.*}
cat << _EOF_  
set shadowsocks.$cfg='servers'  
set shadowsocks.$cfg.server='$(hostip $server)'  
set shadowsocks.$cfg.server_port='$server_port'  
set shadowsocks.$cfg.encrypt_method='$method'  
set shadowsocks.$cfg.password='$password'  
set shadowsocks.$cfg.timeout='60'  
set shadowsocks.$cfg.auth='0'  
set shadowsocks.$cfg.auth_enable='0'  
set shadowsocks.$cfg.alias='$cfg'  
_EOF_  
        done
echo '_EOF_'

cat << _EOF_  
#uci set shadowsocks.@transparent_proxy[0].main_server=jp3
#uci set shadowsocks.@transparent_proxy[0].udp_relay_server=same
#uci set shadowsocks.@socks5_proxy[0].server=jp3
#uci set shadowsocks.@port_forward[0].server=jp3
uci commit  
#/etc/init.d/shadowsocks restart
_EOF_  
} > ishadowsocks.sh

chmod +x ishadowsocks.sh

if ! [[ -f ishadowsocks.sh.md5 ]]  
then  
  md5sum ishadowsocks.sh > ishadowsocks.sh.md5
else  
  old_md5=$(cat ishadowsocks.sh.md5)
  md5sum ishadowsocks.sh > ishadowsocks.sh.md5
  new_md5=$(cat ishadowsocks.sh.md5)
  if [[ "$old_md5" != "$new_md5" ]]
  then
    ./ishadowsocks.sh
  fi
fi

If you copy this script, make sure trailing spaces are removed.

/etc/crontabs/root

*/5 * * * * /root/iss.sh
  • 5 */6 * * * is good enough.
  • start/enable cron service.

/tmp/shadowsocks.sh (generated)

uci batch << _EOF_  
set shadowsocks.us1='servers'  
set shadowsocks.us1.server='45.55.14.59'  
set shadowsocks.us1.server_port='443'  
set shadowsocks.us1.encrypt_method='aes-256-cfb'  
set shadowsocks.us1.password='02284051'  
set shadowsocks.us1.timeout='60'  
set shadowsocks.us1.auth='0'  
set shadowsocks.us1.auth_enable='0'  
set shadowsocks.us1.alias='us1'  
set shadowsocks.us2='servers'  
set shadowsocks.us2.server='98.126.219.93'  
set shadowsocks.us2.server_port='8989'  
set shadowsocks.us2.encrypt_method='aes-256-cfb'  
set shadowsocks.us2.password='07821617'  
set shadowsocks.us2.timeout='60'  
set shadowsocks.us2.auth='0'  
set shadowsocks.us2.auth_enable='0'  
set shadowsocks.us2.alias='us2'  
set shadowsocks.jp3='servers'  
set shadowsocks.jp3.server='106.187.101.74'  
set shadowsocks.jp3.server_port='443'  
set shadowsocks.jp3.encrypt_method='aes-256-cfb'  
set shadowsocks.jp3.password='44487243'  
set shadowsocks.jp3.timeout='60'  
set shadowsocks.jp3.auth='0'  
set shadowsocks.jp3.auth_enable='0'  
set shadowsocks.jp3.alias='jp3'  
_EOF_  
#uci set shadowsocks.@transparent_proxy[0].main_server=jp3
#uci set shadowsocks.@transparent_proxy[0].udp_relay_server=same
#uci set shadowsocks.@socks5_proxy[0].server=jp3
#uci set shadowsocks.@port_forward[0].server=jp3
uci commit  
#/etc/init.d/shadowsocks restart

/tmp/shadowsocks.sh.md5 (generated)

0d8c4de9a5281cbed6cc50258b6703f9  ishadowsocks.sh  

/tmp/resolv.conf.auto (upstream dns)

# Interface wwan
nameserver 218.2.135.1  
nameserver 221.228.255.1  

/etc/config/dhcp (config via luci)

config dnsmasq  
           option noresolv '1'
           list server '127.0.0.1#5300'
           list server '/iss.tf/218.2.135.1'
           list server '/ishadowsocks.org/218.2.135.1'
           list server '/iwififree.com/218.2.135.1'