x-ui.sh 67 KB

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