x-ui.sh 31 KB

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