x-ui.sh 63 KB

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