1
0

x-ui.sh 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920
  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. 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 20.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}
  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. certPath="/root/cert-CF"
  1090. if [ ! -d "$certPath" ]; then
  1091. mkdir -p $certPath
  1092. else
  1093. rm -rf $certPath
  1094. mkdir -p $certPath
  1095. fi
  1096. LOGD "Please set a domain name:"
  1097. read -p "Input your domain here: " CF_Domain
  1098. LOGD "Your domain name is set to: ${CF_Domain}"
  1099. # Set up Cloudflare API details
  1100. CF_GlobalKey=""
  1101. CF_AccountEmail=""
  1102. LOGD "Please set the API key:"
  1103. read -p "Input your key here: " CF_GlobalKey
  1104. LOGD "Your API key is: ${CF_GlobalKey}"
  1105. LOGD "Please set up registered email:"
  1106. read -p "Input your email here: " CF_AccountEmail
  1107. LOGD "Your registered email address is: ${CF_AccountEmail}"
  1108. # Set the default CA to Let's Encrypt
  1109. ~/.acme.sh/acme.sh --set-default-ca --server letsencrypt
  1110. if [ $? -ne 0 ]; then
  1111. LOGE "Default CA, Let'sEncrypt fail, script exiting..."
  1112. exit 1
  1113. fi
  1114. export CF_Key="${CF_GlobalKey}"
  1115. export CF_Email="${CF_AccountEmail}"
  1116. # Issue the certificate using Cloudflare DNS
  1117. ~/.acme.sh/acme.sh --issue --dns dns_cf -d ${CF_Domain} -d *.${CF_Domain} --log
  1118. if [ $? -ne 0 ]; then
  1119. LOGE "Certificate issuance failed, script exiting..."
  1120. exit 1
  1121. else
  1122. LOGI "Certificate issued successfully, Installing..."
  1123. fi
  1124. # Install the certificate
  1125. mkdir -p ${certPath}/${CF_Domain}
  1126. if [ $? -ne 0 ]; then
  1127. LOGE "Failed to create directory: ${certPath}/${CF_Domain}"
  1128. exit 1
  1129. fi
  1130. ~/.acme.sh/acme.sh --installcert -d ${CF_Domain} -d *.${CF_Domain} \
  1131. --fullchain-file ${certPath}/${CF_Domain}/fullchain.pem \
  1132. --key-file ${certPath}/${CF_Domain}/privkey.pem
  1133. if [ $? -ne 0 ]; then
  1134. LOGE "Certificate installation failed, script exiting..."
  1135. exit 1
  1136. else
  1137. LOGI "Certificate installed successfully, Turning on automatic updates..."
  1138. fi
  1139. # Enable auto-update
  1140. ~/.acme.sh/acme.sh --upgrade --auto-upgrade
  1141. if [ $? -ne 0 ]; then
  1142. LOGE "Auto update setup failed, script exiting..."
  1143. exit 1
  1144. else
  1145. LOGI "The certificate is installed and auto-renewal is turned on. Specific information is as follows:"
  1146. ls -lah ${certPath}/${CF_Domain}
  1147. chmod 755 ${certPath}/${CF_Domain}
  1148. fi
  1149. # Prompt user to set panel paths after successful certificate installation
  1150. read -p "Would you like to set this certificate for the panel? (y/n): " setPanel
  1151. if [[ "$setPanel" == "y" || "$setPanel" == "Y" ]]; then
  1152. local webCertFile="${certPath}/${CF_Domain}/fullchain.pem"
  1153. local webKeyFile="${certPath}/${CF_Domain}/privkey.pem"
  1154. if [[ -f "$webCertFile" && -f "$webKeyFile" ]]; then
  1155. /usr/local/x-ui/x-ui cert -webCert "$webCertFile" -webCertKey "$webKeyFile"
  1156. LOGI "Panel paths set for domain: $CF_Domain"
  1157. LOGI " - Certificate File: $webCertFile"
  1158. LOGI " - Private Key File: $webKeyFile"
  1159. echo -e "${green}Access URL: https://${CF_Domain}:${existing_port}${existing_webBasePath}${plain}"
  1160. restart
  1161. else
  1162. LOGE "Error: Certificate or private key file not found for domain: $CF_Domain."
  1163. fi
  1164. else
  1165. LOGI "Skipping panel path setting."
  1166. fi
  1167. else
  1168. show_menu
  1169. fi
  1170. }
  1171. run_speedtest() {
  1172. # Check if Speedtest is already installed
  1173. if ! command -v speedtest &>/dev/null; then
  1174. # If not installed, determine installation method
  1175. if command -v snap &>/dev/null; then
  1176. # Use snap to install Speedtest
  1177. echo "Installing Speedtest using snap..."
  1178. snap install speedtest
  1179. else
  1180. # Fallback to using package managers
  1181. local pkg_manager=""
  1182. local speedtest_install_script=""
  1183. if command -v dnf &>/dev/null; then
  1184. pkg_manager="dnf"
  1185. speedtest_install_script="https://packagecloud.io/install/repositories/ookla/speedtest-cli/script.rpm.sh"
  1186. elif command -v yum &>/dev/null; then
  1187. pkg_manager="yum"
  1188. speedtest_install_script="https://packagecloud.io/install/repositories/ookla/speedtest-cli/script.rpm.sh"
  1189. elif command -v apt-get &>/dev/null; then
  1190. pkg_manager="apt-get"
  1191. speedtest_install_script="https://packagecloud.io/install/repositories/ookla/speedtest-cli/script.deb.sh"
  1192. elif command -v apt &>/dev/null; then
  1193. pkg_manager="apt"
  1194. speedtest_install_script="https://packagecloud.io/install/repositories/ookla/speedtest-cli/script.deb.sh"
  1195. fi
  1196. if [[ -z $pkg_manager ]]; then
  1197. echo "Error: Package manager not found. You may need to install Speedtest manually."
  1198. return 1
  1199. else
  1200. echo "Installing Speedtest using $pkg_manager..."
  1201. curl -s $speedtest_install_script | bash
  1202. $pkg_manager install -y speedtest
  1203. fi
  1204. fi
  1205. fi
  1206. speedtest
  1207. }
  1208. create_iplimit_jails() {
  1209. # Use default bantime if not passed => 30 minutes
  1210. local bantime="${1:-30}"
  1211. # Uncomment 'allowipv6 = auto' in fail2ban.conf
  1212. sed -i 's/#allowipv6 = auto/allowipv6 = auto/g' /etc/fail2ban/fail2ban.conf
  1213. # On Debian 12+ fail2ban's default backend should be changed to systemd
  1214. if [[ "${release}" == "debian" && ${os_version} -ge 12 ]]; then
  1215. sed -i '0,/action =/s/backend = auto/backend = systemd/' /etc/fail2ban/jail.conf
  1216. fi
  1217. cat << EOF > /etc/fail2ban/jail.d/3x-ipl.conf
  1218. [3x-ipl]
  1219. enabled=true
  1220. backend=auto
  1221. filter=3x-ipl
  1222. action=3x-ipl
  1223. logpath=${iplimit_log_path}
  1224. maxretry=2
  1225. findtime=32
  1226. bantime=${bantime}m
  1227. EOF
  1228. cat << EOF > /etc/fail2ban/filter.d/3x-ipl.conf
  1229. [Definition]
  1230. datepattern = ^%%Y/%%m/%%d %%H:%%M:%%S
  1231. failregex = \[LIMIT_IP\]\s*Email\s*=\s*<F-USER>.+</F-USER>\s*\|\|\s*SRC\s*=\s*<ADDR>
  1232. ignoreregex =
  1233. EOF
  1234. cat << EOF > /etc/fail2ban/action.d/3x-ipl.conf
  1235. [INCLUDES]
  1236. before = iptables-allports.conf
  1237. [Definition]
  1238. actionstart = <iptables> -N f2b-<name>
  1239. <iptables> -A f2b-<name> -j <returntype>
  1240. <iptables> -I <chain> -p <protocol> -j f2b-<name>
  1241. actionstop = <iptables> -D <chain> -p <protocol> -j f2b-<name>
  1242. <actionflush>
  1243. <iptables> -X f2b-<name>
  1244. actioncheck = <iptables> -n -L <chain> | grep -q 'f2b-<name>[ \t]'
  1245. actionban = <iptables> -I f2b-<name> 1 -s <ip> -j <blocktype>
  1246. echo "\$(date +"%%Y/%%m/%%d %%H:%%M:%%S") BAN [Email] = <F-USER> [IP] = <ip> banned for <bantime> seconds." >> ${iplimit_banned_log_path}
  1247. actionunban = <iptables> -D f2b-<name> -s <ip> -j <blocktype>
  1248. echo "\$(date +"%%Y/%%m/%%d %%H:%%M:%%S") UNBAN [Email] = <F-USER> [IP] = <ip> unbanned." >> ${iplimit_banned_log_path}
  1249. [Init]
  1250. name = default
  1251. protocol = tcp
  1252. chain = INPUT
  1253. EOF
  1254. echo -e "${green}Ip Limit jail files created with a bantime of ${bantime} minutes.${plain}"
  1255. }
  1256. iplimit_remove_conflicts() {
  1257. local jail_files=(
  1258. /etc/fail2ban/jail.conf
  1259. /etc/fail2ban/jail.local
  1260. )
  1261. for file in "${jail_files[@]}"; do
  1262. # Check for [3x-ipl] config in jail file then remove it
  1263. if test -f "${file}" && grep -qw '3x-ipl' ${file}; then
  1264. sed -i "/\[3x-ipl\]/,/^$/d" ${file}
  1265. echo -e "${yellow}Removing conflicts of [3x-ipl] in jail (${file})!${plain}\n"
  1266. fi
  1267. done
  1268. }
  1269. ip_validation() {
  1270. 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]))$"
  1271. 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)$"
  1272. }
  1273. iplimit_main() {
  1274. echo -e "\n${green}\t1.${plain} Install Fail2ban and configure IP Limit"
  1275. echo -e "${green}\t2.${plain} Change Ban Duration"
  1276. echo -e "${green}\t3.${plain} Unban Everyone"
  1277. echo -e "${green}\t4.${plain} Ban Logs"
  1278. echo -e "${green}\t5.${plain} Ban an IP Address"
  1279. echo -e "${green}\t6.${plain} Unban an IP Address"
  1280. echo -e "${green}\t7.${plain} Real-Time Logs"
  1281. echo -e "${green}\t8.${plain} Service Status"
  1282. echo -e "${green}\t9.${plain} Service Restart"
  1283. echo -e "${green}\t10.${plain} Uninstall Fail2ban and IP Limit"
  1284. echo -e "${green}\t0.${plain} Back to Main Menu"
  1285. read -p "Choose an option: " choice
  1286. case "$choice" in
  1287. 0)
  1288. show_menu
  1289. ;;
  1290. 1)
  1291. confirm "Proceed with installation of Fail2ban & IP Limit?" "y"
  1292. if [[ $? == 0 ]]; then
  1293. install_iplimit
  1294. else
  1295. iplimit_main
  1296. fi
  1297. ;;
  1298. 2)
  1299. read -rp "Please enter new Ban Duration in Minutes [default 30]: " NUM
  1300. if [[ $NUM =~ ^[0-9]+$ ]]; then
  1301. create_iplimit_jails ${NUM}
  1302. systemctl restart fail2ban
  1303. else
  1304. echo -e "${red}${NUM} is not a number! Please, try again.${plain}"
  1305. fi
  1306. iplimit_main
  1307. ;;
  1308. 3)
  1309. confirm "Proceed with Unbanning everyone from IP Limit jail?" "y"
  1310. if [[ $? == 0 ]]; then
  1311. fail2ban-client reload --restart --unban 3x-ipl
  1312. truncate -s 0 "${iplimit_banned_log_path}"
  1313. echo -e "${green}All users Unbanned successfully.${plain}"
  1314. iplimit_main
  1315. else
  1316. echo -e "${yellow}Cancelled.${plain}"
  1317. fi
  1318. iplimit_main
  1319. ;;
  1320. 4)
  1321. show_banlog
  1322. iplimit_main
  1323. ;;
  1324. 5)
  1325. read -rp "Enter the IP address you want to ban: " ban_ip
  1326. ip_validation
  1327. if [[ $ban_ip =~ $ipv4_regex || $ban_ip =~ $ipv6_regex ]]; then
  1328. fail2ban-client set 3x-ipl banip "$ban_ip"
  1329. echo -e "${green}IP Address ${ban_ip} has been banned successfully.${plain}"
  1330. else
  1331. echo -e "${red}Invalid IP address format! Please try again.${plain}"
  1332. fi
  1333. iplimit_main
  1334. ;;
  1335. 6)
  1336. read -rp "Enter the IP address you want to unban: " unban_ip
  1337. ip_validation
  1338. if [[ $unban_ip =~ $ipv4_regex || $unban_ip =~ $ipv6_regex ]]; then
  1339. fail2ban-client set 3x-ipl unbanip "$unban_ip"
  1340. echo -e "${green}IP Address ${unban_ip} has been unbanned successfully.${plain}"
  1341. else
  1342. echo -e "${red}Invalid IP address format! Please try again.${plain}"
  1343. fi
  1344. iplimit_main
  1345. ;;
  1346. 7)
  1347. tail -f /var/log/fail2ban.log
  1348. iplimit_main
  1349. ;;
  1350. 8)
  1351. service fail2ban status
  1352. iplimit_main
  1353. ;;
  1354. 9)
  1355. systemctl restart fail2ban
  1356. iplimit_main
  1357. ;;
  1358. 10)
  1359. remove_iplimit
  1360. iplimit_main
  1361. ;;
  1362. *)
  1363. echo -e "${red}Invalid option. Please select a valid number.${plain}\n"
  1364. iplimit_main
  1365. ;;
  1366. esac
  1367. }
  1368. install_iplimit() {
  1369. if ! command -v fail2ban-client &>/dev/null; then
  1370. echo -e "${green}Fail2ban is not installed. Installing now...!${plain}\n"
  1371. # Check the OS and install necessary packages
  1372. case "${release}" in
  1373. ubuntu)
  1374. if [[ "${os_version}" -ge 24 ]]; then
  1375. apt update && apt install python3-pip -y
  1376. python3 -m pip install pyasynchat --break-system-packages
  1377. fi
  1378. apt update && apt install fail2ban -y
  1379. ;;
  1380. debian | armbian)
  1381. apt update && apt install fail2ban -y
  1382. ;;
  1383. centos | almalinux | rocky | ol)
  1384. yum update -y && yum install epel-release -y
  1385. yum -y install fail2ban
  1386. ;;
  1387. fedora | amzn | virtuozzo)
  1388. dnf -y update && dnf -y install fail2ban
  1389. ;;
  1390. arch | manjaro | parch)
  1391. pacman -Syu --noconfirm fail2ban
  1392. ;;
  1393. *)
  1394. echo -e "${red}Unsupported operating system. Please check the script and install the necessary packages manually.${plain}\n"
  1395. exit 1
  1396. ;;
  1397. esac
  1398. if ! command -v fail2ban-client &>/dev/null; then
  1399. echo -e "${red}Fail2ban installation failed.${plain}\n"
  1400. exit 1
  1401. fi
  1402. echo -e "${green}Fail2ban installed successfully!${plain}\n"
  1403. else
  1404. echo -e "${yellow}Fail2ban is already installed.${plain}\n"
  1405. fi
  1406. echo -e "${green}Configuring IP Limit...${plain}\n"
  1407. # make sure there's no conflict for jail files
  1408. iplimit_remove_conflicts
  1409. # Check if log file exists
  1410. if ! test -f "${iplimit_banned_log_path}"; then
  1411. touch ${iplimit_banned_log_path}
  1412. fi
  1413. # Check if service log file exists so fail2ban won't return error
  1414. if ! test -f "${iplimit_log_path}"; then
  1415. touch ${iplimit_log_path}
  1416. fi
  1417. # Create the iplimit jail files
  1418. # we didn't pass the bantime here to use the default value
  1419. create_iplimit_jails
  1420. # Launching fail2ban
  1421. if ! systemctl is-active --quiet fail2ban; then
  1422. systemctl start fail2ban
  1423. systemctl enable fail2ban
  1424. else
  1425. systemctl restart fail2ban
  1426. fi
  1427. systemctl enable fail2ban
  1428. echo -e "${green}IP Limit installed and configured successfully!${plain}\n"
  1429. before_show_menu
  1430. }
  1431. remove_iplimit() {
  1432. echo -e "${green}\t1.${plain} Only remove IP Limit configurations"
  1433. echo -e "${green}\t2.${plain} Uninstall Fail2ban and IP Limit"
  1434. echo -e "${green}\t0.${plain} Back to Main Menu"
  1435. read -p "Choose an option: " num
  1436. case "$num" in
  1437. 1)
  1438. rm -f /etc/fail2ban/filter.d/3x-ipl.conf
  1439. rm -f /etc/fail2ban/action.d/3x-ipl.conf
  1440. rm -f /etc/fail2ban/jail.d/3x-ipl.conf
  1441. systemctl restart fail2ban
  1442. echo -e "${green}IP Limit removed successfully!${plain}\n"
  1443. before_show_menu
  1444. ;;
  1445. 2)
  1446. rm -rf /etc/fail2ban
  1447. systemctl stop fail2ban
  1448. case "${release}" in
  1449. ubuntu | debian | armbian)
  1450. apt-get remove -y fail2ban
  1451. apt-get purge -y fail2ban -y
  1452. apt-get autoremove -y
  1453. ;;
  1454. centos | almalinux | rocky | ol)
  1455. yum remove fail2ban -y
  1456. yum autoremove -y
  1457. ;;
  1458. fedora | amzn | virtuozzo)
  1459. dnf remove fail2ban -y
  1460. dnf autoremove -y
  1461. ;;
  1462. arch | manjaro | parch)
  1463. pacman -Rns --noconfirm fail2ban
  1464. ;;
  1465. *)
  1466. echo -e "${red}Unsupported operating system. Please uninstall Fail2ban manually.${plain}\n"
  1467. exit 1
  1468. ;;
  1469. esac
  1470. echo -e "${green}Fail2ban and IP Limit removed successfully!${plain}\n"
  1471. before_show_menu
  1472. ;;
  1473. 0)
  1474. show_menu
  1475. ;;
  1476. *)
  1477. echo -e "${red}Invalid option. Please select a valid number.${plain}\n"
  1478. remove_iplimit
  1479. ;;
  1480. esac
  1481. }
  1482. SSH_port_forwarding() {
  1483. local server_ip=$(curl -s https://api.ipify.org)
  1484. local existing_webBasePath=$(/usr/local/x-ui/x-ui setting -show true | grep -Eo 'webBasePath: .+' | awk '{print $2}')
  1485. local existing_port=$(/usr/local/x-ui/x-ui setting -show true | grep -Eo 'port: .+' | awk '{print $2}')
  1486. local existing_listenIP=$(/usr/local/x-ui/x-ui setting -getListen true | grep -Eo 'listenIP: .+' | awk '{print $2}')
  1487. local existing_cert=$(/usr/local/x-ui/x-ui setting -getCert true | grep -Eo 'cert: .+' | awk '{print $2}')
  1488. local existing_key=$(/usr/local/x-ui/x-ui setting -getCert true | grep -Eo 'key: .+' | awk '{print $2}')
  1489. local config_listenIP=""
  1490. local listen_choice=""
  1491. if [[ -n "$existing_cert" && -n "$existing_key" ]]; then
  1492. echo -e "${green}Panel is secure with SSL.${plain}"
  1493. before_show_menu
  1494. fi
  1495. if [[ -z "$existing_cert" && -z "$existing_key" && (-z "$existing_listenIP" || "$existing_listenIP" == "0.0.0.0") ]]; then
  1496. echo -e "\n${red}Warning: No Cert and Key found! The panel is not secure.${plain}"
  1497. echo "Please obtain a certificate or set up SSH port forwarding."
  1498. fi
  1499. if [[ -n "$existing_listenIP" && "$existing_listenIP" != "0.0.0.0" && (-z "$existing_cert" && -z "$existing_key") ]]; then
  1500. echo -e "\n${green}Current SSH Port Forwarding Configuration:${plain}"
  1501. echo -e "Standard SSH command:"
  1502. echo -e "${yellow}ssh -L 2222:${existing_listenIP}:${existing_port} root@${server_ip}${plain}"
  1503. echo -e "\nIf using SSH key:"
  1504. echo -e "${yellow}ssh -i <sshkeypath> -L 2222:${existing_listenIP}:${existing_port} root@${server_ip}${plain}"
  1505. echo -e "\nAfter connecting, access the panel at:"
  1506. echo -e "${yellow}http://localhost:2222${existing_webBasePath}${plain}"
  1507. fi
  1508. echo -e "\nChoose an option:"
  1509. echo -e "${green}1.${plain} Set listen IP"
  1510. echo -e "${green}2.${plain} Clear listen IP"
  1511. echo -e "${green}0.${plain} Back to Main Menu"
  1512. read -p "Choose an option: " num
  1513. case "$num" in
  1514. 1)
  1515. if [[ -z "$existing_listenIP" || "$existing_listenIP" == "0.0.0.0" ]]; then
  1516. echo -e "\nNo listenIP configured. Choose an option:"
  1517. echo -e "1. Use default IP (127.0.0.1)"
  1518. echo -e "2. Set a custom IP"
  1519. read -p "Select an option (1 or 2): " listen_choice
  1520. config_listenIP="127.0.0.1"
  1521. [[ "$listen_choice" == "2" ]] && read -p "Enter custom IP to listen on: " config_listenIP
  1522. /usr/local/x-ui/x-ui setting -listenIP "${config_listenIP}" >/dev/null 2>&1
  1523. echo -e "${green}listen IP has been set to ${config_listenIP}.${plain}"
  1524. echo -e "\n${green}SSH Port Forwarding Configuration:${plain}"
  1525. echo -e "Standard SSH command:"
  1526. echo -e "${yellow}ssh -L 2222:${config_listenIP}:${existing_port} root@${server_ip}${plain}"
  1527. echo -e "\nIf using SSH key:"
  1528. echo -e "${yellow}ssh -i <sshkeypath> -L 2222:${config_listenIP}:${existing_port} root@${server_ip}${plain}"
  1529. echo -e "\nAfter connecting, access the panel at:"
  1530. echo -e "${yellow}http://localhost:2222${existing_webBasePath}${plain}"
  1531. restart
  1532. else
  1533. config_listenIP="${existing_listenIP}"
  1534. echo -e "${green}Current listen IP is already set to ${config_listenIP}.${plain}"
  1535. fi
  1536. ;;
  1537. 2)
  1538. /usr/local/x-ui/x-ui setting -listenIP 0.0.0.0 >/dev/null 2>&1
  1539. echo -e "${green}Listen IP has been cleared.${plain}"
  1540. restart
  1541. ;;
  1542. 0)
  1543. show_menu
  1544. ;;
  1545. *)
  1546. echo -e "${red}Invalid option. Please select a valid number.${plain}\n"
  1547. SSH_port_forwarding
  1548. ;;
  1549. esac
  1550. }
  1551. show_usage() {
  1552. echo -e "┌───────────────────────────────────────────────────────┐
  1553. │ ${blue}x-ui control menu usages (subcommands):${plain} │
  1554. │ │
  1555. │ ${blue}x-ui${plain} - Admin Management Script │
  1556. │ ${blue}x-ui start${plain} - Start │
  1557. │ ${blue}x-ui stop${plain} - Stop │
  1558. │ ${blue}x-ui restart${plain} - Restart │
  1559. │ ${blue}x-ui status${plain} - Current Status │
  1560. │ ${blue}x-ui settings${plain} - Current Settings │
  1561. │ ${blue}x-ui enable${plain} - Enable Autostart on OS Startup │
  1562. │ ${blue}x-ui disable${plain} - Disable Autostart on OS Startup │
  1563. │ ${blue}x-ui log${plain} - Check logs │
  1564. │ ${blue}x-ui banlog${plain} - Check Fail2ban ban logs │
  1565. │ ${blue}x-ui update${plain} - Update │
  1566. │ ${blue}x-ui legacy${plain} - legacy version │
  1567. │ ${blue}x-ui install${plain} - Install │
  1568. │ ${blue}x-ui uninstall${plain} - Uninstall │
  1569. └───────────────────────────────────────────────────────┘"
  1570. }
  1571. show_menu() {
  1572. echo -e "
  1573. ╔────────────────────────────────────────────────╗
  1574. │ ${green}3X-UI Panel Management Script${plain} │
  1575. │ ${green}0.${plain} Exit Script │
  1576. │────────────────────────────────────────────────│
  1577. │ ${green}1.${plain} Install │
  1578. │ ${green}2.${plain} Update │
  1579. │ ${green}3.${plain} Update Menu │
  1580. │ ${green}4.${plain} Legacy Version │
  1581. │ ${green}5.${plain} Uninstall │
  1582. │────────────────────────────────────────────────│
  1583. │ ${green}6.${plain} Reset Username & Password & Secret Token │
  1584. │ ${green}7.${plain} Reset Web Base Path │
  1585. │ ${green}8.${plain} Reset Settings │
  1586. │ ${green}9.${plain} Change Port │
  1587. │ ${green}10.${plain} View Current Settings │
  1588. │────────────────────────────────────────────────│
  1589. │ ${green}11.${plain} Start │
  1590. │ ${green}12.${plain} Stop │
  1591. │ ${green}13.${plain} Restart │
  1592. │ ${green}14.${plain} Check Status │
  1593. │ ${green}15.${plain} Logs Management │
  1594. │────────────────────────────────────────────────│
  1595. │ ${green}16.${plain} Enable Autostart │
  1596. │ ${green}17.${plain} Disable Autostart │
  1597. │────────────────────────────────────────────────│
  1598. │ ${green}18.${plain} SSL Certificate Management │
  1599. │ ${green}19.${plain} Cloudflare SSL Certificate │
  1600. │ ${green}20.${plain} IP Limit Management │
  1601. │ ${green}21.${plain} Firewall Management │
  1602. │ ${green}22.${plain} SSH Port Forwarding Management │
  1603. │────────────────────────────────────────────────│
  1604. │ ${green}23.${plain} Enable BBR │
  1605. │ ${green}24.${plain} Update Geo Files │
  1606. │ ${green}25.${plain} Speedtest by Ookla │
  1607. ╚────────────────────────────────────────────────╝
  1608. "
  1609. show_status
  1610. echo && read -p "Please enter your selection [0-25]: " num
  1611. case "${num}" in
  1612. 0)
  1613. exit 0
  1614. ;;
  1615. 1)
  1616. check_uninstall && install
  1617. ;;
  1618. 2)
  1619. check_install && update
  1620. ;;
  1621. 3)
  1622. check_install && update_menu
  1623. ;;
  1624. 4)
  1625. check_install && legacy_version
  1626. ;;
  1627. 5)
  1628. check_install && uninstall
  1629. ;;
  1630. 6)
  1631. check_install && reset_user
  1632. ;;
  1633. 7)
  1634. check_install && reset_webbasepath
  1635. ;;
  1636. 8)
  1637. check_install && reset_config
  1638. ;;
  1639. 9)
  1640. check_install && set_port
  1641. ;;
  1642. 10)
  1643. check_install && check_config
  1644. ;;
  1645. 11)
  1646. check_install && start
  1647. ;;
  1648. 12)
  1649. check_install && stop
  1650. ;;
  1651. 13)
  1652. check_install && restart
  1653. ;;
  1654. 14)
  1655. check_install && status
  1656. ;;
  1657. 15)
  1658. check_install && show_log
  1659. ;;
  1660. 16)
  1661. check_install && enable
  1662. ;;
  1663. 17)
  1664. check_install && disable
  1665. ;;
  1666. 18)
  1667. ssl_cert_issue_main
  1668. ;;
  1669. 19)
  1670. ssl_cert_issue_CF
  1671. ;;
  1672. 20)
  1673. iplimit_main
  1674. ;;
  1675. 21)
  1676. firewall_menu
  1677. ;;
  1678. 22)
  1679. SSH_port_forwarding
  1680. ;;
  1681. 23)
  1682. bbr_menu
  1683. ;;
  1684. 24)
  1685. update_geo
  1686. ;;
  1687. 25)
  1688. run_speedtest
  1689. ;;
  1690. *)
  1691. LOGE "Please enter the correct number [0-25]"
  1692. ;;
  1693. esac
  1694. }
  1695. if [[ $# > 0 ]]; then
  1696. case $1 in
  1697. "start")
  1698. check_install 0 && start 0
  1699. ;;
  1700. "stop")
  1701. check_install 0 && stop 0
  1702. ;;
  1703. "restart")
  1704. check_install 0 && restart 0
  1705. ;;
  1706. "status")
  1707. check_install 0 && status 0
  1708. ;;
  1709. "settings")
  1710. check_install 0 && check_config 0
  1711. ;;
  1712. "enable")
  1713. check_install 0 && enable 0
  1714. ;;
  1715. "disable")
  1716. check_install 0 && disable 0
  1717. ;;
  1718. "log")
  1719. check_install 0 && show_log 0
  1720. ;;
  1721. "banlog")
  1722. check_install 0 && show_banlog 0
  1723. ;;
  1724. "update")
  1725. check_install 0 && update 0
  1726. ;;
  1727. "legacy")
  1728. check_install 0 && legacy_version 0
  1729. ;;
  1730. "install")
  1731. check_uninstall 0 && install 0
  1732. ;;
  1733. "uninstall")
  1734. check_install 0 && uninstall 0
  1735. ;;
  1736. *) show_usage ;;
  1737. esac
  1738. else
  1739. show_menu
  1740. fi