x-ui.sh 63 KB

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