Skip to content
This repository has been archived by the owner on Nov 12, 2023. It is now read-only.

Commit

Permalink
add cron job setting for updating geo data and xray log clear
Browse files Browse the repository at this point in the history
  • Loading branch information
FranzKafkaYu committed Nov 22, 2022
1 parent 1b088fb commit fabc6ce
Showing 1 changed file with 167 additions and 4 deletions.
171 changes: 167 additions & 4 deletions x-ui.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@ green='\033[0;32m'
yellow='\033[0;33m'
plain='\033[0m'

#consts for log check and clear,unit:M
declare -r DEFAULT_LOG_FILE_DELETE_TRIGGER=35

# consts for geo update
PATH_FOR_GEO_IP='/usr/local/x-ui/bin/geoip.dat'
PATH_FOR_GEO_SITE='/usr/local/x-ui/bin/geosite.dat'
URL_FOR_GEO_IP='https://github.com/Loyalsoldier/v2ray-rules-dat/releases/latest/download/geoip.dat'
URL_FOR_GEO_SITE='https://github.com/Loyalsoldier/v2ray-rules-dat/releases/latest/download/geosite.dat'

#Add some basic function here
function LOGD() {
echo -e "${yellow}[DEG] $* ${plain}"
Expand Down Expand Up @@ -510,8 +519,8 @@ ssl_cert_issue_standalone() {
fi
#install cert
~/.acme.sh/acme.sh --installcert -d ${domain} --ca-file /root/cert/ca.cer \
--cert-file /root/cert/${domain}.cer --key-file /root/cert/${domain}.key \
--fullchain-file /root/cert/fullchain.cer
--cert-file /root/cert/${domain}.cer --key-file /root/cert/${domain}.key \
--fullchain-file /root/cert/fullchain.cer

if [ $? -ne 0 ]; then
LOGE "证书安装失败,脚本退出"
Expand Down Expand Up @@ -595,8 +604,8 @@ ssl_cert_issue_by_cloudflare() {
LOGI "证书签发成功,安装中..."
fi
~/.acme.sh/acme.sh --installcert -d ${CF_Domain} -d *.${CF_Domain} --ca-file /root/cert/ca.cer \
--cert-file /root/cert/${CF_Domain}.cer --key-file /root/cert/${CF_Domain}.key \
--fullchain-file /root/cert/fullchain.cer
--cert-file /root/cert/${CF_Domain}.cer --key-file /root/cert/${CF_Domain}.key \
--fullchain-file /root/cert/fullchain.cer
if [ $? -ne 0 ]; then
LOGE "证书安装失败,脚本退出"
rm -rf ~/.acme.sh/${CF_Domain}
Expand All @@ -620,6 +629,144 @@ ssl_cert_issue_by_cloudflare() {
fi
}

#add for cron jobs,including sync geo data,check logs and restart x-ui
cron_jobs() {
clear
echo -e "
${green}定时任务管理${plain}
${green}0.${plain} 返回主菜单
${green}1.${plain} 开启定时更新geo
${green}2.${plain} 关闭定时更新geo
${green}3.${plain} 开启定时删除xray日志
${green}4.${plain} 关闭定时删除xray日志
"
echo && read -p "请输入选择 [0-4]: " num
case "${num}" in
0)
show_menu
;;
1)
enable_auto_update_geo
;;
2)
disable_auto_update_geo
;;
3)
enable_auto_clear_log
;;
4)
disable_auto_clear_log
;;
*)
LOGE "请输入正确的数字 [0-4]"
;;
esac
}

#update geo data
update_geo() {
#back up first
mv ${PATH_FOR_GEO_IP} ${PATH_FOR_GEO_IP}.bak
#update data
curl -s -L -o ${PATH_FOR_GEO_IP} ${URL_FOR_GEO_IP}
if [[ $? -ne 0 ]]; then
echo "update geoip.dat failed"
mv ${PATH_FOR_GEO_IP}.bak ${PATH_FOR_GEO_IP}
else
echo "update geoip.dat succeed"
rm -f ${PATH_FOR_GEO_IP}.bak
fi
mv ${PATH_FOR_GEO_SITE} ${PATH_FOR_GEO_SITE}.bak
curl -s -L -o ${PATH_FOR_GEO_SITE} ${PATH_FOR_GEO_SITE}
if [[ $? -ne 0 ]]; then
echo "update geosite.dat failed"
mv ${PATH_FOR_GEO_SITE}.bak ${PATH_FOR_GEO_SITE}
else
echo "update geosite.dat succeed"
rm -f ${PATH_FOR_GEO_SITE}.bak
fi
#restart x-ui
systemctl restart x-ui
}

