x-ui.sh 64 KB

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