x-ui.sh 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891
  1. #!/bin/bash
  2. red='\033[0;31m'
  3. green='\033[0;32m'
  4. blue='\033[0;34m'
  5. yellow='\033[0;33m'
  6. plain='\033[0m'
  7. #Add some basic function here
  8. function LOGD() {
  9. echo -e "${yellow}[DEG] $* ${plain}"
  10. }
  11. function LOGE() {
  12. echo -e "${red}[ERR] $* ${plain}"
  13. }
  14. function LOGI() {
  15. echo -e "${green}[INF] $* ${plain}"
  16. }
  17. # check root
  18. [[ $EUID -ne 0 ]] && LOGE "ERROR: You must be root to run this script! \n" && exit 1
  19. # Check OS and set release variable
  20. if [[ -f /etc/os-release ]]; then
  21. source /etc/os-release
  22. release=$ID
  23. elif [[ -f /usr/lib/os-release ]]; then
  24. source /usr/lib/os-release
  25. release=$ID
  26. else
  27. echo "Failed to check the system OS, please contact the author!" >&2
  28. exit 1
  29. fi
  30. echo "The OS release is: $release"
  31. os_version=""
  32. os_version=$(grep "^VERSION_ID" /etc/os-release | cut -d '=' -f2 | tr -d '"' | tr -d '.')
  33. # Declare Variables
  34. log_folder="${XUI_LOG_FOLDER:=/var/log}"
  35. iplimit_log_path="${log_folder}/3xipl.log"
  36. iplimit_banned_log_path="${log_folder}/3xipl-banned.log"
  37. confirm() {
  38. if [[ $# > 1 ]]; then
  39. echo && read -rp "$1 [Default $2]: " temp
  40. if [[ "${temp}" == "" ]]; then
  41. temp=$2
  42. fi
  43. else
  44. read -rp "$1 [y/n]: " temp
  45. fi
  46. if [[ "${temp}" == "y" || "${temp}" == "Y" ]]; then
  47. return 0
  48. else
  49. return 1
  50. fi
  51. }
  52. confirm_restart() {
  53. confirm "Restart the panel, Attention: Restarting the panel will also restart xray" "y"
  54. if [[ $? == 0 ]]; then
  55. restart
  56. else
  57. show_menu
  58. fi
  59. }
  60. before_show_menu() {
  61. echo && echo -n -e "${yellow}Press enter to return to the main menu: ${plain}" && read temp
  62. show_menu
  63. }
  64. install() {
  65. bash <(curl -Ls https://raw.githubusercontent.com/MHSanaei/3x-ui/main/install.sh)
  66. if [[ $? == 0 ]]; then
  67. if [[ $# == 0 ]]; then
  68. start
  69. else
  70. start 0
  71. fi
  72. fi
  73. }
  74. update() {
  75. confirm "This function will forcefully reinstall the latest version, and the data will not be lost. Do you want to continue?" "y"
  76. if [[ $? != 0 ]]; then
  77. LOGE "Cancelled"
  78. if [[ $# == 0 ]]; then
  79. before_show_menu
  80. fi
  81. return 0
  82. fi
  83. bash <(curl -Ls https://raw.githubusercontent.com/MHSanaei/3x-ui/main/install.sh)
  84. if [[ $? == 0 ]]; then
  85. LOGI "Update is complete, Panel has automatically restarted "
  86. before_show_menu
  87. fi
  88. }
  89. update_menu() {
  90. echo -e "${yellow}Updating Menu${plain}"
  91. confirm "This function will update the menu to the latest changes." "y"
  92. if [[ $? != 0 ]]; then
  93. LOGE "Cancelled"
  94. if [[ $# == 0 ]]; then
  95. before_show_menu
  96. fi
  97. return 0
  98. fi
  99. wget -O /usr/bin/x-ui https://raw.githubusercontent.com/MHSanaei/3x-ui/main/x-ui.sh
  100. chmod +x /usr/local/x-ui/x-ui.sh
  101. chmod +x /usr/bin/x-ui
  102. if [[ $? == 0 ]]; then
  103. echo -e "${green}Update successful. The panel has automatically restarted.${plain}"
  104. exit 0
  105. else
  106. echo -e "${red}Failed to update the menu.${plain}"
  107. return 1
  108. fi
  109. }
  110. legacy_version() {
  111. echo "Enter the panel version (like 2.4.0):"
  112. read tag_version
  113. if [ -z "$tag_version" ]; then
  114. echo "Panel version cannot be empty. Exiting."
  115. exit 1
  116. fi
  117. # Use the entered panel version in the download link
  118. install_command="bash <(curl -Ls "https://raw.githubusercontent.com/mhsanaei/3x-ui/v$tag_version/install.sh") v$tag_version"
  119. echo "Downloading and installing panel version $tag_version..."
  120. eval $install_command
  121. }
  122. # Function to handle the deletion of the script file
  123. delete_script() {
  124. rm "$0" # Remove the script file itself
  125. exit 1
  126. }
  127. uninstall() {
  128. confirm "Are you sure you want to uninstall the panel? xray will also uninstalled!" "n"
  129. if [[ $? != 0 ]]; then
  130. if [[ $# == 0 ]]; then
  131. show_menu
  132. fi
  133. return 0
  134. fi
  135. systemctl stop x-ui
  136. systemctl disable x-ui
  137. rm /etc/systemd/system/x-ui.service -f
  138. systemctl daemon-reload
  139. systemctl reset-failed
  140. rm /etc/x-ui/ -rf
  141. rm /usr/local/x-ui/ -rf
  142. echo ""
  143. echo -e "Uninstalled Successfully.\n"
  144. echo "If you need to install this panel again, you can use below command:"
  145. echo -e "${green}bash <(curl -Ls https://raw.githubusercontent.com/mhsanaei/3x-ui/master/install.sh)${plain}"
  146. echo ""
  147. # Trap the SIGTERM signal
  148. trap delete_script SIGTERM
  149. delete_script
  150. }
  151. reset_user() {
  152. confirm "Are you sure to reset the username and password of the panel?" "n"
  153. if [[ $? != 0 ]]; then
  154. if [[ $# == 0 ]]; then
  155. show_menu
  156. fi
  157. return 0
  158. fi
  159. read -rp "Please set the login username [default is a random username]: " config_account
  160. [[ -z $config_account ]] && config_account=$(date +%s%N | md5sum | cut -c 1-8)
  161. read -rp "Please set the login password [default is a random password]: " config_password
  162. [[ -z $config_password ]] && config_password=$(date +%s%N | md5sum | cut -c 1-8)
  163. /usr/local/x-ui/x-ui setting -username ${config_account} -password ${config_password} >/dev/null 2>&1
  164. echo -e "Panel login username has been reset to: ${green} ${config_account} ${plain}"
  165. echo -e "Panel login password has been reset to: ${green} ${config_password} ${plain}"
  166. echo -e "${green} Please use the new login username and password to access the X-UI panel. Also remember them! ${plain}"
  167. confirm_restart
  168. }
  169. gen_random_string() {
  170. local length="$1"
  171. local random_string=$(LC_ALL=C tr -dc 'a-zA-Z0-9' </dev/urandom | fold -w "$length" | head -n 1)
  172. echo "$random_string"
  173. }
  174. reset_webbasepath() {
  175. echo -e "${yellow}Resetting Web Base Path${plain}"
  176. read -rp "Are you sure you want to reset the web base path? (y/n): " confirm
  177. if [[ $confirm != "y" && $confirm != "Y" ]]; then
  178. echo -e "${yellow}Operation canceled.${plain}"
  179. return
  180. fi
  181. config_webBasePath=$(gen_random_string 10)
  182. # Apply the new web base path setting
  183. /usr/local/x-ui/x-ui setting -webBasePath "${config_webBasePath}" >/dev/null 2>&1
  184. echo -e "Web base path has been reset to: ${green}${config_webBasePath}${plain}"
  185. echo -e "${green}Please use the new web base path to access the panel.${plain}"
  186. restart
  187. }
  188. reset_config() {
  189. confirm "Are you sure you want to reset all panel settings, Account data will not be lost, Username and password will not change" "n"
  190. if [[ $? != 0 ]]; then
  191. if [[ $# == 0 ]]; then
  192. show_menu
  193. fi
  194. return 0
  195. fi
  196. /usr/local/x-ui/x-ui setting -reset
  197. echo -e "All panel settings have been reset to default."
  198. restart
  199. }
  200. check_config() {
  201. local info=$(/usr/local/x-ui/x-ui setting -show true)
  202. if [[ $? != 0 ]]; then
  203. LOGE "get current settings error, please check logs"
  204. show_menu
  205. return
  206. fi
  207. LOGI "${info}"
  208. local existing_webBasePath=$(echo "$info" | grep -Eo 'webBasePath: .+' | awk '{print $2}')
  209. local existing_port=$(echo "$info" | grep -Eo 'port: .+' | awk '{print $2}')
  210. local existing_cert=$(/usr/local/x-ui/x-ui setting -getCert true | grep -Eo 'cert: .+' | awk '{print $2}')
  211. local server_ip=$(curl -s https://api.ipify.org)
  212. if [[ -n "$existing_cert" ]]; then
  213. local domain=$(basename "$(dirname "$existing_cert")")
  214. if [[ "$domain" =~ ^[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$ ]]; then
  215. echo -e "${green}Access URL: https://${domain}:${existing_port}${existing_webBasePath}${plain}"
  216. else
  217. echo -e "${green}Access URL: https://${server_ip}:${existing_port}${existing_webBasePath}${plain}"
  218. fi
  219. else
  220. echo -e "${green}Access URL: http://${server_ip}:${existing_port}${existing_webBasePath}${plain}"
  221. fi
  222. }
  223. set_port() {
  224. echo && echo -n -e "Enter port number[1-65535]: " && read port
  225. if [[ -z "${port}" ]]; then
  226. LOGD "Cancelled"
  227. before_show_menu
  228. else
  229. /usr/local/x-ui/x-ui setting -port ${port}
  230. echo -e "The port is set, Please restart the panel now, and use the new port ${green}${port}${plain} to access web panel"
  231. confirm_restart
  232. fi
  233. }
  234. start() {
  235. check_status
  236. if [[ $? == 0 ]]; then
  237. echo ""
  238. LOGI "Panel is running, No need to start again, If you need to restart, please select restart"
  239. else
  240. systemctl start x-ui
  241. sleep 2
  242. check_status
  243. if [[ $? == 0 ]]; then
  244. LOGI "x-ui Started Successfully"
  245. else
  246. LOGE "panel Failed to start, Probably because it takes longer than two seconds to start, Please check the log information later"
  247. fi
  248. fi
  249. if [[ $# == 0 ]]; then
  250. before_show_menu
  251. fi
  252. }
  253. stop() {
  254. check_status
  255. if [[ $? == 1 ]]; then
  256. echo ""
  257. LOGI "Panel stopped, No need to stop again!"
  258. else
  259. systemctl stop x-ui
  260. sleep 2
  261. check_status
  262. if [[ $? == 1 ]]; then
  263. LOGI "x-ui and xray stopped successfully"
  264. else
  265. LOGE "Panel stop failed, Probably because the stop time exceeds two seconds, Please check the log information later"
  266. fi
  267. fi
  268. if [[ $# == 0 ]]; then
  269. before_show_menu
  270. fi
  271. }
  272. restart() {
  273. systemctl restart x-ui
  274. sleep 2
  275. check_status
  276. if [[ $? == 0 ]]; then
  277. LOGI "x-ui and xray Restarted successfully"
  278. else
  279. LOGE "Panel restart failed, Probably because it takes longer than two seconds to start, Please check the log information later"
  280. fi
  281. if [[ $# == 0 ]]; then
  282. before_show_menu
  283. fi
  284. }
  285. status() {
  286. systemctl status x-ui -l
  287. if [[ $# == 0 ]]; then
  288. before_show_menu
  289. fi
  290. }
  291. enable() {
  292. systemctl enable x-ui
  293. if [[ $? == 0 ]]; then
  294. LOGI "x-ui Set to boot automatically on startup successfully"
  295. else
  296. LOGE "x-ui Failed to set Autostart"
  297. fi
  298. if [[ $# == 0 ]]; then
  299. before_show_menu
  300. fi
  301. }
  302. disable() {
  303. systemctl disable x-ui
  304. if [[ $? == 0 ]]; then
  305. LOGI "x-ui Autostart Cancelled successfully"
  306. else
  307. LOGE "x-ui Failed to cancel autostart"
  308. fi
  309. if [[ $# == 0 ]]; then
  310. before_show_menu
  311. fi
  312. }
  313. show_log() {
  314. echo -e "${green}\t1.${plain} Debug Log"
  315. echo -e "${green}\t2.${plain} Clear All logs"
  316. echo -e "${green}\t0.${plain} Back to Main Menu"
  317. read -rp "Choose an option: " choice
  318. case "$choice" in
  319. 0)
  320. show_menu
  321. ;;
  322. 1)
  323. journalctl -u x-ui -e --no-pager -f -p debug
  324. if [[ $# == 0 ]]; then
  325. before_show_menu
  326. fi
  327. ;;
  328. 2)
  329. sudo journalctl --rotate
  330. sudo journalctl --vacuum-time=1s
  331. echo "All Logs cleared."
  332. restart
  333. ;;
  334. *)
  335. echo -e "${red}Invalid option. Please select a valid number.${plain}\n"
  336. show_log
  337. ;;
  338. esac
  339. }
  340. show_banlog() {
  341. local system_log="/var/log/fail2ban.log"
  342. echo -e "${green}Checking ban logs...${plain}\n"
  343. if ! systemctl is-active --quiet fail2ban; then
  344. echo -e "${red}Fail2ban service is not running!${plain}\n"
  345. return 1
  346. fi
  347. if [[ -f "$system_log" ]]; then
  348. echo -e "${green}Recent system ban activities from fail2ban.log:${plain}"
  349. grep "3x-ipl" "$system_log" | grep -E "Ban|Unban" | tail -n 10 || echo -e "${yellow}No recent system ban activities found${plain}"
  350. echo ""
  351. fi
  352. if [[ -f "${iplimit_banned_log_path}" ]]; then
  353. echo -e "${green}3X-IPL ban log entries:${plain}"
  354. if [[ -s "${iplimit_banned_log_path}" ]]; then
  355. grep -v "INIT" "${iplimit_banned_log_path}" | tail -n 10 || echo -e "${yellow}No ban entries found${plain}"
  356. else
  357. echo -e "${yellow}Ban log file is empty${plain}"
  358. fi
  359. else
  360. echo -e "${red}Ban log file not found at: ${iplimit_banned_log_path}${plain}"
  361. fi
  362. echo -e "\n${green}Current jail status:${plain}"
  363. fail2ban-client status 3x-ipl || echo -e "${yellow}Unable to get jail status${plain}"
  364. }
  365. bbr_menu() {
  366. echo -e "${green}\t1.${plain} Enable BBR"
  367. echo -e "${green}\t2.${plain} Disable BBR"
  368. echo -e "${green}\t0.${plain} Back to Main Menu"
  369. read -rp "Choose an option: " choice
  370. case "$choice" in
  371. 0)
  372. show_menu
  373. ;;
  374. 1)
  375. enable_bbr
  376. bbr_menu
  377. ;;
  378. 2)
  379. disable_bbr
  380. bbr_menu
  381. ;;
  382. *)
  383. echo -e "${red}Invalid option. Please select a valid number.${plain}\n"
  384. bbr_menu
  385. ;;
  386. esac
  387. }
  388. disable_bbr() {
  389. if ! grep -q "net.core.default_qdisc=fq" /etc/sysctl.conf || ! grep -q "net.ipv4.tcp_congestion_control=bbr" /etc/sysctl.conf; then
  390. echo -e "${yellow}BBR is not currently enabled.${plain}"
  391. before_show_menu
  392. fi
  393. # Replace BBR with CUBIC configurations
  394. sed -i 's/net.core.default_qdisc=fq/net.core.default_qdisc=pfifo_fast/' /etc/sysctl.conf
  395. sed -i 's/net.ipv4.tcp_congestion_control=bbr/net.ipv4.tcp_congestion_control=cubic/' /etc/sysctl.conf
  396. # Apply changes
  397. sysctl -p
  398. # Verify that BBR is replaced with CUBIC
  399. if [[ $(sysctl net.ipv4.tcp_congestion_control | awk '{print $3}') == "cubic" ]]; then
  400. echo -e "${green}BBR has been replaced with CUBIC successfully.${plain}"
  401. else
  402. echo -e "${red}Failed to replace BBR with CUBIC. Please check your system configuration.${plain}"
  403. fi
  404. }
  405. enable_bbr() {
  406. if grep -q "net.core.default_qdisc=fq" /etc/sysctl.conf && grep -q "net.ipv4.tcp_congestion_control=bbr" /etc/sysctl.conf; then
  407. echo -e "${green}BBR is already enabled!${plain}"
  408. before_show_menu
  409. fi
  410. # Check the OS and install necessary packages
  411. case "${release}" in
  412. ubuntu | debian | armbian)
  413. apt-get update && apt-get install -yqq --no-install-recommends ca-certificates
  414. ;;
  415. centos | almalinux | rocky | ol)
  416. yum -y update && yum -y install ca-certificates
  417. ;;
  418. fedora | amzn | virtuozzo)
  419. dnf -y update && dnf -y install ca-certificates
  420. ;;
  421. arch | manjaro | parch)
  422. pacman -Sy --noconfirm ca-certificates
  423. ;;
  424. *)
  425. echo -e "${red}Unsupported operating system. Please check the script and install the necessary packages manually.${plain}\n"
  426. exit 1
  427. ;;
  428. esac
  429. # Enable BBR
  430. echo "net.core.default_qdisc=fq" | tee -a /etc/sysctl.conf
  431. echo "net.ipv4.tcp_congestion_control=bbr" | tee -a /etc/sysctl.conf
  432. # Apply changes
  433. sysctl -p
  434. # Verify that BBR is enabled
  435. if [[ $(sysctl net.ipv4.tcp_congestion_control | awk '{print $3}') == "bbr" ]]; then
  436. echo -e "${green}BBR has been enabled successfully.${plain}"
  437. else
  438. echo -e "${red}Failed to enable BBR. Please check your system configuration.${plain}"
  439. fi
  440. }
  441. update_shell() {
  442. wget -O /usr/bin/x-ui -N https://github.com/MHSanaei/3x-ui/raw/main/x-ui.sh
  443. if [[ $? != 0 ]]; then
  444. echo ""
  445. LOGE "Failed to download script, Please check whether the machine can connect Github"
  446. before_show_menu
  447. else
  448. chmod +x /usr/bin/x-ui
  449. LOGI "Upgrade script succeeded, Please rerun the script"
  450. before_show_menu
  451. fi
  452. }
  453. # 0: running, 1: not running, 2: not installed
  454. check_status() {
  455. if [[ ! -f /etc/systemd/system/x-ui.service ]]; then
  456. return 2
  457. fi
  458. temp=$(systemctl status x-ui | grep Active | awk '{print $3}' | cut -d "(" -f2 | cut -d ")" -f1)
  459. if [[ "${temp}" == "running" ]]; then
  460. return 0
  461. else
  462. return 1
  463. fi
  464. }
  465. check_enabled() {
  466. temp=$(systemctl is-enabled x-ui)
  467. if [[ "${temp}" == "enabled" ]]; then
  468. return 0
  469. else
  470. return 1
  471. fi
  472. }
  473. check_uninstall() {
  474. check_status
  475. if [[ $? != 2 ]]; then
  476. echo ""
  477. LOGE "Panel installed, Please do not reinstall"
  478. if [[ $# == 0 ]]; then
  479. before_show_menu
  480. fi
  481. return 1
  482. else
  483. return 0
  484. fi
  485. }
  486. check_install() {
  487. check_status
  488. if [[ $? == 2 ]]; then
  489. echo ""
  490. LOGE "Please install the panel first"
  491. if [[ $# == 0 ]]; then
  492. before_show_menu
  493. fi
  494. return 1
  495. else
  496. return 0
  497. fi
  498. }
  499. show_status() {
  500. check_status
  501. case $? in
  502. 0)
  503. echo -e "Panel state: ${green}Running${plain}"
  504. show_enable_status
  505. ;;
  506. 1)
  507. echo -e "Panel state: ${yellow}Not Running${plain}"
  508. show_enable_status
  509. ;;
  510. 2)
  511. echo -e "Panel state: ${red}Not Installed${plain}"
  512. ;;
  513. esac
  514. show_xray_status
  515. }
  516. show_enable_status() {
  517. check_enabled
  518. if [[ $? == 0 ]]; then
  519. echo -e "Start automatically: ${green}Yes${plain}"
  520. else
  521. echo -e "Start automatically: ${red}No${plain}"
  522. fi
  523. }
  524. check_xray_status() {
  525. count=$(ps -ef | grep "xray-linux" | grep -v "grep" | wc -l)
  526. if [[ count -ne 0 ]]; then
  527. return 0
  528. else
  529. return 1
  530. fi
  531. }
  532. show_xray_status() {
  533. check_xray_status
  534. if [[ $? == 0 ]]; then
  535. echo -e "xray state: ${green}Running${plain}"
  536. else
  537. echo -e "xray state: ${red}Not Running${plain}"
  538. fi
  539. }
  540. firewall_menu() {
  541. echo -e "${green}\t1.${plain} ${green}Install${plain} Firewall"
  542. echo -e "${green}\t2.${plain} Port List [numbered]"
  543. echo -e "${green}\t3.${plain} ${green}Open${plain} Ports"
  544. echo -e "${green}\t4.${plain} ${red}Delete${plain} Ports from List"
  545. echo -e "${green}\t5.${plain} ${green}Enable${plain} Firewall"
  546. echo -e "${green}\t6.${plain} ${red}Disable${plain} Firewall"
  547. echo -e "${green}\t7.${plain} Firewall Status"
  548. echo -e "${green}\t0.${plain} Back to Main Menu"
  549. read -rp "Choose an option: " choice
  550. case "$choice" in
  551. 0)
  552. show_menu
  553. ;;
  554. 1)
  555. install_firewall
  556. firewall_menu
  557. ;;
  558. 2)
  559. ufw status numbered
  560. firewall_menu
  561. ;;
  562. 3)
  563. open_ports
  564. firewall_menu
  565. ;;
  566. 4)
  567. delete_ports
  568. firewall_menu
  569. ;;
  570. 5)
  571. ufw enable
  572. firewall_menu
  573. ;;
  574. 6)
  575. ufw disable
  576. firewall_menu
  577. ;;
  578. 7)
  579. ufw status verbose
  580. firewall_menu
  581. ;;
  582. *)
  583. echo -e "${red}Invalid option. Please select a valid number.${plain}\n"
  584. firewall_menu
  585. ;;
  586. esac
  587. }
  588. install_firewall() {
  589. if ! command -v ufw &>/dev/null; then
  590. echo "ufw firewall is not installed. Installing now..."
  591. apt-get update
  592. apt-get install -y ufw
  593. else
  594. echo "ufw firewall is already installed"
  595. fi
  596. # Check if the firewall is inactive
  597. if ufw status | grep -q "Status: active"; then
  598. echo "Firewall is already active"
  599. else
  600. echo "Activating firewall..."
  601. # Open the necessary ports
  602. ufw allow ssh
  603. ufw allow http
  604. ufw allow https
  605. ufw allow 2053/tcp #webPort
  606. ufw allow 2096/tcp #subport
  607. # Enable the firewall
  608. ufw --force enable
  609. fi
  610. }
  611. open_ports() {
  612. # Prompt the user to enter the ports they want to open
  613. read -rp "Enter the ports you want to open (e.g. 80,443,2053 or range 400-500): " ports
  614. # Check if the input is valid
  615. if ! [[ $ports =~ ^([0-9]+|[0-9]+-[0-9]+)(,([0-9]+|[0-9]+-[0-9]+))*$ ]]; then
  616. 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
  617. exit 1
  618. fi
  619. # Open the specified ports using ufw
  620. IFS=',' read -ra PORT_LIST <<<"$ports"
  621. for port in "${PORT_LIST[@]}"; do
  622. if [[ $port == *-* ]]; then
  623. # Split the range into start and end ports
  624. start_port=$(echo $port | cut -d'-' -f1)
  625. end_port=$(echo $port | cut -d'-' -f2)
  626. # Open the port range
  627. ufw allow $start_port:$end_port/tcp
  628. ufw allow $start_port:$end_port/udp
  629. else
  630. # Open the single port
  631. ufw allow "$port"
  632. fi
  633. done
  634. # Confirm that the ports are opened
  635. echo "Opened the specified ports:"
  636. for port in "${PORT_LIST[@]}"; do
  637. if [[ $port == *-* ]]; then
  638. start_port=$(echo $port | cut -d'-' -f1)
  639. end_port=$(echo $port | cut -d'-' -f2)
  640. # Check if the port range has been successfully opened
  641. (ufw status | grep -q "$start_port:$end_port") && echo "$start_port-$end_port"
  642. else
  643. # Check if the individual port has been successfully opened
  644. (ufw status | grep -q "$port") && echo "$port"
  645. fi
  646. done
  647. }
  648. delete_ports() {
  649. # Display current rules with numbers
  650. echo "Current UFW rules:"
  651. ufw status numbered
  652. # Ask the user how they want to delete rules
  653. echo "Do you want to delete rules by:"
  654. echo "1) Rule numbers"
  655. echo "2) Ports"
  656. read -rp "Enter your choice (1 or 2): " choice
  657. if [[ $choice -eq 1 ]]; then
  658. # Deleting by rule numbers
  659. read -rp "Enter the rule numbers you want to delete (1, 2, etc.): " rule_numbers
  660. # Validate the input
  661. if ! [[ $rule_numbers =~ ^([0-9]+)(,[0-9]+)*$ ]]; then
  662. echo "Error: Invalid input. Please enter a comma-separated list of rule numbers." >&2
  663. exit 1
  664. fi
  665. # Split numbers into an array
  666. IFS=',' read -ra RULE_NUMBERS <<<"$rule_numbers"
  667. for rule_number in "${RULE_NUMBERS[@]}"; do
  668. # Delete the rule by number
  669. ufw delete "$rule_number" || echo "Failed to delete rule number $rule_number"
  670. done
  671. echo "Selected rules have been deleted."
  672. elif [[ $choice -eq 2 ]]; then
  673. # Deleting by ports
  674. read -rp "Enter the ports you want to delete (e.g. 80,443,2053 or range 400-500): " ports
  675. # Validate the input
  676. if ! [[ $ports =~ ^([0-9]+|[0-9]+-[0-9]+)(,([0-9]+|[0-9]+-[0-9]+))*$ ]]; then
  677. 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
  678. exit 1
  679. fi
  680. # Split ports into an array
  681. IFS=',' read -ra PORT_LIST <<<"$ports"
  682. for port in "${PORT_LIST[@]}"; do
  683. if [[ $port == *-* ]]; then
  684. # Split the port range
  685. start_port=$(echo $port | cut -d'-' -f1)
  686. end_port=$(echo $port | cut -d'-' -f2)
  687. # Delete the port range
  688. ufw delete allow $start_port:$end_port/tcp
  689. ufw delete allow $start_port:$end_port/udp
  690. else
  691. # Delete a single port
  692. ufw delete allow "$port"
  693. fi
  694. done
  695. # Confirmation of deletion
  696. echo "Deleted the specified ports:"
  697. for port in "${PORT_LIST[@]}"; do
  698. if [[ $port == *-* ]]; then
  699. start_port=$(echo $port | cut -d'-' -f1)
  700. end_port=$(echo $port | cut -d'-' -f2)
  701. # Check if the port range has been deleted
  702. (ufw status | grep -q "$start_port:$end_port") || echo "$start_port-$end_port"
  703. else
  704. # Check if the individual port has been deleted
  705. (ufw status | grep -q "$port") || echo "$port"
  706. fi
  707. done
  708. else
  709. echo "${red}Error:${plain} Invalid choice. Please enter 1 or 2." >&2
  710. exit 1
  711. fi
  712. }
  713. update_geo() {
  714. echo -e "${green}\t1.${plain} Loyalsoldier (geoip.dat, geosite.dat)"
  715. echo -e "${green}\t2.${plain} chocolate4u (geoip_IR.dat, geosite_IR.dat)"
  716. echo -e "${green}\t3.${plain} runetfreedom (geoip_RU.dat, geosite_RU.dat)"
  717. echo -e "${green}\t0.${plain} Back to Main Menu"
  718. read -rp "Choose an option: " choice
  719. cd /usr/local/x-ui/bin
  720. case "$choice" in
  721. 0)
  722. show_menu
  723. ;;
  724. 1)
  725. systemctl stop x-ui
  726. rm -f geoip.dat geosite.dat
  727. wget -N https://github.com/Loyalsoldier/v2ray-rules-dat/releases/latest/download/geoip.dat
  728. wget -N https://github.com/Loyalsoldier/v2ray-rules-dat/releases/latest/download/geosite.dat
  729. echo -e "${green}Loyalsoldier datasets have been updated successfully!${plain}"
  730. restart
  731. ;;
  732. 2)
  733. systemctl stop x-ui
  734. rm -f geoip_IR.dat geosite_IR.dat
  735. wget -O geoip_IR.dat -N https://github.com/chocolate4u/Iran-v2ray-rules/releases/latest/download/geoip.dat
  736. wget -O geosite_IR.dat -N https://github.com/chocolate4u/Iran-v2ray-rules/releases/latest/download/geosite.dat
  737. echo -e "${green}chocolate4u datasets have been updated successfully!${plain}"
  738. restart
  739. ;;
  740. 3)
  741. systemctl stop x-ui
  742. rm -f geoip_RU.dat geosite_RU.dat
  743. wget -O geoip_RU.dat -N https://github.com/runetfreedom/russia-v2ray-rules-dat/releases/latest/download/geoip.dat
  744. wget -O geosite_RU.dat -N https://github.com/runetfreedom/russia-v2ray-rules-dat/releases/latest/download/geosite.dat
  745. echo -e "${green}runetfreedom datasets have been updated successfully!${plain}"
  746. restart
  747. ;;
  748. *)
  749. echo -e "${red}Invalid option. Please select a valid number.${plain}\n"
  750. update_geo
  751. ;;
  752. esac
  753. before_show_menu
  754. }
  755. install_acme() {
  756. # Check if acme.sh is already installed
  757. if command -v ~/.acme.sh/acme.sh &>/dev/null; then
  758. LOGI "acme.sh is already installed."
  759. return 0
  760. fi
  761. LOGI "Installing acme.sh..."
  762. cd ~ || return 1 # Ensure you can change to the home directory
  763. curl -s https://get.acme.sh | sh
  764. if [ $? -ne 0 ]; then
  765. LOGE "Installation of acme.sh failed."
  766. return 1
  767. else
  768. LOGI "Installation of acme.sh succeeded."
  769. fi
  770. return 0
  771. }
  772. ssl_cert_issue_main() {
  773. echo -e "${green}\t1.${plain} Get SSL"
  774. echo -e "${green}\t2.${plain} Revoke"
  775. echo -e "${green}\t3.${plain} Force Renew"
  776. echo -e "${green}\t4.${plain} Show Existing Domains"
  777. echo -e "${green}\t5.${plain} Set Cert paths for the panel"
  778. echo -e "${green}\t0.${plain} Back to Main Menu"
  779. read -rp "Choose an option: " choice
  780. case "$choice" in
  781. 0)
  782. show_menu
  783. ;;
  784. 1)
  785. ssl_cert_issue
  786. ssl_cert_issue_main
  787. ;;
  788. 2)
  789. local domains=$(find /root/cert/ -mindepth 1 -maxdepth 1 -type d -exec basename {} \;)
  790. if [ -z "$domains" ]; then
  791. echo "No certificates found to revoke."
  792. else
  793. echo "Existing domains:"
  794. echo "$domains"
  795. read -rp "Please enter a domain from the list to revoke the certificate: " domain
  796. if echo "$domains" | grep -qw "$domain"; then
  797. ~/.acme.sh/acme.sh --revoke -d ${domain}
  798. LOGI "Certificate revoked for domain: $domain"
  799. else
  800. echo "Invalid domain entered."
  801. fi
  802. fi
  803. ssl_cert_issue_main
  804. ;;
  805. 3)
  806. local domains=$(find /root/cert/ -mindepth 1 -maxdepth 1 -type d -exec basename {} \;)
  807. if [ -z "$domains" ]; then
  808. echo "No certificates found to renew."
  809. else
  810. echo "Existing domains:"
  811. echo "$domains"
  812. read -rp "Please enter a domain from the list to renew the SSL certificate: " domain
  813. if echo "$domains" | grep -qw "$domain"; then
  814. ~/.acme.sh/acme.sh --renew -d ${domain} --force
  815. LOGI "Certificate forcefully renewed for domain: $domain"
  816. else
  817. echo "Invalid domain entered."
  818. fi
  819. fi
  820. ssl_cert_issue_main
  821. ;;
  822. 4)
  823. local domains=$(find /root/cert/ -mindepth 1 -maxdepth 1 -type d -exec basename {} \;)
  824. if [ -z "$domains" ]; then
  825. echo "No certificates found."
  826. else
  827. echo "Existing domains and their paths:"
  828. for domain in $domains; do
  829. local cert_path="/root/cert/${domain}/fullchain.pem"
  830. local key_path="/root/cert/${domain}/privkey.pem"
  831. if [[ -f "${cert_path}" && -f "${key_path}" ]]; then
  832. echo -e "Domain: ${domain}"
  833. echo -e "\tCertificate Path: ${cert_path}"
  834. echo -e "\tPrivate Key Path: ${key_path}"
  835. else
  836. echo -e "Domain: ${domain} - Certificate or Key missing."
  837. fi
  838. done
  839. fi
  840. ssl_cert_issue_main
  841. ;;
  842. 5)
  843. local domains=$(find /root/cert/ -mindepth 1 -maxdepth 1 -type d -exec basename {} \;)
  844. if [ -z "$domains" ]; then
  845. echo "No certificates found."
  846. else
  847. echo "Available domains:"
  848. echo "$domains"
  849. read -rp "Please choose a domain to set the panel paths: " domain
  850. if echo "$domains" | grep -qw "$domain"; then
  851. local webCertFile="/root/cert/${domain}/fullchain.pem"
  852. local webKeyFile="/root/cert/${domain}/privkey.pem"
  853. if [[ -f "${webCertFile}" && -f "${webKeyFile}" ]]; then
  854. /usr/local/x-ui/x-ui cert -webCert "$webCertFile" -webCertKey "$webKeyFile"
  855. echo "Panel paths set for domain: $domain"
  856. echo " - Certificate File: $webCertFile"
  857. echo " - Private Key File: $webKeyFile"
  858. restart
  859. else
  860. echo "Certificate or private key not found for domain: $domain."
  861. fi
  862. else
  863. echo "Invalid domain entered."
  864. fi
  865. fi
  866. ssl_cert_issue_main
  867. ;;
  868. *)
  869. echo -e "${red}Invalid option. Please select a valid number.${plain}\n"
  870. ssl_cert_issue_main
  871. ;;
  872. esac
  873. }
  874. ssl_cert_issue() {
  875. local existing_webBasePath=$(/usr/local/x-ui/x-ui setting -show true | grep -Eo 'webBasePath: .+' | awk '{print $2}')
  876. local existing_port=$(/usr/local/x-ui/x-ui setting -show true | grep -Eo 'port: .+' | awk '{print $2}')
  877. # check for acme.sh first
  878. if ! command -v ~/.acme.sh/acme.sh &>/dev/null; then
  879. echo "acme.sh could not be found. we will install it"
  880. install_acme
  881. if [ $? -ne 0 ]; then
  882. LOGE "install acme failed, please check logs"
  883. exit 1
  884. fi
  885. fi
  886. # install socat second
  887. case "${release}" in
  888. ubuntu | debian | armbian)
  889. apt update && apt install socat -y
  890. ;;
  891. centos | almalinux | rocky | ol)
  892. yum -y update && yum -y install socat
  893. ;;
  894. fedora | amzn | virtuozzo)
  895. dnf -y update && dnf -y install socat
  896. ;;
  897. arch | manjaro | parch)
  898. pacman -Sy --noconfirm socat
  899. ;;
  900. *)
  901. echo -e "${red}Unsupported operating system. Please check the script and install the necessary packages manually.${plain}\n"
  902. exit 1
  903. ;;
  904. esac
  905. if [ $? -ne 0 ]; then
  906. LOGE "install socat failed, please check logs"
  907. exit 1
  908. else
  909. LOGI "install socat succeed..."
  910. fi
  911. # get the domain here, and we need to verify it
  912. local domain=""
  913. read -rp "Please enter your domain name: " domain
  914. LOGD "Your domain is: ${domain}, checking it..."
  915. # check if there already exists a certificate
  916. local currentCert=$(~/.acme.sh/acme.sh --list | tail -1 | awk '{print $1}')
  917. if [ "${currentCert}" == "${domain}" ]; then
  918. local certInfo=$(~/.acme.sh/acme.sh --list)
  919. LOGE "System already has certificates for this domain. Cannot issue again. Current certificate details:"
  920. LOGI "$certInfo"
  921. exit 1
  922. else
  923. LOGI "Your domain is ready for issuing certificates now..."
  924. fi
  925. # create a directory for the certificate
  926. certPath="/root/cert/${domain}"
  927. if [ ! -d "$certPath" ]; then
  928. mkdir -p "$certPath"
  929. else
  930. rm -rf "$certPath"
  931. mkdir -p "$certPath"
  932. fi
  933. # get the port number for the standalone server
  934. local WebPort=80
  935. read -rp "Please choose which port to use (default is 80): " WebPort
  936. if [[ ${WebPort} -gt 65535 || ${WebPort} -lt 1 ]]; then
  937. LOGE "Your input ${WebPort} is invalid, will use default port 80."
  938. WebPort=80
  939. fi
  940. LOGI "Will use port: ${WebPort} to issue certificates. Please make sure this port is open."
  941. # issue the certificate
  942. ~/.acme.sh/acme.sh --set-default-ca --server letsencrypt
  943. ~/.acme.sh/acme.sh --issue -d ${domain} --listen-v6 --standalone --httpport ${WebPort} --force
  944. if [ $? -ne 0 ]; then
  945. LOGE "Issuing certificate failed, please check logs."
  946. rm -rf ~/.acme.sh/${domain}
  947. exit 1
  948. else
  949. LOGE "Issuing certificate succeeded, installing certificates..."
  950. fi
  951. reloadCmd="x-ui restart"
  952. LOGI "Default --reloadcmd for ACME is: ${yellow}x-ui restart"
  953. LOGI "This command will run on every certificate issue and renew."
  954. read -rp "Would you like to modify --reloadcmd for ACME? (y/n): " setReloadcmd
  955. if [[ "$setReloadcmd" == "y" || "$setReloadcmd" == "Y" ]]; then
  956. echo -e "\n${green}\t1.${plain} Preset: systemctl reload nginx ; x-ui restart"
  957. echo -e "${green}\t2.${plain} Input your own command"
  958. echo -e "${green}\t0.${plain} Keep default reloadcmd"
  959. read -rp "Choose an option: " choice
  960. case "$choice" in
  961. 1)
  962. LOGI "Reloadcmd is: systemctl reload nginx ; x-ui restart"
  963. reloadCmd="systemctl reload nginx ; x-ui restart"
  964. ;;
  965. 2)
  966. LOGD "It's recommended to put x-ui restart at the end, so it won't raise an error if other services fails"
  967. read -rp "Please enter your reloadcmd (example: systemctl reload nginx ; x-ui restart): " reloadCmd
  968. LOGI "Your reloadcmd is: ${reloadCmd}"
  969. ;;
  970. *)
  971. LOGI "Keep default reloadcmd"
  972. ;;
  973. esac
  974. fi
  975. # install the certificate
  976. ~/.acme.sh/acme.sh --installcert -d ${domain} \
  977. --key-file /root/cert/${domain}/privkey.pem \
  978. --fullchain-file /root/cert/${domain}/fullchain.pem --reloadcmd "${reloadCmd}"
  979. if [ $? -ne 0 ]; then
  980. LOGE "Installing certificate failed, exiting."
  981. rm -rf ~/.acme.sh/${domain}
  982. exit 1
  983. else
  984. LOGI "Installing certificate succeeded, enabling auto renew..."
  985. fi
  986. # enable auto-renew
  987. ~/.acme.sh/acme.sh --upgrade --auto-upgrade
  988. if [ $? -ne 0 ]; then
  989. LOGE "Auto renew failed, certificate details:"
  990. ls -lah cert/*
  991. chmod 755 $certPath/*
  992. exit 1
  993. else
  994. LOGI "Auto renew succeeded, certificate details:"
  995. ls -lah cert/*
  996. chmod 755 $certPath/*
  997. fi
  998. # Prompt user to set panel paths after successful certificate installation
  999. read -rp "Would you like to set this certificate for the panel? (y/n): " setPanel
  1000. if [[ "$setPanel" == "y" || "$setPanel" == "Y" ]]; then
  1001. local webCertFile="/root/cert/${domain}/fullchain.pem"
  1002. local webKeyFile="/root/cert/${domain}/privkey.pem"
  1003. if [[ -f "$webCertFile" && -f "$webKeyFile" ]]; then
  1004. /usr/local/x-ui/x-ui cert -webCert "$webCertFile" -webCertKey "$webKeyFile"
  1005. LOGI "Panel paths set for domain: $domain"
  1006. LOGI " - Certificate File: $webCertFile"
  1007. LOGI " - Private Key File: $webKeyFile"
  1008. echo -e "${green}Access URL: https://${domain}:${existing_port}${existing_webBasePath}${plain}"
  1009. restart
  1010. else
  1011. LOGE "Error: Certificate or private key file not found for domain: $domain."
  1012. fi
  1013. else
  1014. LOGI "Skipping panel path setting."
  1015. fi
  1016. }
  1017. ssl_cert_issue_CF() {
  1018. local existing_webBasePath=$(/usr/local/x-ui/x-ui setting -show true | grep -Eo 'webBasePath: .+' | awk '{print $2}')
  1019. local existing_port=$(/usr/local/x-ui/x-ui setting -show true | grep -Eo 'port: .+' | awk '{print $2}')
  1020. LOGI "****** Instructions for Use ******"
  1021. LOGI "Follow the steps below to complete the process:"
  1022. LOGI "1. Cloudflare Registered E-mail."
  1023. LOGI "2. Cloudflare Global API Key."
  1024. LOGI "3. The Domain Name."
  1025. LOGI "4. Once the certificate is issued, you will be prompted to set the certificate for the panel (optional)."
  1026. LOGI "5. The script also supports automatic renewal of the SSL certificate after installation."
  1027. confirm "Do you confirm the information and wish to proceed? [y/n]" "y"
  1028. if [ $? -eq 0 ]; then
  1029. # Check for acme.sh first
  1030. if ! command -v ~/.acme.sh/acme.sh &>/dev/null; then
  1031. echo "acme.sh could not be found. We will install it."
  1032. install_acme
  1033. if [ $? -ne 0 ]; then
  1034. LOGE "Install acme failed, please check logs."
  1035. exit 1
  1036. fi
  1037. fi
  1038. CF_Domain=""
  1039. LOGD "Please set a domain name:"
  1040. read -rp "Input your domain here: " CF_Domain
  1041. LOGD "Your domain name is set to: ${CF_Domain}"
  1042. # Set up Cloudflare API details
  1043. CF_GlobalKey=""
  1044. CF_AccountEmail=""
  1045. LOGD "Please set the API key:"
  1046. read -rp "Input your key here: " CF_GlobalKey
  1047. LOGD "Your API key is: ${CF_GlobalKey}"
  1048. LOGD "Please set up registered email:"
  1049. read -rp "Input your email here: " CF_AccountEmail
  1050. LOGD "Your registered email address is: ${CF_AccountEmail}"
  1051. # Set the default CA to Let's Encrypt
  1052. ~/.acme.sh/acme.sh --set-default-ca --server letsencrypt
  1053. if [ $? -ne 0 ]; then
  1054. LOGE "Default CA, Let'sEncrypt fail, script exiting..."
  1055. exit 1
  1056. fi
  1057. export CF_Key="${CF_GlobalKey}"
  1058. export CF_Email="${CF_AccountEmail}"
  1059. # Issue the certificate using Cloudflare DNS
  1060. ~/.acme.sh/acme.sh --issue --dns dns_cf -d ${CF_Domain} -d *.${CF_Domain} --log --force
  1061. if [ $? -ne 0 ]; then
  1062. LOGE "Certificate issuance failed, script exiting..."
  1063. exit 1
  1064. else
  1065. LOGI "Certificate issued successfully, Installing..."
  1066. fi
  1067. # Install the certificate
  1068. certPath="/root/cert/${CF_Domain}"
  1069. if [ -d "$certPath" ]; then
  1070. rm -rf ${certPath}
  1071. fi
  1072. mkdir -p ${certPath}
  1073. if [ $? -ne 0 ]; then
  1074. LOGE "Failed to create directory: ${certPath}"
  1075. exit 1
  1076. fi
  1077. reloadCmd="x-ui restart"
  1078. LOGI "Default --reloadcmd for ACME is: ${yellow}x-ui restart"
  1079. LOGI "This command will run on every certificate issue and renew."
  1080. read -rp "Would you like to modify --reloadcmd for ACME? (y/n): " setReloadcmd
  1081. if [[ "$setReloadcmd" == "y" || "$setReloadcmd" == "Y" ]]; then
  1082. echo -e "\n${green}\t1.${plain} Preset: systemctl reload nginx ; x-ui restart"
  1083. echo -e "${green}\t2.${plain} Input your own command"
  1084. echo -e "${green}\t0.${plain} Keep default reloadcmd"
  1085. read -rp "Choose an option: " choice
  1086. case "$choice" in
  1087. 1)
  1088. LOGI "Reloadcmd is: systemctl reload nginx ; x-ui restart"
  1089. reloadCmd="systemctl reload nginx ; x-ui restart"
  1090. ;;
  1091. 2)
  1092. LOGD "It's recommended to put x-ui restart at the end, so it won't raise an error if other services fails"
  1093. read -rp "Please enter your reloadcmd (example: systemctl reload nginx ; x-ui restart): " reloadCmd
  1094. LOGI "Your reloadcmd is: ${reloadCmd}"
  1095. ;;
  1096. *)
  1097. LOGI "Keep default reloadcmd"
  1098. ;;
  1099. esac
  1100. fi
  1101. ~/.acme.sh/acme.sh --installcert -d ${CF_Domain} -d *.${CF_Domain} \
  1102. --key-file ${certPath}/privkey.pem \
  1103. --fullchain-file ${certPath}/fullchain.pem --reloadcmd "${reloadCmd}"
  1104. if [ $? -ne 0 ]; then
  1105. LOGE "Certificate installation failed, script exiting..."
  1106. exit 1
  1107. else
  1108. LOGI "Certificate installed successfully, Turning on automatic updates..."
  1109. fi
  1110. # Enable auto-update
  1111. ~/.acme.sh/acme.sh --upgrade --auto-upgrade
  1112. if [ $? -ne 0 ]; then
  1113. LOGE "Auto update setup failed, script exiting..."
  1114. exit 1
  1115. else
  1116. LOGI "The certificate is installed and auto-renewal is turned on. Specific information is as follows:"
  1117. ls -lah ${certPath}/*
  1118. chmod 755 ${certPath}/*
  1119. fi
  1120. # Prompt user to set panel paths after successful certificate installation
  1121. read -rp "Would you like to set this certificate for the panel? (y/n): " setPanel
  1122. if [[ "$setPanel" == "y" || "$setPanel" == "Y" ]]; then
  1123. local webCertFile="${certPath}/fullchain.pem"
  1124. local webKeyFile="${certPath}/privkey.pem"
  1125. if [[ -f "$webCertFile" && -f "$webKeyFile" ]]; then
  1126. /usr/local/x-ui/x-ui cert -webCert "$webCertFile" -webCertKey "$webKeyFile"
  1127. LOGI "Panel paths set for domain: $CF_Domain"
  1128. LOGI " - Certificate File: $webCertFile"
  1129. LOGI " - Private Key File: $webKeyFile"
  1130. echo -e "${green}Access URL: https://${CF_Domain}:${existing_port}${existing_webBasePath}${plain}"
  1131. restart
  1132. else
  1133. LOGE "Error: Certificate or private key file not found for domain: $CF_Domain."
  1134. fi
  1135. else
  1136. LOGI "Skipping panel path setting."
  1137. fi
  1138. else
  1139. show_menu
  1140. fi
  1141. }
  1142. run_speedtest() {
  1143. # Check if Speedtest is already installed
  1144. if ! command -v speedtest &>/dev/null; then
  1145. # If not installed, determine installation method
  1146. if command -v snap &>/dev/null; then
  1147. # Use snap to install Speedtest
  1148. echo "Installing Speedtest using snap..."
  1149. snap install speedtest
  1150. else
  1151. # Fallback to using package managers
  1152. local pkg_manager=""
  1153. local speedtest_install_script=""
  1154. if command -v dnf &>/dev/null; then
  1155. pkg_manager="dnf"
  1156. speedtest_install_script="https://packagecloud.io/install/repositories/ookla/speedtest-cli/script.rpm.sh"
  1157. elif command -v yum &>/dev/null; then
  1158. pkg_manager="yum"
  1159. speedtest_install_script="https://packagecloud.io/install/repositories/ookla/speedtest-cli/script.rpm.sh"
  1160. elif command -v apt-get &>/dev/null; then
  1161. pkg_manager="apt-get"
  1162. speedtest_install_script="https://packagecloud.io/install/repositories/ookla/speedtest-cli/script.deb.sh"
  1163. elif command -v apt &>/dev/null; then
  1164. pkg_manager="apt"
  1165. speedtest_install_script="https://packagecloud.io/install/repositories/ookla/speedtest-cli/script.deb.sh"
  1166. fi
  1167. if [[ -z $pkg_manager ]]; then
  1168. echo "Error: Package manager not found. You may need to install Speedtest manually."
  1169. return 1
  1170. else
  1171. echo "Installing Speedtest using $pkg_manager..."
  1172. curl -s $speedtest_install_script | bash
  1173. $pkg_manager install -y speedtest
  1174. fi
  1175. fi
  1176. fi
  1177. speedtest
  1178. }
  1179. create_iplimit_jails() {
  1180. # Use default bantime if not passed => 30 minutes
  1181. local bantime="${1:-30}"
  1182. # Uncomment 'allowipv6 = auto' in fail2ban.conf
  1183. sed -i 's/#allowipv6 = auto/allowipv6 = auto/g' /etc/fail2ban/fail2ban.conf
  1184. # On Debian 12+ fail2ban's default backend should be changed to systemd
  1185. if [[ "${release}" == "debian" && ${os_version} -ge 12 ]]; then
  1186. sed -i '0,/action =/s/backend = auto/backend = systemd/' /etc/fail2ban/jail.conf
  1187. fi
  1188. cat << EOF > /etc/fail2ban/jail.d/3x-ipl.conf
  1189. [3x-ipl]
  1190. enabled=true
  1191. backend=auto
  1192. filter=3x-ipl
  1193. action=3x-ipl
  1194. logpath=${iplimit_log_path}
  1195. maxretry=2
  1196. findtime=32
  1197. bantime=${bantime}m
  1198. EOF
  1199. cat << EOF > /etc/fail2ban/filter.d/3x-ipl.conf
  1200. [Definition]
  1201. datepattern = ^%%Y/%%m/%%d %%H:%%M:%%S
  1202. failregex = \[LIMIT_IP\]\s*Email\s*=\s*<F-USER>.+</F-USER>\s*\|\|\s*SRC\s*=\s*<ADDR>
  1203. ignoreregex =
  1204. EOF
  1205. cat << EOF > /etc/fail2ban/action.d/3x-ipl.conf
  1206. [INCLUDES]
  1207. before = iptables-allports.conf
  1208. [Definition]
  1209. actionstart = <iptables> -N f2b-<name>
  1210. <iptables> -A f2b-<name> -j <returntype>
  1211. <iptables> -I <chain> -p <protocol> -j f2b-<name>
  1212. actionstop = <iptables> -D <chain> -p <protocol> -j f2b-<name>
  1213. <actionflush>
  1214. <iptables> -X f2b-<name>
  1215. actioncheck = <iptables> -n -L <chain> | grep -q 'f2b-<name>[ \t]'
  1216. actionban = <iptables> -I f2b-<name> 1 -s <ip> -j <blocktype>
  1217. echo "\$(date +"%%Y/%%m/%%d %%H:%%M:%%S") BAN [Email] = <F-USER> [IP] = <ip> banned for <bantime> seconds." >> ${iplimit_banned_log_path}
  1218. actionunban = <iptables> -D f2b-<name> -s <ip> -j <blocktype>
  1219. echo "\$(date +"%%Y/%%m/%%d %%H:%%M:%%S") UNBAN [Email] = <F-USER> [IP] = <ip> unbanned." >> ${iplimit_banned_log_path}
  1220. [Init]
  1221. name = default
  1222. protocol = tcp
  1223. chain = INPUT
  1224. EOF
  1225. echo -e "${green}Ip Limit jail files created with a bantime of ${bantime} minutes.${plain}"
  1226. }
  1227. iplimit_remove_conflicts() {
  1228. local jail_files=(
  1229. /etc/fail2ban/jail.conf
  1230. /etc/fail2ban/jail.local
  1231. )
  1232. for file in "${jail_files[@]}"; do
  1233. # Check for [3x-ipl] config in jail file then remove it
  1234. if test -f "${file}" && grep -qw '3x-ipl' ${file}; then
  1235. sed -i "/\[3x-ipl\]/,/^$/d" ${file}
  1236. echo -e "${yellow}Removing conflicts of [3x-ipl] in jail (${file})!${plain}\n"
  1237. fi
  1238. done
  1239. }
  1240. ip_validation() {
  1241. ipv6_regex="^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))$"
  1242. ipv4_regex="^((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]?|0)\.){3}(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]?|0)$"
  1243. }
  1244. iplimit_main() {
  1245. echo -e "\n${green}\t1.${plain} Install Fail2ban and configure IP Limit"
  1246. echo -e "${green}\t2.${plain} Change Ban Duration"
  1247. echo -e "${green}\t3.${plain} Unban Everyone"
  1248. echo -e "${green}\t4.${plain} Ban Logs"
  1249. echo -e "${green}\t5.${plain} Ban an IP Address"
  1250. echo -e "${green}\t6.${plain} Unban an IP Address"
  1251. echo -e "${green}\t7.${plain} Real-Time Logs"
  1252. echo -e "${green}\t8.${plain} Service Status"
  1253. echo -e "${green}\t9.${plain} Service Restart"
  1254. echo -e "${green}\t10.${plain} Uninstall Fail2ban and IP Limit"
  1255. echo -e "${green}\t0.${plain} Back to Main Menu"
  1256. read -rp "Choose an option: " choice
  1257. case "$choice" in
  1258. 0)
  1259. show_menu
  1260. ;;
  1261. 1)
  1262. confirm "Proceed with installation of Fail2ban & IP Limit?" "y"
  1263. if [[ $? == 0 ]]; then
  1264. install_iplimit
  1265. else
  1266. iplimit_main
  1267. fi
  1268. ;;
  1269. 2)
  1270. read -rp "Please enter new Ban Duration in Minutes [default 30]: " NUM
  1271. if [[ $NUM =~ ^[0-9]+$ ]]; then
  1272. create_iplimit_jails ${NUM}
  1273. systemctl restart fail2ban
  1274. else
  1275. echo -e "${red}${NUM} is not a number! Please, try again.${plain}"
  1276. fi
  1277. iplimit_main
  1278. ;;
  1279. 3)
  1280. confirm "Proceed with Unbanning everyone from IP Limit jail?" "y"
  1281. if [[ $? == 0 ]]; then
  1282. fail2ban-client reload --restart --unban 3x-ipl
  1283. truncate -s 0 "${iplimit_banned_log_path}"
  1284. echo -e "${green}All users Unbanned successfully.${plain}"
  1285. iplimit_main
  1286. else
  1287. echo -e "${yellow}Cancelled.${plain}"
  1288. fi
  1289. iplimit_main
  1290. ;;
  1291. 4)
  1292. show_banlog
  1293. iplimit_main
  1294. ;;
  1295. 5)
  1296. read -rp "Enter the IP address you want to ban: " ban_ip
  1297. ip_validation
  1298. if [[ $ban_ip =~ $ipv4_regex || $ban_ip =~ $ipv6_regex ]]; then
  1299. fail2ban-client set 3x-ipl banip "$ban_ip"
  1300. echo -e "${green}IP Address ${ban_ip} has been banned successfully.${plain}"
  1301. else
  1302. echo -e "${red}Invalid IP address format! Please try again.${plain}"
  1303. fi
  1304. iplimit_main
  1305. ;;
  1306. 6)
  1307. read -rp "Enter the IP address you want to unban: " unban_ip
  1308. ip_validation
  1309. if [[ $unban_ip =~ $ipv4_regex || $unban_ip =~ $ipv6_regex ]]; then
  1310. fail2ban-client set 3x-ipl unbanip "$unban_ip"
  1311. echo -e "${green}IP Address ${unban_ip} has been unbanned successfully.${plain}"
  1312. else
  1313. echo -e "${red}Invalid IP address format! Please try again.${plain}"
  1314. fi
  1315. iplimit_main
  1316. ;;
  1317. 7)
  1318. tail -f /var/log/fail2ban.log
  1319. iplimit_main
  1320. ;;
  1321. 8)
  1322. service fail2ban status
  1323. iplimit_main
  1324. ;;
  1325. 9)
  1326. systemctl restart fail2ban
  1327. iplimit_main
  1328. ;;
  1329. 10)
  1330. remove_iplimit
  1331. iplimit_main
  1332. ;;
  1333. *)
  1334. echo -e "${red}Invalid option. Please select a valid number.${plain}\n"
  1335. iplimit_main
  1336. ;;
  1337. esac
  1338. }
  1339. install_iplimit() {
  1340. if ! command -v fail2ban-client &>/dev/null; then
  1341. echo -e "${green}Fail2ban is not installed. Installing now...!${plain}\n"
  1342. # Check the OS and install necessary packages
  1343. case "${release}" in
  1344. ubuntu)
  1345. if [[ "${os_version}" -ge 24 ]]; then
  1346. apt update && apt install python3-pip -y
  1347. python3 -m pip install pyasynchat --break-system-packages
  1348. fi
  1349. apt update && apt install fail2ban -y
  1350. ;;
  1351. debian | armbian)
  1352. apt update && apt install fail2ban -y
  1353. ;;
  1354. centos | almalinux | rocky | ol)
  1355. yum update -y && yum install epel-release -y
  1356. yum -y install fail2ban
  1357. ;;
  1358. fedora | amzn | virtuozzo)
  1359. dnf -y update && dnf -y install fail2ban
  1360. ;;
  1361. arch | manjaro | parch)
  1362. pacman -Syu --noconfirm fail2ban
  1363. ;;
  1364. *)
  1365. echo -e "${red}Unsupported operating system. Please check the script and install the necessary packages manually.${plain}\n"
  1366. exit 1
  1367. ;;
  1368. esac
  1369. if ! command -v fail2ban-client &>/dev/null; then
  1370. echo -e "${red}Fail2ban installation failed.${plain}\n"
  1371. exit 1
  1372. fi
  1373. echo -e "${green}Fail2ban installed successfully!${plain}\n"
  1374. else
  1375. echo -e "${yellow}Fail2ban is already installed.${plain}\n"
  1376. fi
  1377. echo -e "${green}Configuring IP Limit...${plain}\n"
  1378. # make sure there's no conflict for jail files
  1379. iplimit_remove_conflicts
  1380. # Check if log file exists
  1381. if ! test -f "${iplimit_banned_log_path}"; then
  1382. touch ${iplimit_banned_log_path}
  1383. fi
  1384. # Check if service log file exists so fail2ban won't return error
  1385. if ! test -f "${iplimit_log_path}"; then
  1386. touch ${iplimit_log_path}
  1387. fi
  1388. # Create the iplimit jail files
  1389. # we didn't pass the bantime here to use the default value
  1390. create_iplimit_jails
  1391. # Launching fail2ban
  1392. if ! systemctl is-active --quiet fail2ban; then
  1393. systemctl start fail2ban
  1394. else
  1395. systemctl restart fail2ban
  1396. fi
  1397. systemctl enable fail2ban
  1398. echo -e "${green}IP Limit installed and configured successfully!${plain}\n"
  1399. before_show_menu
  1400. }
  1401. remove_iplimit() {
  1402. echo -e "${green}\t1.${plain} Only remove IP Limit configurations"
  1403. echo -e "${green}\t2.${plain} Uninstall Fail2ban and IP Limit"
  1404. echo -e "${green}\t0.${plain} Back to Main Menu"
  1405. read -rp "Choose an option: " num
  1406. case "$num" in
  1407. 1)
  1408. rm -f /etc/fail2ban/filter.d/3x-ipl.conf
  1409. rm -f /etc/fail2ban/action.d/3x-ipl.conf
  1410. rm -f /etc/fail2ban/jail.d/3x-ipl.conf
  1411. systemctl restart fail2ban
  1412. echo -e "${green}IP Limit removed successfully!${plain}\n"
  1413. before_show_menu
  1414. ;;
  1415. 2)
  1416. rm -rf /etc/fail2ban
  1417. systemctl stop fail2ban
  1418. case "${release}" in
  1419. ubuntu | debian | armbian)
  1420. apt-get remove -y fail2ban
  1421. apt-get purge -y fail2ban -y
  1422. apt-get autoremove -y
  1423. ;;
  1424. centos | almalinux | rocky | ol)
  1425. yum remove fail2ban -y
  1426. yum autoremove -y
  1427. ;;
  1428. fedora | amzn | virtuozzo)
  1429. dnf remove fail2ban -y
  1430. dnf autoremove -y
  1431. ;;
  1432. arch | manjaro | parch)
  1433. pacman -Rns --noconfirm fail2ban
  1434. ;;
  1435. *)
  1436. echo -e "${red}Unsupported operating system. Please uninstall Fail2ban manually.${plain}\n"
  1437. exit 1
  1438. ;;
  1439. esac
  1440. echo -e "${green}Fail2ban and IP Limit removed successfully!${plain}\n"
  1441. before_show_menu
  1442. ;;
  1443. 0)
  1444. show_menu
  1445. ;;
  1446. *)
  1447. echo -e "${red}Invalid option. Please select a valid number.${plain}\n"
  1448. remove_iplimit
  1449. ;;
  1450. esac
  1451. }
  1452. SSH_port_forwarding() {
  1453. local server_ip=$(curl -s https://api.ipify.org)
  1454. local existing_webBasePath=$(/usr/local/x-ui/x-ui setting -show true | grep -Eo 'webBasePath: .+' | awk '{print $2}')
  1455. local existing_port=$(/usr/local/x-ui/x-ui setting -show true | grep -Eo 'port: .+' | awk '{print $2}')
  1456. local existing_listenIP=$(/usr/local/x-ui/x-ui setting -getListen true | grep -Eo 'listenIP: .+' | awk '{print $2}')
  1457. local existing_cert=$(/usr/local/x-ui/x-ui setting -getCert true | grep -Eo 'cert: .+' | awk '{print $2}')
  1458. local existing_key=$(/usr/local/x-ui/x-ui setting -getCert true | grep -Eo 'key: .+' | awk '{print $2}')
  1459. local config_listenIP=""
  1460. local listen_choice=""
  1461. if [[ -n "$existing_cert" && -n "$existing_key" ]]; then
  1462. echo -e "${green}Panel is secure with SSL.${plain}"
  1463. before_show_menu
  1464. fi
  1465. if [[ -z "$existing_cert" && -z "$existing_key" && (-z "$existing_listenIP" || "$existing_listenIP" == "0.0.0.0") ]]; then
  1466. echo -e "\n${red}Warning: No Cert and Key found! The panel is not secure.${plain}"
  1467. echo "Please obtain a certificate or set up SSH port forwarding."
  1468. fi
  1469. if [[ -n "$existing_listenIP" && "$existing_listenIP" != "0.0.0.0" && (-z "$existing_cert" && -z "$existing_key") ]]; then
  1470. echo -e "\n${green}Current SSH Port Forwarding Configuration:${plain}"
  1471. echo -e "Standard SSH command:"
  1472. echo -e "${yellow}ssh -L 2222:${existing_listenIP}:${existing_port} root@${server_ip}${plain}"
  1473. echo -e "\nIf using SSH key:"
  1474. echo -e "${yellow}ssh -i <sshkeypath> -L 2222:${existing_listenIP}:${existing_port} root@${server_ip}${plain}"
  1475. echo -e "\nAfter connecting, access the panel at:"
  1476. echo -e "${yellow}http://localhost:2222${existing_webBasePath}${plain}"
  1477. fi
  1478. echo -e "\nChoose an option:"
  1479. echo -e "${green}1.${plain} Set listen IP"
  1480. echo -e "${green}2.${plain} Clear listen IP"
  1481. echo -e "${green}0.${plain} Back to Main Menu"
  1482. read -rp "Choose an option: " num
  1483. case "$num" in
  1484. 1)
  1485. if [[ -z "$existing_listenIP" || "$existing_listenIP" == "0.0.0.0" ]]; then
  1486. echo -e "\nNo listenIP configured. Choose an option:"
  1487. echo -e "1. Use default IP (127.0.0.1)"
  1488. echo -e "2. Set a custom IP"
  1489. read -rp "Select an option (1 or 2): " listen_choice
  1490. config_listenIP="127.0.0.1"
  1491. [[ "$listen_choice" == "2" ]] && read -rp "Enter custom IP to listen on: " config_listenIP
  1492. /usr/local/x-ui/x-ui setting -listenIP "${config_listenIP}" >/dev/null 2>&1
  1493. echo -e "${green}listen IP has been set to ${config_listenIP}.${plain}"
  1494. echo -e "\n${green}SSH Port Forwarding Configuration:${plain}"
  1495. echo -e "Standard SSH command:"
  1496. echo -e "${yellow}ssh -L 2222:${config_listenIP}:${existing_port} root@${server_ip}${plain}"
  1497. echo -e "\nIf using SSH key:"
  1498. echo -e "${yellow}ssh -i <sshkeypath> -L 2222:${config_listenIP}:${existing_port} root@${server_ip}${plain}"
  1499. echo -e "\nAfter connecting, access the panel at:"
  1500. echo -e "${yellow}http://localhost:2222${existing_webBasePath}${plain}"
  1501. restart
  1502. else
  1503. config_listenIP="${existing_listenIP}"
  1504. echo -e "${green}Current listen IP is already set to ${config_listenIP}.${plain}"
  1505. fi
  1506. ;;
  1507. 2)
  1508. /usr/local/x-ui/x-ui setting -listenIP 0.0.0.0 >/dev/null 2>&1
  1509. echo -e "${green}Listen IP has been cleared.${plain}"
  1510. restart
  1511. ;;
  1512. 0)
  1513. show_menu
  1514. ;;
  1515. *)
  1516. echo -e "${red}Invalid option. Please select a valid number.${plain}\n"
  1517. SSH_port_forwarding
  1518. ;;
  1519. esac
  1520. }
  1521. show_usage() {
  1522. echo -e "┌───────────────────────────────────────────────────────┐
  1523. │ ${blue}x-ui control menu usages (subcommands):${plain} │
  1524. │ │
  1525. │ ${blue}x-ui${plain} - Admin Management Script │
  1526. │ ${blue}x-ui start${plain} - Start │
  1527. │ ${blue}x-ui stop${plain} - Stop │
  1528. │ ${blue}x-ui restart${plain} - Restart │
  1529. │ ${blue}x-ui status${plain} - Current Status │
  1530. │ ${blue}x-ui settings${plain} - Current Settings │
  1531. │ ${blue}x-ui enable${plain} - Enable Autostart on OS Startup │
  1532. │ ${blue}x-ui disable${plain} - Disable Autostart on OS Startup │
  1533. │ ${blue}x-ui log${plain} - Check logs │
  1534. │ ${blue}x-ui banlog${plain} - Check Fail2ban ban logs │
  1535. │ ${blue}x-ui update${plain} - Update │
  1536. │ ${blue}x-ui legacy${plain} - legacy version │
  1537. │ ${blue}x-ui install${plain} - Install │
  1538. │ ${blue}x-ui uninstall${plain} - Uninstall │
  1539. └───────────────────────────────────────────────────────┘"
  1540. }
  1541. show_menu() {
  1542. echo -e "
  1543. ╔────────────────────────────────────────────────╗
  1544. │ ${green}3X-UI Panel Management Script${plain} │
  1545. │ ${green}0.${plain} Exit Script │
  1546. │────────────────────────────────────────────────│
  1547. │ ${green}1.${plain} Install │
  1548. │ ${green}2.${plain} Update │
  1549. │ ${green}3.${plain} Update Menu │
  1550. │ ${green}4.${plain} Legacy Version │
  1551. │ ${green}5.${plain} Uninstall │
  1552. │────────────────────────────────────────────────│
  1553. │ ${green}6.${plain} Reset Username & Password │
  1554. │ ${green}7.${plain} Reset Web Base Path │
  1555. │ ${green}8.${plain} Reset Settings │
  1556. │ ${green}9.${plain} Change Port │
  1557. │ ${green}10.${plain} View Current Settings │
  1558. │────────────────────────────────────────────────│
  1559. │ ${green}11.${plain} Start │
  1560. │ ${green}12.${plain} Stop │
  1561. │ ${green}13.${plain} Restart │
  1562. │ ${green}14.${plain} Check Status │
  1563. │ ${green}15.${plain} Logs Management │
  1564. │────────────────────────────────────────────────│
  1565. │ ${green}16.${plain} Enable Autostart │
  1566. │ ${green}17.${plain} Disable Autostart │
  1567. │────────────────────────────────────────────────│
  1568. │ ${green}18.${plain} SSL Certificate Management │
  1569. │ ${green}19.${plain} Cloudflare SSL Certificate │
  1570. │ ${green}20.${plain} IP Limit Management │
  1571. │ ${green}21.${plain} Firewall Management │
  1572. │ ${green}22.${plain} SSH Port Forwarding Management │
  1573. │────────────────────────────────────────────────│
  1574. │ ${green}23.${plain} Enable BBR │
  1575. │ ${green}24.${plain} Update Geo Files │
  1576. │ ${green}25.${plain} Speedtest by Ookla │
  1577. ╚────────────────────────────────────────────────╝
  1578. "
  1579. show_status
  1580. echo && read -rp "Please enter your selection [0-25]: " num
  1581. case "${num}" in
  1582. 0)
  1583. exit 0
  1584. ;;
  1585. 1)
  1586. check_uninstall && install
  1587. ;;
  1588. 2)
  1589. check_install && update
  1590. ;;
  1591. 3)
  1592. check_install && update_menu
  1593. ;;
  1594. 4)
  1595. check_install && legacy_version
  1596. ;;
  1597. 5)
  1598. check_install && uninstall
  1599. ;;
  1600. 6)
  1601. check_install && reset_user
  1602. ;;
  1603. 7)
  1604. check_install && reset_webbasepath
  1605. ;;
  1606. 8)
  1607. check_install && reset_config
  1608. ;;
  1609. 9)
  1610. check_install && set_port
  1611. ;;
  1612. 10)
  1613. check_install && check_config
  1614. ;;
  1615. 11)
  1616. check_install && start
  1617. ;;
  1618. 12)
  1619. check_install && stop
  1620. ;;
  1621. 13)
  1622. check_install && restart
  1623. ;;
  1624. 14)
  1625. check_install && status
  1626. ;;
  1627. 15)
  1628. check_install && show_log
  1629. ;;
  1630. 16)
  1631. check_install && enable
  1632. ;;
  1633. 17)
  1634. check_install && disable
  1635. ;;
  1636. 18)
  1637. ssl_cert_issue_main
  1638. ;;
  1639. 19)
  1640. ssl_cert_issue_CF
  1641. ;;
  1642. 20)
  1643. iplimit_main
  1644. ;;
  1645. 21)
  1646. firewall_menu
  1647. ;;
  1648. 22)
  1649. SSH_port_forwarding
  1650. ;;
  1651. 23)
  1652. bbr_menu
  1653. ;;
  1654. 24)
  1655. update_geo
  1656. ;;
  1657. 25)
  1658. run_speedtest
  1659. ;;
  1660. *)
  1661. LOGE "Please enter the correct number [0-25]"
  1662. ;;
  1663. esac
  1664. }
  1665. if [[ $# > 0 ]]; then
  1666. case $1 in
  1667. "start")
  1668. check_install 0 && start 0
  1669. ;;
  1670. "stop")
  1671. check_install 0 && stop 0
  1672. ;;
  1673. "restart")
  1674. check_install 0 && restart 0
  1675. ;;
  1676. "status")
  1677. check_install 0 && status 0
  1678. ;;
  1679. "settings")
  1680. check_install 0 && check_config 0
  1681. ;;
  1682. "enable")
  1683. check_install 0 && enable 0
  1684. ;;
  1685. "disable")
  1686. check_install 0 && disable 0
  1687. ;;
  1688. "log")
  1689. check_install 0 && show_log 0
  1690. ;;
  1691. "banlog")
  1692. check_install 0 && show_banlog 0
  1693. ;;
  1694. "update")
  1695. check_install 0 && update 0
  1696. ;;
  1697. "legacy")
  1698. check_install 0 && legacy_version 0
  1699. ;;
  1700. "install")
  1701. check_uninstall 0 && install 0
  1702. ;;
  1703. "uninstall")
  1704. check_install 0 && uninstall 0
  1705. ;;
  1706. *) show_usage ;;
  1707. esac
  1708. else
  1709. show_menu
  1710. fi