xray-script.sh 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. #!/bin/bash
  2. function add-user() {
  3. clear
  4. echo -e "Add Xray User"
  5. echo -e "-------------"
  6. read -p "Username : " user
  7. if grep -qw "$user" /iriszz/xray/xray-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/xray/xray-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/xray/domain)
  21. email=${user}@${domain}
  22. echo -e "${user}\t${uuid}\t${exp}" >> /iriszz/xray/xray-clients.txt
  23. cat /usr/local/etc/xray/config.json | jq '.inbounds[0].settings.clients += [{"id": "'${uuid}'","flow": "xtls-rprx-direct","email": "'${email}'"}]' > /usr/local/etc/xray/config_tmp.json
  24. mv -f /usr/local/etc/xray/config_tmp.json /usr/local/etc/xray/config.json
  25. cat /usr/local/etc/xray/config.json | jq '.inbounds[1].settings.clients += [{"id": "'${uuid}'","email": "'${email}'"}]' > /usr/local/etc/xray/config_tmp.json
  26. mv -f /usr/local/etc/xray/config_tmp.json /usr/local/etc/xray/config.json
  27. service xray restart
  28. clear
  29. echo -e "Xray User Information"
  30. echo -e "---------------------"
  31. echo -e "Username : $user"
  32. echo -e "Expired date : $expired"
  33. echo -e ""
  34. }
  35. function delete-user() {
  36. clear
  37. echo -e "Delete Xray User"
  38. echo -e "----------------"
  39. read -p "Username : " user
  40. echo -e ""
  41. if ! grep -qw "$user" /iriszz/xray/xray-clients.txt; then
  42. echo -e ""
  43. echo -e "User '$user' does not exist."
  44. echo -e ""
  45. exit 0
  46. fi
  47. uuid="$(cat /iriszz/xray/xray-clients.txt | grep -w "$user" | awk '{print $2}')"
  48. cat /usr/local/etc/xray/config.json | jq 'del(.inbounds[0].settings.clients[] | select(.id == "'${uuid}'"))' > /usr/local/etc/xray/config_tmp.json
  49. mv -f /usr/local/etc/xray/config_tmp.json /usr/local/etc/xray/config.json
  50. cat /usr/local/etc/xray/config.json | jq 'del(.inbounds[1].settings.clients[] | select(.id == "'${uuid}'"))' > /usr/local/etc/xray/config_tmp.json
  51. mv -f /usr/local/etc/xray/config_tmp.json /usr/local/etc/xray/config.json
  52. sed -i "/\b$user\b/d" /iriszz/xray/xray-clients.txt
  53. service xray restart
  54. echo -e "User '$user' deleted successfully."
  55. echo -e ""
  56. }
  57. function extend-user() {
  58. clear
  59. echo -e "Extend Xray User"
  60. echo -e "----------------"
  61. read -p "Username : " user
  62. if ! grep -qw "$user" /iriszz/xray/xray-clients.txt; then
  63. echo -e ""
  64. echo -e "User '$user' does not exist."
  65. echo -e ""
  66. exit 0
  67. fi
  68. read -p "Duration (day) : " extend
  69. uuid=$(cat /iriszz/xray/xray-clients.txt | grep -w $user | awk '{print $2}')
  70. exp_old=$(cat /iriszz/xray/xray-clients.txt | grep -w $user | awk '{print $3}')
  71. diff=$((($(date -d "${exp_old}" +%s)-$(date +%s))/(86400)))
  72. duration=$(expr $diff + $extend + 1)
  73. exp_new=$(date -d +${duration}days +%Y-%m-%d)
  74. exp=$(date -d "${exp_new}" +"%d %b %Y")
  75. sed -i "/\b$user\b/d" /iriszz/xray/xray-clients.txt
  76. echo -e "$user\t$uuid\t$exp_new" >> /iriszz/xray/xray-clients.txt
  77. clear
  78. echo -e "Xray User Information"
  79. echo -e "---------------------"
  80. echo -e "Username : $user"
  81. echo -e "Expired date : $exp"
  82. echo -e ""
  83. }
  84. function user-list() {
  85. clear
  86. echo -e ""
  87. echo -e "==============================="
  88. echo -e "Username Exp. Date"
  89. echo -e "-------------------------------"
  90. while read expired
  91. do
  92. user=$(echo $expired | awk '{print $1}')
  93. exp=$(echo $expired | awk '{print $3}')
  94. exp_date=$(date -d"${exp}" "+%d %b %Y")
  95. printf "%-17s %2s\n" "$user" "$exp_date"
  96. done < /iriszz/xray/xray-clients.txt
  97. total=$(wc -l /iriszz/xray/xray-clients.txt | awk '{print $1}')
  98. echo -e "-------------------------------"
  99. echo -e "Total accounts: $total"
  100. echo -e "==============================="
  101. echo -e ""
  102. }
  103. function user-monitor() {
  104. data=($(cat /iriszz/xray/xray-clients.txt | awk '{print $1}'))
  105. data2=($(netstat -anp | grep ESTABLISHED | grep tcp6 | grep xray | grep -w 443 | awk '{print $5}' | cut -d: -f1 | sort | uniq))
  106. domain=$(cat /usr/local/etc/xray/domain)
  107. clear
  108. echo -e ""
  109. echo -e "========================="
  110. echo -e " Xray Login Monitor"
  111. echo -e "-------------------------"
  112. for user in "${data[@]}"
  113. do
  114. touch /tmp/ipxray.txt
  115. for ip in "${data2[@]}"
  116. do
  117. total=$(cat /var/log/xray/access.log | grep -w ${user}@${domain} | awk '{print $3}' | cut -d: -f1 | grep -w $ip | sort | uniq)
  118. if [[ "$total" == "$ip" ]]; then
  119. echo -e "$total" >> /tmp/ipxray.txt
  120. fi
  121. done
  122. total=$(cat /tmp/ipxray.txt)
  123. if [[ -n "$total" ]]; then
  124. total2=$(cat /tmp/ipxray.txt | nl)
  125. echo -e "$user :"
  126. echo -e "$total2"
  127. fi
  128. rm -f /tmp/ipxray.txt
  129. done
  130. echo -e "========================="
  131. echo -e ""
  132. }
  133. function show-config() {
  134. echo -e "Xray Config"
  135. echo -e "-----------"
  136. read -p "User : " user
  137. if ! grep -qw "$user" /iriszz/xray/xray-clients.txt; then
  138. echo -e ""
  139. echo -e "User '$user' does not exist."
  140. echo -e ""
  141. exit 0
  142. fi
  143. uuid=$(cat /iriszz/xray/xray-clients.txt | grep -w "$user" | awk '{print $2}')
  144. domain=$(cat /usr/local/etc/xray/domain)
  145. exp=$(cat /iriszz/xray/xray-clients.txt | grep -w "$user" | awk '{print $3}')
  146. exp_date=$(date -d"${exp}" "+%d %b %Y")
  147. echo -e "Expired : $exp_date"
  148. echo -e ""
  149. echo -e "VLESS + WebSocket + TLS"
  150. echo -e "-----------------------"
  151. echo -e "Host : $domain"
  152. echo -e "Server Port : 443"
  153. echo -e "User ID : $uuid"
  154. echo -e "Security : none"
  155. echo -e "Network Type : ws"
  156. echo -e "WebSocket Path : /xray"
  157. echo -e "TLS : tls"
  158. echo -e ""
  159. echo -e "Link : vless://$uuid@$domain:443?type=ws&encryption=none&security=tls&path=%2fxray#XRAY_WS_TLS-$user"
  160. echo -e ""
  161. echo -e "QR : https://api.qrserver.com/v1/create-qr-code/?size=400x400&data=vless://$uuid@$domain:443?type=ws%26encryption=none%26security=tls%26path=%2fxray%23XRAY_WS_TLS-$user"
  162. echo -e ""
  163. echo -e "VLESS + TLS / XTLS"
  164. echo -e "------------------"
  165. echo -e "Adress: $domain"
  166. echo -e "Port: 443"
  167. echo -e "ID: $uuid"
  168. echo -e "Flow: xtls-rprx-direct"
  169. echo -e "Encryption: none"
  170. echo -e "Network: tcp"
  171. echo -e "Header Type: none"
  172. echo -e "TLS: tls / xtls"
  173. echo -e ""
  174. echo -e "Link : vless://$uuid@$domain:443?security=xtls&encryption=none&flow=xtls-rprx-direct#XRAY_XTLS-$user"
  175. echo -e ""
  176. echo -e "QR : https://api.qrserver.com/v1/create-qr-code/?size=400x400&data=vless://$uuid@$domain:443?security=xtls%26encryption=none%26flow=xtls-rprx-direct%23XRAY_XTLS-$user"
  177. echo -e ""
  178. }
  179. clear
  180. echo -e "==========[ Xray Menu ]=========="
  181. echo -e ""
  182. echo -e " [1] Add Xray user"
  183. echo -e " [2] Delete Xray user"
  184. echo -e " [3] Extend Xray user"
  185. echo -e " [4] Xray user list"
  186. echo -e " [5] Xray user monitor"
  187. echo -e " [6] Show Xray configuration"
  188. echo -e " [7] Exit"
  189. echo -e ""
  190. until [[ ${option} =~ ^[1-7]$ ]]; do
  191. read -rp "Select an option [1-7]: " option
  192. done
  193. case "${option}" in
  194. 1)
  195. add-user
  196. ;;
  197. 2)
  198. delete-user
  199. ;;
  200. 3)
  201. extend-user
  202. ;;
  203. 4)
  204. user-list
  205. ;;
  206. 5)
  207. user-monitor
  208. ;;
  209. 6)
  210. clear
  211. show-config
  212. ;;
  213. 7)
  214. clear
  215. exit 0
  216. ;;
  217. esac