x-ui.sh 80 KB

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