上次写了一个单用户的脚本可以修改配置文件的密码并重启服务,这次在上次脚本的基础上做了些改进,对多用户版的配置文件进行修改。
脚本结构依旧和单用户的相同,只是加入了端口的选择和将密码输出到外部API的功能。端口选择可以通过参数修改指定端口的密码,外部API可以放在Web目录让其他外部程序调用,也可以放在内部位置只供本地应用调用。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
#!/bin/bash #================================================================ # Script for Shadowsocks Multi User Password Renewing on Ubuntu/Debian # @author minirplus # Usage: ./ss-password-renew.sh [port] # More info: https://blog.minirplus.com/5817/ #================================================================ config_path='/etc/shadowsocks-multiuser.json' port=$1 public_api_path='/var/www/public-api-'"$port"'.json' # verify if port is define if [ -z "$port" ] ; then echo "[ERROR] you must provide the port for renew the password." exit 1; fi # get old password old_password=$(grep "$port" "$config_path") password_index=$(expr index "$old_password" :) old_password=${old_password:password_index} old_password=${old_password%%,*} old_password=${old_password// /} old_password=${old_password:1:$((${#old_password}-2))} echo "your old password is $old_password" # generate new password new_password=$RANDOM echo "your new password is $new_password" # replace old password to new password echo "replace new password $new_password to the shadowsocks config file on port $port..." sed -i 's/'"$old_password"'/'"$new_password"'/g' "$config_path" # update public api file echo "update public api file $public_api_path..." now=$(date +"%Y-%m-%d %T") echo '{"update": "'"$now"'", "port": "'"$port"'", "password": "'"$new_password"'"}' | tee "$public_api_path" # restart shadowsocks service echo "Restarting shadowsocks service..." /usr/local/bin/ssserver -c "$config_path" -d restart # All Done! echo "All Done!" echo "You can now use new password $new_password on port $port to login shadowsocks service now!" |
运行结果
1 2 3 4 5 6 7 8 9 10 11 12 13 |
root@vps:/usr/local/sbin# change-shadowsocks-password-multiusers 8382 your old password is 32522 your new password is 26378 replace new password 26378 to the shadowsocks config file on port 8382... update public api file /var/www/public-api-8382.json... {"update": "2016-02-15 23:31:59", "port": "8382", "password": "26378"} Restarting shadowsocks service... INFO: loading config from /etc/shadowsocks-multiuser.json 2016-02-15 23:31:59 INFO loading libcrypto from libcrypto.so.1.0.0 stopped started All Done! You can now use new password 26378 on port 8382 to login shadowsocks service now! |
配置文件
1 2 3 4 5 6 7 8 9 10 11 |
{ "server": "0.0.0.0", "port_password": { "8381": "foobar1", "8382": "26378", "8383": "foobar3", "8384": "foobar4" }, "timeout": 300, "method": "aes-256-cfb" } |
API文件
1 2 3 4 5 |
{ update: "2016-02-15 23:30:57", port: "8382", password: "26378" } |
说明
脚本开头三行中的第一行变量config_path是shadowsocks配置文件位置,第三行变量public_api_path是公开的API文件位置,以上两个位置都需要修改成有效的路径,公开API文件如不存在会自动新建。
1 2 3 |
config_path='/etc/shadowsocks-multiuser.json' port=$1 public_api_path='/var/www/public-api-'"$port"'.json' |
使用方法
1.在/usr/local/sbin中新建文件ss-password-renew.sh
1 |
nano /usr/local/sbin/ss-password-renew.sh |
将最顶上的bash代码黏贴到该文件中
2.修改ss-renew.sh中的配置文件地址和API地址为有效路径
3.在shell中运行命令,例如修改监听8382端口的密码
1 2 |
cd /usr/local/sbin/ ss-password-renew.sh 8382 |
4.访问之前设定的API地址,查看是否成功生成API文件
5.设定cron定时程序,定时更新密码
打开定时设置
1 |
crontab -e |
例如添加每天6点更新shadowsocks密码
1 2 |
# Daily 0 6 * * * /usr/local/sbin/ss-password-renew.sh >> /var/log/ss-password-renew.log |
5.All Done!
应用
配合定时任务,可以创建公开shadowsocks服务,因为密码每天定时更新,所以可以引导用户访问页面
如何使用公开API
例如公开API地址为:https://blog.minirplus.com/public-api-8382.json(Demo地址,请勿当真)
使用的代码如下
1 2 3 4 5 6 7 8 9 10 11 12 |
Password will renew on 6:00 every single day. <?php $json = file_get_contents('public-api-8382.json'); $ss_json = json_decode($json); echo 'Server: blog.minirplus.com'; echo '<br>'; echo 'Port: '.$ss_json->port; echo '<br>'; echo 'Password: '.$ss_json->password; echo '<br>'; echo 'Last Update: '.$ss_json->update; ?> |
WordPress中配合PHP Code Widget插件可以在Widget中直接插入上方代码,在页面边栏显示公开API内容
4 comments