| 
									
										
										
										
											2023-07-01 12:26:43 +00:00
										 |  |  | package job | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2024-03-05 13:39:20 +00:00
										 |  |  | 	"io" | 
					
						
							| 
									
										
										
										
											2023-07-01 12:26:43 +00:00
										 |  |  | 	"os" | 
					
						
							| 
									
										
										
										
											2024-06-24 07:57:20 +00:00
										 |  |  | 	"path/filepath" | 
					
						
							| 
									
										
										
										
											2024-03-10 21:31:24 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-01 12:26:43 +00:00
										 |  |  | 	"x-ui/logger" | 
					
						
							|  |  |  | 	"x-ui/xray" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type ClearLogsJob struct{} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func NewClearLogsJob() *ClearLogsJob { | 
					
						
							|  |  |  | 	return new(ClearLogsJob) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-24 07:57:20 +00:00
										 |  |  | // ensureFileExists creates the necessary directories and file if they don't exist
 | 
					
						
							|  |  |  | func ensureFileExists(path string) error { | 
					
						
							|  |  |  | 	dir := filepath.Dir(path) | 
					
						
							|  |  |  | 	if err := os.MkdirAll(dir, 0755); err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	file, err := os.OpenFile(path, os.O_CREATE|os.O_RDWR, 0644) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	file.Close() | 
					
						
							|  |  |  | 	return nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-01 12:26:43 +00:00
										 |  |  | // Here Run is an interface method of the Job interface
 | 
					
						
							|  |  |  | func (j *ClearLogsJob) Run() { | 
					
						
							|  |  |  | 	logFiles := []string{xray.GetIPLimitLogPath(), xray.GetIPLimitBannedLogPath(), xray.GetAccessPersistentLogPath()} | 
					
						
							| 
									
										
										
										
											2024-02-10 10:40:39 +00:00
										 |  |  | 	logFilesPrev := []string{xray.GetIPLimitBannedPrevLogPath(), xray.GetAccessPersistentPrevLogPath()} | 
					
						
							| 
									
										
										
										
											2024-03-10 21:31:24 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-24 07:57:20 +00:00
										 |  |  | 	// Ensure all log files and their paths exist
 | 
					
						
							|  |  |  | 	for _, path := range append(logFiles, logFilesPrev...) { | 
					
						
							|  |  |  | 		if err := ensureFileExists(path); err != nil { | 
					
						
							|  |  |  | 			logger.Warning("Failed to ensure log file exists:", path, "-", err) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Clear log files and copy to previous logs
 | 
					
						
							| 
									
										
										
										
											2023-07-01 12:26:43 +00:00
										 |  |  | 	for i := 0; i < len(logFiles); i++ { | 
					
						
							| 
									
										
										
										
											2024-02-10 10:40:39 +00:00
										 |  |  | 		if i > 0 { | 
					
						
							| 
									
										
										
										
											2024-06-24 07:57:20 +00:00
										 |  |  | 			// Copy to previous logs
 | 
					
						
							| 
									
										
										
										
											2024-03-20 10:47:35 +00:00
										 |  |  | 			logFilePrev, err := os.OpenFile(logFilesPrev[i-1], os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644) | 
					
						
							| 
									
										
										
										
											2024-02-10 10:40:39 +00:00
										 |  |  | 			if err != nil { | 
					
						
							| 
									
										
										
										
											2024-06-24 07:57:20 +00:00
										 |  |  | 				logger.Warning("Failed to open previous log file for writing:", logFilesPrev[i-1], "-", err) | 
					
						
							|  |  |  | 				continue | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			logFile, err := os.OpenFile(logFiles[i], os.O_RDONLY, 0644) | 
					
						
							|  |  |  | 			if err != nil { | 
					
						
							|  |  |  | 				logger.Warning("Failed to open current log file for reading:", logFiles[i], "-", err) | 
					
						
							|  |  |  | 				logFilePrev.Close() | 
					
						
							|  |  |  | 				continue | 
					
						
							| 
									
										
										
										
											2024-02-10 10:40:39 +00:00
										 |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-24 07:57:20 +00:00
										 |  |  | 			_, err = io.Copy(logFilePrev, logFile) | 
					
						
							|  |  |  | 			if err != nil { | 
					
						
							|  |  |  | 				logger.Warning("Failed to copy log file:", logFiles[i], "to", logFilesPrev[i-1], "-", err) | 
					
						
							| 
									
										
										
										
											2024-02-10 10:40:39 +00:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2024-03-10 21:31:24 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-05 13:39:20 +00:00
										 |  |  | 			logFile.Close() | 
					
						
							|  |  |  | 			logFilePrev.Close() | 
					
						
							| 
									
										
										
										
											2024-01-01 15:07:56 +00:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-10 10:40:39 +00:00
										 |  |  | 		err := os.Truncate(logFiles[i], 0) | 
					
						
							| 
									
										
										
										
											2024-01-01 15:07:56 +00:00
										 |  |  | 		if err != nil { | 
					
						
							| 
									
										
										
										
											2024-06-24 07:57:20 +00:00
										 |  |  | 			logger.Warning("Failed to truncate log file:", logFiles[i], "-", err) | 
					
						
							| 
									
										
										
										
											2023-07-01 12:26:43 +00:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } |