From 605895321c9640301cdde61fd7b2629ef1090c53 Mon Sep 17 00:00:00 2001 From: Hamidreza Ghavami <70919649+hamid-gh98@users.noreply.github.com> Date: Tue, 27 Jun 2023 19:54:46 +0430 Subject: [PATCH] add cronjob for clear logs files --- web/job/clear_logs_job.go | 25 +++++++++++++++++++++++++ web/web.go | 3 +++ xray/process.go | 2 +- 3 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 web/job/clear_logs_job.go diff --git a/web/job/clear_logs_job.go b/web/job/clear_logs_job.go new file mode 100644 index 00000000..34f13eaa --- /dev/null +++ b/web/job/clear_logs_job.go @@ -0,0 +1,25 @@ +package job + +import ( + "os" + "x-ui/logger" + "x-ui/xray" +) + +type ClearLogsJob struct{} + +func NewClearLogsJob() *ClearLogsJob { + return new(ClearLogsJob) +} + +// Here Run is an interface method of the Job interface +func (j *ClearLogsJob) Run() { + logFiles := []string{xray.GetIPLimitLogPath(), xray.GetIPLimitBannedLogPath(), xray.GetAccessPersistentLogPath()} + + // clear log files + for i := 0; i < len(logFiles); i++ { + if err := os.Truncate(logFiles[i], 0); err != nil { + logger.Warning("clear logs job err:", err) + } + } +} diff --git a/web/web.go b/web/web.go index b78ac2f0..14e06f67 100644 --- a/web/web.go +++ b/web/web.go @@ -253,6 +253,9 @@ func (s *Server) startTask() { // check client ips from log file every 20 sec s.cron.AddJob("@every 20s", job.NewCheckClientIpJob()) + // check client ips from log file every 3 day + s.cron.AddJob("@every 3d", job.NewClearLogsJob()) + // Make a traffic condition every day, 8:30 var entry cron.EntryID isTgbotenabled, err := s.settingService.GetTgbotenabled() diff --git a/xray/process.go b/xray/process.go index cc76e78a..0882c716 100644 --- a/xray/process.go +++ b/xray/process.go @@ -57,7 +57,7 @@ func GetIPLimitBannedLogPath() string { } func GetAccessPersistentLogPath() string { - return config.GetLogFolder() + "/3xui-access-persistent.log" + return config.GetLogFolder() + "/3xipl-access-persistent.log" } func GetAccessLogPath() string {