ssrmu.sh 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  1. #!/bin/bash
  2. red='\e[1;31m'
  3. green='\e[0;32m'
  4. NC='\e[0m'
  5. MYIP=$(wget -qO- icanhazip.com);
  6. echo "Checking VPS"
  7. clear
  8. #PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
  9. export PATH
  10. sh_ver="1.0.26"
  11. filepath=$(cd "$(dirname "$0")"; pwd)
  12. file=$(echo -e "${filepath}"|awk -F "$0" '{print $1}')
  13. ssr_folder="/usr/local/shadowsocksr"
  14. config_file="${ssr_folder}/config.json"
  15. config_user_file="${ssr_folder}/user-config.json"
  16. config_user_api_file="${ssr_folder}/userapiconfig.py"
  17. config_user_mudb_file="${ssr_folder}/mudb.json"
  18. ssr_log_file="${ssr_folder}/ssserver.log"
  19. Libsodiumr_file="/usr/local/lib/libsodium.so"
  20. Libsodiumr_ver_backup="1.0.17"
  21. jq_file="${ssr_folder}/jq"
  22. masaaktif=30
  23. exp=`date -d "$masaaktif days" +"%Y-%m-%d"`
  24. source /etc/os-release
  25. OS=$ID
  26. Green_font_prefix="\033[32m" && Red_font_prefix="\033[31m" && Green_background_prefix="\033[42;37m" && Red_background_prefix="\033[41;37m" && Font_color_suffix="\033[0m"
  27. Info="${Green_font_prefix}[information]${Font_color_suffix}"
  28. Error="${Red_font_prefix}[error]${Font_color_suffix}"
  29. Tip="${Green_font_prefix}[note]${Font_color_suffix}"
  30. Separator_1="——————————————————————————————"
  31. check_pid(){
  32. PID=`ps -ef |grep -v grep | grep server.py |awk '{print $2}'`
  33. }
  34. SSR_installation_status(){
  35. [[ ! -e ${ssr_folder} ]] && echo -e "${Error} ShadowsocksR folder not found, please check! "&& exit 1
  36. }
  37. Add_iptables(){
  38. if [[ ! -z "${ssr_port}" ]]; then
  39. iptables -I INPUT -m state --state NEW -m tcp -p tcp --dport ${ssr_port} -j ACCEPT
  40. iptables -I INPUT -m state --state NEW -m udp -p udp --dport ${ssr_port} -j ACCEPT
  41. ip6tables -I INPUT -m state --state NEW -m tcp -p tcp --dport ${ssr_port} -j ACCEPT
  42. ip6tables -I INPUT -m state --state NEW -m udp -p udp --dport ${ssr_port} -j ACCEPT
  43. fi
  44. }
  45. Del_iptables(){
  46. if [[ ! -z "${port}" ]]; then
  47. iptables -D INPUT -m state --state NEW -m tcp -p tcp --dport ${port} -j ACCEPT
  48. iptables -D INPUT -m state --state NEW -m udp -p udp --dport ${port} -j ACCEPT
  49. ip6tables -D INPUT -m state --state NEW -m tcp -p tcp --dport ${port} -j ACCEPT
  50. ip6tables -D INPUT -m state --state NEW -m udp -p udp --dport ${port} -j ACCEPT
  51. fi
  52. }
  53. Save_iptables(){
  54. if [[ ${OS} == "centos" ]]; then
  55. iptables-save > /etc/sysconfig/iptable
  56. ip6tables-save > /etc/sysconfig/ip6tables
  57. else
  58. iptables-save > /etc/iptables.up.rules
  59. ip6tables-save > /etc/ip6tables.up.rules
  60. fi
  61. }
  62. Set_iptables(){
  63. if [[ ${OS} == "centos" ]]; then
  64. iptables-save > /etc/sysconfig/iptable
  65. ip6tables-save > /etc/sysconfig/ip6tables
  66. chkconfig --level 2345 iptables on
  67. chkconfig --level 2345 ip6tables on
  68. else
  69. iptables-save > /etc/iptables.up.rules
  70. ip6tables-save > /etc/ip6tables.up.rules
  71. echo -e '#!/bin/bash\n/sbin/iptables-restore < /etc/iptables.up.rules\n/sbin/ip6tables-restore < /etc/ip6tables.up.rules' > /etc/network/if-pre-up.d/iptables
  72. chmod +x /etc/network/if-pre-up.d/iptables
  73. fi
  74. }
  75. # 读取 配置信息
  76. Get_IP(){
  77. ip=$(wget -qO- -t1 -T2 ipinfo.io/ip)
  78. if [[ -z "${ip}" ]]; then
  79. ip=$(wget -qO- -t1 -T2 api.ip.sb/ip)
  80. if [[ -z "${ip}" ]]; then
  81. ip=$(wget -qO- -t1 -T2 members.3322.org/dyndns/getip)
  82. if [[ -z "${ip}" ]]; then
  83. ip="VPS_IP"
  84. fi
  85. fi
  86. fi
  87. }
  88. Get_User_info(){
  89. Get_user_port=$1
  90. user_info_get=$(python mujson_mgr.py -l -p "${Get_user_port}")
  91. match_info=$(echo "${user_info_get}"|grep -w "### user ")
  92. if [[ -z "${match_info}" ]]; then
  93. echo -e "${Error} Failed to get user information $ {Green Font Prefix} [Port: $ {ssh port}] $ {Font_color Suffix} " && exit 1
  94. fi
  95. user_name=$(echo "${user_info_get}"|grep -w "user :"|sed 's/[[:space:]]//g'|awk -F ":" '{print $NF}')
  96. port=$(echo "${user_info_get}"|grep -w "port :"|sed 's/[[:space:]]//g'|awk -F ":" '{print $NF}')
  97. password=$(echo "${user_info_get}"|grep -w "passwd :"|sed 's/[[:space:]]//g'|awk -F ":" '{print $NF}')
  98. method=$(echo "${user_info_get}"|grep -w "method :"|sed 's/[[:space:]]//g'|awk -F ":" '{print $NF}')
  99. protocol=$(echo "${user_info_get}"|grep -w "protocol :"|sed 's/[[:space:]]//g'|awk -F ":" '{print $NF}')
  100. protocol_param=$(echo "${user_info_get}"|grep -w "protocol_param :"|sed 's/[[:space:]]//g'|awk -F ":" '{print $NF}')
  101. [[ -z ${protocol_param} ]] && protocol_param="0(无限)"
  102. obfs=$(echo "${user_info_get}"|grep -w "obfs :"|sed 's/[[:space:]]//g'|awk -F ":" '{print $NF}')
  103. #transfer_enable=$(echo "${user_info_get}"|grep -w "transfer_enable :"|sed 's/[[:space:]]//g'|awk -F ":" '{print $NF}'|awk -F "ytes" '{print $1}'|sed 's/KB/ KB/;s/MB/ MB/;s/GB/ GB/;s/TB/ TB/;s/PB/ PB/')
  104. #u=$(echo "${user_info_get}"|grep -w "u :"|sed 's/[[:space:]]//g'|awk -F ":" '{print $NF}')
  105. #d=$(echo "${user_info_get}"|grep -w "d :"|sed 's/[[:space:]]//g'|awk -F ":" '{print $NF}')
  106. forbidden_port=$(echo "${user_info_get}"|grep -w "forbidden_port :"|sed 's/[[:space:]]//g'|awk -F ":" '{print $NF}')
  107. [[ -z ${forbidden_port} ]] && forbidden_port="Allow all"
  108. speed_limit_per_con=$(echo "${user_info_get}"|grep -w "speed_limit_per_con :"|sed 's/[[:space:]]//g'|awk -F ":" '{print $NF}')
  109. speed_limit_per_user=$(echo "${user_info_get}"|grep -w "speed_limit_per_user :"|sed 's/[[:space:]]//g'|awk -F ":" '{print $NF}')
  110. Get_User_transfer "${port}"
  111. }
  112. Get_User_transfer(){
  113. transfer_port=$1
  114. #echo "transfer_port=${transfer_port}"
  115. all_port=$(${jq_file} '.[]|.port' ${config_user_mudb_file})
  116. #echo "all_port=${all_port}"
  117. port_num=$(echo "${all_port}"|grep -nw "${transfer_port}"|awk -F ":" '{print $1}')
  118. #echo "port_num=${port_num}"
  119. port_num_1=$(expr ${port_num} - 1)
  120. #echo "port_num_1=${port_num_1}"
  121. transfer_enable_1=$(${jq_file} ".[${port_num_1}].transfer_enable" ${config_user_mudb_file})
  122. #echo "transfer_enable_1=${transfer_enable_1}"
  123. u_1=$(${jq_file} ".[${port_num_1}].u" ${config_user_mudb_file})
  124. #echo "u_1=${u_1}"
  125. d_1=$(${jq_file} ".[${port_num_1}].d" ${config_user_mudb_file})
  126. #echo "d_1=${d_1}"
  127. transfer_enable_Used_2_1=$(expr ${u_1} + ${d_1})
  128. #echo "transfer_enable_Used_2_1=${transfer_enable_Used_2_1}"
  129. transfer_enable_Used_1=$(expr ${transfer_enable_1} - ${transfer_enable_Used_2_1})
  130. #echo "transfer_enable_Used_1=${transfer_enable_Used_1}"
  131. if [[ ${transfer_enable_1} -lt 1024 ]]; then
  132. transfer_enable="${transfer_enable_1} B"
  133. elif [[ ${transfer_enable_1} -lt 1048576 ]]; then
  134. transfer_enable=$(awk 'BEGIN{printf "%.2f\n",'${transfer_enable_1}'/'1024'}')
  135. transfer_enable="${transfer_enable} KB"
  136. elif [[ ${transfer_enable_1} -lt 1073741824 ]]; then
  137. transfer_enable=$(awk 'BEGIN{printf "%.2f\n",'${transfer_enable_1}'/'1048576'}')
  138. transfer_enable="${transfer_enable} MB"
  139. elif [[ ${transfer_enable_1} -lt 1099511627776 ]]; then
  140. transfer_enable=$(awk 'BEGIN{printf "%.2f\n",'${transfer_enable_1}'/'1073741824'}')
  141. transfer_enable="${transfer_enable} GB"
  142. elif [[ ${transfer_enable_1} -lt 1125899906842624 ]]; then
  143. transfer_enable=$(awk 'BEGIN{printf "%.2f\n",'${transfer_enable_1}'/'1099511627776'}')
  144. transfer_enable="${transfer_enable} TB"
  145. fi
  146. #echo "transfer_enable=${transfer_enable}"
  147. if [[ ${u_1} -lt 1024 ]]; then
  148. u="${u_1} B"
  149. elif [[ ${u_1} -lt 1048576 ]]; then
  150. u=$(awk 'BEGIN{printf "%.2f\n",'${u_1}'/'1024'}')
  151. u="${u} KB"
  152. elif [[ ${u_1} -lt 1073741824 ]]; then
  153. u=$(awk 'BEGIN{printf "%.2f\n",'${u_1}'/'1048576'}')
  154. u="${u} MB"
  155. elif [[ ${u_1} -lt 1099511627776 ]]; then
  156. u=$(awk 'BEGIN{printf "%.2f\n",'${u_1}'/'1073741824'}')
  157. u="${u} GB"
  158. elif [[ ${u_1} -lt 1125899906842624 ]]; then
  159. u=$(awk 'BEGIN{printf "%.2f\n",'${u_1}'/'1099511627776'}')
  160. u="${u} TB"
  161. fi
  162. #echo "u=${u}"
  163. if [[ ${d_1} -lt 1024 ]]; then
  164. d="${d_1} B"
  165. elif [[ ${d_1} -lt 1048576 ]]; then
  166. d=$(awk 'BEGIN{printf "%.2f\n",'${d_1}'/'1024'}')
  167. d="${d} KB"
  168. elif [[ ${d_1} -lt 1073741824 ]]; then
  169. d=$(awk 'BEGIN{printf "%.2f\n",'${d_1}'/'1048576'}')
  170. d="${d} MB"
  171. elif [[ ${d_1} -lt 1099511627776 ]]; then
  172. d=$(awk 'BEGIN{printf "%.2f\n",'${d_1}'/'1073741824'}')
  173. d="${d} GB"
  174. elif [[ ${d_1} -lt 1125899906842624 ]]; then
  175. d=$(awk 'BEGIN{printf "%.2f\n",'${d_1}'/'1099511627776'}')
  176. d="${d} TB"
  177. fi
  178. #echo "d=${d}"
  179. if [[ ${transfer_enable_Used_1} -lt 1024 ]]; then
  180. transfer_enable_Used="${transfer_enable_Used_1} B"
  181. elif [[ ${transfer_enable_Used_1} -lt 1048576 ]]; then
  182. transfer_enable_Used=$(awk 'BEGIN{printf "%.2f\n",'${transfer_enable_Used_1}'/'1024'}')
  183. transfer_enable_Used="${transfer_enable_Used} KB"
  184. elif [[ ${transfer_enable_Used_1} -lt 1073741824 ]]; then
  185. transfer_enable_Used=$(awk 'BEGIN{printf "%.2f\n",'${transfer_enable_Used_1}'/'1048576'}')
  186. transfer_enable_Used="${transfer_enable_Used} MB"
  187. elif [[ ${transfer_enable_Used_1} -lt 1099511627776 ]]; then
  188. transfer_enable_Used=$(awk 'BEGIN{printf "%.2f\n",'${transfer_enable_Used_1}'/'1073741824'}')
  189. transfer_enable_Used="${transfer_enable_Used} GB"
  190. elif [[ ${transfer_enable_Used_1} -lt 1125899906842624 ]]; then
  191. transfer_enable_Used=$(awk 'BEGIN{printf "%.2f\n",'${transfer_enable_Used_1}'/'1099511627776'}')
  192. transfer_enable_Used="${transfer_enable_Used} TB"
  193. fi
  194. #echo "transfer_enable_Used=${transfer_enable_Used}"
  195. if [[ ${transfer_enable_Used_2_1} -lt 1024 ]]; then
  196. transfer_enable_Used_2="${transfer_enable_Used_2_1} B"
  197. elif [[ ${transfer_enable_Used_2_1} -lt 1048576 ]]; then
  198. transfer_enable_Used_2=$(awk 'BEGIN{printf "%.2f\n",'${transfer_enable_Used_2_1}'/'1024'}')
  199. transfer_enable_Used_2="${transfer_enable_Used_2} KB"
  200. elif [[ ${transfer_enable_Used_2_1} -lt 1073741824 ]]; then
  201. transfer_enable_Used_2=$(awk 'BEGIN{printf "%.2f\n",'${transfer_enable_Used_2_1}'/'1048576'}')
  202. transfer_enable_Used_2="${transfer_enable_Used_2} MB"
  203. elif [[ ${transfer_enable_Used_2_1} -lt 1099511627776 ]]; then
  204. transfer_enable_Used_2=$(awk 'BEGIN{printf "%.2f\n",'${transfer_enable_Used_2_1}'/'1073741824'}')
  205. transfer_enable_Used_2="${transfer_enable_Used_2} GB"
  206. elif [[ ${transfer_enable_Used_2_1} -lt 1125899906842624 ]]; then
  207. transfer_enable_Used_2=$(awk 'BEGIN{printf "%.2f\n",'${transfer_enable_Used_2_1}'/'1099511627776'}')
  208. transfer_enable_Used_2="${transfer_enable_Used_2} TB"
  209. fi
  210. #echo "transfer_enable_Used_2=${transfer_enable_Used_2}"
  211. }
  212. urlsafe_base64(){
  213. date=$(echo -n "$1"|base64|sed ':a;[ $MYIP = $MYIP ];s/\n/ /g;ta'|sed 's/ //g;s/=//g;s/+/-/g;s/\//_/g')
  214. echo -e "${date}"
  215. }
  216. ssr_link_qr(){
  217. SSRprotocol=$(echo ${protocol} | sed 's/_compatible//g')
  218. SSRobfs=$(echo ${obfs} | sed 's/_compatible//g')
  219. SSRPWDbase64=$(urlsafe_base64 "${password}")
  220. SSRbase64=$(urlsafe_base64 "${ip}:${port}:${SSRprotocol}:${method}:${SSRobfs}:${SSRPWDbase64}")
  221. SSRurl="ssr://${SSRbase64}"
  222. ssr_link=" SSR Link : ${Red_font_prefix}${SSRurl}${Font_color_suffix} \n"
  223. }
  224. ss_ssr_determine(){
  225. protocol_suffix=`echo ${protocol} | awk -F "_" '{print $NF}'`
  226. obfs_suffix=`echo ${obfs} | awk -F "_" '{print $NF}'`
  227. ssr_link_qr
  228. }
  229. # Display configuration information
  230. View_User(){
  231. SSR_installation_status
  232. List_port_user
  233. while true
  234. do
  235. echo -e "Please enter the user port to view the account information"
  236. read -e -p "(Default: cancel):" View_user_port
  237. [[ -z "${View_user_port}" ]] && echo -e "cancel..." && exit 1
  238. View_user=$(cat "${config_user_mudb_file}"|grep '"port": '"${View_user_port}"',')
  239. if [[ ! -z ${View_user} ]]; then
  240. Get_User_info "${View_user_port}"
  241. View_User_info
  242. break
  243. else
  244. echo -e "${Error} Please enter the correct port !"
  245. fi
  246. done
  247. }
  248. View_User_info(){
  249. ip=$(cat ${config_user_api_file}|grep "SERVER_PUB_ADDR = "|awk -F "[']" '{print $2}')
  250. [[ -z "${ip}" ]] && Get_IP
  251. ss_ssr_determine
  252. clear && echo "===================================================" && echo
  253. echo -e " User [${user_name}] configuration info:" && echo
  254. echo -e " IP : ${Green_font_prefix}${ip}${Font_color_suffix}"
  255. echo -e " Port : ${Green_font_prefix}${port}${Font_color_suffix}"
  256. echo -e " Password : ${Green_font_prefix}${password}${Font_color_suffix}"
  257. echo -e " Encryption : ${Green_font_prefix}${method}${Font_color_suffix}"
  258. echo -e " Protocol : ${Red_font_prefix}${protocol}${Font_color_suffix}"
  259. echo -e " obfs : ${Red_font_prefix}${obfs}${Font_color_suffix}"
  260. echo -e " Device limit : ${Green_font_prefix}${protocol_param}${Font_color_suffix}"
  261. echo -e "${ssr_link}"
  262. echo && echo "==================================================="
  263. }
  264. # 设置 配置信息
  265. Set_config_enable(){
  266. user_total=$(expr ${user_total} - 1)
  267. for((integer = 0; integer <= ${user_total}; integer++))
  268. do
  269. echo -e "integer=${integer}"
  270. port_jq=$(${jq_file} ".[${integer}].port" "${config_user_mudb_file}")
  271. echo -e "port_jq=${port_jq}"
  272. if [[ "${ssr_port}" == "${port_jq}" ]]; then
  273. enable=$(${jq_file} ".[${integer}].enable" "${config_user_mudb_file}")
  274. echo -e "enable=${enable}"
  275. [[ "${enable}" == "null" ]] && echo -e "${Error} Get the current port[${ssr_port}]Disabled state failed !" && exit 1
  276. ssr_port_num=$(cat "${config_user_mudb_file}"|grep -n '"port": '${ssr_port}','|awk -F ":" '{print $1}')
  277. echo -e "ssr_port_num=${ssr_port_num}"
  278. [[ "${ssr_port_num}" == "null" ]] && echo -e "${Error} Get currentPort[${ssr_port}]Number of rows failed !" && exit 1
  279. ssr_enable_num=$(expr ${ssr_port_num} - 5)
  280. echo -e "ssr_enable_num=${ssr_enable_num}"
  281. break
  282. fi
  283. done
  284. if [[ "${enable}" == "1" ]]; then
  285. echo -e "Port [${ssr_port}] The account status is:${Green_font_prefix}Enabled ${Font_color_suffix} , switch to ${Red_font_prefix}Disabled${Font_color_suffix} ?[Y/n]"
  286. read -e -p "(Default: Y):" ssr_enable_yn
  287. [[ -z "${ssr_enable_yn}" ]] && ssr_enable_yn="y"
  288. if [[ "${ssr_enable_yn}" == [Yy] ]]; then
  289. ssr_enable="0"
  290. else
  291. echo "Cancel..." && exit 0
  292. fi
  293. elif [[ "${enable}" == "0" ]]; then
  294. echo -e "Port [${ssr_port}] The account status is:${Green_font_prefix}Disabled ${Font_color_suffix} , switch to ${Red_font_prefix}Disabled${Font_color_suffix} ?[Y/n]"
  295. read -e -p "(Default: Y):" ssr_enable_yn
  296. [[ -z "${ssr_enable_yn}" ]] && ssr_enable_yn = "y"
  297. if [[ "${ssr_enable_yn}" == [Yy] ]]; then
  298. ssr_enable="1"
  299. else
  300. echo "cancel..." && exit 0
  301. fi
  302. else
  303. echo -e "${Error} The disabled status of the current port varies[${enable}] !" && exit 1
  304. fi
  305. }
  306. Set_user_api_server_pub_addr(){
  307. addr=$1
  308. if [[ "${addr}" == "Modify" ]]; then
  309. server_pub_addr=$(cat ${config_user_api_file}|grep "SERVER_PUB_ADDR = "|awk -F "[']" '{print $2}')
  310. if [[ -z ${server_pub_addr} ]]; then
  311. echo -e "${Error} Failed to obtain the currently configured server IP or domain name!" && exit 1
  312. else
  313. echo -e "${Info} The currently configured server IP or domain name is: ${Green_font_prefix}${server_pub_addr}${Font_color_suffix}"
  314. fi
  315. fi
  316. echo "Please enter the server IP or domain name to be displayed in the user's configuration (when the server has multiple IPs, you can specify the IP or domain name displayed in the user's configuration)"
  317. read -e -p "(Default: Automatic detection of external network IP):" ssr_server_pub_addr
  318. if [[ -z "${ssr_server_pub_addr}" ]]; then
  319. Get_IP
  320. if [[ ${ip} == "VPS_IP" ]]; then
  321. while true
  322. do
  323. read -e -p "${Error} Automatic detection of external network IP failed, please manually enter the server IP or domain name" ssr_server_pub_addr
  324. if [[ -z "$ssr_server_pub_addr" ]]; then
  325. echo -e "${Error} Can not be empty!"
  326. else
  327. break
  328. fi
  329. done
  330. else
  331. ssr_server_pub_addr="${ip}"
  332. fi
  333. fi
  334. echo && echo ${Separator_1} && echo -e " IP or domain name : ${Green_font_prefix}${ssr_server_pub_addr}${Font_color_suffix}" && echo ${Separator_1} && echo
  335. }
  336. Set_config_all(){
  337. lal=$1
  338. if [[ "${lal}" == "Modify" ]]; then
  339. Set_config_password
  340. Set_config_method
  341. Set_config_protocol
  342. Set_config_obfs
  343. Set_config_protocol_param
  344. Set_config_speed_limit_per_con
  345. Set_config_speed_limit_per_user
  346. Set_config_transfer
  347. Set_config_forbid
  348. else
  349. Set_config_user
  350. Set_config_port
  351. Set_config_password
  352. Set_config_method
  353. Set_config_protocol
  354. Set_config_obfs
  355. Set_config_protocol_param
  356. Set_config_speed_limit_per_con
  357. Set_config_speed_limit_per_user
  358. Set_config_transfer
  359. Set_config_forbid
  360. fi
  361. }
  362. Modify_config_password(){
  363. match_edit=$(python mujson_mgr.py -e -p "${ssr_port}" -k "${ssr_password}"|grep -w "edit user ")
  364. if [[ -z "${match_edit}" ]]; then
  365. echo -e "${Error} User password modification failed ${Green_font_prefix}[Port: ${ssr_port}]${Font_color_suffix} " && exit 1
  366. else
  367. echo -e "${Info} User password modified successfully ${Green_font_prefix}[Port: ${ssr_port}]${Font_color_suffix} (It may take about 10 seconds to apply the latest configuration)"
  368. fi
  369. }
  370. Modify_config_enable(){
  371. sed -i "${ssr_enable_num}"'s/"enable": '"$(echo ${enable})"',/"enable": '"$(echo ${ssr_enable})"',/' ${config_user_mudb_file}
  372. }
  373. Modify_user_api_server_pub_addr(){
  374. sed -i "s/SERVER_PUB_ADDR = '${server_pub_addr}'/SERVER_PUB_ADDR = '${ssr_server_pub_addr}'/" ${config_user_api_file}
  375. }
  376. centos_View_user_connection_info(){
  377. format_1=$1
  378. user_info=$(python mujson_mgr.py -l)
  379. user_total=$(echo "${user_info}"|wc -l)
  380. [[ -z ${user_info} ]] && echo -e "${Error} Didn't find the user, please check again !" && exit 1
  381. IP_total=`netstat -anp |grep 'ESTABLISHED' |grep 'python' |grep 'tcp' | grep '::ffff:' |awk '{print $5}' |awk -F ":" '{print $4}' |sort -u |wc -l`
  382. user_list_all=""
  383. for((integer = 1; integer <= ${user_total}; integer++))
  384. do
  385. user_port=$(echo "${user_info}"|sed -n "${integer}p"|awk '{print $4}')
  386. user_IP_1=`netstat -anp |grep 'ESTABLISHED' |grep 'python' |grep 'tcp' |grep ":${user_port} "|grep '::ffff:' |awk '{print $5}' |awk -F ":" '{print $4}' |sort -u`
  387. if [[ -z ${user_IP_1} ]]; then
  388. user_IP_total="0"
  389. else
  390. user_IP_total=`echo -e "${user_IP_1}"|wc -l`
  391. if [[ ${format_1} == "IP_address" ]]; then
  392. get_IP_address
  393. else
  394. user_IP=`echo -e "\n${user_IP_1}"`
  395. fi
  396. fi
  397. user_list_all=${user_list_all}"Port: ${Green_font_prefix}"${user_port}"${Font_color_suffix}, The total number of linked IPs: ${Green_font_prefix}"${user_IP_total}"${Font_color_suffix}, Current linked IP: ${Green_font_prefix}${user_IP}${Font_color_suffix}\n"
  398. user_IP=""
  399. done
  400. echo -e "The total number of users: ${Green_background_prefix} "${user_total}" ${Font_color_suffix} ,The total number of linked IPs: ${Green_background_prefix} "${IP_total}" ${Font_color_suffix} "
  401. echo -e "${user_list_all}"
  402. }
  403. debian_View_user_connection_info(){
  404. format_1=$1
  405. user_info=$(python mujson_mgr.py -l)
  406. user_total=$(echo "${user_info}"|wc -l)
  407. [[ -z ${user_info} ]] && echo -e "${Error} Didn't find the user, please check again! " && exit 1
  408. IP_total=`netstat -anp |grep 'ESTABLISHED' |grep 'python' |grep 'tcp6' |awk '{print $5}' |awk -F ":" '{print $1}' |sort -u |wc -l`
  409. user_list_all=""
  410. for((integer = 1; integer <= ${user_total}; integer++))
  411. do
  412. user_port=$(echo "${user_info}"|sed -n "${integer}p"|awk '{print $4}')
  413. user_IP_1=`netstat -anp |grep 'ESTABLISHED' |grep 'python' |grep 'tcp6' |grep ":${user_port} " |awk '{print $5}' |awk -F ":" '{print $1}' |sort -u`
  414. if [[ -z ${user_IP_1} ]]; then
  415. user_IP_total="0"
  416. else
  417. user_IP_total=`echo -e "${user_IP_1}"|wc -l`
  418. if [[ ${format_1} == "IP_address" ]]; then
  419. get_IP_address
  420. else
  421. user_IP=`echo -e "\n${user_IP_1}"`
  422. fi
  423. fi
  424. user_list_all=${user_list_all}"Port: ${Green_font_prefix}"${user_port}"${Font_color_suffix}, The total number of linked IPs: ${Green_font_prefix}"${user_IP_total}"${Font_color_suffix}, Current linked IP: ${Green_font_prefix}${user_IP}${Font_color_suffix}\n"
  425. user_IP=""
  426. done
  427. echo -e "The total number of users: ${Green_background_prefix} "${user_total}" ${Font_color_suffix} ,The total number of linked IPs: ${Green_background_prefix} "${IP_total}" ${Font_color_suffix} "
  428. echo -e "${user_list_all}"
  429. }
  430. View_user_connection_info(){
  431. SSR_installation_status
  432. echo && echo -e "Please select the format to display:
  433. ${Green_font_prefix}1.${Font_color_suffix} display IP
  434. ${Green_font_prefix}2.${Font_color_suffix} display IP+Resolve the DNS name " && echo
  435. read -e -p "(Default: 1):" ssr_connection_info
  436. [[ -z "${ssr_connection_info}" ]] && ssr_connection_info="1"
  437. if [[ ${ssr_connection_info} == "1" ]]; then
  438. View_user_connection_info_1 ""
  439. elif [[ ${ssr_connection_info} == "2" ]]; then
  440. echo -e "${Tip} Detect IP (ipip.net),it can take longer time if there are many IPs"
  441. View_user_connection_info_1 "IP_address"
  442. else
  443. echo -e "${Error} Please enter the correct number(1-2)" && exit 1
  444. fi
  445. }
  446. View_user_connection_info_1(){
  447. format=$1
  448. if [[ ${release} = "centos" ]]; then
  449. cat /etc/redhat-release |grep 7\..*|grep -i centos>/dev/null
  450. if [[ $? = 0 ]]; then
  451. debian_View_user_connection_info "$format"
  452. else
  453. centos_View_user_connection_info "$format"
  454. fi
  455. else
  456. debian_View_user_connection_info "$format"
  457. fi
  458. }
  459. get_IP_address(){
  460. if [[ ! -z ${user_IP_1} ]]; then
  461. for((integer_1 = ${user_IP_total}; integer_1 >= 1; integer_1--))
  462. do
  463. IP=`echo "${user_IP_1}" |sed -n "$integer_1"p`
  464. IP_address=`wget -qO- -t1 -T2 http://freeapi.ipip.net/${IP}|sed 's/\"//g;s/,//g;s/\[//g;s/\]//g'`
  465. user_IP="${user_IP}\n${IP}(${IP_address})"
  466. sleep 1s
  467. done
  468. fi
  469. }
  470. List_port_user(){
  471. user_info=$(python mujson_mgr.py -l)
  472. user_total=$(echo "${user_info}"|wc -l)
  473. [[ -z ${user_info} ]] && echo -e "${Error} Didn't find the user, please check again! " && exit 1
  474. user_list_all=""
  475. for((integer = 1; integer <= ${user_total}; integer++))
  476. do
  477. user_port=$(echo "${user_info}"|sed -n "${integer}p"|awk '{print $4}')
  478. user_username=$(echo "${user_info}"|sed -n "${integer}p"|awk '{print $2}'|sed 's/\[//g;s/\]//g')
  479. Get_User_transfer "${user_port}"
  480. user_list_all=${user_list_all}"Username: ${Green_font_prefix} "${user_username}"${Font_color_suffix} Port: ${Green_font_prefix}"${user_port}"${Font_color_suffix} Traffic usage (used + remaining = total): ${Green_font_prefix}${transfer_enable_Used_2}${Font_color_suffix} + ${Green_font_prefix}${transfer_enable_Used}${Font_color_suffix} = ${Green_font_prefix}${transfer_enable}${Font_color_suffix}\n"
  481. done
  482. echo && echo -e "=== Total of all users ${Green_background_prefix} "${user_total}" ${Font_color_suffix}"
  483. echo -e ${user_list_all}
  484. }
  485. Manually_Modify_Config(){
  486. SSR_installation_status
  487. nano ${config_user_mudb_file}
  488. echo "Do you want to restart ShadowsocksR? [Y/n]" && echo
  489. read -e -p "(Default: y):" yn
  490. [[ -z ${yn} ]] && yn="y"
  491. if [[ ${yn} == [Yy] ]]; then
  492. Restart_SSR
  493. fi
  494. }
  495. Clear_transfer(){
  496. SSR_installation_status
  497. echo && echo -e "What do you want to do?
  498. ${Green_font_prefix}1.${Font_color_suffix} Clear single user traffic
  499. ${Green_font_prefix}2.${Font_color_suffix} Clear all user traffic (irreparable)
  500. ${Green_font_prefix}3.${Font_color_suffix} All user traffic is cleared on startup
  501. ${Green_font_prefix}4.${Font_color_suffix} Stop timing all user traffic
  502. ${Green_font_prefix}5.${Font_color_suffix} Modify the timing of all user traffic" && echo
  503. read -e -p "(Default: batal):" ssr_modify
  504. [[ -z "${ssr_modify}" ]] && echo "Dibatalkan..." && exit 1
  505. if [[ ${ssr_modify} == "1" ]]; then
  506. Clear_transfer_one
  507. elif [[ ${ssr_modify} == "2" ]]; then
  508. echo "Are you sure you want to clear all user traffic[y/N]" && echo
  509. read -e -p "(Default: n):" yn
  510. [[ -z ${yn} ]] && yn="n"
  511. if [[ ${yn} == [Yy] ]]; then
  512. Clear_transfer_all
  513. else
  514. echo "cancel..."
  515. fi
  516. elif [[ ${ssr_modify} == "3" ]]; then
  517. check_crontab
  518. Set_crontab
  519. Clear_transfer_all_cron_start
  520. elif [[ ${ssr_modify} == "4" ]]; then
  521. check_crontab
  522. Clear_transfer_all_cron_stop
  523. elif [[ ${ssr_modify} == "5" ]]; then
  524. check_crontab
  525. Clear_transfer_all_cron_modify
  526. else
  527. echo -e "${Error} Please enter the correct number (1-5) " && exit 1
  528. fi
  529. }
  530. Clear_transfer_one(){
  531. List_port_user
  532. while true
  533. do
  534. echo -e "Please enter the port of the user who wants to reset the data"
  535. read -e -p "(Default: batal):" Clear_transfer_user_port
  536. [[ -z "${Clear_transfer_user_port}" ]] && echo -e "Dibatalkan..." && exit 1
  537. Clear_transfer_user=$(cat "${config_user_mudb_file}"|grep '"port": '"${Clear_transfer_user_port}"',')
  538. if [[ ! -z ${Clear_transfer_user} ]]; then
  539. match_clear=$(python mujson_mgr.py -c -p "${Clear_transfer_user_port}"|grep -w "clear user ")
  540. if [[ -z "${match_clear}" ]]; then
  541. echo -e "${Error} Failed to reset data used by the user ${Green_font_prefix}[Port: ${Clear_transfer_user_port}]${Font_color_suffix} "
  542. else
  543. echo -e "${Info} Successfully reseted user-used data${Green_font_prefix}[Port: ${Clear_transfer_user_port}]${Font_color_suffix} "
  544. fi
  545. break
  546. else
  547. echo -e "${Error} Please enter the correct Port!"
  548. fi
  549. done
  550. }
  551. Clear_transfer_all(){
  552. cd "${ssr_folder}"
  553. user_info=$(python mujson_mgr.py -l)
  554. user_total=$(echo "${user_info}"|wc -l)
  555. [[ -z ${user_info} ]] && echo -e "${Error} No users were found, please check!" && exit 1
  556. for((integer = 1; integer <= ${user_total}; integer++))
  557. do
  558. user_port=$(echo "${user_info}"|sed -n "${integer}p"|awk '{print $4}')
  559. match_clear=$(python mujson_mgr.py -c -p "${user_port}"|grep -w "clear user ")
  560. if [[ -z "${match_clear}" ]]; then
  561. echo -e "${Error} Failed to reset data used by the user ${Green_font_prefix}[Port: ${user_port}]${Font_color_suffix} "
  562. else
  563. echo -e "${Info} The user has successfully reset traffic ${Green_font_prefix}[Port: ${user_port}]${Font_color_suffix} "
  564. fi
  565. done
  566. echo -e "${Info} All user traffic is removed! "
  567. }
  568. Clear_transfer_all_cron_start(){
  569. crontab -l > "$file/crontab.bak"
  570. sed -i "/ssrmu.sh/d" "$file/crontab.bak"
  571. echo -e "\n${Crontab_time} /bin/bash $file/ssrmu.sh clearall" >> "$file/crontab.bak"
  572. crontab "$file/crontab.bak"
  573. rm -r "$file/crontab.bak"
  574. cron_config=$(crontab -l | grep "ssrmu.sh")
  575. if [[ -z ${cron_config} ]]; then
  576. echo -e "${Error} Failed to reset all user traffic at normal time!" && exit 1
  577. else
  578. echo -e "${Info} All user traffic is cleared and it starts successfully!"
  579. fi
  580. }
  581. Clear_transfer_all_cron_stop(){
  582. crontab -l > "$file/crontab.bak"
  583. sed -i "/ssrmu.sh/d" "$file/crontab.bak"
  584. crontab "$file/crontab.bak"
  585. rm -r "$file/crontab.bak"
  586. cron_config=$(crontab -l | grep "ssrmu.sh")
  587. if [[ ! -z ${cron_config} ]]; then
  588. echo -e "${Error} Fail to clear all user traffic regularly to quit!" && exit 1
  589. else
  590. echo -e "${Info}All user traffic is cleaned regularly and stopped successfully!"
  591. fi
  592. }
  593. Clear_transfer_all_cron_modify(){
  594. Set_crontab
  595. Clear_transfer_all_cron_stop
  596. Clear_transfer_all_cron_start
  597. }
  598. Set_crontab(){
  599. echo -e "Please enter an interval to clear the flow
  600. === Format Description ===
  601. * * * * * In accordance with the minutes, hours, days, months, weeks
  602. ${Green_font_prefix} 0 2 1 * * ${Font_color_suffix} On behalf of every month at 2 o'clock on the 1st, clean used traffic
  603. ${Green_font_prefix} 0 2 15 * * ${Font_color_suffix} On the 15th of each month at 2: 0 pm Remove used traffic
  604. ${Green_font_prefix} 0 2 */7 * * ${Font_color_suffix} Used traffic will be cleared at 2 o'clock every 7 days
  605. ${Green_font_prefix} 0 2 * * 0 ${Font_color_suffix} Every Sunday (7) clean the flow used
  606. ${Green_font_prefix} 0 2 * * 3 ${Font_color_suffix} every Wednesday (3) clear the flow used" && echo
  607. read -e -p "(Default: 0 2 1 * * 2 o'clock on the 1st of every month):" Crontab_time
  608. [[ -z "${Crontab_time}" ]] && Crontab_time="0 2 1 * *"
  609. }
  610. Start_SSR(){
  611. SSR_installation_status
  612. check_pid
  613. [[ ! -z ${PID} ]] && echo -e "${Error} ShadowsocksR Running! " && exit 1
  614. /etc/init.d/ssrmu start
  615. }
  616. Stop_SSR(){
  617. SSR_installation_status
  618. check_pid
  619. [[ -z ${PID} ]] && echo -e "${Error} ShadowsocksR Is Not Running" && exit 1
  620. /etc/init.d/ssrmu stop
  621. }
  622. Restart_SSR(){
  623. SSR_installation_status
  624. check_pid
  625. [[ ! -z ${PID} ]] && /etc/init.d/ssrmu stop
  626. /etc/init.d/ssrmu start
  627. }
  628. View_Log(){
  629. SSR_installation_status
  630. [[ ! -e ${ssr_log_file} ]] && echo -e "${Error} The Shadowsocks log file does not exist! " && exit 1
  631. echo && echo -e "${Tip} Click ${Red_font_prefix}Ctrl+C${Font_color_suffix} Stop Displaying logs " && echo
  632. tail -f ${ssr_log_file}
  633. }
  634. Update_Shell(){
  635. echo -e "The current version is [$ {sh_ver}], start checking for the latest version ... "
  636. sh_new_ver=$(wget --no-check-certificate -qO- "https://raw.githubusercontent.com/hybtoy/ssrrmu/master/ssrrmu.sh"|grep 'sh_ver="'|awk -F "=" '{print $NF}'|sed 's/\"//g'|head -1) && sh_new_type="github"
  637. [[ -z ${sh_new_ver} ]] && sh_new_ver=$(wget --no-check-certificate -qO- "https://raw.githubusercontent.com/hybtoy/ssrrmu/master/ssrrmu.sh"|grep 'sh_ver="'|awk -F "=" '{print $NF}'|sed 's/\"//g'|head -1) && sh_new_type="github"
  638. [[ -z ${sh_new_ver} ]] && echo -e "${Error} Failed to detect the latest version! " && exit 0
  639. if [[ ${sh_new_ver} != ${sh_ver} ]]; then
  640. echo -e "Versi baru ditemukan[ ${sh_new_ver} ],Do you want to be updated? [Y/n]"
  641. read -e -p "(default: y):" yn
  642. [[ -z "${yn}" ]] && yn="y"
  643. if [[ ${yn} == [Yy] ]]; then
  644. cd "${file}"
  645. if [[ $sh_new_type == "github" ]]; then
  646. wget -N --no-check-certificate https://raw.githubusercontent.com/hybtoy/ssrrmu/master/ssrrmu.sh && chmod +x ssrrmu.sh
  647. fi
  648. echo -e "The Script Has Been Updated To The Latest Version..."
  649. else
  650. echo && echo "cancel..." && echo
  651. fi
  652. else
  653. echo -e "Currently The Latest Version[ ${sh_new_ver} ] !"
  654. fi
  655. exit 0
  656. }
  657. menu_status(){
  658. if [[ -e ${ssr_folder} ]]; then
  659. check_pid
  660. if [[ ! -z "${PID}" ]]; then
  661. echo -e " Current status: ${Green_font_prefix} Installed${Font_color_suffix} & ${Green_font_prefix} Running${Font_color_suffix}"
  662. else
  663. echo -e " Current status: ${Green_font_prefix} Installed${Font_color_suffix} but ${Red_font_prefix} Not Running${Font_color_suffix}"
  664. fi
  665. cd "${ssr_folder}"
  666. else
  667. echo -e " Current status: ${Red_font_prefix}tidak terinstall${Font_color_suffix}"
  668. fi
  669. }
  670. if [[ "${action}" == "clearall" ]]; then
  671. Clear_transfer_all
  672. elif [[ "${action}" == "monitor" ]]; then
  673. crontab_monitor_ssr
  674. else
  675. menu_status
  676. echo -e " Menu ShadowsocksR
  677. Versi: ${Green_font_prefix}[v${sh_ver}]${Font_color_suffix}
  678. ${Green_font_prefix}1.${Font_color_suffix} Check the account information
  679. ${Green_font_prefix}2.${Font_color_suffix} Display the connection information
  680. ${Green_font_prefix}3.${Font_color_suffix} Change Password User
  681. ${Green_font_prefix}4.${Font_color_suffix} Manually Modify User Configuration
  682. ${Green_font_prefix}5.${Font_color_suffix} Clear The Used Traffic
  683. ${Green_font_prefix}6.${Font_color_suffix} Start ShadowsocksR
  684. ${Green_font_prefix}7.${Font_color_suffix} Stop ShadowsocksR
  685. ${Green_font_prefix}8.${Font_color_suffix} Restart ShadowsocksR
  686. ${Green_font_prefix}9.${Font_color_suffix} Cek ShadowsocksR log
  687. ${Green_font_prefix}10.${Font_color_suffix} Upgrade script
  688. ${Tip} Username And User Port Can Not Be Changed, If You Need To Modify, Use The Script To Manually Change The Function!
  689. "
  690. echo && read -e -p "Please enter the correct number [1-10]:" num
  691. case "$num" in
  692. 1)
  693. View_User
  694. ;;
  695. 2)
  696. View_user_connection_info
  697. ;;
  698. 3)
  699. Modify_port
  700. Set_config_password
  701. Modify_config_password
  702. ;;
  703. 4)
  704. Manually_Modify_Config
  705. ;;
  706. 5)
  707. Clear_transfer
  708. ;;
  709. 6)
  710. Start_SSR
  711. ;;
  712. 7)
  713. Stop_SSR
  714. ;;
  715. 8)
  716. Restart_SSR
  717. ;;
  718. 9)
  719. View_Log
  720. ;;
  721. 10)
  722. Update_Shell
  723. ;;
  724. *)
  725. echo -e "${Error} Please enter the correct number [1-10] "
  726. ;;
  727. esac
  728. fi