@@ -5,6 +5,16 @@ green='\033[0;32m'
5
5
yellow=' \033[0;33m'
6
6
plain=' \033[0m'
7
7
8
+ # consts for log check and clear,unit:M
9
+ declare -r DEFAULT_LOG_FILE_DELETE_TRIGGER=35
10
+
11
+ # consts for geo update
12
+ PATH_FOR_GEO_IP=' /usr/local/x-ui/bin/geoip.dat'
13
+ PATH_FOR_CONFIG=' /usr/local/x-ui/bin/config.json'
14
+ PATH_FOR_GEO_SITE=' /usr/local/x-ui/bin/geosite.dat'
15
+ URL_FOR_GEO_IP=' https://github.com/Loyalsoldier/v2ray-rules-dat/releases/latest/download/geoip.dat'
16
+ URL_FOR_GEO_SITE=' https://github.com/Loyalsoldier/v2ray-rules-dat/releases/latest/download/geosite.dat'
17
+
8
18
# Add some basic function here
9
19
function LOGD() {
10
20
echo -e " ${yellow} [DEG] $* ${plain} "
@@ -614,6 +624,156 @@ ssl_cert_issue_by_cloudflare() {
614
624
fi
615
625
}
616
626
627
+ # add for cron jobs,including sync geo data,check logs and restart x-ui
628
+ cron_jobs () {
629
+ clear
630
+ echo -e "
631
+ ${green} x-ui cron jobs${plain}
632
+ ${green} 0.${plain} return main menu
633
+ ${green} 1.${plain} enable automatically update geo data
634
+ ${green} 2.${plain} disable automatically update geo data
635
+ ${green} 3.${plain} enable automatically clear xray log
636
+ ${green} 4.${plain} disable automatically clear xray log
637
+ "
638
+ echo && read -p " plz input your choice [0-4]: " num
639
+ case " ${num} " in
640
+ 0)
641
+ show_menu
642
+ ;;
643
+ 1)
644
+ enable_auto_update_geo
645
+ ;;
646
+ 2)
647
+ disable_auto_update_geo
648
+ ;;
649
+ 3)
650
+ enable_auto_clear_log
651
+ ;;
652
+ 4)
653
+ disable_auto_clear_log
654
+ ;;
655
+ * )
656
+ LOGE " plz input a valid choice [0-4]"
657
+ ;;
658
+ esac
659
+ }
660
+
661
+ # update geo data
662
+ update_geo () {
663
+ # back up first
664
+ mv ${PATH_FOR_GEO_IP} ${PATH_FOR_GEO_IP} .bak
665
+ # update data
666
+ curl -s -L -o ${PATH_FOR_GEO_IP} ${URL_FOR_GEO_IP}
667
+ if [[ $? -ne 0 ]]; then
668
+ echo " update geoip.dat failed"
669
+ mv ${PATH_FOR_GEO_IP} .bak ${PATH_FOR_GEO_IP}
670
+ else
671
+ echo " update geoip.dat succeed"
672
+ rm -f ${PATH_FOR_GEO_IP} .bak
673
+ fi
674
+ mv ${PATH_FOR_GEO_SITE} ${PATH_FOR_GEO_SITE} .bak
675
+ curl -s -L -o ${PATH_FOR_GEO_SITE} ${URL_FOR_GEO_SITE}
676
+ if [[ $? -ne 0 ]]; then
677
+ echo " update geosite.dat failed"
678
+ mv ${PATH_FOR_GEO_SITE} .bak ${PATH_FOR_GEO_SITE}
679
+ else
680
+ echo " update geosite.dat succeed"
681
+ rm -f ${PATH_FOR_GEO_SITE} .bak
682
+ fi
683
+ # restart x-ui
684
+ systemctl restart x-ui
685
+ }
686
+
687
+ enable_auto_update_geo () {
688
+ LOGI " enable automatically update geo data..."
689
+ crontab -l > /tmp/crontabTask.tmp
690
+ echo " 00 4 */2 * * x-ui geo > /dev/null" >> /tmp/crontabTask.tmp
691
+ crontab /tmp/crontabTask.tmp
692
+ rm /tmp/crontabTask.tmp
693
+ LOGI " enable automatically update geo data succeed"
694
+ }
695
+
696
+ disable_auto_update_geo () {
697
+ crontab -l | grep -v " x-ui geo" | crontab -
698
+ if [[ $? -ne 0 ]]; then
699
+ LOGI " cancel x-ui automatically update geo data failed"
700
+ else
701
+ LOGI " cancel x-ui automatically update geo data succeed"
702
+ fi
703
+ }
704
+
705
+ # clear xray log,need enable log in config template
706
+ # here we need input an absolute path for log
707
+ clear_log () {
708
+ LOGI " clear xray logs..."
709
+ local filePath=' '
710
+ if [[ $# -gt 0 ]]; then
711
+ filePath=$1
712
+ else
713
+ LOGE " invalid file path,will exit"
714
+ exit 1
715
+ fi
716
+ LOGI " log file:${filePath} "
717
+ if [[ ! -f ${filePath} ]]; then
718
+ LOGE " clear xray log failed,${filePath} didn't exist,plz check it"
719
+ exit 1
720
+ fi
721
+ fileSize=$( ls -la ${filePath} --block-size=M | awk ' {print $5}' | awk -F ' M' ' {print$1}' )
722
+ if [[ ${fileSize} -gt ${DEFAULT_LOG_FILE_DELETE_TRIGGER} ]]; then
723
+ rm $1
724
+ if [[ $? -ne 0 ]]; then
725
+ LOGE " clear xray log :${filePath} failed"
726
+ else
727
+ LOGI " clear xray log :${filePath} succeed"
728
+ systemctl restart x-ui
729
+ fi
730
+ else
731
+ LOGI " current size of xray log is:${fileSize} M,smaller that ${DEFAULT_LOG_FILE_DELETE_TRIGGER} M,won't clear"
732
+ fi
733
+ }
734
+
735
+ # enable auto delete log,need file path as
736
+ enable_auto_clear_log () {
737
+ LOGI " enable automatically clear xray logs..."
738
+ local accessfilePath=' '
739
+ local errorfilePath=' '
740
+ accessfilePath=$( cat ${PATH_FOR_CONFIG} | jq .log.access | tr -d ' "' )
741
+ errorfilePath=$( cat ${PATH_FOR_CONFIG} | jq .log.error | tr -d ' "' )
742
+ if [[ ! -n ${accessfilePath} && ! -n ${errorfilePath} ]]; then
743
+ LOGI " current configuration didn't set valid logs,will exited"
744
+ exit 1
745
+ fi
746
+ if [[ -f ${accessfilePath} ]]; then
747
+ crontab -l > /tmp/crontabTask.tmp
748
+ echo " 30 4 */2 * * x-ui clear ${accessfilePath} > /dev/null" >> /tmp/crontabTask.tmp
749
+ crontab /tmp/crontabTask.tmp
750
+ rm /tmp/crontabTask.tmp
751
+ LOGI " enable automatically clear xray log:${accessfilePath} succeed"
752
+ else
753
+ LOGE " accesslog didn't existed,won't automatically clear it"
754
+ fi
755
+
756
+ if [[ -f ${errorfilePath} ]]; then
757
+ crontab -l > /tmp/crontabTask.tmp
758
+ echo " 30 4 */2 * * x-ui clear ${errorfilePath} > /dev/null" >> /tmp/crontabTask.tmp
759
+ crontab /tmp/crontabTask.tmp
760
+ rm /tmp/crontabTask.tmp
761
+ LOGI " enable automatically clear xray log:${errorfilePath} succeed"
762
+ else
763
+ LOGE " errorlog didn't existed,won't automatically clear it"
764
+ fi
765
+ }
766
+
767
+ # disable auto dlete log
768
+ disable_auto_clear_log () {
769
+ crontab -l | grep -v " x-ui clear" | crontab -
770
+ if [[ $? -ne 0 ]]; then
771
+ LOGI " cancel automatically clear xray logs failed"
772
+ else
773
+ LOGI " cancel automatically clear xray logs succeed"
774
+ fi
775
+ }
776
+
617
777
show_usage () {
618
778
echo " x-ui control menu usages: "
619
779
echo " ------------------------------------------"
@@ -628,6 +788,8 @@ show_usage() {
628
788
echo -e " x-ui update - Update x-ui "
629
789
echo -e " x-ui install - Install x-ui "
630
790
echo -e " x-ui uninstall - Uninstall x-ui "
791
+ echo " x-ui geo - Update x-ui geo "
792
+ echo " x-ui cron - Cron x-ui jobs"
631
793
echo " ------------------------------------------"
632
794
}
633
795
@@ -656,6 +818,7 @@ show_menu() {
656
818
————————————————
657
819
${green} 15.${plain} enable bbr
658
820
${green} 16.${plain} issuse certs
821
+ ${green} 17.${plain} x-ui cron jobs
659
822
"
660
823
show_status
661
824
echo && read -p " please input a legal number[0-16],input 7 for checking login info:" num
@@ -712,8 +875,11 @@ show_menu() {
712
875
16)
713
876
ssl_cert_issue
714
877
;;
878
+ 17)
879
+ check_install && cron_jobs
880
+ ;;
715
881
* )
716
- LOGE " please input a legal number[0-16 ],input 7 for checking login info"
882
+ LOGE " please input a legal number[0-17 ],input 7 for checking login info"
717
883
;;
718
884
esac
719
885
}
@@ -753,6 +919,15 @@ if [[ $# > 0 ]]; then
753
919
" uninstall" )
754
920
check_install 0 && uninstall 0
755
921
;;
922
+ " geo" )
923
+ check_install 0 && update_geo
924
+ ;;
925
+ " clear" )
926
+ check_install 0 && clear_log $2
927
+ ;;
928
+ " cron" )
929
+ check_install && cron_jobs
930
+ ;;
756
931
* ) show_usage ;;
757
932
esac
758
933
else
0 commit comments