mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-11-19 03:25:01 +03:00
fix(ip-limit): append fail2ban lines only after the scan commits (#6590)
updateInboundClientIps wrote the [LIMIT_IP] lines that drive the jail while the scan's transaction was still open, and marked the addresses in bannedSeen at the same time. A commit failure after that point rolls the database back but takes nothing back from the log: fail2ban proceeds to ban addresses the panel never recorded, and the in-memory bannedSeen entry makes the next scan skip them, so the rollback is never repaired. Selection stays inside the transaction. processObserved now collects one pendingBan per enforced client and publishes after the commit succeeds, disconnecting only the clients whose lines actually reached the log. The Xray disconnects already ran after the commit for the same reason. Recording moved with the write rather than with the decision: selectAdvancedSinceLastBan no longer mutates anything, and recordBannedSeen runs once a line is on disk. It also runs for clients with nothing to ban, because that is the pass that forgets addresses a client no longer exceeds its limit with - pruning used to be a side effect of the filter, and skipping it left a stale entry that suppressed the next legitimate ban. The log file is opened once per scan instead of once per client, the write error is checked instead of discarded, and Close is reported. updateInboundClientIps no longer reports shouldCleanLog, because the only thing that set it was the ban branch that moved out; processObserved sets it when a publication actually happens. disAllowedIps went with the write it served. Tests: a transaction failed at COMMIT through a deferred foreign key leaves no line and no bannedSeen entry; a publication that cannot open the log leaves the address retryable; a client returning under its limit has its entry forgotten, so going over again is banned a second time; a committed over-limit scan publishes and reports; and writeBanLines surfaces a write error rather than swallowing it.
This commit is contained in:
@@ -24,6 +24,7 @@ var loggerInitOnce sync.Once
|
||||
// updateInboundClientIps can run end to end. closes the db before
|
||||
// TempDir cleanup so windows doesn't complain about the file being in
|
||||
// use.
|
||||
|
||||
func setupIntegrationDB(t *testing.T) {
|
||||
t.Helper()
|
||||
|
||||
@@ -57,6 +58,19 @@ func setupIntegrationDB(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
// enforceIpLimitForTest runs the same two steps processObserved does: select
|
||||
// inside the transaction, publish only once it would have committed.
|
||||
func (j *CheckClientIpJob) enforceIpLimitForTest(t *testing.T, row *model.InboundClientIps, inbound *model.Inbound, email string, limit int, live []IPWithTimestamp, observedAreLive bool) (banned bool, published []IPWithTimestamp) {
|
||||
t.Helper()
|
||||
candidates, keptLive := j.updateInboundClientIps(database.GetDB(), row, inbound, email, limit, live, true, observedAreLive)
|
||||
actionable := j.selectAdvancedSinceLastBan(email, candidates)
|
||||
done := j.publishBans([]pendingBan{{inbound: inbound, email: email, candidates: candidates, keptLive: keptLive}})
|
||||
if len(done) == 0 {
|
||||
return false, nil
|
||||
}
|
||||
return true, actionable
|
||||
}
|
||||
|
||||
// seed an inbound whose settings json has a single client with the
|
||||
// given email and ip limit.
|
||||
func seedInboundWithClient(t *testing.T, tag, email string, limitIp int) {
|
||||
@@ -206,16 +220,13 @@ func TestUpdateInboundClientIps_LiveIpNotBannedByStillFreshHistoricals(t *testin
|
||||
if err != nil {
|
||||
t.Fatalf("getInboundByEmail: %v", err)
|
||||
}
|
||||
shouldCleanLog, banned := j.updateInboundClientIps(database.GetDB(), row, inbound, email, 3, live, true, false)
|
||||
banned, published := j.enforceIpLimitForTest(t, row, inbound, email, 3, live, false)
|
||||
|
||||
if shouldCleanLog {
|
||||
t.Fatalf("shouldCleanLog must be false, nothing should have been banned with 1 live ip under limit 3")
|
||||
}
|
||||
if banned {
|
||||
t.Fatalf("banned must be false with 1 live ip under limit 3")
|
||||
}
|
||||
if len(j.disAllowedIps) != 0 {
|
||||
t.Fatalf("disAllowedIps must be empty, got %v", j.disAllowedIps)
|
||||
if len(published) != 0 {
|
||||
t.Fatalf("published bans must be empty, got %v", published)
|
||||
}
|
||||
|
||||
persisted := ipSet(readClientIps(t, email))
|
||||
@@ -262,16 +273,13 @@ func TestUpdateInboundClientIps_ExcessLiveIpIsStillBanned(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatalf("getInboundByEmail: %v", err)
|
||||
}
|
||||
shouldCleanLog, banned := j.updateInboundClientIps(database.GetDB(), row, inbound, email, 1, live, true, false)
|
||||
banned, published := j.enforceIpLimitForTest(t, row, inbound, email, 1, live, false)
|
||||
|
||||
if !shouldCleanLog {
|
||||
t.Fatalf("shouldCleanLog must be true when the live set exceeds the limit")
|
||||
}
|
||||
if !banned {
|
||||
t.Fatalf("banned must be true when the live set exceeds the limit")
|
||||
}
|
||||
if len(j.disAllowedIps) != 1 || j.disAllowedIps[0] != "10.1.0.1" {
|
||||
t.Fatalf("expected 10.1.0.1 to be banned; disAllowedIps = %v", j.disAllowedIps)
|
||||
if len(published) != 1 || published[0].IP != "10.1.0.1" {
|
||||
t.Fatalf("expected 10.1.0.1 to be banned; published = %v", published)
|
||||
}
|
||||
|
||||
persisted := ipSet(readClientIps(t, email))
|
||||
@@ -446,13 +454,13 @@ func TestUpdateInboundClientIps_AllowlistedIpIsNeitherCountedNorBanned(t *testin
|
||||
if err != nil {
|
||||
t.Fatalf("getInboundByEmail: %v", err)
|
||||
}
|
||||
_, banned := j.updateInboundClientIps(database.GetDB(), row, inbound, email, 1, live, true, false)
|
||||
banned, published := j.enforceIpLimitForTest(t, row, inbound, email, 1, live, false)
|
||||
|
||||
if banned {
|
||||
t.Fatal("an allowlisted address pushed the client over its limit and something was banned")
|
||||
}
|
||||
if len(j.disAllowedIps) != 0 {
|
||||
t.Fatalf("disAllowedIps = %v, want none", j.disAllowedIps)
|
||||
if len(published) != 0 {
|
||||
t.Fatalf("published = %v, want none", published)
|
||||
}
|
||||
|
||||
persisted := ipSet(readClientIps(t, email))
|
||||
|
||||
Reference in New Issue
Block a user