x-ui.sh 65 KB

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