x-ui.sh 86 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452
  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. # Port helpers: detect listener and owning process (best effort)
  18. is_port_in_use() {
  19. local port="$1"
  20. if command -v ss > /dev/null 2>&1; then
  21. ss -ltn 2> /dev/null | awk -v p=":${port}$" '$4 ~ p {exit 0} END {exit 1}'
  22. return
  23. fi
  24. if command -v netstat > /dev/null 2>&1; then
  25. netstat -lnt 2> /dev/null | awk -v p=":${port} " '$4 ~ p {exit 0} END {exit 1}'
  26. return
  27. fi
  28. if command -v lsof > /dev/null 2>&1; then
  29. lsof -nP -iTCP:${port} -sTCP:LISTEN > /dev/null 2>&1 && return 0
  30. fi
  31. return 1
  32. }
  33. # Simple helpers for domain/IP validation
  34. is_ipv4() {
  35. [[ "$1" =~ ^([0-9]{1,3}\.){3}[0-9]{1,3}$ ]] && return 0 || return 1
  36. }
  37. is_ipv6() {
  38. [[ "$1" =~ : ]] && return 0 || return 1
  39. }
  40. is_ip() {
  41. is_ipv4 "$1" || is_ipv6 "$1"
  42. }
  43. is_domain() {
  44. [[ "$1" =~ ^([A-Za-z0-9](-*[A-Za-z0-9])*\.)+(xn--[a-z0-9]{2,}|[A-Za-z]{2,})$ ]] && return 0 || return 1
  45. }
  46. # check root
  47. [[ $EUID -ne 0 ]] && LOGE "ERROR: You must be root to run this script! \n" && exit 1
  48. # Check OS and set release variable
  49. if [[ -f /etc/os-release ]]; then
  50. source /etc/os-release
  51. release=$ID
  52. elif [[ -f /usr/lib/os-release ]]; then
  53. source /usr/lib/os-release
  54. release=$ID
  55. else
  56. echo "Failed to check the system OS, please contact the author!" >&2
  57. exit 1
  58. fi
  59. echo "The OS release is: $release"
  60. os_version=""
  61. os_version=$(grep "^VERSION_ID" /etc/os-release | cut -d '=' -f2 | tr -d '"' | tr -d '.')
  62. # Declare Variables
  63. xui_folder="${XUI_MAIN_FOLDER:=/usr/local/x-ui}"
  64. xui_service="${XUI_SERVICE:=/etc/systemd/system}"
  65. log_folder="${XUI_LOG_FOLDER:=/var/log/x-ui}"
  66. mkdir -p "${log_folder}"
  67. iplimit_log_path="${log_folder}/3xipl.log"
  68. iplimit_banned_log_path="${log_folder}/3xipl-banned.log"
  69. confirm() {
  70. if [[ $# > 1 ]]; then
  71. echo && read -rp "$1 [Default $2]: " temp
  72. if [[ "${temp}" == "" ]]; then
  73. temp=$2
  74. fi
  75. else
  76. read -rp "$1 [y/n]: " temp
  77. fi
  78. if [[ "${temp}" == "y" || "${temp}" == "Y" ]]; then
  79. return 0
  80. else
  81. return 1
  82. fi
  83. }
  84. confirm_restart() {
  85. confirm "Restart the panel, Attention: Restarting the panel will also restart xray" "y"
  86. if [[ $? == 0 ]]; then
  87. restart
  88. else
  89. show_menu
  90. fi
  91. }
  92. before_show_menu() {
  93. echo && echo -n -e "${yellow}Press enter to return to the main menu: ${plain}" && read -r temp
  94. show_menu
  95. }
  96. install() {
  97. bash <(curl -Ls https://raw.githubusercontent.com/MHSanaei/3x-ui/main/install.sh)
  98. if [[ $? == 0 ]]; then
  99. if [[ $# == 0 ]]; then
  100. start
  101. else
  102. start 0
  103. fi
  104. fi
  105. }
  106. update() {
  107. 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"
  108. if [[ $? != 0 ]]; then
  109. LOGE "Cancelled"
  110. if [[ $# == 0 ]]; then
  111. before_show_menu
  112. fi
  113. return 0
  114. fi
  115. bash <(curl -Ls https://raw.githubusercontent.com/MHSanaei/3x-ui/main/update.sh)
  116. if [[ $? == 0 ]]; then
  117. LOGI "Update is complete, Panel has automatically restarted "
  118. before_show_menu
  119. fi
  120. }
  121. update_menu() {
  122. echo -e "${yellow}Updating Menu${plain}"
  123. confirm "This function will update the menu to the latest changes." "y"
  124. if [[ $? != 0 ]]; then
  125. LOGE "Cancelled"
  126. if [[ $# == 0 ]]; then
  127. before_show_menu
  128. fi
  129. return 0
  130. fi
  131. curl -fLRo /usr/bin/x-ui https://raw.githubusercontent.com/MHSanaei/3x-ui/main/x-ui.sh
  132. chmod +x ${xui_folder}/x-ui.sh
  133. chmod +x /usr/bin/x-ui
  134. if [[ $? == 0 ]]; then
  135. echo -e "${green}Update successful. The panel has automatically restarted.${plain}"
  136. exit 0
  137. else
  138. echo -e "${red}Failed to update the menu.${plain}"
  139. return 1
  140. fi
  141. }
  142. legacy_version() {
  143. echo -n "Enter the panel version (like 2.4.0):"
  144. read -r tag_version
  145. if [ -z "$tag_version" ]; then
  146. echo "Panel version cannot be empty. Exiting."
  147. exit 1
  148. fi
  149. # Use the entered panel version in the download link
  150. install_command="bash <(curl -Ls "https://raw.githubusercontent.com/mhsanaei/3x-ui/v$tag_version/install.sh") v$tag_version"
  151. echo "Downloading and installing panel version $tag_version..."
  152. eval $install_command
  153. }
  154. # Function to handle the deletion of the script file
  155. delete_script() {
  156. rm "$0" # Remove the script file itself
  157. exit 1
  158. }
  159. uninstall() {
  160. confirm "Are you sure you want to uninstall the panel? xray will also uninstalled!" "n"
  161. if [[ $? != 0 ]]; then
  162. if [[ $# == 0 ]]; then
  163. show_menu
  164. fi
  165. return 0
  166. fi
  167. if [[ $release == "alpine" ]]; then
  168. rc-service x-ui stop
  169. rc-update del x-ui
  170. rm /etc/init.d/x-ui -f
  171. else
  172. systemctl stop x-ui
  173. systemctl disable x-ui
  174. rm ${xui_service}/x-ui.service -f
  175. systemctl daemon-reload
  176. systemctl reset-failed
  177. fi
  178. rm /etc/x-ui/ -rf
  179. rm ${xui_folder}/ -rf
  180. echo ""
  181. echo -e "Uninstalled Successfully.\n"
  182. echo "If you need to install this panel again, you can use below command:"
  183. echo -e "${green}bash <(curl -Ls https://raw.githubusercontent.com/mhsanaei/3x-ui/master/install.sh)${plain}"
  184. echo ""
  185. # Trap the SIGTERM signal
  186. trap delete_script SIGTERM
  187. delete_script
  188. }
  189. reset_user() {
  190. confirm "Are you sure to reset the username and password of the panel?" "n"
  191. if [[ $? != 0 ]]; then
  192. if [[ $# == 0 ]]; then
  193. show_menu
  194. fi
  195. return 0
  196. fi
  197. read -rp "Please set the login username [default is a random username]: " config_account
  198. [[ -z $config_account ]] && config_account=$(gen_random_string 10)
  199. read -rp "Please set the login password [default is a random password]: " config_password
  200. [[ -z $config_password ]] && config_password=$(gen_random_string 18)
  201. read -rp "Do you want to disable currently configured two-factor authentication? (y/n): " twoFactorConfirm
  202. if [[ $twoFactorConfirm != "y" && $twoFactorConfirm != "Y" ]]; then
  203. ${xui_folder}/x-ui setting -username "${config_account}" -password "${config_password}" -resetTwoFactor false > /dev/null 2>&1
  204. else
  205. ${xui_folder}/x-ui setting -username "${config_account}" -password "${config_password}" -resetTwoFactor true > /dev/null 2>&1
  206. echo -e "Two factor authentication has been disabled."
  207. fi
  208. echo -e "Panel login username has been reset to: ${green} ${config_account} ${plain}"
  209. echo -e "Panel login password has been reset to: ${green} ${config_password} ${plain}"
  210. echo -e "${green} Please use the new login username and password to access the X-UI panel. Also remember them! ${plain}"
  211. confirm_restart
  212. }
  213. gen_random_string() {
  214. local length="$1"
  215. openssl rand -base64 $((length * 2)) \
  216. | tr -dc 'a-zA-Z0-9' \
  217. | head -c "$length"
  218. }
  219. reset_webbasepath() {
  220. echo -e "${yellow}Resetting Web Base Path${plain}"
  221. read -rp "Are you sure you want to reset the web base path? (y/n): " confirm
  222. if [[ $confirm != "y" && $confirm != "Y" ]]; then
  223. echo -e "${yellow}Operation canceled.${plain}"
  224. return
  225. fi
  226. config_webBasePath=$(gen_random_string 18)
  227. # Apply the new web base path setting
  228. ${xui_folder}/x-ui setting -webBasePath "${config_webBasePath}" > /dev/null 2>&1
  229. echo -e "Web base path has been reset to: ${green}${config_webBasePath}${plain}"
  230. echo -e "${green}Please use the new web base path to access the panel.${plain}"
  231. restart
  232. }
  233. reset_config() {
  234. confirm "Are you sure you want to reset all panel settings, Account data will not be lost, Username and password will not change" "n"
  235. if [[ $? != 0 ]]; then
  236. if [[ $# == 0 ]]; then
  237. show_menu
  238. fi
  239. return 0
  240. fi
  241. ${xui_folder}/x-ui setting -reset
  242. echo -e "All panel settings have been reset to default."
  243. restart
  244. }
  245. check_config() {
  246. local info=$(${xui_folder}/x-ui setting -show true)
  247. if [[ $? != 0 ]]; then
  248. LOGE "get current settings error, please check logs"
  249. show_menu
  250. return
  251. fi
  252. LOGI "${info}"
  253. local existing_webBasePath=$(echo "$info" | grep -Eo 'webBasePath: .+' | awk '{print $2}')
  254. local existing_port=$(echo "$info" | grep -Eo 'port: .+' | awk '{print $2}')
  255. local existing_cert=$(${xui_folder}/x-ui setting -getCert true | grep 'cert:' | awk -F': ' '{print $2}' | tr -d '[:space:]')
  256. local URL_lists=(
  257. "https://api4.ipify.org"
  258. "https://ipv4.icanhazip.com"
  259. "https://v4.api.ipinfo.io/ip"
  260. "https://ipv4.myexternalip.com/raw"
  261. "https://4.ident.me"
  262. "https://check-host.net/ip"
  263. )
  264. local server_ip=""
  265. for ip_address in "${URL_lists[@]}"; do
  266. local response=$(curl -s -w "\n%{http_code}" --max-time 3 "${ip_address}" 2> /dev/null)
  267. local http_code=$(echo "$response" | tail -n1)
  268. local ip_result=$(echo "$response" | head -n-1 | tr -d '[:space:]"')
  269. if [[ "${http_code}" == "200" && "${ip_result}" =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
  270. server_ip="${ip_result}"
  271. break
  272. fi
  273. done
  274. if [[ -z "$server_ip" ]]; then
  275. echo -e "${yellow}Could not auto-detect server IP from any provider.${plain}"
  276. while [[ -z "$server_ip" ]]; do
  277. read -rp "Please enter your server's public IPv4 address: " server_ip
  278. server_ip="${server_ip// /}"
  279. if [[ ! "$server_ip" =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
  280. echo -e "${red}Invalid IPv4 address. Please try again.${plain}"
  281. server_ip=""
  282. fi
  283. done
  284. fi
  285. if [[ -n "$existing_cert" ]]; then
  286. local domain=$(basename "$(dirname "$existing_cert")")
  287. if [[ "$domain" =~ ^[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$ ]]; then
  288. echo -e "${green}Access URL: https://${domain}:${existing_port}${existing_webBasePath}${plain}"
  289. else
  290. echo -e "${green}Access URL: https://${server_ip}:${existing_port}${existing_webBasePath}${plain}"
  291. fi
  292. else
  293. echo -e "${red}⚠ WARNING: No SSL certificate configured!${plain}"
  294. echo -e "${yellow}You can get a Let's Encrypt certificate for your IP address (valid ~6 days, auto-renews).${plain}"
  295. read -rp "Generate SSL certificate for IP now? [y/N]: " gen_ssl
  296. if [[ "$gen_ssl" == "y" || "$gen_ssl" == "Y" ]]; then
  297. stop > /dev/null 2>&1
  298. ssl_cert_issue_for_ip
  299. if [[ $? -eq 0 ]]; then
  300. echo -e "${green}Access URL: https://${server_ip}:${existing_port}${existing_webBasePath}${plain}"
  301. # ssl_cert_issue_for_ip already restarts the panel, but ensure it's running
  302. start > /dev/null 2>&1
  303. else
  304. LOGE "IP certificate setup failed."
  305. echo -e "${yellow}You can try again via option 19 (SSL Certificate Management).${plain}"
  306. start > /dev/null 2>&1
  307. fi
  308. else
  309. echo -e "${yellow}Access URL: http://${server_ip}:${existing_port}${existing_webBasePath}${plain}"
  310. echo -e "${yellow}For security, please configure SSL certificate using option 19 (SSL Certificate Management)${plain}"
  311. fi
  312. fi
  313. }
  314. set_port() {
  315. echo -n "Enter port number[1-65535]: "
  316. read -r port
  317. if [[ -z "${port}" ]]; then
  318. LOGD "Cancelled"
  319. before_show_menu
  320. else
  321. ${xui_folder}/x-ui setting -port ${port}
  322. echo -e "The port is set, Please restart the panel now, and use the new port ${green}${port}${plain} to access web panel"
  323. confirm_restart
  324. fi
  325. }
  326. start() {
  327. check_status
  328. if [[ $? == 0 ]]; then
  329. echo ""
  330. LOGI "Panel is running, No need to start again, If you need to restart, please select restart"
  331. else
  332. if [[ $release == "alpine" ]]; then
  333. rc-service x-ui start
  334. else
  335. systemctl start x-ui
  336. fi
  337. sleep 2
  338. check_status
  339. if [[ $? == 0 ]]; then
  340. LOGI "x-ui Started Successfully"
  341. else
  342. LOGE "panel Failed to start, Probably because it takes longer than two seconds to start, Please check the log information later"
  343. fi
  344. fi
  345. if [[ $# == 0 ]]; then
  346. before_show_menu
  347. fi
  348. }
  349. stop() {
  350. check_status
  351. if [[ $? == 1 ]]; then
  352. echo ""
  353. LOGI "Panel stopped, No need to stop again!"
  354. else
  355. if [[ $release == "alpine" ]]; then
  356. rc-service x-ui stop
  357. else
  358. systemctl stop x-ui
  359. fi
  360. sleep 2
  361. check_status
  362. if [[ $? == 1 ]]; then
  363. LOGI "x-ui and xray stopped successfully"
  364. else
  365. LOGE "Panel stop failed, Probably because the stop time exceeds two seconds, Please check the log information later"
  366. fi
  367. fi
  368. if [[ $# == 0 ]]; then
  369. before_show_menu
  370. fi
  371. }
  372. restart() {
  373. if [[ $release == "alpine" ]]; then
  374. rc-service x-ui restart
  375. else
  376. systemctl restart x-ui
  377. fi
  378. sleep 2
  379. check_status
  380. if [[ $? == 0 ]]; then
  381. LOGI "x-ui and xray Restarted successfully"
  382. else
  383. LOGE "Panel restart failed, Probably because it takes longer than two seconds to start, Please check the log information later"
  384. fi
  385. if [[ $# == 0 ]]; then
  386. before_show_menu
  387. fi
  388. }
  389. restart_xray() {
  390. systemctl reload x-ui
  391. LOGI "xray-core Restart signal sent successfully, Please check the log information to confirm whether xray restarted successfully"
  392. sleep 2
  393. show_xray_status
  394. if [[ $# == 0 ]]; then
  395. before_show_menu
  396. fi
  397. }
  398. status() {
  399. if [[ $release == "alpine" ]]; then
  400. rc-service x-ui status
  401. else
  402. systemctl status x-ui -l
  403. fi
  404. if [[ $# == 0 ]]; then
  405. before_show_menu
  406. fi
  407. }
  408. enable() {
  409. if [[ $release == "alpine" ]]; then
  410. rc-update add x-ui default
  411. else
  412. systemctl enable x-ui
  413. fi
  414. if [[ $? == 0 ]]; then
  415. LOGI "x-ui Set to boot automatically on startup successfully"
  416. else
  417. LOGE "x-ui Failed to set Autostart"
  418. fi
  419. if [[ $# == 0 ]]; then
  420. before_show_menu
  421. fi
  422. }
  423. disable() {
  424. if [[ $release == "alpine" ]]; then
  425. rc-update del x-ui
  426. else
  427. systemctl disable x-ui
  428. fi
  429. if [[ $? == 0 ]]; then
  430. LOGI "x-ui Autostart Cancelled successfully"
  431. else
  432. LOGE "x-ui Failed to cancel autostart"
  433. fi
  434. if [[ $# == 0 ]]; then
  435. before_show_menu
  436. fi
  437. }
  438. show_log() {
  439. if [[ $release == "alpine" ]]; then
  440. echo -e "${green}\t1.${plain} Debug Log"
  441. echo -e "${green}\t0.${plain} Back to Main Menu"
  442. read -rp "Choose an option: " choice
  443. case "$choice" in
  444. 0)
  445. show_menu
  446. ;;
  447. 1)
  448. grep -F 'x-ui[' /var/log/messages
  449. if [[ $# == 0 ]]; then
  450. before_show_menu
  451. fi
  452. ;;
  453. *)
  454. echo -e "${red}Invalid option. Please select a valid number.${plain}\n"
  455. show_log
  456. ;;
  457. esac
  458. else
  459. echo -e "${green}\t1.${plain} Debug Log"
  460. echo -e "${green}\t2.${plain} Clear All logs"
  461. echo -e "${green}\t0.${plain} Back to Main Menu"
  462. read -rp "Choose an option: " choice
  463. case "$choice" in
  464. 0)
  465. show_menu
  466. ;;
  467. 1)
  468. journalctl -u x-ui -e --no-pager -f -p debug
  469. if [[ $# == 0 ]]; then
  470. before_show_menu
  471. fi
  472. ;;
  473. 2)
  474. sudo journalctl --rotate
  475. sudo journalctl --vacuum-time=1s
  476. echo "All Logs cleared."
  477. restart
  478. ;;
  479. *)
  480. echo -e "${red}Invalid option. Please select a valid number.${plain}\n"
  481. show_log
  482. ;;
  483. esac
  484. fi
  485. }
  486. bbr_menu() {
  487. echo -e "${green}\t1.${plain} Enable BBR"
  488. echo -e "${green}\t2.${plain} Disable BBR"
  489. echo -e "${green}\t0.${plain} Back to Main Menu"
  490. read -rp "Choose an option: " choice
  491. case "$choice" in
  492. 0)
  493. show_menu
  494. ;;
  495. 1)
  496. enable_bbr
  497. bbr_menu
  498. ;;
  499. 2)
  500. disable_bbr
  501. bbr_menu
  502. ;;
  503. *)
  504. echo -e "${red}Invalid option. Please select a valid number.${plain}\n"
  505. bbr_menu
  506. ;;
  507. esac
  508. }
  509. disable_bbr() {
  510. if [[ $(sysctl -n net.ipv4.tcp_congestion_control) != "bbr" ]] || [[ ! $(sysctl -n net.core.default_qdisc) =~ ^(fq|cake)$ ]]; then
  511. echo -e "${yellow}BBR is not currently enabled.${plain}"
  512. before_show_menu
  513. fi
  514. if [ -f "/etc/sysctl.d/99-bbr-x-ui.conf" ]; then
  515. old_settings=$(head -1 /etc/sysctl.d/99-bbr-x-ui.conf | tr -d '#')
  516. sysctl -w net.core.default_qdisc="${old_settings%:*}"
  517. sysctl -w net.ipv4.tcp_congestion_control="${old_settings#*:}"
  518. rm /etc/sysctl.d/99-bbr-x-ui.conf
  519. sysctl --system
  520. else
  521. # Replace BBR with CUBIC configurations
  522. if [ -f "/etc/sysctl.conf" ]; then
  523. sed -i 's/net.core.default_qdisc=fq/net.core.default_qdisc=pfifo_fast/' /etc/sysctl.conf
  524. sed -i 's/net.ipv4.tcp_congestion_control=bbr/net.ipv4.tcp_congestion_control=cubic/' /etc/sysctl.conf
  525. sysctl -p
  526. fi
  527. fi
  528. if [[ $(sysctl -n net.ipv4.tcp_congestion_control) != "bbr" ]]; then
  529. echo -e "${green}BBR has been replaced with CUBIC successfully.${plain}"
  530. else
  531. echo -e "${red}Failed to replace BBR with CUBIC. Please check your system configuration.${plain}"
  532. fi
  533. }
  534. enable_bbr() {
  535. if [[ $(sysctl -n net.ipv4.tcp_congestion_control) == "bbr" ]] && [[ $(sysctl -n net.core.default_qdisc) =~ ^(fq|cake)$ ]]; then
  536. echo -e "${green}BBR is already enabled!${plain}"
  537. before_show_menu
  538. fi
  539. # Enable BBR
  540. if [ -d "/etc/sysctl.d/" ]; then
  541. {
  542. echo "#$(sysctl -n net.core.default_qdisc):$(sysctl -n net.ipv4.tcp_congestion_control)"
  543. echo "net.core.default_qdisc = fq"
  544. echo "net.ipv4.tcp_congestion_control = bbr"
  545. } > "/etc/sysctl.d/99-bbr-x-ui.conf"
  546. if [ -f "/etc/sysctl.conf" ]; then
  547. # Backup old settings from sysctl.conf, if any
  548. sed -i 's/^net.core.default_qdisc/# &/' /etc/sysctl.conf
  549. sed -i 's/^net.ipv4.tcp_congestion_control/# &/' /etc/sysctl.conf
  550. fi
  551. sysctl --system
  552. else
  553. sed -i '/net.core.default_qdisc/d' /etc/sysctl.conf
  554. sed -i '/net.ipv4.tcp_congestion_control/d' /etc/sysctl.conf
  555. echo "net.core.default_qdisc=fq" | tee -a /etc/sysctl.conf
  556. echo "net.ipv4.tcp_congestion_control=bbr" | tee -a /etc/sysctl.conf
  557. sysctl -p
  558. fi
  559. # Verify that BBR is enabled
  560. if [[ $(sysctl -n net.ipv4.tcp_congestion_control) == "bbr" ]]; then
  561. echo -e "${green}BBR has been enabled successfully.${plain}"
  562. else
  563. echo -e "${red}Failed to enable BBR. Please check your system configuration.${plain}"
  564. fi
  565. }
  566. update_shell() {
  567. curl -fLRo /usr/bin/x-ui -z /usr/bin/x-ui https://github.com/MHSanaei/3x-ui/raw/main/x-ui.sh
  568. if [[ $? != 0 ]]; then
  569. echo ""
  570. LOGE "Failed to download script, Please check whether the machine can connect Github"
  571. before_show_menu
  572. else
  573. chmod +x /usr/bin/x-ui
  574. LOGI "Upgrade script succeeded, Please rerun the script"
  575. before_show_menu
  576. fi
  577. }
  578. # 0: running, 1: not running, 2: not installed
  579. check_status() {
  580. if [[ $release == "alpine" ]]; then
  581. if [[ ! -f /etc/init.d/x-ui ]]; then
  582. return 2
  583. fi
  584. if [[ $(rc-service x-ui status | grep -F 'status: started' -c) == 1 ]]; then
  585. return 0
  586. else
  587. return 1
  588. fi
  589. else
  590. if [[ ! -f ${xui_service}/x-ui.service ]]; then
  591. return 2
  592. fi
  593. temp=$(systemctl status x-ui | grep Active | awk '{print $3}' | cut -d "(" -f2 | cut -d ")" -f1)
  594. if [[ "${temp}" == "running" ]]; then
  595. return 0
  596. else
  597. return 1
  598. fi
  599. fi
  600. }
  601. check_enabled() {
  602. if [[ $release == "alpine" ]]; then
  603. if [[ $(rc-update show | grep -F 'x-ui' | grep default -c) == 1 ]]; then
  604. return 0
  605. else
  606. return 1
  607. fi
  608. else
  609. temp=$(systemctl is-enabled x-ui)
  610. if [[ "${temp}" == "enabled" ]]; then
  611. return 0
  612. else
  613. return 1
  614. fi
  615. fi
  616. }
  617. check_uninstall() {
  618. check_status
  619. if [[ $? != 2 ]]; then
  620. echo ""
  621. LOGE "Panel installed, Please do not reinstall"
  622. if [[ $# == 0 ]]; then
  623. before_show_menu
  624. fi
  625. return 1
  626. else
  627. return 0
  628. fi
  629. }
  630. check_install() {
  631. check_status
  632. if [[ $? == 2 ]]; then
  633. echo ""
  634. LOGE "Please install the panel first"
  635. if [[ $# == 0 ]]; then
  636. before_show_menu
  637. fi
  638. return 1
  639. else
  640. return 0
  641. fi
  642. }
  643. show_status() {
  644. check_status
  645. case $? in
  646. 0)
  647. echo -e "Panel state: ${green}Running${plain}"
  648. show_enable_status
  649. ;;
  650. 1)
  651. echo -e "Panel state: ${yellow}Not Running${plain}"
  652. show_enable_status
  653. ;;
  654. 2)
  655. echo -e "Panel state: ${red}Not Installed${plain}"
  656. ;;
  657. esac
  658. show_xray_status
  659. }
  660. show_enable_status() {
  661. check_enabled
  662. if [[ $? == 0 ]]; then
  663. echo -e "Start automatically: ${green}Yes${plain}"
  664. else
  665. echo -e "Start automatically: ${red}No${plain}"
  666. fi
  667. }
  668. check_xray_status() {
  669. count=$(ps -ef | grep "xray-linux" | grep -v "grep" | wc -l)
  670. if [[ count -ne 0 ]]; then
  671. return 0
  672. else
  673. return 1
  674. fi
  675. }
  676. show_xray_status() {
  677. check_xray_status
  678. if [[ $? == 0 ]]; then
  679. echo -e "xray state: ${green}Running${plain}"
  680. else
  681. echo -e "xray state: ${red}Not Running${plain}"
  682. fi
  683. }
  684. firewall_menu() {
  685. echo -e "${green}\t1.${plain} ${green}Install${plain} Firewall"
  686. echo -e "${green}\t2.${plain} Port List [numbered]"
  687. echo -e "${green}\t3.${plain} ${green}Open${plain} Ports"
  688. echo -e "${green}\t4.${plain} ${red}Delete${plain} Ports from List"
  689. echo -e "${green}\t5.${plain} ${green}Enable${plain} Firewall"
  690. echo -e "${green}\t6.${plain} ${red}Disable${plain} Firewall"
  691. echo -e "${green}\t7.${plain} Firewall Status"
  692. echo -e "${green}\t0.${plain} Back to Main Menu"
  693. read -rp "Choose an option: " choice
  694. case "$choice" in
  695. 0)
  696. show_menu
  697. ;;
  698. 1)
  699. install_firewall
  700. firewall_menu
  701. ;;
  702. 2)
  703. ufw status numbered
  704. firewall_menu
  705. ;;
  706. 3)
  707. open_ports
  708. firewall_menu
  709. ;;
  710. 4)
  711. delete_ports
  712. firewall_menu
  713. ;;
  714. 5)
  715. ufw enable
  716. firewall_menu
  717. ;;
  718. 6)
  719. ufw disable
  720. firewall_menu
  721. ;;
  722. 7)
  723. ufw status verbose
  724. firewall_menu
  725. ;;
  726. *)
  727. echo -e "${red}Invalid option. Please select a valid number.${plain}\n"
  728. firewall_menu
  729. ;;
  730. esac
  731. }
  732. install_firewall() {
  733. if ! command -v ufw &> /dev/null; then
  734. echo "ufw firewall is not installed. Installing now..."
  735. apt-get update
  736. apt-get install -y ufw
  737. else
  738. echo "ufw firewall is already installed"
  739. fi
  740. # Check if the firewall is inactive
  741. if ufw status | grep -q "Status: active"; then
  742. echo "Firewall is already active"
  743. else
  744. echo "Activating firewall..."
  745. # Open the necessary ports
  746. ufw allow ssh
  747. ufw allow http
  748. ufw allow https
  749. ufw allow 2053/tcp #webPort
  750. ufw allow 2096/tcp #subport
  751. # Enable the firewall
  752. ufw --force enable
  753. fi
  754. }
  755. open_ports() {
  756. # Prompt the user to enter the ports they want to open
  757. read -rp "Enter the ports you want to open (e.g. 80,443,2053 or range 400-500): " ports
  758. # Check if the input is valid
  759. if ! [[ $ports =~ ^([0-9]+|[0-9]+-[0-9]+)(,([0-9]+|[0-9]+-[0-9]+))*$ ]]; then
  760. 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
  761. exit 1
  762. fi
  763. # Open the specified ports using ufw
  764. IFS=',' read -ra PORT_LIST <<< "$ports"
  765. for port in "${PORT_LIST[@]}"; do
  766. if [[ $port == *-* ]]; then
  767. # Split the range into start and end ports
  768. start_port=$(echo $port | cut -d'-' -f1)
  769. end_port=$(echo $port | cut -d'-' -f2)
  770. # Open the port range
  771. ufw allow $start_port:$end_port/tcp
  772. ufw allow $start_port:$end_port/udp
  773. else
  774. # Open the single port
  775. ufw allow "$port"
  776. fi
  777. done
  778. # Confirm that the ports are opened
  779. echo "Opened the specified ports:"
  780. for port in "${PORT_LIST[@]}"; do
  781. if [[ $port == *-* ]]; then
  782. start_port=$(echo $port | cut -d'-' -f1)
  783. end_port=$(echo $port | cut -d'-' -f2)
  784. # Check if the port range has been successfully opened
  785. (ufw status | grep -q "$start_port:$end_port") && echo "$start_port-$end_port"
  786. else
  787. # Check if the individual port has been successfully opened
  788. (ufw status | grep -q "$port") && echo "$port"
  789. fi
  790. done
  791. }
  792. delete_ports() {
  793. # Display current rules with numbers
  794. echo "Current UFW rules:"
  795. ufw status numbered
  796. # Ask the user how they want to delete rules
  797. echo "Do you want to delete rules by:"
  798. echo "1) Rule numbers"
  799. echo "2) Ports"
  800. read -rp "Enter your choice (1 or 2): " choice
  801. if [[ $choice -eq 1 ]]; then
  802. # Deleting by rule numbers
  803. read -rp "Enter the rule numbers you want to delete (1, 2, etc.): " rule_numbers
  804. # Validate the input
  805. if ! [[ $rule_numbers =~ ^([0-9]+)(,[0-9]+)*$ ]]; then
  806. echo "Error: Invalid input. Please enter a comma-separated list of rule numbers." >&2
  807. exit 1
  808. fi
  809. # Split numbers into an array
  810. IFS=',' read -ra RULE_NUMBERS <<< "$rule_numbers"
  811. for rule_number in "${RULE_NUMBERS[@]}"; do
  812. # Delete the rule by number
  813. ufw delete "$rule_number" || echo "Failed to delete rule number $rule_number"
  814. done
  815. echo "Selected rules have been deleted."
  816. elif [[ $choice -eq 2 ]]; then
  817. # Deleting by ports
  818. read -rp "Enter the ports you want to delete (e.g. 80,443,2053 or range 400-500): " ports
  819. # Validate the input
  820. if ! [[ $ports =~ ^([0-9]+|[0-9]+-[0-9]+)(,([0-9]+|[0-9]+-[0-9]+))*$ ]]; then
  821. 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
  822. exit 1
  823. fi
  824. # Split ports into an array
  825. IFS=',' read -ra PORT_LIST <<< "$ports"
  826. for port in "${PORT_LIST[@]}"; do
  827. if [[ $port == *-* ]]; then
  828. # Split the port range
  829. start_port=$(echo $port | cut -d'-' -f1)
  830. end_port=$(echo $port | cut -d'-' -f2)
  831. # Delete the port range
  832. ufw delete allow $start_port:$end_port/tcp
  833. ufw delete allow $start_port:$end_port/udp
  834. else
  835. # Delete a single port
  836. ufw delete allow "$port"
  837. fi
  838. done
  839. # Confirmation of deletion
  840. echo "Deleted the specified ports:"
  841. for port in "${PORT_LIST[@]}"; do
  842. if [[ $port == *-* ]]; then
  843. start_port=$(echo $port | cut -d'-' -f1)
  844. end_port=$(echo $port | cut -d'-' -f2)
  845. # Check if the port range has been deleted
  846. (ufw status | grep -q "$start_port:$end_port") || echo "$start_port-$end_port"
  847. else
  848. # Check if the individual port has been deleted
  849. (ufw status | grep -q "$port") || echo "$port"
  850. fi
  851. done
  852. else
  853. echo "${red}Error:${plain} Invalid choice. Please enter 1 or 2." >&2
  854. exit 1
  855. fi
  856. }
  857. update_all_geofiles() {
  858. update_geofiles "main"
  859. update_geofiles "IR"
  860. update_geofiles "RU"
  861. }
  862. update_geofiles() {
  863. case "${1}" in
  864. "main")
  865. dat_files=(geoip geosite)
  866. dat_source="Loyalsoldier/v2ray-rules-dat"
  867. ;;
  868. "IR")
  869. dat_files=(geoip_IR geosite_IR)
  870. dat_source="chocolate4u/Iran-v2ray-rules"
  871. ;;
  872. "RU")
  873. dat_files=(geoip_RU geosite_RU)
  874. dat_source="runetfreedom/russia-v2ray-rules-dat"
  875. ;;
  876. esac
  877. for dat in "${dat_files[@]}"; do
  878. # Remove suffix for remote filename (e.g., geoip_IR -> geoip)
  879. remote_file="${dat%%_*}"
  880. curl -fLRo ${xui_folder}/bin/${dat}.dat -z ${xui_folder}/bin/${dat}.dat \
  881. https://github.com/${dat_source}/releases/latest/download/${remote_file}.dat
  882. done
  883. }
  884. update_geo() {
  885. echo -e "${green}\t1.${plain} Loyalsoldier (geoip.dat, geosite.dat)"
  886. echo -e "${green}\t2.${plain} chocolate4u (geoip_IR.dat, geosite_IR.dat)"
  887. echo -e "${green}\t3.${plain} runetfreedom (geoip_RU.dat, geosite_RU.dat)"
  888. echo -e "${green}\t4.${plain} All"
  889. echo -e "${green}\t0.${plain} Back to Main Menu"
  890. read -rp "Choose an option: " choice
  891. case "$choice" in
  892. 0)
  893. show_menu
  894. ;;
  895. 1)
  896. update_geofiles "main"
  897. echo -e "${green}Loyalsoldier datasets have been updated successfully!${plain}"
  898. restart
  899. ;;
  900. 2)
  901. update_geofiles "IR"
  902. echo -e "${green}chocolate4u datasets have been updated successfully!${plain}"
  903. restart
  904. ;;
  905. 3)
  906. update_geofiles "RU"
  907. echo -e "${green}runetfreedom datasets have been updated successfully!${plain}"
  908. restart
  909. ;;
  910. 4)
  911. update_all_geofiles
  912. echo -e "${green}All geo files have been updated successfully!${plain}"
  913. restart
  914. ;;
  915. *)
  916. echo -e "${red}Invalid option. Please select a valid number.${plain}\n"
  917. update_geo
  918. ;;
  919. esac
  920. before_show_menu
  921. }
  922. install_acme() {
  923. # Check if acme.sh is already installed
  924. if command -v ~/.acme.sh/acme.sh &> /dev/null; then
  925. LOGI "acme.sh is already installed."
  926. return 0
  927. fi
  928. LOGI "Installing acme.sh..."
  929. cd ~ || return 1 # Ensure you can change to the home directory
  930. curl -s https://get.acme.sh | sh
  931. if [ $? -ne 0 ]; then
  932. LOGE "Installation of acme.sh failed."
  933. return 1
  934. else
  935. LOGI "Installation of acme.sh succeeded."
  936. fi
  937. return 0
  938. }
  939. ssl_cert_issue_main() {
  940. echo -e "${green}\t1.${plain} Get SSL (Domain)"
  941. echo -e "${green}\t2.${plain} Revoke"
  942. echo -e "${green}\t3.${plain} Force Renew"
  943. echo -e "${green}\t4.${plain} Show Existing Domains"
  944. echo -e "${green}\t5.${plain} Set Cert paths for the panel"
  945. echo -e "${green}\t6.${plain} Get SSL for IP Address (6-day cert, auto-renews)"
  946. echo -e "${green}\t0.${plain} Back to Main Menu"
  947. read -rp "Choose an option: " choice
  948. case "$choice" in
  949. 0)
  950. show_menu
  951. ;;
  952. 1)
  953. ssl_cert_issue
  954. ssl_cert_issue_main
  955. ;;
  956. 2)
  957. local domains=$(find /root/cert/ -mindepth 1 -maxdepth 1 -type d -exec basename {} \;)
  958. if [ -z "$domains" ]; then
  959. echo "No certificates found to revoke."
  960. else
  961. echo "Existing domains:"
  962. echo "$domains"
  963. read -rp "Please enter a domain from the list to revoke the certificate: " domain
  964. if echo "$domains" | grep -qw "$domain"; then
  965. ~/.acme.sh/acme.sh --revoke -d ${domain}
  966. LOGI "Certificate revoked for domain: $domain"
  967. else
  968. echo "Invalid domain entered."
  969. fi
  970. fi
  971. ssl_cert_issue_main
  972. ;;
  973. 3)
  974. local domains=$(find /root/cert/ -mindepth 1 -maxdepth 1 -type d -exec basename {} \;)
  975. if [ -z "$domains" ]; then
  976. echo "No certificates found to renew."
  977. else
  978. echo "Existing domains:"
  979. echo "$domains"
  980. read -rp "Please enter a domain from the list to renew the SSL certificate: " domain
  981. if echo "$domains" | grep -qw "$domain"; then
  982. ~/.acme.sh/acme.sh --renew -d ${domain} --force
  983. LOGI "Certificate forcefully renewed for domain: $domain"
  984. else
  985. echo "Invalid domain entered."
  986. fi
  987. fi
  988. ssl_cert_issue_main
  989. ;;
  990. 4)
  991. local domains=$(find /root/cert/ -mindepth 1 -maxdepth 1 -type d -exec basename {} \;)
  992. if [ -z "$domains" ]; then
  993. echo "No certificates found."
  994. else
  995. echo "Existing domains and their paths:"
  996. for domain in $domains; do
  997. local cert_path="/root/cert/${domain}/fullchain.pem"
  998. local key_path="/root/cert/${domain}/privkey.pem"
  999. if [[ -f "${cert_path}" && -f "${key_path}" ]]; then
  1000. echo -e "Domain: ${domain}"
  1001. echo -e "\tCertificate Path: ${cert_path}"
  1002. echo -e "\tPrivate Key Path: ${key_path}"
  1003. else
  1004. echo -e "Domain: ${domain} - Certificate or Key missing."
  1005. fi
  1006. done
  1007. fi
  1008. ssl_cert_issue_main
  1009. ;;
  1010. 5)
  1011. local domains=$(find /root/cert/ -mindepth 1 -maxdepth 1 -type d -exec basename {} \;)
  1012. if [ -z "$domains" ]; then
  1013. echo "No certificates found."
  1014. else
  1015. echo "Available domains:"
  1016. echo "$domains"
  1017. read -rp "Please choose a domain to set the panel paths: " domain
  1018. if echo "$domains" | grep -qw "$domain"; then
  1019. local webCertFile="/root/cert/${domain}/fullchain.pem"
  1020. local webKeyFile="/root/cert/${domain}/privkey.pem"
  1021. if [[ -f "${webCertFile}" && -f "${webKeyFile}" ]]; then
  1022. ${xui_folder}/x-ui cert -webCert "$webCertFile" -webCertKey "$webKeyFile"
  1023. echo "Panel paths set for domain: $domain"
  1024. echo " - Certificate File: $webCertFile"
  1025. echo " - Private Key File: $webKeyFile"
  1026. restart
  1027. else
  1028. echo "Certificate or private key not found for domain: $domain."
  1029. fi
  1030. else
  1031. echo "Invalid domain entered."
  1032. fi
  1033. fi
  1034. ssl_cert_issue_main
  1035. ;;
  1036. 6)
  1037. echo -e "${yellow}Let's Encrypt SSL Certificate for IP Address${plain}"
  1038. echo -e "This will obtain a certificate for your server's IP using the shortlived profile."
  1039. echo -e "${yellow}Certificate valid for ~6 days, auto-renews via acme.sh cron job.${plain}"
  1040. echo -e "${yellow}Port 80 must be open and accessible from the internet.${plain}"
  1041. confirm "Do you want to proceed?" "y"
  1042. if [[ $? == 0 ]]; then
  1043. ssl_cert_issue_for_ip
  1044. fi
  1045. ssl_cert_issue_main
  1046. ;;
  1047. *)
  1048. echo -e "${red}Invalid option. Please select a valid number.${plain}\n"
  1049. ssl_cert_issue_main
  1050. ;;
  1051. esac
  1052. }
  1053. ssl_cert_issue_for_ip() {
  1054. LOGI "Starting automatic SSL certificate generation for server IP..."
  1055. LOGI "Using Let's Encrypt shortlived profile (~6 days validity, auto-renews)"
  1056. local existing_webBasePath=$(${xui_folder}/x-ui setting -show true | grep -Eo 'webBasePath: .+' | awk '{print $2}')
  1057. local existing_port=$(${xui_folder}/x-ui setting -show true | grep -Eo 'port: .+' | awk '{print $2}')
  1058. # Get server IP
  1059. local URL_lists=(
  1060. "https://api4.ipify.org"
  1061. "https://ipv4.icanhazip.com"
  1062. "https://v4.api.ipinfo.io/ip"
  1063. "https://ipv4.myexternalip.com/raw"
  1064. "https://4.ident.me"
  1065. "https://check-host.net/ip"
  1066. )
  1067. local server_ip=""
  1068. for ip_address in "${URL_lists[@]}"; do
  1069. local response=$(curl -s -w "\n%{http_code}" --max-time 3 "${ip_address}" 2> /dev/null)
  1070. local http_code=$(echo "$response" | tail -n1)
  1071. local ip_result=$(echo "$response" | head -n-1 | tr -d '[:space:]"')
  1072. if [[ "${http_code}" == "200" && "${ip_result}" =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
  1073. server_ip="${ip_result}"
  1074. break
  1075. fi
  1076. done
  1077. if [[ -z "$server_ip" ]]; then
  1078. LOGI "Could not auto-detect server IP from any provider."
  1079. while [[ -z "$server_ip" ]]; do
  1080. read -rp "Please enter your server's public IPv4 address: " server_ip
  1081. server_ip="${server_ip// /}"
  1082. if [[ ! "$server_ip" =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
  1083. LOGE "Invalid IPv4 address. Please try again."
  1084. server_ip=""
  1085. fi
  1086. done
  1087. fi
  1088. LOGI "Server IP detected: ${server_ip}"
  1089. # Ask for optional IPv6
  1090. local ipv6_addr=""
  1091. read -rp "Do you have an IPv6 address to include? (leave empty to skip): " ipv6_addr
  1092. ipv6_addr="${ipv6_addr// /}" # Trim whitespace
  1093. # check for acme.sh first
  1094. if ! command -v ~/.acme.sh/acme.sh &> /dev/null; then
  1095. LOGI "acme.sh not found, installing..."
  1096. install_acme
  1097. if [ $? -ne 0 ]; then
  1098. LOGE "Failed to install acme.sh"
  1099. return 1
  1100. fi
  1101. fi
  1102. # install socat
  1103. case "${release}" in
  1104. ubuntu | debian | armbian)
  1105. apt-get update > /dev/null 2>&1 && apt-get install socat -y > /dev/null 2>&1
  1106. ;;
  1107. fedora | amzn | virtuozzo | rhel | almalinux | rocky | ol)
  1108. dnf -y update > /dev/null 2>&1 && dnf -y install socat > /dev/null 2>&1
  1109. ;;
  1110. centos)
  1111. if [[ "${VERSION_ID}" =~ ^7 ]]; then
  1112. yum -y update > /dev/null 2>&1 && yum -y install socat > /dev/null 2>&1
  1113. else
  1114. dnf -y update > /dev/null 2>&1 && dnf -y install socat > /dev/null 2>&1
  1115. fi
  1116. ;;
  1117. arch | manjaro | parch)
  1118. pacman -Sy --noconfirm socat > /dev/null 2>&1
  1119. ;;
  1120. opensuse-tumbleweed | opensuse-leap)
  1121. zypper refresh > /dev/null 2>&1 && zypper -q install -y socat > /dev/null 2>&1
  1122. ;;
  1123. alpine)
  1124. apk add socat curl openssl > /dev/null 2>&1
  1125. ;;
  1126. *)
  1127. LOGW "Unsupported OS for automatic socat installation"
  1128. ;;
  1129. esac
  1130. # Create certificate directory
  1131. certPath="/root/cert/ip"
  1132. mkdir -p "$certPath"
  1133. # Build domain arguments
  1134. local domain_args="-d ${server_ip}"
  1135. if [[ -n "$ipv6_addr" ]] && is_ipv6 "$ipv6_addr"; then
  1136. domain_args="${domain_args} -d ${ipv6_addr}"
  1137. LOGI "Including IPv6 address: ${ipv6_addr}"
  1138. fi
  1139. # Choose port for HTTP-01 listener (default 80, allow override)
  1140. local WebPort=""
  1141. read -rp "Port to use for ACME HTTP-01 listener (default 80): " WebPort
  1142. WebPort="${WebPort:-80}"
  1143. if ! [[ "${WebPort}" =~ ^[0-9]+$ ]] || ((WebPort < 1 || WebPort > 65535)); then
  1144. LOGE "Invalid port provided. Falling back to 80."
  1145. WebPort=80
  1146. fi
  1147. LOGI "Using port ${WebPort} to issue certificate for IP: ${server_ip}"
  1148. if [[ "${WebPort}" -ne 80 ]]; then
  1149. LOGI "Reminder: Let's Encrypt still reaches port 80; forward external port 80 to ${WebPort} for validation."
  1150. fi
  1151. while true; do
  1152. if is_port_in_use "${WebPort}"; then
  1153. LOGI "Port ${WebPort} is currently in use."
  1154. local alt_port=""
  1155. read -rp "Enter another port for acme.sh standalone listener (leave empty to abort): " alt_port
  1156. alt_port="${alt_port// /}"
  1157. if [[ -z "${alt_port}" ]]; then
  1158. LOGE "Port ${WebPort} is busy; cannot proceed with issuance."
  1159. return 1
  1160. fi
  1161. if ! [[ "${alt_port}" =~ ^[0-9]+$ ]] || ((alt_port < 1 || alt_port > 65535)); then
  1162. LOGE "Invalid port provided."
  1163. return 1
  1164. fi
  1165. WebPort="${alt_port}"
  1166. continue
  1167. else
  1168. LOGI "Port ${WebPort} is free and ready for standalone validation."
  1169. break
  1170. fi
  1171. done
  1172. # Reload command - restarts panel after renewal
  1173. local reloadCmd="systemctl restart x-ui 2>/dev/null || rc-service x-ui restart 2>/dev/null"
  1174. # issue the certificate for IP with shortlived profile
  1175. ~/.acme.sh/acme.sh --set-default-ca --server letsencrypt --force
  1176. ~/.acme.sh/acme.sh --issue \
  1177. ${domain_args} \
  1178. --standalone \
  1179. --server letsencrypt \
  1180. --certificate-profile shortlived \
  1181. --days 6 \
  1182. --httpport ${WebPort} \
  1183. --force
  1184. if [ $? -ne 0 ]; then
  1185. LOGE "Failed to issue certificate for IP: ${server_ip}"
  1186. LOGE "Make sure port ${WebPort} is open and the server is accessible from the internet"
  1187. # Cleanup acme.sh data for both IPv4 and IPv6 if specified
  1188. rm -rf ~/.acme.sh/${server_ip} 2> /dev/null
  1189. [[ -n "$ipv6_addr" ]] && rm -rf ~/.acme.sh/${ipv6_addr} 2> /dev/null
  1190. rm -rf ${certPath} 2> /dev/null
  1191. return 1
  1192. else
  1193. LOGI "Certificate issued successfully for IP: ${server_ip}"
  1194. fi
  1195. # Install the certificate
  1196. # Note: acme.sh may report "Reload error" and exit non-zero if reloadcmd fails,
  1197. # but the cert files are still installed. We check for files instead of exit code.
  1198. ~/.acme.sh/acme.sh --installcert -d ${server_ip} \
  1199. --key-file "${certPath}/privkey.pem" \
  1200. --fullchain-file "${certPath}/fullchain.pem" \
  1201. --reloadcmd "${reloadCmd}" 2>&1 || true
  1202. # Verify certificate files exist (don't rely on exit code - reloadcmd failure causes non-zero)
  1203. if [[ ! -f "${certPath}/fullchain.pem" || ! -f "${certPath}/privkey.pem" ]]; then
  1204. LOGE "Certificate files not found after installation"
  1205. # Cleanup acme.sh data for both IPv4 and IPv6 if specified
  1206. rm -rf ~/.acme.sh/${server_ip} 2> /dev/null
  1207. [[ -n "$ipv6_addr" ]] && rm -rf ~/.acme.sh/${ipv6_addr} 2> /dev/null
  1208. rm -rf ${certPath} 2> /dev/null
  1209. return 1
  1210. fi
  1211. LOGI "Certificate files installed successfully"
  1212. # enable auto-renew
  1213. ~/.acme.sh/acme.sh --upgrade --auto-upgrade > /dev/null 2>&1
  1214. chmod 600 $certPath/privkey.pem 2> /dev/null
  1215. chmod 644 $certPath/fullchain.pem 2> /dev/null
  1216. # Set certificate paths for the panel
  1217. local webCertFile="${certPath}/fullchain.pem"
  1218. local webKeyFile="${certPath}/privkey.pem"
  1219. if [[ -f "$webCertFile" && -f "$webKeyFile" ]]; then
  1220. ${xui_folder}/x-ui cert -webCert "$webCertFile" -webCertKey "$webKeyFile"
  1221. LOGI "Certificate configured for panel"
  1222. LOGI " - Certificate File: $webCertFile"
  1223. LOGI " - Private Key File: $webKeyFile"
  1224. LOGI " - Validity: ~6 days (auto-renews via acme.sh cron)"
  1225. echo -e "${green}Access URL: https://${server_ip}:${existing_port}${existing_webBasePath}${plain}"
  1226. LOGI "Panel will restart to apply SSL certificate..."
  1227. restart
  1228. return 0
  1229. else
  1230. LOGE "Certificate files not found after installation"
  1231. return 1
  1232. fi
  1233. }
  1234. ssl_cert_issue() {
  1235. local existing_webBasePath=$(${xui_folder}/x-ui setting -show true | grep -Eo 'webBasePath: .+' | awk '{print $2}')
  1236. local existing_port=$(${xui_folder}/x-ui setting -show true | grep -Eo 'port: .+' | awk '{print $2}')
  1237. # check for acme.sh first
  1238. if ! command -v ~/.acme.sh/acme.sh &> /dev/null; then
  1239. echo "acme.sh could not be found. we will install it"
  1240. install_acme
  1241. if [ $? -ne 0 ]; then
  1242. LOGE "install acme failed, please check logs"
  1243. exit 1
  1244. fi
  1245. fi
  1246. # install socat
  1247. case "${release}" in
  1248. ubuntu | debian | armbian)
  1249. apt-get update > /dev/null 2>&1 && apt-get install socat -y > /dev/null 2>&1
  1250. ;;
  1251. fedora | amzn | virtuozzo | rhel | almalinux | rocky | ol)
  1252. dnf -y update > /dev/null 2>&1 && dnf -y install socat > /dev/null 2>&1
  1253. ;;
  1254. centos)
  1255. if [[ "${VERSION_ID}" =~ ^7 ]]; then
  1256. yum -y update > /dev/null 2>&1 && yum -y install socat > /dev/null 2>&1
  1257. else
  1258. dnf -y update > /dev/null 2>&1 && dnf -y install socat > /dev/null 2>&1
  1259. fi
  1260. ;;
  1261. arch | manjaro | parch)
  1262. pacman -Sy --noconfirm socat > /dev/null 2>&1
  1263. ;;
  1264. opensuse-tumbleweed | opensuse-leap)
  1265. zypper refresh > /dev/null 2>&1 && zypper -q install -y socat > /dev/null 2>&1
  1266. ;;
  1267. alpine)
  1268. apk add socat curl openssl > /dev/null 2>&1
  1269. ;;
  1270. *)
  1271. LOGW "Unsupported OS for automatic socat installation"
  1272. ;;
  1273. esac
  1274. if [ $? -ne 0 ]; then
  1275. LOGE "install socat failed, please check logs"
  1276. exit 1
  1277. else
  1278. LOGI "install socat succeed..."
  1279. fi
  1280. # get the domain here, and we need to verify it
  1281. local domain=""
  1282. while true; do
  1283. read -rp "Please enter your domain name: " domain
  1284. domain="${domain// /}" # Trim whitespace
  1285. if [[ -z "$domain" ]]; then
  1286. LOGE "Domain name cannot be empty. Please try again."
  1287. continue
  1288. fi
  1289. if ! is_domain "$domain"; then
  1290. LOGE "Invalid domain format: ${domain}. Please enter a valid domain name."
  1291. continue
  1292. fi
  1293. break
  1294. done
  1295. LOGD "Your domain is: ${domain}, checking it..."
  1296. SSL_ISSUED_DOMAIN="${domain}"
  1297. # detect existing certificate and reuse it if present
  1298. local cert_exists=0
  1299. if ~/.acme.sh/acme.sh --list 2> /dev/null | awk '{print $1}' | grep -Fxq "${domain}"; then
  1300. cert_exists=1
  1301. local certInfo=$(~/.acme.sh/acme.sh --list 2> /dev/null | grep -F "${domain}")
  1302. LOGI "Existing certificate found for ${domain}, will reuse it."
  1303. [[ -n "${certInfo}" ]] && LOGI "${certInfo}"
  1304. else
  1305. LOGI "Your domain is ready for issuing certificates now..."
  1306. fi
  1307. # create a directory for the certificate
  1308. certPath="/root/cert/${domain}"
  1309. if [ ! -d "$certPath" ]; then
  1310. mkdir -p "$certPath"
  1311. else
  1312. rm -rf "$certPath"
  1313. mkdir -p "$certPath"
  1314. fi
  1315. # get the port number for the standalone server
  1316. local WebPort=80
  1317. read -rp "Please choose which port to use (default is 80): " WebPort
  1318. if [[ ${WebPort} -gt 65535 || ${WebPort} -lt 1 ]]; then
  1319. LOGE "Your input ${WebPort} is invalid, will use default port 80."
  1320. WebPort=80
  1321. fi
  1322. LOGI "Will use port: ${WebPort} to issue certificates. Please make sure this port is open."
  1323. if [[ ${cert_exists} -eq 0 ]]; then
  1324. # issue the certificate
  1325. ~/.acme.sh/acme.sh --set-default-ca --server letsencrypt --force
  1326. ~/.acme.sh/acme.sh --issue -d ${domain} --listen-v6 --standalone --httpport ${WebPort} --force
  1327. if [ $? -ne 0 ]; then
  1328. LOGE "Issuing certificate failed, please check logs."
  1329. rm -rf ~/.acme.sh/${domain}
  1330. exit 1
  1331. else
  1332. LOGE "Issuing certificate succeeded, installing certificates..."
  1333. fi
  1334. else
  1335. LOGI "Using existing certificate, installing certificates..."
  1336. fi
  1337. reloadCmd="x-ui restart"
  1338. LOGI "Default --reloadcmd for ACME is: ${yellow}x-ui restart"
  1339. LOGI "This command will run on every certificate issue and renew."
  1340. read -rp "Would you like to modify --reloadcmd for ACME? (y/n): " setReloadcmd
  1341. if [[ "$setReloadcmd" == "y" || "$setReloadcmd" == "Y" ]]; then
  1342. echo -e "\n${green}\t1.${plain} Preset: systemctl reload nginx ; x-ui restart"
  1343. echo -e "${green}\t2.${plain} Input your own command"
  1344. echo -e "${green}\t0.${plain} Keep default reloadcmd"
  1345. read -rp "Choose an option: " choice
  1346. case "$choice" in
  1347. 1)
  1348. LOGI "Reloadcmd is: systemctl reload nginx ; x-ui restart"
  1349. reloadCmd="systemctl reload nginx ; x-ui restart"
  1350. ;;
  1351. 2)
  1352. LOGD "It's recommended to put x-ui restart at the end, so it won't raise an error if other services fails"
  1353. read -rp "Please enter your reloadcmd (example: systemctl reload nginx ; x-ui restart): " reloadCmd
  1354. LOGI "Your reloadcmd is: ${reloadCmd}"
  1355. ;;
  1356. *)
  1357. LOGI "Keep default reloadcmd"
  1358. ;;
  1359. esac
  1360. fi
  1361. # install the certificate
  1362. local installOutput=""
  1363. installOutput=$(~/.acme.sh/acme.sh --installcert -d ${domain} \
  1364. --key-file /root/cert/${domain}/privkey.pem \
  1365. --fullchain-file /root/cert/${domain}/fullchain.pem --reloadcmd "${reloadCmd}" 2>&1)
  1366. local installRc=$?
  1367. echo "${installOutput}"
  1368. local installWroteFiles=0
  1369. if echo "${installOutput}" | grep -q "Installing key to:" && echo "${installOutput}" | grep -q "Installing full chain to:"; then
  1370. installWroteFiles=1
  1371. fi
  1372. if [[ -f "/root/cert/${domain}/privkey.pem" && -f "/root/cert/${domain}/fullchain.pem" && (${installRc} -eq 0 || ${installWroteFiles} -eq 1) ]]; then
  1373. LOGI "Installing certificate succeeded, enabling auto renew..."
  1374. else
  1375. LOGE "Installing certificate failed, exiting."
  1376. if [[ ${cert_exists} -eq 0 ]]; then
  1377. rm -rf ~/.acme.sh/${domain}
  1378. fi
  1379. exit 1
  1380. fi
  1381. # enable auto-renew
  1382. ~/.acme.sh/acme.sh --upgrade --auto-upgrade
  1383. if [ $? -ne 0 ]; then
  1384. LOGE "Auto renew failed, certificate details:"
  1385. ls -lah cert/*
  1386. chmod 600 $certPath/privkey.pem
  1387. chmod 644 $certPath/fullchain.pem
  1388. exit 1
  1389. else
  1390. LOGI "Auto renew succeeded, certificate details:"
  1391. ls -lah cert/*
  1392. chmod 600 $certPath/privkey.pem
  1393. chmod 644 $certPath/fullchain.pem
  1394. fi
  1395. # Prompt user to set panel paths after successful certificate installation
  1396. read -rp "Would you like to set this certificate for the panel? (y/n): " setPanel
  1397. if [[ "$setPanel" == "y" || "$setPanel" == "Y" ]]; then
  1398. local webCertFile="/root/cert/${domain}/fullchain.pem"
  1399. local webKeyFile="/root/cert/${domain}/privkey.pem"
  1400. if [[ -f "$webCertFile" && -f "$webKeyFile" ]]; then
  1401. ${xui_folder}/x-ui cert -webCert "$webCertFile" -webCertKey "$webKeyFile"
  1402. LOGI "Panel paths set for domain: $domain"
  1403. LOGI " - Certificate File: $webCertFile"
  1404. LOGI " - Private Key File: $webKeyFile"
  1405. echo -e "${green}Access URL: https://${domain}:${existing_port}${existing_webBasePath}${plain}"
  1406. restart
  1407. else
  1408. LOGE "Error: Certificate or private key file not found for domain: $domain."
  1409. fi
  1410. else
  1411. LOGI "Skipping panel path setting."
  1412. fi
  1413. }
  1414. ssl_cert_issue_CF() {
  1415. local existing_webBasePath=$(${xui_folder}/x-ui setting -show true | grep -Eo 'webBasePath: .+' | awk '{print $2}')
  1416. local existing_port=$(${xui_folder}/x-ui setting -show true | grep -Eo 'port: .+' | awk '{print $2}')
  1417. LOGI "****** Instructions for Use ******"
  1418. LOGI "Follow the steps below to complete the process:"
  1419. LOGI "1. Cloudflare Registered E-mail."
  1420. LOGI "2. Cloudflare Global API Key."
  1421. LOGI "3. The Domain Name."
  1422. LOGI "4. Once the certificate is issued, you will be prompted to set the certificate for the panel (optional)."
  1423. LOGI "5. The script also supports automatic renewal of the SSL certificate after installation."
  1424. confirm "Do you confirm the information and wish to proceed? [y/n]" "y"
  1425. if [ $? -eq 0 ]; then
  1426. # Check for acme.sh first
  1427. if ! command -v ~/.acme.sh/acme.sh &> /dev/null; then
  1428. echo "acme.sh could not be found. We will install it."
  1429. install_acme
  1430. if [ $? -ne 0 ]; then
  1431. LOGE "Install acme failed, please check logs."
  1432. exit 1
  1433. fi
  1434. fi
  1435. CF_Domain=""
  1436. LOGD "Please set a domain name:"
  1437. read -rp "Input your domain here: " CF_Domain
  1438. LOGD "Your domain name is set to: ${CF_Domain}"
  1439. # Set up Cloudflare API details
  1440. CF_GlobalKey=""
  1441. CF_AccountEmail=""
  1442. LOGD "Please set the API key:"
  1443. read -rp "Input your key here: " CF_GlobalKey
  1444. LOGD "Your API key is: ${CF_GlobalKey}"
  1445. LOGD "Please set up registered email:"
  1446. read -rp "Input your email here: " CF_AccountEmail
  1447. LOGD "Your registered email address is: ${CF_AccountEmail}"
  1448. # Set the default CA to Let's Encrypt
  1449. ~/.acme.sh/acme.sh --set-default-ca --server letsencrypt --force
  1450. if [ $? -ne 0 ]; then
  1451. LOGE "Default CA, Let'sEncrypt fail, script exiting..."
  1452. exit 1
  1453. fi
  1454. export CF_Key="${CF_GlobalKey}"
  1455. export CF_Email="${CF_AccountEmail}"
  1456. # Issue the certificate using Cloudflare DNS
  1457. ~/.acme.sh/acme.sh --issue --dns dns_cf -d ${CF_Domain} -d *.${CF_Domain} --log --force
  1458. if [ $? -ne 0 ]; then
  1459. LOGE "Certificate issuance failed, script exiting..."
  1460. exit 1
  1461. else
  1462. LOGI "Certificate issued successfully, Installing..."
  1463. fi
  1464. # Install the certificate
  1465. certPath="/root/cert/${CF_Domain}"
  1466. if [ -d "$certPath" ]; then
  1467. rm -rf ${certPath}
  1468. fi
  1469. mkdir -p ${certPath}
  1470. if [ $? -ne 0 ]; then
  1471. LOGE "Failed to create directory: ${certPath}"
  1472. exit 1
  1473. fi
  1474. reloadCmd="x-ui restart"
  1475. LOGI "Default --reloadcmd for ACME is: ${yellow}x-ui restart"
  1476. LOGI "This command will run on every certificate issue and renew."
  1477. read -rp "Would you like to modify --reloadcmd for ACME? (y/n): " setReloadcmd
  1478. if [[ "$setReloadcmd" == "y" || "$setReloadcmd" == "Y" ]]; then
  1479. echo -e "\n${green}\t1.${plain} Preset: systemctl reload nginx ; x-ui restart"
  1480. echo -e "${green}\t2.${plain} Input your own command"
  1481. echo -e "${green}\t0.${plain} Keep default reloadcmd"
  1482. read -rp "Choose an option: " choice
  1483. case "$choice" in
  1484. 1)
  1485. LOGI "Reloadcmd is: systemctl reload nginx ; x-ui restart"
  1486. reloadCmd="systemctl reload nginx ; x-ui restart"
  1487. ;;
  1488. 2)
  1489. LOGD "It's recommended to put x-ui restart at the end, so it won't raise an error if other services fails"
  1490. read -rp "Please enter your reloadcmd (example: systemctl reload nginx ; x-ui restart): " reloadCmd
  1491. LOGI "Your reloadcmd is: ${reloadCmd}"
  1492. ;;
  1493. *)
  1494. LOGI "Keep default reloadcmd"
  1495. ;;
  1496. esac
  1497. fi
  1498. ~/.acme.sh/acme.sh --installcert -d ${CF_Domain} -d *.${CF_Domain} \
  1499. --key-file ${certPath}/privkey.pem \
  1500. --fullchain-file ${certPath}/fullchain.pem --reloadcmd "${reloadCmd}"
  1501. if [ $? -ne 0 ]; then
  1502. LOGE "Certificate installation failed, script exiting..."
  1503. exit 1
  1504. else
  1505. LOGI "Certificate installed successfully, Turning on automatic updates..."
  1506. fi
  1507. # Enable auto-update
  1508. ~/.acme.sh/acme.sh --upgrade --auto-upgrade
  1509. if [ $? -ne 0 ]; then
  1510. LOGE "Auto update setup failed, script exiting..."
  1511. exit 1
  1512. else
  1513. LOGI "The certificate is installed and auto-renewal is turned on. Specific information is as follows:"
  1514. ls -lah ${certPath}/*
  1515. chmod 600 ${certPath}/privkey.pem
  1516. chmod 644 ${certPath}/fullchain.pem
  1517. fi
  1518. # Prompt user to set panel paths after successful certificate installation
  1519. read -rp "Would you like to set this certificate for the panel? (y/n): " setPanel
  1520. if [[ "$setPanel" == "y" || "$setPanel" == "Y" ]]; then
  1521. local webCertFile="${certPath}/fullchain.pem"
  1522. local webKeyFile="${certPath}/privkey.pem"
  1523. if [[ -f "$webCertFile" && -f "$webKeyFile" ]]; then
  1524. ${xui_folder}/x-ui cert -webCert "$webCertFile" -webCertKey "$webKeyFile"
  1525. LOGI "Panel paths set for domain: $CF_Domain"
  1526. LOGI " - Certificate File: $webCertFile"
  1527. LOGI " - Private Key File: $webKeyFile"
  1528. echo -e "${green}Access URL: https://${CF_Domain}:${existing_port}${existing_webBasePath}${plain}"
  1529. restart
  1530. else
  1531. LOGE "Error: Certificate or private key file not found for domain: $CF_Domain."
  1532. fi
  1533. else
  1534. LOGI "Skipping panel path setting."
  1535. fi
  1536. else
  1537. show_menu
  1538. fi
  1539. }
  1540. run_speedtest() {
  1541. # Check if Speedtest is already installed
  1542. if ! command -v speedtest &> /dev/null; then
  1543. # If not installed, determine installation method
  1544. if command -v snap &> /dev/null; then
  1545. # Use snap to install Speedtest
  1546. echo "Installing Speedtest using snap..."
  1547. snap install speedtest
  1548. else
  1549. # Fallback to using package managers
  1550. local pkg_manager=""
  1551. local speedtest_install_script=""
  1552. if command -v dnf &> /dev/null; then
  1553. pkg_manager="dnf"
  1554. speedtest_install_script="https://packagecloud.io/install/repositories/ookla/speedtest-cli/script.rpm.sh"
  1555. elif command -v yum &> /dev/null; then
  1556. pkg_manager="yum"
  1557. speedtest_install_script="https://packagecloud.io/install/repositories/ookla/speedtest-cli/script.rpm.sh"
  1558. elif command -v apt-get &> /dev/null; then
  1559. pkg_manager="apt-get"
  1560. speedtest_install_script="https://packagecloud.io/install/repositories/ookla/speedtest-cli/script.deb.sh"
  1561. elif command -v apt &> /dev/null; then
  1562. pkg_manager="apt"
  1563. speedtest_install_script="https://packagecloud.io/install/repositories/ookla/speedtest-cli/script.deb.sh"
  1564. fi
  1565. if [[ -z $pkg_manager ]]; then
  1566. echo "Error: Package manager not found. You may need to install Speedtest manually."
  1567. return 1
  1568. else
  1569. echo "Installing Speedtest using $pkg_manager..."
  1570. curl -s $speedtest_install_script | bash
  1571. $pkg_manager install -y speedtest
  1572. fi
  1573. fi
  1574. fi
  1575. speedtest
  1576. }
  1577. ip_validation() {
  1578. 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]))$"
  1579. 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)$"
  1580. }
  1581. iplimit_main() {
  1582. echo -e "\n${green}\t1.${plain} Install Fail2ban and configure IP Limit"
  1583. echo -e "${green}\t2.${plain} Change Ban Duration"
  1584. echo -e "${green}\t3.${plain} Unban Everyone"
  1585. echo -e "${green}\t4.${plain} Ban Logs"
  1586. echo -e "${green}\t5.${plain} Ban an IP Address"
  1587. echo -e "${green}\t6.${plain} Unban an IP Address"
  1588. echo -e "${green}\t7.${plain} Real-Time Logs"
  1589. echo -e "${green}\t8.${plain} Service Status"
  1590. echo -e "${green}\t9.${plain} Service Restart"
  1591. echo -e "${green}\t10.${plain} Uninstall Fail2ban and IP Limit"
  1592. echo -e "${green}\t0.${plain} Back to Main Menu"
  1593. read -rp "Choose an option: " choice
  1594. case "$choice" in
  1595. 0)
  1596. show_menu
  1597. ;;
  1598. 1)
  1599. confirm "Proceed with installation of Fail2ban & IP Limit?" "y"
  1600. if [[ $? == 0 ]]; then
  1601. install_iplimit
  1602. else
  1603. iplimit_main
  1604. fi
  1605. ;;
  1606. 2)
  1607. read -rp "Please enter new Ban Duration in Minutes [default 30]: " NUM
  1608. if [[ $NUM =~ ^[0-9]+$ ]]; then
  1609. create_iplimit_jails ${NUM}
  1610. if [[ $release == "alpine" ]]; then
  1611. rc-service fail2ban restart
  1612. else
  1613. systemctl restart fail2ban
  1614. fi
  1615. else
  1616. echo -e "${red}${NUM} is not a number! Please, try again.${plain}"
  1617. fi
  1618. iplimit_main
  1619. ;;
  1620. 3)
  1621. confirm "Proceed with Unbanning everyone from IP Limit jail?" "y"
  1622. if [[ $? == 0 ]]; then
  1623. fail2ban-client reload --restart --unban 3x-ipl
  1624. truncate -s 0 "${iplimit_banned_log_path}"
  1625. echo -e "${green}All users Unbanned successfully.${plain}"
  1626. iplimit_main
  1627. else
  1628. echo -e "${yellow}Cancelled.${plain}"
  1629. fi
  1630. iplimit_main
  1631. ;;
  1632. 4)
  1633. show_banlog
  1634. iplimit_main
  1635. ;;
  1636. 5)
  1637. read -rp "Enter the IP address you want to ban: " ban_ip
  1638. ip_validation
  1639. if [[ $ban_ip =~ $ipv4_regex || $ban_ip =~ $ipv6_regex ]]; then
  1640. fail2ban-client set 3x-ipl banip "$ban_ip"
  1641. echo -e "${green}IP Address ${ban_ip} has been banned successfully.${plain}"
  1642. else
  1643. echo -e "${red}Invalid IP address format! Please try again.${plain}"
  1644. fi
  1645. iplimit_main
  1646. ;;
  1647. 6)
  1648. read -rp "Enter the IP address you want to unban: " unban_ip
  1649. ip_validation
  1650. if [[ $unban_ip =~ $ipv4_regex || $unban_ip =~ $ipv6_regex ]]; then
  1651. fail2ban-client set 3x-ipl unbanip "$unban_ip"
  1652. echo -e "${green}IP Address ${unban_ip} has been unbanned successfully.${plain}"
  1653. else
  1654. echo -e "${red}Invalid IP address format! Please try again.${plain}"
  1655. fi
  1656. iplimit_main
  1657. ;;
  1658. 7)
  1659. tail -f /var/log/fail2ban.log
  1660. iplimit_main
  1661. ;;
  1662. 8)
  1663. service fail2ban status
  1664. iplimit_main
  1665. ;;
  1666. 9)
  1667. if [[ $release == "alpine" ]]; then
  1668. rc-service fail2ban restart
  1669. else
  1670. systemctl restart fail2ban
  1671. fi
  1672. iplimit_main
  1673. ;;
  1674. 10)
  1675. remove_iplimit
  1676. iplimit_main
  1677. ;;
  1678. *)
  1679. echo -e "${red}Invalid option. Please select a valid number.${plain}\n"
  1680. iplimit_main
  1681. ;;
  1682. esac
  1683. }
  1684. install_iplimit() {
  1685. if ! command -v fail2ban-client &> /dev/null; then
  1686. echo -e "${green}Fail2ban is not installed. Installing now...!${plain}\n"
  1687. # Install fail2ban together with nftables. Recent fail2ban packages
  1688. # default to `banaction = nftables-multiport` in /etc/fail2ban/jail.conf,
  1689. # but the `nftables` package isn't pulled in as a dependency on most
  1690. # minimal server images (Debian 12+, Ubuntu 24+, fresh RHEL-family).
  1691. # Without `nft` in PATH the default sshd jail fails to ban with
  1692. # stderr: '/bin/sh: 1: nft: not found'
  1693. # even though our own 3x-ipl jail uses iptables. Bundling the binary
  1694. # at install time prevents that confusing log spam for new installs.
  1695. case "${release}" in
  1696. ubuntu)
  1697. apt-get update
  1698. if [[ "${os_version}" -ge 24 ]]; then
  1699. apt-get install python3-pip -y
  1700. python3 -m pip install pyasynchat --break-system-packages
  1701. fi
  1702. apt-get install fail2ban nftables -y
  1703. ;;
  1704. debian)
  1705. apt-get update
  1706. if [ "$os_version" -ge 12 ]; then
  1707. apt-get install -y python3-systemd
  1708. fi
  1709. apt-get install -y fail2ban nftables
  1710. ;;
  1711. armbian)
  1712. apt-get update && apt-get install fail2ban nftables -y
  1713. ;;
  1714. fedora | amzn | virtuozzo | rhel | almalinux | rocky | ol)
  1715. dnf -y update && dnf -y install fail2ban nftables
  1716. ;;
  1717. centos)
  1718. if [[ "${VERSION_ID}" =~ ^7 ]]; then
  1719. yum update -y && yum install epel-release -y
  1720. yum -y install fail2ban nftables
  1721. else
  1722. dnf -y update && dnf -y install fail2ban nftables
  1723. fi
  1724. ;;
  1725. arch | manjaro | parch)
  1726. pacman -Syu --noconfirm fail2ban nftables
  1727. ;;
  1728. alpine)
  1729. apk add fail2ban nftables
  1730. ;;
  1731. *)
  1732. echo -e "${red}Unsupported operating system. Please check the script and install the necessary packages manually.${plain}\n"
  1733. exit 1
  1734. ;;
  1735. esac
  1736. if ! command -v fail2ban-client &> /dev/null; then
  1737. echo -e "${red}Fail2ban installation failed.${plain}\n"
  1738. exit 1
  1739. fi
  1740. echo -e "${green}Fail2ban installed successfully!${plain}\n"
  1741. else
  1742. echo -e "${yellow}Fail2ban is already installed.${plain}\n"
  1743. fi
  1744. echo -e "${green}Configuring IP Limit...${plain}\n"
  1745. # make sure there's no conflict for jail files
  1746. iplimit_remove_conflicts
  1747. # Check if log file exists
  1748. if ! test -f "${iplimit_banned_log_path}"; then
  1749. touch ${iplimit_banned_log_path}
  1750. fi
  1751. # Check if service log file exists so fail2ban won't return error
  1752. if ! test -f "${iplimit_log_path}"; then
  1753. touch ${iplimit_log_path}
  1754. fi
  1755. # Create the iplimit jail files
  1756. # we didn't pass the bantime here to use the default value
  1757. create_iplimit_jails
  1758. # Launching fail2ban
  1759. if [[ $release == "alpine" ]]; then
  1760. if [[ $(rc-service fail2ban status | grep -F 'status: started' -c) == 0 ]]; then
  1761. rc-service fail2ban start
  1762. else
  1763. rc-service fail2ban restart
  1764. fi
  1765. rc-update add fail2ban
  1766. else
  1767. if ! systemctl is-active --quiet fail2ban; then
  1768. systemctl start fail2ban
  1769. else
  1770. systemctl restart fail2ban
  1771. fi
  1772. systemctl enable fail2ban
  1773. fi
  1774. echo -e "${green}IP Limit installed and configured successfully!${plain}\n"
  1775. before_show_menu
  1776. }
  1777. remove_iplimit() {
  1778. echo -e "${green}\t1.${plain} Only remove IP Limit configurations"
  1779. echo -e "${green}\t2.${plain} Uninstall Fail2ban and IP Limit"
  1780. echo -e "${green}\t0.${plain} Back to Main Menu"
  1781. read -rp "Choose an option: " num
  1782. case "$num" in
  1783. 1)
  1784. rm -f /etc/fail2ban/filter.d/3x-ipl.conf
  1785. rm -f /etc/fail2ban/action.d/3x-ipl.conf
  1786. rm -f /etc/fail2ban/jail.d/3x-ipl.conf
  1787. if [[ $release == "alpine" ]]; then
  1788. rc-service fail2ban restart
  1789. else
  1790. systemctl restart fail2ban
  1791. fi
  1792. echo -e "${green}IP Limit removed successfully!${plain}\n"
  1793. before_show_menu
  1794. ;;
  1795. 2)
  1796. rm -rf /etc/fail2ban
  1797. if [[ $release == "alpine" ]]; then
  1798. rc-service fail2ban stop
  1799. else
  1800. systemctl stop fail2ban
  1801. fi
  1802. case "${release}" in
  1803. ubuntu | debian | armbian)
  1804. apt-get remove -y fail2ban
  1805. apt-get purge -y fail2ban -y
  1806. apt-get autoremove -y
  1807. ;;
  1808. fedora | amzn | virtuozzo | rhel | almalinux | rocky | ol)
  1809. dnf remove fail2ban -y
  1810. dnf autoremove -y
  1811. ;;
  1812. centos)
  1813. if [[ "${VERSION_ID}" =~ ^7 ]]; then
  1814. yum remove fail2ban -y
  1815. yum autoremove -y
  1816. else
  1817. dnf remove fail2ban -y
  1818. dnf autoremove -y
  1819. fi
  1820. ;;
  1821. arch | manjaro | parch)
  1822. pacman -Rns --noconfirm fail2ban
  1823. ;;
  1824. alpine)
  1825. apk del fail2ban
  1826. ;;
  1827. *)
  1828. echo -e "${red}Unsupported operating system. Please uninstall Fail2ban manually.${plain}\n"
  1829. exit 1
  1830. ;;
  1831. esac
  1832. echo -e "${green}Fail2ban and IP Limit removed successfully!${plain}\n"
  1833. before_show_menu
  1834. ;;
  1835. 0)
  1836. show_menu
  1837. ;;
  1838. *)
  1839. echo -e "${red}Invalid option. Please select a valid number.${plain}\n"
  1840. remove_iplimit
  1841. ;;
  1842. esac
  1843. }
  1844. show_banlog() {
  1845. local system_log="/var/log/fail2ban.log"
  1846. echo -e "${green}Checking ban logs...${plain}\n"
  1847. if [[ $release == "alpine" ]]; then
  1848. if [[ $(rc-service fail2ban status | grep -F 'status: started' -c) == 0 ]]; then
  1849. echo -e "${red}Fail2ban service is not running!${plain}\n"
  1850. return 1
  1851. fi
  1852. else
  1853. if ! systemctl is-active --quiet fail2ban; then
  1854. echo -e "${red}Fail2ban service is not running!${plain}\n"
  1855. return 1
  1856. fi
  1857. fi
  1858. if [[ -f "$system_log" ]]; then
  1859. echo -e "${green}Recent system ban activities from fail2ban.log:${plain}"
  1860. grep "3x-ipl" "$system_log" | grep -E "Ban|Unban" | tail -n 10 || echo -e "${yellow}No recent system ban activities found${plain}"
  1861. echo ""
  1862. fi
  1863. if [[ -f "${iplimit_banned_log_path}" ]]; then
  1864. echo -e "${green}3X-IPL ban log entries:${plain}"
  1865. if [[ -s "${iplimit_banned_log_path}" ]]; then
  1866. grep -v "INIT" "${iplimit_banned_log_path}" | tail -n 10 || echo -e "${yellow}No ban entries found${plain}"
  1867. else
  1868. echo -e "${yellow}Ban log file is empty${plain}"
  1869. fi
  1870. else
  1871. echo -e "${red}Ban log file not found at: ${iplimit_banned_log_path}${plain}"
  1872. fi
  1873. echo -e "\n${green}Current jail status:${plain}"
  1874. fail2ban-client status 3x-ipl || echo -e "${yellow}Unable to get jail status${plain}"
  1875. }
  1876. create_iplimit_jails() {
  1877. # Use default bantime if not passed => 30 minutes
  1878. local bantime="${1:-30}"
  1879. # Uncomment 'allowipv6 = auto' in fail2ban.conf
  1880. sed -i 's/#allowipv6 = auto/allowipv6 = auto/g' /etc/fail2ban/fail2ban.conf
  1881. # On Debian 12+ fail2ban's default backend should be changed to systemd
  1882. if [[ "${release}" == "debian" && ${os_version} -ge 12 ]]; then
  1883. sed -i '0,/action =/s/backend = auto/backend = systemd/' /etc/fail2ban/jail.conf
  1884. fi
  1885. cat << EOF > /etc/fail2ban/jail.d/3x-ipl.conf
  1886. [3x-ipl]
  1887. enabled=true
  1888. backend=auto
  1889. filter=3x-ipl
  1890. action=3x-ipl
  1891. logpath=${iplimit_log_path}
  1892. maxretry=1
  1893. findtime=32
  1894. bantime=${bantime}m
  1895. EOF
  1896. cat << EOF > /etc/fail2ban/filter.d/3x-ipl.conf
  1897. [Definition]
  1898. datepattern = ^%Y/%m/%d %H:%M:%S
  1899. failregex = \[LIMIT_IP\]\s*Email\s*=\s*<F-USER>.+</F-USER>\s*\|\|\s*Disconnecting OLD IP\s*=\s*<ADDR>\s*\|\|\s*Timestamp\s*=\s*\d+
  1900. ignoreregex =
  1901. EOF
  1902. cat << EOF > /etc/fail2ban/action.d/3x-ipl.conf
  1903. [INCLUDES]
  1904. before = iptables-allports.conf
  1905. [Definition]
  1906. actionstart = <iptables> -N f2b-<name>
  1907. <iptables> -A f2b-<name> -j <returntype>
  1908. <iptables> -I <chain> -p <protocol> -j f2b-<name>
  1909. actionstop = <iptables> -D <chain> -p <protocol> -j f2b-<name>
  1910. <actionflush>
  1911. <iptables> -X f2b-<name>
  1912. actioncheck = <iptables> -n -L <chain> | grep -q 'f2b-<name>[ \t]'
  1913. actionban = <iptables> -I f2b-<name> 1 -s <ip> -j <blocktype>
  1914. echo "\$(date +"%Y/%m/%d %H:%M:%S") BAN [Email] = <F-USER> [IP] = <ip> banned for <bantime> seconds." >> ${iplimit_banned_log_path}
  1915. actionunban = <iptables> -D f2b-<name> -s <ip> -j <blocktype>
  1916. echo "\$(date +"%Y/%m/%d %H:%M:%S") UNBAN [Email] = <F-USER> [IP] = <ip> unbanned." >> ${iplimit_banned_log_path}
  1917. [Init]
  1918. name = default
  1919. protocol = tcp
  1920. chain = INPUT
  1921. EOF
  1922. echo -e "${green}Ip Limit jail files created with a bantime of ${bantime} minutes.${plain}"
  1923. }
  1924. iplimit_remove_conflicts() {
  1925. local jail_files=(
  1926. /etc/fail2ban/jail.conf
  1927. /etc/fail2ban/jail.local
  1928. )
  1929. for file in "${jail_files[@]}"; do
  1930. # Check for [3x-ipl] config in jail file then remove it
  1931. if test -f "${file}" && grep -qw '3x-ipl' ${file}; then
  1932. sed -i "/\[3x-ipl\]/,/^$/d" ${file}
  1933. echo -e "${yellow}Removing conflicts of [3x-ipl] in jail (${file})!${plain}\n"
  1934. fi
  1935. done
  1936. }
  1937. SSH_port_forwarding() {
  1938. local URL_lists=(
  1939. "https://api4.ipify.org"
  1940. "https://ipv4.icanhazip.com"
  1941. "https://v4.api.ipinfo.io/ip"
  1942. "https://ipv4.myexternalip.com/raw"
  1943. "https://4.ident.me"
  1944. "https://check-host.net/ip"
  1945. )
  1946. local server_ip=""
  1947. for ip_address in "${URL_lists[@]}"; do
  1948. local response=$(curl -s -w "\n%{http_code}" --max-time 3 "${ip_address}" 2> /dev/null)
  1949. local http_code=$(echo "$response" | tail -n1)
  1950. local ip_result=$(echo "$response" | head -n-1 | tr -d '[:space:]"')
  1951. if [[ "${http_code}" == "200" && "${ip_result}" =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
  1952. server_ip="${ip_result}"
  1953. break
  1954. fi
  1955. done
  1956. if [[ -z "$server_ip" ]]; then
  1957. echo -e "${yellow}Could not auto-detect server IP from any provider.${plain}"
  1958. while [[ -z "$server_ip" ]]; do
  1959. read -rp "Please enter your server's public IPv4 address: " server_ip
  1960. server_ip="${server_ip// /}"
  1961. if [[ ! "$server_ip" =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
  1962. echo -e "${red}Invalid IPv4 address. Please try again.${plain}"
  1963. server_ip=""
  1964. fi
  1965. done
  1966. fi
  1967. local existing_webBasePath=$(${xui_folder}/x-ui setting -show true | grep -Eo 'webBasePath: .+' | awk '{print $2}')
  1968. local existing_port=$(${xui_folder}/x-ui setting -show true | grep -Eo 'port: .+' | awk '{print $2}')
  1969. local existing_listenIP=$(${xui_folder}/x-ui setting -getListen true | grep -Eo 'listenIP: .+' | awk '{print $2}')
  1970. local existing_cert=$(${xui_folder}/x-ui setting -getCert true | grep -Eo 'cert: .+' | awk '{print $2}')
  1971. local existing_key=$(${xui_folder}/x-ui setting -getCert true | grep -Eo 'key: .+' | awk '{print $2}')
  1972. local config_listenIP=""
  1973. local listen_choice=""
  1974. if [[ -n "$existing_cert" && -n "$existing_key" ]]; then
  1975. echo -e "${green}Panel is secure with SSL.${plain}"
  1976. before_show_menu
  1977. fi
  1978. if [[ -z "$existing_cert" && -z "$existing_key" && (-z "$existing_listenIP" || "$existing_listenIP" == "0.0.0.0") ]]; then
  1979. echo -e "\n${red}Warning: No Cert and Key found! The panel is not secure.${plain}"
  1980. echo "Please obtain a certificate or set up SSH port forwarding."
  1981. fi
  1982. if [[ -n "$existing_listenIP" && "$existing_listenIP" != "0.0.0.0" && (-z "$existing_cert" && -z "$existing_key") ]]; then
  1983. echo -e "\n${green}Current SSH Port Forwarding Configuration:${plain}"
  1984. echo -e "Standard SSH command:"
  1985. echo -e "${yellow}ssh -L 2222:${existing_listenIP}:${existing_port} root@${server_ip}${plain}"
  1986. echo -e "\nIf using SSH key:"
  1987. echo -e "${yellow}ssh -i <sshkeypath> -L 2222:${existing_listenIP}:${existing_port} root@${server_ip}${plain}"
  1988. echo -e "\nAfter connecting, access the panel at:"
  1989. echo -e "${yellow}http://localhost:2222${existing_webBasePath}${plain}"
  1990. fi
  1991. echo -e "\nChoose an option:"
  1992. echo -e "${green}1.${plain} Set listen IP"
  1993. echo -e "${green}2.${plain} Clear listen IP"
  1994. echo -e "${green}0.${plain} Back to Main Menu"
  1995. read -rp "Choose an option: " num
  1996. case "$num" in
  1997. 1)
  1998. if [[ -z "$existing_listenIP" || "$existing_listenIP" == "0.0.0.0" ]]; then
  1999. echo -e "\nNo listenIP configured. Choose an option:"
  2000. echo -e "1. Use default IP (127.0.0.1)"
  2001. echo -e "2. Set a custom IP"
  2002. read -rp "Select an option (1 or 2): " listen_choice
  2003. config_listenIP="127.0.0.1"
  2004. [[ "$listen_choice" == "2" ]] && read -rp "Enter custom IP to listen on: " config_listenIP
  2005. ${xui_folder}/x-ui setting -listenIP "${config_listenIP}" > /dev/null 2>&1
  2006. echo -e "${green}listen IP has been set to ${config_listenIP}.${plain}"
  2007. echo -e "\n${green}SSH Port Forwarding Configuration:${plain}"
  2008. echo -e "Standard SSH command:"
  2009. echo -e "${yellow}ssh -L 2222:${config_listenIP}:${existing_port} root@${server_ip}${plain}"
  2010. echo -e "\nIf using SSH key:"
  2011. echo -e "${yellow}ssh -i <sshkeypath> -L 2222:${config_listenIP}:${existing_port} root@${server_ip}${plain}"
  2012. echo -e "\nAfter connecting, access the panel at:"
  2013. echo -e "${yellow}http://localhost:2222${existing_webBasePath}${plain}"
  2014. restart
  2015. else
  2016. config_listenIP="${existing_listenIP}"
  2017. echo -e "${green}Current listen IP is already set to ${config_listenIP}.${plain}"
  2018. fi
  2019. ;;
  2020. 2)
  2021. ${xui_folder}/x-ui setting -listenIP 0.0.0.0 > /dev/null 2>&1
  2022. echo -e "${green}Listen IP has been cleared.${plain}"
  2023. restart
  2024. ;;
  2025. 0)
  2026. show_menu
  2027. ;;
  2028. *)
  2029. echo -e "${red}Invalid option. Please select a valid number.${plain}\n"
  2030. SSH_port_forwarding
  2031. ;;
  2032. esac
  2033. }
  2034. show_usage() {
  2035. echo -e "┌────────────────────────────────────────────────────────────────┐
  2036. │ ${blue}x-ui control menu usages (subcommands):${plain} │
  2037. │ │
  2038. │ ${blue}x-ui${plain} - Admin Management Script │
  2039. │ ${blue}x-ui start${plain} - Start │
  2040. │ ${blue}x-ui stop${plain} - Stop │
  2041. │ ${blue}x-ui restart${plain} - Restart │
  2042. | ${blue}x-ui restart-xray${plain} - Restart Xray │
  2043. │ ${blue}x-ui status${plain} - Current Status │
  2044. │ ${blue}x-ui settings${plain} - Current Settings │
  2045. │ ${blue}x-ui enable${plain} - Enable Autostart on OS Startup │
  2046. │ ${blue}x-ui disable${plain} - Disable Autostart on OS Startup │
  2047. │ ${blue}x-ui log${plain} - Check logs │
  2048. │ ${blue}x-ui banlog${plain} - Check Fail2ban ban logs │
  2049. │ ${blue}x-ui update${plain} - Update │
  2050. │ ${blue}x-ui update-all-geofiles${plain} - Update all geo files │
  2051. │ ${blue}x-ui legacy${plain} - Legacy version │
  2052. │ ${blue}x-ui install${plain} - Install │
  2053. │ ${blue}x-ui uninstall${plain} - Uninstall │
  2054. └────────────────────────────────────────────────────────────────┘"
  2055. }
  2056. show_menu() {
  2057. echo -e "
  2058. ╔────────────────────────────────────────────────╗
  2059. │ ${green}3X-UI Panel Management Script${plain} │
  2060. │ ${green}0.${plain} Exit Script │
  2061. │────────────────────────────────────────────────│
  2062. │ ${green}1.${plain} Install │
  2063. │ ${green}2.${plain} Update │
  2064. │ ${green}3.${plain} Update Menu │
  2065. │ ${green}4.${plain} Legacy Version │
  2066. │ ${green}5.${plain} Uninstall │
  2067. │────────────────────────────────────────────────│
  2068. │ ${green}6.${plain} Reset Username & Password │
  2069. │ ${green}7.${plain} Reset Web Base Path │
  2070. │ ${green}8.${plain} Reset Settings │
  2071. │ ${green}9.${plain} Change Port │
  2072. │ ${green}10.${plain} View Current Settings │
  2073. │────────────────────────────────────────────────│
  2074. │ ${green}11.${plain} Start │
  2075. │ ${green}12.${plain} Stop │
  2076. │ ${green}13.${plain} Restart │
  2077. | ${green}14.${plain} Restart Xray │
  2078. │ ${green}15.${plain} Check Status │
  2079. │ ${green}16.${plain} Logs Management │
  2080. │────────────────────────────────────────────────│
  2081. │ ${green}17.${plain} Enable Autostart │
  2082. │ ${green}18.${plain} Disable Autostart │
  2083. │────────────────────────────────────────────────│
  2084. │ ${green}19.${plain} SSL Certificate Management │
  2085. │ ${green}20.${plain} Cloudflare SSL Certificate │
  2086. │ ${green}21.${plain} IP Limit Management │
  2087. │ ${green}22.${plain} Firewall Management │
  2088. │ ${green}23.${plain} SSH Port Forwarding Management │
  2089. │────────────────────────────────────────────────│
  2090. │ ${green}24.${plain} Enable BBR │
  2091. │ ${green}25.${plain} Update Geo Files │
  2092. │ ${green}26.${plain} Speedtest by Ookla │
  2093. ╚────────────────────────────────────────────────╝
  2094. "
  2095. show_status
  2096. echo && read -rp "Please enter your selection [0-26]: " num
  2097. case "${num}" in
  2098. 0)
  2099. exit 0
  2100. ;;
  2101. 1)
  2102. check_uninstall && install
  2103. ;;
  2104. 2)
  2105. check_install && update
  2106. ;;
  2107. 3)
  2108. check_install && update_menu
  2109. ;;
  2110. 4)
  2111. check_install && legacy_version
  2112. ;;
  2113. 5)
  2114. check_install && uninstall
  2115. ;;
  2116. 6)
  2117. check_install && reset_user
  2118. ;;
  2119. 7)
  2120. check_install && reset_webbasepath
  2121. ;;
  2122. 8)
  2123. check_install && reset_config
  2124. ;;
  2125. 9)
  2126. check_install && set_port
  2127. ;;
  2128. 10)
  2129. check_install && check_config
  2130. ;;
  2131. 11)
  2132. check_install && start
  2133. ;;
  2134. 12)
  2135. check_install && stop
  2136. ;;
  2137. 13)
  2138. check_install && restart
  2139. ;;
  2140. 14)
  2141. check_install && restart_xray
  2142. ;;
  2143. 15)
  2144. check_install && status
  2145. ;;
  2146. 16)
  2147. check_install && show_log
  2148. ;;
  2149. 17)
  2150. check_install && enable
  2151. ;;
  2152. 18)
  2153. check_install && disable
  2154. ;;
  2155. 19)
  2156. ssl_cert_issue_main
  2157. ;;
  2158. 20)
  2159. ssl_cert_issue_CF
  2160. ;;
  2161. 21)
  2162. iplimit_main
  2163. ;;
  2164. 22)
  2165. firewall_menu
  2166. ;;
  2167. 23)
  2168. SSH_port_forwarding
  2169. ;;
  2170. 24)
  2171. bbr_menu
  2172. ;;
  2173. 25)
  2174. update_geo
  2175. ;;
  2176. 26)
  2177. run_speedtest
  2178. ;;
  2179. *)
  2180. LOGE "Please enter the correct number [0-26]"
  2181. ;;
  2182. esac
  2183. }
  2184. if [[ $# > 0 ]]; then
  2185. case $1 in
  2186. "start")
  2187. check_install 0 && start 0
  2188. ;;
  2189. "stop")
  2190. check_install 0 && stop 0
  2191. ;;
  2192. "restart")
  2193. check_install 0 && restart 0
  2194. ;;
  2195. "restart-xray")
  2196. check_install 0 && restart_xray 0
  2197. ;;
  2198. "status")
  2199. check_install 0 && status 0
  2200. ;;
  2201. "settings")
  2202. check_install 0 && check_config 0
  2203. ;;
  2204. "enable")
  2205. check_install 0 && enable 0
  2206. ;;
  2207. "disable")
  2208. check_install 0 && disable 0
  2209. ;;
  2210. "log")
  2211. check_install 0 && show_log 0
  2212. ;;
  2213. "banlog")
  2214. check_install 0 && show_banlog 0
  2215. ;;
  2216. "update")
  2217. check_install 0 && update 0
  2218. ;;
  2219. "legacy")
  2220. check_install 0 && legacy_version 0
  2221. ;;
  2222. "install")
  2223. check_uninstall 0 && install 0
  2224. ;;
  2225. "uninstall")
  2226. check_install 0 && uninstall 0
  2227. ;;
  2228. "update-all-geofiles")
  2229. check_install 0 && update_all_geofiles 0 && restart 0
  2230. ;;
  2231. *) show_usage ;;
  2232. esac
  2233. else
  2234. show_menu
  2235. fi