mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-07-21 03:56:07 +00:00
fa1a19c03c
Add .golangci.yml (v2): the standard linters plus bodyclose, errorlint, noctx, misspell, rowserrcheck, sqlclosecheck, unconvert, usestdlibvars, with gofumpt + goimports formatters. Enable the std-error-handling exclusion preset for idiomatic Close/Remove/Setenv ignores; scope-exclude SA1019 (parser.ParseDir in tools/openapigen) and ST1005 (intentional capitalized user-facing error copy that tests assert verbatim). No inline nolint directives were introduced. Resolve all 217 findings behavior-preserving: gofumpt/goimports formatting, explicit blank assignment on intentionally ignored errors, errors.Is/errors.As and %w wrapping, context-aware stdlib calls (CommandContext/QueryContext/NewRequestWithContext/Dialer), staticcheck simplifications, removed redundant conversions, http.StatusOK and http.MethodGet, inlined the go:fix intPtr helper, and deferred sql rows Close. Add a golangci CI job mirroring the existing Go jobs.
95 lines
2.7 KiB
Go
95 lines
2.7 KiB
Go
package job
|
|
|
|
import (
|
|
"io"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"github.com/mhsanaei/3x-ui/v3/internal/logger"
|
|
"github.com/mhsanaei/3x-ui/v3/internal/xray"
|
|
)
|
|
|
|
// ClearLogsJob clears old log files to prevent disk space issues.
|
|
type ClearLogsJob struct{}
|
|
|
|
// NewClearLogsJob creates a new log cleanup job instance.
|
|
func NewClearLogsJob() *ClearLogsJob {
|
|
return new(ClearLogsJob)
|
|
}
|
|
|
|
// 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, 0o755); err != nil {
|
|
return err
|
|
}
|
|
|
|
file, err := os.OpenFile(path, os.O_CREATE|os.O_RDWR, 0o644)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
file.Close()
|
|
return nil
|
|
}
|
|
|
|
// Here Run is an interface method of the Job interface
|
|
func (j *ClearLogsJob) Run() {
|
|
logFiles := []string{xray.GetIPLimitLogPath(), xray.GetIPLimitBannedLogPath()}
|
|
logFilesPrev := []string{xray.GetIPLimitBannedPrevLogPath()}
|
|
|
|
// 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
|
|
for i := range len(logFiles) {
|
|
if i > 0 {
|
|
// Copy to previous logs
|
|
logFilePrev, err := os.OpenFile(logFilesPrev[i-1], os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0o644)
|
|
if err != nil {
|
|
logger.Warning("Failed to open previous log file for writing:", logFilesPrev[i-1], "-", err)
|
|
continue
|
|
}
|
|
|
|
logFile, err := os.OpenFile(logFiles[i], os.O_RDONLY, 0o644)
|
|
if err != nil {
|
|
logger.Warning("Failed to open current log file for reading:", logFiles[i], "-", err)
|
|
logFilePrev.Close()
|
|
continue
|
|
}
|
|
|
|
_, err = io.Copy(logFilePrev, logFile)
|
|
if err != nil {
|
|
logger.Warning("Failed to copy log file:", logFiles[i], "to", logFilesPrev[i-1], "-", err)
|
|
}
|
|
|
|
logFile.Close()
|
|
logFilePrev.Close()
|
|
}
|
|
|
|
err := os.Truncate(logFiles[i], 0)
|
|
if err != nil {
|
|
logger.Warning("Failed to truncate log file:", logFiles[i], "-", err)
|
|
}
|
|
}
|
|
|
|
wipeAccessLog()
|
|
}
|
|
|
|
// wipeAccessLog truncates the user-configured Xray access log so it can't grow
|
|
// unbounded. The IP-limit job no longer reads or rotates it, so this daily wipe
|
|
// is the only thing that caps it. A disabled ("none") or unset access log is
|
|
// left alone, and a missing file is fine — there's nothing to wipe.
|
|
func wipeAccessLog() {
|
|
accessLogPath, err := xray.GetAccessLogPath()
|
|
if err != nil || accessLogPath == "none" || accessLogPath == "" {
|
|
return
|
|
}
|
|
if err := os.Truncate(accessLogPath, 0); err != nil && !os.IsNotExist(err) {
|
|
logger.Warning("Failed to truncate access log:", accessLogPath, "-", err)
|
|
}
|
|
}
|