x-ui.sh 68 KB

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