x-ui.sh 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875
  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} Install Firewall"
  611. echo -e "${green}\t2.${plain} Port List"
  612. echo -e "${green}\t3.${plain} Open Ports"
  613. echo -e "${green}\t4.${plain} Delete Ports from List"
  614. echo -e "${green}\t5.${plain} Disable Firewall"
  615. echo -e "${green}\t6.${plain} Firewall Status"
  616. echo -e "${green}\t0.${plain} Back to Main Menu"
  617. read -p "Choose an option: " choice
  618. case "$choice" in
  619. 0)
  620. show_menu
  621. ;;
  622. 1)
  623. install_firewall
  624. firewall_menu
  625. ;;
  626. 2)
  627. ufw status numbered
  628. firewall_menu
  629. ;;
  630. 3)
  631. open_ports
  632. firewall_menu
  633. ;;
  634. 4)
  635. delete_ports
  636. firewall_menu
  637. ;;
  638. 5)
  639. ufw disable
  640. firewall_menu
  641. ;;
  642. 6)
  643. ufw status verbose
  644. firewall_menu
  645. ;;
  646. *)
  647. echo -e "${red}Invalid option. Please select a valid number.${plain}\n"
  648. firewall_menu
  649. ;;
  650. esac
  651. }
  652. install_firewall() {
  653. if ! command -v ufw &>/dev/null; then
  654. echo "ufw firewall is not installed. Installing now..."
  655. apt-get update
  656. apt-get install -y ufw
  657. else
  658. echo "ufw firewall is already installed"
  659. fi
  660. # Check if the firewall is inactive
  661. if ufw status | grep -q "Status: active"; then
  662. echo "Firewall is already active"
  663. else
  664. echo "Activating firewall..."
  665. # Open the necessary ports
  666. ufw allow ssh
  667. ufw allow http
  668. ufw allow https
  669. ufw allow 2053/tcp #webPort
  670. ufw allow 2096/tcp #subport
  671. # Enable the firewall
  672. ufw --force enable
  673. fi
  674. }
  675. open_ports() {
  676. # Prompt the user to enter the ports they want to open
  677. read -p "Enter the ports you want to open (e.g. 80,443,2053 or range 400-500): " ports
  678. # Check if the input is valid
  679. if ! [[ $ports =~ ^([0-9]+|[0-9]+-[0-9]+)(,([0-9]+|[0-9]+-[0-9]+))*$ ]]; then
  680. 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
  681. exit 1
  682. fi
  683. # Open the specified ports using ufw
  684. IFS=',' read -ra PORT_LIST <<<"$ports"
  685. for port in "${PORT_LIST[@]}"; do
  686. if [[ $port == *-* ]]; then
  687. # Split the range into start and end ports
  688. start_port=$(echo $port | cut -d'-' -f1)
  689. end_port=$(echo $port | cut -d'-' -f2)
  690. # Open the port range
  691. ufw allow $start_port:$end_port/tcp
  692. ufw allow $start_port:$end_port/udp
  693. else
  694. # Open the single port
  695. ufw allow "$port"
  696. fi
  697. done
  698. # Confirm that the ports are opened
  699. echo "Opened the specified ports:"
  700. for port in "${PORT_LIST[@]}"; do
  701. if [[ $port == *-* ]]; then
  702. start_port=$(echo $port | cut -d'-' -f1)
  703. end_port=$(echo $port | cut -d'-' -f2)
  704. # Check if the port range has been successfully opened
  705. (ufw status | grep -q "$start_port:$end_port") && echo "$start_port-$end_port"
  706. else
  707. # Check if the individual port has been successfully opened
  708. (ufw status | grep -q "$port") && echo "$port"
  709. fi
  710. done
  711. }
  712. delete_ports() {
  713. # Prompt the user to enter the ports they want to delete
  714. read -p "Enter the ports you want to delete (e.g. 80,443,2053 or range 400-500): " ports
  715. # Check if the input is valid
  716. if ! [[ $ports =~ ^([0-9]+|[0-9]+-[0-9]+)(,([0-9]+|[0-9]+-[0-9]+))*$ ]]; then
  717. 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
  718. exit 1
  719. fi
  720. # Delete the specified ports using ufw
  721. IFS=',' read -ra PORT_LIST <<<"$ports"
  722. for port in "${PORT_LIST[@]}"; do
  723. if [[ $port == *-* ]]; then
  724. # Split the range into start and end ports
  725. start_port=$(echo $port | cut -d'-' -f1)
  726. end_port=$(echo $port | cut -d'-' -f2)
  727. # Delete the port range
  728. ufw delete allow $start_port:$end_port/tcp
  729. ufw delete allow $start_port:$end_port/udp
  730. else
  731. ufw delete allow "$port"
  732. fi
  733. done
  734. # Confirm that the ports are deleted
  735. echo "Deleted the specified ports:"
  736. for port in "${PORT_LIST[@]}"; do
  737. if [[ $port == *-* ]]; then
  738. start_port=$(echo $port | cut -d'-' -f1)
  739. end_port=$(echo $port | cut -d'-' -f2)
  740. # Check if the port range has been successfully deleted
  741. (ufw status | grep -q "$start_port:$end_port") || echo "$start_port-$end_port"
  742. else
  743. # Check if the individual port has been successfully deleted
  744. (ufw status | grep -q "$port") || echo "$port"
  745. fi
  746. done
  747. }
  748. update_geo() {
  749. echo -e "${green}\t1.${plain} Loyalsoldier (geoip.dat, geosite.dat)"
  750. echo -e "${green}\t2.${plain} chocolate4u (geoip_IR.dat, geosite_IR.dat)"
  751. echo -e "${green}\t3.${plain} vuong2023 (geoip_VN.dat, geosite_VN.dat)"
  752. echo -e "${green}\t0.${plain} Back to Main Menu"
  753. read -p "Choose an option: " choice
  754. cd /usr/local/x-ui/bin
  755. case "$choice" in
  756. 0)
  757. show_menu
  758. ;;
  759. 1)
  760. systemctl stop x-ui
  761. rm -f geoip.dat geosite.dat
  762. wget -N https://github.com/Loyalsoldier/v2ray-rules-dat/releases/latest/download/geoip.dat
  763. wget -N https://github.com/Loyalsoldier/v2ray-rules-dat/releases/latest/download/geosite.dat
  764. echo -e "${green}Loyalsoldier datasets have been updated successfully!${plain}"
  765. restart
  766. ;;
  767. 2)
  768. systemctl stop x-ui
  769. rm -f geoip_IR.dat geosite_IR.dat
  770. wget -O geoip_IR.dat -N https://github.com/chocolate4u/Iran-v2ray-rules/releases/latest/download/geoip.dat
  771. wget -O geosite_IR.dat -N https://github.com/chocolate4u/Iran-v2ray-rules/releases/latest/download/geosite.dat
  772. echo -e "${green}chocolate4u datasets have been updated successfully!${plain}"
  773. restart
  774. ;;
  775. 3)
  776. systemctl stop x-ui
  777. rm -f geoip_VN.dat geosite_VN.dat
  778. wget -O geoip_VN.dat -N https://github.com/vuong2023/vn-v2ray-rules/releases/latest/download/geoip.dat
  779. wget -O geosite_VN.dat -N https://github.com/vuong2023/vn-v2ray-rules/releases/latest/download/geosite.dat
  780. echo -e "${green}vuong2023 datasets have been updated successfully!${plain}"
  781. restart
  782. ;;
  783. *)
  784. echo -e "${red}Invalid option. Please select a valid number.${plain}\n"
  785. update_geo
  786. ;;
  787. esac
  788. before_show_menu
  789. }
  790. install_acme() {
  791. # Check if acme.sh is already installed
  792. if command -v ~/.acme.sh/acme.sh &>/dev/null; then
  793. LOGI "acme.sh is already installed."
  794. return 0
  795. fi
  796. LOGI "Installing acme.sh..."
  797. cd ~ || return 1 # Ensure you can change to the home directory
  798. curl -s https://get.acme.sh | sh
  799. if [ $? -ne 0 ]; then
  800. LOGE "Installation of acme.sh failed."
  801. return 1
  802. else
  803. LOGI "Installation of acme.sh succeeded."
  804. fi
  805. return 0
  806. }
  807. ssl_cert_issue_main() {
  808. echo -e "${green}\t1.${plain} Get SSL"
  809. echo -e "${green}\t2.${plain} Revoke"
  810. echo -e "${green}\t3.${plain} Force Renew"
  811. echo -e "${green}\t4.${plain} Show Existing Domains"
  812. echo -e "${green}\t5.${plain} Set Cert paths for the panel"
  813. echo -e "${green}\t0.${plain} Back to Main Menu"
  814. read -p "Choose an option: " choice
  815. case "$choice" in
  816. 0)
  817. show_menu
  818. ;;
  819. 1)
  820. ssl_cert_issue
  821. ssl_cert_issue_main
  822. ;;
  823. 2)
  824. local domains=$(find /root/cert/ -mindepth 1 -maxdepth 1 -type d -exec basename {} \;)
  825. if [ -z "$domains" ]; then
  826. echo "No certificates found to revoke."
  827. else
  828. echo "Existing domains:"
  829. echo "$domains"
  830. read -p "Please enter a domain from the list to revoke the certificate: " domain
  831. if echo "$domains" | grep -qw "$domain"; then
  832. ~/.acme.sh/acme.sh --revoke -d ${domain}
  833. LOGI "Certificate revoked for domain: $domain"
  834. else
  835. echo "Invalid domain entered."
  836. fi
  837. fi
  838. ssl_cert_issue_main
  839. ;;
  840. 3)
  841. local domains=$(find /root/cert/ -mindepth 1 -maxdepth 1 -type d -exec basename {} \;)
  842. if [ -z "$domains" ]; then
  843. echo "No certificates found to renew."
  844. else
  845. echo "Existing domains:"
  846. echo "$domains"
  847. read -p "Please enter a domain from the list to renew the SSL certificate: " domain
  848. if echo "$domains" | grep -qw "$domain"; then
  849. ~/.acme.sh/acme.sh --renew -d ${domain} --force
  850. LOGI "Certificate forcefully renewed for domain: $domain"
  851. else
  852. echo "Invalid domain entered."
  853. fi
  854. fi
  855. ssl_cert_issue_main
  856. ;;
  857. 4)
  858. local domains=$(find /root/cert/ -mindepth 1 -maxdepth 1 -type d -exec basename {} \;)
  859. if [ -z "$domains" ]; then
  860. echo "No certificates found."
  861. else
  862. echo "Existing domains and their paths:"
  863. for domain in $domains; do
  864. local cert_path="/root/cert/${domain}/fullchain.pem"
  865. local key_path="/root/cert/${domain}/privkey.pem"
  866. if [[ -f "${cert_path}" && -f "${key_path}" ]]; then
  867. echo -e "Domain: ${domain}"
  868. echo -e "\tCertificate Path: ${cert_path}"
  869. echo -e "\tPrivate Key Path: ${key_path}"
  870. else
  871. echo -e "Domain: ${domain} - Certificate or Key missing."
  872. fi
  873. done
  874. fi
  875. ssl_cert_issue_main
  876. ;;
  877. 5)
  878. local domains=$(find /root/cert/ -mindepth 1 -maxdepth 1 -type d -exec basename {} \;)
  879. if [ -z "$domains" ]; then
  880. echo "No certificates found."
  881. else
  882. echo "Available domains:"
  883. echo "$domains"
  884. read -p "Please choose a domain to set the panel paths: " domain
  885. if echo "$domains" | grep -qw "$domain"; then
  886. local webCertFile="/root/cert/${domain}/fullchain.pem"
  887. local webKeyFile="/root/cert/${domain}/privkey.pem"
  888. if [[ -f "${webCertFile}" && -f "${webKeyFile}" ]]; then
  889. /usr/local/x-ui/x-ui cert -webCert "$webCertFile" -webCertKey "$webKeyFile"
  890. echo "Panel paths set for domain: $domain"
  891. echo " - Certificate File: $webCertFile"
  892. echo " - Private Key File: $webKeyFile"
  893. restart
  894. else
  895. echo "Certificate or private key not found for domain: $domain."
  896. fi
  897. else
  898. echo "Invalid domain entered."
  899. fi
  900. fi
  901. ssl_cert_issue_main
  902. ;;
  903. *)
  904. echo -e "${red}Invalid option. Please select a valid number.${plain}\n"
  905. ssl_cert_issue_main
  906. ;;
  907. esac
  908. }
  909. ssl_cert_issue() {
  910. local existing_webBasePath=$(/usr/local/x-ui/x-ui setting -show true | grep -Eo 'webBasePath: .+' | awk '{print $2}')
  911. local existing_port=$(/usr/local/x-ui/x-ui setting -show true | grep -Eo 'port: .+' | awk '{print $2}')
  912. # check for acme.sh first
  913. if ! command -v ~/.acme.sh/acme.sh &>/dev/null; then
  914. echo "acme.sh could not be found. we will install it"
  915. install_acme
  916. if [ $? -ne 0 ]; then
  917. LOGE "install acme failed, please check logs"
  918. exit 1
  919. fi
  920. fi
  921. # install socat second
  922. case "${release}" in
  923. ubuntu | debian | armbian)
  924. apt update && apt install socat -y
  925. ;;
  926. centos | almalinux | rocky | ol)
  927. yum -y update && yum -y install socat
  928. ;;
  929. fedora | amzn)
  930. dnf -y update && dnf -y install socat
  931. ;;
  932. arch | manjaro | parch)
  933. pacman -Sy --noconfirm socat
  934. ;;
  935. *)
  936. echo -e "${red}Unsupported operating system. Please check the script and install the necessary packages manually.${plain}\n"
  937. exit 1
  938. ;;
  939. esac
  940. if [ $? -ne 0 ]; then
  941. LOGE "install socat failed, please check logs"
  942. exit 1
  943. else
  944. LOGI "install socat succeed..."
  945. fi
  946. # get the domain here, and we need to verify it
  947. local domain=""
  948. read -p "Please enter your domain name: " domain
  949. LOGD "Your domain is: ${domain}, checking it..."
  950. # check if there already exists a certificate
  951. local currentCert=$(~/.acme.sh/acme.sh --list | tail -1 | awk '{print $1}')
  952. if [ "${currentCert}" == "${domain}" ]; then
  953. local certInfo=$(~/.acme.sh/acme.sh --list)
  954. LOGE "System already has certificates for this domain. Cannot issue again. Current certificate details:"
  955. LOGI "$certInfo"
  956. exit 1
  957. else
  958. LOGI "Your domain is ready for issuing certificates now..."
  959. fi
  960. # create a directory for the certificate
  961. certPath="/root/cert/${domain}"
  962. if [ ! -d "$certPath" ]; then
  963. mkdir -p "$certPath"
  964. else
  965. rm -rf "$certPath"
  966. mkdir -p "$certPath"
  967. fi
  968. # get the port number for the standalone server
  969. local WebPort=80
  970. read -p "Please choose which port to use (default is 80): " WebPort
  971. if [[ ${WebPort} -gt 65535 || ${WebPort} -lt 1 ]]; then
  972. LOGE "Your input ${WebPort} is invalid, will use default port 80."
  973. WebPort=80
  974. fi
  975. LOGI "Will use port: ${WebPort} to issue certificates. Please make sure this port is open."
  976. # issue the certificate
  977. ~/.acme.sh/acme.sh --set-default-ca --server letsencrypt
  978. ~/.acme.sh/acme.sh --issue -d ${domain} --listen-v6 --standalone --httpport ${WebPort}
  979. if [ $? -ne 0 ]; then
  980. LOGE "Issuing certificate failed, please check logs."
  981. rm -rf ~/.acme.sh/${domain}
  982. exit 1
  983. else
  984. LOGE "Issuing certificate succeeded, installing certificates..."
  985. fi
  986. # install the certificate
  987. ~/.acme.sh/acme.sh --installcert -d ${domain} \
  988. --key-file /root/cert/${domain}/privkey.pem \
  989. --fullchain-file /root/cert/${domain}/fullchain.pem
  990. if [ $? -ne 0 ]; then
  991. LOGE "Installing certificate failed, exiting."
  992. rm -rf ~/.acme.sh/${domain}
  993. exit 1
  994. else
  995. LOGI "Installing certificate succeeded, enabling auto renew..."
  996. fi
  997. # enable auto-renew
  998. ~/.acme.sh/acme.sh --upgrade --auto-upgrade
  999. if [ $? -ne 0 ]; then
  1000. LOGE "Auto renew failed, certificate details:"
  1001. ls -lah cert/*
  1002. chmod 755 $certPath/*
  1003. exit 1
  1004. else
  1005. LOGI "Auto renew succeeded, certificate details:"
  1006. ls -lah cert/*
  1007. chmod 755 $certPath/*
  1008. fi
  1009. # Prompt user to set panel paths after successful certificate installation
  1010. read -p "Would you like to set this certificate for the panel? (y/n): " setPanel
  1011. if [[ "$setPanel" == "y" || "$setPanel" == "Y" ]]; then
  1012. local webCertFile="/root/cert/${domain}/fullchain.pem"
  1013. local webKeyFile="/root/cert/${domain}/privkey.pem"
  1014. if [[ -f "$webCertFile" && -f "$webKeyFile" ]]; then
  1015. /usr/local/x-ui/x-ui cert -webCert "$webCertFile" -webCertKey "$webKeyFile"
  1016. LOGI "Panel paths set for domain: $domain"
  1017. LOGI " - Certificate File: $webCertFile"
  1018. LOGI " - Private Key File: $webKeyFile"
  1019. echo -e "${green}Access URL: https://${domain}:${existing_port}${existing_webBasePath}${plain}"
  1020. restart
  1021. else
  1022. LOGE "Error: Certificate or private key file not found for domain: $domain."
  1023. fi
  1024. else
  1025. LOGI "Skipping panel path setting."
  1026. fi
  1027. }
  1028. ssl_cert_issue_CF() {
  1029. local existing_webBasePath=$(/usr/local/x-ui/x-ui setting -show true | grep -Eo 'webBasePath: .+' | awk '{print $2}')
  1030. local existing_port=$(/usr/local/x-ui/x-ui setting -show true | grep -Eo 'port: .+' | awk '{print $2}')
  1031. LOGI "****** Instructions for Use ******"
  1032. LOGI "Follow the steps below to complete the process:"
  1033. LOGI "1. Cloudflare Registered E-mail."
  1034. LOGI "2. Cloudflare Global API Key."
  1035. LOGI "3. The Domain Name."
  1036. LOGI "4. Once the certificate is issued, you will be prompted to set the certificate for the panel (optional)."
  1037. LOGI "5. The script also supports automatic renewal of the SSL certificate after installation."
  1038. confirm "Do you confirm the information and wish to proceed? [y/n]" "y"
  1039. if [ $? -eq 0 ]; then
  1040. # Check for acme.sh first
  1041. if ! command -v ~/.acme.sh/acme.sh &>/dev/null; then
  1042. echo "acme.sh could not be found. We will install it."
  1043. install_acme
  1044. if [ $? -ne 0 ]; then
  1045. LOGE "Install acme failed, please check logs."
  1046. exit 1
  1047. fi
  1048. fi
  1049. CF_Domain=""
  1050. certPath="/root/cert-CF"
  1051. if [ ! -d "$certPath" ]; then
  1052. mkdir -p $certPath
  1053. else
  1054. rm -rf $certPath
  1055. mkdir -p $certPath
  1056. fi
  1057. LOGD "Please set a domain name:"
  1058. read -p "Input your domain here: " CF_Domain
  1059. LOGD "Your domain name is set to: ${CF_Domain}"
  1060. # Set up Cloudflare API details
  1061. CF_GlobalKey=""
  1062. CF_AccountEmail=""
  1063. LOGD "Please set the API key:"
  1064. read -p "Input your key here: " CF_GlobalKey
  1065. LOGD "Your API key is: ${CF_GlobalKey}"
  1066. LOGD "Please set up registered email:"
  1067. read -p "Input your email here: " CF_AccountEmail
  1068. LOGD "Your registered email address is: ${CF_AccountEmail}"
  1069. # Set the default CA to Let's Encrypt
  1070. ~/.acme.sh/acme.sh --set-default-ca --server letsencrypt
  1071. if [ $? -ne 0 ]; then
  1072. LOGE "Default CA, Let'sEncrypt fail, script exiting..."
  1073. exit 1
  1074. fi
  1075. export CF_Key="${CF_GlobalKey}"
  1076. export CF_Email="${CF_AccountEmail}"
  1077. # Issue the certificate using Cloudflare DNS
  1078. ~/.acme.sh/acme.sh --issue --dns dns_cf -d ${CF_Domain} -d *.${CF_Domain} --log
  1079. if [ $? -ne 0 ]; then
  1080. LOGE "Certificate issuance failed, script exiting..."
  1081. exit 1
  1082. else
  1083. LOGI "Certificate issued successfully, Installing..."
  1084. fi
  1085. # Install the certificate
  1086. mkdir -p ${certPath}/${CF_Domain}
  1087. if [ $? -ne 0 ]; then
  1088. LOGE "Failed to create directory: ${certPath}/${CF_Domain}"
  1089. exit 1
  1090. fi
  1091. ~/.acme.sh/acme.sh --installcert -d ${CF_Domain} -d *.${CF_Domain} \
  1092. --fullchain-file ${certPath}/${CF_Domain}/fullchain.pem \
  1093. --key-file ${certPath}/${CF_Domain}/privkey.pem
  1094. if [ $? -ne 0 ]; then
  1095. LOGE "Certificate installation failed, script exiting..."
  1096. exit 1
  1097. else
  1098. LOGI "Certificate installed successfully, Turning on automatic updates..."
  1099. fi
  1100. # Enable auto-update
  1101. ~/.acme.sh/acme.sh --upgrade --auto-upgrade
  1102. if [ $? -ne 0 ]; then
  1103. LOGE "Auto update setup failed, script exiting..."
  1104. exit 1
  1105. else
  1106. LOGI "The certificate is installed and auto-renewal is turned on. Specific information is as follows:"
  1107. ls -lah ${certPath}/${CF_Domain}
  1108. chmod 755 ${certPath}/${CF_Domain}
  1109. fi
  1110. # Prompt user to set panel paths after successful certificate installation
  1111. read -p "Would you like to set this certificate for the panel? (y/n): " setPanel
  1112. if [[ "$setPanel" == "y" || "$setPanel" == "Y" ]]; then
  1113. local webCertFile="${certPath}/${CF_Domain}/fullchain.pem"
  1114. local webKeyFile="${certPath}/${CF_Domain}/privkey.pem"
  1115. if [[ -f "$webCertFile" && -f "$webKeyFile" ]]; then
  1116. /usr/local/x-ui/x-ui cert -webCert "$webCertFile" -webCertKey "$webKeyFile"
  1117. LOGI "Panel paths set for domain: $CF_Domain"
  1118. LOGI " - Certificate File: $webCertFile"
  1119. LOGI " - Private Key File: $webKeyFile"
  1120. echo -e "${green}Access URL: https://${CF_Domain}:${existing_port}${existing_webBasePath}${plain}"
  1121. restart
  1122. else
  1123. LOGE "Error: Certificate or private key file not found for domain: $CF_Domain."
  1124. fi
  1125. else
  1126. LOGI "Skipping panel path setting."
  1127. fi
  1128. else
  1129. show_menu
  1130. fi
  1131. }
  1132. run_speedtest() {
  1133. # Check if Speedtest is already installed
  1134. if ! command -v speedtest &>/dev/null; then
  1135. # If not installed, determine installation method
  1136. if command -v snap &>/dev/null; then
  1137. # Use snap to install Speedtest
  1138. echo "Installing Speedtest using snap..."
  1139. snap install speedtest
  1140. else
  1141. # Fallback to using package managers
  1142. local pkg_manager=""
  1143. local speedtest_install_script=""
  1144. if command -v dnf &>/dev/null; then
  1145. pkg_manager="dnf"
  1146. speedtest_install_script="https://packagecloud.io/install/repositories/ookla/speedtest-cli/script.rpm.sh"
  1147. elif command -v yum &>/dev/null; then
  1148. pkg_manager="yum"
  1149. speedtest_install_script="https://packagecloud.io/install/repositories/ookla/speedtest-cli/script.rpm.sh"
  1150. elif command -v apt-get &>/dev/null; then
  1151. pkg_manager="apt-get"
  1152. speedtest_install_script="https://packagecloud.io/install/repositories/ookla/speedtest-cli/script.deb.sh"
  1153. elif command -v apt &>/dev/null; then
  1154. pkg_manager="apt"
  1155. speedtest_install_script="https://packagecloud.io/install/repositories/ookla/speedtest-cli/script.deb.sh"
  1156. fi
  1157. if [[ -z $pkg_manager ]]; then
  1158. echo "Error: Package manager not found. You may need to install Speedtest manually."
  1159. return 1
  1160. else
  1161. echo "Installing Speedtest using $pkg_manager..."
  1162. curl -s $speedtest_install_script | bash
  1163. $pkg_manager install -y speedtest
  1164. fi
  1165. fi
  1166. fi
  1167. speedtest
  1168. }
  1169. create_iplimit_jails() {
  1170. # Use default bantime if not passed => 30 minutes
  1171. local bantime="${1:-30}"
  1172. # Uncomment 'allowipv6 = auto' in fail2ban.conf
  1173. sed -i 's/#allowipv6 = auto/allowipv6 = auto/g' /etc/fail2ban/fail2ban.conf
  1174. # On Debian 12+ fail2ban's default backend should be changed to systemd
  1175. if [[ "${release}" == "debian" && ${os_version} -ge 12 ]]; then
  1176. sed -i '0,/action =/s/backend = auto/backend = systemd/' /etc/fail2ban/jail.conf
  1177. fi
  1178. cat << EOF > /etc/fail2ban/jail.d/3x-ipl.conf
  1179. [3x-ipl]
  1180. enabled=true
  1181. backend=auto
  1182. filter=3x-ipl
  1183. action=3x-ipl
  1184. logpath=${iplimit_log_path}
  1185. maxretry=2
  1186. findtime=32
  1187. bantime=${bantime}m
  1188. EOF
  1189. cat << EOF > /etc/fail2ban/filter.d/3x-ipl.conf
  1190. [Definition]
  1191. datepattern = ^%%Y/%%m/%%d %%H:%%M:%%S
  1192. failregex = \[LIMIT_IP\]\s*Email\s*=\s*<F-USER>.+</F-USER>\s*\|\|\s*SRC\s*=\s*<ADDR>
  1193. ignoreregex =
  1194. EOF
  1195. cat << EOF > /etc/fail2ban/action.d/3x-ipl.conf
  1196. [INCLUDES]
  1197. before = iptables-allports.conf
  1198. [Definition]
  1199. actionstart = <iptables> -N f2b-<name>
  1200. <iptables> -A f2b-<name> -j <returntype>
  1201. <iptables> -I <chain> -p <protocol> -j f2b-<name>
  1202. actionstop = <iptables> -D <chain> -p <protocol> -j f2b-<name>
  1203. <actionflush>
  1204. <iptables> -X f2b-<name>
  1205. actioncheck = <iptables> -n -L <chain> | grep -q 'f2b-<name>[ \t]'
  1206. actionban = <iptables> -I f2b-<name> 1 -s <ip> -j <blocktype>
  1207. echo "\$(date +"%%Y/%%m/%%d %%H:%%M:%%S") BAN [Email] = <F-USER> [IP] = <ip> banned for <bantime> seconds." >> ${iplimit_banned_log_path}
  1208. actionunban = <iptables> -D f2b-<name> -s <ip> -j <blocktype>
  1209. echo "\$(date +"%%Y/%%m/%%d %%H:%%M:%%S") UNBAN [Email] = <F-USER> [IP] = <ip> unbanned." >> ${iplimit_banned_log_path}
  1210. [Init]
  1211. name = default
  1212. protocol = tcp
  1213. chain = INPUT
  1214. EOF
  1215. echo -e "${green}Ip Limit jail files created with a bantime of ${bantime} minutes.${plain}"
  1216. }
  1217. iplimit_remove_conflicts() {
  1218. local jail_files=(
  1219. /etc/fail2ban/jail.conf
  1220. /etc/fail2ban/jail.local
  1221. )
  1222. for file in "${jail_files[@]}"; do
  1223. # Check for [3x-ipl] config in jail file then remove it
  1224. if test -f "${file}" && grep -qw '3x-ipl' ${file}; then
  1225. sed -i "/\[3x-ipl\]/,/^$/d" ${file}
  1226. echo -e "${yellow}Removing conflicts of [3x-ipl] in jail (${file})!${plain}\n"
  1227. fi
  1228. done
  1229. }
  1230. ip_validation() {
  1231. 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]))$"
  1232. 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)$"
  1233. }
  1234. iplimit_main() {
  1235. echo -e "\n${green}\t1.${plain} Install Fail2ban and configure IP Limit"
  1236. echo -e "${green}\t2.${plain} Change Ban Duration"
  1237. echo -e "${green}\t3.${plain} Unban Everyone"
  1238. echo -e "${green}\t4.${plain} Ban Logs"
  1239. echo -e "${green}\t5.${plain} Ban an IP Address"
  1240. echo -e "${green}\t6.${plain} Unban an IP Address"
  1241. echo -e "${green}\t7.${plain} Real-Time Logs"
  1242. echo -e "${green}\t8.${plain} Service Status"
  1243. echo -e "${green}\t9.${plain} Service Restart"
  1244. echo -e "${green}\t10.${plain} Uninstall Fail2ban and IP Limit"
  1245. echo -e "${green}\t0.${plain} Back to Main Menu"
  1246. read -p "Choose an option: " choice
  1247. case "$choice" in
  1248. 0)
  1249. show_menu
  1250. ;;
  1251. 1)
  1252. confirm "Proceed with installation of Fail2ban & IP Limit?" "y"
  1253. if [[ $? == 0 ]]; then
  1254. install_iplimit
  1255. else
  1256. iplimit_main
  1257. fi
  1258. ;;
  1259. 2)
  1260. read -rp "Please enter new Ban Duration in Minutes [default 30]: " NUM
  1261. if [[ $NUM =~ ^[0-9]+$ ]]; then
  1262. create_iplimit_jails ${NUM}
  1263. systemctl restart fail2ban
  1264. else
  1265. echo -e "${red}${NUM} is not a number! Please, try again.${plain}"
  1266. fi
  1267. iplimit_main
  1268. ;;
  1269. 3)
  1270. confirm "Proceed with Unbanning everyone from IP Limit jail?" "y"
  1271. if [[ $? == 0 ]]; then
  1272. fail2ban-client reload --restart --unban 3x-ipl
  1273. truncate -s 0 "${iplimit_banned_log_path}"
  1274. echo -e "${green}All users Unbanned successfully.${plain}"
  1275. iplimit_main
  1276. else
  1277. echo -e "${yellow}Cancelled.${plain}"
  1278. fi
  1279. iplimit_main
  1280. ;;
  1281. 4)
  1282. show_banlog
  1283. iplimit_main
  1284. ;;
  1285. 5)
  1286. read -rp "Enter the IP address you want to ban: " ban_ip
  1287. ip_validation
  1288. if [[ $ban_ip =~ $ipv4_regex || $ban_ip =~ $ipv6_regex ]]; then
  1289. fail2ban-client set 3x-ipl banip "$ban_ip"
  1290. echo -e "${green}IP Address ${ban_ip} has been banned successfully.${plain}"
  1291. else
  1292. echo -e "${red}Invalid IP address format! Please try again.${plain}"
  1293. fi
  1294. iplimit_main
  1295. ;;
  1296. 6)
  1297. read -rp "Enter the IP address you want to unban: " unban_ip
  1298. ip_validation
  1299. if [[ $unban_ip =~ $ipv4_regex || $unban_ip =~ $ipv6_regex ]]; then
  1300. fail2ban-client set 3x-ipl unbanip "$unban_ip"
  1301. echo -e "${green}IP Address ${unban_ip} has been unbanned successfully.${plain}"
  1302. else
  1303. echo -e "${red}Invalid IP address format! Please try again.${plain}"
  1304. fi
  1305. iplimit_main
  1306. ;;
  1307. 7)
  1308. tail -f /var/log/fail2ban.log
  1309. iplimit_main
  1310. ;;
  1311. 8)
  1312. service fail2ban status
  1313. iplimit_main
  1314. ;;
  1315. 9)
  1316. systemctl restart fail2ban
  1317. iplimit_main
  1318. ;;
  1319. 10)
  1320. remove_iplimit
  1321. iplimit_main
  1322. ;;
  1323. *)
  1324. echo -e "${red}Invalid option. Please select a valid number.${plain}\n"
  1325. iplimit_main
  1326. ;;
  1327. esac
  1328. }
  1329. install_iplimit() {
  1330. if ! command -v fail2ban-client &>/dev/null; then
  1331. echo -e "${green}Fail2ban is not installed. Installing now...!${plain}\n"
  1332. # Check the OS and install necessary packages
  1333. case "${release}" in
  1334. ubuntu)
  1335. if [[ "${os_version}" -ge 24 ]]; then
  1336. apt update && apt install python3-pip -y
  1337. python3 -m pip install pyasynchat --break-system-packages
  1338. fi
  1339. apt update && apt install fail2ban -y
  1340. ;;
  1341. debian | armbian)
  1342. apt update && apt install fail2ban -y
  1343. ;;
  1344. centos | almalinux | rocky | ol)
  1345. yum update -y && yum install epel-release -y
  1346. yum -y install fail2ban
  1347. ;;
  1348. fedora | amzn)
  1349. dnf -y update && dnf -y install fail2ban
  1350. ;;
  1351. arch | manjaro | parch)
  1352. pacman -Syu --noconfirm fail2ban
  1353. ;;
  1354. *)
  1355. echo -e "${red}Unsupported operating system. Please check the script and install the necessary packages manually.${plain}\n"
  1356. exit 1
  1357. ;;
  1358. esac
  1359. if ! command -v fail2ban-client &>/dev/null; then
  1360. echo -e "${red}Fail2ban installation failed.${plain}\n"
  1361. exit 1
  1362. fi
  1363. echo -e "${green}Fail2ban installed successfully!${plain}\n"
  1364. else
  1365. echo -e "${yellow}Fail2ban is already installed.${plain}\n"
  1366. fi
  1367. echo -e "${green}Configuring IP Limit...${plain}\n"
  1368. # make sure there's no conflict for jail files
  1369. iplimit_remove_conflicts
  1370. # Check if log file exists
  1371. if ! test -f "${iplimit_banned_log_path}"; then
  1372. touch ${iplimit_banned_log_path}
  1373. fi
  1374. # Check if service log file exists so fail2ban won't return error
  1375. if ! test -f "${iplimit_log_path}"; then
  1376. touch ${iplimit_log_path}
  1377. fi
  1378. # Create the iplimit jail files
  1379. # we didn't pass the bantime here to use the default value
  1380. create_iplimit_jails
  1381. # Launching fail2ban
  1382. if ! systemctl is-active --quiet fail2ban; then
  1383. systemctl start fail2ban
  1384. systemctl enable fail2ban
  1385. else
  1386. systemctl restart fail2ban
  1387. fi
  1388. systemctl enable fail2ban
  1389. echo -e "${green}IP Limit installed and configured successfully!${plain}\n"
  1390. before_show_menu
  1391. }
  1392. remove_iplimit() {
  1393. echo -e "${green}\t1.${plain} Only remove IP Limit configurations"
  1394. echo -e "${green}\t2.${plain} Uninstall Fail2ban and IP Limit"
  1395. echo -e "${green}\t0.${plain} Back to Main Menu"
  1396. read -p "Choose an option: " num
  1397. case "$num" in
  1398. 1)
  1399. rm -f /etc/fail2ban/filter.d/3x-ipl.conf
  1400. rm -f /etc/fail2ban/action.d/3x-ipl.conf
  1401. rm -f /etc/fail2ban/jail.d/3x-ipl.conf
  1402. systemctl restart fail2ban
  1403. echo -e "${green}IP Limit removed successfully!${plain}\n"
  1404. before_show_menu
  1405. ;;
  1406. 2)
  1407. rm -rf /etc/fail2ban
  1408. systemctl stop fail2ban
  1409. case "${release}" in
  1410. ubuntu | debian | armbian)
  1411. apt-get remove -y fail2ban
  1412. apt-get purge -y fail2ban -y
  1413. apt-get autoremove -y
  1414. ;;
  1415. centos | almalinux | rocky | ol)
  1416. yum remove fail2ban -y
  1417. yum autoremove -y
  1418. ;;
  1419. fedora | amzn)
  1420. dnf remove fail2ban -y
  1421. dnf autoremove -y
  1422. ;;
  1423. arch | manjaro | parch)
  1424. pacman -Rns --noconfirm fail2ban
  1425. ;;
  1426. *)
  1427. echo -e "${red}Unsupported operating system. Please uninstall Fail2ban manually.${plain}\n"
  1428. exit 1
  1429. ;;
  1430. esac
  1431. echo -e "${green}Fail2ban and IP Limit removed successfully!${plain}\n"
  1432. before_show_menu
  1433. ;;
  1434. 0)
  1435. show_menu
  1436. ;;
  1437. *)
  1438. echo -e "${red}Invalid option. Please select a valid number.${plain}\n"
  1439. remove_iplimit
  1440. ;;
  1441. esac
  1442. }
  1443. SSH_port_forwarding() {
  1444. local server_ip=$(curl -s https://api.ipify.org)
  1445. local existing_webBasePath=$(/usr/local/x-ui/x-ui setting -show true | grep -Eo 'webBasePath: .+' | awk '{print $2}')
  1446. local existing_port=$(/usr/local/x-ui/x-ui setting -show true | grep -Eo 'port: .+' | awk '{print $2}')
  1447. local existing_listenIP=$(/usr/local/x-ui/x-ui setting -getListen true | grep -Eo 'listenIP: .+' | awk '{print $2}')
  1448. local existing_cert=$(/usr/local/x-ui/x-ui setting -getCert true | grep -Eo 'cert: .+' | awk '{print $2}')
  1449. local existing_key=$(/usr/local/x-ui/x-ui setting -getCert true | grep -Eo 'key: .+' | awk '{print $2}')
  1450. local config_listenIP=""
  1451. local listen_choice=""
  1452. if [[ -n "$existing_cert" && -n "$existing_key" ]]; then
  1453. echo -e "${green}Panel is secure with SSL.${plain}"
  1454. before_show_menu
  1455. fi
  1456. if [[ -z "$existing_cert" && -z "$existing_key" && (-z "$existing_listenIP" || "$existing_listenIP" == "0.0.0.0") ]]; then
  1457. echo -e "\n${red}Warning: No Cert and Key found! The panel is not secure.${plain}"
  1458. echo "Please obtain a certificate or set up SSH port forwarding."
  1459. fi
  1460. if [[ -n "$existing_listenIP" && "$existing_listenIP" != "0.0.0.0" && (-z "$existing_cert" && -z "$existing_key") ]]; then
  1461. echo -e "\n${green}Current SSH Port Forwarding Configuration:${plain}"
  1462. echo -e "Standard SSH command:"
  1463. echo -e "${yellow}ssh -L 2222:${existing_listenIP}:${existing_port} root@${server_ip}${plain}"
  1464. echo -e "\nIf using SSH key:"
  1465. echo -e "${yellow}ssh -i <sshkeypath> -L 2222:${existing_listenIP}:${existing_port} root@${server_ip}${plain}"
  1466. echo -e "\nAfter connecting, access the panel at:"
  1467. echo -e "${yellow}http://localhost:2222${existing_webBasePath}${plain}"
  1468. fi
  1469. echo -e "\nChoose an option:"
  1470. echo -e "${green}1.${plain} Set listen IP"
  1471. echo -e "${green}2.${plain} Clear listen IP"
  1472. echo -e "${green}0.${plain} Back to Main Menu"
  1473. read -p "Choose an option: " num
  1474. case "$num" in
  1475. 1)
  1476. if [[ -z "$existing_listenIP" || "$existing_listenIP" == "0.0.0.0" ]]; then
  1477. echo -e "\nNo listenIP configured. Choose an option:"
  1478. echo -e "1. Use default IP (127.0.0.1)"
  1479. echo -e "2. Set a custom IP"
  1480. read -p "Select an option (1 or 2): " listen_choice
  1481. config_listenIP="127.0.0.1"
  1482. [[ "$listen_choice" == "2" ]] && read -p "Enter custom IP to listen on: " config_listenIP
  1483. /usr/local/x-ui/x-ui setting -listenIP "${config_listenIP}" >/dev/null 2>&1
  1484. echo -e "${green}listen IP has been set to ${config_listenIP}.${plain}"
  1485. echo -e "\n${green}SSH Port Forwarding Configuration:${plain}"
  1486. echo -e "Standard SSH command:"
  1487. echo -e "${yellow}ssh -L 2222:${config_listenIP}:${existing_port} root@${server_ip}${plain}"
  1488. echo -e "\nIf using SSH key:"
  1489. echo -e "${yellow}ssh -i <sshkeypath> -L 2222:${config_listenIP}:${existing_port} root@${server_ip}${plain}"
  1490. echo -e "\nAfter connecting, access the panel at:"
  1491. echo -e "${yellow}http://localhost:2222${existing_webBasePath}${plain}"
  1492. restart
  1493. else
  1494. config_listenIP="${existing_listenIP}"
  1495. echo -e "${green}Current listen IP is already set to ${config_listenIP}.${plain}"
  1496. fi
  1497. ;;
  1498. 2)
  1499. /usr/local/x-ui/x-ui setting -listenIP 0.0.0.0 >/dev/null 2>&1
  1500. echo -e "${green}Listen IP has been cleared.${plain}"
  1501. restart
  1502. ;;
  1503. 0)
  1504. show_menu
  1505. ;;
  1506. *)
  1507. echo -e "${red}Invalid option. Please select a valid number.${plain}\n"
  1508. SSH_port_forwarding
  1509. ;;
  1510. esac
  1511. }
  1512. show_usage() {
  1513. echo -e "┌───────────────────────────────────────────────────────┐
  1514. │ ${blue}x-ui control menu usages (subcommands):${plain} │
  1515. │ │
  1516. │ ${blue}x-ui${plain} - Admin Management Script │
  1517. │ ${blue}x-ui start${plain} - Start │
  1518. │ ${blue}x-ui stop${plain} - Stop │
  1519. │ ${blue}x-ui restart${plain} - Restart │
  1520. │ ${blue}x-ui status${plain} - Current Status │
  1521. │ ${blue}x-ui settings${plain} - Current Settings │
  1522. │ ${blue}x-ui enable${plain} - Enable Autostart on OS Startup │
  1523. │ ${blue}x-ui disable${plain} - Disable Autostart on OS Startup │
  1524. │ ${blue}x-ui log${plain} - Check logs │
  1525. │ ${blue}x-ui banlog${plain} - Check Fail2ban ban logs │
  1526. │ ${blue}x-ui update${plain} - Update │
  1527. │ ${blue}x-ui legacy${plain} - legacy version │
  1528. │ ${blue}x-ui install${plain} - Install │
  1529. │ ${blue}x-ui uninstall${plain} - Uninstall │
  1530. └───────────────────────────────────────────────────────┘"
  1531. }
  1532. show_menu() {
  1533. echo -e "
  1534. ╔────────────────────────────────────────────────╗
  1535. │ ${green}3X-UI Panel Management Script${plain} │
  1536. │ ${green}0.${plain} Exit Script │
  1537. │────────────────────────────────────────────────│
  1538. │ ${green}1.${plain} Install │
  1539. │ ${green}2.${plain} Update │
  1540. │ ${green}3.${plain} Update Menu │
  1541. │ ${green}4.${plain} Legacy Version │
  1542. │ ${green}5.${plain} Uninstall │
  1543. │────────────────────────────────────────────────│
  1544. │ ${green}6.${plain} Reset Username & Password & Secret Token │
  1545. │ ${green}7.${plain} Reset Web Base Path │
  1546. │ ${green}8.${plain} Reset Settings │
  1547. │ ${green}9.${plain} Change Port │
  1548. │ ${green}10.${plain} View Current Settings │
  1549. │────────────────────────────────────────────────│
  1550. │ ${green}11.${plain} Start │
  1551. │ ${green}12.${plain} Stop │
  1552. │ ${green}13.${plain} Restart │
  1553. │ ${green}14.${plain} Check Status │
  1554. │ ${green}15.${plain} Logs Management │
  1555. │────────────────────────────────────────────────│
  1556. │ ${green}16.${plain} Enable Autostart │
  1557. │ ${green}17.${plain} Disable Autostart │
  1558. │────────────────────────────────────────────────│
  1559. │ ${green}18.${plain} SSL Certificate Management │
  1560. │ ${green}19.${plain} Cloudflare SSL Certificate │
  1561. │ ${green}20.${plain} IP Limit Management │
  1562. │ ${green}21.${plain} Firewall Management │
  1563. │ ${green}22.${plain} SSH Port Forwarding Management │
  1564. │────────────────────────────────────────────────│
  1565. │ ${green}23.${plain} Enable BBR │
  1566. │ ${green}24.${plain} Update Geo Files │
  1567. │ ${green}25.${plain} Speedtest by Ookla │
  1568. ╚────────────────────────────────────────────────╝
  1569. "
  1570. show_status
  1571. echo && read -p "Please enter your selection [0-25]: " num
  1572. case "${num}" in
  1573. 0)
  1574. exit 0
  1575. ;;
  1576. 1)
  1577. check_uninstall && install
  1578. ;;
  1579. 2)
  1580. check_install && update
  1581. ;;
  1582. 3)
  1583. check_install && update_menu
  1584. ;;
  1585. 4)
  1586. check_install && legacy_version
  1587. ;;
  1588. 5)
  1589. check_install && uninstall
  1590. ;;
  1591. 6)
  1592. check_install && reset_user
  1593. ;;
  1594. 7)
  1595. check_install && reset_webbasepath
  1596. ;;
  1597. 8)
  1598. check_install && reset_config
  1599. ;;
  1600. 9)
  1601. check_install && set_port
  1602. ;;
  1603. 10)
  1604. check_install && check_config
  1605. ;;
  1606. 11)
  1607. check_install && start
  1608. ;;
  1609. 12)
  1610. check_install && stop
  1611. ;;
  1612. 13)
  1613. check_install && restart
  1614. ;;
  1615. 14)
  1616. check_install && status
  1617. ;;
  1618. 15)
  1619. check_install && show_log
  1620. ;;
  1621. 16)
  1622. check_install && enable
  1623. ;;
  1624. 17)
  1625. check_install && disable
  1626. ;;
  1627. 18)
  1628. ssl_cert_issue_main
  1629. ;;
  1630. 19)
  1631. ssl_cert_issue_CF
  1632. ;;
  1633. 20)
  1634. iplimit_main
  1635. ;;
  1636. 21)
  1637. firewall_menu
  1638. ;;
  1639. 22)
  1640. SSH_port_forwarding
  1641. ;;
  1642. 23)
  1643. bbr_menu
  1644. ;;
  1645. 24)
  1646. update_geo
  1647. ;;
  1648. 25)
  1649. run_speedtest
  1650. ;;
  1651. *)
  1652. LOGE "Please enter the correct number [0-25]"
  1653. ;;
  1654. esac
  1655. }
  1656. if [[ $# > 0 ]]; then
  1657. case $1 in
  1658. "start")
  1659. check_install 0 && start 0
  1660. ;;
  1661. "stop")
  1662. check_install 0 && stop 0
  1663. ;;
  1664. "restart")
  1665. check_install 0 && restart 0
  1666. ;;
  1667. "status")
  1668. check_install 0 && status 0
  1669. ;;
  1670. "settings")
  1671. check_install 0 && check_config 0
  1672. ;;
  1673. "enable")
  1674. check_install 0 && enable 0
  1675. ;;
  1676. "disable")
  1677. check_install 0 && disable 0
  1678. ;;
  1679. "log")
  1680. check_install 0 && show_log 0
  1681. ;;
  1682. "banlog")
  1683. check_install 0 && show_banlog 0
  1684. ;;
  1685. "update")
  1686. check_install 0 && update 0
  1687. ;;
  1688. "legacy")
  1689. check_install 0 && legacy_version 0
  1690. ;;
  1691. "install")
  1692. check_uninstall 0 && install 0
  1693. ;;
  1694. "uninstall")
  1695. check_install 0 && uninstall 0
  1696. ;;
  1697. *) show_usage ;;
  1698. esac
  1699. else
  1700. show_menu
  1701. fi