In this version, some new features are added. The main feature added in this version is called renew port number function. Now it can not only renew password but also can renew the port number.
The QR Code api is also updated to added the port number input function for generate the new QR Code image file.
New Features in this version
- auto get port number in config file
- verify the new generated port number if is in use
- auto add new ufw rules, and delete old rules
The running result
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 |
root@vps:~# /usr/local/sbin/change-shadowsocks-password-port-multiuser 6 ====================Renew Shadowsocks Password and Port=========================== Time: 2016-02-23 06:00:01 line number you entered is 6 your config file on line 6 is "23735": "4919", your old port on config file on line 6 is 23735 your old password on config file on line 6 is 4919 check if new port 8736 is in use port 8736 is not in use the new port will be 8736 generate a new password 27645 replacing new password 27645 to the shadowsocks config... replacing new port 8736 to the shadowsocks config file... updating public api file /var/www/public-api.json... {"update": "2016-02-23 06:00:01", "port": "8736", "password": "27645"} updating ufw rules... enabling new rule on port 8736... Rule added Rule added (v6) disabling old rule on port 23735... Rule deleted Rule deleted (v6) Restarting shadowsocks service... stopped All Done! You can use password 27645 on port 8736 to login shadowsocks service now! |
Code Review
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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
#!/bin/bash #================================================================ # Script for Shadowsocks Multi User Password and Port Renewing on Ubuntu/Debian # @author minirplus # Usage: ./ss-renew.sh [line number you want to change on the config file] # More info: https://blog.minirplus.com/5965/ #================================================================ config_path='/etc/shadowsocks-multiuser.json' line_num=$1 public_api_path='/var/www/public-api.json' now=$(date +"%Y-%m-%d %T") # echo the script title echo "====================Renew Shadowsocks Password and Port===========================" echo "Time: $now" # verify if port number is defined if [ -z "$line_num" ] ; then echo "[ERROR] you must provide the correct line number to renew the port and password." exit 1; fi echo "line number you entered is $line_num" # get old port port_temp=$(sed -n "$line_num"p "$config_path") echo "your config file on line $line_num is $port_temp" port_temp_index=$(expr index "$port_temp" :) port_temp=${port_temp:1:port_temp_index} port_temp=${port_temp// /} port_temp=${port_temp%%:*} port_temp=${port_temp:1:$((${#port_temp}-2))} old_port=$port_temp echo "your old port on config file on line $line_num is $old_port" # get old password old_password=$(grep "$old_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 on config file on line $line_num is $old_password" # generate and verify the new port new_port=$(($RANDOM+1024)) echo "check if new port $new_port is in use" grep_port=`netstat -tlpn | grep "\b$new_port\b"` if [ -n "$grep_port" ] then echo "port $new_port is in use" exit 1 else echo "port $new_port is not in use" fi echo "the new port will be $new_port" # generate a new password new_password=$RANDOM echo "generate a new password $new_password" # replace old password to new password echo "replacing new password $new_password to the shadowsocks config..." sed -i 's/'"$old_password"'/'"$new_password"'/g' "$config_path" # replace old port to new port echo "replacing new port $new_port to the shadowsocks config file..." sed -i 's/'"$old_port"'/'"$new_port"'/g' "$config_path" # update public api file echo "updating public api file $public_api_path..." echo '{"update": "'"$now"'", "port": "'"$new_port"'", "password": "'"$new_password"'"}' | tee "$public_api_path" # insert new ufw rule and delete old ufw rule echo "updating ufw rules..." echo "enabling new rule on port $new_port..." /usr/sbin/ufw allow "$new_port" echo "disabling old rule on port $old_port..." /usr/sbin/ufw delete allow "$old_port" # restart shadowsocks service echo "Restarting shadowsocks service..." /usr/local/bin/ssserver -c "$config_path" -d restart # All Done! echo "All Done!" echo "You can use password $new_password on port $new_port to login shadowsocks service now!" |
Functions bellow will be added on the next update
- Verify if the input line number is correct
- Auto regenerate a new port when detected the target port is in use
There are no comments yet