x-ui.sh 63 KB

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