diff options
Diffstat (limited to 'web/job')
| -rw-r--r-- | web/job/check_client_ip_job.go | 3 | ||||
| -rw-r--r-- | web/job/check_xray_running_job.go | 21 | ||||
| -rw-r--r-- | web/job/clear_logs_job.go | 31 |
3 files changed, 46 insertions, 9 deletions
diff --git a/web/job/check_client_ip_job.go b/web/job/check_client_ip_job.go index b15473c5..65257170 100644 --- a/web/job/check_client_ip_job.go +++ b/web/job/check_client_ip_job.go @@ -22,8 +22,11 @@ var job *CheckClientIpJob var disAllowedIps []string var ipFiles = []string{ xray.GetIPLimitLogPath(), +xray.GetIPLimitPrevLogPath(), xray.GetIPLimitBannedLogPath(), +xray.GetIPLimitBannedPrevLogPath(), xray.GetAccessPersistentLogPath(), +xray.GetAccessPersistentPrevLogPath(), } func NewCheckClientIpJob() *CheckClientIpJob { diff --git a/web/job/check_xray_running_job.go b/web/job/check_xray_running_job.go index f1a848bf..d9ffeb7a 100644 --- a/web/job/check_xray_running_job.go +++ b/web/job/check_xray_running_job.go @@ -1,6 +1,9 @@ package job -import "x-ui/web/service" +import ( + "x-ui/logger" + "x-ui/web/service" +) type CheckXrayRunningJob struct { xrayService service.XrayService @@ -15,11 +18,15 @@ func NewCheckXrayRunningJob() *CheckXrayRunningJob { func (j *CheckXrayRunningJob) Run() { if j.xrayService.IsXrayRunning() { j.checkTime = 0 - return + } else { + j.checkTime++ + //only restart if it's down 2 times in a row + if j.checkTime > 1 { + err := j.xrayService.RestartXray(false) + j.checkTime = 0 + if err != nil { + logger.Error("Restart xray failed:", err) + } + } } - j.checkTime++ - if j.checkTime < 2 { - return - } - j.xrayService.SetToNeedRestart() } diff --git a/web/job/clear_logs_job.go b/web/job/clear_logs_job.go index 34f13eaa..5ceb5a75 100644 --- a/web/job/clear_logs_job.go +++ b/web/job/clear_logs_job.go @@ -15,10 +15,37 @@ func NewClearLogsJob() *ClearLogsJob { // Here Run is an interface method of the Job interface func (j *ClearLogsJob) Run() { logFiles := []string{xray.GetIPLimitLogPath(), xray.GetIPLimitBannedLogPath(), xray.GetAccessPersistentLogPath()} + logFilesPrev := []string{xray.GetIPLimitPrevLogPath(), xray.GetIPLimitBannedPrevLogPath(), xray.GetAccessPersistentPrevLogPath()} - // clear log files + // clear old previous logs + for i := 0; i < len(logFilesPrev); i++ { + if err := os.Truncate(logFilesPrev[i], 0); err != nil { + logger.Warning("clear logs job err:", err) + } + } + + // clear log files and copy to previous logs for i := 0; i < len(logFiles); i++ { - if err := os.Truncate(logFiles[i], 0); err != nil { + + // copy to previous logs + logFilePrev, err := os.OpenFile(logFilesPrev[i], os.O_CREATE|os.O_APPEND|os.O_RDWR, 0644) + if err != nil { + logger.Warning("clear logs job err:", err) + } + + logFile, err := os.ReadFile(logFiles[i]) + if err != nil { + logger.Warning("clear logs job err:", err) + } + + _, err = logFilePrev.Write(logFile) + if err != nil { + logger.Warning("clear logs job err:", err) + } + defer logFilePrev.Close() + + err = os.Truncate(logFiles[i], 0) + if err != nil { logger.Warning("clear logs job err:", err) } } |
