ssrmu.sh 30 KB

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