enable_auto_update_geo() {
LOGI "正在开启自动更新geo数据..."
crontab -l >/tmp/crontabTask.tmp
echo "00 4 */2 * * x-ui geo > /dev/null" >>/tmp/crontabTask.tmp
crontab /tmp/crontabTask.tmp
rm /tmp/crontabTask.tmp
LOGI "开启自动更新geo数据成功"
}

disable_auto_update_geo() {
crontab -l | grep -v "x-ui geo" | crontab -
if [[ $? -ne 0 ]]; then
LOGI "取消x-ui 自动更新geo数据失败"
else
LOGI "取消x-ui 自动更新geo数据成功"
fi
}

#clear xray log,need enable log in config template
#here we need input an absolute path for log
clear_log() {
LOGI "清除xray日志中..."
local filePath=''
if [[ $# -gt 0 ]]; then
filePath=$1
else
LOGE "未输入有效文件路径,脚本退出"
exit 1
fi
LOGI "日志路径为:${filePath}"
if [[ ! -f ${filePath} ]]; then
LOGE "清除xray日志文件失败,${filePath}不存在,请确认"
exit 1
fi
fileSize=$(ls -la ${filePath} --block-size=M | awk '{print $5}' | awk -F 'M' '{print$1}')
if [[ ${fileSize} -gt ${DEFAULT_LOG_FILE_DELETE_TRIGGER} ]]; then
rm $1
if [[ $? -ne 0 ]]; then
LOGE "清除xray日志文件:${filePath}失败"
else
LOGI "清除xray日志文件:${filePath}成功"
systemctl restart x-ui
fi
else
LOGI "当前日志大小为${fileSize}M,小于${DEFAULT_LOG_FILE_DELETE_TRIGGER}M,将不会清除"
fi
}

#enable auto delete log,need file path as
enable_auto_clear_log() {
LOGI "设置定时清除xray日志..."
local filePath=''
read -p "请输入日志文件路径": filePath
if [[ ! -n ${filePath} ]]; then
LOGI "输入的日志文件路径无效,脚本退出"
exit 1
fi
if [[ ! -f ${filePath} ]]; then
LOGE "${filePath}不存在,设置定时清除xray日志失败"
exit 1
fi
crontab -l >/tmp/crontabTask.tmp
echo "30 4 */2 * * x-ui clear ${filePath} > /dev/null" >>/tmp/crontabTask.tmp
crontab /tmp/crontabTask.tmp
rm /tmp/crontabTask.tmp
LOGI "设置定时清除xray日志成功"
}

#disable auto dlete log
disable_auto_clear_log() {
crontab -l | grep -v "x-ui clear" | crontab -
if [[ $? -ne 0 ]]; then
LOGI "取消 定时清除xray日志失败"
else
LOGI "取消 定时清除xray日志成功"
fi
}

show_usage() {
echo "x-ui 管理脚本使用方法: "
echo "------------------------------------------"
Expand All @@ -635,6 +782,9 @@ show_usage() {
echo "x-ui update - 更新 x-ui 面板"
echo "x-ui install - 安装 x-ui 面板"
echo "x-ui uninstall - 卸载 x-ui 面板"
echo "x-ui clear - 清除 x-ui 日志"
echo "x-ui geo - 更新 x-ui geo数据"
echo "x-ui cron - 配置 x-ui 定时任务"
echo "------------------------------------------"
}

Expand Down Expand Up @@ -663,6 +813,7 @@ show_menu() {
————————————————
${green}15.${plain} 一键安装 bbr (最新内核)
${green}16.${plain} 一键申请SSL证书(acme申请)
${green}17.${plain} 配置x-ui定时任务
"
show_status
echo && read -p "请输入选择 [0-16]: " num
Expand Down Expand Up @@ -719,6 +870,9 @@ show_menu() {
16)
ssl_cert_issue
;;
17)
check_install && cron_jobs
;;
*)
LOGE "请输入正确的数字 [0-16]"
;;
Expand Down Expand Up @@ -760,6 +914,15 @@ if [[ $# > 0 ]]; then
"uninstall")
check_install 0 && uninstall 0
;;
"geo")
check_install 0 && update_geo
;;
"clear")
check_install 0 && clear_log $2
;;
"cron")
check_install && cron_jobs
;;
*) show_usage ;;
esac
else
Expand Down

0 comments on commit fabc6ce

Please sign in to comment.