x-ui.sh 78 KB

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