2023-07-01 15:26:43 +03:00
|
|
|
package job
|
|
|
|
|
|
|
|
import (
|
2024-03-05 16:39:20 +03:00
|
|
|
"io"
|
2023-07-01 15:26:43 +03:00
|
|
|
"os"
|
2024-06-24 10:57:20 +03:00
|
|
|
"path/filepath"
|
2024-03-11 00:31:24 +03:00
|
|
|
|
2023-07-01 15:26:43 +03:00
|
|
|
"x-ui/logger"
|
|
|
|
"x-ui/xray"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ClearLogsJob struct{}
|
|
|
|
|
|
|
|
func NewClearLogsJob() *ClearLogsJob {
|
|
|
|
return new(ClearLogsJob)
|
|
|
|
}
|
|
|
|
|
2024-06-24 10:57:20 +03: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 15:26:43 +03: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 13:40:39 +03:00
|
|
|
logFilesPrev := []string{xray.GetIPLimitBannedPrevLogPath(), xray.GetAccessPersistentPrevLogPath()}
|
2024-03-11 00:31:24 +03:00
|
|
|
|
2024-06-24 10:57:20 +03: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 15:26:43 +03:00
|
|
|
for i := 0; i < len(logFiles); i++ {
|
2024-02-10 13:40:39 +03:00
|
|
|
if i > 0 {
|
2024-06-24 10:57:20 +03:00
|
|
|
// Copy to previous logs
|
2024-03-20 13:47:35 +03:00
|
|
|
logFilePrev, err := os.OpenFile(logFilesPrev[i-1], os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644)
|
2024-02-10 13:40:39 +03:00
|
|
|
if err != nil {
|
2024-06-24 10:57:20 +03: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 13:40:39 +03:00
|
|
|
}
|
|
|
|
|
2024-06-24 10:57:20 +03: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 13:40:39 +03:00
|
|
|
}
|
2024-03-11 00:31:24 +03:00
|
|
|
|
2024-03-05 16:39:20 +03:00
|
|
|
logFile.Close()
|
|
|
|
logFilePrev.Close()
|
2024-01-01 18:07:56 +03:00
|
|
|
}
|
|
|
|
|
2024-02-10 13:40:39 +03:00
|
|
|
err := os.Truncate(logFiles[i], 0)
|
2024-01-01 18:07:56 +03:00
|
|
|
if err != nil {
|
2024-06-24 10:57:20 +03:00
|
|
|
logger.Warning("Failed to truncate log file:", logFiles[i], "-", err)
|
2023-07-01 15:26:43 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|