Skip to content
This repository was archived by the owner on Nov 13, 2024. It is now read-only.

Commit 7f0eb6b

Browse files
committed
update geo & clear xray logs and cron jobs support for En Version
1 parent 51d9ff6 commit 7f0eb6b

File tree

1 file changed

+176
-1
lines changed

1 file changed

+176
-1
lines changed

x-ui_en.sh

+176-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,16 @@ green='\033[0;32m'
55
yellow='\033[0;33m'
66
plain='\033[0m'
77

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+
818
#Add some basic function here
919
function LOGD() {
1020
echo -e "${yellow}[DEG] $* ${plain}"
@@ -614,6 +624,156 @@ ssl_cert_issue_by_cloudflare() {
614624
fi
615625
}
616626

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+
617777
show_usage() {
618778
echo "x-ui control menu usages: "
619779
echo "------------------------------------------"
@@ -628,6 +788,8 @@ show_usage() {
628788
echo -e "x-ui update - Update x-ui "
629789
echo -e "x-ui install - Install x-ui "
630790
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"
631793
echo "------------------------------------------"
632794
}
633795

@@ -656,6 +818,7 @@ show_menu() {
656818
————————————————
657819
${green}15.${plain} enable bbr
658820
${green}16.${plain} issuse certs
821+
${green}17.${plain} x-ui cron jobs
659822
"
660823
show_status
661824
echo && read -p "please input a legal number[0-16],input 7 for checking login info:" num
@@ -712,8 +875,11 @@ show_menu() {
712875
16)
713876
ssl_cert_issue
714877
;;
878+
17)
879+
check_install && cron_jobs
880+
;;
715881
*)
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"
717883
;;
718884
esac
719885
}
@@ -753,6 +919,15 @@ if [[ $# > 0 ]]; then
753919
"uninstall")
754920
check_install 0 && uninstall 0
755921
;;
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+
;;
756931
*) show_usage ;;
757932
esac
758933
else

0 commit comments

Comments
 (0)