1
0

x-ui.sh 64 KB

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