v2ray-script.sh 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. #!/bin/bash
  2. function add-user() {
  3. clear
  4. echo -e "Add V2Ray User"
  5. echo -e "--------------"
  6. read -p "Username : " user
  7. if grep -qw "$user" /iriszz/v2ray/v2ray-clients.txt; then
  8. echo -e ""
  9. echo -e "User '$user' already exist."
  10. echo -e ""
  11. exit 0
  12. fi
  13. read -p "Duration (day) : " duration
  14. uuid=$(uuidgen)
  15. while grep -qw "$uuid" /iriszz/v2ray/v2ray-clients.txt; do
  16. uuid=$(uuidgen)
  17. done
  18. exp=$(date -d +${duration}days +%Y-%m-%d)
  19. expired=$(date -d "${exp}" +"%d %b %Y")
  20. domain=$(cat /usr/local/etc/v2ray/domain)
  21. email=${user}@${domain}
  22. echo -e "${user}\t${uuid}\t${exp}" >> /iriszz/v2ray/v2ray-clients.txt
  23. cat /usr/local/etc/v2ray/ws-tls.json | jq '.inbounds[0].settings.clients += [{"id": "'${uuid}'","alterId": 2,"email": "'${email}'"}]' > /usr/local/etc/v2ray/ws-tls_tmp.json
  24. mv -f /usr/local/etc/v2ray/ws-tls_tmp.json /usr/local/etc/v2ray/ws-tls.json
  25. cat /usr/local/etc/v2ray/ws.json | jq '.inbounds[0].settings.clients += [{"id": "'${uuid}'","alterId": 2,"email": "'${email}'"}]' > /usr/local/etc/v2ray/ws_tmp.json
  26. mv -f /usr/local/etc/v2ray/ws_tmp.json /usr/local/etc/v2ray/ws.json
  27. service v2ray@ws-tls restart
  28. service v2ray@ws restart
  29. clear
  30. echo -e "V2Ray User Information"
  31. echo -e "----------------------"
  32. echo -e "Username : $user"
  33. echo -e "Expired date : $expired"
  34. echo -e ""
  35. }
  36. function delete-user() {
  37. clear
  38. echo -e "Delete V2Ray User"
  39. echo -e "-----------------"
  40. read -p "Username : " user
  41. echo -e ""
  42. if ! grep -qw "$user" /iriszz/v2ray/v2ray-clients.txt; then
  43. echo -e ""
  44. echo -e "User '$user' does not exist."
  45. echo -e ""
  46. exit 0
  47. fi
  48. uuid="$(cat /iriszz/v2ray/v2ray-clients.txt | grep -w "$user" | awk '{print $2}')"
  49. cat /usr/local/etc/v2ray/ws-tls.json | jq 'del(.inbounds[0].settings.clients[] | select(.id == "'${uuid}'"))' > /usr/local/etc/v2ray/ws-tls_tmp.json
  50. mv -f /usr/local/etc/v2ray/ws-tls_tmp.json /usr/local/etc/v2ray/ws-tls.json
  51. cat /usr/local/etc/v2ray/ws.json | jq 'del(.inbounds[0].settings.clients[] | select(.id == "'${uuid}'"))' > /usr/local/etc/v2ray/ws_tmp.json
  52. mv -f /usr/local/etc/v2ray/ws_tmp.json /usr/local/etc/v2ray/ws.json
  53. sed -i "/\b$user\b/d" /iriszz/v2ray/v2ray-clients.txt
  54. service v2ray@ws-tls restart
  55. service v2ray@ws restart
  56. echo -e "User '$user' deleted successfully."
  57. echo -e ""
  58. }
  59. function extend-user() {
  60. clear
  61. echo -e "Extend V2Ray User"
  62. echo -e "-----------------"
  63. read -p "Username : " user
  64. if ! grep -qw "$user" /iriszz/v2ray/v2ray-clients.txt; then
  65. echo -e ""
  66. echo -e "User '$user' does not exist."
  67. echo -e ""
  68. exit 0
  69. fi
  70. read -p "Duration (day) : " extend
  71. uuid=$(cat /iriszz/v2ray/v2ray-clients.txt | grep -w $user | awk '{print $2}')
  72. exp_old=$(cat /iriszz/v2ray/v2ray-clients.txt | grep -w $user | awk '{print $3}')
  73. diff=$((($(date -d "${exp_old}" +%s)-$(date +%s))/(86400)))
  74. duration=$(expr $diff + $extend + 1)
  75. exp_new=$(date -d +${duration}days +%Y-%m-%d)
  76. exp=$(date -d "${exp_new}" +"%d %b %Y")
  77. sed -i "/\b$user\b/d" /iriszz/v2ray/v2ray-clients.txt
  78. echo -e "$user\t$uuid\t$exp_new" >> /iriszz/v2ray/v2ray-clients.txt
  79. clear
  80. echo -e "V2Ray User Information"
  81. echo -e "----------------------"
  82. echo -e "Username : $user"
  83. echo -e "Expired date : $exp"
  84. echo -e ""
  85. }
  86. function user-list() {
  87. clear
  88. echo -e ""
  89. echo -e "==============================="
  90. echo -e "Username Exp. Date"
  91. echo -e "-------------------------------"
  92. while read expired
  93. do
  94. user=$(echo $expired | awk '{print $1}')
  95. exp=$(echo $expired | awk '{print $3}')
  96. exp_date=$(date -d"${exp}" "+%d %b %Y")
  97. printf "%-17s %2s\n" "$user" "$exp_date"
  98. done < /iriszz/v2ray/v2ray-clients.txt
  99. total=$(wc -l /iriszz/v2ray/v2ray-clients.txt | awk '{print $1}')
  100. echo -e "-------------------------------"
  101. echo -e "Total accounts: $total"
  102. echo -e "==============================="
  103. echo -e ""
  104. }
  105. function user-monitor() {
  106. data=($(cat /iriszz/v2ray/v2ray-clients.txt | awk '{print $1}'))
  107. data2=($(netstat -anp | grep ESTABLISHED | grep tcp6 | grep v2ray | grep -w '80\|443' | awk '{print $5}' | cut -d: -f1 | sort | uniq))
  108. domain=$(cat /usr/local/etc/v2ray/domain)
  109. clear
  110. echo -e ""
  111. echo -e "========================="
  112. echo -e " V2Ray Login Monitor"
  113. echo -e "-------------------------"
  114. for user in "${data[@]}"
  115. do
  116. touch /tmp/ipv2ray.txt
  117. for ip in "${data2[@]}"
  118. do
  119. total=$(cat /var/log/v2ray/access.log | grep -w ${user}@${domain} | awk '{print $3}' | cut -d: -f1 | grep -w $ip | sort | uniq)
  120. if [[ "$total" == "$ip" ]]; then
  121. echo -e "$total" >> /tmp/ipv2ray.txt
  122. fi
  123. done
  124. total=$(cat /tmp/ipv2ray.txt)
  125. if [[ -n "$total" ]]; then
  126. total2=$(cat /tmp/ipv2ray.txt | nl)
  127. echo -e "$user :"
  128. echo -e "$total2"
  129. fi
  130. rm -f /tmp/ipv2ray.txt
  131. done
  132. echo -e "========================="
  133. echo -e ""
  134. }
  135. function show-config() {
  136. echo -e "V2Ray Config"
  137. echo -e "------------"
  138. read -p "User : " user
  139. if ! grep -qw "$user" /iriszz/v2ray/v2ray-clients.txt; then
  140. echo -e ""
  141. echo -e "User '$user' does not exist."
  142. echo -e ""
  143. exit 0
  144. fi
  145. uuid=$(cat /iriszz/v2ray/v2ray-clients.txt | grep -w "$user" | awk '{print $2}')
  146. domain=$(cat /usr/local/etc/v2ray/domain)
  147. exp=$(cat /iriszz/v2ray/v2ray-clients.txt | grep -w "$user" | awk '{print $3}')
  148. exp_date=$(date -d"${exp}" "+%d %b %Y")
  149. cat> /tmp/v2ray_ws_tls_client.json << END
  150. {
  151. "v": "2",
  152. "ps": "V2RAY_WS_TLS-${user}",
  153. "add": "${domain}",
  154. "port": "443",
  155. "id": "${uuid}",
  156. "aid": "2",
  157. "host": "${domain}",
  158. "path": "/v2ray",
  159. "net": "ws",
  160. "security": "auto",
  161. "tls": "tls",
  162. "sni": "${domain}"
  163. }
  164. END
  165. cat> /tmp/v2ray_ws_client.json << END
  166. {
  167. "v": "2",
  168. "ps": "V2RAY_WS-${user}",
  169. "add": "${domain}",
  170. "port": "80",
  171. "id": "${uuid}",
  172. "aid": "2",
  173. "host": "${domain}",
  174. "path": "/v2ray",
  175. "net": "ws",
  176. "security": "auto"
  177. }
  178. END
  179. vmess_ws_tls="vmess://$(base64 -w 0 /tmp/v2ray_ws_tls_client.json)"
  180. vmess_ws="vmess://$(base64 -w 0 /tmp/v2ray_ws_client.json)"
  181. rm -f /tmp/{v2ray_ws_tls_client.json,v2ray_ws_client.json}
  182. echo -e "Expired : $exp_date"
  183. echo -e ""
  184. echo -e "VMESS + WebSocket + TLS"
  185. echo -e "-----------------------"
  186. echo -e "Address : ${domain}"
  187. echo -e "Port : 443"
  188. echo -e "ID : ${uuid}"
  189. echo -e "alterID : 2"
  190. echo -e "Security : auto"
  191. echo -e "Network : ws"
  192. echo -e "Path : /v2ray"
  193. echo -e "TLS : tls"
  194. echo -e ""
  195. echo -e "Link : $vmess_ws_tls"
  196. echo -e ""
  197. echo -e "QR : https://api.qrserver.com/v1/create-qr-code/?size=400x400&data=$vmess_ws_tls"
  198. echo -e ""
  199. echo -e "VMESS + WebSocket"
  200. echo -e "-----------------"
  201. echo -e "Address : ${domain}"
  202. echo -e "Port : 80"
  203. echo -e "ID : ${uuid}"
  204. echo -e "alterID : 2"
  205. echo -e "Security : auto"
  206. echo -e "Network : ws"
  207. echo -e "Path : /v2ray"
  208. echo -e ""
  209. echo -e "Link : $vmess_ws"
  210. echo -e ""
  211. echo -e "QR : https://api.qrserver.com/v1/create-qr-code/?size=400x400&data=$vmess_ws"
  212. echo -e ""
  213. }
  214. clear
  215. echo -e "=========[ V2Ray Menu ]========="
  216. echo -e ""
  217. echo -e " [1] Add V2Ray user"
  218. echo -e " [2] Delete V2Ray user"
  219. echo -e " [3] Extend V2Ray user"
  220. echo -e " [4] V2Ray user list"
  221. echo -e " [5] V2Ray user monitor"
  222. echo -e " [6] Show V2Ray configuration"
  223. echo -e " [7] Exit"
  224. echo -e ""
  225. until [[ ${option} =~ ^[1-7]$ ]]; do
  226. read -rp "Select an option [1-7]: " option
  227. done
  228. case "${option}" in
  229. 1)
  230. add-user
  231. ;;
  232. 2)
  233. delete-user
  234. ;;
  235. 3)
  236. extend-user
  237. ;;
  238. 4)
  239. user-list
  240. ;;
  241. 5)
  242. user-monitor
  243. ;;
  244. 6)
  245. clear
  246. show-config
  247. ;;
  248. 7)
  249. clear
  250. exit 0
  251. ;;
  252. esac