Files
3x-ui/internal/web/job/clear_logs_job_test.go
T
MHSanaei 42cd351e4e refactor(job): drop access log from IP limiting, wipe it daily instead
The IP-limit job tracks per-client IPs via the core's online-stats API; the access-log parser only ran as a fallback for cores predating that API (which the panel never bundles). Remove the parser, the availability check, and the hourly rotation that truncated a log the job no longer reads.

Move the user-enabled access-log wipe to the daily clear-logs job, guarded so a disabled ('none') or missing log is left alone. Retire the now-unwritten 3xipl-ap persistent-log machinery.

Also resolve IP-limit clients via the exact clients/client_inbounds relation instead of a fragile settings LIKE '%email%' substring, keeping the JSON scan only as a fallback (carried from #5496).
2026-06-23 11:42:00 +02:00

56 lines
1.5 KiB
Go

package job
import (
"encoding/json"
"os"
"path/filepath"
"testing"
)
// writeAccessLogConfig points bin/config.json at the given access log path (use
// "none" to disable), so GetAccessLogPath resolves it the way the job does.
func writeAccessLogConfig(t *testing.T, accessPath string) {
t.Helper()
binDir := t.TempDir()
t.Setenv("XUI_BIN_FOLDER", binDir)
configData, err := json.Marshal(map[string]any{
"log": map[string]any{"access": accessPath},
})
if err != nil {
t.Fatalf("marshal xray config: %v", err)
}
if err := os.WriteFile(filepath.Join(binDir, "config.json"), configData, 0644); err != nil {
t.Fatalf("write xray config: %v", err)
}
}
func TestWipeAccessLog_TruncatesEnabledLog(t *testing.T) {
accessLog := filepath.Join(t.TempDir(), "access.log")
if err := os.WriteFile(accessLog, []byte("2026/06/23 12:00:00 from tcp:203.0.113.10:443 accepted\n"), 0644); err != nil {
t.Fatalf("seed access log: %v", err)
}
writeAccessLogConfig(t, accessLog)
wipeAccessLog()
info, err := os.Stat(accessLog)
if err != nil {
t.Fatalf("access log should still exist: %v", err)
}
if info.Size() != 0 {
t.Fatalf("access log should be truncated to 0, got %d bytes", info.Size())
}
}
func TestWipeAccessLog_LeavesDisabledLogAlone(t *testing.T) {
writeAccessLogConfig(t, "none")
// Must not panic or create a file literally named "none".
wipeAccessLog()
if _, err := os.Stat("none"); err == nil {
os.Remove("none")
t.Fatal(`wipeAccessLog must not create a file named "none"`)
}
}