1
0

x-ui.sh 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033
  1. #!/bin/bash
  2. red='\033[0;31m'
  3. green='\033[0;32m'
  4. yellow='\033[0;33m'
  5. plain='\033[0m'
  6. #Add some basic function here
  7. function LOGD() {
  8. echo -e "${yellow}[DEG] $* ${plain}"
  9. }
  10. function LOGE() {
  11. echo -e "${red}[ERR] $* ${plain}"
  12. }
  13. function LOGI() {
  14. echo -e "${green}[INF] $* ${plain}"
  15. }
  16. # check root
  17. [[ $EUID -ne 0 ]] && LOGE "ERROR: You must be root to run this script! \n" && exit 1
  18. # Check OS and set release variable
  19. if [[ -f /etc/os-release ]]; then
  20. source /etc/os-release
  21. release=$ID
  22. elif [[ -f /usr/lib/os-release ]]; then
  23. source /usr/lib/os-release
  24. release=$ID
  25. else
  26. echo "Failed to check the system OS, please contact the author!" >&2
  27. exit 1
  28. fi
  29. echo "The OS release is: $release"
  30. os_version=""
  31. os_version=$(grep -i version_id /etc/os-release | cut -d \" -f2 | cut -d . -f1)
  32. if [[ "${release}" == "centos" ]]; then
  33. if [[ ${os_version} -lt 8 ]]; then
  34. echo -e "${red} Please use CentOS 8 or higher ${plain}\n" && exit 1
  35. fi
  36. elif [[ "${release}" == "ubuntu" ]]; then
  37. if [[ ${os_version} -lt 20 ]]; then
  38. echo -e "${red}please use Ubuntu 20 or higher version! ${plain}\n" && exit 1
  39. fi
  40. elif [[ "${release}" == "fedora" ]]; then
  41. if [[ ${os_version} -lt 36 ]]; then
  42. echo -e "${red}please use Fedora 36 or higher version! ${plain}\n" && exit 1
  43. fi
  44. elif [[ "${release}" == "debian" ]]; then
  45. if [[ ${os_version} -lt 10 ]]; then
  46. echo -e "${red} Please use Debian 10 or higher ${plain}\n" && exit 1
  47. fi
  48. fi
  49. confirm() {
  50. if [[ $# > 1 ]]; then
  51. echo && read -p "$1 [Default $2]: " temp
  52. if [[ "${temp}" == "" ]]; then
  53. temp=$2
  54. fi
  55. else
  56. read -p "$1 [y/n]: " temp
  57. fi
  58. if [[ "${temp}" == "y" || "${temp}" == "Y" ]]; then
  59. return 0
  60. else
  61. return 1
  62. fi
  63. }
  64. confirm_restart() {
  65. confirm "Restart the panel, Attention: Restarting the panel will also restart xray" "y"
  66. if [[ $? == 0 ]]; then
  67. restart
  68. else
  69. show_menu
  70. fi
  71. }
  72. before_show_menu() {
  73. echo && echo -n -e "${yellow}Press enter to return to the main menu: ${plain}" && read temp
  74. show_menu
  75. }
  76. install() {
  77. bash <(curl -Ls https://raw.githubusercontent.com/MHSanaei/3x-ui/main/install.sh)
  78. if [[ $? == 0 ]]; then
  79. if [[ $# == 0 ]]; then
  80. start
  81. else
  82. start 0
  83. fi
  84. fi
  85. }
  86. update() {
  87. confirm "This function will forcefully reinstall the latest version, and the data will not be lost. Do you want to continue?" "n"
  88. if [[ $? != 0 ]]; then
  89. LOGE "Cancelled"
  90. if [[ $# == 0 ]]; then
  91. before_show_menu
  92. fi
  93. return 0
  94. fi
  95. bash <(curl -Ls https://raw.githubusercontent.com/MHSanaei/3x-ui/main/install.sh)
  96. if [[ $? == 0 ]]; then
  97. LOGI "Update is complete, Panel has automatically restarted "
  98. exit 0
  99. fi
  100. }
  101. uninstall() {
  102. confirm "Are you sure you want to uninstall the panel? xray will also uninstalled!" "n"
  103. if [[ $? != 0 ]]; then
  104. if [[ $# == 0 ]]; then
  105. show_menu
  106. fi
  107. return 0
  108. fi
  109. systemctl stop x-ui
  110. systemctl disable x-ui
  111. rm /etc/systemd/system/x-ui.service -f
  112. systemctl daemon-reload
  113. systemctl reset-failed
  114. rm /etc/x-ui/ -rf
  115. rm /usr/local/x-ui/ -rf
  116. echo ""
  117. echo -e "Uninstalled Successfully, If you want to remove this script, then after exiting the script run ${green}rm /usr/bin/x-ui -f${plain} to delete it."
  118. echo ""
  119. if [[ $# == 0 ]]; then
  120. before_show_menu
  121. fi
  122. }
  123. reset_user() {
  124. confirm "Are you sure to reset the username and password of the panel?" "n"
  125. if [[ $? != 0 ]]; then
  126. if [[ $# == 0 ]]; then
  127. show_menu
  128. fi
  129. return 0
  130. fi
  131. read -rp "Please set the login username [default is a random username]: " config_account
  132. [[ -z $config_account ]] && config_account=$(date +%s%N | md5sum | cut -c 1-8)
  133. read -rp "Please set the login password [default is a random password]: " config_password
  134. [[ -z $config_password ]] && config_password=$(date +%s%N | md5sum | cut -c 1-8)
  135. /usr/local/x-ui/x-ui setting -username ${config_account} -password ${config_password} >/dev/null 2>&1
  136. /usr/local/x-ui/x-ui setting -remove_secret >/dev/null 2>&1
  137. echo -e "Panel login username has been reset to: ${green} ${config_account} ${plain}"
  138. echo -e "Panel login password has been reset to: ${green} ${config_password} ${plain}"
  139. echo -e "${yellow} Panel login secret token disabled ${plain}"
  140. echo -e "${green} Please use the new login username and password to access the X-UI panel. Also remember them! ${plain}"
  141. confirm_restart
  142. }
  143. reset_config() {
  144. confirm "Are you sure you want to reset all panel settings, Account data will not be lost, Username and password will not change" "n"
  145. if [[ $? != 0 ]]; then
  146. if [[ $# == 0 ]]; then
  147. show_menu
  148. fi
  149. return 0
  150. fi
  151. /usr/local/x-ui/x-ui setting -reset
  152. echo -e "All panel settings have been reset to default, Please restart the panel now, and use the default ${green}2053${plain} Port to Access the web Panel"
  153. confirm_restart
  154. }
  155. check_config() {
  156. info=$(/usr/local/x-ui/x-ui setting -show true)
  157. if [[ $? != 0 ]]; then
  158. LOGE "get current settings error, please check logs"
  159. show_menu
  160. fi
  161. LOGI "${info}"
  162. }
  163. set_port() {
  164. echo && echo -n -e "Enter port number[1-65535]: " && read port
  165. if [[ -z "${port}" ]]; then
  166. LOGD "Cancelled"
  167. before_show_menu
  168. else
  169. /usr/local/x-ui/x-ui setting -port ${port}
  170. echo -e "The port is set, Please restart the panel now, and use the new port ${green}${port}${plain} to access web panel"
  171. confirm_restart
  172. fi
  173. }
  174. start() {
  175. check_status
  176. if [[ $? == 0 ]]; then
  177. echo ""
  178. LOGI "Panel is running, No need to start again, If you need to restart, please select restart"
  179. else
  180. systemctl start x-ui
  181. sleep 2
  182. check_status
  183. if [[ $? == 0 ]]; then
  184. LOGI "x-ui Started Successfully"
  185. else
  186. LOGE "panel Failed to start, Probably because it takes longer than two seconds to start, Please check the log information later"
  187. fi
  188. fi
  189. if [[ $# == 0 ]]; then
  190. before_show_menu
  191. fi
  192. }
  193. stop() {
  194. check_status
  195. if [[ $? == 1 ]]; then
  196. echo ""
  197. LOGI "Panel stopped, No need to stop again!"
  198. else
  199. systemctl stop x-ui
  200. sleep 2
  201. check_status
  202. if [[ $? == 1 ]]; then
  203. LOGI "x-ui and xray stopped successfully"
  204. else
  205. LOGE "Panel stop failed, Probably because the stop time exceeds two seconds, Please check the log information later"
  206. fi
  207. fi
  208. if [[ $# == 0 ]]; then
  209. before_show_menu
  210. fi
  211. }
  212. restart() {
  213. systemctl restart x-ui
  214. sleep 2
  215. check_status
  216. if [[ $? == 0 ]]; then
  217. LOGI "x-ui and xray Restarted successfully"
  218. else
  219. LOGE "Panel restart failed, Probably because it takes longer than two seconds to start, Please check the log information later"
  220. fi
  221. if [[ $# == 0 ]]; then
  222. before_show_menu
  223. fi
  224. }
  225. status() {
  226. systemctl status x-ui -l
  227. if [[ $# == 0 ]]; then
  228. before_show_menu
  229. fi
  230. }
  231. enable() {
  232. systemctl enable x-ui
  233. if [[ $? == 0 ]]; then
  234. LOGI "x-ui Set to boot automatically on startup successfully"
  235. else
  236. LOGE "x-ui Failed to set Autostart"
  237. fi
  238. if [[ $# == 0 ]]; then
  239. before_show_menu
  240. fi
  241. }
  242. disable() {
  243. systemctl disable x-ui
  244. if [[ $? == 0 ]]; then
  245. LOGI "x-ui Autostart Cancelled successfully"
  246. else
  247. LOGE "x-ui Failed to cancel autostart"
  248. fi
  249. if [[ $# == 0 ]]; then
  250. before_show_menu
  251. fi
  252. }
  253. show_log() {
  254. journalctl -u x-ui.service -e --no-pager -f
  255. if [[ $# == 0 ]]; then
  256. before_show_menu
  257. fi
  258. }
  259. enable_bbr() {
  260. if grep -q "net.core.default_qdisc=fq" /etc/sysctl.conf && grep -q "net.ipv4.tcp_congestion_control=bbr" /etc/sysctl.conf; then
  261. echo -e "${green}BBR is already enabled!${plain}"
  262. exit 0
  263. fi
  264. # Check the OS and install necessary packages
  265. if [[ "$(cat /etc/os-release | grep -E '^ID=' | awk -F '=' '{print $2}')" == "ubuntu" ]]; then
  266. sudo apt-get update && sudo apt-get install -yqq --no-install-recommends ca-certificates
  267. elif [[ "$(cat /etc/os-release | grep -E '^ID=' | awk -F '=' '{print $2}')" == "debian" ]]; then
  268. sudo apt-get update && sudo apt-get install -yqq --no-install-recommends ca-certificates
  269. elif [[ "$(cat /etc/os-release | grep -E '^ID=' | awk -F '=' '{print $2}')" == "fedora" ]]; then
  270. sudo dnf -y update && sudo dnf -y install ca-certificates
  271. elif [[ "$(cat /etc/os-release | grep -E '^ID=' | awk -F '=' '{print $2}')" == "centos" ]]; then
  272. sudo yum -y update && sudo yum -y install ca-certificates
  273. else
  274. echo "Unsupported operating system. Please check the script and install the necessary packages manually."
  275. exit 1
  276. fi
  277. # Enable BBR
  278. echo "net.core.default_qdisc=fq" | sudo tee -a /etc/sysctl.conf
  279. echo "net.ipv4.tcp_congestion_control=bbr" | sudo tee -a /etc/sysctl.conf
  280. # Apply changes
  281. sudo sysctl -p
  282. # Verify that BBR is enabled
  283. if [[ $(sysctl net.ipv4.tcp_congestion_control | awk '{print $3}') == "bbr" ]]; then
  284. echo -e "${green}BBR has been enabled successfully.${plain}"
  285. else
  286. echo -e "${red}Failed to enable BBR. Please check your system configuration.${plain}"
  287. fi
  288. }
  289. update_shell() {
  290. wget -O /usr/bin/x-ui -N --no-check-certificate https://github.com/MHSanaei/3x-ui/raw/main/x-ui.sh
  291. if [[ $? != 0 ]]; then
  292. echo ""
  293. LOGE "Failed to download script, Please check whether the machine can connect Github"
  294. before_show_menu
  295. else
  296. chmod +x /usr/bin/x-ui
  297. LOGI "Upgrade script succeeded, Please rerun the script" && exit 0
  298. fi
  299. }
  300. # 0: running, 1: not running, 2: not installed
  301. check_status() {
  302. if [[ ! -f /etc/systemd/system/x-ui.service ]]; then
  303. return 2
  304. fi
  305. temp=$(systemctl status x-ui | grep Active | awk '{print $3}' | cut -d "(" -f2 | cut -d ")" -f1)
  306. if [[ "${temp}" == "running" ]]; then
  307. return 0
  308. else
  309. return 1
  310. fi
  311. }
  312. check_enabled() {
  313. temp=$(systemctl is-enabled x-ui)
  314. if [[ "${temp}" == "enabled" ]]; then
  315. return 0
  316. else
  317. return 1
  318. fi
  319. }
  320. check_uninstall() {
  321. check_status
  322. if [[ $? != 2 ]]; then
  323. echo ""
  324. LOGE "Panel installed, Please do not reinstall"
  325. if [[ $# == 0 ]]; then
  326. before_show_menu
  327. fi
  328. return 1
  329. else
  330. return 0
  331. fi
  332. }
  333. check_install() {
  334. check_status
  335. if [[ $? == 2 ]]; then
  336. echo ""
  337. LOGE "Please install the panel first"
  338. if [[ $# == 0 ]]; then
  339. before_show_menu
  340. fi
  341. return 1
  342. else
  343. return 0
  344. fi
  345. }
  346. show_status() {
  347. check_status
  348. case $? in
  349. 0)
  350. echo -e "Panel state: ${green}Running${plain}"
  351. show_enable_status
  352. ;;
  353. 1)
  354. echo -e "Panel state: ${yellow}Not Running${plain}"
  355. show_enable_status
  356. ;;
  357. 2)
  358. echo -e "Panel state: ${red}Not Installed${plain}"
  359. ;;
  360. esac
  361. show_xray_status
  362. }
  363. show_enable_status() {
  364. check_enabled
  365. if [[ $? == 0 ]]; then
  366. echo -e "Start automatically: ${green}Yes${plain}"
  367. else
  368. echo -e "Start automatically: ${red}No${plain}"
  369. fi
  370. }
  371. check_xray_status() {
  372. count=$(ps -ef | grep "xray-linux" | grep -v "grep" | wc -l)
  373. if [[ count -ne 0 ]]; then
  374. return 0
  375. else
  376. return 1
  377. fi
  378. }
  379. show_xray_status() {
  380. check_xray_status
  381. if [[ $? == 0 ]]; then
  382. echo -e "xray state: ${green}Running${plain}"
  383. else
  384. echo -e "xray state: ${red}Not Running${plain}"
  385. fi
  386. }
  387. open_ports() {
  388. if ! command -v ufw &>/dev/null; then
  389. echo "ufw firewall is not installed. Installing now..."
  390. sudo apt-get update
  391. sudo apt-get install -y ufw
  392. else
  393. echo "ufw firewall is already installed"
  394. fi
  395. # Check if the firewall is inactive
  396. if sudo ufw status | grep -q "Status: active"; then
  397. echo "firewall is already active"
  398. else
  399. # Open the necessary ports
  400. sudo ufw allow ssh
  401. sudo ufw allow http
  402. sudo ufw allow https
  403. sudo ufw allow 2053/tcp
  404. # Enable the firewall
  405. sudo ufw --force enable
  406. fi
  407. # Prompt the user to enter a list of ports
  408. read -p "Enter the ports you want to open (e.g. 80,443,2053 or range 400-500): " ports
  409. # Check if the input is valid
  410. if ! [[ $ports =~ ^([0-9]+|[0-9]+-[0-9]+)(,([0-9]+|[0-9]+-[0-9]+))*$ ]]; then
  411. echo "Error: Invalid input. Please enter a comma-separated list of ports or a range of ports (e.g. 80,443,2053 or 400-500)." >&2
  412. exit 1
  413. fi
  414. # Open the specified ports using ufw
  415. IFS=',' read -ra PORT_LIST <<<"$ports"
  416. for port in "${PORT_LIST[@]}"; do
  417. if [[ $port == *-* ]]; then
  418. # Split the range into start and end ports
  419. start_port=$(echo $port | cut -d'-' -f1)
  420. end_port=$(echo $port | cut -d'-' -f2)
  421. # Loop through the range and open each port
  422. for ((i = start_port; i <= end_port; i++)); do
  423. sudo ufw allow $i
  424. done
  425. else
  426. sudo ufw allow "$port"
  427. fi
  428. done
  429. # Confirm that the ports are open
  430. sudo ufw status | grep $ports
  431. }
  432. update_geo() {
  433. local defaultBinFolder="/usr/local/x-ui/bin"
  434. read -p "Please enter x-ui bin folder path. Leave blank for default. (Default: '${defaultBinFolder}')" binFolder
  435. binFolder=${binFolder:-${defaultBinFolder}}
  436. if [[ ! -d ${binFolder} ]]; then
  437. LOGE "Folder ${binFolder} not exists!"
  438. LOGI "making bin folder: ${binFolder}..."
  439. mkdir -p ${binFolder}
  440. fi
  441. systemctl stop x-ui
  442. cd ${binFolder}
  443. rm -f geoip.dat geosite.dat iran.dat
  444. wget -N https://github.com/Loyalsoldier/v2ray-rules-dat/releases/latest/download/geoip.dat
  445. wget -N https://github.com/Loyalsoldier/v2ray-rules-dat/releases/latest/download/geosite.dat
  446. wget -N https://github.com/bootmortis/iran-hosted-domains/releases/latest/download/iran.dat
  447. systemctl start x-ui
  448. echo -e "${green}Geosite.dat + Geoip.dat + Iran.dat have been updated successfully in bin folder '${binfolder}'!${plain}"
  449. before_show_menu
  450. }
  451. install_acme() {
  452. cd ~
  453. LOGI "install acme..."
  454. curl https://get.acme.sh | sh
  455. if [ $? -ne 0 ]; then
  456. LOGE "install acme failed"
  457. return 1
  458. else
  459. LOGI "install acme succeed"
  460. fi
  461. return 0
  462. }
  463. ssl_cert_issue_main() {
  464. echo -e "${green}\t1.${plain} Get SSL"
  465. echo -e "${green}\t2.${plain} Revoke"
  466. echo -e "${green}\t3.${plain} Force Renew"
  467. read -p "Choose an option: " choice
  468. case "$choice" in
  469. 1) ssl_cert_issue ;;
  470. 2)
  471. local domain=""
  472. read -p "Please enter your domain name to revoke the certificate: " domain
  473. ~/.acme.sh/acme.sh --revoke -d ${domain}
  474. LOGI "Certificate revoked"
  475. ;;
  476. 3)
  477. local domain=""
  478. read -p "Please enter your domain name to forcefully renew an SSL certificate: " domain
  479. ~/.acme.sh/acme.sh --renew -d ${domain} --force ;;
  480. *) echo "Invalid choice" ;;
  481. esac
  482. }
  483. ssl_cert_issue() {
  484. #check for acme.sh first
  485. if ! command -v ~/.acme.sh/acme.sh &>/dev/null; then
  486. echo "acme.sh could not be found. we will install it"
  487. install_acme
  488. if [ $? -ne 0 ]; then
  489. LOGE "install acme failed, please check logs"
  490. exit 1
  491. fi
  492. fi
  493. #install socat second
  494. if [[ "${release}" == "centos" ]] || [[ "${release}" == "fedora" ]]; then
  495. yum install socat -y
  496. else
  497. apt install socat -y
  498. fi
  499. if [ $? -ne 0 ]; then
  500. LOGE "install socat failed,please check logs"
  501. exit 1
  502. else
  503. LOGI "install socat succeed..."
  504. fi
  505. #get the domain here,and we need verify it
  506. local domain=""
  507. read -p "Please enter your domain name:" domain
  508. LOGD "your domain is:${domain},check it..."
  509. #here we need to judge whether there exists cert already
  510. local currentCert=$(~/.acme.sh/acme.sh --list | tail -1 | awk '{print $1}')
  511. if [ ${currentCert} == ${domain} ]; then
  512. local certInfo=$(~/.acme.sh/acme.sh --list)
  513. LOGE "system already has certs here,can not issue again,current certs details:"
  514. LOGI "$certInfo"
  515. exit 1
  516. else
  517. LOGI "your domain is ready for issuing cert now..."
  518. fi
  519. #create a directory for install cert
  520. certPath="/root/cert/${domain}"
  521. if [ ! -d "$certPath" ]; then
  522. mkdir -p "$certPath"
  523. else
  524. rm -rf "$certPath"
  525. mkdir -p "$certPath"
  526. fi
  527. #get needed port here
  528. local WebPort=80
  529. read -p "please choose which port do you use,default will be 80 port:" WebPort
  530. if [[ ${WebPort} -gt 65535 || ${WebPort} -lt 1 ]]; then
  531. LOGE "your input ${WebPort} is invalid,will use default port"
  532. fi
  533. LOGI "will use port:${WebPort} to issue certs,please make sure this port is open..."
  534. #NOTE:This should be handled by user
  535. #open the port and kill the occupied progress
  536. ~/.acme.sh/acme.sh --set-default-ca --server letsencrypt
  537. ~/.acme.sh/acme.sh --issue -d ${domain} --standalone --httpport ${WebPort}
  538. if [ $? -ne 0 ]; then
  539. LOGE "issue certs failed,please check logs"
  540. rm -rf ~/.acme.sh/${domain}
  541. exit 1
  542. else
  543. LOGE "issue certs succeed,installing certs..."
  544. fi
  545. #install cert
  546. ~/.acme.sh/acme.sh --installcert -d ${domain} \
  547. --key-file /root/cert/${domain}/privkey.pem \
  548. --fullchain-file /root/cert/${domain}/fullchain.pem
  549. if [ $? -ne 0 ]; then
  550. LOGE "install certs failed,exit"
  551. rm -rf ~/.acme.sh/${domain}
  552. exit 1
  553. else
  554. LOGI "install certs succeed,enable auto renew..."
  555. fi
  556. ~/.acme.sh/acme.sh --upgrade --auto-upgrade
  557. if [ $? -ne 0 ]; then
  558. LOGE "auto renew failed, certs details:"
  559. ls -lah cert/*
  560. chmod 755 $certPath/*
  561. exit 1
  562. else
  563. LOGI "auto renew succeed, certs details:"
  564. ls -lah cert/*
  565. chmod 755 $certPath/*
  566. fi
  567. }
  568. warp_cloudflare() {
  569. echo -e "${green}\t1.${plain} install WARP"
  570. echo -e "${green}\t2.${plain} Account Type (free, plus, team)"
  571. echo -e "${green}\t3.${plain} Turn on/off WireProxy"
  572. echo -e "${green}\t4.${plain} Uninstall WARP"
  573. read -p "Choose an option: " choice
  574. case "$choice" in
  575. 1)
  576. bash <(curl -sSL https://gist.githubusercontent.com/hamid-gh98/dc5dd9b0cc5b0412af927b1ccdb294c7/raw/install_warp_proxy.sh)
  577. ;;
  578. 2)
  579. warp a
  580. ;;
  581. 3)
  582. warp y
  583. ;;
  584. 4)
  585. warp u
  586. ;;
  587. *) echo "Invalid choice" ;;
  588. esac
  589. }
  590. run_speedtest() {
  591. # Check if Speedtest is already installed
  592. if ! command -v speedtest &> /dev/null; then
  593. # If not installed, install it
  594. local pkg_manager=""
  595. local speedtest_install_script=""
  596. if command -v dnf &> /dev/null; then
  597. pkg_manager="dnf"
  598. speedtest_install_script="https://packagecloud.io/install/repositories/ookla/speedtest-cli/script.rpm.sh"
  599. elif command -v yum &> /dev/null; then
  600. pkg_manager="yum"
  601. speedtest_install_script="https://packagecloud.io/install/repositories/ookla/speedtest-cli/script.rpm.sh"
  602. elif command -v apt-get &> /dev/null; then
  603. pkg_manager="apt-get"
  604. speedtest_install_script="https://packagecloud.io/install/repositories/ookla/speedtest-cli/script.deb.sh"
  605. elif command -v apt &> /dev/null; then
  606. pkg_manager="apt"
  607. speedtest_install_script="https://packagecloud.io/install/repositories/ookla/speedtest-cli/script.deb.sh"
  608. fi
  609. if [[ -z $pkg_manager ]]; then
  610. echo "Error: Package manager not found. You may need to install Speedtest manually."
  611. return 1
  612. else
  613. curl -s $speedtest_install_script | sudo bash
  614. sudo $pkg_manager install -y speedtest
  615. fi
  616. fi
  617. # Run Speedtest
  618. speedtest
  619. }
  620. iplimit_main() {
  621. echo -e "\n${green}\t1.${plain} Install Fail2ban and configure IP Limit"
  622. echo -e "${green}\t2.${plain} Change Ban Duration"
  623. echo -e "${green}\t3.${plain} Unban Everyone"
  624. echo -e "${green}\t4.${plain} Check Logs"
  625. echo -e "${green}\t5.${plain} Uninstall IP Limit"
  626. echo -e "${green}\t0.${plain} Back to Main Menu"
  627. read -p "Choose an option: " choice
  628. case "$choice" in
  629. 0)
  630. show_menu ;;
  631. 1)
  632. confirm "Proceed with installation of Fail2ban & IP Limit?" "y"
  633. if [[ $? == 0 ]]; then
  634. install_iplimit
  635. else
  636. iplimit_main
  637. fi ;;
  638. 2)
  639. read -rp "Please enter new Ban Duration in Minutes [default 5]: " NUM
  640. if [[ $NUM =~ ^[0-9]+$ ]]; then
  641. echo -e "\n[3x-ipl]\nenabled=true\nfilter=3x-ipl\naction=3x-ipl\nlogpath=/var/log/3xipl.log\nmaxretry=3\nfindtime=100\nbantime=${NUM}m" > /etc/fail2ban/jail.d/3x-ipl.conf
  642. sudo systemctl restart fail2ban
  643. echo -e "${green}Bantime set to ${NUM} minutes successfully.${plain}"
  644. else
  645. echo -e "${red}${NUM} is not a number! Please, try again.${plain}"
  646. fi
  647. iplimit_main ;;
  648. 3)
  649. confirm "Proceed with Unbanning everyone from IP Limit jail?" "y"
  650. if [[ $? == 0 ]]; then
  651. fail2ban-client reload --restart --unban 3x-ipl
  652. echo -e "${green}All users Unbanned successfully.${plain}"
  653. iplimit_main
  654. else
  655. echo -e "${yellow}Cancelled.${plain}"
  656. fi
  657. iplimit_main ;;
  658. 4)
  659. if test -f "/var/log/3xipl-banned.log"; then
  660. if [[ -s "/var/log/3xipl-banned.log" ]]; then
  661. cat /var/log/3xipl-banned.log
  662. else
  663. echo -e "${red}Log file is empty.${plain}\n"
  664. fi
  665. else
  666. echo -e "${red}Log file not found. Please Install Fail2ban and IP Limit first.${plain}\n"
  667. iplimit_main
  668. fi ;;
  669. 5)
  670. remove_iplimit ;;
  671. *) echo "Invalid choice" ;;
  672. esac
  673. }
  674. install_iplimit() {
  675. if ! command -v fail2ban-client &>/dev/null; then
  676. echo -e "${green}Fail2ban is not installed. Installing now...!${plain}\n"
  677. # Check the OS and install necessary packages
  678. case "${release}" in
  679. ubuntu|debian)
  680. sudo apt-get update && sudo apt-get install fail2ban -y ;;
  681. centos)
  682. sudo yum -y update && sudo yum -y install fail2ban ;;
  683. fedora)
  684. sudo dnf -y update && sudo dnf -y install fail2ban ;;
  685. *)
  686. echo -e "${red}Unsupported operating system. Please check the script and install the necessary packages manually.${plain}\n"
  687. exit 1 ;;
  688. esac
  689. echo -e "${green}Fail2ban installed successfully!${plain}\n"
  690. else
  691. echo -e "${yellow}Fail2ban is already installed.${plain}\n"
  692. fi
  693. echo -e "${green}Configuring IP Limit...${plain}\n"
  694. #Check if [3x-ipl] exists in jail.local (just making sure there's no double config for jail)
  695. if grep -qw '3x-ipl' /etc/fail2ban/jail.local || grep -qw '3x-ipl' /etc/fail2ban/jail.conf; then
  696. echo -e "${red}Found conflicts in /etc/fail2ban/jail.conf or jail.local file!\nPlease manually remove anything related 3x-ipl in that files and try again.\nInstallation of IP Limit failed.${plain}\n"
  697. exit 1
  698. fi
  699. #Check if log file exists
  700. if ! test -f "/var/log/3xipl-banned.log"; then
  701. touch /var/log/3xipl-banned.log
  702. fi
  703. #Check if service log file exists so fail2ban won't return error
  704. if ! test -f "/var/log/3xipl.log"; then
  705. touch /var/log/3xipl.log
  706. fi
  707. echo -e "\n[3x-ipl]\nenabled=true\nfilter=3x-ipl\naction=3x-ipl\nlogpath=/var/log/3xipl.log\nmaxretry=3\nfindtime=100\nbantime=5m" > /etc/fail2ban/jail.d/3x-ipl.conf
  708. sudo cat > /etc/fail2ban/filter.d/3x-ipl.conf << EOF
  709. [Definition]
  710. datepattern = ^%%Y/%%m/%%d %%H:%%M:%%S
  711. failregex = \[LIMIT_IP\]\s*Email\s*=\s*<F-USER>.+</F-USER>\s*\|\|\s*SRC\s*=\s*<ADDR>
  712. ignoreregex =
  713. EOF
  714. sudo cat > /etc/fail2ban/action.d/3x-ipl.conf << 'EOF'
  715. [INCLUDES]
  716. before = iptables-common.conf
  717. [Definition]
  718. actionstart = <iptables> -N f2b-<name>
  719. <iptables> -A f2b-<name> -j <returntype>
  720. <iptables> -I <chain> -p <protocol> -j f2b-<name>
  721. actionstop = <iptables> -D <chain> -p <protocol> -j f2b-<name>
  722. <actionflush>
  723. <iptables> -X f2b-<name>
  724. actioncheck = <iptables> -n -L <chain> | grep -q 'f2b-<name>[ \t]'
  725. actionban = <iptables> -I f2b-<name> 1 -s <ip> -j <blocktype>
  726. echo "$(date +"%%Y/%%m/%%d %%H:%%M:%%S") BAN [Email] = <F-USER> [IP] = <ip> banned for <bantime> seconds." >> /var/log/3xipl-banned.log
  727. actionunban = <iptables> -D f2b-<name> -s <ip> -j <blocktype>
  728. echo "$(date +"%%Y/%%m/%%d %%H:%%M:%%S") UNBAN [Email] = <F-USER> [IP] = <ip> unbanned." >> /var/log/3xipl-banned.log
  729. [Init]
  730. EOF
  731. #Launching fail2ban
  732. if ! sudo systemctl is-active --quiet fail2ban; then
  733. sudo systemctl start fail2ban
  734. else
  735. systemctl restart fail2ban
  736. fi
  737. sudo systemctl enable fail2ban
  738. echo -e "${green}IP Limit installed and configured successfully!${plain}\n"
  739. before_show_menu
  740. }
  741. remove_iplimit(){
  742. echo -e "${green}\t1.${plain} Only remove IP Limit configurations"
  743. echo -e "${green}\t2.${plain} Uninstall Fail2ban and IP Limit"
  744. echo -e "${green}\t0.${plain} Abort"
  745. read -p "Choose an option: " num
  746. case "$num" in
  747. 1)
  748. rm -f /etc/fail2ban/filter.d/3x-ipl.conf
  749. rm -f /etc/fail2ban/action.d/3x-ipl.conf
  750. rm -f /etc/fail2ban/jail.d/3x-ipl.conf
  751. sudo systemctl restart fail2ban
  752. echo -e "${green}IP Limit removed successfully!${plain}\n"
  753. before_show_menu ;;
  754. 2)
  755. rm -f /etc/fail2ban/filter.d/3x-ipl.conf
  756. rm -f /etc/fail2ban/action.d/3x-ipl.conf
  757. rm -f /etc/fail2ban/jail.d/3x-ipl.conf
  758. sudo systemctl stop fail2ban
  759. sudo systemctl disable fail2ban
  760. case "${release}" in
  761. ubuntu|debian)
  762. sudo apt-get remove fail2ban -y ;;
  763. centos)
  764. sudo yum -y remove fail2ban ;;
  765. fedora)
  766. sudo dnf -y remove fail2ban ;;
  767. *)
  768. echo -e "${red}Unsupported operating system. Please uninstall Fail2ban manually.${plain}\n"
  769. exit 1 ;;
  770. esac
  771. rm -rf /etc/fail2ban/*
  772. echo -e "${green}Fail2ban and IP Limit removed successfully!${plain}\n"
  773. before_show_menu ;;
  774. 0)
  775. echo -e "${yellow}Cancelled.${plain}\n"
  776. iplimit_main ;;
  777. *)
  778. echo -e "${red}Invalid option. Please select a valid number.${plain}\n"
  779. remove_iplimit ;;
  780. esac
  781. }
  782. show_usage() {
  783. echo "x-ui control menu usages: "
  784. echo "------------------------------------------"
  785. echo -e "x-ui - Enter control menu"
  786. echo -e "x-ui start - Start x-ui "
  787. echo -e "x-ui stop - Stop x-ui "
  788. echo -e "x-ui restart - Restart x-ui "
  789. echo -e "x-ui status - Show x-ui status"
  790. echo -e "x-ui enable - Enable x-ui on system startup"
  791. echo -e "x-ui disable - Disable x-ui on system startup"
  792. echo -e "x-ui log - Check x-ui logs"
  793. echo -e "x-ui update - Update x-ui "
  794. echo -e "x-ui install - Install x-ui "
  795. echo -e "x-ui uninstall - Uninstall x-ui "
  796. echo "------------------------------------------"
  797. }
  798. show_menu() {
  799. echo -e "
  800. ${green}3X-ui Panel Management Script${plain}
  801. ${green}0.${plain} Exit Script
  802. ————————————————
  803. ${green}1.${plain} Install x-ui
  804. ${green}2.${plain} Update x-ui
  805. ${green}3.${plain} Uninstall x-ui
  806. ————————————————
  807. ${green}4.${plain} Reset Username & Password & Secret Token
  808. ${green}5.${plain} Reset Panel Settings
  809. ${green}6.${plain} Change Panel Port
  810. ${green}7.${plain} View Current Panel Settings
  811. ————————————————
  812. ${green}8.${plain} Start x-ui
  813. ${green}9.${plain} Stop x-ui
  814. ${green}10.${plain} Restart x-ui
  815. ${green}11.${plain} Check x-ui Status
  816. ${green}12.${plain} Check x-ui Logs
  817. ————————————————
  818. ${green}13.${plain} Enable x-ui On System Startup
  819. ${green}14.${plain} Disable x-ui On System Startup
  820. ————————————————
  821. ${green}15.${plain} SSL Certificate Management
  822. ${green}16.${plain} IP Limit Management
  823. ${green}17.${plain} WARP Management
  824. ————————————————
  825. ${green}18.${plain} Enable BBR
  826. ${green}19.${plain} Update Geo Files
  827. ${green}20.${plain} Active Firewall and open ports
  828. ${green}21.${plain} Speedtest by Ookla
  829. "
  830. show_status
  831. echo && read -p "Please enter your selection [0-21]: " num
  832. case "${num}" in
  833. 0)
  834. exit 0
  835. ;;
  836. 1)
  837. check_uninstall && install
  838. ;;
  839. 2)
  840. check_install && update
  841. ;;
  842. 3)
  843. check_install && uninstall
  844. ;;
  845. 4)
  846. check_install && reset_user
  847. ;;
  848. 5)
  849. check_install && reset_config
  850. ;;
  851. 6)
  852. check_install && set_port
  853. ;;
  854. 7)
  855. check_install && check_config
  856. ;;
  857. 8)
  858. check_install && start
  859. ;;
  860. 9)
  861. check_install && stop
  862. ;;
  863. 10)
  864. check_install && restart
  865. ;;
  866. 11)
  867. check_install && status
  868. ;;
  869. 12)
  870. check_install && show_log
  871. ;;
  872. 13)
  873. check_install && enable
  874. ;;
  875. 14)
  876. check_install && disable
  877. ;;
  878. 15)
  879. ssl_cert_issue_main
  880. ;;
  881. 16)
  882. iplimit_main
  883. ;;
  884. 17)
  885. warp_cloudflare
  886. ;;
  887. 18)
  888. enable_bbr
  889. ;;
  890. 19)
  891. update_geo
  892. ;;
  893. 20)
  894. open_ports
  895. ;;
  896. 21)
  897. run_speedtest
  898. ;;
  899. *)
  900. LOGE "Please enter the correct number [0-21]"
  901. ;;
  902. esac
  903. }
  904. if [[ $# > 0 ]]; then
  905. case $1 in
  906. "start")
  907. check_install 0 && start 0
  908. ;;
  909. "stop")
  910. check_install 0 && stop 0
  911. ;;
  912. "restart")
  913. check_install 0 && restart 0
  914. ;;
  915. "status")
  916. check_install 0 && status 0
  917. ;;
  918. "enable")
  919. check_install 0 && enable 0
  920. ;;
  921. "disable")
  922. check_install 0 && disable 0
  923. ;;
  924. "log")
  925. check_install 0 && show_log 0
  926. ;;
  927. "update")
  928. check_install 0 && update 0
  929. ;;
  930. "install")
  931. check_uninstall 0 && install 0
  932. ;;
  933. "uninstall")
  934. check_install 0 && uninstall 0
  935. ;;
  936. *) show_usage ;;
  937. esac
  938. else
  939. show_menu
  940. fi