clear_logs_job.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package job
  2. import (
  3. "io"
  4. "os"
  5. "path/filepath"
  6. "github.com/mhsanaei/3x-ui/v3/internal/logger"
  7. "github.com/mhsanaei/3x-ui/v3/internal/xray"
  8. )
  9. const defaultMaxXrayLogBytes int64 = 64 << 20
  10. var maxXrayLogBytes = defaultMaxXrayLogBytes
  11. // ClearLogsJob clears old log files to prevent disk space issues.
  12. type ClearLogsJob struct{}
  13. // PruneXrayLogsJob truncates oversized Xray access and error logs.
  14. // PruneXrayLogsJob truncates the Xray access and error logs once either exceeds maxXrayLogBytes.
  15. type PruneXrayLogsJob struct{}
  16. // NewClearLogsJob creates a new log cleanup job instance.
  17. func NewClearLogsJob() *ClearLogsJob {
  18. return new(ClearLogsJob)
  19. }
  20. // NewPruneXrayLogsJob creates a new Xray log pruning job instance.
  21. func NewPruneXrayLogsJob() *PruneXrayLogsJob {
  22. return new(PruneXrayLogsJob)
  23. }
  24. // ensureFileExists creates the necessary directories and file if they don't exist
  25. func ensureFileExists(path string) error {
  26. dir := filepath.Dir(path)
  27. if err := os.MkdirAll(dir, 0o755); err != nil {
  28. return err
  29. }
  30. file, err := os.OpenFile(path, os.O_CREATE|os.O_RDWR, 0o644)
  31. if err != nil {
  32. return err
  33. }
  34. file.Close()
  35. return nil
  36. }
  37. // Here Run is an interface method of the Job interface
  38. func (j *ClearLogsJob) Run() {
  39. logFiles := []string{xray.GetIPLimitLogPath(), xray.GetIPLimitBannedLogPath()}
  40. logFilesPrev := []string{xray.GetIPLimitBannedPrevLogPath()}
  41. // Ensure all log files and their paths exist
  42. for _, path := range append(logFiles, logFilesPrev...) {
  43. if err := ensureFileExists(path); err != nil {
  44. logger.Warning("Failed to ensure log file exists:", path, "-", err)
  45. }
  46. }
  47. // Clear log files and copy to previous logs
  48. for i := range len(logFiles) {
  49. if i > 0 {
  50. // Copy to previous logs
  51. logFilePrev, err := os.OpenFile(logFilesPrev[i-1], os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0o644)
  52. if err != nil {
  53. logger.Warning("Failed to open previous log file for writing:", logFilesPrev[i-1], "-", err)
  54. continue
  55. }
  56. logFile, err := os.OpenFile(logFiles[i], os.O_RDONLY, 0o644)
  57. if err != nil {
  58. logger.Warning("Failed to open current log file for reading:", logFiles[i], "-", err)
  59. logFilePrev.Close()
  60. continue
  61. }
  62. _, err = io.Copy(logFilePrev, logFile)
  63. if err != nil {
  64. logger.Warning("Failed to copy log file:", logFiles[i], "to", logFilesPrev[i-1], "-", err)
  65. }
  66. logFile.Close()
  67. logFilePrev.Close()
  68. }
  69. err := os.Truncate(logFiles[i], 0)
  70. if err != nil {
  71. logger.Warning("Failed to truncate log file:", logFiles[i], "-", err)
  72. }
  73. }
  74. wipeXrayLogs()
  75. }
  76. func (j *PruneXrayLogsJob) Run() {
  77. truncateXrayLog(xray.GetAccessLogPath, maxXrayLogBytes)
  78. truncateXrayLog(xray.GetErrorLogPath, maxXrayLogBytes)
  79. }
  80. func wipeXrayLogs() {
  81. truncateXrayLog(xray.GetAccessLogPath, 0)
  82. truncateXrayLog(xray.GetErrorLogPath, 0)
  83. }
  84. func truncateXrayLog(pathFn func() (string, error), maxBytes int64) {
  85. logPath, err := pathFn()
  86. if err != nil || disabledLogPath(logPath) {
  87. return
  88. }
  89. if maxBytes > 0 {
  90. info, err := os.Stat(logPath)
  91. if err != nil {
  92. if !os.IsNotExist(err) {
  93. logger.Warning("Failed to stat Xray log:", logPath, "-", err)
  94. }
  95. return
  96. }
  97. if info.Size() <= maxBytes {
  98. return
  99. }
  100. }
  101. if err := os.Truncate(logPath, 0); err != nil && !os.IsNotExist(err) {
  102. logger.Warning("Failed to truncate Xray log:", logPath, "-", err)
  103. }
  104. }
  105. func disabledLogPath(path string) bool {
  106. return path == "" || path == "none"
  107. }